30 Matrix(std::size_t rows, std::size_t cols)
53 Matrix(std::size_t rows, std::size_t cols,
const std::vector<T>& data)
58 assert(rows * cols == data.size());
69 return data[col * rows + row];
78 const T&
operator()(std::size_t row, std::size_t col)
const
80 return data[col * rows + row];
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);
99 std::size_t
ncol()
const {
return cols; }
102 std::size_t
nrow()
const {
return rows; }
105 std::size_t rows, cols;
115template<
typename T, std::
size_t rows, std::
size_t cols>
129 constexpr FixedMatrix(std::initializer_list<std::initializer_list<T>> list)
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;
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);
164 return data[row * cols + col];
173 constexpr const T&
operator()(std::size_t row, std::size_t col)
const
175 return data[row * cols + col];
186 for (std::size_t i = 0; i < rows * cols; ++i) {
187 result.data[i] = this->data[i] * scalar;
197 constexpr std::array<T, rows>
operator*(
const std::array<T, cols>& vec)
const
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];
214 template<std::
size_t other_cols>
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);
237 assert(cols == other.
nrow());
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);
258 for (std::size_t i = 0; i < rows * cols; ++i) {
259 result.data[i] = this->data[i] + other.data[i];
273 for (std::size_t i = 0; i < rows * cols; ++i) {
274 result.data[i] = this->data[i] - other.data[i];
280 constexpr void fill(
const T& value) { data.fill(value); }
283 constexpr void zeros() { data.fill(0); }
286 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.