OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
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
38template<class T, int R, int C>
39class FMatrix: public FArray2D<T, R, C> {
40
41public:
42
44 // Construct zero matrix.
46
48 FMatrix(const FMatrix &);
49
52
54 // Set all matrix elements to [b]t[/b].
55 explicit FMatrix(const T &t);
56
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
75
78
81};
82
83
84// ------------------------------------------------------------------------
85
87template <class T, int R, int C>
89
91template <class T, int R, int C>
93
95template <class T, int R, int C>
97
99template <class T, int R, int C>
101
103template <class T, int R, int C>
105
107template <class T, int R, int C>
109
111template <class T, int R, int C>
113
115template <class T, int R, int C>
117
119template <class T, int C>
121
123template <class T, int C>
125
126
127// Public methods of FMatrix<T,R,C>.
128// ------------------------------------------------------------------------
129
130template<class T, int R, int C>
132 FArray2D<T, R, C>()
133{}
134
135
136template<class T, int R, int C>
138 FArray2D<T, R, C>(rhs)
139{}
140
141
142template<class T, int R, int C>
144 FArray2D<T, R, C>(rhs)
145{}
146
147
148template<class T, int R, int C>
150 FArray2D<T, R, C>(rhs)
151{}
152
153
154template<class T, int R, int C>
156 FArray2D<T, R, C>(rhs)
157{}
158
159
160template<class T, int R, int C>
163 return *this;
164}
165
166
167template<class T, int R, int C>
170 return *this;
171}
172
173
174template<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
182template<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
190template<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
198template<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
206template <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
221template<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
229template<class T, int R, int C>
231 FMatrix<T, R, C> result(lhs);
232 return result += rhs;
233}
234
235
236template<class T, int R, int C>
238 FMatrix<T, R, C> result(lhs);
239 return result -= rhs;
240}
241
242
243template <class T, int R, int I, int C>
245 FMatrix<T, R, C> result;
246
247 for(int i = 0; i < R; 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
260template<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
273template<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
286template<class T, int R, int C>
288 FMatrix<T, R, C> result(lhs);
289 return result *= rhs;
290}
291
292
293template<class T, int R, int C>
295 FMatrix<T, R, C> result(rhs);
296 return result *= lhs;
297}
298
299
300template<class T, int R, int C>
302 FMatrix<T, R, C> result(lhs);
303 return result /= rhs;
304}
305
306
307// Special functions on square matrices.
308// ------------------------------------------------------------------------
309
310template<class T, int C>
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
318template<class T, int C>
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
FMatrix< T, R, C > operator+(const FMatrix< T, R, C > &, const FMatrix< T, R, C > &)
Add.
Definition: FMatrix.h:230
FMatrix< T, R, C > operator/(const FMatrix< T, R, C > &, const T &)
Matrix divided by scalar.
Definition: FMatrix.h:301
FMatrix< T, R, C > operator-(const FMatrix< T, R, C > &)
Unary minus.
Definition: FMatrix.h:222
FVector< T, R > operator*(const FMatrix< T, R, C > &, const FVector< T, C > &)
Multiply by column vector.
Definition: FMatrix.h:261
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
A templated representation for 2-dimensional arrays.
Definition: FArray2D.h:40
iterator end()
Get pointer past end of data.
Definition: FArray2D.h:277
row_iterator row_end(int r)
Get row iterator.
Definition: FArray2D.h:304
const FArray2D< T, M, N > & operator=(const FArray2D< T, M, N > &)
Assignment.
Definition: FArray2D.h:228
col_iterator col_begin(int c)
Get column iterator.
Definition: FArray2D.h:343
const T * const_row_iterator
Iterator for access by rows.
Definition: FArray2D.h:57
row_iterator row_begin(int r)
Get row iterator.
Definition: FArray2D.h:295
iterator begin()
Get beginning of data.
Definition: FArray2D.h:271
A templated representation for matrices.
Definition: FMatrix.h:39
FMatrix & operator+=(const FMatrix &)
Add matrix and assign.
Definition: FMatrix.h:191
FMatrix & operator-=(const FMatrix &)
Subtract ,atrix and assign.
Definition: FMatrix.h:199
FMatrix & operator/=(const T &)
Divide by scalar and assign.
Definition: FMatrix.h:183
FMatrix(const FArray2D< T, R, C > &)
Conversion from two-dimensional array.
Definition: FMatrix.h:143
FMatrix(const T *t)
Constructor.
Definition: FMatrix.h:155
FMatrix(const T &t)
Constructor.
Definition: FMatrix.h:149
FMatrix & operator=(const FArray2D< T, R, C > &)
Convert and assign.
Definition: FMatrix.h:168
FMatrix & operator*=(const T &)
Multiply by scalar and assign.
Definition: FMatrix.h:175
FMatrix< T, C, R > transpose() const
FMatrix transpose.
Definition: FMatrix.h:207
FMatrix(const FMatrix &)
Copy constructor.
Definition: FMatrix.h:137
FMatrix & operator=(const FMatrix &)
Assignment.
Definition: FMatrix.h:161
FMatrix()
Default constructor.
Definition: FMatrix.h:131
A templated representation for vectors.
Definition: FVector.h:38