Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
scrmatrix.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File scrmatrix.h
5 // Declaration of template class TSymCompRowMatrix ('template symmetric
6 // compressed-row matrix')
7 //
8 // The following typedefs for specific template types have been defined
9 // for convenience:
10 // RSymCompRowMatrix = TSymCompRowMatrix<double> ('real')
11 // FSymCompRowMatrix = TSymCompRowMatrix<float> ('float')
12 // CSymCompRowMatrix = TSymCompRowMatrix<complex> ('complex')
13 // ISymCompRowMatrix = TSymCompRowMatrix<int> ('integer')
14 //
15 // Inheritance:
16 // ------------
17 // TGenericSparseMatrix ----> TSymCompRowMatrix
18 // ==========================================================================
19 
20 #ifndef __SCRMATRIX_H
21 #define __SCRMATRIX_H
22 
23 #include "gsmatrix.h"
24 
25 // ==========================================================================
26 // Nonmember declarations
27 
28 template<class MT>class TSymCompRowMatrix;
29 
30 template<class MT>
31 bool CholeskyFactorize (const TSymCompRowMatrix<MT> &A, TCompRowMatrix<MT> &L,
32  TVector<MT> &d, bool recover = false);
33 
34 template<class MT>
35 std::istream &operator>> (std::istream &is, TSymCompRowMatrix<MT> &m);
36 
37 template<class MT>
38 std::ostream &operator<< (std::ostream &os, const TSymCompRowMatrix<MT> &m);
39 
40 // ==========================================================================
41 // class TSymCompRowMatrix
42 
43 template<class MT> class TSymCompRowMatrix: public TGenericSparseMatrix<MT> {
44 
45 public:
46 
47 
48  void New (int nrows, int ncols);
49 
51  TSymCompRowMatrix (int rows, int cols);
52 
53  TSymCompRowMatrix (int rows, int cols,
54  const int *rptr, const int *cidx, const MT *data=0);
55  // creates matrix of logical dimension rows x cols. fill-in structure
56  // is given by _rowptr and _colidx (but entries in the upper triangle are
57  // ignored.
58 
59  //~TSymCompRowMatrix ();
60 
62 
63  void Initialise (const int *_rowptr, const int *_colidx,
64  const MT *data = 0);
65  // Redefine sparse structure and assign data
66  // If data are not provided, all entries are set to zero
67 
68  void AllowColIndexing (bool yes);
69  // enable/disable column access index lists
70 
71  void SetColAccess (bool yes) const;
72  // create/delete index arrays for column access
73 
74  MT &operator() (int r, int c);
75  MT Get (int r, int c) const;
76 
77  void Add (int r, int c, const MT &val)
78  { if (r >= c) (*this)(r,c) += val; }
79 
80  TVector<MT> Row (int r) const;
81  // Returns row r as a vector
82 
83  TVector<MT> Col (int c) const
84  { ERROR_UNDEF; return TVector<MT>(); }
85  // Returns column c as a vector
86 
87  int SparseRow (int r, idxtype *ci, MT *rv) const;
88 
89  void RowScale (const TVector<MT> &scale)
90  { ERROR_UNDEF; }
91  // scales the rows with 'scale'
92 
93  void ColScale (const TVector<MT> &scale)
94  { ERROR_UNDEF; }
95  // scales the columns with 'scale'
96 
97  MT GetNext (int &r, int &c) const;
98 
99  void Ax (const TVector<MT> &x, TVector<MT> &b) const;
100 
101  void Ax (const TVector<MT> &x, TVector<MT> &b, int r1, int r2) const;
102 
103  void ATx (const TVector<MT> &x, TVector<MT> &b) const;
104 
105  int Shrink ();
106  // Remove all entries with zero value. Return value is the number of
107  // eliminated entries.
108 
109  void SymbolicCholeskyFactorize (idxtype *&frowptr, idxtype *&fcolidx) const;
110  // Calculate the sparse fill-in structure of the lower triangle of
111  // the Cholesky factorisation of the matrix (excluding diagonal)
112  // and return in index lists `frowptr' and 'fcolidx'
113 
114  friend bool CholeskyFactorize<> (const TSymCompRowMatrix<MT> &A,
115  TCompRowMatrix<MT> &L, TVector<MT> &d, bool recover);
116  // Perform Cholesky factorisation of A and return the result in lower
117  // triangle L and diagonal d (L does not contain diagonal elements)
118  // L must have been initialised with the index lists returned from
119  // A.CalculateCholeskyFillin()
120 
121  idxtype *rowptr;
122  idxtype *colidx;
123 
124  mutable idxtype *colptr, *rowidx, *vofs;
125  mutable bool col_access;
126  // index arrays and flag for column access
127 
128  friend std::istream &operator>> <> (std::istream &is,
130  friend std::ostream &operator<< <> (std::ostream &os,
131  const TSymCompRowMatrix<MT> &m);
132  // IO in generic format
133 
134 private:
135  int Get_index (int r, int c) const;
136  // returns offset into data array of element at row r and column c
137  // returns -1 if entry does not exist
138  // This always returns -1 if r < c
139 
140  mutable int iterator_pos;
141 
142  bool allow_col_indexing;
143 };
144 
145 // ==========================================================================
146 // friend prototypes
147 
148 #ifdef NEED_FRIEND_PT
149 
150 template<class MT>
151 bool CholeskyFactorize (const TSymCompRowMatrix<MT> &A, TCompRowMatrix<MT> &L,
152  TVector<MT> &d, bool recover);
153 
154 #endif // NEED_FRIEND_PT
155 
156 // ==========================================================================
157 // typedefs for specific instances of `TCompRowMatrix'
158 
163 typedef TSymCompRowMatrix<int> ISymCompRowMatrix; // 'integer'
164 
165 #endif // !__SCRMATRIX_H
Virtual base class for sparse matrix types.
Definition: gsmatrix.h:47
symmetric compressed row storage (sparse)
Definition: matrix.h:27
int SparseRow(int r, idxtype *ci, MT *rv) const
Returns a row of the matrix in sparse format.
MatrixStorage
Definition: matrix.h:20
TVector< MT > Col(int c) const
Returns a vector containing a copy of column 'c'.
Definition: scrmatrix.h:83
MT Get(int r, int c) const
Retrieve a matrix element.
Definition: scrmatrix.h:28
Compressed-row sparse matrix class.
Definition: cdmatrix.h:27
MatrixStorage StorageType() const
Matrix storage class.
Definition: scrmatrix.h:61
TVector< MT > Row(int r) const
Returns a vector containing a copy of row `r'.
void New(int nrows, int ncols)
Reset the matrix dimensions.