Base Framework
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Related Functions | List of all members
Matrix< TYPE > Class Template Reference

Matrix. More...

#include <base/math/Matrix.h>

Inherits Object.

Classes

class  ElementReference
 Reference to an element within a matrix. More...
 
class  IncompatibleMatrices
 Raised if an operation is given incompatible matrices to work on. More...
 
class  MatrixException
 Exception raised by the Matrix class. More...
 
class  NotSquare
 Raised by some operations if matrix is non-square. More...
 
class  Row
 
class  RowReference
 Reference to a row within a matrix. More...
 
class  Singular
 Raised by some operations if matrix is singular. More...
 

Public Member Functions

 Matrix () noexcept
 
 Matrix (const Dimension &dimension)
 
 Matrix (const TYPE elements[], const Dimension &dimension)
 
 Matrix (Enumerator< ReadEnumeratorTraits< TYPE > > &diagonal, const Dimension &dimension)
 
 Matrix (const Matrix &copy) noexcept
 
Matrixoperator= (const Matrix &assign)
 
Dimension getDimension () const noexcept
 
unsigned int getRows () const noexcept
 
unsigned int getColumns () const noexcept
 
bool isEmpty () const noexcept
 
bool isSquare () const noexcept
 
bool isEqual (const Matrix &value) const noexcept
 
bool isZero () const noexcept
 
bool isDiagonal () const noexcept
 
bool isLowerTriangular () const noexcept
 
bool isUpperTriangular () const noexcept
 
bool isSymmetric () const noexcept
 
TYPE getNorm () const noexcept
 
TYPE getDeterminant () const
 
Matrix getMinor (unsigned int row, unsigned int column) const
 
TYPE getCofactor (unsigned int row, unsigned int column) const
 
Matrix getAdjoint () const
 
Matrix solve (const Matrix< TYPE > &value) const
 
const TYPE & getAt (unsigned int row, unsigned int colum) const
 
void setAt (unsigned int row, unsigned int column, const TYPE &value)
 
const TYPE & operator() (unsigned int row, unsigned int column) const
 
RowReference operator[] (unsigned int row)
 
Matrix plus () const noexcept
 
Matrix minus () const noexcept
 
Matrix invert () const
 
Matrix transpose () const noexcept
 
Matrixclear ()
 
Matrixidentity ()
 
MatrixzeroAdjust (const TYPE &zero)
 
MatrixrowEchelon ()
 
Matrixpivot (unsigned int row, unsigned int column)
 
Matrixclip (unsigned int rows, unsigned int columns)
 
Matrixnegate ()
 
Matrixadd (const Matrix &value)
 
Matrixsubtract (const Matrix &value)
 
Matrixmultiply (const TYPE &value)
 
Matrixdivide (const TYPE &value)
 
Matrixnegate (const Matrix &value)
 
Matrixadd (const Matrix &left, const Matrix &right)
 
Matrixsubtract (const Matrix &left, const Matrix &right)
 
Matrixmultiply (const Matrix< TYPE > &left, const Matrix< TYPE > &right)
 
Matrixmultiply (const Matrix &left, const TYPE &right)
 
Matrixmultiply (const TYPE &left, const Matrix &right)
 
Matrixdivide (const Matrix &left, const TYPE &right)
 
Matrixtranspose (const Matrix &matrix)
 
bool operator== (const Matrix &value) const noexcept
 
Matrixoperator+= (const Matrix &value)
 
Matrixoperator-= (const Matrix &value)
 
Matrixoperator*= (const TYPE &value)
 
Matrixoperator/= (const TYPE &value)
 
Matrix operator+ () const
 
Matrix operator- () const
 

Protected Member Functions

TYPE * getMutableElements ()
 
TYPE * getElements ()
 
const TYPE * getReadOnlyElements () const noexcept
 
const TYPE * getElements () const noexcept
 
bool isCompatible (const Matrix &value) const noexcept
 
void setSize (unsigned int rows, unsigned int columns)
 
void setDimension (const Matrix &value)
 
MemorySize getSize () const noexcept
 
void validateElement (unsigned int row, unsigned int column) const
 
void validateRow (unsigned int row) const
 
void validateColumn (unsigned int column) const
 
unsigned int getIndexOfElement (unsigned int row, unsigned int column) const noexcept
 

Protected Attributes

