/* read a problem from a file; use %g for integers to avoid cs_long_t conflicts */ problem *get_problem (FILE *f, double tol) { cs_cl *T, *A, *C ; cs_long_t sym, m, n, mn, nz1, nz2 ; problem *Prob ; Prob = cs_cl_calloc (1, sizeof (problem)) ; if (!Prob) return (NULL) ; T = cs_cl_load (f) ; /* load triplet matrix T from a file */ Prob->A = A = cs_cl_compress (T) ; /* A = compressed-column form of T */ cs_cl_spfree (T) ; /* clear T */ if (!cs_cl_dupl (A)) return (free_problem (Prob)) ; /* sum up duplicates */ Prob->sym = sym = is_sym (A) ; /* determine if A is symmetric */ m = A->m ; n = A->n ; mn = CS_MAX (m,n) ; nz1 = A->p [n] ; cs_cl_dropzeros (A) ; /* drop zero entries */ nz2 = A->p [n] ; if (tol > 0) cs_cl_droptol (A, tol) ; /* drop tiny entries (just to test) */ Prob->C = C = sym ? make_sym (A) : A ; /* C = A + triu(A,1)', or C=A */ if (!C) return (free_problem (Prob)) ; printf ("\n--- Matrix: %g-by-%g, nnz: %g (sym: %g: nnz %g), norm: %8.2e\n", (double) m, (double) n, (double) (A->p [n]), (double) sym, (double) (sym ? C->p [n] : 0), cs_cl_norm (C)) ; if (nz1 != nz2) printf ("zero entries dropped: %g\n", (double) (nz1 - nz2)); if (nz2 != A->p [n]) printf ("tiny entries dropped: %g\n", (double) (nz2 - A->p [n])) ; Prob->b = cs_cl_malloc (mn, sizeof (cs_complex_t)) ; Prob->x = cs_cl_malloc (mn, sizeof (cs_complex_t)) ; Prob->resid = cs_cl_malloc (mn, sizeof (cs_complex_t)) ; return ((!Prob->b || !Prob->x || !Prob->resid) ? free_problem (Prob) : Prob) ; }
/* cs_sparse: convert triplet form into compress-column form sparse matrix */ void mexFunction ( int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin [ ] ) { if (nargout > 1 || nargin != 3) { mexErrMsgTxt ("Usage: A = cs_sparse(i,j,x)") ; } if (mxIsComplex (pargin [2])) { #ifndef NCOMPLEX cs_cl *A, *C, *T, Tmatrix ; T = &Tmatrix ; /* get i,j,x and copy to triplet form */ T->nz = mxGetM (pargin [0]) ; T->p = cs_dl_mex_get_int (T->nz, pargin [0], &(T->n), 1) ; T->i = cs_dl_mex_get_int (T->nz, pargin [1], &(T->m), 1) ; cs_mex_check (1, T->nz, 1, 0, 0, 1, pargin [2]) ; T->x = cs_cl_mex_get_double (T->nz, pargin [2]) ; T->nzmax = T->nz ; C = cs_cl_compress (T) ; /* create sparse matrix C */ cs_cl_dupl (C) ; /* remove duplicates from C */ cs_cl_dropzeros (C) ; /* remove zeros from C */ A = cs_cl_transpose (C, -1) ; /* A=C.' */ cs_cl_spfree (C) ; pargout [0] = cs_cl_mex_put_sparse (&A) ; /* return A */ cs_free (T->p) ; cs_free (T->i) ; cs_free (T->x) ; /* free copy of complex values*/ #else mexErrMsgTxt ("complex matrices not supported") ; #endif } else { cs_dl *A, *C, *T, Tmatrix ; T = &Tmatrix ; /* get i,j,x and copy to triplet form */ T->nz = mxGetM (pargin [0]) ; T->p = cs_dl_mex_get_int (T->nz, pargin [0], &(T->n), 1) ; T->i = cs_dl_mex_get_int (T->nz, pargin [1], &(T->m), 1) ; cs_mex_check (1, T->nz, 1, 0, 0, 1, pargin [2]) ; T->x = mxGetPr (pargin [2]) ; T->nzmax = T->nz ; C = cs_dl_compress (T) ; /* create sparse matrix C */ cs_dl_dupl (C) ; /* remove duplicates from C */ cs_dl_dropzeros (C) ; /* remove zeros from C */ A = cs_dl_transpose (C, 1) ; /* A=C' */ cs_dl_spfree (C) ; pargout [0] = cs_dl_mex_put_sparse (&A) ; /* return A */ cs_free (T->p) ; cs_free (T->i) ; } }