Debian Codegraph @

src/acl/aclMath/aclMatrixOfElements.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 #ifndef ACLMATRIXOFELEMENTS_H
25 #define ACLMATRIXOFELEMENTS_H
26
27 #include "aclVectorOfElementsDef.h"
28
29 namespace acl
30 {
31 /// The class represents a matrix elements of ::Element
32 /**
33 \ingroup ComplexDataTypes
34 */
35 class MatrixOfElements
36 {
37 private:
38 /// number of columns
39 unsigned int nRow;
40 /// number of rows
41 unsigned int nCol;
42
43 VectorOfElements ve;
44 /// defines convertion rule of matrix indeces \p i and \p j into vector one
45 unsigned int ij2i(unsigned int i, unsigned int j) const;
46 public:
47 explicit MatrixOfElements(unsigned int nR = 0, unsigned int nC = 0);
48
49 void setElement(unsigned int r, unsigned int c, Element a);
50 void setRow(unsigned int r,const VectorOfElements & a);
51 void setColumn(unsigned int c, const VectorOfElements & a);
52 const Element getElement(unsigned int r, unsigned int c) const;
53 const VectorOfElements getVE(unsigned int r, unsigned int c) const;
54 const unsigned int getNColumns() const;
55 const unsigned int getNRows() const;
56 VectorOfElements & getInternalVector();
57 const VectorOfElements & getInternalVector() const;
58 inline void resize(unsigned int nr, unsigned int nc);
59 MatrixOfElements operator= (const MatrixOfElements & m);
60 };
61
62 ///function copies the MatrixOfElements class.
63 /**
64 \relates MatrixOfElements
65 */
66 void copy(const MatrixOfElements & source, MatrixOfElements & destination);
67
68 ///summ of two matrices
69 /**
70 \relates MatrixOfElements
71 */
72 MatrixOfElements operator+(const MatrixOfElements & a, const MatrixOfElements & b);
73 ///difference of two matrices
74 /**
75 \relates MatrixOfElements
76 */
77 MatrixOfElements operator-(const MatrixOfElements & a, const MatrixOfElements & b);
78
79 ///product of two matrices
80 /**
81 \relates MatrixOfElements
82 */
83 MatrixOfElements operator*(const MatrixOfElements & a, const MatrixOfElements & b);
84
85 ///product of vector and matrix
86 /**
87 \relates MatrixOfElements
88 */
89 VectorOfElements operator*(const VectorOfElements & a, const MatrixOfElements & b);
90
91 ///product of vector and matrix
92 /**
93 \relates MatrixOfElements
94 */
95 VectorOfElements operator*(const MatrixOfElements & a, const VectorOfElements & b);
96
97 ///division of a matrix on a VectorOfElements with 1 element
98 /**
99 \relates MatrixOfElements
100 */
101 MatrixOfElements operator/(const MatrixOfElements & a, const VectorOfElements & b);
102
103
104 ///transposed matrix
105 /**
106 \relates MatrixOfElements
107 */
108 MatrixOfElements transpose(MatrixOfElements & source);
109
110 ///element product of two vectors
111 /**
112 \relates MatrixOfElements
113 \f$ elementProduct\left(
114 \left[\begin{array}{c}
115 a_1\\ \vdots \\ a_n
116 \end{array}\right],
117 \left[\begin{array}{c}
118 b_1\\ \vdots \\ b_n
119 \end{array}\right]\right) =
120 \left[\begin{array}{ccc}
121 a_1b_1 & \cdots & a_1b_n\\
122 \vdots & \ddots & \vdots\\
123 a_nb_1 & \cdots & a_nb_n\\
124 \end{array}\right]
125
126 \f$, \f$A_{ij} = a_i b_j \f$
127 */
128 MatrixOfElements elementProduct(const VectorOfElements & a, const VectorOfElements & b);
129
130 ///Trace of a matrix \f$Tr(A)\equiv A_{ii}\f$ \relates MatrixOfElements
131 VectorOfElements trace(const MatrixOfElements & a);
132
133 ///Trace of a matrix product \f$Tr(A B)\equiv A_{ij}B_{ji}\f$ \relates MatrixOfElements
134 VectorOfElements trace(const MatrixOfElements & a, const MatrixOfElements & b);
135
136 /// generates a matrix with a row \relates MatrixOfElements
137 MatrixOfElements generateME(const VectorOfElements & a);
138
139 /// generates a matrix with two rows \relates MatrixOfElements
140 MatrixOfElements generateME(const VectorOfElements & a,VectorOfElements & b);
141
142 /// generates a matrix with three rows \relates MatrixOfElements
143 MatrixOfElements generateME(const VectorOfElements & a,
144 const VectorOfElements & b,
145 const VectorOfElements & c);
146
147 /// generates a matrix with \p n rows \f$ generateME(\{u_i\}_j) = A_{ji}\f$ \relates MatrixOfElements
148 MatrixOfElements generateME(const VectorOfElements *a, unsigned int n);
149
150 /// generates a matrix with \p n rows \f$ generateME(\{u_i\}_j) = A_{ji}\f$ \relates MatrixOfElements
151 MatrixOfElements generateME(const vector<VectorOfElements> &a);
152
153 /// returns VectorOfElements containing the diagonal elements
154 /**
155 \relates MatrixOfElements
156 the finction is valid only for square matrices
157 */
158 VectorOfElements getDiagonal(const MatrixOfElements & a);
159
160 /// returns VectorOfElements containing the uper off diagonal elements
161 /**
162 \relates MatrixOfElements
163 the finction is valid only for square matrices
164 */
165 VectorOfElements getOffDiagonalUp(const MatrixOfElements & a);
166
167 /// computes determinant expression fo cases 2x2 and 3x3 only
168 /**
169 \relates MatrixOfElements
170 */
171 VectorOfElements det(const MatrixOfElements & m);
172
173 /// returns solution of a system of linear equations
174 /**
175 \ingroup ComplexDataTypes
176 */
177 VectorOfElements solveSystem(const MatrixOfElements & a, const VectorOfElements & b);
178
179 /// generates code for solving the solution of a system of linear equations
180 /**
181 \ingroup ComplexDataTypes
182 Solves system of 2 or 3 equations with the Cramer's rule
183 */
184 vector<Element> gcSolveSystem(const MatrixOfElements & a,
185 const VectorOfElements & b,
186 const VectorOfElements & x);
187
188 /// generates code for solving the solution of a system of linear equations
189 /**
190 \ingroup ComplexDataTypes
191 Solves system of an arbiterary number of equations with the conjugate gradient method.
192 The function constructs the algorithm with number of iterations of vector size. The initial
193 value of \f$ x_0 \f$ is \f$b\f$.
194 */
195 vector<Element> gcSolveSystemCG(const MatrixOfElements & a,
196 const VectorOfElements & b,
197 const VectorOfElements & x);
198
199 /// generate matrix with content of the matrix \p a but with replaced row \p r by vector \p b \relates MatrixOfElements
200 MatrixOfElements replaceRow(const MatrixOfElements & a, const VectorOfElements & b, unsigned int r);
201
202 /// generate matrix with content of the matrix \p a but with replaced column \p c by vector \p b \relates MatrixOfElements
203 MatrixOfElements replaceColumn(const MatrixOfElements & a, const VectorOfElements & b, unsigned int c);
204
205 /// returns the matrix of cofactors for cases 2x2 and 3x3 \relates MatrixOfElements
206 MatrixOfElements generateMatrixCofactors(const MatrixOfElements & a);
207
208 /// returns vector of elements for computing the inverse matrix for cases 2x2 and 3x3 \relates MatrixOfElements
209 vector<Element> gcMatrixInversion(const MatrixOfElements & a, MatrixOfElements & inv);
210
211 // ------------------------------ Implementation -----------------
212
213 inline void MatrixOfElements::resize(unsigned int nr, unsigned int nc)
214 {
215 nRow=nr;
216 nCol=nc;
217 ve.resize(nr*nc);
218 }
219
220
221 } //namespace acl
222
223 #endif // ACLMATRIXOFELEMENTS_H