Theoretica
A C++ numerical and automatic mathematical library
theoretica::mat< Type, N, K > Class Template Reference

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.
 
template<typename Matrix >
 mat (const Matrix &m)
 Copy constructor.
 
template<typename T = Type>
 mat (const std::initializer_list< std::initializer_list< T >> &rows)
 Construct from a list of the rows.
 
template<typename Matrix >
mat< Type, N, K > & operator= (const Matrix &other)
 Copy constructor.
 
void make_zeroes ()
 Set all elements to zero.
 
template<typename Matrix >
mat< Type, N, K > operator+ (const Matrix &other) const
 Matrix addition.
 
template<typename Matrix >
mat< Type, N, K > operator- (const Matrix &other) const
 Matrix subtraction.
 
mat< Type, N, K > operator* (Type scalar) const
 Scalar multiplication.
 
mat< Type, N, K > operator/ (Type scalar) const
 Scalar division.
 
template<typename Vector >
Vector transform (const Vector &v) const
 Transform a vector v by the matrix.
 
vec< Type, N > transform (const vec< Type, K > &v) const
 Transform a vector by the matrix.
 
vec< Type, N > operator* (const vec< Type, K > &v) const
 Transform a vector by the matrix.
 
template<unsigned int M>
mat< Type, N, M > mul (const mat< Type, K, M > &B) const
 Matrix multiplication.
 
template<typename Matrix >
Matrix mul (const Matrix &B) const
 Matrix multiplication by any matrix type.
 
template<typename Matrix >
auto operator* (const Matrix &B) const
 Matrix multiplication.
 
template<typename Matrix >
mat< Type, N, K > & operator+= (const Matrix &other)
 Matrix addition.
 
template<typename Matrix >
mat< Type, N, K > & operator-= (const Matrix &other)
 Matrix subtraction.
 
mat< Type, N, K > & operator*= (Type scalar)
 Scalar multiplication.
 
mat< Type, N, K > & operator/= (Type scalar)
 Scalar division.
 
template<typename Matrix >
mat< Type, N, K > & operator*= (const Matrix &B)
 Matrix multiplication.
 
mat< Type, N, K > & transpose ()
 Transpose the matrix itself.
 
mat< Type, K, N > transposed () const
 Return the transposed matrix, without modifying the matrix itself.
 
Type & at (unsigned int i, unsigned int j)
 Access the element at the i-th row and j-th column.
 
const Type & at (unsigned int i, unsigned int j) const
 Access the element at the i-th row and j-th column.
 
Type & operator() (unsigned int i, unsigned int j)
 Access the element at the i-th row and j-th column.
 
const Type & operator() (unsigned int i, unsigned int j) const
 Get the element at the i-th row and j-th column.
 
Type get (unsigned int i, unsigned int j) const
 Get the element at the i-th row and j-th column.
 
auto begin ()
 Get an iterator to the first element of the matrix.
 
auto end ()
 Get an iterator to one plus the last element of the matrix.
 
auto begin () const
 Get a const iterator to the first element of the matrix.
 
auto end () const
 Get a const iterator to one plus the last element of the matrix.
 
TH_CONSTEXPR unsigned int rows () const
 Get the number of rows of the matrix.
 
TH_CONSTEXPR unsigned int cols () const
 Get the number of columns of the matrix.
 
unsigned int size () const
 Get the total number of elements of the matrix (rows * columns)
 
template<typename Matrix >
bool operator== (const Matrix &other) const
 Check whether two matrices are equal element by element.
 
template<typename Matrix >
bool operator!= (const Matrix &other) const
 Check whether two matrices are unequal element by element.
 
bool is_square () const
 Return whether the matrix is square.
 
bool is_diagonal () const
 Return whether the matrix is diagonal.
 
bool is_symmetric () const
 Return whether the matrix is symmetric.
 
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 ()
 Compute the trace (sum of elements on the diagonal) of a square matrix.
 
Type diagonal_product ()
 Compute the product of the diagonal elements of a square matrix.
 
Type det () const
 Compute the determinant of the matrix.
 
mat< Type, N, K > inverse () const
 Compute the inverse of a generic square matrix.
 
mat< Type, N, K > & invert ()
 Invert a generic square matrix.
 
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
 Convert the matrix to string representation.
 
 operator std::string ()
 Convert the matrix to string representation.
 

Static Public Member Functions

static mat< Type, N, K > zeroes ()
 Get the null matrix.
 
static mat< Type, N, K > identity ()
 Get the identity matrix.
 
static mat< Type, N, K > diagonal (Type diag)
 Get a diagonal matrix.
 
template<typename Vector = vec<real, N - 1>>
static mat< Type, N, K > translation (Vector &&t)
 Get a 4x4 matrix which translates by {x, y, z}.
 
static mat< Type, N, K > rotation_2d (real theta)
 Get a matrix which rotates the 2D plane of <theta> radians.
 
static mat< Type, N, K > rotation_3d_xaxis (real theta)
 Get a matrix which rotates <theta> radians around the x axis.
 
static mat< Type, N, K > rotation_3d_yaxis (real theta)
 Get a matrix which rotates <theta> radians around the y axis.
 
static mat< Type, N, K > rotation_3d_zaxis (real theta)
 Get a matrix which rotates <theta> radians around the z axis.
 
template<typename Vector = vec<real, 3>>
static mat< Type, N, K > rotation_3d (real theta, Vector &&axis)
 Get a matrix which rotates <theta> radians around the <axis> axis.
 
static mat< Type, N, K > perspective (real left, real right, real bottom, real top, real near, real far)
 
static mat< Type, N, K > perspective_fov (real fov, real aspect, real near, real far)
 
static mat< Type, N, K > ortho (real left, real right, real bottom, real top, real near, real far)
 
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.
 
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.
 

Public Attributes

Type data [K][N]
 

Friends

mat< Type, N, K > operator* (Type a, const mat< Type, N, K > &B)
 Friend operator to enable equations of the form (T) * (mat)
 
template<typename VecType , unsigned int M>
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)
 
std::ostream & operator<< (std::ostream &out, const mat< Type, N, K > &obj)
 Stream the matrix in string representation to an output stream (std::ostream)
 

Detailed Description

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
class theoretica::mat< Type, N, K >

A generic matrix with a fixed number of rows and columns.

Parameters
TypeThe type of the elements
NThe number of rows
KThe number of columns

Member Function Documentation

◆ density()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
real theoretica::mat< Type, N, K >::density ( real  tolerance = 1E-12)
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.

Parameters
toleranceThe minimum tolerance in absolute value to consider an element non-zero.
Returns
A real number between 0 and 1 representing the proportion of non-zero elements of the matrix.

◆ resize()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat<Type, N, K> theoretica::mat< Type, N, K >::resize ( unsigned int  n,
unsigned int  k 
) const
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.

◆ sparsity()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
real theoretica::mat< Type, N, K >::sparsity ( real  tolerance = 1E-12)
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.

Parameters
toleranceThe minimum tolerance in absolute value to consider an element non-zero.
Returns
A real number between 0 and 1 representing the proportion of zero elements of the matrix.

The documentation for this class was generated from the following file: