File: GB_spones_mex.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (60 lines) | stat: -rw-r--r-- 2,064 bytes parent folder | download | duplicates (2)
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
58
59
60
//------------------------------------------------------------------------------
// GB_spones_mex: like spones(A) in MATLAB but do not drop zeros on input
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// The MATLAB built-in function spones(A) has changed, as 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 ;
    }
}