Debian Codegraph @

src/math/aslMatrices.h

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 /// \file aslMatrices.h Matrices
25
26 #ifndef ASLMATRICES
27 #define ASLMATRICES
28
29
30 #include "../aslUtilities.h"
31 #include "aslVectors.h"
32
33
34 namespace asl
35 {
36 /// class algebraic matrix.
37 /// The class is an implementation of a dynamic matrix with defined algebraic operations
38 template <typename T = double> class AMatr
39 {
40 private:
41 unsigned int nRow;
42 unsigned int nCol;
43 AVec<T> internalVec;
44 public:
45 inline AMatr();
46 inline AMatr(unsigned int nR, unsigned int nC);
47 inline AMatr(const AMatr<T> &a);
48 inline AMatr(unsigned int nR, unsigned int nC, AVec<T> v);
49 template <typename T1> AMatr(const AMatr<T1> &a);
50 const AMatr<T> & operator=(const AMatr & a);
51 ///doesn't chek boundaries
52 inline T& operator()(int i, int j) {return internalVec[i*nCol+j];}
53 ///doesn't chek boundaries
54 inline const T& operator()(int i, int j)const {return internalVec[i*nCol+j];}
55 ///doesn't chek boundaries
56 inline T& operator[](int i) {return internalVec[i];}
57 ///doesn't chek boundaries
58 inline const T& operator[](int i)const {return internalVec[i];}
59 inline unsigned int getNRow() const;
60 inline unsigned int getNCol() const;
61 inline void resize(unsigned int nR, unsigned int nCol);
62 inline const AVec<T> & getInternalVec() const;
63 inline AVec<T> & getInternalVec();
64 void setRow(unsigned int r, const AVec<T> & a);
65 void setColumn(unsigned int c, const AVec<T> & a);
66 };
67
68
69 /// \relates AMatr
70 template <typename T> std::ostream& operator<<(std::ostream &f,const AMatr<T> & a);
71 /// \relates AMatr
72 template <typename T>
73 inline const AMatr<T> & operator+=(AMatr<T> & a, const AMatr<T> & b);
74 /// \relates AMatr
75 template <typename T>
76 inline const AMatr<T> operator+ (const AMatr<T> & a, const AMatr<T> & b);
77 /// \relates AMatr
78 template <typename T>
79 inline const AMatr<T> operator- (const AMatr<T> & a, const AMatr<T> & b);
80 /// \relates AMatr
81 template <typename T>
82 const AMatr<T> operator* (const AMatr<T> &a, const AMatr<T> & b);
83
84 /// \relates AMatr
85 template <typename T>
86 const AVec<T> operator* (const AMatr<T> &a, const AVec<T> & b);
87
88 /// \relates AMatr
89 template <typename T>
90 const AVec<T> operator* (const AVec<T> &a, const AMatr<T> & b);
91
92 /// \relates AMatr
93 template <typename T>
94 const AMatr<T> operator* (const AMatr<T> &a, const T & b);
95
96 /// \relates AMatr
97 template <typename T>
98 const AMatr<T> operator* (const T &a, const AMatr<T> & b);
99
100 ///Trace of a matrix \f$Tr(A)\equiv A_{ii}\f$ \relates AMatr
101 template <typename T> const T trace(const AMatr<T> &a);
102
103 ///Trace of a matrix product \f$Tr(A B)\equiv A_{ij}B_{ji}\f$ \relates AMatr
104 template <typename T> const T trace(const AMatr<T> & a, const AMatr<T> & b);
105 /// \relates AMatr
106 template <typename T>
107 inline const AMatr<T> operator/ (const AMatr<T> & b, const T & a);
108
109 ///element product of two vectors
110 /**
111 \relates AMatr
112 \f$ elementProduct\left(
113 \left[\begin{array}{c}
114 a_1\\ \vdots \\ a_n
115 \end{array}\right],
116 \left[\begin{array}{c}
117 b_1\\ \vdots \\ b_n
118 \end{array}\right] =
119 \left[\begin{array}{ccc}
120 a_1b_1 & \cdots & a_1b_n\\
121 \vdots & \ddots & \vdots\\
122 a_nb_1 & \cdots & a_nb_n\\
123 \end{array}\right]
124 \right)
125 \f$
126 */
127 template <typename T>
128 AMatr<T> elementProduct(const AVec<T> & a, const AVec<T> & b);
129
130
131 /// generates a matrix with a row \relates AMatr
132 template <typename T> AMatr<T> makeAMatr(const AVec<T> & a);
133
134 /// generates a matrix with two rows \relates AMatr
135 template <typename T> AMatr<T> makeAMatr(const AVec<T> & a, const AVec<T> & b);
136
137 /// generates a matrix with three rows \relates AMatr
138 template <typename T> AMatr<T> makeAMatr(const AVec<T> & a,
139 const AVec<T> & b,
140 const AVec<T> & c);
141
142 /// generates a matrix with \p n rows \relates AMatr
143 template <typename T> AMatr<T> makeAMatr(AVec<T> *a, unsigned int n);
144
145 /// \relates AMatr
146 template <typename T=int>AMatr<T> makeAMatrUnit(unsigned int n);
147
148
149 /// returns AVec containing the diagonal elements
150 /**
151 \relates AMatr
152 the finction is valid only for square matrices
153 */
154 template <typename T> AVec<T> getDiagonal(const AMatr<T> & a);
155
156 /// returns AVec<T> containing the uper off diagonal elements
157 /**
158 \relates AMatr
159 the function is valid only for square matrices
160 \todo implement
161 */
162 template <typename T> AVec<T> getOffDiagonalUp(const AMatr<T> & a);
163
164 /// computes determinant expression fo cases 2x2 and 3x3 only
165 /**
166 \relates AMatr
167 */
168 template <typename T> T det(const AMatr<T> & m);
169
170 /// returns solution of a system of linear equations
171 /**
172 \ingroup ComplexDataTypes
173 */
174 template <typename T> AVec<T> solveSystem(const AMatr<T> & a,
175 const AVec<T> & b);
176
177 /// generate matrix with content of the matrix \p a but with replaced row \p r by vector \p b \relates AMatr<T>
178 template <typename T>
179 AMatr<T> replaceRow(const AMatr<T> & a, const AVec<T> & b, unsigned int r);
180
181 /// generate matrix with content of the matrix \p a but with replaced column \p c by vector \p b \relates AMatr<T>
182 template <typename T>
183 AMatr<T> replaceColumn(const AMatr<T> & a, const AVec<T> & b, unsigned int c);
184
185 /// returns inverse matrix for cases 2x2 and 3x3 \relates AMatr<T>
186 template <typename T>
187 AMatr<T> inverseMatrix(const AMatr<T> & a);
188
189
190 // ------------------------------ Implementation -----------------
191 template <typename T> inline AMatr<T>::AMatr():
192 nRow(0),
193 nCol(0)
194 {
195 }
196
197 template <typename T> inline AMatr<T>::AMatr(unsigned int nR, unsigned int nC):
198 nRow(nR),
199 nCol(nC),
200 internalVec(nR*nC)
201 {
202 }
203 template <typename T> inline AMatr<T>::AMatr(const AMatr<T> &a):
204 nRow(a.nRow),
205 nCol(a.nCol),
206 internalVec(a.internalVec)
207 {
208 }
209 template <typename T> inline AMatr<T>::AMatr(unsigned int nR, unsigned int nC, AVec<T> v):
210 nRow(nR),
211 nCol(nC),
212 internalVec(v)
213 {
214 }
215
216 template <typename T> inline AVec<T> & AMatr<T>::getInternalVec()
217 {
218 return internalVec;
219 }
220
221 template <typename T> inline const AVec<T> & AMatr<T>::getInternalVec() const
222 {
223 return internalVec;
224 }
225
226 template <typename T> inline unsigned int AMatr<T>::getNRow() const
227 {
228 return nRow;
229 }
230
231 template <typename T> inline unsigned int AMatr<T>::getNCol() const
232 {
233 return nCol;
234 }
235
236 template <typename T>
237 inline void AMatr<T>::resize(unsigned int nr, unsigned int nc)
238 {
239 nRow=nr;
240 nCol=nc;
241 internalVec.resize(nr*nc);
242 }
243
244 template <typename T>
245 inline const AMatr<T> & operator+=(AMatr<T> & a, const AMatr<T> & b)
246 {
247 a.getInternalVec += b.getInternalVec;
248 return a;
249 }
250
251 template <typename T>
252 inline const AMatr<T> operator+ (const AMatr<T> & a, const AMatr<T> & b)
253 {
254 return {a.getNRow(),a.getNCol(),a.getInternalVec() + b.getInternalVec()};
255 }
256
257 template <typename T>
258 inline const AMatr<T> operator- (const AMatr<T> & a,const AMatr<T> & b)
259 {
260 return {a.getNRow(),a.getNCol(),a.getInternalVec() - b.getInternalVec()};
261 }
262
263 template <typename T>
264 inline const AMatr<T> operator/ (const AMatr<T> & b, const T & a)
265 {
266 return {b.getNRow(), b.getNCol(), b.getInternalVec() / a};
267 }
268
269 /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2
270 /**
271 \param a matrix element
272 \param b matrix element
273 \param c matrix element
274 \param l1 first eigenvalue
275 \param l2 second eigenvalue
276 \param v1x x component of first eigenvector
277 \param v1y y component of first eigenvector
278 \param v2x x component of second eigenvector
279 \param v2y y component of second eigenvector
280
281 \f[
282 A=
283 \left| \begin{array}{cc}
284 a & c \\
285 c & b
286 \end{array}\right|
287 \f]
288
289 */
290 void getEValEVecMatSym2x2(double a, double b, double c, double & l1, double & l2, double & v1x, double & v1y, double & v2x, double & v2y);
291 /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2
292 /**
293 \param a matrix element
294 \param b matrix element
295 \param c matrix element
296 \param l1 first eigenvalue
297 \param l2 second eigenvalue
298 \param v1x x component of first eigenvector
299 \param v1y y component of first eigenvector
300 \param v2x x component of second eigenvector
301 \param v2y y component of second eigenvector
302
303 \f[
304 A=
305 \left| \begin{array}{cc}
306 a & c \\
307 c & b
308 \end{array}\right|
309 \f]
310
311 */
312 void getEValEVecMatSym2x2(double a, double b, double c, double & l1, double & l2, double & v1x, double & v1y, double & v2x, double & v2y);
313
314 /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2
315 /**
316 \param a matrix element
317 \param b matrix element
318 \param c matrix element
319 \param d matrix element
320 \param e matrix element
321 \param f matrix element
322 \param l1 first eigenvalue
323 \param l2 second eigenvalue
324 \param l3 second eigenvalue
325 \param v1x x component of first eigenvector
326 \param v1y y component of first eigenvector
327 \param v1z z component of first eigenvector
328 \param v2x x component of second eigenvector
329 \param v2y y component of second eigenvector
330 \param v2z z component of second eigenvector
331 \param v3x x component of second eigenvector
332 \param v3y y component of second eigenvector
333 \param v3z z component of second eigenvector
334
335 \f[
336 A=
337 \left| \begin{array}{ccc}
338 a & d & e\\
339 d & b & f \\
340 e & f & c \\
341 \end{array}\right|
342 \f]
343
344 */
345 void getEValEVecMatSym3x3(double a, double b, double c, double e, double f, double g,
346 double & l1, double & l2, double & l3,
347 double & v1x, double & v1y, double & v1z,
348 double & v2x, double & v2y, double & v2z,
349 double & v3x, double & v3y, double & v3z);
350
351 } // asl
352
353 #endif //ASLMATRICES
354