1 //------------------------------------------------------------------------------
2 // GB_mex_Vector_extract: MATLAB interface for w<mask> = accum (w,u(I))
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 #include "GB_mex.h"
11 
12 #define USAGE "w = GB_mex_Vector_extract w, mask, accum, u, I, desc)"
13 
14 #define FREE_ALL                        \
15 {                                       \
16     GrB_Vector_free_(&w) ;              \
17     GrB_Vector_free_(&mask) ;           \
18     GrB_Vector_free_(&u) ;              \
19     GrB_Descriptor_free_(&desc) ;       \
20     GB_mx_put_global (true) ;           \
21 }
22 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])23 void mexFunction
24 (
25     int nargout,
26     mxArray *pargout [ ],
27     int nargin,
28     const mxArray *pargin [ ]
29 )
30 {
31 
32     bool malloc_debug = GB_mx_get_global (true) ;
33     GrB_Vector w = NULL ;
34     GrB_Vector mask = NULL ;
35     GrB_Vector u = NULL ;
36     GrB_Descriptor desc = NULL ;
37     GrB_Index *I = NULL, ni = 0, I_range [3] ;
38     bool ignore ;
39 
40     // check inputs
41     if (nargout > 1 || nargin < 5 || nargin > 6)
42     {
43         mexErrMsgTxt ("Usage: " USAGE) ;
44     }
45 
46     // get w (make a deep copy)
47     #define GET_DEEP_COPY \
48     w = GB_mx_mxArray_to_Vector (pargin [0], "w input", true, true) ;
49     #define FREE_DEEP_COPY GrB_Vector_free_(&w) ;
50     GET_DEEP_COPY ;
51     if (w == NULL)
52     {
53         FREE_ALL ;
54         mexErrMsgTxt ("w failed") ;
55     }
56 
57     // get mask (shallow copy)
58     mask = GB_mx_mxArray_to_Vector (pargin [1], "mask", false, false) ;
59     if (mask == NULL && !mxIsEmpty (pargin [1]))
60     {
61         FREE_ALL ;
62         mexErrMsgTxt ("mask failed") ;
63     }
64 
65     // get u (shallow copy)
66     u = GB_mx_mxArray_to_Vector (pargin [3], "A input", false, true) ;
67     if (u == NULL)
68     {
69         FREE_ALL ;
70         mexErrMsgTxt ("u failed") ;
71     }
72 
73     // get accum, if present
74     bool user_complex = (Complex != GxB_FC64)
75         && (w->type == Complex || u->type == Complex) ;
76     GrB_BinaryOp accum ;
77     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [2], "accum",
78         w->type, user_complex))
79     {
80         FREE_ALL ;
81         mexErrMsgTxt ("accum failed") ;
82     }
83 
84     // get I
85     if (!GB_mx_mxArray_to_indices (&I, pargin [4], &ni, I_range, &ignore))
86     {
87         FREE_ALL ;
88         mexErrMsgTxt ("I failed") ;
89     }
90 
91     // get desc
92     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (5), "desc"))
93     {
94         FREE_ALL ;
95         mexErrMsgTxt ("desc failed") ;
96     }
97 
98     // w<mask> = accum (w,u(I))
99     METHOD (GrB_Vector_extract_(w, mask, accum, u, I, ni, desc)) ;
100 
101     // return w to MATLAB as a struct and free the GraphBLAS w
102     pargout [0] = GB_mx_Vector_to_mxArray (&w, "w output", true) ;
103 
104     FREE_ALL ;
105 }
106 
107