1function C = accum_mask (C, Mask, accum, T, C_replace, Mask_complement)
2%ACCUM_MASK apply the mask
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7[m n] = size (C.matrix) ;
8Z.matrix  = zeros (m, n) ;
9Z.pattern = false (m, n) ;
10
11if (isempty (accum))
12   Z = T ;     % no accum operator
13else
14   % Z = accum (C,T), like Z=C+T but with an binary operator, accum
15   p =  C.pattern &  T.pattern ; Z.matrix (p) = accum (C.matrix (p), T.matrix (p));
16   p =  C.pattern & ~T.pattern ; Z.matrix (p) = C.matrix (p) ;
17   p = ~C.pattern &  T.pattern ; Z.matrix (p) = T.matrix (p) ;
18   Z.pattern = C.pattern | T.pattern ;
19end
20
21% apply the mask to the values and pattern
22C.matrix  = mask (C.matrix,  Mask, Z.matrix,  C_replace, Mask_complement) ;
23C.pattern = mask (C.pattern, Mask, Z.pattern, C_replace, Mask_complement) ;
24end
25
26function C = mask (C, Mask, Z, C_replace, Mask_complement)
27% replace C if requested
28if (C_replace)
29   C (:,:) = 0 ;
30end
31if (isempty (Mask))             % if empty, Mask is implicit ones(m,n)
32   % implicitly, Mask = ones (size (C))
33   if (~Mask_complement)
34      C = Z ;                   % this is the default
35   else
36      C = C ;                   % Z need never have been computed
37   end
38else
39   % apply the mask
40   if (~Mask_complement)
41      C (Mask) = Z (Mask) ;
42   else
43      C (~Mask) = Z (~Mask) ;
44   end
45end
46end
47
48