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