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