44 Matrix(std::size_t rows, std::size_t cols)
67 Matrix(std::size_t rows, std::size_t cols,
const std::vector<T>& data)
72 assert(rows * cols == data.size());
85 return data[col * rows + row];
96 const T&
operator()(std::size_t row, std::size_t col)
const
98 return data[col * rows + row];
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);
117 std::size_t
ncol()
const {
return cols; }
120 std::size_t
nrow()
const {
return rows; }
123 std::size_t rows, cols;
144template<
typename T, std::
size_t rows, std::
size_t cols>
158 constexpr FixedMatrix(std::initializer_list<std::initializer_list<T>> list)
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;
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);
195 return data[row * cols + col];
206 constexpr const T&
operator()(std::size_t row, std::size_t col)
const
208 return data[row * cols + col];
219 for (std::size_t i = 0; i < rows * cols; ++i) {
220 result.data[i] = this->data[i] * scalar;
230 constexpr std::array<T, rows>
operator*(
const std::array<T, cols>& vec)
const
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];
247 template<std::
size_t other_cols>
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);
270 assert(cols == other.
nrow());
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);
291 for (std::size_t i = 0; i < rows * cols; ++i) {
292 result.data[i] = this->data[i] + other.data[i];
306 for (std::size_t i = 0; i < rows * cols; ++i) {
307 result.data[i] = this->data[i] - other.data[i];
313 constexpr void fill(
const T& value) { data.fill(value); }
316 constexpr void zeros() { data.fill(0); }
319 std::array<T, rows * cols> data;
Fixed-size matrix class with compile-time dimensions.
constexpr void fill(const T &value)
Fill all elements with specified value.
constexpr FixedMatrix< T, rows, cols > operator+(const FixedMatrix< T, rows, cols > &other) const
Matrix addition.
constexpr Matrix< T > operator*(const Matrix< T > &other) const
Matrix-matrix multiplication (FixedMatrix * dynamic Matrix)
constexpr FixedMatrix< T, rows, other_cols > operator*(const FixedMatrix< T, cols, other_cols > &other) const
Matrix-matrix multiplication (FixedMatrix * FixedMatrix).
constexpr T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable).
constexpr FixedMatrix< T, rows, cols > operator-(const FixedMatrix< T, rows, cols > &other) const
Matrix subtraction.
constexpr FixedMatrix< T, cols, rows > t() const
Create transpose of this matrix.
constexpr std::array< T, rows > operator*(const std::array< T, cols > &vec) const
Matrix-vector multiplication.
constexpr FixedMatrix< T, rows, cols > operator*(const T &scalar) const
Scalar multiplication.
constexpr void zeros()
Set all elements to zero.
constexpr FixedMatrix()
Default constructor - elements are zero-initialized.
constexpr FixedMatrix(std::initializer_list< std::initializer_list< T > > list)
Construct from nested initializer list.
constexpr const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const).
Dynamic matrix class with runtime-determined dimensions.
Matrix< T > transpose() const
Create transpose of this matrix.
const T & operator()(std::size_t row, std::size_t col) const
Access matrix element (const).
std::size_t nrow() const
Get number of rows.
Matrix(std::size_t rows, std::size_t cols, const std::vector< T > &data)
Construct matrix with specified dimensions and data.
std::size_t ncol() const
Get number of columns.
Matrix()
Default constructor creates an empty matrix (0x0)
Matrix(std::size_t rows, std::size_t cols)
Construct matrix with specified dimensions.
T & operator()(std::size_t row, std::size_t col)
Access matrix element (mutable).
Qualitative color palette generation library.