Reference< ReferenceCountedAllocator< TYPE > > elements
 
unsigned int rows = 0
 
unsigned int columns = 0
 

Related Functions

(Note that these are not member functions.)

template<class TYPE >
bool operator== (const Matrix< TYPE > &left, const Matrix< TYPE > &right) noexcept
 
template<class TYPE >
Matrix< TYPE > operator+ (const Matrix< TYPE > &left, const Matrix< TYPE > &right)
 
template<class TYPE >
Matrix< TYPE > operator- (const Matrix< TYPE > &left, const Matrix< TYPE > &right)
 
template<class TYPE >
Matrix< TYPE > operator* (const Matrix< TYPE > &left, const Matrix< TYPE > &right)
 
template<class TYPE >
Matrix< TYPE > operator* (const Matrix< TYPE > &left, const TYPE &right)
 
template<class TYPE >
Matrix< TYPE > operator* (const TYPE &left, const Matrix< TYPE > &right)
 
template<class TYPE >
Matrix< TYPE > operator/ (const Matrix< TYPE > &left, const Matrix< TYPE > &right)
 
template<class TYPE >
Matrix< TYPE > operator/ (const Matrix< TYPE > &left, const TYPE &right)
 
template<class TYPE >
Matrix< TYPE > operator/ (const TYPE &left, const Matrix< TYPE > &right)
 

Detailed Description

template<class TYPE>
class Matrix< TYPE >

Matrix.

Matrix implementation.

Version
1.10

Constructor & Destructor Documentation

◆ Matrix() [1/5]

template<class TYPE >
Matrix< TYPE >::Matrix ( )
inlinenoexcept

Initializes a matrix with no elements.

◆ Matrix() [2/5]

template<class TYPE >
Matrix< TYPE >::Matrix ( const Dimension dimension)
explicit

Initializes matrix with the specified dimension.

Parameters
dimensionThe desired dimension of the matrix.

◆ Matrix() [3/5]

template<class TYPE >
Matrix< TYPE >::Matrix ( const TYPE  elements[],
const Dimension dimension 
)

Initializes matrix using the elements of an array. The elements are expected to be ordered from left to right and top to bottom.

Parameters
elementsThe desired elements of the matrix.
dimensionThe desired dimension of the matrix.

◆ Matrix() [4/5]

template<class TYPE >
Matrix< TYPE >::Matrix ( Enumerator< ReadEnumeratorTraits< TYPE > > &  diagonal,
const Dimension dimension 
)

Initializes matrix as a diagonal matrix with the diagonal elements provided by the specified enumerator. Raises OutOfDomain if enumeration is empty.

Parameters
diagonalThe enumerator containing the desired diagonal elements.
dimensionThe desired dimension of the matrix.

◆ Matrix() [5/5]

template<class TYPE >
Matrix< TYPE >::Matrix ( const Matrix< TYPE > &  copy)
inlinenoexcept

Initializes matrix from other matrix.

Parameters
matrixThe matrix to be copied.

Member Function Documentation

◆ add() [1/2]

template<class TYPE >
Matrix& Matrix< TYPE >::add ( const Matrix< TYPE > &  left,
const Matrix< TYPE > &  right 
)

Sets this matrix to the sum of the specified matrices.

Parameters
leftMatrix.
rightMatrix.

◆ add() [2/2]

template<class TYPE >
Matrix& Matrix< TYPE >::add ( const Matrix< TYPE > &  value)

Adds the specified matrix to this matrix.

Parameters
valueThe matrix to be added.

◆ clear()

template<class TYPE >
Matrix& Matrix< TYPE >::clear ( )

Sets this matrix to the zero matrix.

◆ clip()

template<class TYPE >
Matrix& Matrix< TYPE >::clip ( unsigned int  rows,
unsigned int  columns 
)

Throws away any elements that exceed the specified dimension.

Parameters
rowsThe desired number of row.
columnsThe desired number of columns.

◆ divide() [1/2]

template<class TYPE >
Matrix& Matrix< TYPE >::divide ( const Matrix< TYPE > &  left,
const TYPE &  right 
)

Sets this matrix to the result of the division of the left by right operand.

Parameters
leftThe left operand.
rightThe right operand.

◆ divide() [2/2]

template<class TYPE >
Matrix& Matrix< TYPE >::divide ( const TYPE &  value)

