GSI_Toolbox 1.0.0
A toolbox for Gas-Surface Interaction simulations
Loading...
Searching...
No Matches
Matrix< T > Class Template Reference

This class implements a matrix object used for linear algebra and vectorized operations. More...

#include <Matrix.h>

Public Member Functions

 Matrix ()
 Default constructor. Initializes an empty matrix.
 
 Matrix (long dim_x, long dim_y)
 Constructor that initializes the matrix with the specified dimensions.
 
 Matrix (long dim_x, long dim_y, std::initializer_list< T > l)
 Constructor that initializes the matrix with the specified dimensions and data.
 
 ~Matrix ()
 Destructor. Cleans up the allocated memory.
 
 Matrix (const Matrix &other)
 Copy constructor. Creates a copy of an existing Matrix object.
 
 Matrix (Matrix &&other)
 Move constructor. Moves the data from a given Matrix object to the new one.
 
T * getData ()
 
long get_rows () const
 
long get_cols () const
 
Matrixoperator= (const Matrix &other)
 Copy assignment operator. Copies the data from the given Matrix object.
 
Matrixoperator= (Matrix &&other)
 Move assignment operator. Moves the data from the given Matrix object.
 
Matrixoperator= (T constant)
 Constant assignment operator. Assigns all Matrix values to one constant.
 
Matrixoperator+= (const Matrix &other)
 Compound assignment operator (addition). Adds another Matrix to this Matrix.
 
Matrixoperator-= (const Matrix &other)
 Compound assignment operator (subtraction). Subtracts another Matrix from this Matrix.
 
Matrixoperator*= (const Matrix &other)
 Compound assignment operator (multiplication). Multiplies this Matrix with another Matrix.
 
Matrixoperator/= (const Matrix &other)
 Compound assignment operator (division). Divides this Matrix by another Matrix.
 
Matrixoperator+= (T other)
 Compound assignment operator (addition). Adds a scalar to this Matrix.
 
Matrixoperator-= (T other)
 Compound assignment operator (subtraction). Subtracts a scalar from this Matrix.
 
Matrixoperator*= (T other)
 Compound assignment operator (multiplication). Multiplies this Matrix by a scalar.
 
Matrixoperator/= (T other)
 Compound assignment operator (division). Divides this Matrix by a scalar.
 
T & operator() (const long i, const long j)
 Subscript operator for element access (read/write).
 
const T & operator() (const long i, const long j) const
 Subscript operator for element access (read-only).
 
T & operator() (const long n)
 Subscript operator for element access in a single row (read/write).
 
const T & operator() (const long n) const
 Subscript operator for element access in a single row (read-only).
 
Matrix operator+ (const Matrix &other)
 
Matrix operator- (const Matrix &other)
 
Matrix operator* (const Matrix &other)
 
Matrix operator/ (const Matrix &other)
 
Matrix operator* (T constant)
 
Matrix operator/ (T constant)
 
Matrix operator^ (T constant)
 
Matrix operator+ (T constant)
 
Matrix operator- (T constant)
 
Matrix dot (const Matrix &other)
 Calculates the dot product of two matrices.
 
Matrix cross (const Matrix &other)
 Calculates the cross product of two matrices.
 
Matrix outer (const Matrix &other)
 Calculates the outer product of two matrices.
 
std::tuple< Matrix< T >, Matrix< T > > meshgrid (const Matrix &other)
 Calculates the meshgrid of 2 1D matrices.
 
Matrix tr ()
 Transposes the matrix.
 
norm ()
 Calculates the Frobenius norm of the matrix.
 
max ()
 Finds the maximum element in the matrix.
 
min ()
 Finds the minimum element in the matrix.
 
void print ()
 Prints the matrix to the console.
 

Detailed Description

template<typename T>
class Matrix< T >

This class implements a matrix object used for linear algebra and vectorized operations.

The Matrix class provides a range of operations that can be performed on matrix objects, including addition, subtraction, multiplication, division, dot product, cross product, and various other operations common in linear algebra.

Constructor & Destructor Documentation

◆ Matrix() [1/4]

template<typename T >
Matrix< T >::Matrix ( long dim_x,
long dim_y )

Constructor that initializes the matrix with the specified dimensions.

The created matrix has the specified number of rows and columns, all set to zero.

Parameters
dim_xNumber of rows.
dim_yNumber of columns.

◆ Matrix() [2/4]

template<typename T >
Matrix< T >::Matrix ( long dim_x,
long dim_y,
std::initializer_list< T > l )

Constructor that initializes the matrix with the specified dimensions and data.

The created matrix has the specified number of rows and columns. The data for the matrix is provided by an initializer list. If the size of the list does not match the expected size of the matrix (rows * columns), an error message is displayed.

Parameters
dim_xNumber of rows.
dim_yNumber of columns.
lInitializer list containing data to populate the matrix.

