Theoretica
A C++ numerical and automatic mathematical library
|
A generic matrix with a fixed number of rows and columns. More...
#include <mat.h>
Public Types | |
using | iterator = mat_iterator< mat< Type, N, K >, Type & > |
Iterator for statically allocated matrices. | |
using | const_iterator = mat_iterator< const mat< Type, N, K >, const Type & > |
Const iterator for statically allocated matrices. | |
Public Member Functions | |
mat () | |
Default constructor. More... | |
template<typename Matrix > | |
mat (const Matrix &m) | |
Copy constructor. More... | |
template<typename T = Type> | |
mat (const std::initializer_list< std::initializer_list< T >> &rows) | |
Constructs a matrix from an initializer list. More... | |
mat (Type diagonal, unsigned int n=0, unsigned int k=0) | |
Constructor that initializes a diagonal matrix with equal entries on the diagonal. More... | |
template<typename Matrix > | |
mat< Type, N, K > & | operator= (const Matrix &other) |
Assignment operator to copy from another matrix. More... | |
void | make_zeroes () |
Sets all elements of the matrix to zero. | |
template<typename Matrix > | |
mat< Type, N, K > | operator+ (const Matrix &other) const |
Matrix addition. More... | |
template<typename Matrix > | |
mat< Type, N, K > | operator- (const Matrix &other) const |
Matrix subtraction. More... | |
mat< Type, N, K > | operator* (Type scalar) const |
Scalar multiplication. More... | |
mat< Type, N, K > | operator/ (Type scalar) const |
Scalar division. More... | |
template<typename Vector > | |
Vector | transform (const Vector &v) const |
Transforms a vector by multiplying it with the matrix. More... | |
vec< Type, N > | transform (const vec< Type, K > &v) const |
Transforms a fixed-size vector by multiplying it with the matrix. More... | |
vec< Type, N > | operator* (const vec< Type, K > &v) const |
Overloads the * operator to transform a fixed-size vector by the matrix. More... | |
template<unsigned int M> | |
mat< Type, N, M > | mul (const mat< Type, K, M > &B) const |
Matrix multiplication for matrices with different column counts. More... | |
template<typename Matrix > | |
Matrix | mul (const Matrix &B) const |
Matrix multiplication for matrices with any type. More... | |
template<typename Matrix > | |
auto | operator* (const Matrix &B) const |
Overloads the * operator for matrix multiplication. More... | |
template<typename Matrix > | |
mat< Type, N, K > & | operator+= (const Matrix &other) |
Matrix addition. More... | |
template<typename Matrix > | |
mat< Type, N, K > & | operator-= (const Matrix &other) |
Matrix subtraction. More... | |
mat< Type, N, K > & | operator*= (Type scalar) |
Scalar multiplication. More... | |
mat< Type, N, K > & | operator/= (Type scalar) |
Scalar division. More... | |
template<typename Matrix > | |
mat< Type, N, K > & | operator*= (const Matrix &B) |
Matrix multiplication with an assignment operator. More... | |
mat< Type, N, K > & | transpose () |
Transposes the matrix in place. More... | |
mat< Type, K, N > | transposed () const |
Returns a transposed version of the matrix. More... | |
Type & | at (unsigned int i, unsigned int j) |
Accesses the element at the given row and column. More... | |
const Type & | at (unsigned int i, unsigned int j) const |
Accesses the element at the given row and column. More... | |
Type & | operator() (unsigned int i, unsigned int j) |
Overloads the () operator to access an element. More... | |
const Type & | operator() (unsigned int i, unsigned int j) const |
Overloads the () operator to access an element. More... | |
Type | get (unsigned int i, unsigned int j) const |
Gets the element at the specified row and column. More... | |
auto | begin () |
Returns an iterator to the first element of the matrix. More... | |
auto | end () |
Returns an iterator to one past the last element of the matrix. More... | |
auto | begin () const |
Returns a const iterator to the first element of the matrix. More... | |
auto | end () const |
Returns a const iterator to one past the last element of the matrix. More... | |
TH_CONSTEXPR unsigned int | rows () const |
Returns the number of rows in the matrix. More... | |
TH_CONSTEXPR unsigned int | cols () const |
Returns the number of columns in the matrix. More... | |
unsigned int | size () const |
Returns the total number of elements in the matrix. More... | |
template<typename Matrix > | |
bool | operator== (const Matrix &other) const |
Checks whether this matrix is equal to another matrix element-wise. More... | |
template<typename Matrix > | |
bool | operator!= (const Matrix &other) const |
Checks whether this matrix is not equal to another matrix element-wise. More... | |
bool | is_square () const |
Checks if the matrix is square. More... | |
bool | is_diagonal () const |
Checks if the matrix is diagonal. More... | |
bool | is_symmetric () const |
Checks if the matrix is symmetric. More... | |
real | density (real tolerance=1E-12) |
Compute the density of the matrix, counting the proportion of non-zero (bigger in module than the given tolerance) elements with respect to the total number of elements. More... | |
real | sparsity (real tolerance=1E-12) |
Compute the sparsity of the matrix, counting the proportion of zero (smaller in module than the given tolerance) elements with respect to the total number of elements. More... | |
Type | trace () |
Computes the trace of the matrix. More... | |
Type | diagonal_product () |
Computes the product of the diagonal elements. More... | |
Type | det () const |
Computes the determinant of the matrix. More... | |
mat< Type, N, K > | inverse () const |
Computes the inverse of the matrix. More... | |
mat< Type, N, K > & | invert () |
Inverts the matrix in place. More... | |
mat< Type, N, K > | resize (unsigned int n, unsigned int k) const |
Compatibility function to allow for allocation or resizing of dynamic matrices. More... | |
std::string | to_string (std::string separator=", ", bool parenthesis=true) const |
Converts the matrix to a string representation. More... | |
operator std::string () | |
Convert the matrix to string representation. | |
Static Public Member Functions | |
static mat< Type, N, K > | zeroes () |
Returns a null matrix with all elements set to zero. More... | |
static mat< Type, N, K > | identity () |
Returns the identity matrix. More... | |
static mat< Type, N, K > | diagonal (Type diag) |
Returns a diagonal matrix with the specified diagonal element. More... | |
template<typename Vector = vec<real, N - 1>> | |
static mat< Type, N, K > | translation (Vector &&t) |
Returns a 4x4 matrix for translation by the vector {x, y, z}. More... | |
static mat< Type, N, K > | rotation_2d (real theta) |
Returns a matrix for 2D rotation by theta radians. More... | |
static mat< Type, N, K > | rotation_3d_xaxis (real theta) |
Returns a matrix for 3D rotation around the x-axis. More... | |
static mat< Type, N, K > | rotation_3d_yaxis (real theta) |
Returns a matrix for 3D rotation around the y-axis. More... | |
static mat< Type, N, K > | rotation_3d_zaxis (real theta) |
Returns a matrix for 3D rotation around the z-axis. More... | |
template<typename Vector = vec<real, 3>> | |
static mat< Type, N, K > | rotation_3d (real theta, Vector &&axis) |
Returns a matrix for 3D rotation around an arbitrary axis. More... | |
static mat< Type, N, K > | perspective (real left, real right, real bottom, real top, real near, real far) |
Returns a perspective projection matrix. More... | |
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. More... | |
static mat< Type, N, K > | ortho (real left, real right, real bottom, real top, real near, real far) |
Returns an orthographic projection matrix. More... | |
template<typename Vector1 , typename Vector2 , typename Vector3 > | |
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 <camera> point. More... | |
static mat< Type, N, K > | symplectic (unsigned int n=0, unsigned int k=0) |
A symplectic NxN matrix, where \(N = 2K\) for some natural K. More... | |
Public Attributes | |
Type | data [K][N] |
Friends | |
mat< Type, N, K > | operator* (Type a, const mat< Type, N, K > &B) |
Friend operator for scalar multiplication (T * mat). More... | |
template<typename VecType , unsigned int M> | |
vec< VecType, K > | operator* (const vec< VecType, M > &a, const mat< Type, N, K > &B) |
Friend operator for vector-matrix multiplication. More... | |
std::ostream & | operator<< (std::ostream &out, const mat< Type, N, K > &obj) |
Outputs the matrix to an output stream in string format. More... | |
A generic matrix with a fixed number of rows and columns.
Type | The type of the elements |
N | The number of rows |
K | The number of columns |
|
inline |
Default constructor.
Initializes the matrix with all elements set to zero.
|
inline |
Copy constructor.
Matrix | The type of the matrix to copy from. |
m | The matrix to copy. |
Copies all elements from the given matrix m
into this matrix.
|
inline |
Constructs a matrix from an initializer list.
T | The type of elements in the initializer list (default is Type ). |
rows | An initializer list representing the rows of the matrix. |
Initializes the matrix with values from the initializer list. Ensures that the initializer list matches the size of the matrix (i.e., N
rows and K
columns).
|
inline |
Constructor that initializes a diagonal matrix with equal entries on the diagonal.
The size parameters are required for compatibility with mat<T, 0> and may be used for additional error prevention.
diagonal | The value for the diagonal entries. |
n | Number of rows. |
k | Number of columns. |
|
inline |
Accesses the element at the given row and column.
i | The row index. |
j | The column index. |
|
inline |
Accesses the element at the given row and column.
i | The row index. |
j | The column index. |
|
inline |
Returns an iterator to the first element of the matrix.
|
inline |
Returns a const iterator to the first element of the matrix.
|
inline |
Returns the number of columns in the matrix.
|
inline |
Compute the density of the matrix, counting the proportion of non-zero (bigger in module than the given tolerance) elements with respect to the total number of elements.
tolerance | The minimum tolerance in absolute value to consider an element non-zero. |
|
inline |
Computes the determinant of the matrix.
This function is only valid for square matrices.
|
inlinestatic |
Returns a diagonal matrix with the specified diagonal element.
diag | The value for the diagonal elements. |
|
inline |
Computes the product of the diagonal elements.
|
inline |
Returns an iterator to one past the last element of the matrix.
|
inline |
Returns a const iterator to one past the last element of the matrix.
|
inline |
Gets the element at the specified row and column.
i | The row index. |
j | The column index. |
|
inlinestatic |
Returns the identity matrix.
|
inline |
Computes the inverse of the matrix.
This function is only valid for square matrices.
|
inline |
Inverts the matrix in place.
This function is only valid for square matrices.
|
inline |
Checks if the matrix is diagonal.
true
if the matrix is diagonal, false
otherwise.
|
inline |
Checks if the matrix is square.
true
if the matrix is square (N == K), false
otherwise.
|
inline |
Checks if the matrix is symmetric.
true
if the matrix is symmetric, false
otherwise.
|
inlinestatic |
Return a 4x4 transformation matrix that points the field of view towards a given point from the <camera> point.
Vector1,Vector2,Vector3 | Types for the camera, target, and up vectors. |
camera | The camera position. |
target | The target point to look at. |
up | The up direction vector. |
|
inline |
Matrix multiplication for matrices with different column counts.
M | The number of columns in matrix B . |
B | The matrix to multiply with. |
|
inline |
Matrix multiplication for matrices with any type.
Matrix | The type of the matrix to multiply with. |
B | The matrix to multiply with. |
This function multiplies this matrix with another matrix B
. It checks if the number of rows in B
matches the number of columns in this matrix.
|
inline |
Checks whether this matrix is not equal to another matrix element-wise.
Matrix | The type of the other matrix. |
other | The matrix to compare with. |
true
if any elements are unequal, false
otherwise.
|
inline |
Overloads the ()
operator to access an element.
i | The row index. |
j | The column index. |
|
inline |
Overloads the ()
operator to access an element.
i | The row index. |
j | The column index. |
|
inline |
Overloads the *
operator for matrix multiplication.
Matrix | The type of the matrix to multiply with. |
B | The matrix to multiply with. |
|
inline |
Overloads the *
operator to transform a fixed-size vector by the matrix.
v | The vector to transform. |
|
inline |
Scalar multiplication.
scalar | The scalar value to multiply. |
scalar
.
|
inline |
Matrix multiplication with an assignment operator.
Matrix | The type of the matrix to multiply with. |
B | The matrix to multiply with. |
|
inline |
Scalar multiplication.
scalar | The scalar value to multiply with. |
|
inline |
Matrix addition.
Matrix | The type of the matrix to add. |
other | The matrix to add. |
other
to this matrix.
|
inline |
Matrix addition.
Matrix | The type of the matrix to add. |
other | The matrix to add to this matrix. |
|
inline |
Matrix subtraction.
Matrix | The type of the matrix to subtract. |
other | The matrix to subtract. |
other
from this matrix.
|
inline |
Matrix subtraction.
Matrix | The type of the matrix to subtract. |
other | The matrix to subtract from this matrix. |
|
inline |
Scalar division.
scalar | The scalar divisor. |
scalar
.If scalar
is close to zero, an error is raised.
|
inline |
Scalar division.
scalar | The scalar value to divide by. |
If the scalar value is close to zero, this function raises a division by zero error.
|
inline |
Assignment operator to copy from another matrix.
Matrix | The type of the matrix to copy from. |
other | The matrix to copy. |
|
inline |
Checks whether this matrix is equal to another matrix element-wise.
Matrix | The type of the other matrix. |
other | The matrix to compare with. |
true
if all elements are equal, false
otherwise.
|
inlinestatic |
Returns an orthographic projection matrix.
left | The left boundary. |
right | The right boundary. |
bottom | The bottom boundary. |
top | The top boundary. |
near | The near boundary. |
far | The far boundary. |
|
inlinestatic |
Returns a perspective projection matrix.
left | The left boundary. |
right | The right boundary. |
bottom | The bottom boundary. |
top | The top boundary. |
near | The near boundary. |
far | The far boundary. |
|
inlinestatic |
Returns a perspective projection matrix based on field of view.
fov | The field of view angle in radians. |
aspect | The aspect ratio. |
near | The near boundary. |
far | The far boundary. |
|
inline |
Compatibility function to allow for allocation or resizing of dynamic matrices.
Since statically allocated matrices cannot change size, this function only checks whether the target size is the same as the matrix's.
|
inlinestatic |
Returns a matrix for 2D rotation by theta radians.
theta | The angle of rotation in radians. |
|
inlinestatic |
Returns a matrix for 3D rotation around an arbitrary axis.
Vector | The type of the rotation axis vector. |
theta | The angle of rotation in radians. |
axis | The axis vector to rotate around. |
|
inlinestatic |
Returns a matrix for 3D rotation around the x-axis.
theta | The angle of rotation in radians. |
|
inlinestatic |
Returns a matrix for 3D rotation around the y-axis.
theta | The angle of rotation in radians. |
|
inlinestatic |
Returns a matrix for 3D rotation around the z-axis.
theta | The angle of rotation in radians. |
|
inline |
Returns the number of rows in the matrix.
|
inline |
Returns the total number of elements in the matrix.
|
inline |
Compute the sparsity of the matrix, counting the proportion of zero (smaller in module than the given tolerance) elements with respect to the total number of elements.
tolerance | The minimum tolerance in absolute value to consider an element non-zero. |
|
inlinestatic |
A symplectic NxN matrix, where \(N = 2K\) for some natural K.
n | Optional parameter for number of rows. |
k | Optional parameter for number of columns. |
|
inline |
Converts the matrix to a string representation.
separator | Separator between elements (default is ", "). |
parenthesis | Whether to enclose each row in parentheses (default is true). |
|
inline |
Computes the trace of the matrix.
|
inline |
Transforms a fixed-size vector by multiplying it with the matrix.
v | The vector to transform. |
vec<Type, N>
.
|
inline |
Transforms a vector by multiplying it with the matrix.
Vector | The type of the vector to transform. |
v | The vector to transform. |
This function multiplies the given vector v
by the matrix. It checks if the size of v
matches the number of columns in the matrix.
|
inlinestatic |
Returns a 4x4 matrix for translation by the vector {x, y, z}.
Vector | The type of the translation vector. |
t | The translation vector. |
|
inline |
Transposes the matrix in place.
This function only works if the matrix is square. An assertion will trigger if the matrix is not square.
|
inline |
Returns a transposed version of the matrix.
|
inlinestatic |
Returns a null matrix with all elements set to zero.
|
friend |
Friend operator for vector-matrix multiplication.
VecType | The type of vector elements. |
M | The number of elements in the vector. |
a | The vector to multiply. |
B | The matrix to be multiplied by. |
|
friend |
Friend operator for scalar multiplication (T * mat).
a | The scalar multiplier. |
B | The matrix to be multiplied. |
B
by a
.
|
friend |
Outputs the matrix to an output stream in string format.
out | The output stream. |
obj | The matrix to output. |