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;
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)
226 template<
typename Matrix>
242 template<
typename Matrix>
253 template<
typename Matrix>
284 template<
typename VecType,
unsigned int M>
316 template<
typename Vector>
319 if(v.size() !=
cols()) {
351 template<
unsigned int M>
365 template<
typename Matrix>
369 res.resize(N, B.cols());
385 template<
typename Matrix>
389 res.resize(N, B.cols());
405 template<
typename Matrix>
415 template<
typename Matrix>
449 template<
typename Matrix>
451 return (*
this = this->
operator*(B));
462 N ==
K,
"The matrix must be square to be transposed in place.");
471 inline Type&
at(
unsigned int i,
unsigned int j) {
473#ifdef THEORETICA_ROW_FIRST
485 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
487#ifdef THEORETICA_ROW_FIRST
517 inline Type get(
unsigned int i,
unsigned int j)
const {
519#ifdef THEORETICA_ROW_FIRST
579 inline unsigned int size()
const {
588 template<
typename Matrix>
598 template<
typename Matrix>
609 static_assert(N ==
K,
"The matrix must be square to be invertible.");
623 }
else if(
cols() !=
k) {
631#ifndef THEORETICA_NO_PRINT
640 std::stringstream
res;
642 for (
unsigned int i = 0; i <
rows(); ++i) {
647 for (
unsigned int j = 0;
j <
cols(); ++
j) {
659 res <<
")" << std::endl;
667 inline operator std::string() {
678 return out <<
obj.to_string();
692 template<
typename Type>
707 mat() : row_sz(0), col_sz(0) {}
713 mat(
unsigned int n,
unsigned int k) {
734 template<
typename T = Type>
735 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
740 for (
const auto& row :
rows) {
742 for (
const auto& x : row) {
747 else if (row.size() != col_sz) {
754 j = (
j + 1) % col_sz;
765 template<
typename Matrix>
780 const unsigned int m =
min(
n,
k);
782 for (
unsigned int i = 0; i <
m; ++i)
804 template<
typename Matrix>
816 template<
typename Matrix>
849 template<
typename VecType,
unsigned int M>
877 template<
typename Vector>
880 if(v.size() !=
rows()) {
895 template<
unsigned int N = 0,
unsigned int K = 0>
905 template<
unsigned int N = 0,
unsigned int K = 0>
954 template<
typename Matrix>
960 if(B.rows() !=
cols()) {
976 template<
typename Matrix>
989 template<
typename Matrix>
1002 template<
typename Matrix>
1043 template<
typename Matrix>
1045 return (*
this = this->
operator*(B));
1068#ifdef THEORETICA_ROW_FIRST
1069 return data[
j + i * row_sz];
1071 return data[i +
j * col_sz];
1082 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1084#ifdef THEORETICA_ROW_FIRST
1085 return data[
j + i * row_sz];
1087 return data[i +
j * col_sz];
1122 inline Type get(
unsigned int i,
unsigned int j)
const {
1204 template<
typename Matrix>
1216 template<
typename Matrix>
1237 if (row_sz ==
rows && col_sz ==
cols)
1259#ifndef THEORETICA_NO_PRINT
1265 std::stringstream
res;
1267 for (
unsigned int i = 0; i <
rows(); ++i) {
1272 for (
unsigned int j = 0;
j <
cols(); ++
j) {
1284 res <<
")" << std::endl;
1292 inline operator std::string() {
1300 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:1298
mat_iterator< mat< Type, 0, 0 >, Type & > iterator
Iterator for dynamically allocated matrices.
Definition mat.h:1128
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition mat.h:817
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition mat.h:977
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition mat.h:713
mat< Type > & invert()
Invert a generic square matrix.
Definition mat.h:1226
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition mat.h:859
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition mat.h:1205
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition mat.h:1025
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition mat.h:990
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition mat.h:776
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:896
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition mat.h:1066
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition mat.h:838
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition mat.h:1217
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition mat.h:1044
mat_iterator< const mat< Type, 0, 0 >, const Type & > const_iterator
Const iterator for dynamically allocated matrices.
Definition mat.h:1132
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition mat.h:1082
mat(const std::initializer_list< std::initializer_list< T > > &rows)
Constructor that initializes a matrix from a list of rows.
Definition mat.h:735
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition mat.h:725
mat< Type > & resize(unsigned int rows, unsigned int cols)
Set or change the size of the matrix.
Definition mat.h:1234
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition mat.h:1171
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition mat.h:1185
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:850
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition mat.h:1178
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:878
unsigned int size() const
Get the total number of elements of the matrix (rows * columns)
Definition mat.h:1193
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:906
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition mat.h:827
auto end()
Get an iterator to one past the last element of the matrix.
Definition mat.h:1151
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition mat.h:1014
auto begin()
Get an iterator to the first element of the matrix.
Definition mat.h:1140
mat()
Default constructor.
Definition mat.h:707
mat< Type > & transpose()
Transpose the current matrix in place.
Definition mat.h:1055
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition mat.h:1262
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition mat.h:1003
void make_zeroes()
Sets all elements in the matrix to zero.
Definition mat.h:795
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition mat.h:805
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition mat.h:766
std::vector< Type > data
Dynamically allocated array of the elements.
Definition mat.h:697
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition mat.h:928
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition mat.h:955
auto begin() const
Get a const iterator to the first element of the matrix.
Definition mat.h:1161
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:1099
~mat()
Destructor that resets the matrix size.
Definition mat.h:788
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:1111
Type get(unsigned int i, unsigned int j) const
Get a copy of the element at a specific row and column.
Definition mat.h:1122
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:243
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:352
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition mat.h:637
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition mat.h:263
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:599
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition mat.h:254
void make_zeroes()
Sets all elements of the matrix to zero.
Definition mat.h:233
auto end() const
Returns a const iterator to one past the last element of the matrix.
Definition mat.h:558
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition mat.h:528
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:676
Type get(unsigned int i, unsigned int j) const
Gets the element at the specified row and column.
Definition mat.h:517
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition mat.h:608
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition mat.h:334
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition mat.h:434
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:619
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:285
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition mat.h:450
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition mat.h:460
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:565
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition mat.h:424
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.
Definition mat.h:471
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition mat.h:532
unsigned int size() const
Returns the total number of elements in the matrix.
Definition mat.h:579
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:572
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition mat.h:416
mat()
Default constructor.
Definition mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition mat.h:544
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition mat.h:273
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition mat.h:551
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition mat.h:406
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:342
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition mat.h:386
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition mat.h:317
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition mat.h:366
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition mat.h:296
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition mat.h:589
const Type & at(unsigned int i, unsigned int j) const
Accesses the element at the given row and column.
Definition mat.h:485
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element.
Definition mat.h:508
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element.
Definition mat.h:499
auto begin()
Returns an iterator to the first element of the matrix.
Definition mat.h:537
#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:219
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
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
Vector class and operations.