Divides this matrix with the specified value.

Parameters
valueThe divisor.

◆ getAdjoint()

template<class TYPE >
Matrix Matrix< TYPE >::getAdjoint ( ) const

Returns the adjoint of this matrix.

◆ getAt()

template<class TYPE >
const TYPE& Matrix< TYPE >::getAt ( unsigned int  row,
unsigned int  colum 
) const

Returns an enumeration of all the elements in the specified row.

Parameters
rowThe desired row to enumerate. Returns a read-only enumeration of all the elements in the specified row.
rowThe desired row to enumerate. Returns an enumeration of all the elements in the specified column.
columnThe desired column to enumerate. Returns a read-only enumeration of all the elements in the specified column.
columnThe desired column to enumerate. Returns an enumeration of all the elements of the diagonal. Returns a read-only enumeration of all the elements of the diagonal. Returns the element at the specified position.
rowThe row of the element.
columnThe column of the element.

◆ getCofactor()

template<class TYPE >
TYPE Matrix< TYPE >::getCofactor ( unsigned int  row,
unsigned int  column 
) const

Returns the cofactor of this matrix for the specified element.

Parameters
rowThe row of the element.
columnThe column of the element.

◆ getColumns()

template<class TYPE >
unsigned int Matrix< TYPE >::getColumns ( ) const
inlinenoexcept

Returns the number of columns of this matrix.

◆ getDeterminant()

template<class TYPE >
TYPE Matrix< TYPE >::getDeterminant ( ) const

Returns the determinant of this matrix.

◆ getDimension()

template<class TYPE >
Dimension Matrix< TYPE >::getDimension ( ) const
inlinenoexcept

Returns the dimension of the matrix.

◆ getIndexOfElement()

template<class TYPE >
unsigned int Matrix< TYPE >::getIndexOfElement ( unsigned int  row,
unsigned int  column 
) const
inlineprotectednoexcept

Converts 2D index to 1D index in the element array. Does NOT check boundaries.

◆ getMinor()

template<class TYPE >
Matrix Matrix< TYPE >::getMinor ( unsigned int  row,
unsigned int  column 
) const

Returns the minor of this matrix.

Parameters
rowThe row to be discarded.
columnThe column to be discarded.

◆ getMutableElements()

template<class TYPE >
TYPE* Matrix< TYPE >::getMutableElements ( )
inlineprotected

Returns the elements of the matrix for modification. May copy all elements of the matrix.

◆ getNorm()

template<class TYPE >
TYPE Matrix< TYPE >::getNorm ( ) const
noexcept

Returns the Hilbert-Schmidt norm of the matrix.

◆ getReadOnlyElements()

template<class TYPE >
const TYPE* Matrix< TYPE >::getReadOnlyElements ( ) const
inlineprotectednoexcept

Returns the elements of the matrix for read-only.

◆ getRows()

template<class TYPE >
unsigned int Matrix< TYPE >::getRows ( ) const
inlinenoexcept

Returns the number of rows of this matrix.

◆ getSize()

template<class TYPE >
MemorySize Matrix< TYPE >::getSize ( ) const
inlineprotectednoexcept

Gets the number of elements in the matrix.

◆ identity()

template<class TYPE >
Matrix& Matrix< TYPE >::identity ( )

Sets this matrix to the identity matrix.

◆ invert()

template<class TYPE >
Matrix Matrix< TYPE >::invert ( ) const

Unary inversion.

◆ isCompatible()

template<class TYPE >
bool Matrix< TYPE >::isCompatible ( const Matrix< TYPE > &  value) const
inlineprotectednoexcept

Returns true if this matrix and the specified matrix have identical dimensions.

◆ isDiagonal()

template<class TYPE >
bool Matrix< TYPE >::isDiagonal ( ) const
noexcept

Returns true if this matrix is a diagonal matrix.

Returns
false if matrix is not square or contains no elements.

◆ isEmpty()

template<class TYPE >
bool Matrix< TYPE >::isEmpty ( ) const
inlinenoexcept

Returns true if the matrix is empty (i.e. has no elements).

◆ isEqual()

template<class TYPE >
bool Matrix< TYPE >::isEqual ( const Matrix< TYPE > &  value) const
noexcept

Returns true if the matrices are equal.

