VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
nummatrix.h
Go to the documentation of this file.
1/*--------------------------------*- VM2D -*-----------------*---------------*\
2| ## ## ## ## #### ##### | | Version 1.14 |
3| ## ## ### ### ## ## ## ## | VM2D: Vortex Method | 2026/03/06 |
4| ## ## ## # ## ## ## ## | for 2D Flow Simulation *----------------*
5| #### ## ## ## ## ## | Open Source Code |
6| ## ## ## ###### ##### | https://www.github.com/vortexmethods/VM2D |
7| |
8| Copyright (C) 2017-2026 I. Marchevsky, K. Sokol, E. Ryatina, A. Kolganova |
9*-----------------------------------------------------------------------------*
10| File name: nummatrix.h |
11| Info: Source code of VM2D |
12| |
13| This file is part of VM2D. |
14| VM2D is free software: you can redistribute it and/or modify it |
15| under the terms of the GNU General Public License as published by |
16| the Free Software Foundation, either version 3 of the License, or |
17| (at your option) any later version. |
18| |
19| VM2D is distributed in the hope that it will be useful, but WITHOUT |
20| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21| FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
22| for more details. |
23| |
24| You should have received a copy of the GNU General Public License |
25| along with VM2D. If not, see <http://www.gnu.org/licenses/>. |
26\*---------------------------------------------------------------------------*/
27
28
40#ifndef NUMMATRIX_H_
41#define NUMMATRIX_H_
42
43#include "numvector.h"
44
45namespace VMlib
46{
47
64 template<typename T, size_t n, size_t m>
65 class nummatrix : public std::array<T, n*m>
66 {
67
68 public:
69
70 public:
71
78 {
79 return *(reinterpret_cast<numvector<T, m>*>(this->data() + (i*m)));
80 }
81
87 const numvector<T, m>& operator[](size_t i) const
88 {
89 return *(reinterpret_cast<const numvector<T, m>*>(this->data() + (i*m)));
90 }
91
92
93
102 template <typename P>
104 {
105 for (size_t i = 0; i < n*m; ++i)
106 this->data()[i] *= c;
107 return *this;
108 }//operator*=
109
110
119 template <typename P>
121 {
122 for (size_t i = 0; i < n*m; ++i)
123 this->data()[i] /= c;
124 return *this;
125 }//operator/=
126
127
136 template <typename P>
138 {
139 for (size_t i = 0; i < n*m; ++i)
140 this->data()[i] += y.data()[i];
141 return *this;
142 }//operator+=
143
144
153 template <typename P>
155 {
156 for (size_t i = 0; i < n*m; ++i)
157 this->data()[i] -= y.data()[i];
158 return *this;
159 }//operator-=
160
161
170 template <typename P>
171 auto operator+(const nummatrix<P, n, m>& y) const -> nummatrix<typename std::remove_const<decltype(this->data()[0] + y.data()[0])>::type, n, m>
172 {
173 nummatrix<typename std::remove_const<decltype(this->data()[0] + y.data()[0])>::type, n, m> res;
174 for (size_t i = 0; i < n*m; ++i)
175 res.data()[i] = this->data()[i] + y.data()[i];
176 return res;
177 }//operator+
178
179
188 template <typename P>
189 auto operator-(const nummatrix<P, n, m>& y) const -> nummatrix<typename std::remove_const<decltype(this->data()[0] - y.data()[0])>::type, n, m>
190 {
191 nummatrix<typename std::remove_const<decltype(this->data()[0] - y.data()[0])>::type, n, m> res;
192 for (size_t i = 0; i < n*m; ++i)
193 res.data()[i] = this->data()[i] - y.data()[i];
194 return res;
195 }//operator-
196
197
203 template <typename P>
204 auto operator*(const P c) const -> nummatrix<typename std::remove_const<decltype(this->data()[0] * c)>::type, n, m>
205 {
206 nummatrix<typename std::remove_const<decltype(this->data()[0] * c)>::type, n, m> res;
207 for (size_t i = 0; i < n*m; ++i)
208 res.data()[i] = c * this->data()[i];
209 return res;
210 }//operator*
211
212
213
221 {
223 for (size_t i = 0; i < n*m; ++i)
224 res.data()[i] = -this->data()[i];
225 return res;
226 }//operator-
227
228
237 template <typename P>
238 bool operator==(const nummatrix<P, n, m>& y) const
239 {
240 for (size_t i = 0; i < n*m; ++i)
241 if (this->data()[i] != y.data()[i])
242 return false;
243 return true;
244 }//operator==
245
246
255 template <typename P>
256 bool operator!=(const nummatrix<P, n, m>& y) const
257 {
258 return !(*this == y);
259 }//operator!=
260
261
265 std::pair<size_t, size_t> size() const { return{ n, m }; }
266
267
271 auto norm1() const -> typename std::remove_const<typename std::remove_reference<decltype(this->data()[0])>::type>::type
272 {
273 std::vector<typename std::remove_const<typename std::remove_reference<decltype(this->data()[0])>::type>::type> ms(m, 0);
274 for (size_t i = 0; i < m; ++i)
275 for (size_t j = 0; j < n; ++j)
276 ms[i] += abs(this->data()[j*m + i]);
277 return std::max_element(ms.begin(), ms.end());
278 }
279
283 auto norminf() const -> typename std::remove_const<typename std::remove_reference<decltype(this->data()[0])>::type>::type
284 {
285 std::vector<typename std::remove_const<typename std::remove_reference<decltype(this->data()[0])>::type>::type> ms(n, 0);
286 for (size_t i = 0; i < n; ++i)
287 for (size_t j = 0; j < m; ++j)
288 ms[i] += abs(this->data()[i*m + j]);
289 return std::max_element(ms.begin(), ms.end());
290 }
291
292
301 {
302 for (size_t i = 0; i < n*m; ++i)
303 this->data()[i] = val;
304 return *this;
305 }
306
307
314 {
315 for (size_t i = 0; i < n; ++i)
316 for (size_t j = 0; j < m; ++j)
317 this->data()[i*m + j] = (i == j) ? 1 : 0;
318 return *this;
319 }
320
328 {
330 for (size_t i = 0; i < n; ++i)
331 for (size_t j = 0; j < m; ++j)
332 res[j][i] = (*this)[i][j];
333 return res;
334 }
335
342 {
343 return (*this + this->t())*0.5;
344 }
345
348
349
354 template <typename P>
355 explicit nummatrix(const P z)
356 {
357 for (size_t i = 0; i < n*m; ++i)
358 this->data()[i] = z;
359 }//nummatrix(...)
360
361
368 template <typename P>
370 {
371 for (size_t i = 0; i < n*m; ++i)
372 this->data()[i] = mtr.data()[i];
373 }//nummatrix(...)
374
375
382 template <typename P>
383 nummatrix(const std::vector<std::vector<P>>& vec)
384 {
385 if ((vec.size() != n) || (vec[0].size() != m))
386 throw;
387 for (size_t i = 0; i < n; ++i)
388 for (size_t j = 0; j < m; ++j)
389 this->data()[i*m + j] = vec[i][j];
390 }//nummatrix(...)
391
392
399 template <typename P>
401 {
402 for (size_t i = 0; i < n; ++i)
403 for (size_t j = 0; j < m; ++j)
404 this->data()[i*m + j] = mtr[i][j];
405 }//nummatrix(...)
406
407
408#if !defined(__CUDACC__)
414 nummatrix(const std::initializer_list<numvector<T, m>>& z)
415 {
416 if (z.size() != n)
417 throw;
418 for (size_t i = 0; i < n; ++i)
419 {
420 const numvector<T, m>& numv = *(z.begin() + i);
421 for (size_t j = 0; j < m; ++j)
422 this->data()[i*m + j] = numv[j];
423 }
424 }//nummatrix(...)
425
426
432 template <typename P>
433 nummatrix(const std::initializer_list<numvector<P, m>>& z)
434 {
435 if (z.size() != n)
436 throw;
437 for (size_t i = 0; i < n; ++i)
438 {
439 const numvector<T, m>& numv = *(z.begin() + i);
440 for (size_t j = 0; j < m; ++j)
441 this->data()[i*m + j] = numv[j];
442 }
443 }//nummatrix(...)
444#endif
445
446
450 operator std::vector<std::vector<T>>() const
451 {
452 std::vector<std::vector<T>> vec;
453 vec.reserve(n);
454 for (size_t i = 0; i < n; ++i)
455 {
456 vec[i].reserve(m);
457 for (size_t j = 0; j < m; ++j)
458 vec[i].push_back(this->data()[i*m + j]);
459 }
460 return vec;
461 }
462
463
466 {
468 for (size_t i = 0; i < n; ++i)
469 for (size_t j = 0; j < m; ++j)
470 res[j][i] = this->data()[i*m + j];
471 return res;
472 }//Transpose(...)
473
474
482 template<typename P>
483 auto operator&(const numvector<P, m>& x) -> \
484 numvector<typename std::remove_const<decltype(this->data()[0] * x[0])>::type, n>
485 {
486 numvector<typename std::remove_const<decltype(this->data()[0] * x[0])>::type, n> res;
487
488 for (size_t i = 0; i < n; ++i)
489 res[i] = (*this)[i] & x;
490
491 return res;
492 }//operator&
493
501 template<typename P, size_t p>
502 auto operator&(const nummatrix<P, m, p>& B) const -> \
503 nummatrix<typename std::remove_const<decltype(this->data()[0] * B.data()[0])>::type, n, p>
504 {
506 nummatrix<typename std::remove_const<decltype(this->data()[0] * B.data()[0])>::type, n, p> res;
507
508 for (size_t i = 0; i < n; ++i)
509 for (size_t j = 0; j < p; ++j)
510 res[i][j] = 0;
511
512 for (size_t i = 0; i < n; ++i)
513 for (size_t j = 0; j < p; ++j)
514 for (size_t k = 0; k < m; ++k)
515 res[i][j] += (*this)[i][k] * B[k][j];
516 return res;
517 }//operator&
518
519 };
520
521
531 template<typename T, typename P, size_t n, size_t m>
532 auto operator&(const numvector<P, n>& x, const nummatrix<T, n, m>& A) -> \
533 numvector<typename std::remove_const<decltype(x[0] * A.data()[0])>::type, m>
534 {
535 numvector<typename std::remove_const<decltype(x[0] * A.data()[0])>::type, m> res;
536 for (size_t i = 0; i < m; ++i)
537 {
538 res[i] = 0;
539 for (size_t j = 0; j < n; ++j)
540 res[i] += x[j] * A[j][i];
541 }
542 return res;
543 }//operator&(...)
544
545
547 template<typename T, typename P, size_t n, size_t m>
548 inline auto operator| (const numvector<T, n>& x, const numvector<P, m>& y) -> \
549 nummatrix<typename std::remove_const<decltype(x[0] * y[0])>::type, n, m>
550 {
551 nummatrix<typename std::remove_const<decltype(x[0] * y[0])>::type, n, m> res;
552
553 for (size_t i = 0; i < n; ++i)
554 for (size_t j = 0; j < m; ++j)
555 res[i][j] = x[i] * y[j];
556
557 return res;
558 }//operator|(...)
559
560
561
571 template<typename T, size_t n, size_t m>
572 std::ostream& operator<< (std::ostream& str, const nummatrix<T, n, m>& x)
573 {
574 str << "{ ";
575 for (size_t j = 0; j < n - 1; ++j)
576 str << x[j] << ", ";
577 str << x[n - 1];
578 str << " }";
579 return str;
580 }//operator<<
581
582}//namespace VMlib
583
584using VMlib::nummatrix;
585
586#endif
Шаблонный класс, определяющий матрицу фиксированного размера Фактически представляет собой массив,...
Definition nummatrix.h:66
nummatrix< T, m, n > transpose() const
Транспонирование
Definition nummatrix.h:465
nummatrix(const numvector< numvector< P, m >, n > &mtr)
Конструктор инициализации при помощи numvector из numvector.
Definition nummatrix.h:400
nummatrix< T, n, n > & toIdentity()
Definition nummatrix.h:313
nummatrix(const P z)
Конструктор, инициализирующий всю матрицу одной и той же константой
Definition nummatrix.h:355
nummatrix(const std::initializer_list< numvector< T, m > > &z)
Нешаблонный конструктор инициализации списком из numvector.
Definition nummatrix.h:414
nummatrix< T, n, m > operator-() const
Перегрузка оператора "-" унарного минуса
Definition nummatrix.h:220
nummatrix< T, m, n > t() const
Definition nummatrix.h:327
auto operator&(const nummatrix< P, m, p > &B) const -> nummatrix< typename std::remove_const< decltype(this->data()[0] *B.data()[0])>::type, n, p >
Умножение матрицы на матрицу
Definition nummatrix.h:502
auto norminf() const -> typename std::remove_const< typename std::remove_reference< decltype(this->data()[0])>::type >::type
Вычисление inf-нормы матрицы
Definition nummatrix.h:283
nummatrix()
Пустой конструктор
Definition nummatrix.h:347
nummatrix(const std::initializer_list< numvector< P, m > > &z)
Шаблонный конструктор инициализации списком из numvector.
Definition nummatrix.h:433
auto operator&(const numvector< P, m > &x) -> numvector< typename std::remove_const< decltype(this->data()[0] *x[0])>::type, n >
Умножение матрицы на вектор
Definition nummatrix.h:483
auto operator+(const nummatrix< P, n, m > &y) const -> nummatrix< typename std::remove_const< decltype(this->data()[0]+y.data()[0])>::type, n, m >
Перегрузка оператора "+" сложения двух матриц
Definition nummatrix.h:171
bool operator!=(const nummatrix< P, n, m > &y) const
Перегрузка оператора "!=" логического неравенства
Definition nummatrix.h:256
bool operator==(const nummatrix< P, n, m > &y) const
Перегрузка оператора "==" логического равенства
Definition nummatrix.h:238
auto operator-(const nummatrix< P, n, m > &y) const -> nummatrix< typename std::remove_const< decltype(this->data()[0] - y.data()[0])>::type, n, m >
Перегрузка оператора "-" вычитания двух матриц
Definition nummatrix.h:189
nummatrix(const nummatrix< P, n, m > &mtr)
Конструктор копирования
Definition nummatrix.h:369
nummatrix< T, n, m > & toZero(T val=0.0)
Definition nummatrix.h:300
nummatrix< T, n, m > & operator-=(const nummatrix< P, n, m > &y)
Перегрузка оператора "-=" вычитания другой матрицы
Definition nummatrix.h:154
nummatrix< T, n, n > sym() const
Definition nummatrix.h:341
auto norm1() const -> typename std::remove_const< typename std::remove_reference< decltype(this->data()[0])>::type >::type
Вычисление 1-нормы матрицы
Definition nummatrix.h:271
nummatrix< T, n, m > & operator/=(const P c)
Перегрузка оператора "/=" деления матрицы на действительное число
Definition nummatrix.h:120
const numvector< T, m > & operator[](size_t i) const
Перегрузка оператора "[]" доступа к строке
Definition nummatrix.h:87
nummatrix< T, n, m > & operator+=(const nummatrix< P, n, m > &y)
Перегрузка оператора "+=" прибавления другой матрицы
Definition nummatrix.h:137
std::pair< size_t, size_t > size() const
Вычисление размерности матрицы (числа строк и столбцов в ней)
Definition nummatrix.h:265
auto operator*(const P c) const -> nummatrix< typename std::remove_const< decltype(this->data()[0] *c)>::type, n, m >
Перегрузка оператора "*" умножения матрицы справа на число
Definition nummatrix.h:204
nummatrix< T, n, m > & operator*=(const P c)
Перегрузка оператора "*=" домножения матрицы на действительное число
Definition nummatrix.h:103
nummatrix(const std::vector< std::vector< P > > &vec)
Конструктор инициализации с помощью std::vector из std::vector.
Definition nummatrix.h:383
numvector< T, m > & operator[](size_t i)
Перегрузка оператора "[]" доступа к строке
Definition nummatrix.h:77
Шаблонный класс, определяющий вектор фиксированной длины Фактически представляет собой массив,...
Definition numvector.h:99
auto operator|(const numvector< T, n > &x, const numvector< P, m > &y) -> nummatrix< typename std::remove_const< decltype(x[0] *y[0])>::type, n, m >
Умножение вектора на вектор внешним образом
Definition nummatrix.h:548
auto operator&(const numvector< P, n > &x, const nummatrix< T, n, m > &A) -> numvector< typename std::remove_const< decltype(x[0] *A.data()[0])>::type, m >
Умножение вектора на матрицу
Definition nummatrix.h:532
std::ostream & operator<<(std::ostream &_stream, const std::vector< T > &_vec)
Переопределение оператора "<<" для вывода в поток вектора std::vector.
Definition defs.h:292
Описание класса numvector.