Theoretica
A C++ numerical and automatic mathematical library
Loading...
Searching...
No Matches
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)
 Constructs a matrix from an initializer list.
 
 mat (Type diagonal, unsigned int n=0, unsigned int k=0)
 Constructor that initializes a diagonal matrix with equal entries on the diagonal.
 
template<typename Matrix >
mat< Type, N, K > & operator= (const Matrix &other)
 Assignment operator to copy from another matrix.
 
void make_zeroes ()
 Sets all elements of the matrix to zero.
 
template<typename Matrix >
mat< Type, N, Koperator+ (const Matrix &other) const
 Matrix addition.
 
template<typename Matrix >
mat< Type, N, Koperator- (const Matrix &other) const
 Matrix subtraction.
 
mat< Type, N, Koperator* (Type scalar) const
 Scalar multiplication.
 
mat< Type, N, Koperator/ (Type scalar) const
 Scalar division.
 
template<typename Vector >
Vector transform (const Vector &v) const
 Transforms a vector by multiplying it with the matrix.
 
vec< Type, N > transform (const vec< Type, K > &v) const
 Transforms a fixed-size vector by multiplying it with the matrix.
 
vec< Type, N > operator* (const vec< Type, K > &v) const
 Overloads the * operator to transform a fixed-size vector by the matrix.
 
template<unsigned int M>
mat< Type, N, Mmul (const mat< Type, K, M > &B) const
 Matrix multiplication for matrices with different column counts.
 
template<typename Matrix >
Matrix mul (const Matrix &B) const
 Matrix multiplication for matrices with any type.
 
template<typename Matrix >
auto operator* (const Matrix &B) const
 Overloads the * operator for 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 with an assignment operator.
 
mat< Type, N, K > & transpose ()
 Transposes the matrix in place.
 
Typeat (unsigned int i, unsigned int j)
 Accesses the element at the given row and column.
 
const Typeat (unsigned int i, unsigned int j) const
 Accesses the element at the given row and column.
 
Typeoperator() (unsigned int i, unsigned int j)
 Overloads the () operator to access an element.
 
const Typeoperator() (unsigned int i, unsigned int j) const
 Overloads the () operator to access an element.
 
Type get (unsigned int i, unsigned int j) const
 Gets the element at the specified row and column.
 
auto begin ()
 Returns an iterator to the first element of the matrix.
 
auto end ()
 Returns an iterator to one past the last element of the matrix.
 
auto begin () const
 Returns a const iterator to the first element of the matrix.
 
auto end () const
 Returns a const iterator to one past the last element of the matrix.
 
TH_CONSTEXPR unsigned int rows () const
 Returns the number of rows in the matrix.
 
TH_CONSTEXPR unsigned int cols () const
 Returns the number of columns in the matrix.
 
unsigned int size () const
 Returns the total number of elements in the matrix.
 
template<typename Matrix >
bool operator== (const Matrix &other) const
 Checks whether this matrix is equal to another matrix element-wise.
 
template<typename Matrix >
bool operator!= (const Matrix &other) const
 Checks whether this matrix is not equal to another matrix element-wise.
 
mat< Type, N, K > & invert ()
 Inverts the matrix in place.
 
mat< Type, N, Kresize (unsigned int n, unsigned int k) const
 Compatibility function to allow for allocation or resizing of dynamic matrices.
 
std::string to_string (std::string separator=", ", bool parenthesis=true) const
 Converts the matrix to a string representation.
 
 operator std::string ()
 Convert the matrix to string representation.
 

Public Attributes

Type data [K][N]
 

Friends

mat< Type, N, Koperator* (Type a, const mat< Type, N, K > &B)
 Friend operator for scalar multiplication (T * mat).
 
template<typename VecType , unsigned int M>
vec< VecType, Koperator* (const vec< VecType, M > &a, const mat< Type, N, K > &B)
 Friend operator for vector-matrix multiplication.
 
std::ostream & operator<< (std::ostream &out, const mat< Type, N, K > &obj)
 Outputs the matrix to an output stream in string format.
 

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

Constructor & Destructor Documentation

◆ mat() [1/4]

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

Default constructor.

Initializes the matrix with all elements set to zero.

◆ mat() [2/4]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
theoretica::mat< Type, N, K >::mat ( const Matrix m)
inline

Copy constructor.

Template Parameters
MatrixThe type of the matrix to copy from.
Parameters
mThe matrix to copy.

Copies all elements from the given matrix m into this matrix.

◆ mat() [3/4]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename T = Type>
theoretica::mat< Type, N, K >::mat ( const std::initializer_list< std::initializer_list< T > > &  rows)
inline

Constructs a matrix from an initializer list.

Template Parameters
TThe type of elements in the initializer list (default is Type).
Parameters
rowsAn 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).

◆ mat() [4/4]

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

Parameters
diagonalThe value for the diagonal entries.
nNumber of rows.
kNumber of columns.

