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) {
215 const unsigned int m =
min(n, k);
217 for (
unsigned int i = 0; i < m; ++i)
226 template<
typename Matrix>
251 template<
typename Matrix>
262 template<
typename Matrix>
293 template<
typename VecType,
unsigned int M>
325 template<
typename Vector>
328 if(v.size() !=
cols()) {
360 template<
unsigned int M>
374 template<
typename Matrix>
375 inline Matrix
mul(
const Matrix& B)
const {
378 res.resize(N, B.cols());
394 template<
typename Matrix>
398 res.resize(N, B.cols());
414 template<
typename Matrix>
424 template<
typename Matrix>
458 template<
typename Matrix>
460 return (*
this = this->
operator*(B));
471 N == K,
"The matrix must be square to be transposed in place.");
487 inline Type&
at(
unsigned int i,
unsigned int j) {
489 #ifdef THEORETICA_ROW_FIRST
501 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
503 #ifdef THEORETICA_ROW_FIRST
524 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
533 inline Type
get(
unsigned int i,
unsigned int j)
const {
535 #ifdef THEORETICA_ROW_FIRST
595 inline unsigned int size()
const {
604 template<
typename Matrix>
614 template<
typename Matrix>
686 static_assert(N == K,
"The matrix must be square to compute the determinant.");
696 static_assert(N == K,
"The matrix must be square to be invertible.");
706 static_assert(N == K,
"The matrix must be square to be invertible.");
720 }
else if(
cols() != k) {
734 return algebra::identity<mat<Type, N, K>>();
749 template<
typename Vector =
vec<
real, N - 1>>
751 return algebra::translation<mat<Type, N, K>>(t);
759 static_assert(N >= 2 && K >= 2,
"The matrix must be 2x2 or bigger");
760 return algebra::rotation_2d<mat<Type, N, K>>(theta);
768 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
769 return algebra::rotation_3d_xaxis<mat<Type, N, K>>(theta);
777 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
778 return algebra::rotation_3d_yaxis<mat<Type, N, K>>(theta);
786 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
787 return algebra::rotation_3d_zaxis<mat<Type, N, K>>(theta);
796 template<
typename Vector = vec<real, 3>>
798 static_assert(N >= 3 && K >= 3,
"The matrix must be 3x3 or bigger");
799 return algebra::rotation_3d<mat<Type, N, K>>(theta, axis);
815 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
816 return algebra::perspective<mat<Type, N, K>>(
817 left, right, bottom, top, near, far);
830 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
831 return algebra::perspective_fov<mat<Type, N, K>>(fov, aspect, near, far);
845 static_assert(N >= 4 && K >= 4,
"The matrix must be 4x4 or bigger");
846 return algebra::ortho<mat<Type, N, K>>(left, right, bottom, top, near, far);
857 template<
typename Vector1,
typename Vector2,
typename Vector3>
859 const Vector1& camera,
const Vector2& target,
const Vector3& up) {
860 return algebra::look_at<mat<Type, 4, 4>>(camera, target, up);
869 static_assert(N == K && (N % 2 == 0),
870 "N must equal K and they should be a multiple of 2");
871 return algebra::symplectic<mat<Type, N, K>>(n, k);
876 #ifndef THEORETICA_NO_PRINT
883 std::string separator =
", ",
bool parenthesis =
true)
const {
885 std::stringstream res;
887 for (
unsigned int i = 0; i <
rows(); ++i) {
892 for (
unsigned int j = 0; j <
cols(); ++j) {
904 res <<
")" << std::endl;
912 inline operator std::string() {
937 template<
typename Type>
943 using Container = std::vector<T>;
946 Container<Container<Type>>
data;
956 mat() : row_sz(0), col_sz(0) {}
962 mat(
unsigned int n,
unsigned int k) {
975 resize(m.rows(), m.cols());
983 template<
typename T = Type>
984 inline mat(
const std::initializer_list<std::initializer_list<T>>&
rows) {
989 for (
const auto& row :
rows) {
991 for (
const auto& x : row) {
994 this->
resize(rows.size(), row.size());
996 else if (row.size() != col_sz) {
1003 j = (j + 1) % col_sz;
1014 template<
typename Matrix>
1016 resize(other.rows(), other.cols());
1029 const unsigned int m =
min(n, k);
1031 for (
unsigned int i = 0; i < m; ++i)
1065 template<
typename Matrix>
1077 template<
typename Matrix>
1110 template<
typename VecType,
unsigned int M>
1138 template<
typename Vector>
1141 if(v.size() !=
rows()) {
1142 TH_MATH_ERROR(
"mat::transform", v.size(), INVALID_ARGUMENT);
1157 template<
unsigned int N = 0,
unsigned int K = 0>
1167 template<
unsigned int N = 0,
unsigned int K = 0>
1216 template<
typename Matrix>
1217 inline Matrix
mul(
const Matrix& B)
const {
1220 res.resize(
rows(), B.cols());
1222 if(B.rows() !=
cols()) {
1238 template<
typename Matrix>
1251 template<
typename Matrix>
1264 template<
typename Matrix>
1305 template<
typename Matrix>
1307 return (*
this = this->
operator*(B));
1328 return algebra::transpose<mat<Type>,
mat<Type>>(*this);
1338 inline Type&
at(
unsigned int i,
unsigned int j) {
1340 #ifdef THEORETICA_ROW_FIRST
1354 inline const Type&
at(
unsigned int i,
unsigned int j)
const {
1356 #ifdef THEORETICA_ROW_FIRST
1383 inline const Type&
operator()(
unsigned int i,
unsigned int j)
const {
1394 inline Type
get(
unsigned int i,
unsigned int j)
const {
1396 #ifdef THEORETICA_ROW_FIRST
1481 template<
typename Matrix>
1493 template<
typename Matrix>
1609 #ifdef THEORETICA_ROW_FIRST
1610 size_t size1 = n, size2 = k;
1612 size_t size1 = k, size2 = n;
1619 for (
unsigned int i = 0; i < size1; ++i)
1629 std::vector<std::vector<Type>> new_data;
1630 new_data.resize(size1);
1631 for (
unsigned int i = 0; i < size1; ++i)
1632 new_data[i].
resize(size2);
1637 const size_t row_bound =
min(
rows(), n);
1638 const size_t col_bound =
min(
cols(), n);
1640 for (
unsigned int i = 0; i < row_bound; ++i) {
1641 for (
unsigned int j = 0; j < col_bound; ++j) {
1642 #ifdef THEORETICA_ROW_FIRST
1643 new_data[i][j] =
get(i, j);
1645 new_data[j][i] =
get(i, j);
1652 for (
unsigned int i = 0; i < n; ++i) {
1653 for (
unsigned int j = col_bound; j < k; ++j) {
1656 #ifdef THEORETICA_ROW_FIRST
1657 new_data[i][j] = (Type) 0;
1659 new_data[j][i] = (Type) 0;
1667 for (
unsigned int i = row_bound; i < n; ++i) {
1668 for (
unsigned int j = 0; j < k; ++j) {
1671 #ifdef THEORETICA_ROW_FIRST
1672 new_data[i][j] = (Type) 0;
1674 new_data[j][i] = (Type) 0;
1700 unsigned int row_sz,
unsigned int col_sz) {
1702 return algebra::identity<mat<Type>>(row_sz, col_sz);
1712 Type diag,
unsigned int row_sz,
unsigned int col_sz) {
1720 template<
typename Vector>
1722 return algebra::translation<mat<Type>>(t);
1730 return algebra::rotation_2d<mat<Type>>(theta);
1738 return algebra::rotation_3d_xaxis<mat<Type>>(theta);
1746 return algebra::rotation_3d_yaxis<mat<Type>>(theta);
1754 return algebra::rotation_3d_zaxis<mat<Type>>(theta);
1762 template<
typename Vector = vec<real, 3>>
1764 return algebra::rotation_3d<mat<Type>>(theta, axis);
1779 return algebra::perspective<mat<Type>>(
1780 left, right, bottom, top, near, far);
1792 return algebra::perspective_fov<mat<Type>>(fov, aspect, near, far);
1805 return algebra::ortho<mat<Type>>(left, right, bottom, top, near, far);
1815 template<
typename Vector1,
typename Vector2,
typename Vector3>
1817 const Vector1& camera,
const Vector2& target,
const Vector3& up) {
1818 return algebra::look_at<mat<Type>>(camera, target, up);
1827 return algebra::symplectic<mat<Type>>(
rows,
cols);
1833 #ifndef THEORETICA_NO_PRINT
1837 std::string separator =
", ",
bool parenthesis =
true)
const {
1839 std::stringstream res;
1841 for (
unsigned int i = 0; i <
rows(); ++i) {
1846 for (
unsigned int j = 0; j <
cols(); ++j) {
1858 res <<
")" << std::endl;
1866 inline operator std::string() {
1873 std::ostream& out,
const mat<Type>& obj) {
A generic matrix with a variable number of rows and columns.
Definition: mat.h:938
mat< Type > operator/(Type scalar) const
Divides each element in the matrix by a scalar.
Definition: mat.h:1120
Type diagonal_product()
Compute the product of the diagonal elements of a square matrix.
Definition: mat.h:1565
static mat< Type > rotation_3d_zaxis(real theta)
Get a matrix which rotates by theta radians around the z-axis.
Definition: mat.h:1753
static mat< Type > rotation_3d_xaxis(real theta)
Get a matrix which rotates by theta radians around the x-axis.
Definition: mat.h:1737
mat< Type > & operator=(const Matrix &other)
Copy assignment operator for copying from another matrix.
Definition: mat.h:1015
bool is_diagonal() const
Determine if the matrix is diagonal (all non-diagonal elements are zero).
Definition: mat.h:1512
static mat< Type > identity(unsigned int row_sz, unsigned int col_sz)
Get the identity matrix.
Definition: mat.h:1699
mat< Type > & resize(unsigned int n, unsigned int k)
Set or change the size of the matrix.
Definition: mat.h:1602
vec< Type, N > operator*(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition: mat.h:1168
mat< Type > & transpose()
Transpose the current matrix in place.
Definition: mat.h:1317
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a vector by multiplying it with the matrix.
Definition: mat.h:1158
auto operator*(const Matrix &B) const
Matrix multiplication with any matrix type.
Definition: mat.h:1239
mat(unsigned int n, unsigned int k)
Constructor that initializes a matrix with the specified number of rows and columns.
Definition: mat.h:962
mat< Type > & operator-=(const Matrix &other)
Matrix subtraction with another matrix.
Definition: mat.h:1265
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:1816
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:1383
bool operator==(const Matrix &other) const
Check if two matrices are equal element by element.
Definition: mat.h:1482
static mat< Type > rotation_3d_yaxis(real theta)
Get a matrix which rotates by theta radians around the y-axis.
Definition: mat.h:1745
mat(Type diagonal, unsigned int n, unsigned int k)
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
Definition: mat.h:1025
bool is_symmetric() const
Determine if the matrix is symmetric (matrix == transpose).
Definition: mat.h:1521
bool operator!=(const Matrix &other) const
Check if two matrices are unequal element by element.
Definition: mat.h:1494
Type trace()
Compute the trace (sum of elements on the diagonal) of a square matrix.
Definition: mat.h:1556
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:1111
static mat< Type > rotation_3d(real theta, Vector &&axis)
Get a matrix which rotates by theta radians around the given axis.
Definition: mat.h:1763
bool is_square() const
Determine if the matrix is square (rows == columns).
Definition: mat.h:1503
mat(const Matrix &m)
Copy constructor for creating a matrix from another matrix.
Definition: mat.h:974
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:1371
static mat< Type > zeroes(unsigned int rows, unsigned int cols)
Static method that returns a null matrix with specified dimensions.
Definition: mat.h:1053
mat(const std::initializer_list< std::initializer_list< T >> &rows)
Constructor that initializes a matrix from a list of rows.
Definition: mat.h:984
static mat< Type > translation(Vector &&t)
Get a 4x4 matrix which translates by {x, y, z}.
Definition: mat.h:1721
static mat< Type > perspective_fov(real fov, real aspect, real near, real far)
Get a perspective projection matrix using field of view.
Definition: mat.h:1789
auto end() const
Get a const iterator to one past the last element of the matrix.
Definition: mat.h:1448
TH_CONSTEXPR unsigned int cols() const
Get the number of columns in the matrix.
Definition: mat.h:1462
mat< Type > & operator*=(const Matrix &B)
Matrix multiplication with any matrix type.
Definition: mat.h:1306
TH_CONSTEXPR unsigned int rows() const
Get the number of rows in the matrix.
Definition: mat.h:1455
mat< Type > mul(const mat< Type > &B) const
Transform a vector by the matrix.
Definition: mat.h:1190
mat< Type > operator*(Type scalar) const
Multiplies the matrix by a scalar.
Definition: mat.h:1088
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:1872
Container< Container< Type > > data
Dynamically allocated array of the elements.
Definition: mat.h:946
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition: mat.h:1139
unsigned int size() const
Get the total number of elements of the matrix (rows * columns)
Definition: mat.h:1470
mat< Type > operator+(const Matrix &other) const
Adds two matrices element-wise.
Definition: mat.h:1066
mat< Type > & invert()
Invert a generic square matrix.
Definition: mat.h:1594
auto end()
Get an iterator to one past the last element of the matrix.
Definition: mat.h:1428
static mat< Type > rotation_2d(real theta)
Get a matrix which rotates the 2D plane by theta radians.
Definition: mat.h:1729
Type & at(unsigned int i, unsigned int j)
Access a modifiable element at a specific row and column.
Definition: mat.h:1338
static mat< Type > diagonal(Type diag, unsigned int row_sz, unsigned int col_sz)
Get a diagonal matrix with a specified diagonal value.
Definition: mat.h:1711
const Type & at(unsigned int i, unsigned int j) const
Access a constant element at a specific row and column.
Definition: mat.h:1354
auto begin()
Get an iterator to the first element of the matrix.
Definition: mat.h:1417
friend mat< Type > operator*(Type a, const mat< Type > &B)
Friend operator to enable equations of the form (T) * (mat)
Definition: mat.h:1099
mat< Type > & operator+=(const Matrix &other)
Matrix addition with another matrix.
Definition: mat.h:1252
mat()
Default constructor.
Definition: mat.h:956
mat< Type > & operator*=(Type scalar)
Scalar multiplication of the matrix.
Definition: mat.h:1276
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Convert the matrix to string representation.
Definition: mat.h:1836
void make_zeroes()
Sets all elements in the matrix to zero.
Definition: mat.h:1044
static mat< Type > symplectic(unsigned int rows, unsigned int cols)
A symplectic NxN matrix, where for some natural K.
Definition: mat.h:1826
mat< Type > inverse() const
Compute the inverse of a generic square matrix.
Definition: mat.h:1585
static mat< Type > ortho(real left, real right, real bottom, real top, real near, real far)
Get an orthographic projection matrix.
Definition: mat.h:1803
mat< Type > & operator/=(Type scalar)
Scalar division of the matrix.
Definition: mat.h:1287
mat< Type > operator-(const Matrix &other) const
Subtracts another matrix element-wise.
Definition: mat.h:1078
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:1547
mat< Type > transposed() const
Return a transposed copy of the current matrix.
Definition: mat.h:1327
static mat< Type > perspective(real left, real right, real bottom, real top, real near, real far)
Get a perspective projection matrix.
Definition: mat.h:1775
Matrix mul(const Matrix &B) const
Multiplies the matrix by another matrix of any compatible type.
Definition: mat.h:1217
auto begin() const
Get a const iterator to the first element of the matrix.
Definition: mat.h:1438
~mat()
Destructor that deallocates memory and resets matrix size.
Definition: mat.h:1037
Type det() const
Compute the determinant of the matrix.
Definition: mat.h:1575
Type get(unsigned int i, unsigned int j) const
Get a copy of the element at a specific row and column.
Definition: mat.h:1394
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:1534
A sequential iterator for matrices.
Definition: mat.h:34
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
mat_iterator & operator++()
Advances the iterator to the next element in row-major order.
Definition: mat.h:84
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
static mat< Type, N, K > rotation_3d_zaxis(real theta)
Returns a matrix for 3D rotation around the z-axis.
Definition: mat.h:785
static mat< Type, N, K > rotation_2d(real theta)
Returns a matrix for 2D rotation by theta radians.
Definition: mat.h:758
std::string to_string(std::string separator=", ", bool parenthesis=true) const
Converts the matrix to a string representation.
Definition: mat.h:882
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:716
bool is_symmetric() const
Checks if the matrix is symmetric.
Definition: mat.h:636
static mat< Type, N, K > perspective_fov(real fov, real aspect, real near, real far)
Returns a perspective projection matrix based on field of view.
Definition: mat.h:827
mat< Type, N, K > & invert()
Inverts the matrix in place.
Definition: mat.h:705
bool operator!=(const Matrix &other) const
Checks whether this matrix is not equal to another matrix element-wise.
Definition: mat.h:615
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:858
Type & operator()(unsigned int i, unsigned int j)
Overloads the () operator to access an element.
Definition: mat.h:515
static mat< Type, N, K > zeroes()
Returns a null matrix with all elements set to zero.
Definition: mat.h:240
bool is_diagonal() const
Checks if the matrix is diagonal.
Definition: mat.h:629
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:574
mat< Type, N, K > inverse() const
Computes the inverse of the matrix.
Definition: mat.h:695
mat_iterator< mat< Type, N, K >, Type & > iterator
Iterator for statically allocated matrices.
Definition: mat.h:544
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:921
Type get(unsigned int i, unsigned int j) const
Gets the element at the specified row and column.
Definition: mat.h:533
Type trace()
Computes the trace of the matrix.
Definition: mat.h:669
static mat< Type, N, K > translation(Vector &&t)
Returns a 4x4 matrix for translation by the vector {x, y, z}.
Definition: mat.h:750
mat< Type, N, K > & operator+=(const Matrix &other)
Matrix addition.
Definition: mat.h:415
mat< Type, N, K > operator/(Type scalar) const
Scalar division.
Definition: mat.h:305
mat(const std::initializer_list< std::initializer_list< T >> &rows)
Constructs a matrix from an initializer list.
Definition: mat.h:172
mat< Type, N, M > mul(const mat< Type, K, M > &B) const
Matrix multiplication for matrices with different column counts.
Definition: mat.h:361
bool is_square() const
Checks if the matrix is square.
Definition: mat.h:622
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:294
mat< Type, N, K > & operator=(const Matrix &other)
Assignment operator to copy from another matrix.
Definition: mat.h:227
Type diagonal_product()
Computes the product of the diagonal elements.
Definition: mat.h:676
static mat< Type, N, K > ortho(real left, real right, real bottom, real top, real near, real far)
Returns an orthographic projection matrix.
Definition: mat.h:843
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition: mat.h:581
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
const Type & at(unsigned int i, unsigned int j) const
Accesses the element at the given row and column.
Definition: mat.h:501
mat_iterator< const mat< Type, N, K >, const Type & > const_iterator
Const iterator for statically allocated matrices.
Definition: mat.h:548
unsigned int size() const
Returns the total number of elements in the matrix.
Definition: mat.h:595
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition: mat.h:588
static mat< Type, N, K > rotation_3d(real theta, Vector &&axis)
Returns a matrix for 3D rotation around an arbitrary axis.
Definition: mat.h:797
mat()
Default constructor.
Definition: mat.h:149
auto end()
Returns an iterator to one past the last element of the matrix.
Definition: mat.h:560
const Type & operator()(unsigned int i, unsigned int j) const
Overloads the () operator to access an element.
Definition: mat.h:524
static mat< Type, N, K > perspective(real left, real right, real bottom, real top, real near, real far)
Returns a perspective projection matrix.
Definition: mat.h:811
auto begin() const
Returns a const iterator to the first element of the matrix.
Definition: mat.h:567
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:649
static mat< Type, N, K > rotation_3d_yaxis(real theta)
Returns a matrix for 3D rotation around the y-axis.
Definition: mat.h:776
mat(const Matrix &m)
Copy constructor.
Definition: mat.h:160
static mat< Type, N, K > rotation_3d_xaxis(real theta)
Returns a matrix for 3D rotation around the x-axis.
Definition: mat.h:767
auto operator*(const Matrix &B) const
Overloads the * operator for matrix multiplication.
Definition: mat.h:395
Vector transform(const Vector &v) const
Transforms a vector by multiplying it with the matrix.
Definition: mat.h:326
Matrix mul(const Matrix &B) const
Matrix multiplication for matrices with any type.
Definition: mat.h:375
bool operator==(const Matrix &other) const
Checks whether this matrix is equal to another matrix element-wise.
Definition: mat.h:605
mat< Type, N, K > & operator*=(Type scalar)
Scalar multiplication.
Definition: mat.h:433
Type & at(unsigned int i, unsigned int j)
Accesses the element at the given row and column.
Definition: mat.h:487
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:662
static mat< Type, N, K > diagonal(Type diag)
Returns a diagonal matrix with the specified diagonal element.
Definition: mat.h:741
mat< Type, K, N > transposed() const
Returns a transposed version of the matrix.
Definition: mat.h:478
mat< Type, N, K > & operator/=(Type scalar)
Scalar division.
Definition: mat.h:443
Type det() const
Computes the determinant of the matrix.
Definition: mat.h:685
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:868
mat< Type, N, K > & operator*=(const Matrix &B)
Matrix multiplication with an assignment operator.
Definition: mat.h:459
static mat< Type, N, K > identity()
Returns the identity matrix.
Definition: mat.h:733
mat< Type, N, K > & operator-=(const Matrix &other)
Matrix subtraction.
Definition: mat.h:425
vec< Type, N > transform(const vec< Type, K > &v) const
Transforms a fixed-size vector by multiplying it with the matrix.
Definition: mat.h:343
mat< Type, N, K > operator+(const Matrix &other) const
Matrix addition.
Definition: mat.h:252
mat< Type, N, K > operator*(Type scalar) const
Scalar multiplication.
Definition: mat.h:272
mat< Type, N, K > & transpose()
Transposes the matrix in place.
Definition: mat.h:469
friend mat< Type, N, K > operator*(Type a, const mat< Type, N, K > &B)
Friend operator for scalar multiplication (T * mat).
Definition: mat.h:282
mat< Type, N, K > operator-(const Matrix &other) const
Matrix subtraction.
Definition: mat.h:263
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:351
auto begin()
Returns an iterator to the first element of the matrix.
Definition: mat.h:553
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:92
#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
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:198
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, 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:207
constexpr real E
The Euler mathematical constant (e)
Definition: constants.h:237
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.