1function test154
2%TEST154 test GrB_apply with scalar binding
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 ;
8ops = binops.all ;
9types = types.all ;
10
11fprintf ('\n--- testing apply with binaryop and scalar binding\n') ;
12rng ('default') ;
13% the right approach for apply_bind1st and apply_bind2nd
14desc0.inp0 = 'tran' ;
15desc1.inp1 = 'tran' ;
16% shotgun approach for eWiseMult
17desc.inp0 = 'tran' ;
18desc.inp1 = 'tran' ;
19
20n_operators = 0 ;
21for k2 = 1:length(ops)
22    mulop = ops {k2} ;
23    fprintf ('\n%-10s ', mulop) ;
24
25    for k1 = 1:length (types)
26    type = types {k1} ;
27
28    % create the op
29    clear op
30    op.opname = mulop ;
31    op.optype = type ;
32
33    try
34        [oname ot ztype xtype ytype] = GB_spec_operator (op) ;
35    catch
36        continue ;
37    end
38    n_operators = n_operators + 1  ;
39
40    switch (mulop)
41        case { 'pow' }
42            xlimits = [0, 5] ;
43            ylimits = [0, 5] ;
44        case { 'ldexp' }
45            xlimits = [-5, 5] ;
46            ylimits = [-5, 5] ;
47        otherwise
48            xlimits = [ ] ;
49            ylimits = [ ] ;
50    end
51
52    if (contains (type, 'single'))
53        tol = 1e-5 ;
54    elseif (contains (type, 'double'))
55        tol = 1e-12 ;
56    else
57        tol = 0 ;
58    end
59
60    fprintf ('.') ;
61
62    for m = [1 4] % [ 1 10 ]% 100]
63    for n = [1 4] % [1 10 ]% 100]
64    for hi = [1 5] % [-1:2:5 ]
65    for lo = [-1 0] % [-3:2:5 ]
66    Amat = (hi*sprand (m,n,0.8)-lo) .* sprand (m,n,0.5) ;
67    Bmat = (hi*sprand (m,n,0.8)-lo) .* sprand (m,n,0.5) ;
68    xmat = (hi*sparse (rand(1))-lo) .* sparse (rand(1)) ;
69    ymat = (hi*sparse (rand(1))-lo) .* sparse (rand(1)) ;
70    Cmat = sparse (m, n) ;
71
72    if (~isempty (xlimits))
73        Amat = max (Amat, xlimits (1)) ;
74        Amat = min (Amat, xlimits (2)) ;
75        xmat = max (xmat, xlimits (1)) ;
76        xmat = min (xmat, xlimits (2)) ;
77    end
78    if (xmat == 0)
79        xmat = sparse (0.5) ;
80    end
81
82    if (~isempty (ylimits))
83        Bmat = max (Bmat, ylimits (1)) ;
84        Bmat = min (Bmat, ylimits (2)) ;
85        ymat = max (ymat, ylimits (1)) ;
86        ymat = min (ymat, ylimits (2)) ;
87    end
88    if (ymat == 0)
89        ymat = sparse (0.5) ;
90    end
91
92    C.matrix = Cmat ;
93    C.class = ztype ;
94
95    CT.matrix = Cmat' ;
96    CT.class = ztype ;
97
98    A.matrix = Amat ;
99    A.class = xtype ;
100
101    B.matrix = Bmat ;
102    B.class = ytype ;
103
104    x.matrix = xmat ;
105    x.class = xtype ;
106
107    y.matrix = ymat ;
108    y.class = ytype ;
109
110    X.matrix = xmat .* spones (Bmat) ;
111    X.class = xtype ;
112
113    Y.matrix = ymat .* spones (Amat) ;
114    Y.class = ytype ;
115
116    op_ewise = op ;
117    if (isequal (op.opname, 'any'))
118        op_ewise.opname = 'second' ;
119    end
120
121    C1 = GB_mex_apply1 (C, [ ], [ ], op, 0, x, B) ;
122    C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise, X, B, [ ]) ;
123    GB_spec_compare (C1, C2, 0, tol) ;
124    C1 = GB_mex_apply1 (C, [ ], [ ], op, 1, x, B) ;
125    GB_spec_compare (C1, C2, 0, tol) ;
126
127    C1 = GB_mex_apply1 (CT, [ ], [ ], op, 0, x, B, desc1) ;
128    C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise, X, B, desc) ;
129    GB_spec_compare (C1, C2, 0, tol) ;
130    C1 = GB_mex_apply1 (CT, [ ], [ ], op, 1, x, B, desc1) ;
131    GB_spec_compare (C1, C2, 0, tol) ;
132
133    op_ewise = op ;
134    if (isequal (op.opname, 'any'))
135        op_ewise.opname = 'first' ;
136    end
137
138    for csc = 0:1
139
140        A.is_csc = csc ;
141        C.is_csc = csc ;
142        CT.is_csc = csc ;
143
144        C1 = GB_mex_apply2 (C, [ ], [ ], op, 0, A, y) ;
145        C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise, A, Y, [ ]) ;
146        GB_spec_compare (C1, C2, 0, tol) ;
147        C1 = GB_mex_apply2 (C, [ ], [ ], op, 1, A, y) ;
148        GB_spec_compare (C1, C2, 0, tol) ;
149
150        C1 = GB_mex_apply2 (CT, [ ], [ ], op, 0, A, y, desc0) ;
151        C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise, A, Y, desc) ;
152        GB_spec_compare (C1, C2, 0, tol) ;
153        C1 = GB_mex_apply2 (CT, [ ], [ ], op, 1, A, y, desc0) ;
154        GB_spec_compare (C1, C2, 0, tol) ;
155    end
156
157    y.class = 'double' ;
158    Y.class = 'double' ;
159
160    C1 = GB_mex_apply2 (C, [ ], [ ], op, 0, A, y) ;
161    C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise, A, Y, [ ]) ;
162    GB_spec_compare (C1, C2, 0, tol) ;
163
164    C1 = GB_mex_apply2 (C, [ ], [ ], op, 1, A, y) ;
165    GB_spec_compare (C1, C2, 0, tol) ;
166
167    C1 = GB_mex_apply2 (CT, [ ], [ ], op, 0, A, y, desc0) ;
168    C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise, A, Y, desc) ;
169    GB_spec_compare (C1, C2, 0, tol) ;
170    C1 = GB_mex_apply2 (CT, [ ], [ ], op, 1, A, y, desc0) ;
171    GB_spec_compare (C1, C2, 0, tol) ;
172
173end
174end
175end
176end
177end
178end
179
180fprintf ('\nNumber of built-in GraphBLAS operators: %d\n',  n_operators) ;
181fprintf ('\ntest154: all tests passed\n') ;
182
183