Member Function Documentation

◆ at() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
Type & theoretica::mat< Type, N, K >::at ( unsigned int  i,
unsigned int  j 
)
inline

Accesses the element at the given row and column.

Parameters
iThe row index.
jThe column index.
Returns
A reference to the element at position (i, j).

◆ at() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
const Type & theoretica::mat< Type, N, K >::at ( unsigned int  i,
unsigned int  j 
) const
inline

Accesses the element at the given row and column.

Parameters
iThe row index.
jThe column index.
Returns
A constant reference to the element at position (i, j).

◆ begin() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
auto theoretica::mat< Type, N, K >::begin ( )
inline

Returns an iterator to the first element of the matrix.

Returns
An iterator to the beginning of the matrix.

◆ begin() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
auto theoretica::mat< Type, N, K >::begin ( ) const
inline

Returns a const iterator to the first element of the matrix.

Returns
A const iterator to the beginning of the matrix.

◆ cols()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
TH_CONSTEXPR unsigned int theoretica::mat< Type, N, K >::cols ( ) const
inline

Returns the number of columns in the matrix.

Returns
The number of columns.

◆ end() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
auto theoretica::mat< Type, N, K >::end ( )
inline

Returns an iterator to one past the last element of the matrix.

Returns
An iterator to the end of the matrix.

◆ end() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
auto theoretica::mat< Type, N, K >::end ( ) const
inline

Returns a const iterator to one past the last element of the matrix.

Returns
A const iterator to the end of the matrix.

◆ get()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
Type theoretica::mat< Type, N, K >::get ( unsigned int  i,
unsigned int  j 
) const
inline

Gets the element at the specified row and column.

Parameters
iThe row index.
jThe column index.
Returns
A copy of the element at position (i, j).

◆ invert()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > & theoretica::mat< Type, N, K >::invert ( )
inline

Inverts the matrix in place.

Returns
Reference to the inverted matrix.

This function is only valid for square matrices.

◆ mul() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<unsigned int M>
mat< Type, N, M > theoretica::mat< Type, N, K >::mul ( const mat< Type, K, M > &  B) const
inline

Matrix multiplication for matrices with different column counts.

Template Parameters
MThe number of columns in matrix B.
Parameters
BThe matrix to multiply with.
Returns
A new matrix resulting from the multiplication.

◆ mul() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
Matrix theoretica::mat< Type, N, K >::mul ( const Matrix B) const
inline

Matrix multiplication for matrices with any type.

Template Parameters
MatrixThe type of the matrix to multiply with.
Parameters
BThe matrix to multiply with.
Returns
The resulting matrix.

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.

◆ operator!=()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
bool theoretica::mat< Type, N, K >::operator!= ( const Matrix other) const
inline

Checks whether this matrix is not equal to another matrix element-wise.

Template Parameters
MatrixThe type of the other matrix.
Parameters
otherThe matrix to compare with.
Returns
true if any elements are unequal, false otherwise.

◆ operator()() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
Type & theoretica::mat< Type, N, K >::operator() ( unsigned int  i,
unsigned int  j 
)
inline

Overloads the () operator to access an element.

Parameters
iThe row index.
jThe column index.
Returns
A reference to the element at position (i, j).

◆ operator()() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
const Type & theoretica::mat< Type, N, K >::operator() ( unsigned int  i,
unsigned int  j 
) const
inline

Overloads the () operator to access an element.

Parameters
iThe row index.
jThe column index.
Returns
A constant reference to the element at position (i, j).

◆ operator*() [1/3]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
auto theoretica::mat< Type, N, K >::operator* ( const Matrix B) const
inline

Overloads the * operator for matrix multiplication.

Template Parameters
MatrixThe type of the matrix to multiply with.
Parameters
BThe matrix to multiply with.
Returns
The resulting matrix.

◆ operator*() [2/3]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
vec< Type, N > theoretica::mat< Type, N, K >::operator* ( const vec< Type, K > &  v) const
inline

Overloads the * operator to transform a fixed-size vector by the matrix.

Parameters
vThe vector to transform.
Returns
The transformed vector.

◆ operator*() [3/3]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > theoretica::mat< Type, N, K >::operator* ( Type  scalar) const
inline

Scalar multiplication.

Parameters
scalarThe scalar value to multiply.
Returns
A new matrix that is the result of multiplying this matrix by scalar.

◆ operator*=() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator*= ( const Matrix B)
inline

Matrix multiplication with an assignment operator.

Template Parameters
MatrixThe type of the matrix to multiply with.
Parameters
BThe matrix to multiply with.
Returns
Reference to this matrix after multiplication.

◆ operator*=() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator*= ( Type  scalar)
inline

Scalar multiplication.

Parameters
scalarThe scalar value to multiply with.
Returns
Reference to this matrix after multiplication.

◆ operator+()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > theoretica::mat< Type, N, K >::operator+ ( const Matrix other) const
inline

