1function test175
2%TEST175 test GrB_assign with accum
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7[binops, ~, ~, types, ~, ~] = GB_spec_opsall ;
8binops = binops.all ;
9types = types.all ;
10
11fprintf ('test175 ------------ GrB_assign with accum\n') ;
12
13m = 10 ;
14n = 14 ;
15
16rng ('default') ;
17
18M1.matrix = logical (spones (sprand (m, n, 0.5))) ;
19M1.sparsity = 2 ; % sparse
20
21M2.matrix = logical (spones (sprand (m, n, 0.5))) ;
22M2.sparsity = 4 ; % sparse
23
24Amat2 = sparse (2 * rand (m,n)) ;
25Bmat2 = sparse (2 * sprand (m,n, 0.5)) ;
26Cmat2 = sparse (2 * rand (m,n)) ;
27
28Amat = 5000 * Amat2 ;
29B1mat = 5000 * Bmat2 ;
30Cmat = 5000 * Cmat2 ;
31
32B2mat = rand (m,n) ;
33
34Smat = sparse (m,n) ;
35Xmat = sparse (pi) ;
36desc.mask = 'structural' ;
37drep.outp = 'replace' ;
38
39for k1 = 1:length (types)
40    type = types {k1}  ;
41    fprintf ('%s, ', type) ;
42
43    clear A B1 B2 C S X
44    A.matrix = Amat   ; A.class = 'see below' ;
45    B1.matrix = B1mat ; B1.class = 'see below' ;
46    B2.matrix = B2mat ; B2.class = 'see below' ;
47    C.matrix = Cmat   ; C.class = 'see below' ;
48    S.matrix = Smat   ; S.class = 'see below' ;
49    X.matrix = Xmat   ; X.class = 'see below' ;
50    A.class = type ;
51
52    for k3 = 1:3
53
54    if (k3 == 1)
55        X.class = type ;
56        B1.class = type ;
57        B2.class = type ;
58        C.class = 'logical' ;
59        S.class = 'logical' ;
60    elseif (k3 == 2)
61        X.class = type ;
62        B1.class = type ;
63        B2.class = type ;
64        C.class = type ;
65        S.class = type ;
66    else
67        X.class = 'int8' ;
68        B1.class = 'int8' ;
69        B2.class = 'int8' ;
70        C.class = type ;
71        S.class = type ;
72    end
73
74    for k4 = 1:7
75
76        if (k4 == 1)
77            M = M1 ;
78            B = B1 ;
79            C.sparsity = 2 ;
80            B.sparsity = 2 ;
81        elseif (k4 == 2)
82            M = M2 ;
83            B = B1 ;
84            C.sparsity = 2 ;
85            B.sparsity = 2 ;
86        elseif (k4 == 3)
87            M = M1 ;
88            B = B1 ;
89            C.sparsity = 4 ;
90            B.sparsity = 2 ;
91        elseif (k4 == 4)
92            M = M2 ;
93            B = B1 ;
94            C.sparsity = 4 ;
95            B.sparsity = 2 ;
96        elseif (k4 == 5)
97            M = M2 ;
98            B = B1 ;
99            C.sparsity = 4 ;
100            B.sparsity = 4 ;
101        elseif (k4 == 6)
102            M = M2 ;
103            B = B2 ;
104            C.sparsity = 4 ;
105            B.sparsity = 4 ;
106        elseif (k4 == 7)
107            M = M2 ;
108            B = B2 ;
109            C.sparsity = 2 ;
110            B.sparsity = 4 ;
111        end
112
113        %---------------------------------------
114        % C<M> = A where A is dense
115        %---------------------------------------
116
117        C0 = GB_spec_assign (C, M, [ ], A, [ ], [ ], [ ], false) ;
118        C1 = GB_mex_assign  (C, M, [ ], A, [ ], [ ], [ ]) ;
119        GB_spec_compare (C0, C1) ;
120
121        %---------------------------------------
122        % C<M> = B where B is sparse
123        %---------------------------------------
124
125        C0 = GB_spec_assign (C, M, [ ], B, [ ], [ ], [ ], false) ;
126        C1 = GB_mex_assign  (C, M, [ ], B, [ ], [ ], [ ]) ;
127        GB_spec_compare (C0, C1) ;
128
129        %---------------------------------------
130        % C<M> = A where A is dense and C starts empty
131        %---------------------------------------
132
133        C0 = GB_spec_assign (S, M, [ ], A, [ ], [ ], desc, false) ;
134        C1 = GB_mex_assign  (S, M, [ ], A, [ ], [ ], desc) ;
135        GB_spec_compare (C0, C1) ;
136
137        %---------------------------------------
138        % C<M> = x where C is dense
139        %---------------------------------------
140
141        C0 = GB_spec_assign (C, M, [ ], X, [ ], [ ], [ ], true) ;
142        C1 = GB_mex_assign  (C, M, [ ], X, [ ], [ ], [ ]) ;
143        GB_spec_compare (C0, C1) ;
144
145        %---------------------------------------
146        % C<M> = x where C is dense
147        %---------------------------------------
148
149        C0 = GB_spec_assign (C, M, [ ], X, [ ], [ ], desc, true) ;
150        C1 = GB_mex_assign  (C, M, [ ], X, [ ], [ ], desc) ;
151        GB_spec_compare (C0, C1) ;
152
153        %---------------------------------------
154        % C<M,struct> = x
155        %---------------------------------------
156
157        C0 = GB_spec_assign (S, M, [ ], X, [ ], [ ], desc, true) ;
158        C1 = GB_mex_assign  (S, M, [ ], X, [ ], [ ], desc) ;
159        GB_spec_compare (C0, C1) ;
160
161    end
162
163    %---------------------------------------
164    % C = x
165    %---------------------------------------
166
167    C0 = GB_spec_assign (S, [ ], [ ], X, [ ], [ ], [ ], true) ;
168    C1 = GB_mex_assign  (S, [ ], [ ], X, [ ], [ ], [ ]) ;
169    GB_spec_compare (C0, C1) ;
170
171    %---------------------------------------
172    % with accum operators
173    %---------------------------------------
174
175    clear A B C
176
177    for k2 = 1:length(binops)
178        binop = binops {k2}  ;
179
180        tol = 0 ;
181        switch (binop)
182            case { 'pow', 'atan2', 'hypot', 'remainder' }
183                A.matrix = Amat2 ;
184                B.matrix = Bmat2 ;
185                C.matrix = Cmat2 ;
186                if (contains (type, 'single'))
187                    tol = 1e-5 ;
188                elseif (contains (type, 'double'))
189                    tol = 1e-12 ;
190                end
191            otherwise
192                A.matrix = Amat ;
193                B.matrix = B1mat ;
194                C.matrix = Cmat ;
195        end
196
197        accum.opname = binop ;
198        accum.optype = type ;
199
200        try
201            GB_spec_operator (accum) ;
202        catch
203            continue
204        end
205
206        if (GB_spec_is_positional (accum))
207            continue ;
208        end
209
210        %---------------------------------------
211        % C += A where A is dense
212        %---------------------------------------
213
214        C0 = GB_spec_assign (C, [ ], accum, A, [ ], [ ], [ ], false) ;
215        C1 = GB_mex_assign  (C, [ ], accum, A, [ ], [ ], [ ]) ;
216        GB_spec_compare (C0, C1, 0, tol) ;
217
218        %---------------------------------------
219        % C += B where B is sparse
220        %---------------------------------------
221
222        C0 = GB_spec_assign (C, [ ], accum, B, [ ], [ ], [ ], false) ;
223        C1 = GB_mex_assign  (C, [ ], accum, B, [ ], [ ], [ ]) ;
224        GB_spec_compare (C0, C1, 0, tol) ;
225
226        %---------------------------------------
227        % C += x
228        %---------------------------------------
229
230        C0 = GB_spec_assign (C, [ ], accum, X, [ ], [ ], [ ], true) ;
231        C1 = GB_mex_assign  (C, [ ], accum, X, [ ], [ ], [ ]) ;
232        GB_spec_compare (C0, C1, 0, tol) ;
233
234        %---------------------------------------
235        % C<replace> += x
236        %---------------------------------------
237
238        C0 = GB_spec_assign (C, [ ], accum, X, [ ], [ ], drep, true) ;
239        C1 = GB_mex_subassign  (C, [ ], accum, X, [ ], [ ], drep) ;
240        GB_spec_compare (C0, C1, 0, tol) ;
241
242    end
243end
244end
245
246fprintf ('\ntest175: all tests passed\n') ;
247
248