Library of Assembled Shared Sources
lass::num::Matrix< T, S > Class Template Reference

a dynamic sized n-dimensional matrix with expression templates More...

#include <matrix.h>

Inheritance diagram for lass::num::Matrix< T, S >:

Public Member Functions

 Matrix ()
 constructs an empty matrix
 
 Matrix (TSize rows, TSize columns)
 Construct a matrix of dimension rows x columns.
 
template<typename T2, typename S2>
 Matrix (const Matrix< T2, S2 > &other)
 assign storage/expression matrix to this (this should be a storage matrix).
 
template<typename T2, typename S2>
Matrix< T, S > & operator= (const Matrix< T2, S2 > &other)
 assign storage/expression matrix to this (this should be a storage matrix).
 
const TSize rows () const
 return number of rows in matrix.
 
const TSize columns () const
 return number of columns in matrix.
 
const TValue operator() (TSize row, TSize column) const
 return the component value at position (row, column)
 
TReference operator() (TSize row, TSize column)
 access the component value at position (row, column)
 
const TValue at (TSigned row, TSigned column) const
 return the component value at position (row, column) (row, column) will be wrapped to range [0, this->rows()) x [0, this->columns()] by using a modulus operator
 
TReference at (TSigned row, TSigned column)
 access the component value at position (row, column) (row, column) will be wrapped to range [0, this->rows()) x [0, this->columns()] by using a modulus operator
 
ConstRow row (TSigned index) const
 return a row of matrix.
 
Row row (TSigned index)
 return a row of matrix.
 
ConstColumn column (TSigned index) const
 return a column of matrix.
 
Column column (TSigned index)
 return a column of matrix.
 
const Matrix< T, S > & operator+ () const
 A weird way to get back the same object.
 
const Matrix< T, impl::MNeg< T, S > > operator- () const
 return a vector with all components negated (-a)(i, j) == -(a(i, j)).
 
template<typename S2>
Matrix< T, S > & operator+= (const Matrix< T, S2 > &other)
 componentswise addition assignment of two matrices
 
template<typename S2>
Matrix< T, S > & operator-= (const Matrix< T, S2 > &other)
 componentswise subtraction assignment of two matrices This method is equivalent to
this+=(-iB) .
 
Matrix< T, S > & operator*= (TParam scalar)
 scalar multiplication assignment of matrix
 
Matrix< T, S > & operator/= (TParam scalar)
 scalar multiplication assignment of matrix
 
void setZero (TSize rows, TSize columns)
 set this to a zero matrix of rows rows and columns columns.
 
void setIdentity (TSize size)
 set matrix to an identity matrix.
 
bool isEmpty () const
 return true if this is a (0�0) matrix
 
bool isZero () const
 test if this matrix equals a zero matrix.
 
bool isIdentity () const
 test if this matrix equals a identity matrix
 
bool isDiagonal () const
 test if this matrix is an diagonal matrix
 
bool isSquare () const
 return true if matrix has as many rows as columns
 
const Matrix< T, impl::MTrans< T, S > > transposed () const
 return transposed matrix
 
void invert ()
 replace matrix by its inverse.
 
void swap (Matrix< T, S > &other)
 swap two matrices
 

Related Symbols

(Note that these are not member symbols.)

template<typename T, typename S1, typename S2>
bool operator== (const Matrix< T, S1 > &a, const Matrix< T, S2 > &b)
 
template<typename T, typename S1, typename S2>
bool operator!= (const Matrix< T, S1 > &a, const Matrix< T, S2 > &b)
 
template<typename T, typename S1, typename S2>
const Matrix< T, impl::MAdd< T, S1, S2 > > operator+ (const Matrix< T, S1 > &a, const Matrix< T, S2 > &b)
 componentwise addition
 
template<typename T, typename S1, typename S2>
const Matrix< T, impl::MSub< T, S1, S2 > > operator- (const Matrix< T, S1 > &a, const Matrix< T, S2 > &b)
 componentwise addition
 
template<typename T, typename S1, typename S2>
const Matrix< T, impl::MProd< T, S1, S2 > > operator* (const Matrix< T, S1 > &a, const Matrix< T, S2 > &b)
 Matrix multiplication.
 