Matrix addition.

Template Parameters
MatrixThe type of the matrix to add.
Parameters
otherThe matrix to add.
Returns
A new matrix that is the result of adding other to this matrix.

◆ operator+=()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator+= ( const Matrix other)
inline

Matrix addition.

Template Parameters
MatrixThe type of the matrix to add.
Parameters
otherThe matrix to add to this matrix.
Returns
Reference to this matrix after addition.

◆ operator-()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > theoretica::mat< Type, N, K >::operator- ( const Matrix other) const
inline

Matrix subtraction.

Template Parameters
MatrixThe type of the matrix to subtract.
Parameters
otherThe matrix to subtract.
Returns
A new matrix that is the result of subtracting other from this matrix.

◆ operator-=()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator-= ( const Matrix other)
inline

Matrix subtraction.

Template Parameters
MatrixThe type of the matrix to subtract.
Parameters
otherThe matrix to subtract from this matrix.
Returns
Reference to this matrix after subtraction.

◆ operator/()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > theoretica::mat< Type, N, K >::operator/ ( Type  scalar) const
inline

Scalar division.

Parameters
scalarThe scalar divisor.
Returns
A new matrix that is the result of dividing this matrix by scalar.

If scalar is close to zero, an error is raised.

◆ operator/=()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator/= ( Type  scalar)
inline

Scalar division.

Parameters
scalarThe scalar value to divide by.
Returns
Reference to this matrix after division.

If the scalar value is close to zero, this function raises a division by zero error.

◆ operator=()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
mat< Type, N, K > & theoretica::mat< Type, N, K >::operator= ( const Matrix other)
inline

Assignment operator to copy from another matrix.

Template Parameters
MatrixThe type of the matrix to copy from.
Parameters
otherThe matrix to copy.
Returns
Reference to this matrix after assignment.

◆ operator==()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Matrix >
bool theoretica::mat< Type, N, K >::operator== ( const Matrix other) const
inline

Checks whether this matrix is equal to another matrix element-wise.

Template Parameters
MatrixThe type of the other matrix.
Parameters
otherThe matrix to compare with.
Returns
true if all elements are equal, false otherwise.

◆ 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.

◆ rows()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
TH_CONSTEXPR unsigned int theoretica::mat< Type, N, K >::rows ( ) const
inline

Returns the number of rows in the matrix.

Returns
The number of rows.

◆ size()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
unsigned int theoretica::mat< Type, N, K >::size ( ) const
inline

Returns the total number of elements in the matrix.

Returns
The total number of elements (rows * columns).

◆ to_string()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
std::string theoretica::mat< Type, N, K >::to_string ( std::string  separator = ", ",
bool  parenthesis = true 
) const
inline

Converts the matrix to a string representation.

Parameters
separatorSeparator between elements (default is ", ").
parenthesisWhether to enclose each row in parentheses (default is true).
Returns
The matrix as a formatted string.

◆ transform() [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
vec< Type, N > theoretica::mat< Type, N, K >::transform ( const vec< Type, K > &  v) const
inline

Transforms a fixed-size vector by multiplying it with the matrix.

Parameters
vThe vector to transform.
Returns
The transformed vector as a new vec<Type, N>.

◆ transform() [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename Vector >
Vector theoretica::mat< Type, N, K >::transform ( const Vector v) const
inline

Transforms a vector by multiplying it with the matrix.

Template Parameters
VectorThe type of the vector to transform.
Parameters
vThe vector to transform.
Returns
The transformed vector.

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.

◆ transpose()

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > & theoretica::mat< Type, N, K >::transpose ( )
inline

Transposes the matrix in place.

Returns
Reference to this matrix after transposition.

This function only works if the matrix is square. An assertion will trigger if the matrix is not square.

Friends And Related Symbol Documentation

◆ operator* [1/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
template<typename VecType , unsigned int M>
vec< VecType, K > operator* ( const vec< VecType, M > &  a,
const mat< Type, N, K > &  B 
)
friend

Friend operator for vector-matrix multiplication.

Template Parameters
VecTypeThe type of vector elements.
MThe number of elements in the vector.
Parameters
aThe vector to multiply.
BThe matrix to be multiplied by.
Returns
The resulting vector after multiplication.

◆ operator* [2/2]

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
mat< Type, N, K > operator* ( Type  a,
const mat< Type, N, K > &  B 
)
friend

Friend operator for scalar multiplication (T * mat).

Parameters
aThe scalar multiplier.
BThe matrix to be multiplied.
Returns
A new matrix that is the result of multiplying B by a.

◆ operator<<

template<typename Type = real, unsigned int N = 0, unsigned int K = 0>
std::ostream & operator<< ( std::ostream &  out,
const mat< Type, N, K > &  obj 
)
friend

Outputs the matrix to an output stream in string format.

Parameters
outThe output stream.
objThe matrix to output.
Returns
The modified output stream.

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