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


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:31