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>
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) {
209 mat(Type diagonal,
unsigned int n = 0,
unsigned int k = 0) {
215 const unsigned int m =
min(n, k);
217 for (
unsigned int i = 0; i < m; ++i)
218 get(i, i) = diagonal;
226 template<
typename Matrix>
227 inline mat<Type, N, K>&
operator=(
const Matrix& other) {
236 template<
typename Matrix>
237 inline mat<Type, N, K>
operator+(
const Matrix& other)
const {
247 template<
typename Matrix>
248 inline mat<Type, N, K>
operator-(
const Matrix& other)
const {
267 inline friend mat<Type, N, K>
operator*(Type a,
const mat<Type, N, K>& B) {
278 template<
typename VecType,
unsigned int M>
280 const vec<VecType, M>& a,
const mat<Type, N, K>& B) {
310 template<
typename Vector>
313 if(v.size() !=
cols()) {
328 inline vec<Type, N>
transform(
const vec<Type, K>& v)
const {
336 inline vec<Type, N>
operator*(
const vec<Type, K>& v)
const {
345 template<
unsigned int M>
346 inline mat<Type, N, M>
mul(
const mat<Type, K, M>& B)
const {
359 template<
typename Matrix>
360 inline Matrix
mul(
const Matrix& B)
const {
363 res.resize(N, B.cols());
379 template<
typename Matrix>
383 res.resize(N, B.cols());
399 template<
typename Matrix>
409 template<
typename Matrix>
443 template<
typename Matrix>
445 return (*
this = this->
operator*(B));
456 N == K,
"The matrix must be square to be transposed in place.");
466 inline Type&
get(
unsigned int i,
unsigned int j) {
468#ifdef THEORETICA_ROW_FIRST
481 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
483#ifdef THEORETICA_ROW_FIRST
496 inline Type&
at(
unsigned int i,
unsigned int j) {
499 throw std::out_of_range(
500 "The provided row index in mat::at() is out of range"
505 throw std::out_of_range(
506 "The provided column index in mat::at() is out of range"
519 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
522 throw std::out_of_range(
523 "The provided row index in mat::at() is out of range"
528 throw std::out_of_range(
529 "The provided column index in mat::at() is out of range"
550 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
556 using iterator = mat_iterator<mat<Type, N, K>, Type&>;
607 inline unsigned int size()
const {
616 template<
typename Matrix>
626 template<
typename Matrix>
637 static_assert(N == K,
"The matrix must be square to be invertible.");
647 inline mat<Type, N, K>
resize(
unsigned int n,
unsigned int k)
const {
651 }
else if(
cols() != k) {
659#ifndef THEORETICA_NO_PRINT
666 std::string separator =
", ",
bool parenthesis =
true)
const {
668 std::stringstream res;
670 for (
unsigned int i = 0; i <
rows(); ++i) {
675 for (
unsigned int j = 0; j <
cols(); ++j) {
687 res <<
")" << std::endl;
695 inline operator std::string() {
705 std::ostream& out,
const mat<Type, N, K>& obj) {
706 return out << obj.to_string();
720 template<
typename Type>
735 mat() : row_sz(0), col_sz(0) {}
741 mat(
unsigned int n,
unsigned int k) {
751 typename Matrix, enable_matrix<Matrix>
754 resize(m.rows(), m.cols());
762 template<
typename T = Type>
763 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
768 for (
const auto& row :
rows) {
770 for (
const auto& x : row) {
775 else if (row.size() != col_sz) {
782 j = (j + 1) % col_sz;
793 template<
typename Matrix>
795 resize(other.rows(), other.cols());
804 mat(Type diagonal,
unsigned int n,
unsigned int k) {
808 const unsigned int m =
min(n, k);
810 for (
unsigned int i = 0; i < m; ++i)
811 get(i, i) = diagonal;
832 template<
typename Matrix>
844 template<
typename Matrix>
866 inline friend mat<Type>
operator*(Type a,
const mat<Type>& B) {
877 template<
typename VecType,
unsigned int M>
879 const vec<VecType, M>& a,
const mat<Type, 0, 0>& B) {
905 template<
typename Vector>
908 if(v.size() !=
rows()) {
923 template<
unsigned int N = 0,
unsigned int K = 0>
924 inline vec<Type, N>
transform(
const vec<Type, K>& v)
const {
933 template<
unsigned int N = 0,
unsigned int K = 0>
934 inline vec<Type, N>
operator*(
const vec<Type, K>& v)
const {
956 inline mat<Type>
mul(
const mat<Type>& B)
const {
982 template<
typename Matrix>
983 inline Matrix
mul(
const Matrix& B)
const {
986 res.resize(
rows(), B.cols());
988 if(B.rows() !=
cols()) {
1004 template<
typename Matrix>
1017 template<
typename Matrix>
1030 template<
typename Matrix>
1071 template<
typename Matrix>
1073 return (*
this = this->
operator*(B));
1093 inline Type&
get(
unsigned int i,
unsigned int j) {
1095#ifdef THEORETICA_ROW_FIRST
1096 return data[j + i * row_sz];
1098 return data[i + j * col_sz];
1108 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
1110#ifdef THEORETICA_ROW_FIRST
1111 return data[j + i * row_sz];
1113 return data[i + j * col_sz];
1124 inline Type&
at(
unsigned int i,
unsigned int j) {
1127 throw std::out_of_range(
1128 "The provided row index in mat::at() is out of range"
1133 throw std::out_of_range(
1134 "The provided column index in mat::at() is out of range"
1148 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1151 throw std::out_of_range(
1152 "The provided row index in mat::at() is out of range"
1157 throw std::out_of_range(
1158 "The provided column index in mat::at() is out of range"
1185 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
1266 template<
typename Matrix>
1278 template<
typename Matrix>
1299 if (row_sz ==
rows && col_sz ==
cols)
1302 std::vector<Type> new_data (
rows *
cols);
1309 for (
unsigned int i = 0; i < min_elements; ++i)
1310 new_data[i] = data[i];
1321#ifndef THEORETICA_NO_PRINT
1325 std::string separator =
", ",
bool parenthesis =
true)
const {
1327 std::stringstream res;
1329 for (
unsigned int i = 0; i <
rows(); ++i) {
1334 for (
unsigned int j = 0; j <
cols(); ++j) {
1346 res <<
")" << std::endl;
1354 inline operator std::string() {
1361 std::ostream& out,
const mat<Type>& obj) {
1362 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:1360
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1191
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:845
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1005
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:741
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1288
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:887
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1267
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1053
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1018
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:804
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:924
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1124
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:866
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1279
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1072
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1195
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1148
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:763
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:753
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1296
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1234
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1248
friend vec< VecType, 0 > operator*(const vec< VecType, M > &a, const mat< Type, 0, 0 > &B)
Friend operator to enable equations of the form (vec) * (mat) (Enables vector-matrix multiplication....
Definition mat.h:878
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1241
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:906
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1255
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:934
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1093
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:855
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1214
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1042
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1203
mat()
Default constructor.
Definition mat.h:735
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1083
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1324
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1031
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:823
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:833
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:794
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:725
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:956
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:983
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1108
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1224
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:1173
~mat()
Destructor that resets the matrix size.
Definition mat.h:816
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:1185
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:237
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition mat.h:227
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition mat.h:346
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:665
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:257
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:481
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructs a matrix from an initializer list.
Definition mat.h:172
bool operator!=(const Matrix &other) const
Checks whether this matrix is not equal to another matrix element-wise.
Definition mat.h:627
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:248
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:586
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:556
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:704
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:636
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:328
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:428
mat< Type, N, K > resize(unsigned int n, unsigned int k) const
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:647
friend vec< VecType, K > operator*(const vec< VecType, M > &a, const mat< Type, N, K > &B)
Friend operator for vector-matrix multiplication.
Definition mat.h:279
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:444
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:454
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:593
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:418
mat(Type diagonal, unsigned int n=0, unsigned int k=0)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:209
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column with bound checking.
Definition mat.h:496
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:560
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:607
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:466
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:600
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:410
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:572
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:267
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:579
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:400
mat(const Matrix &m)
Copy constructor.
Definition mat.h:160
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:336
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:380
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:311
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:360
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:290
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:617
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:519
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element by value.
Definition mat.h:550
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element by reference.
Definition mat.h:541
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:565
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:161
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:225
Matrix2 & mat_sum(Matrix1 &A, const Matrix2 &B)
Sum two matrices and store the result in the first matrix.
Definition algebra.h:764
Matrix & mat_scalmul(Field a, Matrix &m)
Multiply a matrix by a scalar of any compatible type.
Definition algebra.h:590
Matrix & invert(Matrix &m)
Invert the given matrix and overwrite it.
Definition algebra.h:1997
Matrix2 & mat_diff(Matrix1 &A, const Matrix2 &B)
Subtract two matrices and store the result in the first matrix.
Definition algebra.h:832
bool mat_equals(const Matrix1 &A, const Matrix2 &B, real tolerance=10 *MACH_EPSILON)
Checks whether two matrices are equal.
Definition algebra.h:1109
Vector transform(const Matrix &A, const Vector &v)
Returns the matrix transformation of a vector.
Definition algebra.h:702
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:44
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:977
Matrix & mat_zeroes(Matrix &m)
Overwrite a matrix with all zeroes.
Definition algebra.h:93
Matrix1 & mat_copy(Matrix1 &dest, const Matrix2 &src)
Copy a matrix by overwriting another.
Definition algebra.h:125
Matrix & make_transposed(Matrix &m)
Transpose the given matrix.
Definition algebra.h:420
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
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:351
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
Vector class and operations.