cs_util.c
Go to the documentation of this file.
00001 #include "cs.h"
00002 /* allocate a sparse matrix (triplet form or compressed-column form) */
00003 cs *cs_spalloc (int m, int n, int nzmax, int values, int triplet)
00004 {
00005     cs *A = cs_calloc (1, sizeof (cs)) ;    /* allocate the cs struct */
00006     if (!A) return (NULL) ;                 /* out of memory */
00007     A->m = m ;                              /* define dimensions and nzmax */
00008     A->n = n ;
00009     A->nzmax = nzmax = CS_MAX (nzmax, 1) ;
00010     A->nz = triplet ? 0 : -1 ;              /* allocate triplet or comp.col */
00011     A->p = cs_malloc (triplet ? nzmax : n+1, sizeof (int)) ;
00012     A->i = cs_malloc (nzmax, sizeof (int)) ;
00013     A->x = values ? cs_malloc (nzmax, sizeof (double)) : NULL ;
00014     return ((!A->p || !A->i || (values && !A->x)) ? cs_spfree (A) : A) ;
00015 }
00016 
00017 /* change the max # of entries sparse matrix */
00018 int cs_sprealloc (cs *A, int nzmax)
00019 {
00020     int ok, oki, okj = 1, okx = 1 ;
00021     if (!A) return (0) ;
00022     if (nzmax <= 0) nzmax = (CS_CSC (A)) ? (A->p [A->n]) : A->nz ;
00023     A->i = cs_realloc (A->i, nzmax, sizeof (int), &oki) ;
00024     if (CS_TRIPLET (A)) A->p = cs_realloc (A->p, nzmax, sizeof (int), &okj) ;
00025     if (A->x) A->x = cs_realloc (A->x, nzmax, sizeof (double), &okx) ;
00026     ok = (oki && okj && okx) ;
00027     if (ok) A->nzmax = nzmax ;
00028     return (ok) ;
00029 }
00030 
00031 /* free a sparse matrix */
00032 cs *cs_spfree (cs *A)
00033 {
00034     if (!A) return (NULL) ;     /* do nothing if A already NULL */
00035     cs_free (A->p) ;
00036     cs_free (A->i) ;
00037     cs_free (A->x) ;
00038     return (cs_free (A)) ;      /* free the cs struct and return NULL */
00039 }
00040 
00041 /* free a numeric factorization */
00042 csn *cs_nfree (csn *N)
00043 {
00044     if (!N) return (NULL) ;     /* do nothing if N already NULL */
00045     cs_spfree (N->L) ;
00046     cs_spfree (N->U) ;
00047     cs_free (N->pinv) ;
00048     cs_free (N->B) ;
00049     return (cs_free (N)) ;      /* free the csn struct and return NULL */
00050 }
00051 
00052 /* free a symbolic factorization */
00053 css *cs_sfree (css *S)
00054 {
00055     if (!S) return (NULL) ;     /* do nothing if S already NULL */
00056     cs_free (S->pinv) ;
00057     cs_free (S->q) ;
00058     cs_free (S->parent) ;
00059     cs_free (S->cp) ;
00060     cs_free (S->leftmost) ;
00061     return (cs_free (S)) ;      /* free the css struct and return NULL */
00062 }
00063 
00064 /* allocate a cs_dmperm or cs_scc result */
00065 csd *cs_dalloc (int m, int n)
00066 {
00067     csd *D ;
00068     D = cs_calloc (1, sizeof (csd)) ;
00069     if (!D) return (NULL) ;
00070     D->p = cs_malloc (m, sizeof (int)) ;
00071     D->r = cs_malloc (m+6, sizeof (int)) ;
00072     D->q = cs_malloc (n, sizeof (int)) ;
00073     D->s = cs_malloc (n+6, sizeof (int)) ;
00074     return ((!D->p || !D->r || !D->q || !D->s) ? cs_dfree (D) : D) ;
00075 }
00076 
00077 /* free a cs_dmperm or cs_scc result */
00078 csd *cs_dfree (csd *D)
00079 {
00080     if (!D) return (NULL) ;     /* do nothing if D already NULL */
00081     cs_free (D->p) ;
00082     cs_free (D->q) ;
00083     cs_free (D->r) ;
00084     cs_free (D->s) ;
00085     return (cs_free (D)) ;
00086 }
00087 
00088 /* free workspace and return a sparse matrix result */
00089 cs *cs_done (cs *C, void *w, void *x, int ok)
00090 {
00091     cs_free (w) ;                       /* free workspace */
00092     cs_free (x) ;
00093     return (ok ? C : cs_spfree (C)) ;   /* return result if OK, else free it */
00094 }
00095 
00096 /* free workspace and return int array result */
00097 int *cs_idone (int *p, cs *C, void *w, int ok)
00098 {
00099     cs_spfree (C) ;                     /* free temporary matrix */
00100     cs_free (w) ;                       /* free workspace */
00101     return (ok ? p : cs_free (p)) ;     /* return result if OK, else free it */
00102 }
00103 
00104 /* free workspace and return a numeric factorization (Cholesky, LU, or QR) */
00105 csn *cs_ndone (csn *N, cs *C, void *w, void *x, int ok)
00106 {
00107     cs_spfree (C) ;                     /* free temporary matrix */
00108     cs_free (w) ;                       /* free workspace */
00109     cs_free (x) ;
00110     return (ok ? N : cs_nfree (N)) ;    /* return result if OK, else free it */
00111 }
00112 
00113 /* free workspace and return a csd result */
00114 csd *cs_ddone (csd *D, cs *C, void *w, int ok)
00115 {
00116     cs_spfree (C) ;                     /* free temporary matrix */
00117     cs_free (w) ;                       /* free workspace */
00118     return (ok ? D : cs_dfree (D)) ;    /* return result if OK, else free it */
00119 }


hogman_minimal
Author(s): Maintained by Juergen Sturm
autogenerated on Mon Oct 6 2014 00:06:58