1function test141
2%TEST141 test GrB_eWiseAdd (all types and operators) for dense matrices
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 ('test141 ------------ GrB_eWiseAdd with dense matrices\n') ;
12
13m = 5 ;
14n = 5 ;
15
16rng ('default') ;
17
18M = sprand (m, n, 0.5) ;
19
20Amat2 = 2 * sparse (rand (m,n)) ;
21Bmat2 = 2 * sparse (rand (m,n)) ;
22Cmat2 = 2 * sparse (rand (m,n)) ;
23
24Amat = 50 * sparse (rand (m,n)) ;
25Bmat = 50 * sparse (rand (m,n)) ;
26Cmat = 50 * sparse (rand (m,n)) ;
27
28Emat = sprand (m, n, 0.5) ;
29Smat = sparse (m,n) ;
30desc.mask = 'structural' ;
31
32A.matrix = Amat ; A.class = 'see below' ;
33B.matrix = Bmat ; B.class = 'see below' ;
34C.matrix = Cmat ; C.class = 'see below' ;
35S.matrix = Smat ; S.class = 'see below' ;
36E.matrix = Emat ; E.class = 'see below' ;
37
38for k2 = 1:length(binops)
39    binop = binops {k2}  ;
40    if (isequal (binop, 'pow'))
41        continue ;
42    end
43    fprintf ('\n%-10s ', binop) ;
44
45    for k1 = 1:length (types)
46        type = types {k1}  ;
47        op.opname = binop ;
48        op.optype = type ;
49
50        try
51            GB_spec_operator (op) ;
52        catch
53            continue ;
54        end
55
56        fprintf ('.') ;
57
58        switch (binop)
59            % domain is ok, but limit it to avoid integer typecast
60            % failures from O(eps) errors, or overflow to inf
61            case { 'ldexp', 'pow' }
62                A.matrix = Amat2 ;
63                B.matrix = Bmat2 ;
64                C.matrix = Cmat2 ;
65            otherwise
66                % no change
67        end
68
69        A.class = type ;
70        B.class = type ;
71        E.class = type ;
72
73        if (k2 > 20)
74            % eq, ne, gt, lt, ge, le
75            S.class = 'logical' ;
76            C.class = 'logical' ;
77        else
78            S.class = type ;
79            C.class = type ;
80        end
81
82        %---------------------------------------
83        % C = A+B
84        %---------------------------------------
85
86        C0 = GB_spec_Matrix_eWiseAdd (S, [ ], [ ], op, A, B, [ ]) ;
87        C1 = GB_mex_Matrix_eWiseAdd  (S, [ ], [ ], op, A, B, [ ]) ;
88        GB_spec_compare (C0, C1) ;
89
90        %---------------------------------------
91        % C<M> = A+B, both A and B dense
92        %---------------------------------------
93
94        C0 = GB_spec_Matrix_eWiseAdd (S, M, [ ], op, A, B, desc) ;
95        C1 = GB_mex_Matrix_eWiseAdd  (S, M, [ ], op, A, B, desc) ;
96        GB_spec_compare (C0, C1) ;
97
98        %---------------------------------------
99        % C<M> = A+E, A dense, E sparse
100        %---------------------------------------
101
102        C0 = GB_spec_Matrix_eWiseAdd (S, M, [ ], op, A, E, desc) ;
103        C1 = GB_mex_Matrix_eWiseAdd  (S, M, [ ], op, A, E, desc) ;
104        GB_spec_compare (C0, C1) ;
105
106        %---------------------------------------
107        % C<M> = E+A, A dense, E sparse
108        %---------------------------------------
109
110        C0 = GB_spec_Matrix_eWiseAdd (S, M, [ ], op, E, A, desc) ;
111        C1 = GB_mex_Matrix_eWiseAdd  (S, M, [ ], op, E, A, desc) ;
112        GB_spec_compare (C0, C1) ;
113
114        %---------------------------------------
115        % C += A+B
116        %---------------------------------------
117
118        if (~GB_spec_is_positional (op))
119            C0 = GB_spec_Matrix_eWiseAdd (C, [ ], op, op, A, B, [ ]) ;
120            C1 = GB_mex_Matrix_eWiseAdd  (C, [ ], op, op, A, B, [ ]) ;
121            GB_spec_compare (C0, C1) ;
122        end
123    end
124end
125
126fprintf ('\ntest141: all tests passed\n') ;
127
128