File: cs_reachr_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 (67 lines) | stat: -rw-r--r-- 1,947 bytes parent folder | download | duplicates (14)
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
#include "cs_mex.h"
/* find nonzero pattern of x=L\sparse(b).  L must be sparse and lower
 * triangular.  b must be a sparse vector. */

static
void dfsr (CS_INT j, const cs *L, CS_INT *top, CS_INT *xi, CS_INT *w)
{
    CS_INT p ;
    w [j] = 1 ;                                 /* mark node j */
    for (p = L->p [j] ; p < L->p [j+1] ; p++)   /* for each i in L(:,j) */
    {
        if (w [L->i [p]] != 1)                  /* if i is unmarked */
        {
            dfsr (L->i [p], L, top, xi, w) ;    /* start a dfs at i */
        }
    }
    xi [--(*top)] = j ;                         /* push j onto the stack */
}

/* w [0..n-1] == 0 on input, <= 1 on output.  size n */
static
CS_INT reachr (const cs *L, const cs *B, CS_INT *xi, CS_INT *w)
{
    CS_INT p, n = L->n ;
    CS_INT top = n ;                            /* stack is empty */
    for (p = B->p [0] ; p < B->p [1] ; p++)     /* for each i in pattern of b */
    {
        if (w [B->i [p]] != 1)                  /* if i is unmarked */
        {
            dfsr (B->i [p], L, &top, xi, w) ;   /* start a dfs at i */
        }
    }
    return (top) ;                              /* return top of stack */
}

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    cs_dl Lmatrix, Bmatrix, *L, *B ;
    double *x ;
    CS_INT i, j, top, *xi ;

    if (nargout > 1 || nargin != 2)
    {
        mexErrMsgTxt ("Usage: x = cs_reachr(L,b)") ;
    }

    /* get inputs */
    L = cs_dl_mex_get_sparse (&Lmatrix, 1, 1, pargin [0]) ;
    B = cs_dl_mex_get_sparse (&Bmatrix, 0, 1, pargin [1]) ;
    cs_mex_check (0, L->n, 1, 0, 1, 1, pargin [1]) ;

    xi = cs_dl_calloc (2*L->n, sizeof (CS_INT)) ;

    top = reachr (L, B, xi, xi + L->n) ;

    pargout [0] = mxCreateDoubleMatrix (L->n - top, 1, mxREAL) ;
    x = mxGetPr (pargout [0]) ;
    for (j = 0, i = top ; i < L->n ; i++, j++) x [j] = xi [i] ;

    cs_free (xi) ;
}