6#ifndef THEORETICA_MATRIX_H
7#define THEORETICA_MATRIX_H
9#ifndef THEORETICA_NO_PRINT
17#include "../core/error.h"
18#include "../core/constants.h"
19#include "../core/real_analysis.h"
33 template<
typename Matrix,
typename ReturnType = matrix_element_t<Matrix>&>
49 using iterator_category = std::forward_iterator_tag;
50 using value_type = matrix_element_t<Matrix>;
51 using pointer = value_type*;
52 using reference = value_type&;
67 : matrix(matrix), row(row), col(col) {}
75 return matrix(row, col);
88 if(col == matrix.cols()) {
115 return (row ==
other.row) &&
124 return !(*
this ==
other);
135 template<
typename Type = real,
unsigned int N = 0,
unsigned int K = 0>
139#ifdef THEORETICA_ROW_FIRST
159 template<
typename Matrix, enable_matrix<Matrix> = true>
171 template<
typename T = Type>
172 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
174 if(
rows.size() != N) {
183 for (
const auto& row :
rows) {
185 if (row.size() !=
K) {
191 for (
const auto& x : row) {
222 for (
unsigned int i = 0; i <
m; ++i)
223 get(i, i) = diagonal;
235 for (
unsigned int i = 0; i <
m; ++i)
236 get(i, i) = diagonal;
247 template<
typename Vector, enable_vector<Vector> = true>
256 if(v.size() !=
size()) {
262 for (
size_t i = 0; i <
rows(); i++)
263 for (
size_t j = 0;
j <
cols();
j++)
272 template<
typename Matrix>
282 template<
typename Matrix>
293 template<
typename Matrix>
324 template<
typename VecType,
unsigned int M>
327 return algebra::vec_mat_mul<vec<VecType, K>>(v, A);
356 template<
typename Vector, enable_vector<Vector> = true>
359 if(v.size() !=
cols()) {
393 template<
unsigned int M>
407 template<
typename Matrix, enable_matrix<Matrix> = true>
411 res.resize(N, B.cols());
427 template<
typename Matrix, enable_matrix<Matrix> = true>
431 res.resize(N, B.cols());
447 template<
typename Matrix>
457 template<
typename Matrix>
491 template<
typename Matrix>
493 return (*
this = this->
operator*(B));
504 N ==
K,
"The matrix must be square to be transposed in place.");
516#ifdef THEORETICA_ROW_FIRST
517 return elements[i][
j];
519 return elements[
j][i];
529 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
531#ifdef THEORETICA_ROW_FIRST
532 return elements[i][
j];
534 return elements[
j][i];
544 inline Type&
at(
unsigned int i,
unsigned int j) {
547 throw std::out_of_range(
548 "The provided row index in mat::at() is out of range"
553 throw std::out_of_range(
554 "The provided column index in mat::at() is out of range"
567 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
570 throw std::out_of_range(
571 "The provided row index in mat::at() is out of range"
576 throw std::out_of_range(
577 "The provided column index in mat::at() is out of range"
655 inline unsigned int size()
const {
662 return (
Type*) elements;
668 return (
const Type*) elements;
676 template<
typename Vector = vec<Type, N * K>>
687 for (
size_t i = 0; i <
rows(); i++)
688 for (
size_t j = 0;
j <
cols();
j++)
699 template<
typename Matrix>
709 template<
typename Matrix>
720 static_assert(N ==
K,
"The matrix must be square to be invertible.");
735 }
else if(
cols() !=
k) {
744#ifndef THEORETICA_NO_PRINT
753 std::stringstream
res;
755 for (
unsigned int i = 0; i <
rows(); ++i) {
760 for (
unsigned int j = 0;
j <
cols(); ++
j) {
772 res <<
")" << std::endl;
780 inline operator std::string() {
791 return out <<
obj.to_string();
805 template<
typename Type>
820 mat() : row_sz(0), col_sz(0) {}
826 mat(
unsigned int n,
unsigned int k) {
835 template <
typename Matrix, enable_matrix<Matrix>>
844 template<
typename T = Type>
845 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
850 for (
const auto& row :
rows) {
852 for (
const auto& x : row) {
857 else if (row.size() != col_sz) {
864 j = (
j + 1) % col_sz;
875 template<
typename Matrix>
890 const unsigned int m =
min(
n,
k);
892 for (
unsigned int i = 0; i <
m; ++i)
893 get(i, i) = diagonal;
904 template<
typename Vector, enable_vector<Vector> = true>
909 if(v.size() !=
size()) {
915 for (
size_t i = 0; i <
rows(); i++)
916 for (
size_t j = 0;
j <
cols();
j++)
938 template<
typename Matrix>
950 template<
typename Matrix>
983 template<
typename VecType,
unsigned int M>
986 return algebra::vec_mat_mul<vec<VecType>>(v, A);
1011 template<
typename Vector, enable_vector<Vector> = true>
1014 if(v.size() !=
rows()) {
1029 template<
unsigned int N = 0,
unsigned int K = 0>
1043 template<
unsigned int N = 0,
unsigned int K = 0>
1092 template<
typename Matrix, enable_matrix<Matrix> = true>
1098 if(B.rows() !=
cols()) {
1114 template<
typename Matrix>
1127 template<
typename Matrix>
1140 template<
typename Matrix>
1181 template<
typename Matrix>
1183 return (*
this = this->
operator*(B));
1194 for (
size_t i = 0; i <
rows(); i++)
1195 for (
size_t j = 0;
j <
cols();
j++)
1198 return (*
this =
res);
1209#ifdef THEORETICA_ROW_FIRST
1210 return elements[
j + i * col_sz];
1212 return elements[i +
j * row_sz];
1222 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
1224#ifdef THEORETICA_ROW_FIRST
1225 return elements[
j + i * col_sz];
1227 return elements[i +
j * row_sz];
1241 throw std::out_of_range(
1242 "The provided row index in mat::at() is out of range"
1247 throw std::out_of_range(
1248 "The provided column index in mat::at() is out of range"
1262 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1265 throw std::out_of_range(
1266 "The provided row index in mat::at() is out of range"
1271 throw std::out_of_range(
1272 "The provided column index in mat::at() is out of range"
1376 return elements.data();
1382 return elements.data();
1390 template<
typename Vector = vec<Type>>
1401 for (
size_t i = 0; i <
rows(); i++)
1402 for (
size_t j = 0;
j <
cols();
j++)
1415 template<
typename Matrix>
1427 template<
typename Matrix>
1448 if (row_sz ==
rows && col_sz ==
cols)
1453 if (elements.size()) {
1470#ifndef THEORETICA_NO_PRINT
1476 std::stringstream
res;
1478 for (
unsigned int i = 0; i <
rows(); ++i) {
1483 for (
unsigned int j = 0;
j <
cols(); ++
j) {
1495 res <<
")" << std::endl;
1503 inline operator std::string() {
1511 return out <<
obj.to_string();
friend std::ostream & operator<<(std::ostream &out, const mat< Type > &obj)
Stream the matrix in string representation to an output stream (std::ostream)
Definition mat.h:1509
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:951
Vector unpack() const
Unpack the matrix elements into a vector.
Definition mat.h:1391
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1115
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:826
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1437
mat(const Vector &v, unsigned int row_size, unsigned int col_size)
Constructor that initializes a matrix from a vector in row-major order.
Definition mat.h:905
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:993
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1416
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1163
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1128
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:886
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:1030
Type * data()
Get a raw pointer to the underlying elements in memory.
Definition mat.h:1375
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1238
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:972
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1428
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1182
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1262
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:845
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:836
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1445
std::vector< Type > elements
Dynamically allocated array of the elements.
Definition mat.h:810
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1348
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1362
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1355
friend vec< VecType > operator*(const vec< VecType, M > &v, const mat< Type, 0, 0 > &A)
Friend operator to enable equations of the form (vec) * (mat) (Enables vector-matrix multiplication....
Definition mat.h:984
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:1012
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1369
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:1044
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1207
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:961
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1328
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1152
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1317
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:1093
mat()
Default constructor.
Definition mat.h:820
const Type * data() const
Get a raw pointer to the underlying elements in memory.
Definition mat.h:1381
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1189
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1473
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1141
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:929
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:939
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:876
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:1066
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1222
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1338
Type & operator()(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column using the function call operator.
Definition mat.h:1287
~mat()
Destructor that resets the matrix size.
Definition mat.h:922
const Type & operator()(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column using the function call operator.
Definition mat.h:1299
A sequential iterator for matrices.
Definition mat.h:34
mat_iterator & operator++()
Advances the iterator to the next element in row-major order.
Definition mat.h:84
mat_iterator(Matrix &matrix, size_t row=0, size_t col=0)
Constructs an iterator for a matrix, optionally starting at a specified row and column.
Definition mat.h:63
size_t col_index()
Retrieves the current column index of the iterator.
Definition mat.h:106
bool operator!=(const mat_iterator &other) const
Inequality operator to compare two iterators.
Definition mat.h:123
ReturnType operator*()
Dereferences the iterator to access the current matrix element.
Definition mat.h:74
bool operator==(const mat_iterator &other) const
Equality operator to compare two iterators.
Definition mat.h:114
size_t row_index()
Retrieves the current row index of the iterator.
Definition mat.h:99
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
mat< Type, N, K > operator+(const Matrix &other) const
Matrix addition.
Definition mat.h:283
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition mat.h:273
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition mat.h:394
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:750
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:303
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:529
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructs a matrix from an initializer list.
Definition mat.h:172
Vector unpack() const
Unpack the matrix elements into a vector.
Definition mat.h:677
bool operator!=(const Matrix &other) const
Checks whether this matrix is not equal to another matrix element-wise.
Definition mat.h:710
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:294
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:634
friend vec< VecType, K > operator*(const vec< VecType, M > &v, const mat< Type, N, K > &A)
Friend operator for vector-matrix multiplication.
Definition mat.h:325
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:604
friend std::ostream & operator<<(std::ostream &out, const mat< Type, N, K > &obj)
Outputs the matrix to an output stream in string format.
Definition mat.h:789
mat(const Vector &v, unsigned int row_size=N, unsigned int col_size=K)
Constructor that initializes a matrix from a vector in row-major order.
Definition mat.h:248
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:719
Type * data()
Get a raw pointer to the underlying elements in memory.
Definition mat.h:661
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:374
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:408
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:357
mat(Type diagonal)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:230
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:476
const Type * data() const
Get a raw pointer to the underlying elements in memory.
Definition mat.h:667
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:492
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:502
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:641
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:466
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column with bound checking.
Definition mat.h:544
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:608
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:655
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:514
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:648
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:428
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:458
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:620
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:313
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:627
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:448
vec< Type, N > operator*(const vec< Type, K > &v) const
Overloads the * operator to transform a fixed-size vector by the matrix.
Definition mat.h:384
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:209
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:336
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:700
const Type & at(unsigned int i, unsigned int j) const
Accesses the element at the given row and column with bound checking.
Definition mat.h:567
mat< Type, N, K > resize(unsigned int n, unsigned int k)
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:730
mat(const Matrix &m)
Copy constructor.
Definition mat.h:160
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element by value.
Definition mat.h:598
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element by reference.
Definition mat.h:589
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:613
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:170
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Matrix2 & mat_sum(Matrix1 &A, const Matrix2 &B)
Sum two matrices and store the result in the first matrix.
Definition algebra.h:858
Matrix & mat_scalmul(Field a, Matrix &m)
Multiply a matrix by a scalar of any compatible type.
Definition algebra.h:685
Matrix & invert(Matrix &m)
Invert the given matrix and overwrite it.
Definition algebra.h:2122
Matrix2 & mat_diff(Matrix1 &A, const Matrix2 &B)
Subtract two matrices and store the result in the first matrix.
Definition algebra.h:920
Vector transform(const Matrix &A, const Vector &v)
Returns the matrix transformation of a vector.
Definition algebra.h:799
Matrix & mat_error(Matrix &m)
Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywher...
Definition algebra.h:40
Matrix3 mat_mul(const Matrix1 &A, const Matrix2 &B)
Multiply two matrices and store the result in the first matrix, equivalent to the operation .
Definition algebra.h:1054
Matrix & mat_zeroes(Matrix &m)
Overwrite a matrix with all zeroes.
Definition algebra.h:181
Matrix1 & mat_copy(Matrix1 &dest, const Matrix2 &src)
Copy a matrix by overwriting another.
Definition algebra.h:213
Matrix & make_transposed(Matrix &m)
Transpose the given matrix.
Definition algebra.h:521
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
bool mat_equals(const Matrix1 &A, const Matrix2 &B, real tolerance=ALGEBRA_ELEMENT_TOL)
Checks whether two matrices are equal.
Definition algebra.h:1182
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:347
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
@ InvalidArgument
Invalid argument.
@ ImpossibleOperation
Mathematically impossible operation.
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
Vector class and operations.