CLASS MANUAL
sparse.h
1 #ifndef __SPA__
2 #define __SPA__
3 /****************************************/
4 /* Sparse Matrix algorithms for CLASS */
5 /* 15/11 2010 */
6 /* Thomas Tram */
7 /****************************************/
8 #include "common.h"
9 
10 /* Structures: */
11 typedef struct sparse_matrix{
12  /* Sparse matrix in compressed column form: */
13  int ncols; /* Number of columns */
14  int nrows; /* Number of rows */
15  int maxnz; /* Maximum number of non-zero entries*/
16  int *Ap; /* Ap[0..ncols]. Ap[k+1]-Ap[k] is the number of entries in the k'th column. */
17  int *Ai; /* Ai[0..(maxnz-1)]. Contains the row indices of the entries. */
18  double *Ax; /* Ax[0..(maxnz-1)]. Contains the values of the entries. */
19 } sp_mat;
20 
21 typedef struct sparse_numerical{
22  /* Sparse LU decomposition along with enough information to do a fast refactorization: */
23  int n; /*Matrix assumed square, [nxn] */
24  sp_mat *L; /*L and U is the factors of the decomposed matrix.*/
25  sp_mat *U;
26  int **xi; /*xi[k] points to a row of xi, which holds the topological ordered indices.*/
27  int *topvec; /*topvec[k] holds the first index in xi[k].*/
28  int *pinv; /*Inverse row permutation. */
29  int *p; /*Row permutation. */
30  int *q; /* Column permutation */
31  int *wamd; /* Work array for sp_amd */
32  double *w; /* Work array for sp_lu */
33 } sp_num;
34 
35 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 /* Routines and macros: */
43 int sp_mat_alloc(sp_mat** A, int ncols, int nrows, int maxnz, ErrorMsg error_message);
44 int sp_mat_free(sp_mat *A);
45 int sp_num_alloc(sp_num** N, int n,ErrorMsg error_message);
46 int sp_num_free(sp_num *N);
47 int reachr(sp_mat *G, sp_mat *B,int k, int *xik,int *pinv);
48 void dfsr(int j, sp_mat *G, int *top, int *xik, int *pinv);
49 int sp_splsolve(sp_mat *G, sp_mat *B, int k, int*xik, int top, double *x, int *pinv);
50 int sp_ludcmp(sp_num *N, sp_mat *A, double pivtol);
51 int sp_lusolve(sp_num *N, double *b, double *x);
52 int sp_refactor(sp_num *N, sp_mat *A);
53 int column_grouping(sp_mat *G, int *col_g, int *col_wi);
54 int sp_amd(int *Cp, int *Ci, int n, int cnzmax, int *P, int *W);
55 int sp_wclear(int mark, int lemax, int *w, int n);
56 int sp_tdfs(int j, int k, int *head, const int *next, int *post, int *stack);
57 
58 
59 #define SPFLIP(i) (-(i)-2)
60 #define SPUNFLIP(i) (((i)<0) ? SPFLIP(i) : (i))
61 #define SPMARKED(w,j) (w[j] < 0)
62 #define SPMARK(w,j) {w[j] = SPFLIP(w[j]);}
63 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 
69 #endif