OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FMatrix.h
Go to the documentation of this file.
1 #ifndef CLASSIC_FMatrix_HH
2 #define CLASSIC_FMatrix_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: FMatrix.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.2.2.2 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: FMatrix<T,R,C>
13 //
14 // ------------------------------------------------------------------------
15 // Class category: FixedAlgebra
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2004/11/18 22:18:06 $
19 // $Author: jsberg $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include "FixedAlgebra/FArray2D.h"
24 #include "FixedAlgebra/FVector.h"
25 #include "Utilities/SizeError.h"
26 #include <algorithm>
27 #include <numeric>
28 #include <functional>
29 
30 // Template class FMatrix<T,R,C>
31 // ------------------------------------------------------------------------
33 // This class implements the basic algebraic operations on general matrices,
34 // which need not be square.
35 // The destructor generated by the compiler does the correct thing,
36 // and is not defined for speed.
37 
38 template<class T, int R, int C>
39 class FMatrix: public FArray2D<T, R, C> {
40 
41 public:
42 
44  // Construct zero matrix.
45  FMatrix();
46 
48  FMatrix(const FMatrix &);
49 
51  FMatrix(const FArray2D<T, R, C> &);
52 
54  // Set all matrix elements to [b]t[/b].
55  explicit FMatrix(const T &t);
56 
58  FMatrix &operator=(const FMatrix &);
59 
61  // Fill all matrix elements from the C-array [b]t[/b].
62  explicit FMatrix(const T *t);
63 
66 
68  FMatrix &operator*=(const T &);
69 
71  FMatrix &operator/=(const T &);
72 
74  FMatrix &operator+=(const FMatrix &);
75 
77  FMatrix &operator-=(const FMatrix &);
78 
81 };
82 
83 
84 // ------------------------------------------------------------------------
85 
87 template <class T, int R, int C>
89 
91 template <class T, int R, int C>
93 
95 template <class T, int R, int C>
97 
99 template <class T, int R, int C>
101 
103 template <class T, int R, int C>
105 
107 template <class T, int R, int C>
108 FMatrix<T, R, C> operator*(const FMatrix<T, R, C> &, const T &);
109 
111 template <class T, int R, int C>
112 FMatrix<T, R, C> operator*(const T &, const FMatrix<T, R, C> &);
113 
115 template <class T, int R, int C>
116 FMatrix<T, R, C> operator/(const FMatrix<T, R, C> &, const T &);
117 
119 template <class T, int C>
120 FMatrix<T, C, C> operator+(const FMatrix<T, C, C> &, const T &t);
121 
123 template <class T, int C>
124 FMatrix<T, C, C> operator-(const FMatrix<T, C, C> &, const T &t);
125 
126 
127 // Public methods of FMatrix<T,R,C>.
128 // ------------------------------------------------------------------------
129 
130 template<class T, int R, int C>
132  FArray2D<T, R, C>()
133 {}
134 
135 
136 template<class T, int R, int C>
138  FArray2D<T, R, C>(rhs)
139 {}
140 
141 
142 template<class T, int R, int C>
144  FArray2D<T, R, C>(rhs)
145 {}
146 
147 
148 template<class T, int R, int C>
150  FArray2D<T, R, C>(rhs)
151 {}
152 
153 
154 template<class T, int R, int C>
156  FArray2D<T, R, C>(rhs)
157 {}
158 
159 
160 template<class T, int R, int C>
163  return *this;
164 }
165 
166 
167 template<class T, int R, int C>
170  return *this;
171 }
172 
173 
174 template<class T, int R, int C>
176  std::transform(this->begin(), this->end(), this->begin(),
177  std::bind(std::multiplies<T>(), std::placeholders::_1, rhs));
178  return *this;
179 }
180 
181 
182 template<class T, int R, int C>
184  std::transform(this->begin(), this->end(), this->begin(),
185  std::bind(std::divides<T>(), std::placeholders::_1, rhs));
186  return *this;
187 }
188 
189 
190 template<class T, int R, int C>
192  std::transform(this->begin(), this->end(), array.begin(), this->begin(),
193  std::plus<T>());
194  return *this;
195 }
196 
197 
198 template<class T, int R, int C>
200  std::transform(this->begin(), this->end(), array.begin(), this->begin(),
201  std::minus<T>());
202  return *this;
203 }
204 
205 
206 template <class T, int R, int C>
208  FMatrix<T, C, R> result;
209 
210  for(int i = 0; i < R; ++i) {
211  std::copy(this->row_begin(i), this->row_end(i), result.col_begin(i));
212  }
213 
214  return result;
215 }
216 
217 
218 // Global functions acting on FMatrix<T,R,C>.
219 // ------------------------------------------------------------------------
220 
221 template<class T, int R, int C>
223  FMatrix<T, R, C> result;
224  std::transform(rhs.begin(), rhs.end(), result.begin(), std::negate<T>());
225  return result;
226 }
227 
228 
229 template<class T, int R, int C>
231  FMatrix<T, R, C> result(lhs);
232  return result += rhs;
233 }
234 
235 
236 template<class T, int R, int C>
238  FMatrix<T, R, C> result(lhs);
239  return result -= rhs;
240 }
241 
242 
243 template <class T, int R, int I, int C>
245  FMatrix<T, R, C> result;
246 
247  for(int i = 0; i < R; i++) {
248  typename FMatrix<T, R, I>::const_row_iterator i1 = lhs.row_begin(i);
249  typename FMatrix<T, R, I>::const_row_iterator i2 = lhs.row_end(i);
250 
251  for(int j = 0; j < C; j++) {
252  result(i, j) = std::inner_product(i1, i2, rhs.col_begin(j), T(0));
253  }
254  }
255 
256  return result;
257 }
258 
259 
260 template<class T, int R, int C>
262  FVector<T, R> result;
263 
264  for(int i = 0; i < R; ++i) {
265  result[i] = std::inner_product(lhs.row_begin(i), lhs.row_end(i),
266  rhs.begin(), T(0));
267  }
268 
269  return result;
270 }
271 
272 
273 template<class T, int R, int C>
275  FVector<T, C> result;
276 
277  for(int j = 0; j < C; j++) {
278  result[j] = std::inner_product(lhs.begin(), lhs.end(),
279  rhs.col_begin(j), T(0));
280  }
281 
282  return result;
283 }
284 
285 
286 template<class T, int R, int C>
287 FMatrix<T, R, C> operator*(const FMatrix<T, R, C> &lhs, const T &rhs) {
288  FMatrix<T, R, C> result(lhs);
289  return result *= rhs;
290 }
291 
292 
293 template<class T, int R, int C>
294 FMatrix<T, R, C> operator*(const T &lhs, const FMatrix<T, R, C> &rhs) {
295  FMatrix<T, R, C> result(rhs);
296  return result *= lhs;
297 }
298 
299 
300 template<class T, int R, int C>
301 FMatrix<T, R, C> operator/(const FMatrix<T, R, C> &lhs, const T &rhs) {
302  FMatrix<T, R, C> result(lhs);
303  return result /= rhs;
304 }
305 
306 
307 // Special functions on square matrices.
308 // ------------------------------------------------------------------------
309 
310 template<class T, int C>
311 FMatrix<T, C, C> operator+(const FMatrix<T, C, C> &lhs, const T &rhs) {
312  FMatrix<T, C, C> result(lhs);
313  for(int i = 0; i < C; i++) result[i][i] += rhs;
314  return result;
315 }
316 
317 
318 template<class T, int C>
319 FMatrix<T, C, C> operator-(const FMatrix<T, C, C> &lhs, const T &rhs) {
320  FMatrix<T, C, C> result(lhs);
321  for(int i = 0; i < C; i++) result[i][i] -= rhs;
322  return result;
323 }
324 
325 #endif // CLASSIC_FMatrix_HH
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
FMatrix & operator=(const FMatrix &)
Assignment.
Matrix< T > operator/(const Matrix< T > &, const T &)
Matrix divided by scalar.
Definition: Matrix.h:329
Definition: rbendmap.h:8
A templated representation for matrices.
Definition: IdealMapper.h:26
A templated representation for vectors.
Definition: PartBunchBase.h:26
FMatrix()
Default constructor.
Definition: FMatrix.h:131
FMatrix & operator*=(const T &)
Multiply by scalar and assign.
Definition: FMatrix.h:175
FMatrix & operator+=(const FMatrix &)
Add matrix and assign.
Definition: FMatrix.h:191
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
iterator begin()
Get beginning of data.
iterator end()
Get pointer past end of data.
row_iterator row_end(int r)
Get row iterator.
FMatrix< T, C, R > transpose() const
FMatrix transpose.
Definition: FMatrix.h:207
FMatrix & operator/=(const T &)
Divide by scalar and assign.
Definition: FMatrix.h:183
col_iterator col_begin(int c)
Get column iterator.
const FArray2D< T, M, N > & operator=(const FArray2D< T, M, N > &)
Assignment.
Definition: FArray2D.h:228
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
const T * const_row_iterator
Iterator for access by rows.
Definition: FArray2D.h:57
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
FMatrix & operator-=(const FMatrix &)
Subtract ,atrix and assign.
Definition: FMatrix.h:199
A templated representation for 2-dimensional arrays.
Definition: FArray2D.h:40
row_iterator row_begin(int r)
Get row iterator.