template<typename T, typename S>
const Matrix< T, impl::MAdd< T, impl::MScalar< T >, S > > operator+ (const T &a, const Matrix< T, S > &b)
 add a to all components of b
 
template<typename T, typename S>
const Matrix< T, impl::MSub< T, impl::MScalar< T >, S > > operator- (const T &a, const Matrix< T, S > &b)
 add a to all negated components of b
 
template<typename T, typename S>
const Matrix< T, impl::MMul< T, impl::MScalar< T >, S > > operator* (const T &a, const Matrix< T, S > &b)
 multiply a with all components of b
 
template<typename T, typename S>
const Matrix< T, impl::MAdd< T, S, impl::MScalar< T > > > operator+ (const Matrix< T, S > &a, typename Matrix< T, S >::TParam b)
 add b to all components of a
 
template<typename T, typename S>
const Matrix< T, impl::MAdd< T, S, impl::MScalar< T > > > operator- (const Matrix< T, S > &a, typename Matrix< T, S >::TParam b)
 subtract b from all components of a
 
template<typename T, typename S>
const Matrix< T, impl::MMul< T, S, impl::MScalar< T > > > operator* (const Matrix< T, S > &a, typename Matrix< T, S >::TParam b)
 multiply all components of a with b.
 
template<typename T, typename S>
const Matrix< T, impl::MMul< T, S, impl::MScalar< T > > > operator/ (const Matrix< T, S > &a, typename Matrix< T, S >::TParam b)
 divide all components of a by b.
 
template<typename T, typename S, typename S2>
void solve (const Matrix< T, S > &a, Matrix< T, S2 > &bx, bool improve)
 solve equation A * X = B
 

Detailed Description

template<typename T, typename S = impl::MStorage<T>>
class lass::num::Matrix< T, S >

a dynamic sized n-dimensional matrix with expression templates

Author
Bram de Greve [BdG]

Definition at line 69 of file matrix.h.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

template<typename T, typename S>
lass::num::Matrix< T, S >::Matrix ( )

constructs an empty matrix

Exception Safety:
strong guarentee.

Definition at line 84 of file matrix.inl.

