/* x = b(p) */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { CS_INT n, k, *p ; double *xx ; if (nargout > 1 || nargin != 2) { mexErrMsgTxt ("Usage: x = cs_pvec(b,p)") ; } n = mxGetNumberOfElements (pargin [0]) ; if (n != mxGetNumberOfElements (pargin [1])) { mexErrMsgTxt ("b or p wrong size") ; } xx = mxGetPr (pargin [1]) ; p = cs_dl_malloc (n, sizeof (CS_INT)) ; for (k = 0 ; k < n ; k++) p [k] = xx [k] - 1 ; if (mxIsComplex (pargin [0])) { #ifdef NCOMPLEX mexErrMsgTxt ("complex case not supported") ; #else cs_complex_t *x, *b ; b = cs_cl_mex_get_double (n, pargin [0]) ; x = cs_dl_malloc (n, sizeof (cs_complex_t)) ; cs_cl_pvec (p, b, x, n) ; pargout [0] = cs_cl_mex_put_double (n, x) ; cs_free (b) ; /* free copy of complex values */ #endif } else { double *x, *b ; b = cs_dl_mex_get_double (n, pargin [0]) ; pargout [0] = mxCreateDoubleMatrix (n, 1, mxREAL) ; x = mxGetPr (pargout [0]) ; cs_dl_pvec (p, b, x, n) ; } cs_free (p) ; }
/* Cholesky update/downdate */ cs_long_t demo3 (problem *Prob) { cs_cl *A, *C, *W = NULL, *WW, *WT, *E = NULL, *W2 ; cs_long_t n, k, *Li, *Lp, *Wi, *Wp, p1, p2, *p = NULL, ok ; cs_complex_t *b, *x, *resid, *y = NULL, *Lx, *Wx, s ; double t, t1 ; cs_cls *S = NULL ; cs_cln *N = NULL ; if (!Prob || !Prob->sym || Prob->A->n == 0) return (0) ; A = Prob->A ; C = Prob->C ; b = Prob->b ; x = Prob->x ; resid = Prob->resid; n = A->n ; if (!Prob->sym || n == 0) return (1) ; rhs (x, b, n) ; /* compute right-hand side */ printf ("\nchol then update/downdate ") ; print_order (1) ; y = cs_cl_malloc (n, sizeof (cs_complex_t)) ; t = tic () ; S = cs_cl_schol (1, C) ; /* symbolic Chol, amd(A+A') */ printf ("\nsymbolic chol time %8.2f\n", toc (t)) ; t = tic () ; N = cs_cl_chol (C, S) ; /* numeric Cholesky */ printf ("numeric chol time %8.2f\n", toc (t)) ; if (!S || !N || !y) return (done3 (0, S, N, y, W, E, p)) ; t = tic () ; cs_cl_ipvec (S->pinv, b, y, n) ; /* y = P*b */ cs_cl_lsolve (N->L, y) ; /* y = L\y */ cs_cl_ltsolve (N->L, y) ; /* y = L'\y */ cs_cl_pvec (S->pinv, y, x, n) ; /* x = P'*y */ printf ("solve chol time %8.2f\n", toc (t)) ; printf ("original: ") ; print_resid (1, C, x, b, resid) ; /* print residual */ k = n/2 ; /* construct W */ W = cs_cl_spalloc (n, 1, n, 1, 0) ; if (!W) return (done3 (0, S, N, y, W, E, p)) ; Lp = N->L->p ; Li = N->L->i ; Lx = N->L->x ; Wp = W->p ; Wi = W->i ; Wx = W->x ; Wp [0] = 0 ; p1 = Lp [k] ; Wp [1] = Lp [k+1] - p1 ; s = Lx [p1] ; srand (1) ; for ( ; p1 < Lp [k+1] ; p1++) { p2 = p1 - Lp [k] ; Wi [p2] = Li [p1] ; Wx [p2] = s * rand () / ((double) RAND_MAX) ; } t = tic () ; ok = cs_cl_updown (N->L, +1, W, S->parent) ; /* update: L*L'+W*W' */ t1 = toc (t) ; printf ("update: time: %8.2f\n", t1) ; if (!ok) return (done3 (0, S, N, y, W, E, p)) ; t = tic () ; cs_cl_ipvec (S->pinv, b, y, n) ; /* y = P*b */ cs_cl_lsolve (N->L, y) ; /* y = L\y */ cs_cl_ltsolve (N->L, y) ; /* y = L'\y */ cs_cl_pvec (S->pinv, y, x, n) ; /* x = P'*y */ t = toc (t) ; p = cs_cl_pinv (S->pinv, n) ; W2 = cs_cl_permute (W, p, NULL, 1) ; /* E = C + (P'W)*(P'W)' */ WT = cs_cl_transpose (W2,1) ; WW = cs_cl_multiply (W2, WT) ; cs_cl_spfree (WT) ; cs_cl_spfree (W2) ; E = cs_cl_add (C, WW, 1, 1) ; cs_cl_spfree (WW) ; if (!E || !p) return (done3 (0, S, N, y, W, E, p)) ; printf ("update: time: %8.2f (incl solve) ", t1+t) ; print_resid (1, E, x, b, resid) ; /* print residual */ cs_cl_nfree (N) ; /* clear N */ t = tic () ; N = cs_cl_chol (E, S) ; /* numeric Cholesky */ if (!N) return (done3 (0, S, N, y, W, E, p)) ; cs_cl_ipvec (S->pinv, b, y, n) ; /* y = P*b */ cs_cl_lsolve (N->L, y) ; /* y = L\y */ cs_cl_ltsolve (N->L, y) ; /* y = L'\y */ cs_cl_pvec (S->pinv, y, x, n) ; /* x = P'*y */ t = toc (t) ; printf ("rechol: time: %8.2f (incl solve) ", t) ; print_resid (1, E, x, b, resid) ; /* print residual */ t = tic () ; ok = cs_cl_updown (N->L, -1, W, S->parent) ; /* downdate: L*L'-W*W' */ t1 = toc (t) ; if (!ok) return (done3 (0, S, N, y, W, E, p)) ; printf ("downdate: time: %8.2f\n", t1) ; t = tic () ; cs_cl_ipvec (S->pinv, b, y, n) ; /* y = P*b */ cs_cl_lsolve (N->L, y) ; /* y = L\y */ cs_cl_ltsolve (N->L, y) ; /* y = L'\y */ cs_cl_pvec (S->pinv, y, x, n) ; /* x = P'*y */ t = toc (t) ; printf ("downdate: time: %8.2f (incl solve) ", t1+t) ; print_resid (1, C, x, b, resid) ; /* print residual */ return (done3 (1, S, N, y, W, E, p)) ; }