File: dimacs10_convert_to_graph.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 (86 lines) | stat: -rw-r--r-- 2,163 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//------------------------------------------------------------------------------
// dimacs10/dimacs10_convert_to_graph.c
//------------------------------------------------------------------------------

// DIMACS10, Copyright (c) 2011, Timothy A Davis. All Rights Reserved.
// SPDX-License-Identifier: BSD-3-clause

//------------------------------------------------------------------------------

#include "mex.h"
/* convert_to_graph mexFunction

    function S = convert_to_graph (A, binary)
    if (nargin < 2)
        binary = 0 ;
    end
    S = tril (A,-1) + triu (A,1) ;
    if (binary)
        S = spones (S) ;
    end

Ignores the imaginary part of a complex matrix A.

 */

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    mwSignedIndex *Ap, *Ai, *Sp, *Si, m, n, nz, j, p, i ;
    double *Sx, *Ax ;
    int binary ;
    if (nargin < 1 || nargin > 2 || nargout > 1)
    {
        mexErrMsgTxt ("usage: S = convert_to_graph (A, binary)") ;
    }

    /* get the input A matrix */
    Ap = (mwSignedIndex *) mxGetJc (pargin [0]) ;
    Ai = (mwSignedIndex *) mxGetIr (pargin [0]) ;
    Ax = mxGetPr (pargin [0]) ;
    m = mxGetM (pargin [0]) ;
    n = mxGetN (pargin [0]) ;
    nz = Ap [n] ;
    if (nz == 0) nz = 1 ;

    /* get the 2nd input argument */
    binary = (nargin < 2) ? 0 : ((int) mxGetScalar (pargin [1])) ;

    /* allocate the output matrix */
    pargout [0] = mxCreateSparse (m, n, nz, mxREAL) ;
    Sp = (mwSignedIndex *) mxGetJc (pargout [0]) ;
    Si = (mwSignedIndex *) mxGetIr (pargout [0]) ;
    Sx = mxGetPr (pargout [0]) ;

    /* strip the diagonal from A */
    nz = 0 ;
    for (j = 0 ; j < n ; j++)
    {
        Sp [j] = nz ;
        for (p = Ap [j] ; p < Ap [j+1] ; p++)
        {
            i = Ai [p] ;
            if (i != j)
            {
                Si [nz] = i ;
                if (!binary) Sx [nz] = Ax [p] ;
                nz++ ;
            }
        }
    }
    Sp [n] = nz ;

    /* convert the result to binary, if requested */
    if (binary)
    {
        for (p = 0 ; p < nz ; p++)
        {
            Sx [p] = 1 ;
        }
    }
}