◆ Matrix() [3/4]

template<typename T >
Matrix< T >::Matrix ( const Matrix< T > & other)

Copy constructor. Creates a copy of an existing Matrix object.

This constructor creates a new Matrix object with the same dimensions and copies all elements from the original matrix to the new one.

Parameters
otherThe Matrix object to copy from.

◆ Matrix() [4/4]

template<typename T >
Matrix< T >::Matrix ( Matrix< T > && other)

Move constructor. Moves the data from a given Matrix object to the new one.

This constructor "steals" the internal data from the original object and leaves it in an empty state. This is more efficient than copying when the original object is no longer needed.

Parameters
otherThe Matrix object whose data is to be moved.

Member Function Documentation

◆ cross()

template<typename T >
Matrix< T > Matrix< T >::cross ( const Matrix< T > & other)

Calculates the cross product of two matrices.

This function calculates the cross product, or vector product, of two matrices. Both matrices must be 3x1 (three rows, one column), otherwise, an exception is thrown.

Parameters
otherThe matrix with which to calculate the cross product.
Returns
A new Matrix object containing the result of the cross product.

◆ dot()

template<typename T >
Matrix< T > Matrix< T >::dot ( const Matrix< T > & other)

Calculates the dot product of two matrices.

This function calculates the dot product, or scalar product, of two matrices. Both matrices must have the same dimensions, otherwise, an exception is thrown.

Parameters
otherThe matrix with which to calculate the dot product.
Returns
A new Matrix object containing the result of the dot product.

◆ max()

template<typename T >
T Matrix< T >::max ( )

Finds the maximum element in the matrix.

This function searches for the largest element in the matrix.

Returns
The value of the largest element in the matrix.

◆ meshgrid()

template<typename T >
std::tuple< Matrix< T >, Matrix< T > > Matrix< T >::meshgrid ( const Matrix< T > & other)

Calculates the meshgrid of 2 1D matrices.

This function calculates the meshgrid matrix of two 1D vectors. Both vectors must be column vectors, otherwise, an exception is thrown.

Parameters
otherThe matrix with which to calculate the meshgrid.
Returns
A new Matrix object containing the result of the mehsgrid.

◆ min()

template<typename T >
T Matrix< T >::min ( )

Finds the minimum element in the matrix.

This function searches for the smallest element in the matrix.

Returns
The value of the smallest element in the matrix.

◆ norm()

template<typename T >
T Matrix< T >::norm ( )

Calculates the Frobenius norm of the matrix.

This function calculates the square root of the sum of the absolute squares of its elements.

Returns
The Frobenius norm of the matrix.

◆ operator()() [1/4]

template<typename T >
T & Matrix< T >::operator() ( const long i,
const long j )
inline

Subscript operator for element access (read/write).

This operator provides direct access to the matrix data using a two-dimensional index. It performs boundary checks and throws an exception if the indices are out of range.

Parameters
iThe row index.
jThe column index.
Returns
A reference to the element at the specified position.

◆ operator()() [2/4]

template<typename T >
const T & Matrix< T >::operator() ( const long i,
const long j ) const
inline

Subscript operator for element access (read-only).

This operator provides direct access to the matrix data using a two-dimensional index. It performs boundary checks and throws an exception if the indices are out of range.

Parameters
iThe row index.
jThe column index.
Returns
A constant reference to the element at the specified position.

◆ operator()() [3/4]

template<typename T >
T & Matrix< T >::operator() ( const long n)
inline

Subscript operator for element access in a single row (read/write).

This operator provides direct access to the matrix data considering it as a single row. It performs boundary checks and throws an exception if the index is out of range.

Parameters
nThe index in the matrix considering it as a single row.
Returns
A reference to the element at the specified position.

◆ operator()() [4/4]

template<typename T >
const T & Matrix< T >::operator() ( const long n) const
inline

Subscript operator for element access in a single row (read-only).

This operator provides direct access to the matrix data considering it as a single row. It performs boundary checks and throws an exception if the index is out of range.

Parameters
nThe index in the matrix considering it as a single row.
Returns
A constant reference to the element at the specified position.

◆ operator*=() [1/2]

template<typename T >
Matrix< T > & Matrix< T >::operator*= ( const Matrix< T > & other)

Compound assignment operator (multiplication). Multiplies this Matrix with another Matrix.

This operator performs matrix multiplication between this Matrix and the 'other' Matrix. The number of columns in this Matrix must be equal to the number of rows in the 'other' matrix, otherwise, an exception is thrown.

Parameters
otherThe Matrix object to be multiplied with this one.
Returns
A reference to the current object, to enable operator chaining.

◆ operator*=() [2/2]

template<typename T >
Matrix< T > & Matrix< T >::operator*= ( T other)

Compound assignment operator (multiplication). Multiplies this Matrix by a scalar.

