File: GB_spones_mex.c

package info (click to toggle)
suitesparse 1%3A5.12.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176,720 kB
  • sloc: ansic: 1,193,914; cpp: 31,704; makefile: 6,638; fortran: 1,927; java: 1,826; csh: 765; ruby: 725; sh: 529; python: 333; perl: 225; sed: 164; awk: 35
file content (57 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//------------------------------------------------------------------------------
// GB_spones_mex: like spones(A) in MATLAB but do not drop zeros on input
//------------------------------------------------------------------------------

// The MATLAB built-in function spones(A) has changed, of MATLAB R2019b.
// It now drops zeros on input.  Prior versions converted them to 1 on output.
// The tests here use the old behavior, so this function replaces spones(A)
// with GB_spones_mex (A), which has the old behavior of spones.

#include "mex.h"
#include <string.h>

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    if (nargin != 1 || nargout > 1 || !mxIsSparse (pargin [0]))
    {
        mexErrMsgTxt ("usage: C = GB_spones_mex (A), A must be sparse") ;
    }

    //--------------------------------------------------------------------------
    // get the input matrix
    //--------------------------------------------------------------------------

    mwSize m = mxGetM (pargin [0]) ;
    mwSize n = mxGetN (pargin [0]) ;
    mwIndex *Ap = mxGetJc (pargin [0]) ;
    mwIndex *Ai = mxGetIr (pargin [0]) ;
    mwSize nz = Ap [n] ;

    //--------------------------------------------------------------------------
    // create the output matrix
    //--------------------------------------------------------------------------

    pargout [0] = mxCreateSparse (m, n, nz+1, mxREAL) ;
    mwIndex *Cp = mxGetJc (pargout [0]) ;
    mwIndex *Ci = mxGetIr (pargout [0]) ;
    double *Cx = mxGetDoubles (pargout [0]) ;

    memcpy (Cp, Ap, (n+1) * sizeof (mwIndex)) ;
    memcpy (Ci, Ai, nz    * sizeof (mwIndex)) ;
    for (mwSize p = 0 ; p < nz ; p++)
    {
        Cx [p] = 1 ;
    }
}