1function testcb
2%TESTCB test complex reduce
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7tol = 1e-13 ;
8seed = 1 ;
9for m = [1 5 10 100]
10    for n = [1 5 10 100]
11        for trial = 1:100
12
13            A = GB_mex_random (m, n, 10*(m+n), 1, seed) ; seed = seed + 1 ;
14            s = complex (0) ;
15
16            c1 = complex (full (sum (A (:)))) ;
17            c2 = GB_mex_reduce_to_scalar (s, [], 'plus', A) ;
18            assert (abs (c1 - c2) <= tol * abs (c1) + tol) ;
19            % assert (isequal (c1,c2))
20
21            [i j x] = find (A) ;
22            c1 = complex (prod (x)) ;
23            c2 = GB_mex_reduce_to_scalar (s, [], 'times', A) ;
24            assert (abs (c1 - c2) <= tol * abs (c1) + tol) ;
25            % assert (isequal (c1,c2))
26
27            s = complex (pi,2) ;
28            c1 = s * complex (full (sum (A (:)))) ;
29            c2 = GB_mex_reduce_to_scalar (s, 'times', 'plus', A) ;
30            assert (abs (c1 - c2) <= tol * abs (c1) + tol) ;
31            % assert (isequal (c1,c2))
32
33            s = complex (pi,2) ;
34            c1 = s + complex (full (sum (A (:)))) ;
35            c2 = GB_mex_reduce_to_scalar (s, 'plus', 'plus', A) ;
36            assert (abs (c1 - c2) <= tol * abs (c1) + tol) ;
37            % assert (isequal (c1,c2))
38
39            if (n > 1)
40
41                w = GB_mex_complex (sparse (m, 1)) ;
42                c1 = sum (A.').' ;
43                c2 = GB_mex_reduce_to_vector (w, [], [], 'plus', A, []) ;
44                assert (norm (c1 - c2.matrix, 1) <= tol * norm (c1, 1) + tol) ;
45                % assert (isequal (c1,c2.matrix))
46
47                w = GB_mex_random (m, 1, 10, 1, seed) ; seed = seed + 1 ;
48                c1 = w + sum (A.').' ;
49                c2 = GB_mex_reduce_to_vector (w, [], 'plus', 'plus', A, []) ;
50                assert (norm (c1 - c2.matrix, 1) <= tol * norm (c1, 1) + tol) ;
51                % assert (isequal (c1,c2.matrix))
52
53            end
54        end
55    end
56end
57
58fprintf ('testcb: all complex reduce tests passed\n') ;
59
60