/* cs_lu: sparse LU factorization, with optional fill-reducing ordering */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { css *S ; csn *N ; cs Amatrix, *A, *D ; csi n, order, *p ; double tol ; if (nargout > 4 || nargin > 3 || nargin < 1) { mexErrMsgTxt ("Usage: [L,U,p,q] = cs_lu (A,tol)") ; } A = cs_mex_get_sparse (&Amatrix, 1, 1, pargin [0]) ; /* get A */ n = A->n ; if (nargin == 2) /* determine tol and ordering */ { tol = mxGetScalar (pargin [1]) ; order = (nargout == 4) ? 1 : 0 ; /* amd (A+A'), or natural */ } else { tol = 1 ; order = (nargout == 4) ? 2 : 0 ; /* amd(S'*S) w/dense rows or I */ } S = cs_sqr (order, A, 0) ; /* symbolic ordering, no QR bound */ N = cs_lu (A, S, tol) ; /* numeric factorization */ if (!N) mexErrMsgTxt ("cs_lu failed (singular, or out of memory)") ; cs_dropzeros (N->L) ; /* drop zeros from L and sort it */ D = cs_transpose (N->L, 1) ; cs_spfree (N->L) ; N->L = cs_transpose (D, 1) ; cs_spfree (D) ; cs_dropzeros (N->U) ; /* drop zeros from U and sort it */ D = cs_transpose (N->U, 1) ; cs_spfree (N->U) ; N->U = cs_transpose (D, 1) ; cs_spfree (D) ; p = cs_pinv (N->pinv, n) ; /* p=pinv' */ pargout [0] = cs_mex_put_sparse (&(N->L)) ; /* return L */ pargout [1] = cs_mex_put_sparse (&(N->U)) ; /* return U */ pargout [2] = cs_mex_put_int (p, n, 1, 1) ; /* return p */ /* return Q */ if (nargout == 4) pargout [3] = cs_mex_put_int (S->q, n, 1, 0) ; cs_nfree (N) ; cs_sfree (S) ; }
/* cs_qr: sparse QR factorization */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { css *S ; csn *N ; cs Amatrix, *A, *D ; csi m, n, order, *p ; if (nargout > 5 || nargin != 1) { mexErrMsgTxt ("Usage: [V,beta,p,R,q] = cs_qr(A)") ; } A = cs_mex_get_sparse (&Amatrix, 0, 1, pargin [0]) ; /* get A */ m = A->m ; n = A->n ; if (m < n) mexErrMsgTxt ("A must have # rows >= # columns") ; order = (nargout == 5) ? 3 : 0 ; /* determine ordering */ S = cs_sqr (order, A, 1) ; /* symbolic QR ordering & analysis*/ N = cs_qr (A, S) ; /* numeric QR factorization */ if (!N) mexErrMsgTxt ("qr failed") ; cs_dropzeros (N->L) ; /* drop zeros from V and sort */ D = cs_transpose (N->L, 1) ; cs_spfree (N->L) ; N->L = cs_transpose (D, 1) ; cs_spfree (D) ; cs_dropzeros (N->U) ; /* drop zeros from R and sort */ D = cs_transpose (N->U, 1) ; cs_spfree (N->U) ; N->U = cs_transpose (D, 1) ; cs_spfree (D) ; m = N->L->m ; /* m may be larger now */ p = cs_pinv (S->pinv, m) ; /* p = pinv' */ pargout [0] = cs_mex_put_sparse (&(N->L)) ; /* return V */ cs_mex_put_double (n, N->B, &(pargout [1])) ; /* return beta */ pargout [2] = cs_mex_put_int (p, m, 1, 1) ; /* return p */ pargout [3] = cs_mex_put_sparse (&(N->U)) ; /* return R */ pargout [4] = cs_mex_put_int (S->q, n, 1, 0) ; /* return q */ cs_nfree (N) ; cs_sfree (S) ; }
/* cs_amd: approximate minimum degree ordering */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { cs Amatrix, *A ; int *P, order ; if (nargout > 1 || nargin < 1 || nargin > 2) { mexErrMsgTxt ("Usage: p = cs_amd(A,order)") ; } A = cs_mex_get_sparse (&Amatrix, 0, 0, pargin [0]) ; /* get A */ order = (nargin > 1) ? mxGetScalar (pargin [1]) : 1 ; /* get ordering */ order = CS_MAX (order, 1) ; order = CS_MIN (order, 3) ; P = cs_amd (order, A) ; /* min. degree ordering */ pargout [0] = cs_mex_put_int (P, A->n, 1, 1) ; /* return P */ }
/* cs_dmperm: maximum matching or Dulmage-Mendelsohn permutation. */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { double seed ; cs *A, Amatrix ; csd *D ; csi m, n, *jmatch, iseed ; if (nargin < 1 || nargin > 2 || nargout > 6) { mexErrMsgTxt ("Usage: [p,q,r,s,cc,rr] = cs_dmperm (A,seed)") ; } seed = (nargin > 1) ? mxGetScalar (pargin [1]) : 0 ; /* get seed */ iseed = (seed > 0 && seed < 1) ? (seed * RAND_MAX) : seed ; A = cs_mex_get_sparse (&Amatrix, 0, 0, pargin [0]) ; /* get A */ n = A->n ; m = A->m ; if (nargout <= 1) { jmatch = cs_maxtrans (A, iseed) ; /* max. matching */ pargout [0] = cs_mex_put_int (jmatch+m, n, 1, 0) ; /* return imatch */ cs_free (jmatch) ; } else { D = cs_dmperm (A, iseed) ; /* Dulmage-Mendelsohn decomposition */ pargout [0] = cs_mex_put_int (D->p, m, 1, 0) ; pargout [1] = cs_mex_put_int (D->q, n, 1, 0) ; pargout [2] = cs_mex_put_int (D->r, D->nb+1, 1, 0) ; pargout [3] = cs_mex_put_int (D->s, D->nb+1, 1, 0) ; pargout [4] = cs_mex_put_int (D->cc, 5, 1, 0) ; pargout [5] = cs_mex_put_int (D->rr, 5, 1, 0) ; cs_dfree (D) ; } }