1function test127
2%TEST127 test GrB_eWiseAdd and GrB_eWiseMult (all types and operators)
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 ('test127 -----------tests of GrB_eWiseAdd and eWiseMult (all ops)\n') ;
12
13m = 5 ;
14n = 5 ;
15
16rng ('default') ;
17
18dnn = struct ;
19dtn = struct ( 'inp0', 'tran' ) ;
20dnt = struct ( 'inp1', 'tran' ) ;
21dtt = struct ( 'inp0', 'tran', 'inp1', 'tran' ) ;
22dnn_notM = struct ('mask', 'complement') ;
23
24Amat2 = sparse (2 * sprand (m,n, 0.8)) ;
25Bmat2 = sparse (2 * sprand (m,n, 0.8)) ;
26Cmat2 = sparse (2 * sprand (m,n, 0.8)) ;
27w2 = sparse (2 * sprand (m,1, 0.8)) ;
28uvec2 = sparse (2 * sprand (m,1, 0.8)) ;
29vvec2 = sparse (2 * sprand (m,1, 0.8)) ;
30
31Amat = sparse (100 * sprandn (m,n, 0.8)) ;
32Bmat = sparse (100 * sprandn (m,n, 0.8)) ;
33Cmat = sparse (100 * sprandn (m,n, 0.8)) ;
34w = sparse (100 * sprandn (m,1, 0.8)) ;
35uvec = sparse (100 * sprandn (m,1, 0.8)) ;
36vvec = sparse (100 * sprandn (m,1, 0.8)) ;
37
38Maskmat = sprandn (m,n,0.9) ~= 0 ;
39maskvec = sprandn (m,1,0.9) ~= 0 ;
40
41% create a very sparse matrix mask
42Maskmat2 = sparse (m,n) ;
43T = Amat .* Bmat ;
44[i j x] = find (T) ;
45if (length (i) > 0)
46    Maskmat2 (i(1), j(1)) = 1 ;
47end
48T = (Amat ~= 0) & (Bmat == 0) ;
49[i j x] = find (T) ;
50if (length (i) > 0)
51    Maskmat2 (i(1), j(1)) = 1 ;
52end
53T = (Amat == 0) & (Bmat ~= 0) ;
54[i j x] = find (T) ;
55if (length (i) > 0)
56    Maskmat2 (i(1), j(1)) = 1 ;
57end
58clear T i j x
59
60% create a very sparse vector mask
61maskvec2 = sparse (m,1) ;
62T = uvec .* vvec ;
63[i j x] = find (T) ;
64if (length (i) > 0)
65    maskvec2 (i(1), j(1)) = 1 ;
66end
67T = (uvec ~= 0) & (vvec == 0) ;
68[i j x] = find (T) ;
69if (length (i) > 0)
70    maskvec2 (i(1), j(1)) = 1 ;
71end
72T = (uvec == 0) & (vvec ~= 0) ;
73[i j x] = find (T) ;
74if (length (i) > 0)
75    maskvec2 (i(1), j(1)) = 1 ;
76end
77clear T i j x
78
79ATmat = Amat' ;
80BTmat = Bmat' ;
81
82ATmat2 = Amat2.' ;
83BTmat2 = Bmat2.' ;
84
85for k1 = 1:length (types)
86    type = types {k1}  ;
87    fprintf ('\n\n%-8s : ', type) ;
88
89    for k2 = 1:length(binops)
90        binop = binops {k2}  ;
91
92        op.opname = binop ;
93        op.optype = type ;
94
95        if (contains (type, 'single'))
96            tol = 1e-5 ;
97        elseif (contains (type, 'double'))
98            tol = 1e-12 ;
99        else
100            tol = 0 ;
101        end
102
103        try
104            GB_spec_operator (op) ;
105        catch
106            continue ;
107        end
108
109        fprintf (' %s', binop) ;
110
111        for A_sparsity_control = 0:1
112        for A_is_csc   = 0 % 0:1
113        for B_sparsity_control = 0:1
114        for B_is_csc   = 0 % 0:1
115        for C_sparsity_control = 0:1
116        for C_is_csc   = 0 % 0:1
117
118        if (A_sparsity_control == 0)
119            A_is_hyper = 0 ; % not hyper
120            A_sparsity = 1 ; % sparse
121        else
122            A_is_hyper = 0 ; % not hyper
123            A_sparsity = 4 ; % bitmap
124        end
125
126        if (B_sparsity_control == 0)
127            B_is_hyper = 0 ; % not hyper
128            B_sparsity = 1 ; % sparse
129        else
130            B_is_hyper = 0 ; % not hyper
131            B_sparsity = 4 ; % bitmap
132        end
133
134        if (C_sparsity_control == 0)
135            C_is_hyper = 0 ; % not hyper
136            C_sparsity = 1 ; % sparse
137        else
138            C_is_hyper = 0 ; % not hyper
139            C_sparsity = 4 ; % bitmap
140        end
141
142        for native = 1 % 0:1
143
144        clear A AT B BT C u v
145
146        if (isequal (binop, 'pow'))
147            A.matrix = Amat2 ;
148            AT.matrix = ATmat2 ;
149            B.matrix = Bmat2 ;
150            BT.matrix = BTmat2 ;
151            C.matrix = Cmat2 ;
152            u.matrix = uvec2 ;
153            v.matrix = vvec2 ;
154        else
155            A.matrix = Amat ;
156            AT.matrix = ATmat ;
157            B.matrix = Bmat ;
158            BT.matrix = BTmat ;
159            C.matrix = Cmat ;
160            u.matrix = uvec ;
161            v.matrix = vvec ;
162        end
163
164        A.is_hyper = A_is_hyper ;
165        A.is_csc   = A_is_csc   ;
166        A.sparsity = A_sparsity ;
167        if (native)
168            A.class = op.optype ;
169        end
170
171        AT.is_hyper = A_is_hyper ;
172        AT.sparsity = A_sparsity ;
173        AT.is_csc   = A_is_csc   ;
174        if (native)
175            AT.class = op.optype ;
176        end
177
178        B.is_hyper = B_is_hyper ;
179        B.sparsity = B_sparsity ;
180        B.is_csc   = B_is_csc   ;
181        if (native)
182            B.class = op.optype ;
183        end
184
185        BT.is_hyper = B_is_hyper ;
186        BT.sparsity = B_sparsity ;
187        BT.is_csc   = B_is_csc   ;
188        if (native)
189            BT.class = op.optype ;
190        end
191
192        C.is_hyper = C_is_hyper ;
193        C.is_csc   = C_is_csc   ;
194        C.sparsity = C_sparsity ;
195
196        u.is_csc = true ;
197        if (native)
198            u.class = op.optype ;
199        end
200
201        v.is_csc = true ;
202        if (native)
203            v.class = op.optype ;
204        end
205
206        %---------------------------------------
207        % A+B
208        %---------------------------------------
209
210        C0 = GB_spec_Matrix_eWiseAdd (C, [ ], [ ], op, A, B, dnn) ;
211        C1 = GB_mex_Matrix_eWiseAdd  (C, [ ], [ ], op, A, B, dnn) ;
212        GB_spec_compare (C0, C1, 0, tol) ;
213
214        w0 = GB_spec_Vector_eWiseAdd (w, [ ], [ ], op, u, v, dnn) ;
215        w1 = GB_mex_Vector_eWiseAdd  (w, [ ], [ ], op, u, v, dnn) ;
216        GB_spec_compare (w0, w1, 0, tol) ;
217
218        %---------------------------------------
219        % A'+B
220        %---------------------------------------
221
222        C0 = GB_spec_Matrix_eWiseAdd (C, [ ], [ ], op, AT, B, dtn) ;
223        C1 = GB_mex_Matrix_eWiseAdd  (C, [ ], [ ], op, AT, B, dtn) ;
224        GB_spec_compare (C0, C1, 0, tol) ;
225
226        %---------------------------------------
227        % A+B'
228        %---------------------------------------
229
230        C0 = GB_spec_Matrix_eWiseAdd (C, [ ], [ ], op, A, BT, dnt) ;
231        C1 = GB_mex_Matrix_eWiseAdd  (C, [ ], [ ], op, A, BT, dnt) ;
232        GB_spec_compare (C0, C1, 0, tol) ;
233
234        %---------------------------------------
235        % A'+B'
236        %---------------------------------------
237
238        C0 = GB_spec_Matrix_eWiseAdd (C, [ ], [ ], op, AT, BT, dtt) ;
239        C1 = GB_mex_Matrix_eWiseAdd  (C, [ ], [ ], op, AT, BT, dtt) ;
240        GB_spec_compare (C0, C1, 0, tol) ;
241
242        %---------------------------------------
243        % A.*B
244        %---------------------------------------
245
246        C0 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op, A, B, dnn) ;
247        C1 = GB_mex_Matrix_eWiseMult  (C, [ ], [ ], op, A, B, dnn) ;
248        GB_spec_compare (C0, C1, 0, tol) ;
249
250        w0 = GB_spec_Vector_eWiseMult (w, [ ], [ ], op, u, v, dnn) ;
251        w1 = GB_mex_Vector_eWiseMult  (w, [ ], [ ], op, u, v, dnn) ;
252        GB_spec_compare (w0, w1, 0, tol) ;
253
254        %---------------------------------------
255        % A'.*B
256        %---------------------------------------
257
258        C0 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op, AT, B, dtn) ;
259        C1 = GB_mex_Matrix_eWiseMult  (C, [ ], [ ], op, AT, B, dtn) ;
260        GB_spec_compare (C0, C1, 0, tol) ;
261
262        %---------------------------------------
263        % B.*A'
264        %---------------------------------------
265
266        C0 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op, B, AT, dnt) ;
267        C1 = GB_mex_Matrix_eWiseMult  (C, [ ], [ ], op, B, AT, dnt) ;
268        GB_spec_compare (C0, C1, 0, tol) ;
269
270        %---------------------------------------
271        % A.*B'
272        %---------------------------------------
273
274        C0 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op, A, BT, dnt) ;
275        C1 = GB_mex_Matrix_eWiseMult  (C, [ ], [ ], op, A, BT, dnt) ;
276        GB_spec_compare (C0, C1, 0, tol) ;
277
278        %---------------------------------------
279        % A'.*B'
280        %---------------------------------------
281
282        C0 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op, AT, BT, dtt) ;
283        C1 = GB_mex_Matrix_eWiseMult  (C, [ ], [ ], op, AT, BT, dtt) ;
284        GB_spec_compare (C0, C1, 0, tol) ;
285
286        %-----------------------------------------------
287        % with mask
288        %-----------------------------------------------
289
290        for M_is_very_sparse = 0 % 0:1
291        % for M_is_hyper = 0 % 0:1
292        for M_sparsity_control = 0:1
293        for M_is_csc   = 0 % 0:1
294
295        clear Mask mask
296        if (M_is_very_sparse)
297            Mask.matrix = Maskmat2 ;
298            mask.matrix = maskvec2 ;
299        else
300            Mask.matrix = Maskmat ;
301            mask.matrix = maskvec ;
302        end
303
304        if (M_sparsity_control == 0)
305            M_is_hyper = 0 ; % not hyper
306            M_sparsity = 1 ; % sparse
307        else
308            M_is_hyper = 0 ; % not hyper
309            M_sparsity = 4 ; % bitmap
310        end
311
312        Mask.is_hyper = M_is_hyper ;
313        Mask.sparsity = M_sparsity ;
314        Mask.is_csc   = M_is_csc   ;
315        mask.is_csc = true ;
316
317        %---------------------------------------
318        % A+B, with mask
319        %---------------------------------------
320
321        C0 = GB_spec_Matrix_eWiseAdd (C, Mask, [ ], op, A, B, dnn) ;
322        C1 = GB_mex_Matrix_eWiseAdd  (C, Mask, [ ], op, A, B, dnn) ;
323        GB_spec_compare (C0, C1, 0, tol) ;
324
325        w0 = GB_spec_Vector_eWiseAdd (w, mask, [ ], op, u, v, dnn) ;
326        w1 = GB_mex_Vector_eWiseAdd  (w, mask, [ ], op, u, v, dnn) ;
327        GB_spec_compare (w0, w1, 0, tol) ;
328
329        %---------------------------------------
330        % A+B, with mask complemented
331        %---------------------------------------
332
333        C0 = GB_spec_Matrix_eWiseAdd (C, Mask, [ ], op, A, B, dnn_notM) ;
334        C1 = GB_mex_Matrix_eWiseAdd  (C, Mask, [ ], op, A, B, dnn_notM) ;
335        GB_spec_compare (C0, C1, 0, tol) ;
336
337        w0 = GB_spec_Vector_eWiseAdd (w, mask, [ ], op, u, v, dnn_notM) ;
338        w1 = GB_mex_Vector_eWiseAdd  (w, mask, [ ], op, u, v, dnn_notM) ;
339        GB_spec_compare (w0, w1, 0, tol) ;
340
341        %---------------------------------------
342        % A'+B, with mask
343        %---------------------------------------
344
345        C0 = GB_spec_Matrix_eWiseAdd (C, Mask, [ ], op, AT, B, dtn) ;
346        C1 = GB_mex_Matrix_eWiseAdd  (C, Mask, [ ], op, AT, B, dtn) ;
347        GB_spec_compare (C0, C1, 0, tol) ;
348
349        %---------------------------------------
350        % A+B', with mask
351        %---------------------------------------
352
353        C0 = GB_spec_Matrix_eWiseAdd (C, Mask, [ ], op, A, BT, dnt) ;
354        C1 = GB_mex_Matrix_eWiseAdd  (C, Mask, [ ], op, A, BT, dnt) ;
355        GB_spec_compare (C0, C1, 0, tol) ;
356
357        %---------------------------------------
358        % A'+B', with mask
359        %---------------------------------------
360
361        C0 = GB_spec_Matrix_eWiseAdd (C, Mask, [ ], op, AT, BT, dtt) ;
362        C1 = GB_mex_Matrix_eWiseAdd  (C, Mask, [ ], op, AT, BT, dtt) ;
363        GB_spec_compare (C0, C1, 0, tol) ;
364
365        %---------------------------------------
366        % A.*B, with mask
367        %---------------------------------------
368
369        C0 = GB_spec_Matrix_eWiseMult (C, Mask, [ ], op, A, B, dnn) ;
370        C1 = GB_mex_Matrix_eWiseMult  (C, Mask, [ ], op, A, B, dnn) ;
371        GB_spec_compare (C0, C1, 0, tol) ;
372
373        w0 = GB_spec_Vector_eWiseMult (w, mask, [ ], op, u, v, dnn) ;
374        w1 = GB_mex_Vector_eWiseMult  (w, mask, [ ], op, u, v, dnn) ;
375        GB_spec_compare (w0, w1, 0, tol) ;
376
377        %---------------------------------------
378        % A'.*B, with mask
379        %---------------------------------------
380
381        C0 = GB_spec_Matrix_eWiseMult (C, Mask, [ ], op, AT, B, dtn) ;
382        C1 = GB_mex_Matrix_eWiseMult  (C, Mask, [ ], op, AT, B, dtn) ;
383        GB_spec_compare (C0, C1, 0, tol) ;
384
385        %---------------------------------------
386        % A.*B', with mask
387        %---------------------------------------
388
389        C0 = GB_spec_Matrix_eWiseMult (C, Mask, [ ], op, A, BT, dnt) ;
390        C1 = GB_mex_Matrix_eWiseMult  (C, Mask, [ ], op, A, BT, dnt) ;
391        GB_spec_compare (C0, C1, 0, tol) ;
392
393        %---------------------------------------
394        % A'.*B', with mask
395        %---------------------------------------
396
397        C0 = GB_spec_Matrix_eWiseMult (C, Mask, [ ], op, AT, BT, dtt) ;
398        C1 = GB_mex_Matrix_eWiseMult  (C, Mask, [ ], op, AT, BT, dtt) ;
399        GB_spec_compare (C0, C1, 0, tol) ;
400
401        %---------------------------------------
402        % A.*B, with mask complemented
403        %---------------------------------------
404
405        C0 = GB_spec_Matrix_eWiseMult (C, Mask, [ ], op, A, B, dnn_notM) ;
406        C1 = GB_mex_Matrix_eWiseMult  (C, Mask, [ ], op, A, B, dnn_notM) ;
407        GB_spec_compare (C0, C1, 0, tol) ;
408
409        w0 = GB_spec_Vector_eWiseMult (w, mask, [ ], op, u, v, dnn_notM) ;
410        w1 = GB_mex_Vector_eWiseMult  (w, mask, [ ], op, u, v, dnn_notM) ;
411        GB_spec_compare (w0, w1, 0, tol) ;
412
413
414        end
415        end
416        end
417        end
418        end
419        end
420        end
421        end
422        end
423        end
424    end
425end
426
427fprintf ('\ntest127: all tests passed\n') ;
428
429