1function test142
2%TEST142 test GrB_assign 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 ('test142 ------------ GrB_assign with dense matrices\n') ;
12
13m = 10 ;
14n = 12 ;
15
16rng ('default') ;
17
18M = sprand (m, n, 0.5) ;
19
20Amat2 = sparse (2 * rand (m,n)) ;
21Bmat2 = sparse (2 * sprand (m,n, 0.5)) ;
22Cmat2 = sparse (2 * rand (m,n)) ;
23
24Amat = 50 * Amat2 ;
25Bmat = 50 * Bmat2 ;
26Cmat = 50 * Cmat2 ;
27
28Smat = sparse (m,n) ;
29Xmat = sparse (pi) ;
30desc.mask = 'structural' ;
31drep.outp = 'replace' ;
32
33A.matrix = Amat ; A.class = 'see below' ;
34B.matrix = Bmat ; B.class = 'see below' ;
35C.matrix = Cmat ; C.class = 'see below' ;
36S.matrix = Smat ; S.class = 'see below' ;
37X.matrix = Xmat ; X.class = 'see below' ;
38Bmask = logical (Bmat) ;
39
40for k1 = 1:length (types)
41    type = types {k1}  ;
42    fprintf ('%s ', type) ;
43
44    A.class = type ;
45
46    for k3 = 1:3
47
48        if (k3 == 1)
49            X.class = type ;
50            B.class = type ;
51            C.class = 'logical' ;
52            S.class = 'logical' ;
53        elseif (k3 == 2)
54            X.class = type ;
55            B.class = type ;
56            C.class = type ;
57            S.class = type ;
58        else
59            X.class = 'int8' ;
60            B.class = 'int8' ;
61            C.class = type ;
62            S.class = type ;
63        end
64
65        %---------------------------------------
66        % C<M> = A where A is dense
67        %---------------------------------------
68
69        C0 = GB_spec_assign (C, M, [ ], A, [ ], [ ], [ ], false) ;
70        C1 = GB_mex_assign  (C, M, [ ], A, [ ], [ ], [ ]) ;
71        GB_spec_compare (C0, C1) ;
72
73        %---------------------------------------
74        % C<M> = B where B is sparse
75        %---------------------------------------
76
77        C0 = GB_spec_assign (C, M, [ ], B, [ ], [ ], [ ], false) ;
78        C1 = GB_mex_assign  (C, M, [ ], B, [ ], [ ], [ ]) ;
79        GB_spec_compare (C0, C1) ;
80
81        %---------------------------------------
82        % C<M> = A where A is dense and C starts empty
83        %---------------------------------------
84
85        C0 = GB_spec_assign (S, M, [ ], A, [ ], [ ], desc, false) ;
86        C1 = GB_mex_assign  (S, M, [ ], A, [ ], [ ], desc) ;
87        GB_spec_compare (C0, C1) ;
88
89        %---------------------------------------
90        % C<B> = B where B is sparse
91        %---------------------------------------
92
93        C0 = GB_spec_assign (C, Bmask, [ ], B, [ ], [ ], desc, false) ;
94        C1 = GB_mex_assign_alias_mask (C, B, desc) ;
95        GB_spec_compare (C0, C1) ;
96
97        %---------------------------------------
98        % C<M> = x where C is dense
99        %---------------------------------------
100
101        C0 = GB_spec_assign (C, M, [ ], X, [ ], [ ], [ ], true) ;
102        C1 = GB_mex_assign  (C, M, [ ], X, [ ], [ ], [ ]) ;
103        GB_spec_compare (C0, C1) ;
104
105        %---------------------------------------
106        % C<M> = x where C is dense
107        %---------------------------------------
108
109        C0 = GB_spec_assign (C, M, [ ], X, [ ], [ ], desc, true) ;
110        C1 = GB_mex_assign  (C, M, [ ], X, [ ], [ ], desc) ;
111        GB_spec_compare (C0, C1) ;
112
113        %---------------------------------------
114        % C<M,struct> = x
115        %---------------------------------------
116
117        C0 = GB_spec_assign (S, M, [ ], X, [ ], [ ], desc, true) ;
118        C1 = GB_mex_assign  (S, M, [ ], X, [ ], [ ], desc) ;
119        GB_spec_compare (C0, C1) ;
120
121        %---------------------------------------
122        % C = x
123        %---------------------------------------
124
125        C0 = GB_spec_assign (S, [ ], [ ], X, [ ], [ ], [ ], true) ;
126        C1 = GB_mex_assign  (S, [ ], [ ], X, [ ], [ ], [ ]) ;
127        GB_spec_compare (C0, C1) ;
128
129        %---------------------------------------
130        % with accum operators
131        %---------------------------------------
132
133        for k2 = 1:length(binops)
134            binop = binops {k2}  ;
135
136            tol = 0 ;
137            switch (binop)
138                case { 'pow', 'atan2', 'hypot', 'remainder' }
139                    A.matrix = Amat2 ;
140                    B.matrix = Bmat2 ;
141                    C.matrix = Cmat2 ;
142                    if (contains (type, 'single'))
143                        tol = 1e-5 ;
144                    elseif (contains (type, 'double'))
145                        tol = 1e-12 ;
146                    end
147                otherwise
148                    A.matrix = Amat ;
149                    B.matrix = Bmat ;
150                    C.matrix = Cmat ;
151            end
152
153            accum.opname = binop ;
154            accum.optype = type ;
155
156            try
157                GB_spec_operator (accum) ;
158            catch
159                continue
160            end
161
162            if (GB_spec_is_positional (accum))
163                continue ;
164            end
165
166            %---------------------------------------
167            % C += A where A is dense
168            %---------------------------------------
169
170            C0 = GB_spec_assign (C, [ ], accum, A, [ ], [ ], [ ], false) ;
171            C1 = GB_mex_assign  (C, [ ], accum, A, [ ], [ ], [ ]) ;
172            GB_spec_compare (C0, C1, 0, tol) ;
173
174            %---------------------------------------
175            % C += B where B is sparse
176            %---------------------------------------
177
178            C0 = GB_spec_assign (C, [ ], accum, B, [ ], [ ], [ ], false) ;
179            C1 = GB_mex_assign  (C, [ ], accum, B, [ ], [ ], [ ]) ;
180            GB_spec_compare (C0, C1, 0, tol) ;
181
182            %---------------------------------------
183            % C += x
184            %---------------------------------------
185
186            C0 = GB_spec_assign (C, [ ], accum, X, [ ], [ ], [ ], true) ;
187            C1 = GB_mex_assign  (C, [ ], accum, X, [ ], [ ], [ ]) ;
188            GB_spec_compare (C0, C1, 0, tol) ;
189
190            %---------------------------------------
191            % C<replace> += x
192            %---------------------------------------
193
194            C0 = GB_spec_assign (C, [ ], accum, X, [ ], [ ], drep, true) ;
195            C1 = GB_mex_subassign  (C, [ ], accum, X, [ ], [ ], drep) ;
196            GB_spec_compare (C0, C1, 0, tol) ;
197
198        end
199    end
200end
201
202fprintf ('\ntest142: all tests passed\n') ;
203
204