qualpal 2.3.0
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <array>
12#include <cassert>
13#include <vector>
14
15namespace qualpal {
16
21template<typename T>
22class Matrix
23{
24public:
30 Matrix(std::size_t rows, std::size_t cols)
31 : rows(rows)
32 , cols(cols)
33 , data(rows * cols)
34 {
35 }
36
41 : rows(0)
42 , cols(0)
43 , data()
44 {
45 }
46
53 Matrix(std::size_t rows, std::size_t cols, const std::vector<T>& data)
54 : rows(rows)
55 , cols(cols)
56 , data(data)
57 {
58 assert(rows * cols == data.size());
59 }
60
67 T& operator()(std::size_t row, std::size_t col)
68 {
69 return data[col * rows + row];
70 }
71
78 const T& operator()(std::size_t row, std::size_t col) const
79 {
80 return data[col * rows + row];
81 }
82
88 {
89 Matrix<T> result(cols, rows);
90 for (std::size_t i = 0; i < rows; ++i) {
91 for (std::size_t j = 0; j < cols; ++j) {
92 result(j, i) = (*this)(i, j);
93 }
94 }
95 return result;
96 }
97
99 std::size_t ncol() const { return cols; }
100
102 std::size_t nrow() const { return rows; }
103
104private:
105 std::size_t rows, cols;
106 std::vector<T> data;
107};
108
115template<typename T, std::size_t rows, std::size_t cols>
117{
118public:
120 constexpr FixedMatrix()
121 : data{}
122 {
123 }
124
129 constexpr FixedMatrix(std::initializer_list<std::initializer_list<T>> list)
130 : data{}
131 {
132 auto it = list.begin();
133 for (std::size_t i = 0; i < rows && it != list.end(); ++i, ++it) {
134 auto col_it = it->begin();
135 for (std::size_t j = 0; j < cols && col_it != it->end(); ++j, ++col_it) {
136 data[i * cols + j] = *col_it;
137 }
138 }
139 }
140
146 {
148 for (std::size_t i = 0; i < rows; ++i) {
149 for (std::size_t j = 0; j < cols; ++j) {
150 result(j, i) = (*this)(i, j);
151 }
152 }
153 return result;
154 }
155
162 constexpr T& operator()(std::size_t row, std::size_t col)
163 {
164 return data[row * cols + col];
165 }
166
173 constexpr const T& operator()(std::size_t row, std::size_t col) const
174 {
175 return data[row * cols + col];
176 }
177
183 constexpr FixedMatrix<T, rows, cols> operator*(const T& scalar) const
184 {
186 for (std::size_t i = 0; i < rows * cols; ++i) {
187 result.data[i] = this->data[i] * scalar;
188 }
189 return result;
190 }
191
197 constexpr std::array<T, rows> operator*(const std::array<T, cols>& vec) const
198 {
199 std::array<T, rows> result{};
200 for (std::size_t i = 0; i < rows; ++i) {
201 for (std::size_t j = 0; j < cols; ++j) {
202 result[i] += (*this)(i, j) * vec[j];
203 }
204 }
205 return result;
206 }
207
214 template<std::size_t other_cols>
216 const FixedMatrix<T, cols, other_cols>& other) const
217 {
219 result.zeros();
220 for (std::size_t i = 0; i < rows; ++i) {
221 for (std::size_t j = 0; j < other_cols; ++j) {
222 for (std::size_t k = 0; k < cols; ++k) {
223 result(i, j) += (*this)(i, k) * other(k, j);
224 }
225 }
226 }
227 return result;
228 }
229
235 constexpr Matrix<T> operator*(const Matrix<T>& other) const
236 {
237 assert(cols == other.nrow());
238 Matrix<T> result(rows, other.ncol());
239 for (std::size_t i = 0; i < rows; ++i) {
240 for (std::size_t j = 0; j < other.ncol(); ++j) {
241 for (std::size_t k = 0; k < cols; ++k) {
242 result(i, j) += (*this)(i, k) * other(k, j);
243 }
244 }
245 }
246 return result;
247 }
248
255 const FixedMatrix<T, rows, cols>& other) const
256 {
258 for (std::size_t i = 0; i < rows * cols; ++i) {
259 result.data[i] = this->data[i] + other.data[i];
260 }
261 return result;
262 }
263
270 const FixedMatrix<T, rows, cols>& other) const
271 {
273 for (std::size_t i = 0; i < rows * cols; ++i) {
274 result.data[i] = this->data[i] - other.data[i];
275 }
276 return result;
277 }
278
280 constexpr void fill(const T& value) { data.fill(value); }
281
283 constexpr void zeros() { data.fill(0); }
284
285private:
286 std::array<T, rows * cols> data;
287};
288
289} // namespace qualpal
Fixed-size matrix class with compile-time dimensions.
Definition matrix.h:117
constexpr void fill(const T &value)
Fill all elements with specified value.
Definition matrix.h:280
constexpr FixedMatrix< T, rows, cols > operator+(const FixedMatrix< T, rows, cols > &other) const
Matrix addition.
Definition matrix.h:254
constexpr Matrix< T > operator*(const Matrix< T > &other) const
Matrix-matrix multiplication (FixedMatrix * dynamic Matrix)
Definition matrix.h:235
constexpr FixedMatrix< T, rows, other_cols > operator*(const FixedMatrix< T, cols, other_cols > &other) const
Matrix-matrix multiplication (FixedMatrix * FixedMatrix)
Definition matrix.h:215
constexpr T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable)
Definition matrix.h:162
constexpr FixedMatrix< T, rows, cols > operator-(const FixedMatrix< T, rows, cols > &other) const
Matrix subtraction.
Definition matrix.h:269
constexpr FixedMatrix< T, cols, rows > t() const
Create transpose of this matrix.
Definition matrix.h:145
constexpr std::array< T, rows > operator*(const std::array< T, cols > &vec) const
Matrix-vector multiplication.
Definition matrix.h:197
constexpr FixedMatrix< T, rows, cols > operator*(const T &scalar) const
Scalar multiplication.
Definition matrix.h:183
constexpr void zeros()
Set all elements to zero.
Definition matrix.h:283
constexpr FixedMatrix()
Default constructor - elements are zero-initialized.
Definition matrix.h:120
constexpr FixedMatrix(std::initializer_list< std::initializer_list< T > > list)
Construct from nested initializer list.
Definition matrix.h:129
constexpr const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const)
Definition matrix.h:173
Dynamic matrix class with runtime-determined dimensions.
Definition matrix.h:23
Matrix< T > transpose() const
Create transpose of this matrix.
Definition matrix.h:87
const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const)
Definition matrix.h:78
std::size_t nrow() const
Get number of rows.
Definition matrix.h:102
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &data)
Construct matrix with specified dimensions and data.
Definition matrix.h:53
std::size_t ncol() const
Get number of columns.
Definition matrix.h:99
Matrix()
Default constructor creates an empty matrix (0x0)
Definition matrix.h:40
Matrix(std::size_t rows, std::size_t cols)
Construct matrix with specified dimensions.
Definition matrix.h:30
T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable)
Definition matrix.h:67
Qualitative color palette generation library.
Definition analyze.h:19