OpenLB 1.7
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
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
27namespace olb {
28
30template <typename T, unsigned ROWS, unsigned COLS>
31class Matrix {
32private:
33 Vector<Vector<T, COLS>, ROWS> _data;
34
35public:
37 static constexpr unsigned rows = ROWS;
39 static constexpr unsigned cols = COLS;
40
41 constexpr Matrix() {};
42 constexpr Matrix(T data[ROWS][COLS])
43 {
44 for (unsigned m = 0; m < ROWS; ++m) {
45 for (unsigned n = 0; n < COLS; ++n) {
46 _data[m][n] = data[m][n];
47 }
48 }
49 }
50
52 template <unsigned V_SIZE>
53 constexpr Matrix(const Vector<T, V_SIZE>& vector)
54 {
55 static_assert(V_SIZE == ROWS * COLS,
56 "ERROR: Vector size must correspond to matrix size.");
57 for (unsigned m = 0; m < ROWS; ++m) {
58 for (unsigned n = 0; n < COLS; ++n) {
59 _data[m][n] = vector[m * COLS + n];
60 }
61 }
62 }
63
64 constexpr Matrix(const Matrix<T, ROWS, COLS>& matrix)
65 {
66 _data = matrix._data;
67 }
68 constexpr Matrix& operator=(const Matrix& matrix)
69 {
70 _data = matrix._data;
71 return *this;
72 }
73 constexpr Matrix(Matrix&& matrix) { _data = std::move(matrix._data); }
74 constexpr Matrix& operator=(Matrix&& matrix)
75 {
76 _data = std::move(matrix._data);
77 return *this;
78 }
79
80 constexpr T* data() { return _data.data(); }
81 constexpr const T* data() const { return _data.data(); }
82
83 constexpr Vector<T, COLS>& operator[](const unsigned row)
84 {
85 return _data[row];
86 };
87 constexpr const Vector<T, COLS>& operator[](const unsigned row) const
88 {
89 return _data[row];
90 }
91
92 friend std::ostream& operator<<(std::ostream& os, const Matrix& matrix)
93 {
94 for (unsigned row = 0; row < matrix.rows; ++row) {
95 os << matrix[row] << '\n';
96 }
97 return os;
98 }
99
100 template <unsigned C>
101 constexpr inline Matrix operator*(const Matrix<T, COLS, C>& matrix) const
102 {
103 Matrix<T,ROWS,C> result;
104 for (unsigned i = 0; i < ROWS; ++i) {
105 for (unsigned j = 0; j < C; ++j) {
106 result[i][j] = 0;
107 for (unsigned k = 0; k < ROWS; ++k) {
108 result[i][j] += this->operator[](i)[k] * matrix[k][j];
109 }
110 }
111 }
112 return result;
113 }
114
115 constexpr inline Vector<T, COLS>
116 operator*(const Vector<T, COLS>& vector) const
117 {
118 Vector<T, COLS> result;
119 for (unsigned row = 0; row < ROWS; ++row) {
120 result[row] = _data[row] * vector;
121 }
122 return result;
123 }
124
125 constexpr inline Matrix<T, COLS, ROWS> transpose() const
126 {
127 Matrix<T,COLS,ROWS> result;
128 for (unsigned m = 0; m < ROWS; ++m) {
129 for (unsigned n = 0; n < COLS; ++n) {
130 result[n][m] = ((*this)[m][n]);
131 }
132 }
133 return result;
134 }
135};
136
137} // namespace olb
138
139#endif
Matrix with a defined number of ROWS and columns (COLS)
Definition matrix.h:31
static constexpr unsigned rows
number of rows
Definition matrix.h:37
friend std::ostream & operator<<(std::ostream &os, const Matrix &matrix)
Definition matrix.h:92
constexpr Matrix< T, COLS, ROWS > transpose() const
Definition matrix.h:125
constexpr Vector< T, COLS > & operator[](const unsigned row)
Definition matrix.h:83
constexpr Matrix()
Definition matrix.h:41
constexpr const T * data() const
Definition matrix.h:81
constexpr Matrix & operator=(Matrix &&matrix)
Definition matrix.h:74
constexpr Matrix(T data[ROWS][COLS])
Definition matrix.h:42
constexpr Matrix(const Vector< T, V_SIZE > &vector)
Create matrix from olb::Vector.
Definition matrix.h:53
static constexpr unsigned cols
number of columns
Definition matrix.h:39
constexpr Matrix(Matrix &&matrix)
Definition matrix.h:73
constexpr Matrix operator*(const Matrix< T, COLS, C > &matrix) const
Definition matrix.h:101
constexpr T * data()
Definition matrix.h:80
constexpr const Vector< T, COLS > & operator[](const unsigned row) const
Definition matrix.h:87
constexpr Vector< T, COLS > operator*(const Vector< T, COLS > &vector) const
Definition matrix.h:116
constexpr Matrix(const Matrix< T, ROWS, COLS > &matrix)
Definition matrix.h:64
constexpr Matrix & operator=(const Matrix &matrix)
Definition matrix.h:68
Plain old scalar vector.
Definition vector.h:47
constexpr const T * data() const any_platform
Definition vector.h:161
Top level namespace for all of OpenLB.