qualpal 3.3.0
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <array>
14#include <cassert>
15#include <vector>
16
17namespace qualpal {
18
35template<typename T>
36class Matrix
37{
38public:
44 Matrix(std::size_t rows, std::size_t cols)
45 : rows(rows)
46 , cols(cols)
47 , data(rows * cols)
48 {
49 }
50
55 : rows(0)
56 , cols(0)
57 , data()
58 {
59 }
60
67 Matrix(std::size_t rows, std::size_t cols, const std::vector<T>& data)
68 : rows(rows)
69 , cols(cols)
70 , data(data)
71 {
72 assert(rows * cols == data.size());
73 }
74
83 T& operator()(std::size_t row, std::size_t col)
84 {
85 return data[col * rows + row];
86 }
87
96 const T& operator()(std::size_t row, std::size_t col) const
97 {
98 return data[col * rows + row];
99 }
100
106 {
107 Matrix<T> result(cols, rows);
108 for (std::size_t i = 0; i < rows; ++i) {
109 for (std::size_t j = 0; j < cols; ++j) {
110 result(j, i) = (*this)(i, j);
111 }
112 }
113 return result;
114 }
115
117 std::size_t ncol() const { return cols; }
118
120 std::size_t nrow() const { return rows; }
121
122private:
123 std::size_t rows, cols;
124 std::vector<T> data;
125};
126
144template<typename T, std::size_t rows, std::size_t cols>
146{
147public:
149 constexpr FixedMatrix()
150 : data{}
151 {
152 }
153
158 constexpr FixedMatrix(std::initializer_list<std::initializer_list<T>> list)
159 : data{}
160 {
161 auto it = list.begin();
162 for (std::size_t i = 0; i < rows && it != list.end(); ++i, ++it) {
163 auto col_it = it->begin();
164 for (std::size_t j = 0; j < cols && col_it != it->end(); ++j, ++col_it) {
165 data[i * cols + j] = *col_it;
166 }
167 }
168 }
169
175 {
177 for (std::size_t i = 0; i < rows; ++i) {
178 for (std::size_t j = 0; j < cols; ++j) {
179 result(j, i) = (*this)(i, j);
180 }
181 }
182 return result;
183 }
184
193 constexpr T& operator()(std::size_t row, std::size_t col)
194 {
195 return data[row * cols + col];
196 }
197
206 constexpr const T& operator()(std::size_t row, std::size_t col) const
207 {
208 return data[row * cols + col];
209 }
210
216 constexpr FixedMatrix<T, rows, cols> operator*(const T& scalar) const
217 {
219 for (std::size_t i = 0; i < rows * cols; ++i) {
220 result.data[i] = this->data[i] * scalar;
221 }
222 return result;
223 }
224
230 constexpr std::array<T, rows> operator*(const std::array<T, cols>& vec) const
231 {
232 std::array<T, rows> result{};
233 for (std::size_t i = 0; i < rows; ++i) {
234 for (std::size_t j = 0; j < cols; ++j) {
235 result[i] += (*this)(i, j) * vec[j];
236 }
237 }
238 return result;
239 }
240
247 template<std::size_t other_cols>
249 const FixedMatrix<T, cols, other_cols>& other) const
250 {
252 result.zeros();
253 for (std::size_t i = 0; i < rows; ++i) {
254 for (std::size_t j = 0; j < other_cols; ++j) {
255 for (std::size_t k = 0; k < cols; ++k) {
256 result(i, j) += (*this)(i, k) * other(k, j);
257 }
258 }
259 }
260 return result;
261 }
262
268 constexpr Matrix<T> operator*(const Matrix<T>& other) const
269 {
270 assert(cols == other.nrow());
271 Matrix<T> result(rows, other.ncol());
272 for (std::size_t i = 0; i < rows; ++i) {
273 for (std::size_t j = 0; j < other.ncol(); ++j) {
274 for (std::size_t k = 0; k < cols; ++k) {
275 result(i, j) += (*this)(i, k) * other(k, j);
276 }
277 }
278 }
279 return result;
280 }
281
288 const FixedMatrix<T, rows, cols>& other) const
289 {
291 for (std::size_t i = 0; i < rows * cols; ++i) {
292 result.data[i] = this->data[i] + other.data[i];
293 }
294 return result;
295 }
296
303 const FixedMatrix<T, rows, cols>& other) const
304 {
306 for (std::size_t i = 0; i < rows * cols; ++i) {
307 result.data[i] = this->data[i] - other.data[i];
308 }
309 return result;
310 }
311
313 constexpr void fill(const T& value) { data.fill(value); }
314
316 constexpr void zeros() { data.fill(0); }
317
318private:
319 std::array<T, rows * cols> data;
320};
321
322} // namespace qualpal
Fixed-size matrix class with compile-time dimensions.
Definition matrix.h:146
constexpr void fill(const T &value)
Fill all elements with specified value.
Definition matrix.h:313
constexpr FixedMatrix< T, rows, cols > operator+(const FixedMatrix< T, rows, cols > &other) const
Matrix addition.
Definition matrix.h:287
constexpr Matrix< T > operator*(const Matrix< T > &other) const
Matrix-matrix multiplication (FixedMatrix * dynamic Matrix)
Definition matrix.h:268
constexpr FixedMatrix< T, rows, other_cols > operator*(const FixedMatrix< T, cols, other_cols > &other) const
Matrix-matrix multiplication (FixedMatrix * FixedMatrix).
Definition matrix.h:248
constexpr T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable).
Definition matrix.h:193
constexpr FixedMatrix< T, rows, cols > operator-(const FixedMatrix< T, rows, cols > &other) const
Matrix subtraction.
Definition matrix.h:302
constexpr FixedMatrix< T, cols, rows > t() const
Create transpose of this matrix.
Definition matrix.h:174
constexpr std::array< T, rows > operator*(const std::array< T, cols > &vec) const
Matrix-vector multiplication.
Definition matrix.h:230
constexpr FixedMatrix< T, rows, cols > operator*(const T &scalar) const
Scalar multiplication.
Definition matrix.h:216
constexpr void zeros()
Set all elements to zero.
Definition matrix.h:316
constexpr FixedMatrix()
Default constructor - elements are zero-initialized.
Definition matrix.h:149
constexpr FixedMatrix(std::initializer_list< std::initializer_list< T > > list)
Construct from nested initializer list.
Definition matrix.h:158
constexpr const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const).
Definition matrix.h:206
Dynamic matrix class with runtime-determined dimensions.
Definition matrix.h:37
Matrix< T > transpose() const
Create transpose of this matrix.
Definition matrix.h:105
const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const).
Definition matrix.h:96
std::size_t nrow() const
Get number of rows.
Definition matrix.h:120
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &data)
Construct matrix with specified dimensions and data.
Definition matrix.h:67
std::size_t ncol() const
Get number of columns.
Definition matrix.h:117
Matrix()
Default constructor creates an empty matrix (0x0)
Definition matrix.h:54
Matrix(std::size_t rows, std::size_t cols)
Construct matrix with specified dimensions.
Definition matrix.h:44
T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable).
Definition matrix.h:83
Qualitative color palette generation library.
Definition analyze.h:21