Debian Codegraph @

src/acl/aclMath/aclMatrixOfElements.cxx

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 #include "aclMatrixOfElements.h"
25 #include "aclVectorOfElements.h"
26 #include "aclGenerators.h"
27 #include "acl.h"
28 #include "../aslUtilities.h"
29 #include <algorithm>
30
31
32 using namespace std;
33 using asl::errorMessage;
34
35 namespace acl
36 {
37 MatrixOfElements::MatrixOfElements(unsigned int nR, unsigned int nC):
38 nRow(nR),
39 nCol(nC),
40 ve(nR*nC)
41 {
42 }
43
44 unsigned int MatrixOfElements::ij2i(unsigned int i, unsigned int j) const
45 {
46 return i*nCol+j;
47 }
48
49 void MatrixOfElements::setElement(unsigned int i, unsigned int j, Element a)
50 {
51 ve.at(ij2i(i,j))=a;
52 }
53
54 void MatrixOfElements::setRow(unsigned int r, const VectorOfElements & b)
55 {
56 if(nCol != b.size())
57 errorMessage("Error: MatrixOfElements::setRow: size of b does not match number of columns");
58 if(nRow < r)
59 errorMessage("Error: MatrixOfElements::setRow: r larger than number of rows");
60 for(unsigned int i(0); i < b.size(); ++i)
61 setElement(r,i,b[i]);
62 }
63
64 void MatrixOfElements::setColumn(unsigned int c, const VectorOfElements & b)
65 {
66 if(nRow != b.size())
67 errorMessage("Error: MatrixOfElements::setColumn: size of b does not match number of rows");
68 if(nCol < c)
69 errorMessage("Error: MatrixOfElements::setColumn: c larger than number of columns");
70 for(unsigned int i(0); i < b.size(); ++i)
71 setElement(i,c,b[i]);
72 }
73
74 const Element MatrixOfElements::getElement(unsigned int i, unsigned int j) const
75 {
76 return ve.at(ij2i(i,j));
77 }
78
79 const VectorOfElements MatrixOfElements::getVE(unsigned int i, unsigned int j) const
80 {
81 return subVE(ve, ij2i(i,j));
82 }
83
84 const unsigned int MatrixOfElements::getNColumns() const
85 {
86 return nCol;
87 }
88
89 const unsigned int MatrixOfElements::getNRows() const
90 {
91 return nRow;
92 }
93
94 VectorOfElements & MatrixOfElements::getInternalVector()
95 {
96 return ve;
97 }
98
99 const VectorOfElements & MatrixOfElements::getInternalVector() const
100 {
101 return ve;
102 }
103
104 MatrixOfElements MatrixOfElements::operator= (const MatrixOfElements & m)
105 {
106 MatrixOfElements res(nRow, nCol);
107 copy (ve = m.ve, res.ve);
108 return res;
109 }
110
111
112 void copy(const MatrixOfElements & source, MatrixOfElements & destination)
113 {
114 if((source.getNRows() != destination.getNRows()) ||
115 (source.getNColumns() != destination.getNColumns()))
116 errorMessage("copy: matricess have different sizes");
117 copy(source.getInternalVector(),destination.getInternalVector());
118 }
119
120 MatrixOfElements operator+(const MatrixOfElements & a, const MatrixOfElements & b)
121 {
122 if (a.getNRows() != b.getNRows() || a.getNColumns() != b.getNColumns())
123 errorMessage("operator+ - the sizes of two MatrixOfElements are incompatible");
124
125 MatrixOfElements m(a.getNRows(),a.getNColumns());
126 copy(a.getInternalVector()+b.getInternalVector(),m.getInternalVector());
127 return m;
128 }
129
130 MatrixOfElements operator-(const MatrixOfElements & a, const MatrixOfElements & b)
131 {
132 if (a.getNRows() != b.getNRows() || a.getNColumns() != b.getNColumns())
133 errorMessage("operator- - the sizes of two MatrixOfElements are incompatible");
134
135 MatrixOfElements m(a.getNRows(),a.getNColumns());
136 copy(a.getInternalVector()-b.getInternalVector(),m.getInternalVector());
137 return m;
138 }
139
140 MatrixOfElements operator*(const MatrixOfElements & a, const MatrixOfElements & b)
141 {
142 if (a.getNColumns() != b.getNRows() &&
143 !(a.getNColumns()==1 && a.getNRows()==1) &&
144 !(b.getNColumns()==1 && b.getNRows()==1))
145 errorMessage("operator* - the sizes of two MatrixOfElements are incompatible");
146
147 MatrixOfElements m;
148 if (a.getNColumns()==1 && a.getNRows()==1)
149 {
150 m.resize(b.getNRows(), b.getNColumns());
151 copy(b.getInternalVector() * a.getInternalVector(),
152 m.getInternalVector());
153 }
154 else
155 {
156 if (b.getNColumns()==1 && b.getNRows()==1)
157 {
158 m.resize(a.getNRows(), a.getNColumns());
159 copy(a.getInternalVector() * b.getInternalVector(),
160 m.getInternalVector());
161 }
162 else
163 {
164 m.resize(a.getNRows(),b.getNColumns());
165 for (unsigned int i(0); i < a.getNRows(); ++i)
166 for (unsigned int j(0);j<b.getNColumns();++j)
167 {
168 using namespace elementOperators;
169 Element r(a.getElement(i,0)*b.getElement(0,j));
170 for (unsigned int k(1);k<b.getNRows();++k){
171 r=r+a.getElement(i,k)*b.getElement(k,j);
172 }
173 m.setElement(i,j,r);
174 }
175 }
176 }
177 return m;
178 }
179
180 VectorOfElements operator*(const VectorOfElements & a, const MatrixOfElements & b)
181 {
182 if (a.size() != b.getNRows())
183 errorMessage("operator* - the sizes of VectorOfElements and MatrixOfElements are incompatible");
184
185 VectorOfElements v(b.getNColumns());
186 for (unsigned int j(0);j<b.getNColumns();++j)
187 {
188 using namespace elementOperators;
189 Element r(a.at(0)*b.getElement(0,j));
190 for (unsigned int k(1);k<b.getNRows();++k)
191 {
192 r=r+a.at(k)*b.getElement(k,j);
193 }
194 v[j] = r;
195 }
196 return v;
197 }
198
199 VectorOfElements operator*(const MatrixOfElements & a, const VectorOfElements & b)
200 {
201 if (a.getNColumns() != b.size())
202 errorMessage("operator* - the sizes of MatrixOfElements and VectorOfElements are incompatible");
203
204
205 VectorOfElements v(a.getNRows());
206 for (unsigned int i(0); i < a.getNRows(); ++i){
207 using namespace elementOperators;
208 Element r(a.getElement(i,0)*b.at(0));
209 for (unsigned int k(1);k<a.getNColumns();++k){
210 r=r+a.getElement(i,k)*b.at(k);
211 }
212 v[i] = r;
213 }
214 return v;
215 }
216
217 MatrixOfElements operator/(const MatrixOfElements & a, const VectorOfElements & b)
218 {
219 if (b.size() !=1)
220 errorMessage("operator/ - the sizes of Vector of elements is not 1");
221
222
223 MatrixOfElements m(a.getNRows(),a.getNColumns());
224 copy(a.getInternalVector()/b,m.getInternalVector());
225 return m;
226 }
227
228 MatrixOfElements elementProduct(const VectorOfElements & a, const VectorOfElements & b){
229 MatrixOfElements m(a.size(),b.size());
230 using namespace elementOperators;
231 for (unsigned int i(0); i < a.size(); ++i)
232 for (unsigned int j(0);j<b.size();++j)
233 m.setElement(i,j,a[i]*b[j]);
234 return m;
235 }
236
237 VectorOfElements trace(const MatrixOfElements &a)
238 {
239 VectorOfElements c(a.getVE(0,0));
240 for (unsigned int i(1); i < a.getNRows(); ++i)
241 copy(c + a.getVE(i, i), c);
242 return c;
243 }
244
245 VectorOfElements trace(const MatrixOfElements & a, const MatrixOfElements & b)
246 {
247 VectorOfElements c(generateVEConstant(0.));
248 for (unsigned int i(0); i < a.getNRows(); ++i)
249 for (unsigned int j(0); j < a.getNColumns(); ++j)
250 copy(c + a.getVE(i,j) * b.getVE(j,i), c);
251 return c;
252 }
253
254
255 MatrixOfElements generateME(const VectorOfElements & a)
256 {
257 MatrixOfElements m(1,a.size());
258 copy(a,m.getInternalVector());
259 return m;
260 }
261
262 MatrixOfElements generateME(const VectorOfElements & a,
263 const VectorOfElements & b)
264 {
265 if(a.size() != b.size())
266 errorMessage("generateME: vectors have different sizes" );
267 MatrixOfElements m(2,a.size());
268 copy(cat(a,b),m.getInternalVector());
269 return m;
270 }
271
272 MatrixOfElements generateME(const VectorOfElements & a,
273 const VectorOfElements & b,
274 const VectorOfElements & c)
275 {
276 if((a.size() != b.size()) || (a.size() != c.size()))
277 errorMessage("generateME: vectors have different sizes" );
278 MatrixOfElements m(3,a.size());
279 copy(cat(a,b,c),m.getInternalVector());
280 return m;
281 }
282
283 MatrixOfElements generateME(const VectorOfElements *a, unsigned int n)
284 {
285 for(unsigned int i(1); i<n; ++i)
286 if((a[0].size() != a[i].size()))
287 errorMessage("generateME: some vectors have different sizes");
288
289 MatrixOfElements m(n, a[0].size());
290 for(unsigned int i(0); i<n; ++i)
291 for(unsigned int j(0); j<a[i].size(); ++j)
292 m.setElement(i,j,a[i][j]);
293 return m;
294 }
295
296 MatrixOfElements generateME(const vector<VectorOfElements> & a)
297 {
298 return generateME(&a[0], a.size());
299 }
300
301 VectorOfElements getDiagonal(const MatrixOfElements & a)
302 {
303 if(a.getNColumns()!=a.getNRows())
304 errorMessage("Error: getDiagonal: the matrix is not square one");
305
306 unsigned int n(a.getNColumns());
307 VectorOfElements v(n);
308
309 for(unsigned int i(0); i<n; ++i)
310 v[i]=a.getElement(i,i);
311
312 return v;
313 }
314
315 VectorOfElements getOffDiagonalUp(const MatrixOfElements & a)
316 {
317 if(a.getNColumns()!=a.getNRows())
318 errorMessage("Error: getOffDiagonalUp: the matrix is not square one");
319
320 unsigned int n(a.getNColumns());
321 VectorOfElements v((n*n-n)/2);
322
323 unsigned int k(0);
324 for(unsigned int i(1); i<n; ++i)
325 for(unsigned int j(1); j<=i; ++j)
326 {
327 v[k]=a.getElement(i,j);
328 ++k;
329 }
330 return v;
331 }
332
333 /// computes determinant expression fo cases 2x2 and 3x3 only
334 VectorOfElements det(const MatrixOfElements & m)
335 {
336 if(m.getNColumns()!=m.getNRows())
337 errorMessage("Error: det: the matrix is not square one");
338 if(m.getNColumns()>3)
339 errorMessage("Error: det: the matrix size is larger than 3");
340
341 VectorOfElements v(1);
342
343 if(m.getNColumns()==1)
344 copy(m.getInternalVector(),v);
345 if(m.getNColumns()==2)
346 copy(m.getVE(0,0) * m.getVE(1,1) - m.getVE(0,1) * m.getVE(1,0),
347 v);
348 // copy(mad(m.getVE(0,0), m.getVE(1,1), - m.getVE(0,1) * m.getVE(1,0)),
349 // v);
350 if(m.getNColumns()==3)
351 /* copy(mad(m.getVE(0,0) * m.getVE(1,1),
352 m.getVE(2,2),
353 mad(m.getVE(1,0) * m.getVE(2,1),
354 m.getVE(0,2),
355 mad(m.getVE(0,1) * m.getVE(1,2),
356 m.getVE(2,0),
357 -mad(m.getVE(0,2) * m.getVE(1,1),
358 m.getVE(2,0),
359 mad(m.getVE(1,0) * m.getVE(0,1),
360 m.getVE(2,2),
361 m.getVE(2,1) * m.getVE(1,2) * m.getVE(0,0)))))),
362 v);*/
363 copy(m.getVE(0,0) * m.getVE(1,1) * m.getVE(2,2) +
364 m.getVE(1,0) * m.getVE(2,1) * m.getVE(0,2) +
365 m.getVE(0,1) * m.getVE(1,2) * m.getVE(2,0) -
366 m.getVE(0,2) * m.getVE(1,1) * m.getVE(2,0) -
367 m.getVE(1,0) * m.getVE(0,1) * m.getVE(2,2) -
368 m.getVE(2,1) * m.getVE(1,2) * m.getVE(0,0),
369 v);
370
371 return v;
372 }
373
374 VectorOfElements solveSystem(const MatrixOfElements & a,
375 const VectorOfElements & b)
376 {
377 if(a.getNColumns()!=a.getNRows())
378 errorMessage("Error: solveSystem: the matrix is not square one");
379 if(a.getNColumns()>3)
380 errorMessage("Error: solveSystem: the matrix size is larger than 3");
381 if(a.getNColumns()!=b.size())
382 errorMessage("Error: solveSystem: size of b does not match the size of matrix");
383
384 VectorOfElements v(b.size());
385
386 auto d(det(a));
387 VectorOfElements db(b.size());
388 for(unsigned int i(0); i < b.size(); ++i)
389 db[i] = (det(replaceColumn(a,b,i)))[0];
390
391 return db/d;
392 }
393
394 vector<Element> gcSolveSystem(const MatrixOfElements & a,
395 const VectorOfElements & b,
396 const VectorOfElements & x)
397 {
398 if(a.getNColumns()!=a.getNRows())
399 errorMessage("Error: solveSystem: the matrix is not square one");
400 if(a.getNColumns()>3)
401 errorMessage("Error: solveSystem: the matrix size is larger than 3");
402 if(a.getNColumns()!=b.size())
403 errorMessage("Error: solveSystem: size of b does not match the size of matrix");
404
405 vector<Element> res(0);
406 res.push_back((subVE(x,0) = 1./det(a))[0]);
407
408 VectorOfElements db(b.size());
409 for(unsigned int i(1); i < b.size(); ++i)
410 res.push_back((subVE(x,i) = subVE(x,0) * det(replaceColumn(a,b,i)))[0]);
411 res.push_back((subVE(x,0) *= det(replaceColumn(a,b,0)))[0]);
412 return res;
413 }
414
415 vector<Element> gcSolveSystemCG(const MatrixOfElements & a,
416 const VectorOfElements & b,
417 const VectorOfElements & x)
418 {
419 if(a.getNColumns()!=a.getNRows())
420 errorMessage("Error: solveSystem: the matrix is not square one");
421 if(a.getNColumns()!=b.size())
422 errorMessage("Error: solveSystem: size of b does not match the size of matrix");
423 if(a.getNColumns()!=x.size())
424 errorMessage("Error: solveSystem: size of x does not match the size of matrix");
425
426 TypeID type(getElementType(x));
427 auto p(generateVEPrivateVariable(b.size(),type));
428 auto r(generateVEPrivateVariable(b.size(),type));
429 auto a_p(generateVEPrivateVariable(b.size(),type));
430 auto alpha(generateVEPrivateVariable(1,type));
431 auto rr(generateVEPrivateVariable(1,type));
432
433 vector<Element> res(0);
434 res << (x=b);
435 res << (r=b-a*x);
436 res << (p=r);
437 res << (rr = r*r);
438 for(unsigned int i(0); i<b.size()-1; ++i)
439 {
440 res << (a_p = a * p);
441 res << (alpha=rr/(p*a_p));
442 res << (x+=alpha*p);
443 res << (r-=alpha*a_p);
444 // this line corresponds to beta
445 res << (alpha=1./rr);
446 res << (rr=r*r);
447 res << (p = r+ rr*alpha * p);
448 }
449 res << (a_p = a * p);
450 res << (alpha=rr/(p*a_p));
451 res << (x+=alpha*p);
452
453 return res;
454 }
455
456
457 MatrixOfElements replaceRow(const MatrixOfElements & a,
458 const VectorOfElements & b,
459 unsigned int r)
460 {
461 MatrixOfElements m(a.getNRows(),a.getNColumns());
462 copy(a,m);
463 m.setRow(r,b);
464 return m;
465 }
466
467 MatrixOfElements replaceColumn(const MatrixOfElements & a,
468 const VectorOfElements & b,
469 unsigned int c)
470 {
471 MatrixOfElements m(a.getNRows(),a.getNColumns());
472 copy(a,m);
473 m.setColumn(c,b);
474 return m;
475 }
476
477
478 VectorOfElements matrixCofactor(const MatrixOfElements & a,
479 unsigned int r,
480 unsigned int c)
481 {
482 MatrixOfElements m(a.getNRows()-1, a.getNColumns()-1);
483 for(unsigned int i(0), im(0); im < m.getNRows(); ++i, ++im)
484 {
485 i += (i == r) ? 1 : 0;
486 for(unsigned int j(0), jm(0); jm < m.getNColumns(); ++j, ++jm)
487 {
488 j += (j == c) ? 1 : 0;
489 m.setElement(im, jm, a.getElement(i,j));
490 }
491 }
492 return det(m);
493 }
494
495 MatrixOfElements generateMatrixCofactors(const MatrixOfElements & a)
496 {
497 if(a.getNRows() != a.getNColumns())
498 errorMessage("Error: generateMatrixCofactors: the matrix is not rectangular one");
499 if(a.getNRows() > 3)
500 errorMessage("Error: generateMatrixCofactors: the matrix size is more than 3");
501 MatrixOfElements m(a.getNRows(), a.getNColumns());
502 int rfactor(-1);
503 for(unsigned int i(0); i < a.getNRows(); ++i){
504 rfactor*=-1;
505 int factor(-rfactor);
506 for(unsigned int j(0); j < a.getNColumns(); ++j)
507 {
508 factor*=-1;
509 m.setElement(i,j, (factor * matrixCofactor(a,i,j))[0] );
510 }
511 }
512 return m;
513 }
514
515 /// returns vector of elements for computing the inverse matrix for cases 2x2 and 3x3 \relates MatrixOfElements
516 vector<Element> gcMatrixInversion(const MatrixOfElements & a, MatrixOfElements & inv)
517 {
518 if((a.getNRows() != inv.getNRows()) ||
519 (a.getNColumns() != inv.getNColumns()))
520 errorMessage("Error: generateMatrixInversionCode: two matrices have different sizes");
521 auto mc(generateMatrixCofactors(a));
522 auto d(det(a));
523
524 unsigned int nr(a.getNRows());
525 unsigned int nc(a.getNColumns());
526
527 auto lastEl(inv.getVE(nr-1, nc-1));
528
529 auto op1(lastEl = 1./det(a));
530 auto op2(subVE(inv.getInternalVector(), 0, nr*nc-2) =
531 subVE(mc.getInternalVector(), 0, nr*nc-2) * lastEl);
532 auto op3(lastEl*=mc.getVE(nr-1, nc-1));
533
534 return cat(op1,op2,op3);
535 }
536
537
538 } // namespace acl