EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cs_util.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* allocate a sparse matrix (triplet form or compressed-column form) */
3 cs *cs_spalloc (CS_INT m, CS_INT n, CS_INT nzmax, CS_INT values, CS_INT triplet)
4 {
5  cs *A = cs_calloc (1, sizeof (cs)) ; /* allocate the cs struct */
6  if (!A) return (NULL) ; /* out of memory */
7  A->m = m ; /* define dimensions and nzmax */
8  A->n = n ;
9  A->nzmax = nzmax = CS_MAX (nzmax, 1) ;
10  A->nz = triplet ? 0 : -1 ; /* allocate triplet or comp.col */
11  A->p = cs_malloc (triplet ? nzmax : n+1, sizeof (CS_INT)) ;
12  A->i = cs_malloc (nzmax, sizeof (CS_INT)) ;
13  A->x = values ? cs_malloc (nzmax, sizeof (CS_ENTRY)) : NULL ;
14  return ((!A->p || !A->i || (values && !A->x)) ? cs_spfree (A) : A) ;
15 }
16 
17 /* change the max # of entries sparse matrix */
19 {
20  CS_INT ok, oki, okj = 1, okx = 1 ;
21  if (!A) return (0) ;
22  if (nzmax <= 0) nzmax = (CS_CSC (A)) ? (A->p [A->n]) : A->nz ;
23  nzmax = CS_MAX (nzmax, 1) ;
24  A->i = cs_realloc (A->i, nzmax, sizeof (CS_INT), &oki) ;
25  if (CS_TRIPLET (A)) A->p = cs_realloc (A->p, nzmax, sizeof (CS_INT), &okj) ;
26  if (A->x) A->x = cs_realloc (A->x, nzmax, sizeof (CS_ENTRY), &okx) ;
27  ok = (oki && okj && okx) ;
28  if (ok) A->nzmax = nzmax ;
29  return (ok) ;
30 }
31 
32 /* free a sparse matrix */
34 {
35  if (!A) return (NULL) ; /* do nothing if A already NULL */
36  cs_free (A->p) ;
37  cs_free (A->i) ;
38  cs_free (A->x) ;
39  return ((cs *) cs_free (A)) ; /* free the cs struct and return NULL */
40 }
41 
42 /* free a numeric factorization */
44 {
45  if (!N) return (NULL) ; /* do nothing if N already NULL */
46  cs_spfree (N->L) ;
47  cs_spfree (N->U) ;
48  cs_free (N->pinv) ;
49  cs_free (N->B) ;
50  return ((csn *) cs_free (N)) ; /* free the csn struct and return NULL */
51 }
52 
53 /* free a symbolic factorization */
55 {
56  if (!S) return (NULL) ; /* do nothing if S already NULL */
57  cs_free (S->pinv) ;
58  cs_free (S->q) ;
59  cs_free (S->parent) ;
60  cs_free (S->cp) ;
61  cs_free (S->leftmost) ;
62  return ((css *) cs_free (S)) ; /* free the css struct and return NULL */
63 }
64 
65 /* allocate a cs_dmperm or cs_scc result */
67 {
68  csd *D ;
69  D = cs_calloc (1, sizeof (csd)) ;
70  if (!D) return (NULL) ;
71  D->p = cs_malloc (m, sizeof (CS_INT)) ;
72  D->r = cs_malloc (m+6, sizeof (CS_INT)) ;
73  D->q = cs_malloc (n, sizeof (CS_INT)) ;
74  D->s = cs_malloc (n+6, sizeof (CS_INT)) ;
75  return ((!D->p || !D->r || !D->q || !D->s) ? cs_dfree (D) : D) ;
76 }
77 
78 /* free a cs_dmperm or cs_scc result */
80 {
81  if (!D) return (NULL) ; /* do nothing if D already NULL */
82  cs_free (D->p) ;
83  cs_free (D->q) ;
84  cs_free (D->r) ;
85  cs_free (D->s) ;
86  return ((csd *) cs_free (D)) ; /* free the csd struct and return NULL */
87 }
88 
89 /* free workspace and return a sparse matrix result */
90 cs *cs_done (cs *C, void *w, void *x, CS_INT ok)
91 {
92  cs_free (w) ; /* free workspace */
93  cs_free (x) ;
94  return (ok ? C : cs_spfree (C)) ; /* return result if OK, else free it */
95 }
96 
97 /* free workspace and return CS_INT array result */
98 CS_INT *cs_idone (CS_INT *p, cs *C, void *w, CS_INT ok)
99 {
100  cs_spfree (C) ; /* free temporary matrix */
101  cs_free (w) ; /* free workspace */
102  return (ok ? p : (CS_INT *) cs_free (p)) ; /* return result, or free it */
103 }
104 
105 /* free workspace and return a numeric factorization (Cholesky, LU, or QR) */
106 csn *cs_ndone (csn *N, cs *C, void *w, void *x, CS_INT ok)
107 {
108  cs_spfree (C) ; /* free temporary matrix */
109  cs_free (w) ; /* free workspace */
110  cs_free (x) ;
111  return (ok ? N : cs_nfree (N)) ; /* return result if OK, else free it */
112 }
113 
114 /* free workspace and return a csd result */
115 csd *cs_ddone (csd *D, cs *C, void *w, CS_INT ok)
116 {
117  cs_spfree (C) ; /* free temporary matrix */
118  cs_free (w) ; /* free workspace */
119  return (ok ? D : cs_dfree (D)) ; /* return result if OK, else free it */
120 }
csn * cs_ndone(csn *N, cs *C, void *w, void *x, CS_INT ok)
Definition: cs_util.c:106
void * cs_free(void *p)
Definition: cs_malloc.c:22
css * cs_sfree(css *S)
Definition: cs_util.c:54
#define cs
Definition: cs.h:637
#define css
Definition: cs.h:688
#define CS_ENTRY
Definition: cs.h:635
cs * cs_spfree(cs *A)
Definition: cs_util.c:33
#define csd
Definition: cs.h:690
cs * cs_spalloc(CS_INT m, CS_INT n, CS_INT nzmax, CS_INT values, CS_INT triplet)
Definition: cs_util.c:3
#define CS_MAX(a, b)
Definition: cs.h:653
#define csn
Definition: cs.h:689
#define CS_CSC(A)
Definition: cs.h:659
void * cs_calloc(CS_INT n, size_t size)
Definition: cs_malloc.c:16
void * cs_realloc(void *p, CS_INT n, size_t size, CS_INT *ok)
Definition: cs_malloc.c:29
#define CS_INT
Definition: cs.h:627
csn * cs_nfree(csn *N)
Definition: cs_util.c:43
cs * cs_done(cs *C, void *w, void *x, CS_INT ok)
Definition: cs_util.c:90
CS_INT cs_sprealloc(cs *A, CS_INT nzmax)
Definition: cs_util.c:18
#define CS_TRIPLET(A)
Definition: cs.h:660
csd * cs_dalloc(CS_INT m, CS_INT n)
Definition: cs_util.c:66
void * cs_malloc(CS_INT n, size_t size)
Definition: cs_malloc.c:10
csd * cs_ddone(csd *D, cs *C, void *w, CS_INT ok)
Definition: cs_util.c:115
CS_INT * cs_idone(CS_INT *p, cs *C, void *w, CS_INT ok)
Definition: cs_util.c:98
csd * cs_dfree(csd *D)
Definition: cs_util.c:79