Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
bsmatrix.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib Martin Schweiger - 30.5.96
4 // File bsmatrix.h
5 // Declaration of template class TBandSymMatrix ('template banded symmetric
6 // matrix')
7 //
8 // The following typedefs for specific template types have been defined
9 // for convenience:
10 // RBandSymMatrix = TBandSymMatrix<double> ('real')
11 // FBandSymMatrix = TBandSymMatrix<float> ('float')
12 // CBandSymMatrix = TBandSymMatrix<complex> ('complex')
13 // IBandSymMatrix = TBandSymMatrix<int> ('integer')
14 // BandSymMatrix = TBandSymMatrix<double> backward compatibility
15 //
16 // Inheritance:
17 // ------------
18 // TRootMatrix ----> TBandSymMatrix
19 //
20 // Storage convention:
21 // -------------------
22 // full matrix: a b c 0 0 0 TBandSymMatrix: 0 0 a
23 // b a b c 0 0 0 b a
24 // c b a b c 0 c b a
25 // 0 c b a b c c b a
26 // 0 0 c b a b c b a
27 // 0 0 0 c b a c b a
28 // ==========================================================================
29 
30 #ifndef __BSMATRIX_H
31 #define __BSMATRIX_H
32 
33 // ==========================================================================
34 // class TBandSymMatrix
35 
36 template<class MT> class TBandSymMatrix: public TRootMatrix<MT> {
37 public:
38  TBandSymMatrix ();
39  TBandSymMatrix (int rc, int hb);
41  virtual ~TBandSymMatrix ();
42 
43  TVector<MT> &operator[] (int i) const;
44  // line-vector reference
45 
46  MT Get (int r, int c) const;
47  TVector<MT> Row (int r) const;
48  TVector<MT> Col (int c) const;
49  void New (int rc, int hb);
50  void Copy (const TBandSymMatrix &mat);
51 
52  void Unlink ();
53  // unlinks the matrix from its data block
54 
55  bool PIndex (int i, int j, int &r, int &c);
56  // converts logical matrix indices i,j to physical indices r,c
57  // returns TRUE if physical mapping is found, FALSE otherwise
58 
59  void LIndex (int r, int c, int &i, int &j);
60  // converts physical matrix position r,c to logical position i,j
61 
62  TVector<MT> operator* (const TVector<MT> &x) const;
63  // matrix * vector operation
64 
65  virtual void Ax (const TVector<MT> &x, TVector<MT> &b) const;
66  // alternative function to operator*; returns the result in parameter b
67  // and avoids local vector creation and copy
68 
69  MT LineVecMul (int line, const TVector<MT> &x) const;
70  // inner product of line `line' of the BandSymMatrix and vector `x'
71 
72  // **** friends ****
73 
74  friend TSquareMatrix<MT> ToSquare (TBandSymMatrix<MT> &A);
75  // converts the BandSymMatrix into a SquareMatrix
76 
77  friend TSymMatrix<MT> ToSym (TBandSymMatrix<MT> &A);
78  // converts the BandSymMatrix into a SymMatrix
79 
80  friend bool CHdecomp (TBandSymMatrix<MT> &a, bool recover = FALSE);
81  // Replaces `a' with its Cholesky decomposition.
82  // If recover=TRUE then the function will return even if the matrix is
83  // not positive definite, otherwise the error handler is invoked.
84  // If the function returns, the return value indicates success (i.e.
85  // positive-definiteness). A function which calls CHdecomp with
86  // recover=TRUE is responsible for handling failures.
87 
88  friend TVector<MT> CHsubst (const TBandSymMatrix<MT> &a,
89  const TVector<MT> &b);
90  // Cholesky backward and forward substitution
91 
92  friend TVector<MT> PCHsubst (const TBandSymMatrix<MT> &a,
93  const TVector<MT> &b, const TVector<MT> &p);
94  // preconditioned Cholesky substitution
95 
96 protected:
97  int hband;
98  void Allocate (int rc, int hband);
99  TVector<MT> *data; // data are stored in row vectors over the band
100 };
101 
102 
103 // ==========================================================================
104 // inline functions
105 
106 template<class MT>
108 : TRootMatrix<MT> ()
109 {
110  data = 0;
111  hband = 0;
112 }
113 
114 template<class MT>
115 inline TBandSymMatrix<MT>::TBandSymMatrix (int rc, int hb)
116 : TRootMatrix<MT> (rc, hband)
117 {
118  data = 0;
119  Allocate (rc, hb);
120 }
121 
122 template<class MT>
124 {
125  data = 0;
126  Allocate (A.rows, A.hband);
127  Copy (A);
128 }
129 
130 template<class MT>
132 {
133  Unlink ();
134 }
135 
136 // Unlink current data block and allocate a new one
137 template<class MT>
138 inline void TBandSymMatrix<MT>::New (int rc, int hb)
139 {
140  Unlink ();
141  Allocate (rc, hb);
142 }
143 
144 #ifndef MATH_DEBUG
145 template<class MT>
146 inline TVector<MT>& TBandSymMatrix<MT>::operator[] (int i) const
147 {
148  return data[i];
149 }
150 #endif // !MATH_DEBUG
151 
152 #ifndef MATH_DEBUG
153 template<class MT>
154 inline void TBandSymMatrix<MT>::Ax (const TVector<MT> &x, TVector<MT> &b) const
155 {
156  int r, c, k;
157  MT br;
158 
159  for (r = 0; r < rows; r++) {
160  TVector<MT> &lr = data[r];
161  for (c = hband-1, k = r, br = 0; c >= 0 && k >= 0; c--, k--)
162  br += lr[c] * x[k];
163  for (c = hband-2, k = r+1; c >= 0 && k < rows; c--, k++)
164  br += data[k][c] * x[k];
165  b[r] = br;
166  }
167 }
168 #endif // !MATH_DEBUG
169 
170 // ==========================================================================
171 // friend prototypes
172 
173 #ifdef NEED_FRIEND_PT
174 
175 template<class MT>
176 TSquareMatrix<MT> ToSquare (TBandSymMatrix<MT> &A);
177 
178 template<class MT>
180 
181 template<class MT>
182 bool CHdecomp (TBandSymMatrix<MT> &A, bool recover = FALSE);
183 
184 template<class MT>
185 TVector<MT> CHsubst (const TBandSymMatrix<MT> &A, const TVector<MT> &b);
186 
187 template<class MT>
188 TVector<MT> PCHsubst (const TBandSymMatrix<MT> &A, const TVector<MT> &b,
189  const TVector<MT> &p);
190 
191 #endif
192 
193 // ==========================================================================
194 // typedefs for specific instances of `TBandSymMatrix'
195 
196 typedef TBandSymMatrix<double> RBandSymMatrix; // 'real'
197 typedef TBandSymMatrix<float> FBandSymMatrix; // 'float'
198 typedef TBandSymMatrix<complex> CBandSymMatrix; // 'complex'
199 typedef TBandSymMatrix<int> IBandSymMatrix; // 'integer'
200 typedef TBandSymMatrix<double> BandSymMatrix; // for backward compatibility
201 
202 #endif // !__BSMATRIX_H
Definition: dnsmatrix.h:17
Definition: bsmatrix.h:36