Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
spmatrix.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File spmatrix.h
5 // Declaration of template class TSparseMatrix ('template sparse matrix')
6 //
7 // The following typedefs for specific template types have been defined
8 // for convenience:
9 // RSparseMatrix = TSparseMatrix<double> ('real')
10 // FSparseMatrix = TSparseMatrix<float> ('float')
11 // CSparseMatrix = TSparseMatrix<complex> ('complex')
12 // ISparseMatrix = TSparseMatrix<int> ('integer')
13 // SparseMatrix = TSparseMatrix<double> for backward compatibility
14 //
15 // Inheritance:
16 // ------------
17 // TRootMatrix ----> TSparseMatrix
18 // ==========================================================================
19 
20 #ifndef __SPMATRIX_H
21 #define __SPMATRIX_H
22 
23 // ==========================================================================
24 // class TSparseMatrix
25 
26 template<class MT> class TSparseMatrix: public TRootMatrix<MT> {
27 public:
28  TSparseMatrix ();
29  TSparseMatrix (int r, int c);
30  TSparseMatrix (const TSparseMatrix<MT> &mat);
31  virtual ~TSparseMatrix ();
32  operator TMatrix<MT> ();
33  // cast to full matrix
34 
35  MatrixStorage StorageType() const { return MATRIX_COMPROW; }
36 
37  void Copy (const TSparseMatrix<MT> &mat);
38  void New (int r, int c);
39 
40  void Initialise (int *pindex);
41  // allocates pindex[i] entries for each row vector i
42 
43  void Initialise (int pindex);
44  // allocates pindex entries for each row vector
45 
46  void Initialise (int *rowptr, int *colidx);
47  // initialise matrix from the index lists of a compressed-row
48  // type matrix. This allocates storage AND initialises the index
49  // list.
50 
51  virtual MT Get (int r, int c) const;
52  virtual void Put (int r, int c, MT val);
53  virtual void Add (int r, int c, MT val);
54  TVector<MT> Row (int r) const;
55  TVector<MT> Col (int c) const;
56  void Unlink ();
57  void Shrink ();
58  // removes all unused entries (i.e. those with index pointer -1) from
59  // the matrix
60 
61  void GetFillIn (int *fillin);
62  // Returns the physical size of the matrix, by filling array 'fillin' with
63  // the pdim value of each row.
64  // 'fillin' must be allocated to the correct size before the call
65  // Note: this corresponds not necessarily to the number of nonzeros,
66  // since allocated elements may have value zero
67 
68  void Clear ();
69  // sets all entries to zero but without deallocating space
70 
71  TSparseMatrix<MT> operator= (const TSparseMatrix<MT> &mat);
72 
73  TSparseMatrix<MT> operator* (const MT &mt) const;
74  // scalar post multiplication
75 
76  TVector<MT> operator* (const TVector<MT> &x) const;
77  // matrix * vector operation
78 
79  virtual void Ax (const TVector<MT> &x, TVector<MT> &b) const;
80  // alternative function to operator*; returns the result in parameter b
81  // and avoids local vector creation and copy
82 
83  TVector<MT> ATx (const TVector<MT> &x) const;
84  // efficient coding of transpose(A) * x
85 
86  void ATx (const TVector<MT> &x, TVector<MT> &b) const;
87  // alternative function to ATx(Vector&); returns the result in parameter
88  // b and avoids local vector creation and copy
89 
90  TVector<MT> diag () const; // returns matrix diagonal as vector
91 
92  void SparseOutput (ostream &os) const;
93 
94  // friends
95  friend TSparseMatrix<MT> transpose (const TSparseMatrix<MT> &A);
96  // transpose
97 
98  friend void CHdecompFill (const TSparseMatrix<MT> &A,
100  // Calculates the fillin-structure of matrix A after Cholesky decomposition
101  // and returns it in lower triangular matrix L. (nonzero entries in the
102  // decomposed matrix are indicated by entries of 1 in L)
103  // Note that diagonal elements are omitted
104 
105  friend bool CHdecomp (const TSparseMatrix<MT> &A, TSparseMatrix<MT> &L,
106  TSparseMatrix<MT> &LT, TVector<MT> &d, bool reallocate,
107  bool recover);
108  // Returns Cholesky decomposition of sparse matrix A.
109  // On exit, vector d contains the diagonal of the decomposition, matrix
110  // L contains the lower triangle (without the diagonal) of the
111  // decomposition, and LT is the transpose of L
112  // If 'reallocate' is false then L and LT are not reset, i.e. are assumed
113  // to have entries for exactly the required elements allocated on entry.
114 
115  friend bool IncompleteCHdecomp (const TSparseMatrix<MT> &A,
117  bool reallocate, bool recover);
118  // Returns incomplete Cholesky decomposition of sparse matrix A.
119  // On exit, vector d contains the diagonal of the decomposition, matrix
120  // L contains the lower triangle (without the diagonal) of the
121  // decomposition, and LT is the transpose of L. L and LT retain the
122  // fill-in pattern of A. A is assumed symmetric, but must contain the full
123  // matrix (not triangle only)
124  // If 'reallocate' is false then L and LT are not reset, i.e. are assumed
125  // to have entries for exactly the required elements allocated on entry.
126 
127  friend void LineCHdecomp (const TSparseMatrix<MT> &A, TMatrix<MT> &T,
128  int p);
129  // line-wise Cholesky decomposition of matrix A. A must be banded with
130  // bandwidth b. T must be a bxb matrix whose contents must be retained
131  // between consecutive calls to LineCHdecomp. For the first call, T
132  // must be blank. p is the line number to be decomposed, and must be
133  // incremented by 1 for each consecutive call, starting from 0.
134  // On return, the first line of T contains the decomposition of line p.
135 
136  friend TVector<MT> CHsubst (const TSparseMatrix<MT> &L,
137  const TSparseMatrix<MT> &LT, const TVector<MT> &d,
138  const TVector<MT> &b);
139 
140  friend void CHsubst (const TSparseMatrix<MT> &L,
141  const TSparseMatrix<MT> &LT, const TVector<MT> &d,
142  const TVector<MT> &b, TVector<MT> &x);
143  // alternative syntax; returns solution in x, avoiding local creation
144  // and copy of vector
145 
146  friend TVector<MT> CGsolve (const TSparseMatrix<MT> &A,
147  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
148  int *niter = 0);
149  // Non-preconditioned conjugate gradient solver
150 
151  friend TVector<MT> PCGsolve (const TSparseMatrix<MT> &A,
152  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
153  int *niter = 0);
154  // Preconditioned conjugate gradient solution of Ax=b. A must be
155  // symmetric and positive definite. xinit may be set to an initial
156  // guess, otherwise x will be initialised to 0. If niter != 0 it
157  // returns the number of CG iterations. This version is internally
158  // preconditioned with the diagonal of A.
159 
160  friend TVector<MT> PCGsolve (const TSparseMatrix<MT> &A,
161  TVector<MT> P, const TVector<MT> &b, TVector<MT> *xinit = 0,
162  double err_limit = 1e-18, int *niter = 0);
163  // as above, but expects the diagonal preconditioner passed as argument
164  // P.
165 
166  friend TVector<MT> XCGsolve (const TSparseMatrix<MT> &A,
167  const TVector<MT> &P, const TVector<MT> &b, TVector<MT> *xinit,
168  double err_limit, int *niter);
169  // as above, but expects the diagonal preconditioner passed as argument
170  // P.
171 
172  friend TVector<MT> PCGsolve (const TSparseMatrix<MT> &A,
173  const TSparseMatrix<MT> &Pr, const TSparseMatrix<MT> &PrT,
174  const TVector<MT> &PrD, const TVector<MT> &b, TVector<MT> *xinit,
175  double err_limit, int *niter);
176  // as above, but uses a non-diagonal symmetric matrix as preconditioner:
177  // on input, Pr and PrT are the lower triangle and transpose of the
178  // preconditioner without the diagonal, and PrD is the diagonal
179 
180  friend TVector<MT> BiPCGsolve (const TSparseMatrix<MT> &A,
181  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
182  int *niter = 0);
183 
184  friend void ComplexBiCGsolve (const TSparseMatrix<MT> &Are,
185  const TSparseMatrix<MT> &Aim, const TVector<MT> &bre,
186  const TVector<MT> &bim, TVector<MT> &xre, TVector<MT> &xim,
187  double err_limit, int *niter);
188 
189 #ifdef MATH_DEBUG
190  TSparseVector<MT> &operator[] (int i) const; // out-of-line
191 #else
192  TSparseVector<MT> &operator[] (int i) const { return data[i]; }
193 #endif
194 
195 protected:
196  virtual void Allocate (int r, int c);
197  TSparseVector<MT> *data;
198 };
199 
200 // ==========================================================================
201 // inline functions
202 
203 #ifndef MATH_DEBUG
204 template<class MT>
205 inline void TSparseMatrix<MT>::Ax (const TVector<MT> &x, TVector<MT> &b) const
206 {
207  for (int r = 0; r < rows; r++) b[r] = data[r] & x;
208 }
209 #endif // !MATH_DEBUG
210 
211 // ==========================================================================
212 // friend prototypes
213 
214 #ifdef NEED_FRIEND_PT
215 
216 template<class MT>
217 TSparseMatrix<MT> transpose (const TSparseMatrix<MT> &A);
218 
219 template<class MT>
220 void CHdecompFill (const TSparseMatrix<MT> &A, TSparseMatrix<int> &L);
221 
222 template<class MT>
223 bool CHdecomp (const TSparseMatrix<MT> &A, TSparseMatrix<MT> &L,
224  TSparseMatrix<MT> &LT, TVector<MT> &d, bool reallocate, bool recover);
225 
226 template<class MT>
227 bool IncompleteCHdecomp (const TSparseMatrix<MT> &A, TSparseMatrix<MT> &L,
228  TSparseMatrix<MT> &LT, TVector<MT> &d, bool reallocate, bool recover);
229 
230 template<class MT>
231 void LineCHdecomp (const TSparseMatrix<MT> &A, TMatrix<MT> &T, int p);
232 
233 template<class MT>
234 TVector<MT> CHsubst (const TSparseMatrix<MT> &L, const TSparseMatrix<MT> &LT,
235  const TVector<MT> &d, const TVector<MT> &b);
236 
237 template<class MT>
238 void CHsubst (const TSparseMatrix<MT> &L, const TSparseMatrix<MT> &LT,
239  const TVector<MT> &d, const TVector<MT> &b, TVector<MT> &x);
240 
241 template<class MT>
242 TVector<MT> CGsolve (const TSparseMatrix<MT> &A,
243  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
244  int *niter = 0);
245 
246 template<class MT>
247 TVector<MT> PCGsolve (const TSparseMatrix<MT> &A, TVector<MT> P,
248  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
249  int *niter = 0);
250 
251 template<class MT>
252 TVector<MT> PCGsolve (const TSparseMatrix<MT> &A,
253  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
254  int *niter = 0);
255 
256 template<class MT>
257 TVector<MT> XCGsolve (const TSparseMatrix<MT> &A,
258  const TVector<MT> &P, const TVector<MT> &b, TVector<MT> *xinit = 0,
259  double err_limit = 1e-18, int *niter = 0);
260 
261 template<class MT>
262 TVector<MT> PCGsolve (const TSparseMatrix<MT> &A, const TSparseMatrix<MT> &Pr,
263  const TSparseMatrix<MT> &PrT, const TVector<MT> &PrD, const TVector<MT> &b,
264  TVector<MT> *xinit, double err_limit, int *niter);
265 
266 template<class MT>
267 TVector<MT> BiPCGsolve (const TSparseMatrix<MT> &A,
268  const TVector<MT> &b, TVector<MT> *xinit = 0, double err_limit = 1e-18,
269  int *niter = 0);
270 
271 #endif
272 
273 // ==========================================================================
274 // typedefs for specific instances of `TMatrix'
275 
276 typedef TSparseMatrix<double> RSparseMatrix; // 'real'
277 typedef TSparseMatrix<float> FSparseMatrix; // 'float'
278 typedef TSparseMatrix<complex> CSparseMatrix; // 'complex'
279 typedef TSparseMatrix<int> ISparseMatrix; // 'integer'
280 typedef TSparseMatrix<double> SparseMatrix; // for backward compatibility
281 
282 
283 #endif // !__SPMATRIX_H
compressed row storage (sparse)
Definition: matrix.h:25
MatrixStorage
Definition: matrix.h:20
Virtual base class for all matrix types (dense and sparse)
Definition: matrix.h:43
Definition: spmatrix.h:26