1function C = GB_spec_select (C, Mask, accum, opname, A, thunk, descriptor)
2%GB_SPEC_SELECT a MATLAB mimic of GxB_select
3%
4% Usage:
5% C = GB_spec_select (C, Mask, accum, opname, A, thunk, descriptor)
6
7% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
8% SPDX-License-Identifier: Apache-2.0
9
10%-------------------------------------------------------------------------------
11% get inputs
12%-------------------------------------------------------------------------------
13
14if (nargout > 1 || nargin ~= 7)
15    error ('usage: C = GB_spec_select (C, Mask, accum, opname, A, thunk, desc)');
16end
17
18C = GB_spec_matrix (C) ;
19A = GB_spec_matrix (A) ;
20[C_replace Mask_comp Atrans Btrans Mask_struct] = ...
21    GB_spec_descriptor (descriptor) ;
22Mask = GB_spec_getmask (Mask, Mask_struct) ;
23
24%-------------------------------------------------------------------------------
25% do the work via a clean MATLAB interpretation of the entire GraphBLAS spec
26%-------------------------------------------------------------------------------
27
28% select the descriptor to A
29if (Atrans)
30    A.matrix = A.matrix.' ;
31    A.pattern = A.pattern' ;
32end
33
34atype = A.class ;
35T.matrix = GB_spec_zeros (size (A.matrix), atype) ;
36thunk = full (thunk) ;
37xthunk = GB_mex_cast (thunk, atype) ;
38
39is_complex = contains (atype, 'complex') ;
40if (is_complex)
41    switch (opname)
42        case { 'gt_zero', 'ge_zero', 'lt_zero', 'le_zero', ...
43               'gt_thunk', 'ge_thunk', 'lt_thunk', 'le_thunk' }
44            error ('op %s not defined for complex types', opname) ;
45        otherwise
46            % op is OK
47    end
48end
49
50switch (opname)
51    case 'tril'
52        p = tril (A.pattern, thunk) ;
53    case 'triu'
54        p = triu (A.pattern, thunk) ;
55    case 'diag'
56        p = tril (triu (A.pattern, thunk), thunk) ;
57    case 'offdiag'
58        p = tril (A.pattern, thunk-1) | triu (A.pattern, thunk+1) ;
59    case 'nonzero'
60        p = A.pattern & (A.matrix ~= 0) ;
61    case 'eq_zero'
62        p = A.pattern & (A.matrix == 0) ;
63    case 'gt_zero'
64        p = A.pattern & (A.matrix > 0) ;
65    case 'ge_zero'
66        p = A.pattern & (A.matrix >= 0) ;
67    case 'lt_zero'
68        p = A.pattern & (A.matrix < 0) ;
69    case 'le_zero'
70        p = A.pattern & (A.matrix <= 0) ;
71    case 'ne_thunk'
72        p = A.pattern & (A.matrix ~= xthunk) ;
73    case 'eq_thunk'
74        p = A.pattern & (A.matrix == xthunk) ;
75    case 'gt_thunk'
76        p = A.pattern & (A.matrix > xthunk) ;
77    case 'ge_thunk'
78        p = A.pattern & (A.matrix >= xthunk) ;
79    case 'lt_thunk'
80        p = A.pattern & (A.matrix < xthunk) ;
81    case 'le_thunk'
82        p = A.pattern & (A.matrix <= xthunk) ;
83    case 'isnan'
84        p = A.pattern & isnan (A.matrix) ;
85    otherwise
86        error ('invalid op') ;
87end
88
89T.matrix (p) = A.matrix (p) ;
90T.pattern = p ;
91
92% C<Mask> = accum (C,T): select the accum, then Mask, and return the result
93C = GB_spec_accum_mask (C, Mask, accum, T, C_replace, Mask_comp, 0) ;
94
95