This operator performs element-wise multiplication of this Matrix by a scalar value. Each element of this Matrix is multiplied by the scalar value.

Parameters
otherThe scalar value to multiply this Matrix's elements by.
Returns
A reference to the current object, to enable operator chaining.

◆ operator+=() [1/2]

template<typename T >
Matrix< T > & Matrix< T >::operator+= ( const Matrix< T > & other)

Compound assignment operator (addition). Adds another Matrix to this Matrix.

This operator performs element-wise addition between this Matrix and the 'other' Matrix. Both matrices must have the same dimensions, otherwise, an exception is thrown.

Parameters
otherThe Matrix object to be added to this one.
Returns
A reference to the current object, to enable operator chaining.

◆ operator+=() [2/2]

template<typename T >
Matrix< T > & Matrix< T >::operator+= ( T other)

Compound assignment operator (addition). Adds a scalar to this Matrix.

This operator performs element-wise addition of a scalar value to this Matrix. The scalar value is added to every element of this Matrix.

Parameters
otherThe scalar value to be added to this Matrix's elements.
Returns
A reference to the current object, to enable operator chaining.

◆ operator-=() [1/2]

template<typename T >
Matrix< T > & Matrix< T >::operator-= ( const Matrix< T > & other)

Compound assignment operator (subtraction). Subtracts another Matrix from this Matrix.

This operator performs element-wise subtraction of the 'other' Matrix from this Matrix. Both matrices must have the same dimensions, otherwise, an exception is thrown.

Parameters
otherThe Matrix object to be subtracted from this one.
Returns
A reference to the current object, to enable operator chaining.

◆ operator-=() [2/2]

template<typename T >
Matrix< T > & Matrix< T >::operator-= ( T other)

Compound assignment operator (subtraction). Subtracts a scalar from this Matrix.

This operator performs element-wise subtraction of a scalar value from this Matrix. The scalar value is subtracted from every element of this Matrix.

Parameters
otherThe scalar value to be subtracted from this Matrix's elements.
Returns
A reference to the current object, to enable operator chaining.

◆ operator/=() [1/2]

template<typename T >
Matrix< T > & Matrix< T >::operator/= ( const Matrix< T > & other)

Compound assignment operator (division). Divides this Matrix by another Matrix.

This operator performs element-wise division of this Matrix by the 'other' Matrix. Both matrices must have the same dimensions, and the 'other' matrix must not contain any zero elements, otherwise, an exception is thrown.

Parameters
otherThe Matrix object to divide this one by.
Returns
A reference to the current object, to enable operator chaining.

◆ operator/=() [2/2]

template<typename T >
Matrix< T > & Matrix< T >::operator/= ( T other)

Compound assignment operator (division). Divides this Matrix by a scalar.

This operator performs element-wise division of this Matrix by a scalar value. Each element of this Matrix is divided by the scalar value. The scalar must not be zero, otherwise, an exception is thrown.

Parameters
otherThe scalar value to divide this Matrix's elements by.
Returns
A reference to the current object, to enable operator chaining.

◆ operator=() [1/3]

template<typename T >
Matrix< T > & Matrix< T >::operator= ( const Matrix< T > & other)

Copy assignment operator. Copies the data from the given Matrix object.

This operator replaces the current Matrix's data with a copy of the data from the 'other' Matrix.

Parameters
otherThe Matrix object to copy data from.
Returns
A reference to the current object, to enable operator chaining.

◆ operator=() [2/3]

template<typename T >
Matrix< T > & Matrix< T >::operator= ( Matrix< T > && other)

Move assignment operator. Moves the data from the given Matrix object.

This operator "steals" the internal data from the 'other' Matrix, leaving it in an empty state.

Parameters
otherThe Matrix object to move data from.
Returns
A reference to the current object, to enable operator chaining.

◆ operator=() [3/3]

template<typename T >
Matrix< T > & Matrix< T >::operator= ( T constant)

Constant assignment operator. Assigns all Matrix values to one constant.

Parameters
constantThe constant to be assigned.
Returns
A reference to the current object, to enable operator chaining.

◆ outer()

template<typename T >
Matrix< T > Matrix< T >::outer ( const Matrix< T > & other)

Calculates the outer product of two matrices.

This function calculates the outer product of two 1D vectors. Both vectors must be column vectors, otherwise, an exception is thrown.

Parameters
otherThe matrix with which to calculate the outer product.
Returns
A new Matrix object containing the result of the outer product.

◆ print()

template<typename T >
void Matrix< T >::print ( )

Prints the matrix to the console.

This function prints a textual representation of the matrix to the console. Each row is printed on a new line, and the elements are separated by spaces.

◆ tr()

template<typename T >
Matrix< T > Matrix< T >::tr ( )

Transposes the matrix.

This function returns a new matrix which is the transpose of the current matrix.

Returns
A new Matrix object that is the transpose of the current matrix.

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