1function w = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, descriptor) 2%GB_SPEC_REDUCE_TO_VECTOR a MATLAB mimic of GrB_reduce (to vector) 3% 4% Usage: 5% w = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, desc) 6% 7% Reduces a matrix to a vector 8 9% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 10% SPDX-License-Identifier: Apache-2.0 11 12%------------------------------------------------------------------------------- 13% get inputs 14%------------------------------------------------------------------------------- 15 16if (nargout > 1 || nargin ~= 6) 17 error ... 18 ('usage: c = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, desc)') ; 19end 20 21% get the class of A 22if (isstruct (A)) 23 aclass = A.class ; 24else 25 aclass = GB_spec_type (A) ; 26end 27 28% get the reduce operator. default type is the type of A 29if (isempty (reduce)) 30 reduce = 'plus' 31end 32[reduce_op reduce_class] = GB_spec_operator (reduce, aclass) ; 33 34if (GB_spec_is_positional (reduce_op)) 35 error ('reduce op must not be positional') ; 36end 37 38% get the identity 39identity = GB_spec_identity (reduce_op, reduce_class) ; 40if (isempty (identity)) 41 identity = 0 ; 42end 43 44% get the input matrix 45A = GB_spec_matrix (A, identity) ; 46 47% get the input vector 48w = GB_spec_matrix (w, identity) ; 49 50% get the mask 51[C_replace Mask_comp Atrans Btrans Mask_struct] = ... 52 GB_spec_descriptor (descriptor) ; 53mask = GB_spec_getmask (mask, Mask_struct) ; 54 55%------------------------------------------------------------------------------- 56% do the work via a clean MATLAB interpretation of the entire GraphBLAS spec 57%------------------------------------------------------------------------------- 58 59% apply the descriptor to A 60if (Atrans) 61 A.matrix = A.matrix.' ; 62 A.pattern = A.pattern' ; 63end 64 65tclass = reduce_class ; 66[m n] = size (A.matrix) ; 67T.matrix = GB_spec_zeros ([m 1], tclass) ; 68T.pattern = zeros (m, 1, 'logical') ; 69T.matrix (:,:) = identity ; 70T.class = tclass ; 71 72% cast A to the type of the reduce operator, but only entries in the pattern 73 74% Note that GraphBLAS does not need the identity value to do this step. 75% Nor is the identity value specified in the GraphBLAS spec. 76% It is needed here since T is a dense column, not sparse, and the 77% implicit values need to be inserted properly. 78 79for i = 1:m 80 t = identity ; 81 for j = 1:n 82 if (A.pattern (i,j)) 83 aij = GB_mex_cast (A.matrix (i,j), reduce_class) ; 84 t = GB_spec_op (reduce, t, aij) ; 85 T.pattern (i) = true ; 86 end 87 end 88 T.matrix (i) = t ; 89end 90 91%------------------------------------------------------------------------------- 92% apply the mask and accum 93%------------------------------------------------------------------------------- 94 95% w<mask> = accum (C,T): apply the accum, then mask, and return the result 96w = GB_spec_accum_mask (w, mask, accum, T, C_replace, Mask_comp, identity) ; 97 98