1 #include "cs_mex.h"
2 /* find nonzero pattern of x=L\sparse(b). L must be sparse and lower
3 * triangular. b must be a sparse vector. */
4
5 static
dfsr(CS_INT j,const cs * L,CS_INT * top,CS_INT * xi,CS_INT * w)6 void dfsr (CS_INT j, const cs *L, CS_INT *top, CS_INT *xi, CS_INT *w)
7 {
8 CS_INT p ;
9 w [j] = 1 ; /* mark node j */
10 for (p = L->p [j] ; p < L->p [j+1] ; p++) /* for each i in L(:,j) */
11 {
12 if (w [L->i [p]] != 1) /* if i is unmarked */
13 {
14 dfsr (L->i [p], L, top, xi, w) ; /* start a dfs at i */
15 }
16 }
17 xi [--(*top)] = j ; /* push j onto the stack */
18 }
19
20 /* w [0..n-1] == 0 on input, <= 1 on output. size n */
21 static
reachr(const cs * L,const cs * B,CS_INT * xi,CS_INT * w)22 CS_INT reachr (const cs *L, const cs *B, CS_INT *xi, CS_INT *w)
23 {
24 CS_INT p, n = L->n ;
25 CS_INT top = n ; /* stack is empty */
26 for (p = B->p [0] ; p < B->p [1] ; p++) /* for each i in pattern of b */
27 {
28 if (w [B->i [p]] != 1) /* if i is unmarked */
29 {
30 dfsr (B->i [p], L, &top, xi, w) ; /* start a dfs at i */
31 }
32 }
33 return (top) ; /* return top of stack */
34 }
35
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])36 void mexFunction
37 (
38 int nargout,
39 mxArray *pargout [ ],
40 int nargin,
41 const mxArray *pargin [ ]
42 )
43 {
44 cs_dl Lmatrix, Bmatrix, *L, *B ;
45 double *x ;
46 CS_INT i, j, top, *xi ;
47
48 if (nargout > 1 || nargin != 2)
49 {
50 mexErrMsgTxt ("Usage: x = cs_reachr(L,b)") ;
51 }
52
53 /* get inputs */
54 L = cs_dl_mex_get_sparse (&Lmatrix, 1, 1, pargin [0]) ;
55 B = cs_dl_mex_get_sparse (&Bmatrix, 0, 1, pargin [1]) ;
56 cs_mex_check (0, L->n, 1, 0, 1, 1, pargin [1]) ;
57
58 xi = cs_dl_calloc (2*L->n, sizeof (CS_INT)) ;
59
60 top = reachr (L, B, xi, xi + L->n) ;
61
62 pargout [0] = mxCreateDoubleMatrix (L->n - top, 1, mxREAL) ;
63 x = mxGetPr (pargout [0]) ;
64 for (j = 0, i = top ; i < L->n ; i++, j++) x [j] = xi [i] ;
65
66 cs_free (xi) ;
67 }
68