Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
cdmatrix.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File cdmatrix.h
5 // Declaration of template class TCoordMatrix ('template coordinate storage
6 // matrix')
7 //
8 // The following typedefs for specific template types have been defined
9 // for convenience:
10 // RCoordMatrix = TCoordMatrix<double> ('real')
11 // FCoordMatrix = TCoordMatrix<float> ('float')
12 // CCoordMatrix = TCoordMatrix<complex> ('complex')
13 // ICoordMatrix = TCoordMatrix<int> ('integer')
14 //
15 // Inheritance:
16 // ------------
17 // TGenericSparseMatrix ----> TCoordMatrix
18 // ==========================================================================
19 
20 #ifndef __CDMATRIX_H
21 #define __CDMATRIX_H
22 
23 // ==========================================================================
24 // Nonmember declarations
25 
26 template<class MT> class TCoordMatrix;
27 template<class MT> class TCompRowMatrix;
28 
29 template<class MT>
31  const TCoordMatrix<MT> &B);
32 
33 template<class MT>
35  const TCoordMatrix<MT> &B);
36 
37 template<class MT>
38 std::istream &operator>> (std::istream &is, TCoordMatrix<MT> &m);
39 
40 template<class MT>
41 std::ostream &operator<< (std::ostream &os, const TCoordMatrix<MT> &m);
42 
43 // ==========================================================================
44 // class TCoordMatrix
45 // ==========================================================================
57 template<class MT> class TCoordMatrix: public TGenericSparseMatrix<MT> {
58 
59  friend class TCompRowMatrix<MT>;
60 
61 public:
62 
66  TCoordMatrix ();
67 
75  TCoordMatrix (int rows, int cols);
76 
77  TCoordMatrix (int rows, int cols, int vals, idxtype *_rowidx, idxtype *_colidx,
78  MT *_data = 0);
79  TCoordMatrix (const TCoordMatrix<MT> &m);
80  ~TCoordMatrix ();
81 
83 
84  void New (int rows, int cols);
85  // reset logical dimension to rows x cols
86  // deallocate data and index lists, i.e. create an empty matrix
87 
88  void Unlink ();
89 
90  void Initialise (int nv, idxtype *_rowidx, idxtype *_colidx, MT *data = 0);
91  // reallocate data block to size nv, and assign data and index lists
92 
93  TCoordMatrix &operator= (const TCoordMatrix<MT> &m);
94  // assignment operator
95 
96  MT &operator() (int r, int c);
97  MT Get (int r, int c) const;
98  // access to element in row r, col c.
99  // First version for write acces, second for read access
100 
101  bool Exists (int r, int c) const;
102  // true if an entry for the element is allocated
103 
104  TVector<MT> Row (int r) const;
105  // Returns row r as a vector
106 
107  TVector<MT> Col (int c) const;
108  // Returns column c as a vector
109 
110  int SparseRow (int r, idxtype *ci, MT *rv) const;
111  // See TRootMatrix
112  // Note: 1. returned column indices may be unsorted
113  // 2. This is expensive since complete matrix must be traversed
114 
115  void ColScale (const TVector<MT> &scale);
116  // scales the columns with 'scale'
117 
118  void RowScale (const TVector<MT> &scale);
119  // scales the rows with 'scale'
120 
121  MT GetNext (int &r, int &c) const;
122 
123  void Ax (const TVector<MT> &x, TVector<MT> &b) const;
124  void Ax (const TVector<MT> &x, TVector<MT> &b, int r1, int r2) const;
125  void ATx (const TVector<MT> &x, TVector<MT> &b) const;
126 
127  void Transpone ();
128  // Replace matrix with its transpose
129 
130  inline MT &Val (int i) { return this->val[i]; }
131  inline const MT &Val (int i) const { return this->val[i]; }
132  // return i-th value of data vector
133 
134  void Sort (bool roworder=true) const;
135 
136  inline const idxtype *RowIndex () const { return rowidx; }
137  inline const idxtype *ColIndex () const { return colidx; }
138 
139  inline idxtype RowIndex (int i) const { return rowidx[i]; }
140  inline idxtype ColIndex (int i) const { return colidx[i]; }
141  // Index list entries. These should probably not be public, but
142  // can't be avoided now
143 
153  friend TCoordMatrix<MT> cath<> (const TCoordMatrix<MT> &A,
154  const TCoordMatrix<MT> &B);
155 
166  friend TCoordMatrix<MT> catv<> (const TCoordMatrix<MT> &A,
167  const TCoordMatrix<MT> &B);
168 
169  friend std::istream &operator>> <> (std::istream &is, TCoordMatrix<MT> &m);
170  friend std::ostream &operator<< <> (std::ostream &os,
171  const TCoordMatrix<MT> &m);
172 
173 private:
174  int Get_index (int r, int c) const;
175 
176  int Insert (int r, int c, MT v = 0);
177  // create an entry for row r, col c, set value to v,
178  // and return the index into the data array
179  // Assumes that an entry for (r,c) does not exist yet!
180 
181  idxtype *rowidx;
182  idxtype *colidx;
183  mutable int iterator_pos;
184 };
185 
186 // ==========================================================================
187 // typedefs for specific instances of `TCoordMatrix'
188 
189 typedef TCoordMatrix<double> RCoordMatrix; // 'real'
190 typedef TCoordMatrix<float> FCoordMatrix; // 'float'
191 typedef TCoordMatrix<std::complex<double> > CCoordMatrix; // 'complex'
192 typedef TCoordMatrix<std::complex<float> > SCCoordMatrix; // 'single complex'
193 typedef TCoordMatrix<int> ICoordMatrix; // 'integer'
194 
195 // ==========================================================================
196 // extern declarations of TCoordMatrix (only required for VS)
197 
198 #ifndef __CDMATRIX_CC
199 //extern template class MATHLIB TCoordMatrix<double>;
200 //extern template class MATHLIB TCoordMatrix<float>;
201 //extern template class MATHLIB TCoordMatrix<toast::complex>;
202 //extern template class MATHLIB TCoordMatrix<scomplex>;
203 //extern template class MATHLIB TCoordMatrix<int>;
204 #endif // !__CDMATRIX_CC
205 
206 #endif // !__CDMATRIX_H
Virtual base class for sparse matrix types.
Definition: gsmatrix.h:47
Coordinate-storage sparse matrix class.
Definition: cdmatrix.h:26
TVector< MT > Col(int c) const
Returns a vector containing a copy of column 'c'.
void New(int rows, int cols)
Reset the matrix dimensions.
MatrixStorage
Definition: matrix.h:20
friend TCompRowMatrix< MT > catv(const TCompRowMatrix< MT > &A, const TCompRowMatrix< MT > &B)
Concatenate two matrices vertically.
TVector< MT > Row(int r) const
Returns a vector containing a copy of row `r'.
bool Exists(int r, int c) const
Checks allocation of a matrix element.
Compressed-row sparse matrix class.
Definition: cdmatrix.h:27
friend TCompRowMatrix< MT > cath(const TCompRowMatrix< MT > &A, const TCompRowMatrix< MT > &B)
Concatenate two matrices horizontally.
MT Get(int r, int c) const
Retrieve a matrix element.
int SparseRow(int r, idxtype *ci, MT *rv) const
Returns a row of the matrix in sparse format.
MatrixStorage StorageType() const
Matrix storage class.
Definition: cdmatrix.h:82
coordinate storage (sparse)
Definition: matrix.h:24
TCoordMatrix()
Creates a coordinate matrix of dimension 0 x 0.