SpatiumLib
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
spatium::Matrix Class Reference

Mathematical matrix with an arbitrary number number of rows and columns. More...

#include <Matrix.h>

Inheritance diagram for spatium::Matrix:
Inheritance graph
[legend]
Collaboration diagram for spatium::Matrix:
Collaboration graph
[legend]

Public Member Functions

 Matrix (size_t rows, size_t cols)
 Constructor. More...
 
 Matrix (std::initializer_list< std::initializer_list< double >> array)
 Constructor. More...
 
bool operator== (const Matrix &other) const
 Compare operator. Is equal. More...
 
bool operator!= (const Matrix &other) const
 Compare operator. Is unequal. More...
 
size_t rows () const
 Get the number of rows. More...
 
size_t cols () const
 Get the number of columns. More...
 
std::vector< double > data () const
 Get all matrix elements. More...
 
void clear ()
 Clear all matrix elements. More...
 
Matrix transposed () const
 Get transposed matrix. More...
 
double determinant () const
 Calculate determinant. More...
 
double minor (size_t row, size_t col) const
 Calulcate minor. More...
 
Matrix omit (size_t row, size_t col) const
 Omit a given row and column. More...
 
Matrix inverse () const
 Calculate inverse of matrix. More...
 
double operator() (size_t row, size_t col) const
 Access element by value. More...
 
double & operator() (size_t row, size_t col)
 Access element by reference. More...
 
Matrix operator+ (const Matrix &other) const
 Add by matrix. More...
 
Matrix operator- (const Matrix &other) const
 Subtract by matrix. More...
 
Matrix operator * (const Matrix &other) const
 Multiply by matrix. More...
 
Matrix operator * (double scalar) const
 Multiply by scalar. More...
 
Matrix operator/ (double scalar) const
 Divide by scalar. More...
 
void resize (size_t rows, size_t cols)
 Resize. More...
 

Static Public Member Functions

static Matrix identity (size_t order)
 Construct identity matrix. More...
 

Protected Member Functions

double determinant (const Matrix &matrix) const
 Calculate determinant. More...
 

Protected Attributes

size_t m_rows
 
size_t m_cols
 
std::vector< double > m_data
 

Friends

std::ostream & operator<< (std::ostream &os, const Matrix &matrix)
 Output to ostream. More...
 

Detailed Description

Mathematical matrix with an arbitrary number number of rows and columns.

Matrix is a class to represent a mathematical matrix, i.e. a 2D array defined by a number of rows and columns. The content of the matrix is stored in column-major format.

Constructor & Destructor Documentation

◆ Matrix() [1/2]

spatium::Matrix::Matrix ( size_t  rows,
size_t  cols 
)
inline

Constructor.

Parameters
[in]rowsNumber of rows
[in]colsNumber of columns

◆ Matrix() [2/2]

spatium::Matrix::Matrix ( std::initializer_list< std::initializer_list< double >>  array)
inline

Constructor.

Parameters
[in]array2D Initializer list

Member Function Documentation

◆ clear()

void spatium::Matrix::clear ( )
inline

Clear all matrix elements.

◆ cols()

size_t spatium::Matrix::cols ( ) const
inline

Get the number of columns.

Returns
Number of columns

◆ data()

std::vector<double> spatium::Matrix::data ( ) const
inline

Get all matrix elements.

Returns
All matrix elements

◆ determinant() [1/2]

double spatium::Matrix::determinant ( ) const
inline

Calculate determinant.

The determinant is calculated through expansion by minors.

Exceptions
std::out_of_rangeMatrix is not square
Returns
Determinant

◆ determinant() [2/2]

double spatium::Matrix::determinant ( const Matrix matrix) const
inlineprotected

Calculate determinant.

Recursive function.

Returns
Determinant

◆ identity()

static Matrix spatium::Matrix::identity ( size_t  order)
inlinestatic

Construct identity matrix.

Returns
Identity matrix

◆ inverse()

Matrix spatium::Matrix::inverse ( ) const
inline

Calculate inverse of matrix.

Exceptions
std::out_of_rangeMatrix is not square
std::out_of_rangeMatrix has no inverse
Returns
Inverse of matrix

◆ minor()

double spatium::Matrix::minor ( size_t  row,
size_t  col 
) const
inline

Calulcate minor.

The minor is the determinant of the matrix formed by omitting a given row and column.

Parameters
[in]rowRow to omit
[out]colColumn to omit
Returns
Minor

◆ omit()

Matrix spatium::Matrix::omit ( size_t  row,
size_t  col 
) const
inline

Omit a given row and column.

Parameters
[in]rowRow to omit
[out]colColumn to omit
Returns
Matrix with omitted row and column

◆ operator *() [1/2]

Matrix spatium::Matrix::operator * ( const Matrix other) const
inline

Multiply by matrix.

Parameters
[in]otherMatrix to multiply with
Exceptions
std::out_of_rangeMatrix dimensions mismatch
Returns
Multiplied matrix

◆ operator *() [2/2]

Matrix spatium::Matrix::operator * ( double  scalar) const
inline

Multiply by scalar.

Parameters
[in]scalarScalar
Returns
Multiplied matrix

◆ operator!=()

bool spatium::Matrix::operator!= ( const Matrix other) const
inline

Compare operator. Is unequal.

Parameters
[in]otherOther matrix
Returns
True if unequal, otherwise false

◆ operator()() [1/2]

double spatium::Matrix::operator() ( size_t  row,
size_t  col 
) const
inline

Access element by value.

Parameters
[in]rowRow of element
[in]colColumn of element
Exceptions
std::out_of_rangeMatrix element out of range
Returns
Element value

◆ operator()() [2/2]

double& spatium::Matrix::operator() ( size_t  row,
size_t  col 
)
inline

Access element by reference.

Parameters
[in]rowRow of element
[in]colColumn of element
Exceptions
std::out_of_rangeMatrix element out of range
Returns
Element reference

◆ operator+()

Matrix spatium::Matrix::operator+ ( const Matrix other) const
inline

Add by matrix.

Parameters
[in]otherMatrix to add
Exceptions
std::out_of_rangeMatrix dimensions mismatch
Returns
Added matrix

◆ operator-()

Matrix spatium::Matrix::operator- ( const Matrix other) const
inline

Subtract by matrix.

Parameters
[in]otherMatrix to subtract
Exceptions
std::out_of_rangeMatrix dimensions mismatch
Returns
Subtracted matrix

◆ operator/()

Matrix spatium::Matrix::operator/ ( double  scalar) const
inline

Divide by scalar.

Parameters
[in]scalarScalar
Returns
Divided matrix

◆ operator==()

bool spatium::Matrix::operator== ( const Matrix other) const
inline

Compare operator. Is equal.

Equal matrices have equal dimensions and values.

Parameters
[in]otherOther matrix
Returns
True if equal, otherwise false

◆ resize()

void spatium::Matrix::resize ( size_t  rows,
size_t  cols 
)
inline

Resize.

This will set all values to 0.

Parameters
[in]rowsNumber of rows
[in]colsNumber of columns

◆ rows()

size_t spatium::Matrix::rows ( ) const
inline

Get the number of rows.

Returns
Number of rows

◆ transposed()

Matrix spatium::Matrix::transposed ( ) const
inline

Get transposed matrix.

Returns
Transposed matrix

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Matrix matrix 
)
friend

Output to ostream.

Member Data Documentation

◆ m_cols

size_t spatium::Matrix::m_cols
protected

◆ m_data

std::vector<double> spatium::Matrix::m_data
protected

◆ m_rows

size_t spatium::Matrix::m_rows
protected

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