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&;
59 : matrix(matrix), row(row), col(col) {}
65 return matrix(row, col);
74 if(col == matrix.cols()) {
115 return (row == other.row) &&
120 return !(*
this == other);
131 template<
typename Type = real,
unsigned int N = 0,
unsigned int K = 0>
135 #ifdef THEORETICA_ROW_FIRST
149 template<
typename Matrix>
156 template<
typename T = Type>
157 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
159 if(
rows.size() != N) {
168 for (
const auto& row :
rows) {
170 if (row.size() != K) {
176 for (
const auto& x : row) {
188 template<
typename Matrix>
209 template<
typename Matrix>
217 template<
typename Matrix>
240 template<
typename VecType,
unsigned int M>
262 template<
typename Vector>
265 if(v.size() !=
cols()) {
290 template<
unsigned int M>
298 template<
typename Matrix>
299 inline Matrix
mul(
const Matrix& B)
const {
302 res.resize(N, B.cols());
315 template<
typename Matrix>
319 res.resize(N, B.cols());
332 template<
typename Matrix>
339 template<
typename Matrix>
364 template<
typename Matrix>
366 return (*
this = this->
operator*(B));
373 N == K,
"The matrix must be square to be transposed in place.");
386 inline Type&
at(
unsigned int i,
unsigned int j) {
388 #ifdef THEORETICA_ROW_FIRST
397 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
399 #ifdef THEORETICA_ROW_FIRST
414 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
420 inline Type
get(
unsigned int i,
unsigned int j)
const {
422 #ifdef THEORETICA_ROW_FIRST
478 inline unsigned int size()
const {
484 template<
typename Matrix>
491 template<
typename Matrix>
555 static_assert(N == K,
"The matrix must be square to compute the determinant.");
562 static_assert(N == K,
"The matrix must be square to be invertible.");
569 static_assert(N == K,
"The matrix must be square to be invertible.");
583 }
else if(
cols() != k) {
596 return algebra::identity<mat<Type, N, K>>();
607 template<
typename Vector =
vec<
real, N - 1>>
609 return algebra::translation<mat<Type, N, K>>(t);
615 static_assert(N >= 2 && K >= 2,
"The matrix must be 2x2 or bigger");
616 return algebra::rotation_2d<mat<Type, N, K>>(theta);
622 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
623 return algebra::rotation_3d_xaxis<mat<Type, N, K>>(theta);
629 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
630 return algebra::rotation_3d_yaxis<mat<Type, N, K>>(theta);
636 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
637 return algebra::rotation_3d_zaxis<mat<Type, N, K>>(theta);
642 template<
typename Vector = vec<real, 3>>
644 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
645 return algebra::rotation_3d<mat<Type, N, K>>(theta, axis);
653 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
654 return algebra::perspective<mat<Type, N, K>>(
655 left, right, bottom, top, near, far);
659 inline static mat<Type, N, K> perspective_fov(
662 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
663 return algebra::perspective_fov<mat<Type, N, K>>(fov, aspect, near, far);
667 inline static mat<Type, N, K> ortho(
669 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
670 return algebra::ortho<mat<Type, N, K>>(left, right, bottom, top, near, far);
676 template<
typename Vector1,
typename Vector2,
typename Vector3>
678 const Vector1& camera,
const Vector2& target,
const Vector3& up) {
679 return algebra::look_at<mat<Type, 4, 4>>(camera, target, up);
685 static_assert(N == K && (N % 2 == 0),
686 "N must equal K and they should be a multiple of 2");
687 return algebra::symplectic<mat<Type, N, K>>(n, k);
692 #ifndef THEORETICA_NO_PRINT
696 std::string separator =
", ",
bool parenthesis =
true)
const {
698 std::stringstream res;
700 for (
unsigned int i = 0; i <
rows(); ++i) {
705 for (
unsigned int j = 0; j <
cols(); ++j) {
717 res <<
")" << std::endl;
747 template<
typename Type>
753 using Container = std::vector<T>;
756 Container<Container<Type>>
data;
766 mat() : row_sz(0), col_sz(0) {}
770 mat(
unsigned int n,
unsigned int k) {
781 resize(m.rows(), m.cols());
787 template<
typename T = Type>
788 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
793 for (
const auto& row :
rows) {
795 for (
const auto& x : row) {
798 this->
resize(rows.size(), row.size());
800 else if (row.size() != col_sz) {
807 j = (j + 1) % col_sz;
816 template<
typename Matrix>
818 resize(other.rows(), other.cols());
829 const unsigned int m =
min(n, k);
831 for (
unsigned int i = 0; i < m; ++i)
859 template<
typename Matrix>
868 template<
typename Matrix>
893 template<
typename VecType,
unsigned int M>
916 template<
typename Vector>
919 if(v.size() !=
rows()) {
932 template<
unsigned int N = 0,
unsigned int K = 0>
939 template<
unsigned int N = 0,
unsigned int K = 0>
968 template<
typename Matrix>
969 inline Matrix
mul(
const Matrix& B)
const {
972 res.resize(
rows(), B.cols());
974 if(B.rows() !=
cols()) {
985 template<
typename Matrix>
992 template<
typename Matrix>
999 template<
typename Matrix>
1024 template<
typename Matrix>
1026 return (*
this = this->
operator*(B));
1039 return algebra::transpose<mat<Type>,
mat<Type>>(*this);
1044 inline Type&
at(
unsigned int i,
unsigned int j) {
1046 #ifdef THEORETICA_ROW_FIRST
1055 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1057 #ifdef THEORETICA_ROW_FIRST
1072 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
1078 inline Type
get(
unsigned int i,
unsigned int j)
const {
1080 #ifdef THEORETICA_ROW_FIRST
1140 template<
typename Matrix>
1147 template<
typename Matrix>
1238 #ifdef THEORETICA_ROW_FIRST
1239 size_t size1 = n, size2 = k;
1241 size_t size1 = k, size2 = n;
1248 for (
unsigned int i = 0; i < size1; ++i)
1258 std::vector<std::vector<Type>> new_data;
1259 new_data.resize(size1);
1260 for (
unsigned int i = 0; i < size1; ++i)
1261 new_data[i].
resize(size2);
1266 const size_t row_bound =
min(
rows(), n);
1267 const size_t col_bound =
min(
cols(), n);
1269 for (
unsigned int i = 0; i < row_bound; ++i) {
1270 for (
unsigned int j = 0; j < col_bound; ++j) {
1271 #ifdef THEORETICA_ROW_FIRST
1272 new_data[i][j] =
get(i, j);
1274 new_data[j][i] =
get(i, j);
1281 for (
unsigned int i = 0; i < n; ++i) {
1282 for (
unsigned int j = col_bound; j < k; ++j) {
1285 #ifdef THEORETICA_ROW_FIRST
1286 new_data[i][j] = (Type) 0;
1288 new_data[j][i] = (Type) 0;
1296 for (
unsigned int i = row_bound; i < n; ++i) {
1297 for (
unsigned int j = 0; j < k; ++j) {
1300 #ifdef THEORETICA_ROW_FIRST
1301 new_data[i][j] = (Type) 0;
1303 new_data[j][i] = (Type) 0;
1324 unsigned int row_sz,
unsigned int col_sz) {
1326 return algebra::identity<mat<Type>>(row_sz, col_sz);
1332 Type diag,
unsigned int row_sz,
unsigned int col_sz) {
1338 template<
typename Vector>
1340 return algebra::translation<mat<Type>>(t);
1346 return algebra::rotation_2d<mat<Type>>(theta);
1352 return algebra::rotation_3d_xaxis<mat<Type>>(theta);
1358 return algebra::rotation_3d_yaxis<mat<Type>>(theta);
1364 return algebra::rotation_3d_zaxis<mat<Type>>(theta);
1369 template<
typename Vector = vec<real, 3>>
1371 return algebra::rotation_3d<mat<Type>>(theta, axis);
1379 return algebra::perspective<mat<Type>>(
1380 left, right, bottom, top, near, far);
1384 inline static mat<Type> perspective_fov(
1387 return algebra::perspective_fov<mat<Type>>(fov, aspect, near, far);
1391 inline static mat<Type> ortho(
1393 return algebra::ortho<mat<Type>>(left, right, bottom, top, near, far);
1399 template<
typename Vector1,
typename Vector2,
typename Vector3>
1401 const Vector1& camera,
const Vector2& target,
const Vector3& up) {
1402 return algebra::look_at<mat<Type>>(camera, target, up);
1408 return algebra::symplectic<mat<Type>>(
rows,
cols);
1414 #ifndef THEORETICA_NO_PRINT
1418 std::string separator =
", ",
bool parenthesis =
true)
const {
1420 std::stringstream res;
1422 for (
unsigned int i = 0; i <
rows(); ++i) {
1427 for (
unsigned int j = 0; j <
cols(); ++j) {
1439 res <<
")" << std::endl;
1454 std::ostream& out,
const mat<Type>& obj) {
A generic matrix with a variable number of rows and columns.
Definition: mat.h:748
mat< Type > operator/(Type scalar) const
Scalar division.
Definition: mat.h:901
Type diagonal_product()
Compute the product of the diagonal elements of a square matrix.
Definition: mat.h:1204
static mat< Type > rotation_3d_zaxis(real theta)
Get a matrix which rotates <theta> radians around the z axis.
Definition: mat.h:1363
static mat< Type > rotation_3d_xaxis(real theta)
Get a matrix which rotates <theta> radians around the x axis.
Definition: mat.h:1351
mat< Type > & operator=(const Matrix &other)
Copy constructor.
Definition: mat.h:817
bool is_diagonal() const
Return whether the matrix is diagonal.
Definition: mat.h:1160
static mat< Type > identity(unsigned int row_sz, unsigned int col_sz)
Get the identity matrix.
Definition: mat.h:1323
mat< Type > & resize(unsigned int n, unsigned int k)
Set or change the size of the matrix.
Definition: mat.h:1230
vec< Type, N > operator*(const vec< Type, K > &v) const
Transform a vector by the matrix.
Definition: mat.h:940
mat< Type > & transpose()
Transpose the matrix itself.
Definition: mat.h:1031
vec< Type, N > transform(const vec< Type, K > &v) const
Transform a vector by the matrix.
Definition: mat.h:933
auto operator*(const Matrix &B) const
Matrix multiplication.
Definition: mat.h:986
mat(unsigned int n, unsigned int k)
Construct a matrix with n rows and k columns.
Definition: mat.h:770
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction.
Definition: mat.h:1000
static mat< Type > look_at(const Vector1 &camera, const Vector2 &target, const Vector3 &up)
Return a 4x4 transformation matrix that points the field of view towards a given point from the <came...
Definition: mat.h:1400
const Type & operator()(unsigned int i, unsigned int j) const
Get the element at the i-th row and j-th column.
Definition: mat.h:1072
bool operator==(const Matrix &other) const
Check whether two matrices are equal element by element.
Definition: mat.h:1141
static mat< Type > rotation_3d_yaxis(real theta)
Get a matrix which rotates <theta> radians around the y axis.
Definition: mat.h:1357
mat(Type diagonal, unsigned int n, unsigned int k)
Construct a diagonal matrix with all equal entries with n rows and k columns.
Definition: mat.h:825
bool is_symmetric() const
Return whether the matrix is symmetric.
Definition: mat.h:1166
bool operator!=(const Matrix &other) const
Check whether two matrices are unequal element by element.
Definition: mat.h:1148
Type trace()
Compute the trace (sum of elements on the diagonal) of a square matrix.
Definition: mat.h:1198
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)
Definition: mat.h:894
static mat< Type > rotation_3d(real theta, Vector &&axis)
Get a matrix which rotates <theta> radians around the <axis> axis.
Definition: mat.h:1370
bool is_square() const
Return whether the matrix is square.
Definition: mat.h:1154
mat(const Matrix &m)
Copy constructor.
Definition: mat.h:780
Type & operator()(unsigned int i, unsigned int j)
Access the element at the i-th row and j-th column.
Definition: mat.h:1066
static mat< Type > zeroes(unsigned int rows, unsigned int cols)
Get the null matrix.
Definition: mat.h:850
mat(const std::initializer_list< std::initializer_list< T >> &rows)
Construct from a list of the rows.
Definition: mat.h:788
static mat< Type > translation(Vector &&t)
Get a 4x4 matrix which translates by {x, y, z}.
Definition: mat.h:1339
auto end() const
Get a const iterator to one plus the last element of the matrix.
Definition: mat.h:1115
TH_CONSTEXPR unsigned int cols() const
Get the number of columns of the matrix.
Definition: mat.h:1127
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication.
Definition: mat.h:1025
TH_CONSTEXPR unsigned int rows() const
Get the number of rows of the matrix.
Definition: mat.h:1121
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition: mat.h:952
mat< Type > operator*(Type scalar) const
Scalar multiplication.
Definition: mat.h:877
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:1453
Container< Container< Type > > data
Dynamically allocated array of the elements.
Definition: mat.h:756
Vector transform(const Vector &v) const
Transform a vector v by the matrix.
Definition: mat.h:917
unsigned int size() const
Get the total number of elements of the matrix (rows * columns)
Definition: mat.h:1134
mat< Type > operator+(const Matrix &other) const
Matrix addition.
Definition: mat.h:860
mat< Type > & invert()
Invert a generic square matrix.
Definition: mat.h:1222
auto end()
Get an iterator to one plus the last element of the matrix.
Definition: mat.h:1103
static mat< Type > rotation_2d(real theta)
Get a matrix which rotates the 2D plane of <theta> radians.
Definition: mat.h:1345
Type & at(unsigned int i, unsigned int j)
Access the element at the i-th row and j-th column.
Definition: mat.h:1044
static mat< Type > diagonal(Type diag, unsigned int row_sz, unsigned int col_sz)
Get a diagonal matrix.
Definition: mat.h:1331
const Type & at(unsigned int i, unsigned int j) const
Access the element at the i-th row and j-th column.
Definition: mat.h:1055
auto begin()
Get an iterator to the first element of the matrix.
Definition: mat.h:1097
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition: mat.h:886
mat< Type > & operator+=(const Matrix &other)
Matrix addition.
Definition: mat.h:993
mat()
Default constructor.
Definition: mat.h:766
mat< Type > & operator*=(Type scalar)
Scalar multiplication.
Definition: mat.h:1006
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition: mat.h:1417
void make_zeroes()
Set all elements to zero.
Definition: mat.h:844
static mat< Type > symplectic(unsigned int rows, unsigned int cols)
A symplectic NxN matrix, where for some natural K.
Definition: mat.h:1407
mat< Type > inverse() const
Compute the inverse of a generic square matrix.
Definition: mat.h:1216
mat< Type > & operator/=(Type scalar)
Scalar division.
Definition: mat.h:1012
mat< Type > operator-(const Matrix &other) const
Matrix subtraction.
Definition: mat.h:869
real sparsity(real tolerance=1E-12)
Compute the sparsity of the matrix, counting the proportion of zero (smaller in module than the given...
Definition: mat.h:1192
mat< Type > transposed() const
Return the transposed matrix, without modifying the matrix itself.
Definition: mat.h:1038
Matrix mul(const Matrix &B) const
Matrix multiplication by any matrix type.
Definition: mat.h:969
auto begin() const
Get a const iterator to the first element of the matrix.
Definition: mat.h:1109
~mat()
Deallocate memory.
Definition: mat.h:837
Type det() const
Compute the determinant of the matrix.
Definition: mat.h:1210
Type get(unsigned int i, unsigned int j) const
Get the element at the i-th row and j-th column.
Definition: mat.h:1078
real density(real tolerance=1E-12)
Compute the density of the matrix, counting the proportion of non-zero (bigger in module than the giv...
Definition: mat.h:1179
A sequential iterator for matrices.
Definition: mat.h:34
mat_iterator(Matrix &matrix, size_t row=0, size_t col=0)
Construct iterator from a matrix.
Definition: mat.h:55
size_t col_index()
Get the index of the current column.
Definition: mat.h:90
ReturnType operator*()
Dereference the iterator to get the current element by reference.
Definition: mat.h:64
bool operator==(const mat_iterator &other) const
Move to the previous element in the matrix.
Definition: mat.h:114
mat_iterator & operator++()
Move to the next element in the matrix.
Definition: mat.h:70
size_t row_index()
Get the index of the current row.
Definition: mat.h:84
A generic matrix with a fixed number of rows and columns.
Definition: mat.h:132
static mat< Type, N, K > rotation_3d_zaxis(real theta)
Get a matrix which rotates <theta> radians around the z axis.
Definition: mat.h:635
static mat< Type, N, K > rotation_2d(real theta)
Get a matrix which rotates the 2D plane of <theta> radians.
Definition: mat.h:614
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition: mat.h:695
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:579
bool is_symmetric() const
Return whether the matrix is symmetric.
Definition: mat.h:510
mat< Type, N, K > & invert()
Invert a generic square matrix.
Definition: mat.h:568
bool operator!=(const Matrix &other) const
Check whether two matrices are unequal element by element.
Definition: mat.h:492
static mat< Type, 4, 4 > look_at(const Vector1 &camera, const Vector2 &target, const Vector3 &up)
Return a 4x4 transformation matrix that points the field of view towards a given point from the <came...
Definition: mat.h:677
Type & operator()(unsigned int i, unsigned int j)
Access the element at the i-th row and j-th column.
Definition: mat.h:408
static mat< Type, N, K > zeroes()
Get the null matrix.
Definition: mat.h:201
bool is_diagonal() const
Return whether the matrix is diagonal.
Definition: mat.h:504
void make_zeroes()
Set all elements to zero.
Definition: mat.h:195
auto end() const
Get a const iterator to one plus the last element of the matrix.
Definition: mat.h:459
mat< Type, N, K > inverse() const
Compute the inverse of a generic square matrix.
Definition: mat.h:561
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition: mat.h:431
friend std::ostream & operator<<(std::ostream &out, const mat< Type, N, K > &obj)
Stream the matrix in string representation to an output stream (std::ostream)
Definition: mat.h:731
Type get(unsigned int i, unsigned int j) const
Get the element at the i-th row and j-th column.
Definition: mat.h:420
Type trace()
Compute the trace (sum of elements on the diagonal) of a square matrix.
Definition: mat.h:542
static mat< Type, N, K > translation(Vector &&t)
Get a 4x4 matrix which translates by {x, y, z}.
Definition: mat.h:608
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition: mat.h:333
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition: mat.h:248
mat(const std::initializer_list< std::initializer_list< T >> &rows)
Construct from a list of the rows.
Definition: mat.h:157
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication.
Definition: mat.h:291
bool is_square() const
Return whether the matrix is square.
Definition: mat.h:498
friend vec< VecType, K > operator*(const vec< VecType, M > &a, const mat< Type, N, K > &B)
Friend operator to enable equations of the form (vec) * (mat)
Definition: mat.h:241
mat< Type, N, K > & operator=(const Matrix &other)
Copy constructor.
Definition: mat.h:189
Type diagonal_product()
Compute the product of the diagonal elements of a square matrix.
Definition: mat.h:548
TH_CONSTEXPR unsigned int rows() const
Get the number of rows of the matrix.
Definition: mat.h:465
const Type & at(unsigned int i, unsigned int j) const
Access the element at the i-th row and j-th column.
Definition: mat.h:397
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition: mat.h:435
unsigned int size() const
Get the total number of elements of the matrix (rows * columns)
Definition: mat.h:478
TH_CONSTEXPR unsigned int cols() const
Get the number of columns of the matrix.
Definition: mat.h:471
static mat< Type, N, K > rotation_3d(real theta, Vector &&axis)
Get a matrix which rotates <theta> radians around the <axis> axis.
Definition: mat.h:643
mat()
Default constructor.
Definition: mat.h:143
auto end()
Get an iterator to one plus the last element of the matrix.
Definition: mat.h:445
const Type & operator()(unsigned int i, unsigned int j) const
Get the element at the i-th row and j-th column.
Definition: mat.h:414
auto begin() const
Get a const iterator to the first element of the matrix.
Definition: mat.h:452
real density(real tolerance=1E-12)
Compute the density of the matrix, counting the proportion of non-zero (bigger in module than the giv...
Definition: mat.h:523
static mat< Type, N, K > rotation_3d_yaxis(real theta)
Get a matrix which rotates <theta> radians around the y axis.
Definition: mat.h:628
mat(const Matrix &m)
Copy constructor.
Definition: mat.h:150
static mat< Type, N, K > rotation_3d_xaxis(real theta)
Get a matrix which rotates <theta> radians around the x axis.
Definition: mat.h:621
auto operator*(const Matrix &B) const
Matrix multiplication.
Definition: mat.h:316
Vector transform(const Vector &v) const
Transform a vector v by the matrix.
Definition: mat.h:263
Matrix mul(const Matrix &B) const
Matrix multiplication by any matrix type.
Definition: mat.h:299
bool operator==(const Matrix &other) const
Check whether two matrices are equal element by element.
Definition: mat.h:485
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition: mat.h:346
Type & at(unsigned int i, unsigned int j)
Access the element at the i-th row and j-th column.
Definition: mat.h:386
real sparsity(real tolerance=1E-12)
Compute the sparsity of the matrix, counting the proportion of zero (smaller in module than the given...
Definition: mat.h:536
static mat< Type, N, K > diagonal(Type diag)
Get a diagonal matrix.
Definition: mat.h:601
mat< Type, K, N > transposed() const
Return the transposed matrix, without modifying the matrix itself.
Definition: mat.h:380
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition: mat.h:352
Type det() const
Compute the determinant of the matrix.
Definition: mat.h:554
static mat< Type, N, K > symplectic(unsigned int n=0, unsigned int k=0)
A symplectic NxN matrix, where for some natural K.
Definition: mat.h:684
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication.
Definition: mat.h:365
static mat< Type, N, K > identity()
Get the identity matrix.
Definition: mat.h:595
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition: mat.h:340
vec< Type, N > transform(const vec< Type, K > &v) const
Transform a vector by the matrix.
Definition: mat.h:278
mat< Type, N, K > operator+(const Matrix &other) const
Matrix addition.
Definition: mat.h:210
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition: mat.h:225
mat< Type, N, K > & transpose()
Transpose the matrix itself.
Definition: mat.h:371
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition: mat.h:233
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition: mat.h:218
vec< Type, N > operator*(const vec< Type, K > &v) const
Transform a vector by the matrix.
Definition: mat.h:284
auto begin()
Get an iterator to the first element of the matrix.
Definition: mat.h:439
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition: constants.h:151
#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
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
Matrix & mat_zeroes(Matrix &m)
Overwrite a matrix with all zeroes.
Definition: algebra.h:93
Matrix & make_transposed(Matrix &m)
Transpose the given matrix.
Definition: algebra.h:420
bool is_square(const Matrix &m)
Returns whether the matrix is square.
Definition: algebra.h:1232
auto trace(const Matrix &m)
Compute the trace of the given matrix.
Definition: algebra.h:555
bool is_symmetric(const Matrix &m, real tolerance=ALGEBRA_ELEMENT_TOL)
Returns whether the matrix is symmetric.
Definition: algebra.h:1259
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 & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition: algebra.h:62
Matrix & mat_scalmul(Field a, Matrix &m)
Multiply a matrix by a scalar of any compatible type.
Definition: algebra.h:590
Vector transform(const Matrix &A, const Vector &v)
Returns the matrix transformation of a vector.
Definition: algebra.h:702
bool is_diagonal(const Matrix &m, real tolerance=10 *MACH_EPSILON)
Returns whether the matrix is diagonal.
Definition: algebra.h:1243
real density(const Matrix &A, real tolerance=1E-12)
Compute the density of a matrix, counting the proportion of non-zero (bigger in module than the given...
Definition: algebra.h:1323
Matrix2 & mat_sum(Matrix1 &A, const Matrix2 &B)
Sum two matrices and store the result in the first matrix.
Definition: algebra.h:764
real sparsity(const Matrix &A, real tolerance=1E-12)
Compute the sparsity of a matrix, counting the proportion of zero (smaller in module than the given t...
Definition: algebra.h:1346
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
Matrix1 & mat_copy(Matrix1 &dest, const Matrix2 &src)
Copy a matrix by overwriting another.
Definition: algebra.h:125
auto diagonal_product(const Matrix &m)
Compute the product of the elements of the main diagonal of a generic matrix.
Definition: algebra.h:573
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
auto det(const Matrix &A)
Compute the determinant of a square matrix.
Definition: algebra.h:2024
Matrix & invert(Matrix &m)
Invert the given matrix and overwrite it.
Definition: algebra.h:1997
Matrix1 & inverse(Matrix1 &dest, const Matrix2 &src)
Invert the given matrix.
Definition: algebra.h:1883
Matrix2 & mat_diff(Matrix1 &A, const Matrix2 &B)
Subtract two matrices and store the result in the first matrix.
Definition: algebra.h:832
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
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:183
std::remove_reference_t< decltype(std::declval< Structure >()(0, 0))> matrix_element_t
Extract the type of a matrix (or any doubly indexable container) from its operator().
Definition: core_traits.h:140
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
constexpr real E
The Euler mathematical constant (e)
Definition: constants.h:227
std::enable_if_t< is_matrix< Structure >::value, T > enable_matrix
Enable a function overload if the template typename is considerable a matrix.
Definition: core_traits.h:160
Vector class and operations.