1 //------------------------------------------------------------------------------
2 // GB_spones_mex: like spones(A) in MATLAB but do not drop zeros on input
3 //------------------------------------------------------------------------------
4 
5 // The MATLAB built-in function spones(A) has changed, of MATLAB R2019b.
6 // It now drops zeros on input.  Prior versions converted them to 1 on output.
7 // The tests here use the old behavior, so this function replaces spones(A)
8 // with GB_spones_mex (A), which has the old behavior of spones.
9 
10 #include "mex.h"
11 #include <string.h>
12 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])13 void mexFunction
14 (
15     int nargout,
16     mxArray *pargout [ ],
17     int nargin,
18     const mxArray *pargin [ ]
19 )
20 {
21 
22     //--------------------------------------------------------------------------
23     // check inputs
24     //--------------------------------------------------------------------------
25 
26     if (nargin != 1 || nargout > 1 || !mxIsSparse (pargin [0]))
27     {
28         mexErrMsgTxt ("usage: C = GB_spones_mex (A), A must be sparse") ;
29     }
30 
31     //--------------------------------------------------------------------------
32     // get the input matrix
33     //--------------------------------------------------------------------------
34 
35     mwSize m = mxGetM (pargin [0]) ;
36     mwSize n = mxGetN (pargin [0]) ;
37     mwIndex *Ap = mxGetJc (pargin [0]) ;
38     mwIndex *Ai = mxGetIr (pargin [0]) ;
39     mwSize nz = Ap [n] ;
40 
41     //--------------------------------------------------------------------------
42     // create the output matrix
43     //--------------------------------------------------------------------------
44 
45     pargout [0] = mxCreateSparse (m, n, nz+1, mxREAL) ;
46     mwIndex *Cp = mxGetJc (pargout [0]) ;
47     mwIndex *Ci = mxGetIr (pargout [0]) ;
48     double *Cx = mxGetDoubles (pargout [0]) ;
49 
50     memcpy (Cp, Ap, (n+1) * sizeof (mwIndex)) ;
51     memcpy (Ci, Ai, nz    * sizeof (mwIndex)) ;
52     for (mwSize p = 0 ; p < nz ; p++)
53     {
54         Cx [p] = 1 ;
55     }
56 }
57 
58