1function C = GB_spec_Matrix_eWiseAdd (C, Mask, accum, add, A, B, descriptor, ignore)
2%GB_SPEC_MATRIX_EWISEADD a MATLAB mimic of GrB_Matrix_eWiseAdd
3%
4% Usage:
5% C = GB_spec_Matrix_eWiseAdd (C, Mask, accum, add, A, B, descriptor)
6%
7% Computes C<Mask> = accum(C,T), in GraphBLAS notation, where T =A+B, A'+B,
8% A+B' or A'+B'.  The pattern of T is the union of A and B.
9
10% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
11% SPDX-License-Identifier: Apache-2.0
12
13%-------------------------------------------------------------------------------
14% get inputs
15%-------------------------------------------------------------------------------
16
17if (nargout > 1 || ~(nargin == 7 || nargin == 8))
18    error ('usage: C = GB_spec_Matrix_eWiseAdd (C, Mask, accum, add, A, B, descriptor)') ;
19end
20
21C = GB_spec_matrix (C) ;
22A = GB_spec_matrix (A) ;
23B = GB_spec_matrix (B) ;
24[add_op optype ztype xtype ytype] = GB_spec_operator (add, C.class) ;
25[C_replace Mask_comp Atrans Btrans Mask_struct] = ...
26    GB_spec_descriptor (descriptor) ;
27Mask = GB_spec_getmask (Mask, Mask_struct) ;
28
29%-------------------------------------------------------------------------------
30% do the work via a clean MATLAB interpretation of the entire GraphBLAS spec
31%-------------------------------------------------------------------------------
32
33% apply the descriptor to A
34if (Atrans)
35    A.matrix = A.matrix.' ;
36    A.pattern = A.pattern' ;
37end
38
39% apply the descriptor to B
40if (Btrans)
41    B.matrix = B.matrix.' ;
42    B.pattern = B.pattern' ;
43end
44
45% T = A+B, with typecasting
46T.matrix = GB_spec_zeros (size (A.matrix), ztype) ;
47p = A.pattern & B.pattern ;
48
49% apply the add to entries in the intersection of A and B
50if (GB_spec_is_positional (add))
51    [m, n] = size (A.matrix) ;
52    for i = 1:m
53        for j = 1:n
54            if (p (i,j))
55                T.matrix (i,j) = GB_spec_binop_positional (add_op, i, j, i, j) ;
56            end
57        end
58    end
59else
60    % cast the entries into the class of the operator
61    A1 = GB_mex_cast (A.matrix (p), xtype) ;
62    B1 = GB_mex_cast (B.matrix (p), ytype) ;
63    % apply the operator
64    T.matrix (p) = GB_spec_op (add, A1, B1) ;
65end
66
67% cast entries in A but not in B, into the result T
68p = A.pattern & ~B.pattern ;
69T.matrix (p) = GB_mex_cast (A.matrix (p), ztype) ;
70
71% cast entries in B but not in A, into the result T
72p = B.pattern & ~A.pattern ;
73T.matrix (p) = GB_mex_cast (B.matrix (p), ztype) ;
74
75% the pattern of T is the union of both A and B
76T.pattern = A.pattern | B.pattern ;
77
78T.class = ztype ;
79
80% C<Mask> = accum (C,T): apply the accum, then Mask, and return the result
81C = GB_spec_accum_mask (C, Mask, accum, T, C_replace, Mask_comp, 0) ;
82
83
84