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,
unsigned int k) {
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;
244 template<
typename Matrix>
245 inline mat<Type, N, K>&
operator=(
const Matrix& other) {
254 template<
typename Matrix>
255 inline mat<Type, N, K>
operator+(
const Matrix& other)
const {
265 template<
typename Matrix>
266 inline mat<Type, N, K>
operator-(
const Matrix& other)
const {
285 inline friend mat<Type, N, K>
operator*(Type a,
const mat<Type, N, K>& B) {
296 template<
typename VecType,
unsigned int M>
298 const vec<VecType, M>& a,
const mat<Type, N, K>& B) {
328 template<
typename Vector>
331 if(v.size() !=
cols()) {
346 inline vec<Type, N>
transform(
const vec<Type, K>& v)
const {
354 inline vec<Type, N>
operator*(
const vec<Type, K>& v)
const {
363 template<
unsigned int M>
364 inline mat<Type, N, M>
mul(
const mat<Type, K, M>& B)
const {
377 template<
typename Matrix>
378 inline Matrix
mul(
const Matrix& B)
const {
381 res.resize(N, B.cols());
397 template<
typename Matrix>
401 res.resize(N, B.cols());
417 template<
typename Matrix>
427 template<
typename Matrix>
461 template<
typename Matrix>
463 return (*
this = this->
operator*(B));
474 N == K,
"The matrix must be square to be transposed in place.");
484 inline Type&
get(
unsigned int i,
unsigned int j) {
486#ifdef THEORETICA_ROW_FIRST
499 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
501#ifdef THEORETICA_ROW_FIRST
514 inline Type&
at(
unsigned int i,
unsigned int j) {
517 throw std::out_of_range(
518 "The provided row index in mat::at() is out of range"
523 throw std::out_of_range(
524 "The provided column index in mat::at() is out of range"
537 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
540 throw std::out_of_range(
541 "The provided row index in mat::at() is out of range"
546 throw std::out_of_range(
547 "The provided column index in mat::at() is out of range"
568 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
574 using iterator = mat_iterator<mat<Type, N, K>, Type&>;
625 inline unsigned int size()
const {
634 template<
typename Matrix>
644 template<
typename Matrix>
655 static_assert(N == K,
"The matrix must be square to be invertible.");
665 inline mat<Type, N, K>
resize(
unsigned int n,
unsigned int k)
const {
669 }
else if(
cols() != k) {
677#ifndef THEORETICA_NO_PRINT
684 std::string separator =
", ",
bool parenthesis =
true)
const {
686 std::stringstream res;
688 for (
unsigned int i = 0; i <
rows(); ++i) {
693 for (
unsigned int j = 0; j <
cols(); ++j) {
705 res <<
")" << std::endl;
713 inline operator std::string() {
723 std::ostream& out,
const mat<Type, N, K>& obj) {
724 return out << obj.to_string();
738 template<
typename Type>
753 mat() : row_sz(0), col_sz(0) {}
759 mat(
unsigned int n,
unsigned int k) {
769 typename Matrix, enable_matrix<Matrix>
772 resize(m.rows(), m.cols());
780 template<
typename T = Type>
781 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
786 for (
const auto& row :
rows) {
788 for (
const auto& x : row) {
793 else if (row.size() != col_sz) {
800 j = (j + 1) % col_sz;
811 template<
typename Matrix>
813 resize(other.rows(), other.cols());
822 mat(Type diagonal,
unsigned int n,
unsigned int k) {
826 const unsigned int m =
min(n, k);
828 for (
unsigned int i = 0; i < m; ++i)
829 get(i, i) = diagonal;
850 template<
typename Matrix>
862 template<
typename Matrix>
884 inline friend mat<Type>
operator*(Type a,
const mat<Type>& B) {
895 template<
typename VecType,
unsigned int M>
897 const vec<VecType, M>& a,
const mat<Type, 0, 0>& B) {
923 template<
typename Vector>
926 if(v.size() !=
rows()) {
941 template<
unsigned int N = 0,
unsigned int K = 0>
942 inline vec<Type, N>
transform(
const vec<Type, K>& v)
const {
951 template<
unsigned int N = 0,
unsigned int K = 0>
952 inline vec<Type, N>
operator*(
const vec<Type, K>& v)
const {
974 inline mat<Type>
mul(
const mat<Type>& B)
const {
1000 template<
typename Matrix>
1001 inline Matrix
mul(
const Matrix& B)
const {
1004 res.resize(
rows(), B.cols());
1006 if(B.rows() !=
cols()) {
1022 template<
typename Matrix>
1035 template<
typename Matrix>
1048 template<
typename Matrix>
1089 template<
typename Matrix>
1091 return (*
this = this->
operator*(B));
1111 inline Type&
get(
unsigned int i,
unsigned int j) {
1113#ifdef THEORETICA_ROW_FIRST
1114 return data[j + i * row_sz];
1116 return data[i + j * col_sz];
1126 inline const Type&
get(
unsigned int i,
unsigned int j)
const {
1128#ifdef THEORETICA_ROW_FIRST
1129 return data[j + i * row_sz];
1131 return data[i + j * col_sz];
1142 inline Type&
at(
unsigned int i,
unsigned int j) {
1145 throw std::out_of_range(
1146 "The provided row index in mat::at() is out of range"
1151 throw std::out_of_range(
1152 "The provided column index in mat::at() is out of range"
1166 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1169 throw std::out_of_range(
1170 "The provided row index in mat::at() is out of range"
1175 throw std::out_of_range(
1176 "The provided column index in mat::at() is out of range"
1203 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
1284 template<
typename Matrix>
1296 template<
typename Matrix>
1317 if (row_sz ==
rows && col_sz ==
cols)
1320 std::vector<Type> new_data (
rows *
cols);
1327 for (
unsigned int i = 0; i < min_elements; ++i)
1328 new_data[i] = data[i];
1339#ifndef THEORETICA_NO_PRINT
1343 std::string separator =
", ",
bool parenthesis =
true)
const {
1345 std::stringstream res;
1347 for (
unsigned int i = 0; i <
rows(); ++i) {
1352 for (
unsigned int j = 0; j <
cols(); ++j) {
1364 res <<
")" << std::endl;
1372 inline operator std::string() {
1379 std::ostream& out,
const mat<Type>& obj) {
1380 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:1378
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1209
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:863
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:1023
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:759
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1306
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:905
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1285
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1071
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:1036
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:822
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:942
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1142
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:884
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1297
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1090
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1213
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1166
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:781
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:771
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1314
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1252
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1266
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:896
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1259
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:924
unsigned int size() const
Get the total number of elements of the matrix, equal to (rows * columns).
Definition mat.h:1273
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:952
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:1111
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:873
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1232
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1060
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1221
mat()
Default constructor.
Definition mat.h:753
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1101
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1342
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1049
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:841
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:851
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:812
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:743
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:974
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:1001
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:1126
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1242
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:1191
~mat()
Destructor that resets the matrix size.
Definition mat.h:834
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:1203
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:255
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition mat.h:245
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition mat.h:364
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:683
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:275
const Type & get(unsigned int i, unsigned int j) const
Get the element at the given row and column.
Definition mat.h:499
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:645
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:266
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:604
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:574
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:722
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:654
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:346
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:446
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:665
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:297
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:462
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:472
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:611
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:436
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column with bound checking.
Definition mat.h:514
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:578
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:625
Type & get(unsigned int i, unsigned int j)
Access the element at the given row and column, by reference.
Definition mat.h:484
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:618
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:428
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:590
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:285
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:597
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:418
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:354
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
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:398
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:329
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:378
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:308
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:635
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:537
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element by value.
Definition mat.h:568
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element by reference.
Definition mat.h:559
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:583
#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:238
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
@ InvalidArgument
Invalid argument.
@ ImpossibleOperation
Mathematically impossible operation.
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
Vector class and operations.