OpenLB 1.8.1
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 Jan Eric Marquardt, 2023 Julius Jessberger
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef MATRIX_H
25#define MATRIX_H
26
27#include "vectorHelpers.h"
28
29namespace olb {
30
32template <typename T, unsigned ROWS, unsigned COLS>
33class Matrix {
34private:
35 Vector<Vector<T, COLS>, ROWS> _data;
36
37public:
39 static constexpr unsigned rows = ROWS;
41 static constexpr unsigned cols = COLS;
42
43 constexpr Matrix() {};
44 constexpr Matrix(const T data[ROWS][COLS])
45 {
46 for (unsigned m = 0; m < ROWS; ++m) {
47 for (unsigned n = 0; n < COLS; ++n) {
48 _data[m][n] = data[m][n];
49 }
50 }
51 }
52
54 template <unsigned V_SIZE>
55 constexpr Matrix(const Vector<T, V_SIZE>& vector)
56 {
57 static_assert(V_SIZE == ROWS * COLS,
58 "ERROR: Vector size must correspond to matrix size.");
59 for (unsigned m = 0; m < ROWS; ++m) {
60 for (unsigned n = 0; n < COLS; ++n) {
61 _data[m][n] = vector[m * COLS + n];
62 }
63 }
64 }
65
66 constexpr Matrix(const Matrix<T, ROWS, COLS>& matrix)
67 {
68 _data = matrix._data;
69 }
70 constexpr Matrix& operator=(const Matrix& matrix)
71 {
72 _data = matrix._data;
73 return *this;
74 }
75 constexpr Matrix(Matrix&& matrix) { _data = std::move(matrix._data); }
76 constexpr Matrix& operator=(Matrix&& matrix)
77 {
78 _data = std::move(matrix._data);
79 return *this;
80 }
81
82 constexpr T* data() { return _data.data(); }
83 constexpr const T* data() const { return _data.data(); }
84
85 constexpr Vector<T, COLS>& operator[](const unsigned row)
86 {
87 return _data[row];
88 };
89 constexpr const Vector<T, COLS>& operator[](const unsigned row) const
90 {
91 return _data[row];
92 }
93
94 friend std::ostream& operator<<(std::ostream& os, const Matrix& matrix)
95 {
96 for (unsigned row = 0; row < matrix.rows; ++row) {
97 os << matrix[row] << '\n';
98 }
99 return os;
100 }
101
102 template <unsigned C>
103 constexpr inline Matrix operator*(const Matrix<T, COLS, C>& matrix) const
104 {
105 Matrix<T,ROWS,C> result;
106 for (unsigned i = 0; i < ROWS; ++i) {
107 for (unsigned j = 0; j < C; ++j) {
108 result[i][j] = 0;
109 for (unsigned k = 0; k < ROWS; ++k) {
110 result[i][j] += this->operator[](i)[k] * matrix[k][j];
111 }
112 }
113 }
114 return result;
115 }
116
117 constexpr inline Vector<T, COLS>
118 operator*(const Vector<T, COLS>& vector) const
119 {
120 Vector<T, COLS> result;
121 for (unsigned row = 0; row < ROWS; ++row) {
122 result[row] = _data[row] * vector;
123 }
124 return result;
125 }
126
127 constexpr inline Matrix<T, COLS, ROWS> transpose() const
128 {
129 Matrix<T,COLS,ROWS> result;
130 for (unsigned m = 0; m < ROWS; ++m) {
131 for (unsigned n = 0; n < COLS; ++n) {
132 result[n][m] = ((*this)[m][n]);
133 }
134 }
135 return result;
136 }
137};
138
139
140namespace util {
141
142template <typename T>
143constexpr auto determinant(const Matrix<T,2,2>& m)
144{
145 return determinant<T>(m[0], m[1]);
146}
147
148template <typename T>
149constexpr auto determinant(const Matrix<T,3,3>& m)
150{
151 return determinant<T>(m[0], m[1], m[2]);
152}
153
155// Returns +-inf if a[0], a[1] are linearly dependent and rhs is not (no solution)
156// Returns nan if a[0], a[1], rhs are linearly dependent (infinitely many solutions)
157template <typename T>
159{
160 return solveLinearSystem(a[0], a[1], rhs);
161}
162
164// Returns +-inf if a0, a1, a2 are linearly dependent and rhs is not (no solution)
165// Returns nan if a0, a1, a2, rhs are linearly dependent (infinitely many solutions)
166template <typename T>
168{
169 return solveLinearSystem(a[0], a[1], a[2], rhs);
170}
171
172}
173
174} // namespace olb
175
176#endif
Matrix with a defined number of ROWS and columns (COLS)
Definition matrix.h:33
static constexpr unsigned rows
number of rows
Definition matrix.h:39
friend std::ostream & operator<<(std::ostream &os, const Matrix &matrix)
Definition matrix.h:94
constexpr Matrix< T, COLS, ROWS > transpose() const
Definition matrix.h:127
constexpr Vector< T, COLS > & operator[](const unsigned row)
Definition matrix.h:85
constexpr Matrix()
Definition matrix.h:43
constexpr const T * data() const
Definition matrix.h:83
constexpr Matrix & operator=(Matrix &&matrix)
Definition matrix.h:76
constexpr Matrix(const T data[ROWS][COLS])
Definition matrix.h:44
constexpr Matrix(const Vector< T, V_SIZE > &vector)
Create matrix from olb::Vector.
Definition matrix.h:55
static constexpr unsigned cols
number of columns
Definition matrix.h:41
constexpr Matrix(Matrix &&matrix)
Definition matrix.h:75
constexpr Matrix operator*(const Matrix< T, COLS, C > &matrix) const
Definition matrix.h:103
constexpr T * data()
Definition matrix.h:82
constexpr const Vector< T, COLS > & operator[](const unsigned row) const
Definition matrix.h:89
constexpr Vector< T, COLS > operator*(const Vector< T, COLS > &vector) const
Definition matrix.h:118
constexpr Matrix(const Matrix< T, ROWS, COLS > &matrix)
Definition matrix.h:66
constexpr Matrix & operator=(const Matrix &matrix)
Definition matrix.h:70
Plain old scalar vector.
constexpr const T * data() const any_platform
Definition vector.h:172
auto solveLinearSystem(const Matrix< T, 2, 2 > &a, const Vector< T, 2 > &rhs)
Solve a * x = rhs.
Definition matrix.h:158
constexpr auto determinant(const Matrix< T, 2, 2 > &m)
Definition matrix.h:143
Top level namespace for all of OpenLB.