35template <
typename T,
size_t W,
size_t H>
50 for (
size_t i = 0;
i <
H; ++
i) {
51 for (
size_t j = 0;
j <
W; ++
j) {
52 result[
i][
j] = (
i ==
j) ?
static_cast<T>(1) :
static_cast<T>(0);
66 static_assert(
W ==
H,
"Matrix can only be calculated for square matrices");
67 static_assert(
W >= 2,
"Matrix size must be at least 2x2 for rotation matrix generation.");
140 for (
size_t i = 0;
i <
H; ++
i) {
141 for (
size_t j = 0;
j <
W; ++
j) {
142 result[
j][
i] = d[
i][
j];
155 for (
size_t i = 0;
i <
H; ++
i) {
156 for (
size_t j = 0;
j <
W; ++
j) {
170 for (
size_t i = 0;
i <
H; ++
i) {
171 for (
size_t j = 0;
j <
W; ++
j) {
185 for (
size_t i = 0;
i <
H; ++
i) {
186 for (
size_t j = 0;
j <
W; ++
j) {
201 for (
size_t i = 0;
i <
W; ++
i) {
202 for (
size_t j = 0;
j <
K; ++
j) {
204 for (
size_t k = 0;
k <
H; ++
k) {
220 for (
size_t i = 0;
i <
H; ++
i) {
221 for (
size_t j = 0;
j <
W; ++
j) {
234 for (
size_t i = 0;
i <
H; ++
i) {
235 for (
size_t j = 0;
j <
W; ++
j) {
239 L[
j][
i] = (*this)[
j][
i];
240 for (
size_t k = 0;
k <
i; ++
k) {
251 for (
size_t k = 0;
k <
i; ++
k) {
268 static_assert(
W ==
H,
"Determinant can only be calculated for square matrices");
270 if constexpr (
W == 2) {
271 return d[0][0] * d[1][1] - d[0][1] * d[1][0];
272 }
else if constexpr (
W == 3) {
273 return d[0][0] * (d[1][1] * d[2][2] - d[1][2] * d[2][1]) -
274 d[0][1] * (d[1][0] * d[2][2] - d[1][2] * d[2][0]) +
275 d[0][2] * (d[1][0] * d[2][1] - d[1][1] * d[2][0]);
283 for (
size_t i = 0;
i <
H; ++
i) {
300 static_assert(
W ==
H,
"Inverse can only be calculated for square matrices");
309 if constexpr (
W == 2) {
310 result[0][0] = d[1][1] /
det;
311 result[0][1] = -d[0][1] /
det;
312 result[1][0] = -d[1][0] /
det;
313 result[1][1] = d[0][0] /
det;
314 }
else if constexpr (
W == 3) {
315 result[0][0] = (d[1][1] * d[2][2] - d[1][2] * d[2][1]) /
det;
316 result[0][1] = (d[0][2] * d[2][1] - d[0][1] * d[2][2]) /
det;
317 result[0][2] = (d[0][1] * d[1][2] - d[0][2] * d[1][1]) /
det;
318 result[1][0] = (d[1][2] * d[2][0] - d[1][0] * d[2][2]) /
det;
319 result[1][1] = (d[0][0] * d[2][2] - d[0][2] * d[2][0]) /
det;
320 result[1][2] = (d[0][2] * d[1][0] - d[0][0] * d[1][2]) /
det;
321 result[2][0] = (d[1][0] * d[2][1] - d[1][1] * d[2][0]) /
det;
322 result[2][1] = (d[0][1] * d[2][0] - d[0][0] * d[2][1]) /
det;
323 result[2][2] = (d[0][0] * d[1][1] - d[0][1] * d[1][0]) /
det;
329 for (
size_t i = 0;
i <
W; ++
i) {
332 for (
size_t j =
i + 1;
j <
H; ++
j) {
333 if (std::abs(temp[
j][
i]) > std::abs(temp[
pivot][
i])) {
351 for (
size_t j = 0;
j <
W; ++
j) {
357 for (
size_t j = 0;
j <
H; ++
j) {
360 for (
size_t k = 0;
k <
W; ++
k) {
381 static_assert(
W == 1 ||
H == 1,
"Both matrices must have a single row or a single column");
382 static_assert(
L ==
H ||
L ==
W,
"The matrices must have the same number of elements");
384 size_t size = (
W == 1) ?
H :
W;
387 if constexpr (
W == 1 &&
other.width() == 1) {
388 for (
size_t i = 0;
i < size; ++
i) {
389 result += (*this)[
i][0] *
other[
i][0];
391 }
else if constexpr (
H == 1 &&
other.height() == 1) {
392 for (
size_t i = 0;
i < size; ++
i) {
393 result += (*this)[0][
i] *
other[0][
i];
407 for (
size_t i = 0;
i <
H; ++
i) {
408 for (
size_t j = 0;
j <
W; ++
j) {
409 result[
i][
j] = func((*
this)[
i][
j]);
421 for (
size_t i = 0;
i <
H; ++
i) {
422 for (
size_t j = 0;
j <
W; ++
j) {
423 sum += (*this)[
i][
j] * (*this)[
i][
j];
426 return std::sqrt(
sum);
434 static_assert(
W ==
H,
"Trace can only be calculated for square matrices");
436 for (
size_t i = 0;
i <
H; ++
i) {
437 sum += (*this)[
i][
i];
448 for (
size_t i = 0;
i <
H; ++
i) {
449 for (
size_t j = 0;
j <
W; ++
j) {
463 for (
size_t i = 0;
i <
H; ++
i) {
464 for (
size_t j = 0;
j <
W; ++
j) {
481 static_assert(
W == 1,
"The first matrix must have a single column");
483 for (
size_t i = 0;
i <
H; ++
i) {
484 for (
size_t j = 0;
j <
L; ++
j) {
485 result[
i][
j] = (*this)[
i][0] *
other[
j][0];
497 for (
size_t i = 0;
i <
H; ++
i) {
499 for (
size_t j = 0;
j <
W; ++
j) {
513 for (
size_t j = 0;
j <
W; ++
j) {
515 for (
size_t i = 0;
i <
H; ++
i) {
528 static_assert(
W ==
H,
"Diagonal can only be calculated for square matrices");
531 for (
size_t i = 0;
i <
W; ++
i) {
532 result[
i][0] = (*this)[
i][
i];
543 for (
size_t i = 0;
i <
H; ++
i) {
545 for (
size_t j = 0;
j <
W; ++
j) {
559 for (
size_t j = 0;
j <
W; ++
j) {
561 for (
size_t i = 0;
i <
H; ++
i) {
575 for (
size_t i = 0;
i <
H; ++
i) {
576 for (
size_t j = 0;
j <
W; ++
j) {
577 result[
i][
j] = (*this)[
H -
j - 1][
i];
589 for (
size_t i = 0;
i <
H; ++
i) {
590 for (
size_t j = 0;
j <
W; ++
j) {
591 result[
i][
j] = (*this)[
j][
W -
i - 1];
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ InvalidDimension
Invalid dimension value.
Definition error.h:87
@ SingularMatrix
Matrix is singular and cannot be inverted.
Definition error.h:54
Dynamic array container wrapping std::vector.
Definition list.h:40
void swap(List< T > &other) noexcept
Swaps the list data with another list of the same type.
Definition list.h:530
Generic fixed-size matrix with compile-time dimensions.
Definition matrix.h:36
T determinant() const
Computes the determinant of a square matrix.
Definition matrix.h:267
static Matrix< T, W, H > identity()
Creates an identity matrix.
Definition matrix.h:48
Matrix< T, W, H > operator/(const T &scalar) const
Scalar division.
Definition matrix.h:218
Matrix< T, H, W > rotateCCW() const
Rotates the matrix 90 degrees counter-clockwise.
Definition matrix.h:587
Matrix< T, W, H > apply(Func &&func) const
Applies a function to each element of the matrix.
Definition matrix.h:405
Matrix< double, W, 1 > colMean() const
Computes the mean of each column.
Definition matrix.h:557
Matrix< double, 1, H > rowMean() const
Computes the mean of each row.
Definition matrix.h:541
DataType & data()
Returns a mutable reference to the underlying data.
Definition matrix.h:110
RowDataType & operator[](size_t index)
Accesses a row by index.
Definition matrix.h:96
void LUdecomposition(Matrix< T, W, H > &L, Matrix< T, W, H > &U) const
Computes the LU decomposition of this matrix.
Definition matrix.h:233
size_t height() const
Returns the number of rows.
Definition matrix.h:125
T dot(const Matrix< T, L, 1 > &other) const
Computes the dot product between two matrices treated as vectors.
Definition matrix.h:380
Matrix< T, W, 1 > diagonal() const
Extracts the diagonal elements of a square matrix.
Definition matrix.h:527
const DataType & data() const
Returns a const reference to the underlying data.
Definition matrix.h:115
Matrix< T, H, W > transpose() const
Returns the transpose of this matrix.
Definition matrix.h:138
Matrix< T, H, L > outer_product(const Matrix< T, L, 1 > &other) const
Computes the outer product of two column vectors.
Definition matrix.h:480
Matrix(const DataType &data)
Constructs a matrix from existing data.
Definition matrix.h:89
Matrix< T, W, K > operator*(const Matrix< T, H, K > &other) const
Matrix multiplication.
Definition matrix.h:199
Matrix< T, W, 1 > colSum() const
Computes the sum of each column.
Definition matrix.h:511
Matrix< T, W, H > operator*(const T &scalar) const
Scalar multiplication.
Definition matrix.h:183
T sum() const
Computes the sum of all elements in the matrix.
Definition matrix.h:446
size_t width() const
Returns the number of columns.
Definition matrix.h:120
bool isSquare() const
Returns true if the matrix is square (W == H).
Definition matrix.h:130
Matrix< T, W, H > operator+(const Matrix< T, W, H > &other) const
Element-wise matrix addition.
Definition matrix.h:153
T trace() const
Computes the trace (sum of diagonal elements) of a square matrix.
Definition matrix.h:433
Array< RowDataType, H > DataType
Type alias for the entire matrix storage (array of rows).
Definition matrix.h:42
Matrix< T, W, H > hadamardProduct(const Matrix< T, W, H > &other) const
Computes the Hadamard (element-wise) product of two matrices.
Definition matrix.h:461
const RowDataType & operator[](size_t index) const
Accesses a row by index (const).
Definition matrix.h:105
Matrix< T, W, H > inverse(Error *err=nullptr) const
Computes the inverse of a square matrix.
Definition matrix.h:299
Matrix< T, 1, H > rowSum() const
Computes the sum of each row.
Definition matrix.h:495
T frobeniusNorm() const
Computes the Frobenius norm of the matrix.
Definition matrix.h:419
Matrix< T, W, H > operator-(const Matrix< T, W, H > &other) const
Element-wise matrix subtraction.
Definition matrix.h:168
static Matrix< T, W, H > rotationMatrix(T radians, size_t dim, Error *err=nullptr)
Creates a rotation matrix for the given angle and dimension pair.
Definition matrix.h:65
Matrix()
Constructs a zero-initialized matrix.
Definition matrix.h:83
Matrix< T, H, W > rotateCW() const
Rotates the matrix 90 degrees clockwise.
Definition matrix.h:573
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19