◆ isLowerTriangular()

template<class TYPE >
bool Matrix< TYPE >::isLowerTriangular ( ) const
noexcept

Returns true if this matrix is lower triangular (i.e. the matrix has no non-zero values above the diagonal line).

Returns
false if matrix is not square or contains no elements.

◆ isSquare()

template<class TYPE >
bool Matrix< TYPE >::isSquare ( ) const
inlinenoexcept

Returns true if the matrix is a square matrix.

◆ isSymmetric()

template<class TYPE >
bool Matrix< TYPE >::isSymmetric ( ) const
noexcept

Returns true if this matrix is symmetric.

Returns
false if matrix is not square or contains no elements.

◆ isUpperTriangular()

template<class TYPE >
bool Matrix< TYPE >::isUpperTriangular ( ) const
noexcept

Returns true if this matrix is upper triangular (i.e. the matrix has no non-zero values below the diagonal line).

Returns
false if matrix is not square or contains no elements.

◆ isZero()

template<class TYPE >
bool Matrix< TYPE >::isZero ( ) const
noexcept

Returns true if the matrix is the zero matrix.

Returns
false if matrix contains no elements.

◆ minus()

template<class TYPE >
Matrix Matrix< TYPE >::minus ( ) const
noexcept

Unary minus.

◆ multiply() [1/4]

template<class TYPE >
Matrix& Matrix< TYPE >::multiply ( const Matrix< TYPE > &  left,
const TYPE &  right 
)

Sets this matrix to the product of the operands.

Parameters
leftThe left operand.
rightThe right operand.

◆ multiply() [2/4]

template<class TYPE >
Matrix& Matrix< TYPE >::multiply ( const Matrix< TYPE > &  left,
const Matrix< TYPE > &  right 
)

Sets this matrix to the product of the specified matrices.

Parameters
leftMatrix.
rightMatrix.

◆ multiply() [3/4]

template<class TYPE >
Matrix& Matrix< TYPE >::multiply ( const TYPE &  left,
const Matrix< TYPE > &  right 
)

Sets this matrix to the product of the operands.

Parameters
leftThe left operand.
rightThe right operand.

◆ multiply() [4/4]

template<class TYPE >
Matrix& Matrix< TYPE >::multiply ( const TYPE &  value)

Multiplies this matrix with the specified value.

Parameters
valueThe multiplicator.

◆ negate() [1/2]

template<class TYPE >
Matrix& Matrix< TYPE >::negate ( )

Negates this matrix.

◆ negate() [2/2]

template<class TYPE >
Matrix& Matrix< TYPE >::negate ( const Matrix< TYPE > &  value)

Negates the specified matrix and stores the result in this matrix.

◆ operator()()

template<class TYPE >
const TYPE& Matrix< TYPE >::operator() ( unsigned int  row,
unsigned int  column 
) const
inline

Returns the element at the specified position.

◆ operator*=()

template<class TYPE >
Matrix& Matrix< TYPE >::operator*= ( const TYPE &  value)
inline

Multiplies this matrix with the specified value.

Parameters
valueThe multiplicator.

◆ operator+()

template<class TYPE >
Matrix Matrix< TYPE >::operator+ ( ) const
inline

Unary plus.

◆ operator+=()

template<class TYPE >
Matrix& Matrix< TYPE >::operator+= ( const Matrix< TYPE > &  value)
inline

Adds the specified matrix from this matrix.

Parameters
valueThe value to be added.

◆ operator-()

template<class TYPE >
Matrix Matrix< TYPE >::operator- ( ) const
inline

Unary minus.

◆ operator-=()

template<class TYPE >
Matrix& Matrix< TYPE >::operator-= ( const Matrix< TYPE > &  value)
inline

Subtracts the specified matrix from this matrix.

Parameters
valueThe value to be subtracted.

◆ operator/=()

template<class TYPE >
Matrix& Matrix< TYPE >::operator/= ( const TYPE &  value)
inline

Divides this matrix with the specified value.

Parameters
valueThe divisor.

◆ operator=()

template<class TYPE >
Matrix& Matrix< TYPE >::operator= ( const Matrix< TYPE > &  assign)
inline

Assigns matrix to this matrix.

Parameters
eqThe matrix containing the desired elements.

◆ operator==()

