1 //------------------------------------------------------------------------------
2 // GB_mex_apply: C<Mask> = accum(C,op(A)) or op(A')
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // Apply a unary operator to a matrix
11 
12 #include "GB_mex.h"
13 
14 #define USAGE "C = GB_mex_apply (C, Mask, accum, op, A, desc)"
15 
16 #define FREE_ALL                        \
17 {                                       \
18     GrB_Matrix_free_(&C) ;              \
19     GrB_Matrix_free_(&Mask) ;           \
20     GrB_Matrix_free_(&A) ;              \
21     GrB_Descriptor_free_(&desc) ;       \
22     GB_mx_put_global (true) ;           \
23 }
24 
25 GrB_Matrix C = NULL ;
26 GrB_Matrix Mask = NULL ;
27 GrB_Matrix A = NULL ;
28 GrB_Descriptor desc = NULL ;
29 GrB_BinaryOp accum = NULL ;
30 GrB_UnaryOp op = NULL ;
31 GrB_Info apply (bool is_matrix) ;
32 
33 //------------------------------------------------------------------------------
34 
apply(bool is_matrix)35 GrB_Info apply (bool is_matrix)
36 {
37     GrB_Info info ;
38 
39     if (is_matrix)
40     {
41         info = GrB_Matrix_apply_(C, Mask, accum, op, A, desc) ;
42     }
43     else
44     {
45         info = GrB_Vector_apply_((GrB_Vector) C, (GrB_Vector) Mask, accum, op,
46             (GrB_Vector) A, desc) ;
47     }
48 
49     return (info) ;
50 }
51 
52 //------------------------------------------------------------------------------
53 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])54 void mexFunction
55 (
56     int nargout,
57     mxArray *pargout [ ],
58     int nargin,
59     const mxArray *pargin [ ]
60 )
61 {
62 
63     bool malloc_debug = GB_mx_get_global (true) ;
64 
65     // check inputs
66     if (nargout > 1 || nargin < 5 || nargin > 6)
67     {
68         mexErrMsgTxt ("Usage: " USAGE) ;
69     }
70 
71     // get C (make a deep copy)
72     #define GET_DEEP_COPY \
73     C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
74     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
75     GET_DEEP_COPY ;
76     if (C == NULL)
77     {
78         FREE_ALL ;
79         mexErrMsgTxt ("C failed") ;
80     }
81 
82     // get Mask (shallow copy)
83     Mask = GB_mx_mxArray_to_Matrix (pargin [1], "Mask", false, false) ;
84     if (Mask == NULL && !mxIsEmpty (pargin [1]))
85     {
86         FREE_ALL ;
87         mexErrMsgTxt ("Mask failed") ;
88     }
89 
90     // get A (shallow copy)
91     A = GB_mx_mxArray_to_Matrix (pargin [4], "A input", false, true) ;
92     if (A == NULL || A->magic != GB_MAGIC)
93     {
94         FREE_ALL ;
95         mexErrMsgTxt ("A failed") ;
96     }
97 
98     // get accum, if present
99     bool user_complex = (Complex != GxB_FC64)
100         && (C->type == Complex || A->type == Complex) ;
101     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [2], "accum",
102         C->type, user_complex))
103     {
104         FREE_ALL ;
105         mexErrMsgTxt ("accum failed") ;
106     }
107 
108     // get op
109     if (!GB_mx_mxArray_to_UnaryOp (&op, pargin [3], "op",
110         A->type, user_complex) || op == NULL)
111     {
112         FREE_ALL ;
113         mexErrMsgTxt ("UnaryOp failed") ;
114     }
115 
116     // get desc
117     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (5), "desc"))
118     {
119         FREE_ALL ;
120         mexErrMsgTxt ("desc failed") ;
121     }
122 
123     // C<Mask> = accum(C,op(A))
124     if (GB_NCOLS (C) == 1 && (desc == NULL || desc->in0 == GxB_DEFAULT))
125     {
126         // this is just to test the Vector version
127         METHOD (apply (false)) ;
128     }
129     else
130     {
131         METHOD (apply (true)) ;
132     }
133 
134     // return C to MATLAB as a struct and free the GraphBLAS C
135     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
136 
137     FREE_ALL ;
138 }
139 
140