Referenced by invert(), Matrix(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/=(), swap(), and transposed().

◆ Matrix() [2/3]

template<typename T, typename S>
lass::num::Matrix< T, S >::Matrix ( TSize rows,
TSize columns )
explicit

Construct a matrix of dimension rows x columns.

Can be used as default constructor for storage in containers.

Parameters
rowsthe number of rows of the matrix to be created.
columnsthe number of columns of the matrix to be created. By default both rows and columns are zero, what creates an empty matrix. Not very usefull though, except as place holder. So, it's safe to pass zero as arguments, but you shouldn't pass negative values.
Complexity:
O(rows * columns)
Exception Safety:
strong guarentee.

Definition at line 106 of file matrix.inl.

References columns(), and rows().

◆ Matrix() [3/3]

template<typename T, typename S>
template<typename T2, typename S2>
lass::num::Matrix< T, S >::Matrix ( const Matrix< T2, S2 > & other)

assign storage/expression matrix to this (this should be a storage matrix).

Precondition
this must be an l-value (storage matrix).
Complexity:
O(other.rows() * other.columns())
Exception Safety:
basic guarentee.

Definition at line 133 of file matrix.inl.

References columns(), LASS_META_ASSERT, Matrix(), and rows().

Member Function Documentation

◆ operator=()

template<typename T, typename S>
template<typename T2, typename S2>
Matrix< T, S > & lass::num::Matrix< T, S >::operator= ( const Matrix< T2, S2 > & other)

assign storage/expression matrix to this (this should be a storage matrix).

Precondition
this must be an l-value (storage matrix).
Complexity:
O(other.rows() * other.columns())
Exception Safety:
basic guarentee.

Definition at line 164 of file matrix.inl.

◆ rows()

template<typename T, typename S>
const Matrix< T, S >::TSize lass::num::Matrix< T, S >::rows ( ) const
inline

return number of rows in matrix.

Postcondition
should never return be a negative value.
Complexity:
O(1)
Exception safety:
no-fail

Definition at line 196 of file matrix.inl.

Referenced by at(), at(), invert(), isDiagonal(), isIdentity(), isZero(), Matrix(), Matrix(), operator()(), operator()(), operator*(), operator*(), operator*=(), operator+(), operator+(), operator+=(), operator-(), operator-(), operator-=(), operator/(), operator/=(), operator==(), row(), row(), setZero(), and solve().

◆ columns()

template<typename T, typename S>
const Matrix< T, S >::TSize lass::num::Matrix< T, S >::columns ( ) const
inline

return number of columns in matrix.

Postcondition
should never return be a negative value.
Complexity:
O(1)
Exception safety:
no-fail

Definition at line 215 of file matrix.inl.

Referenced by at(), at(), column(), column(), isDiagonal(), isIdentity(), isZero(), Matrix(), Matrix(), operator()(), operator()(), operator*(), operator*(), operator*=(), operator+=(), operator-=(), operator/(), operator/=(), operator==(), setZero(), and solve().

◆ operator()() [1/2]

template<typename T, typename S>
const Matrix< T, S >::TValue lass::num::Matrix< T, S >::operator() ( TSize row,
TSize column ) const
inline

return the component value at position (row, column)

Precondition
(row, column) shouldn't be out of the range [0, this->rows()) x [0, this->columns()], unless you're asking for trouble.

Definition at line 229 of file matrix.inl.

References column(), columns(), row(), and rows().

◆ operator()() [2/2]

template<typename T, typename S>
Matrix< T, S >::TReference lass::num::Matrix< T, S >::operator() ( TSize iRow,
TSize iColumn )
inline

access the component value at position (row, column)

Precondition
(row, column) shouldn't be out of the range [0, this->rows()) x [0, this->columns()], unless you're asking for trouble.
this must be an l-value (storage matrix).

Definition at line 246 of file matrix.inl.

References columns(), LASS_META_ASSERT, and rows().

◆ at()

template<typename T, typename S>
Matrix< T, S >::TReference lass::num::Matrix< T, S >::at ( TSigned row,
TSigned column )
inline

access the component value at position (row, column) (row, column) will be wrapped to range [0, this->rows()) x [0, this->columns()] by using a modulus operator

Precondition
this must be an l-value (storage matrix).

Definition at line 276 of file matrix.inl.

References column(), columns(), LASS_META_ASSERT, row(), and rows().

◆ row() [1/2]

template<typename T, typename S>
Matrix< T, S >::ConstRow lass::num::Matrix< T, S >::row ( TSigned index) const
inline

return a row of matrix.

index will be wrapped to range [0, this->rows()) by using a modulus operator

Definition at line 289 of file matrix.inl.

References rows().

Referenced by at(), at(), and operator()().

◆ row() [2/2]

template<typename T, typename S>
Matrix< T, S >::Row lass::num::Matrix< T, S >::row ( TSigned index)
inline

return a row of matrix.

index will be wrapped to range [0, this->rows()) by using a modulus operator. THIS MUST BE LVALUE (storage matrix).

Definition at line 302 of file matrix.inl.

References rows().

◆ column() [1/2]

template<typename T, typename S>
Matrix< T, S >::ConstColumn lass::num::Matrix< T, S >::column ( TSigned index) const
inline

return a column of matrix.

index will be wrapped to range [0, this->columns()) by using a modulus operator

Definition at line 314 of file matrix.inl.

References columns().

Referenced by at(), at(), invert(), operator()(), and solve().

◆ column() [2/2]

template<typename T, typename S>
Matrix< T, S >::Column lass::num::Matrix< T, S >::column ( TSigned index)
inline

return a column of matrix.

index will be wrapped to range [0, this->columns()) by using a modulus operator THIS MUST BE LVALUE (storage matrix).

Definition at line 327 of file matrix.inl.

References columns().

◆ operator+()

template<typename T, typename S>
const Matrix< T, S > & lass::num::Matrix< T, S >::operator+ ( ) const
inline

A weird way to get back the same object.

Complexity:
O(1)
Exception safety:
no-fail

Definition at line 343 of file matrix.inl.

References Matrix().

◆ operator-()

template<typename T, typename S>
const Matrix< T, impl::MNeg< T, S > > lass::num::Matrix< T, S >::operator- ( ) const

return a vector with all components negated (-a)(i, j) == -(a(i, j)).

Complexity:
O(1)
Exception safety:
no-fail

Definition at line 361 of file matrix.inl.

References Matrix().

◆ operator+=()

template<typename T, typename S>
template<typename S2>
Matrix< T, S > & lass::num::Matrix< T, S >::operator+= ( const Matrix< T, S2 > & other)

componentswise addition assignment of two matrices

Matrix Addition: Denote the sum of two matrices
A and
B (of the same dimensions) by
C=A+B. The sum is defined by adding entries with the same indices
Cij=Aij+Bij over all
i and
j.
http://mathworld.wolfram.com/MatrixAddition.html

This method implements the assignment addition what reduces to
Aij+=Bij over all
i and
j.

Precondition
this must be an l-value (storage matrix).
Complexity:
O(this->rows() * this->columns())
Exceptions
anexception is thrown if *this and iB are not of the same dimensions

Definition at line 388 of file matrix.inl.

References columns(), LASS_META_ASSERT, Matrix(), and rows().

◆ operator-=()

template<typename T, typename S>
template<typename S2>
Matrix< T, S > & lass::num::Matrix< T, S >::operator-= ( const Matrix< T, S2 > & other)

componentswise subtraction assignment of two matrices This method is equivalent to
this+=(-iB) .

See also
Matrix<T>::operator+=
Precondition
this must be an l-value (storage matrix).
Complexity:
O(this->rows() * this->columns())
Exceptions
anexception is thrown if *this and iB are not of the same dimensions

Definition at line 421 of file matrix.inl.

References columns(), LASS_META_ASSERT, Matrix(), and rows().

◆ operator*=()

template<typename T, typename S>
Matrix< T, S > & lass::num::Matrix< T, S >::operator*= ( TParam scalar)

scalar multiplication assignment of matrix

Precondition
this must be an l-value (storage matrix).
Complexity:
O(this->rows() * this->columns())

Definition at line 448 of file matrix.inl.

References columns(), LASS_META_ASSERT, Matrix(), and rows().

◆ operator/=()

template<typename T, typename S>
Matrix< T, S > & lass::num::Matrix< T, S >::operator/= ( TParam scalar)

scalar multiplication assignment of matrix

Precondition
this must be an l-value (storage matrix).

Definition at line 470 of file matrix.inl.

References columns(), LASS_META_ASSERT, Matrix(), and rows().

◆ setZero()

template<typename T, typename S>
void lass::num::Matrix< T, S >::setZero ( TSize rows,
TSize columns )

set this to a zero matrix of rows rows and columns columns.

See also
Matrix<T>::isZero
Precondition
this should be an lvalue
Complexity:
O(this->rows() * this->columns())

Definition at line 496 of file matrix.inl.

References columns(), LASS_META_ASSERT, and rows().

◆ setIdentity()

template<typename T, typename S>
void lass::num::Matrix< T, S >::setIdentity ( TSize iSize)

set matrix to an identity matrix.

See also
Matrix<T>::isIdentity
Precondition
this should be an lvalue
Complexity:
O(this->rows() * this->columns())

Definition at line 520 of file matrix.inl.

References LASS_META_ASSERT.

Referenced by invert().

◆ isEmpty()

template<typename T, typename S>
bool lass::num::Matrix< T, S >::isEmpty ( ) const

return true if this is a (0�0) matrix

Complexity:
O(1)
Exception safety:
no-fail

Definition at line 544 of file matrix.inl.

◆ isZero()

template<typename T, typename S>
bool lass::num::Matrix< T, S >::isZero ( ) const

test if this matrix equals a zero matrix.

A zero matrix is an
m�n matrix consisting of all 0s (MacDuffee 1943, p. 27), denoted
0. Zero matrices are sometimes also known as null matrices (Akivis and Goldberg 1972, p. 71).
http://mathworld.wolfram.com/ZeroMatrix.html

an empty matrix is also a zero matrix.

Complexity:
O(this->rows() * this->columns())

Definition at line 563 of file matrix.inl.

References columns(), and rows().

◆ isIdentity()

template<typename T, typename S>
bool lass::num::Matrix< T, S >::isIdentity ( ) const

test if this matrix equals a identity matrix

The identity matrix is a the simplest nontrivial diagonal matrix, defined such that
I(X)=X for all vectors
X. An identity matrix may be denoted
1,
I, or
E (the latter being an abbreviation for the German term "Einheitsmatrix"; Courant and Hilbert 1989, p. 7). Identity matrices are sometimes also known as unit matrices (Akivis and Goldberg 1972, p. 71). The
n�n identity matrix is given explicitly by
Iij=dij for
i,
j = 1, 2, ..., n, where
dij is the Kronecker delta.
http://mathworld.wolfram.com/IdentityMatrix.html

an empty matrix is also an identity matrix.

Complexity:
O(this->rows() * this->columns())

Definition at line 598 of file matrix.inl.

References columns(), isSquare(), and rows().

◆ isDiagonal()

template<typename T, typename S>
bool lass::num::Matrix< T, S >::isDiagonal ( ) const

test if this matrix is an diagonal matrix

A diagonal matrix is a square matrix
A of the form
Aij=Ci*dij where
dij is the Kronecker delta,
Ci are constants, and
i,
j = 1, 2, ..., n, with is no implied summation over indices.
http://mathworld.wolfram.com/DiagonalMatrix.html

Note
both square zero matrices and identity matrices will yield true on this one.
Complexity:
O(this->rows() * this->columns())

Definition at line 634 of file matrix.inl.

References columns(), isSquare(), and rows().

◆ isSquare()

template<typename T, typename S>
bool lass::num::Matrix< T, S >::isSquare ( ) const

return true if matrix has as many rows as columns

Complexity:
O(1)

Definition at line 663 of file matrix.inl.

Referenced by invert(), isDiagonal(), isIdentity(), and solve().

◆ transposed()

template<typename T, typename S>
const Matrix< T, impl::MTrans< T, S > > lass::num::Matrix< T, S >::transposed ( ) const

return transposed matrix

Complexity:
O(1)

Definition at line 677 of file matrix.inl.

References Matrix().

◆ invert()

template<typename T, typename S>
void lass::num::Matrix< T, S >::invert ( )

replace matrix by its inverse.

If matrix has no inverse (this is singular), no exception is thrown. Instead, an empty matrix is made.

Precondition
this must be an l-value (storage matrix).
Returns
true if succeeded, false if not.
Exceptions
anexception is thrown if this is not a square matrix

Definition at line 696 of file matrix.inl.

References column(), isSquare(), LASS_META_ASSERT, Matrix(), rows(), and setIdentity().

◆ swap()

template<typename T, typename S>
void lass::num::Matrix< T, S >::swap ( Matrix< T, S > & other)
inline

swap two matrices

Precondition
this and other must be an l-value (storage matrix).
Complexity:
O(1)
Exception safety:
no-fail

Definition at line 750 of file matrix.inl.

References LASS_META_ASSERT, and Matrix().

Friends And Related Symbol Documentation

◆ operator==()

template<typename T, typename S1, typename S2>
bool operator== ( const Matrix< T, S1 > & a,
const Matrix< T, S2 > & b )
related
Complexity:
O(a.rows() * a.columns())

Definition at line 774 of file matrix.inl.

◆ operator!=()

template<typename T, typename S1, typename S2>
bool operator!= ( const Matrix< T, S1 > & a,
const Matrix< T, S2 > & b )
related
Complexity:
O(a.rows() * a.columns())

Definition at line 804 of file matrix.inl.

◆ operator+() [1/3]

template<typename T, typename S1, typename S2>
const Matrix< T, impl::MAdd< T, S1, S2 > > operator+ ( const Matrix< T, S1 > & a,
const Matrix< T, S2 > & b )
related

componentwise addition

Complexity:
O(1)

Definition at line 819 of file matrix.inl.

◆ operator-() [1/3]

template<typename T, typename S1, typename S2>
const Matrix< T, impl::MSub< T, S1, S2 > > operator- ( const Matrix< T, S1 > & a,
const Matrix< T, S2 > & b )
related

componentwise addition

Complexity:
O(1)

Definition at line 836 of file matrix.inl.

◆ operator*() [1/3]

template<typename T, typename S1, typename S2>
const Matrix< T, impl::MProd< T, S1, S2 > > operator* ( const Matrix< T, S1 > & a,
const Matrix< T, S2 > & b )
related

Matrix multiplication.

The product
C of two matrices
A and
B is defined by
Cik=Aij*Bjk, where
j is summed over for all possible values of
i and
k. The implied summation over repeated indices without the presence of an explicit sum sign is called Einstein summation, and is commonly used in both matrix and tensor analysis. Therefore, in order for matrix multiplication to be defined, the dimensions of the matrices must satisfy
(n�m)*(m�p)=(n�p) where
(a�b) denotes a matrix with
a rows and
b columns.
http://mathworld.wolfram.com/MatrixMultiplication.html

Exceptions
anexception is throw in a and b don't meet the requirement
a.columns()==b.rows() .
Complexity:
O(1)

Definition at line 864 of file matrix.inl.

◆ operator+() [2/3]

template<typename T, typename S>
const Matrix< T, impl::MAdd< T, impl::MScalar< T >, S > > operator+ ( const T & a,
const Matrix< T, S > & b )
related

add a to all components of b

Complexity:
O(1)

Definition at line 881 of file matrix.inl.

◆ operator-() [2/3]

template<typename T, typename S>
const Matrix< T, impl::MSub< T, impl::MScalar< T >, S > > operator- ( const T & a,
const Matrix< T, S > & b )
related

add a to all negated components of b

Complexity:
O(1)

Definition at line 898 of file matrix.inl.

◆ operator*() [2/3]

template<typename T, typename S>
const Matrix< T, impl::MMul< T, impl::MScalar< T >, S > > operator* ( const T & a,
const Matrix< T, S > & b )
related

multiply a with all components of b

Complexity:
O(1)

Definition at line 915 of file matrix.inl.

◆ operator+() [3/3]

template<typename T, typename S>
const Matrix< T, impl::MAdd< T, S, impl::MScalar< T > > > operator+ ( const Matrix< T, S > & a,
typename Matrix< T, S >::TParam b )
related

add b to all components of a

Complexity:
O(1)

Definition at line 932 of file matrix.inl.

◆ operator-() [3/3]

template<typename T, typename S>
const Matrix< T, impl::MAdd< T, S, impl::MScalar< T > > > operator- ( const Matrix< T, S > & a,
typename Matrix< T, S >::TParam b )
related

subtract b from all components of a

Complexity:
O(1)

Definition at line 949 of file matrix.inl.

◆ operator*() [3/3]

template<typename T, typename S>
const Matrix< T, impl::MMul< T, S, impl::MScalar< T > > > operator* ( const Matrix< T, S > & a,
typename Matrix< T, S >::TParam b )
related

multiply all components of a with b.

Complexity:
O(1)

Definition at line 966 of file matrix.inl.

◆ operator/()

template<typename T, typename S>
const Matrix< T, impl::MMul< T, S, impl::MScalar< T > > > operator/ ( const Matrix< T, S > & a,
typename Matrix< T, S >::TParam b )
related

divide all components of a by b.

Complexity:
O(1)

Definition at line 983 of file matrix.inl.

◆ solve()

template<typename T, typename S, typename S2>
void solve ( const Matrix< T, S > & a,
Matrix< T, S2 > & bx,
bool improve )
related

solve equation A * X = B

Parameters
a[in] matrix A
bx[in,out] serves both as the input matrix B and the output matrix X.
improve[in] if true, it will perform an extra iteration to improve the solution, but it comes at the cost of having to take an extra copy of a.
Precondition
bx must be an l-value (storage matrix)
Exceptions
anexception is thrown if dimensions don't match (this->isSquare() && this->columns() == bx.rows())

Definition at line 1006 of file matrix.inl.


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