template<class TYPE >
bool Matrix< TYPE >::operator== ( const Matrix< TYPE > &  value) const
inlinenoexcept

Returns true if the matrices are equal element by element. This member should only be used for integer types.

Parameters
valueMatrix to be compared.

◆ operator[]()

template<class TYPE >
RowReference Matrix< TYPE >::operator[] ( unsigned int  row)
inline

Returns a reference to the specified row.

◆ pivot()

template<class TYPE >
Matrix& Matrix< TYPE >::pivot ( unsigned int  row,
unsigned int  column 
)

Does a reduced row echelon transformation of this matrix for the specified element. The element value is transformed to 1 and the other values of the column are transformed to 0.

Parameters
rowThe row of the element.
columnThe column of the element.

◆ plus()

template<class TYPE >
Matrix Matrix< TYPE >::plus ( ) const
noexcept

Unary plus.

◆ rowEchelon()

template<class TYPE >
Matrix& Matrix< TYPE >::rowEchelon ( )

Does a row echelon transformation of this matrix.

◆ setAt()

template<class TYPE >
void Matrix< TYPE >::setAt ( unsigned int  row,
unsigned int  column,
const TYPE &  value 
)

Sets the element at the specified position.

Parameters
rowThe row of the element.
columnThe column of the element.
valueThe desired value.

◆ setDimension()

template<class TYPE >
void Matrix< TYPE >::setDimension ( const Matrix< TYPE > &  value)
inlineprotected

Sets the dimension of this matrix to the dimension of the specified matrix.

◆ setSize()

template<class TYPE >
void Matrix< TYPE >::setSize ( unsigned int  rows,
unsigned int  columns 
)
inlineprotected

Sets the size of the matrix. Only invocated by constructors.

◆ solve()

template<class TYPE >
Matrix Matrix< TYPE >::solve ( const Matrix< TYPE > &  value) const

Returns the solution(s) for the linear system(s).

Parameters
valueThe constant column vector(s).

◆ subtract() [1/2]

template<class TYPE >
Matrix& Matrix< TYPE >::subtract ( const Matrix< TYPE > &  left,
const Matrix< TYPE > &  right 
)

Sets this matrix to the difference of the specified matrices.

Parameters
leftMatrix.
rightMatrix.

◆ subtract() [2/2]

template<class TYPE >
Matrix& Matrix< TYPE >::subtract ( const Matrix< TYPE > &  value)

Subtracts the specified matrix from this matrix.

Parameters
valueThe matrix to be subtracted.

◆ transpose() [1/2]

template<class TYPE >
Matrix Matrix< TYPE >::transpose ( ) const
noexcept

Returns a new matrix that is the transpose of this matrix.

◆ transpose() [2/2]

template<class TYPE >
Matrix& Matrix< TYPE >::transpose ( const Matrix< TYPE > &  matrix)

Sets this matrix to the transpose of the specified matrix.

Parameters
matrixThe matrix to be transposed.

◆ validateColumn()

template<class TYPE >
void Matrix< TYPE >::validateColumn ( unsigned int  column) const
inlineprotected

Raises OutOfRange if column is invalid.

◆ validateElement()

template<class TYPE >
void Matrix< TYPE >::validateElement ( unsigned int  row,
unsigned int  column 
) const
inlineprotected

Raises OutOfRange if element coordinate (row, column) is invalid.

◆ validateRow()

template<class TYPE >
void Matrix< TYPE >::validateRow ( unsigned int  row) const
inlineprotected

Raises OutOfRange if row is invalid.

◆ zeroAdjust()

template<class TYPE >
Matrix& Matrix< TYPE >::zeroAdjust ( const TYPE &  zero)

Pivot transformation of this matrix. Solves the equations. Sets the values of this matrix that exceed the specified value to zero (0).

Parameters
zeroSpecifies the zero limit.

Member Data Documentation

◆ columns

template<class TYPE >
unsigned int Matrix< TYPE >::columns = 0
protected

The number of columns in the matrix.

◆ elements

template<class TYPE >
Reference<ReferenceCountedAllocator<TYPE> > Matrix< TYPE >::elements
protected

The elements of the matrix stored in an array. The array is guarantied to be non-empty when the matrix object has been initialized.

◆ rows

template<class TYPE >
unsigned int Matrix< TYPE >::rows = 0
protected

The number of rows in the matrix.