1 //------------------------------------------------------------------------------
2 // GB_mex_nonzero: compute C=nonzero(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 // C = nonzero (A), where A and C are double
11 
12 #include "GB_mex.h"
13 
14 #define USAGE "C = GB_mex_nonzero (A)"
15 
16 #define FREE_ALL                        \
17 {                                       \
18     GrB_Matrix_free_(&A) ;              \
19     GrB_Matrix_free_(&C) ;              \
20     GB_mx_put_global (true) ;           \
21 }
22 
23 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])24 void mexFunction
25 (
26     int nargout,
27     mxArray *pargout [ ],
28     int nargin,
29     const mxArray *pargin [ ]
30 )
31 {
32 
33     bool malloc_debug = GB_mx_get_global (true) ;
34     GrB_Matrix A = NULL, C = NULL ;
35 
36     // check inputs
37     if (nargout > 1 || nargin != 1)
38     {
39         mexErrMsgTxt ("Usage: " USAGE) ;
40     }
41 
42     #define GET_DEEP_COPY ;
43     #define FREE_DEEP_COPY ;
44 
45     // get A
46     A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
47     if (A == NULL)
48     {
49         FREE_ALL ;
50         mexErrMsgTxt ("failed") ;
51     }
52 
53     #define GET_DEEP_COPY ;
54     #define FREE_DEEP_COPY ;
55 
56     // construct C
57     METHOD (GrB_Matrix_new (&C, GrB_FP64, A->vlen, A->vdim)) ;
58 
59     #undef GET_DEEP_COPY
60     #undef FREE_DEEP_COPY
61 
62     #define GET_DEEP_COPY  GrB_Matrix_new (&C, GrB_FP64, A->vlen, A->vdim) ;
63     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
64 
65     // C = nonzero (A)
66     METHOD (GxB_Matrix_select_(C, NULL, NULL, GxB_NONZERO, A, NULL, NULL)) ;
67 
68     // return C to MATLAB as a regular MATLAB sparse matrix
69     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C nonzero", false) ;
70 
71     FREE_ALL ;
72 }
73 
74