Chemical Data Processing Library C++ API - Version 1.4.0
Public Types | Public Member Functions | Friends | List of all members
CDPL::Math::Matrix< T, A > Class Template Reference

Dynamically-sized dense row-major matrix with configurable underlying storage. More...

#include <Matrix.hpp>

+ Inheritance diagram for CDPL::Math::Matrix< T, A >:

Public Types

typedef T ValueType
 The scalar value type stored in the matrix. More...
 
typedef T & Reference
 Mutable reference type to an element. More...
 
typedef const T & ConstReference
 Constant reference type to an element. More...
 
typedef A::size_type SizeType
 The unsigned size type used by the underlying storage container. More...
 
typedef A::difference_type DifferenceType
 The signed difference type used by the underlying storage container. More...
 
typedef A ArrayType
 The underlying storage container type (row-major linear storage). More...
 
typedef T * Pointer
 Pointer type for raw element access. More...
 
typedef const T * ConstPointer
 Constant pointer type for raw element access. More...
 
typedef MatrixReference< SelfTypeClosureType
 Closure type used when this matrix appears inside another expression. More...
 
typedef const MatrixReference< const SelfTypeConstClosureType
 Constant closure type used when this matrix appears inside another expression. More...
 
typedef SelfType MatrixTemporaryType
 Concrete temporary matrix type used by expression-template machinery. More...
 
typedef Vector< T, A > VectorTemporaryType
 Concrete temporary vector type compatible with this matrix's value type and storage. More...
 
typedef std::shared_ptr< SelfTypeSharedPointer
 A reference-counted smart pointer [SHPTR] for dynamically allocated Matrix instances. More...
 
typedef std::initializer_list< std::initializer_list< T > > InitializerListType
 Type of the brace-initializer list of lists accepted by the corresponding constructor (one inner list per row). More...
 
- Public Types inherited from CDPL::Math::MatrixContainer< Matrix< T, std::vector< T > > >
typedef Matrix< T, std::vector< T > > ContainerType
 The derived matrix container type. More...
 
- Public Types inherited from CDPL::Math::MatrixExpression< E >
typedef E ExpressionType
 The derived matrix expression type. More...
 
- Public Types inherited from CDPL::Math::Expression< E >
typedef E ExpressionType
 The derived expression type (made available to expression-template machinery). More...
 

Public Member Functions

 Matrix ()
 Constructs an empty matrix (zero rows and zero columns). More...
 
 Matrix (SizeType m, SizeType n)
 Constructs an m × n matrix with default-initialized elements. More...
 
 Matrix (SizeType m, SizeType n, const ValueType &v)
 Constructs an m × n matrix with every element initialized to v. More...
 
 Matrix (const Matrix &m)
 Constructs a copy of the matrix m. More...
 
 Matrix (Matrix &&m)
 Move-constructs a matrix from m (m is left in a valid empty state). More...
 
 Matrix (InitializerListType l)
 Constructs the matrix from a brace-initializer list of rows. More...
 
template<typename E >
 Matrix (const MatrixExpression< E > &e)
 Constructs the matrix from the matrix expression e (materializing the expression result). More...
 
Reference operator() (SizeType i, SizeType j)
 Returns a mutable reference to the element at (i, j). More...
 
ConstReference operator() (SizeType i, SizeType j) const
 Returns a const reference to the element at (i, j). More...
 
bool isEmpty () const
 Tells whether the matrix is empty. More...
 
SizeType getSize1 () const
 Returns the number of rows. More...
 
SizeType getSize2 () const
 Returns the number of columns. More...
 
SizeType getMaxSize () const
 Returns the maximum total element count the underlying storage container can hold. More...
 
ArrayTypegetData ()
 Returns a mutable reference to the underlying storage container (row-major linear layout). More...
 
const ArrayTypegetData () const
 Returns a const reference to the underlying storage container (row-major linear layout). More...
 
Matrixoperator= (const Matrix &m)
 Copy-assigns the contents of m to this matrix. More...
 
Matrixoperator= (Matrix &&m)
 Move-assigns the contents of m to this matrix. More...
 
template<typename C >
Matrixoperator= (const MatrixContainer< C > &c)
 Assigns the contents of the matrix container c to this matrix (no alias check needed). More...
 
Matrixoperator= (InitializerListType l)
 Assigns the rows in l to this matrix (resizes to match). More...
 
template<typename E >
Matrixoperator= (const MatrixExpression< E > &e)
 Assigns the matrix expression e to this matrix (via a temporary to handle aliasing). More...
 
template<typename C >
Matrixoperator+= (const MatrixContainer< C > &c)
 Adds the contents of the matrix container c element-wise to this matrix (no alias check needed). More...
 
Matrixoperator+= (InitializerListType l)
 Adds the rows in l element-wise to this matrix. More...
 
template<typename E >
Matrixoperator+= (const MatrixExpression< E > &e)
 Adds the matrix expression e element-wise to this matrix (via a temporary to handle aliasing). More...
 
template<typename C >
Matrixoperator-= (const MatrixContainer< C > &c)
 Subtracts the contents of the matrix container c element-wise from this matrix (no alias check needed). More...
 
Matrixoperator-= (InitializerListType l)
 Subtracts the rows in l element-wise from this matrix. More...
 
template<typename E >
Matrixoperator-= (const MatrixExpression< E > &e)
 Subtracts the matrix expression e element-wise from this matrix (via a temporary to handle aliasing). More...
 
template<typename T1 >
std::enable_if< IsScalar< T1 >::value, Matrix >::type & operator*= (const T1 &t)
 Multiplies every element by the scalar t. More...
 
template<typename T1 >
std::enable_if< IsScalar< T1 >::value, Matrix >::type & operator/= (const T1 &t)
 Divides every element by the scalar t. More...
 
template<typename E >
Matrixassign (const MatrixExpression< E > &e)
 Resizes this matrix to match the dimensions of e and assigns its elements (without intermediate temporary). More...
 
Matrixassign (InitializerListType l)
 Resizes this matrix to match l and assigns its elements. More...
 
template<typename E >
MatrixplusAssign (const MatrixExpression< E > &e)
 Adds the elements of the matrix expression e to this matrix without intermediate temporary. More...
 
MatrixplusAssign (InitializerListType l)
 Adds the rows in l element-wise to this matrix without intermediate temporary. More...
 
template<typename E >
MatrixminusAssign (const MatrixExpression< E > &e)
 Subtracts the elements of the matrix expression e from this matrix without intermediate temporary. More...
 
MatrixminusAssign (InitializerListType l)
 Subtracts the rows in l element-wise from this matrix without intermediate temporary. More...
 
void swap (Matrix &m)
 Swaps the contents of this matrix with those of m. More...
 
void clear (const ValueType &v=ValueType())
 Sets every element of the matrix to the value v. More...
 
void resize (SizeType m, SizeType n, bool preserve=true, const ValueType &v=ValueType())
 Resizes the matrix to \( m \times n \) elements. More...
 
- Public Member Functions inherited from CDPL::Math::MatrixContainer< Matrix< T, std::vector< T > > >
const ContainerTypeoperator() () const
 Returns a const reference to the derived matrix container. More...
 
ContainerTypeoperator() ()
 Returns a reference to the derived matrix container. More...
 
- Public Member Functions inherited from CDPL::Math::MatrixExpression< E >
const ExpressionTypeoperator() () const
 Returns a const reference to the derived matrix expression. More...
 
ExpressionTypeoperator() ()
 Returns a reference to the derived matrix expression. More...
 

Friends

void swap (Matrix &m1, Matrix &m2)
 ADL-enabled free-function form of swap(). More...
 

Additional Inherited Members

- Protected Member Functions inherited from CDPL::Math::MatrixContainer< Matrix< T, std::vector< T > > >
 MatrixContainer ()
 
 ~MatrixContainer ()
 
- Protected Member Functions inherited from CDPL::Math::MatrixExpression< E >
 MatrixExpression ()
 
 ~MatrixExpression ()
 
- Protected Member Functions inherited from CDPL::Math::Expression< E >
 Expression ()
 
 ~Expression ()
 

Detailed Description

template<typename T, typename A = std::vector<T>>
class CDPL::Math::Matrix< T, A >

Dynamically-sized dense row-major matrix with configurable underlying storage.

Template Parameters
TThe scalar value type.
AThe underlying storage container type (default: std::vector).

Member Typedef Documentation

◆ ValueType

template<typename T , typename A = std::vector<T>>
typedef T CDPL::Math::Matrix< T, A >::ValueType

The scalar value type stored in the matrix.

◆ Reference

template<typename T , typename A = std::vector<T>>
typedef T& CDPL::Math::Matrix< T, A >::Reference

Mutable reference type to an element.

◆ ConstReference

template<typename T , typename A = std::vector<T>>
typedef const T& CDPL::Math::Matrix< T, A >::ConstReference

Constant reference type to an element.

◆ SizeType

template<typename T , typename A = std::vector<T>>
typedef A::size_type CDPL::Math::Matrix< T, A >::SizeType

The unsigned size type used by the underlying storage container.

◆ DifferenceType

template<typename T , typename A = std::vector<T>>
typedef A::difference_type CDPL::Math::Matrix< T, A >::DifferenceType

The signed difference type used by the underlying storage container.

◆ ArrayType

template<typename T , typename A = std::vector<T>>
typedef A CDPL::Math::Matrix< T, A >::ArrayType

The underlying storage container type (row-major linear storage).

◆ Pointer

template<typename T , typename A = std::vector<T>>
typedef T* CDPL::Math::Matrix< T, A >::Pointer

Pointer type for raw element access.

◆ ConstPointer

template<typename T , typename A = std::vector<T>>
typedef const T* CDPL::Math::Matrix< T, A >::ConstPointer

Constant pointer type for raw element access.

◆ ClosureType

template<typename T , typename A = std::vector<T>>
typedef MatrixReference<SelfType> CDPL::Math::Matrix< T, A >::ClosureType

Closure type used when this matrix appears inside another expression.

◆ ConstClosureType

template<typename T , typename A = std::vector<T>>
typedef const MatrixReference<const SelfType> CDPL::Math::Matrix< T, A >::ConstClosureType

Constant closure type used when this matrix appears inside another expression.

◆ MatrixTemporaryType

template<typename T , typename A = std::vector<T>>
typedef SelfType CDPL::Math::Matrix< T, A >::MatrixTemporaryType

Concrete temporary matrix type used by expression-template machinery.

◆ VectorTemporaryType

template<typename T , typename A = std::vector<T>>
typedef Vector<T, A> CDPL::Math::Matrix< T, A >::VectorTemporaryType

Concrete temporary vector type compatible with this matrix's value type and storage.

◆ SharedPointer

template<typename T , typename A = std::vector<T>>
typedef std::shared_ptr<SelfType> CDPL::Math::Matrix< T, A >::SharedPointer

A reference-counted smart pointer [SHPTR] for dynamically allocated Matrix instances.

◆ InitializerListType

template<typename T , typename A = std::vector<T>>
typedef std::initializer_list<std::initializer_list<T> > CDPL::Math::Matrix< T, A >::InitializerListType

Type of the brace-initializer list of lists accepted by the corresponding constructor (one inner list per row).

Constructor & Destructor Documentation

◆ Matrix() [1/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( )
inline

Constructs an empty matrix (zero rows and zero columns).

◆ Matrix() [2/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( SizeType  m,
SizeType  n 
)
inline

Constructs an m × n matrix with default-initialized elements.

Parameters
mThe number of rows.
nThe number of columns.

◆ Matrix() [3/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( SizeType  m,
SizeType  n,
const ValueType v 
)
inline

Constructs an m × n matrix with every element initialized to v.

Parameters
mThe number of rows.
nThe number of columns.
vThe element value used to initialize every cell.

◆ Matrix() [4/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( const Matrix< T, A > &  m)
inline

Constructs a copy of the matrix m.

Parameters
mThe matrix to copy.

◆ Matrix() [5/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( Matrix< T, A > &&  m)
inline

Move-constructs a matrix from m (m is left in a valid empty state).

Parameters
mThe matrix to move from.

◆ Matrix() [6/7]

template<typename T , typename A = std::vector<T>>
CDPL::Math::Matrix< T, A >::Matrix ( InitializerListType  l)
inline

Constructs the matrix from a brace-initializer list of rows.

The outer list yields the rows; each inner list yields the row elements. The size is derived from the dimensions of the supplied initializer (all inner lists must have the same length).

Parameters
lThe initializer list of rows.

◆ Matrix() [7/7]

template<typename T , typename A = std::vector<T>>
template<typename E >
CDPL::Math::Matrix< T, A >::Matrix ( const MatrixExpression< E > &  e)
inline

Constructs the matrix from the matrix expression e (materializing the expression result).

Template Parameters
EThe matrix expression type.
Parameters
eThe matrix expression to materialize.

Member Function Documentation

◆ operator()() [1/2]

template<typename T , typename A = std::vector<T>>
Reference CDPL::Math::Matrix< T, A >::operator() ( SizeType  i,
SizeType  j 
)
inline

Returns a mutable reference to the element at (i, j).

Parameters
iThe zero-based row index.
jThe zero-based column index.
Returns
A mutable reference to the element.
Exceptions
Base::IndexErrorif either index is out of range.

◆ operator()() [2/2]

template<typename T , typename A = std::vector<T>>
ConstReference CDPL::Math::Matrix< T, A >::operator() ( SizeType  i,
SizeType  j 
) const
inline

Returns a const reference to the element at (i, j).

Parameters
iThe zero-based row index.
jThe zero-based column index.
Returns
A const reference to the element.
Exceptions
Base::IndexErrorif either index is out of range.

◆ isEmpty()

template<typename T , typename A = std::vector<T>>
bool CDPL::Math::Matrix< T, A >::isEmpty ( ) const
inline

Tells whether the matrix is empty.

Returns
true if the underlying storage holds no elements, and false otherwise.

◆ getSize1()

template<typename T , typename A = std::vector<T>>
SizeType CDPL::Math::Matrix< T, A >::getSize1 ( ) const
inline

Returns the number of rows.

Returns
The number of rows.

◆ getSize2()

template<typename T , typename A = std::vector<T>>
SizeType CDPL::Math::Matrix< T, A >::getSize2 ( ) const
inline

Returns the number of columns.

Returns
The number of columns.

◆ getMaxSize()

template<typename T , typename A = std::vector<T>>
SizeType CDPL::Math::Matrix< T, A >::getMaxSize ( ) const
inline

Returns the maximum total element count the underlying storage container can hold.

Returns
The maximum total element count.

◆ getData() [1/2]

template<typename T , typename A = std::vector<T>>
ArrayType& CDPL::Math::Matrix< T, A >::getData ( )
inline

Returns a mutable reference to the underlying storage container (row-major linear layout).

Returns
A mutable reference to the storage container.

◆ getData() [2/2]

template<typename T , typename A = std::vector<T>>
const ArrayType& CDPL::Math::Matrix< T, A >::getData ( ) const
inline

Returns a const reference to the underlying storage container (row-major linear layout).

Returns
A const reference to the storage container.

◆ operator=() [1/5]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::operator= ( const Matrix< T, A > &  m)
inline

Copy-assigns the contents of m to this matrix.

Parameters
mThe source matrix.
Returns
A reference to itself.

◆ operator=() [2/5]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::operator= ( Matrix< T, A > &&  m)
inline

Move-assigns the contents of m to this matrix.

Parameters
mThe source matrix (left in a valid but unspecified state).
Returns
A reference to itself.

◆ operator=() [3/5]

template<typename T , typename A = std::vector<T>>
template<typename C >
Matrix& CDPL::Math::Matrix< T, A >::operator= ( const MatrixContainer< C > &  c)
inline

Assigns the contents of the matrix container c to this matrix (no alias check needed).

Template Parameters
CThe source matrix container type.
Parameters
cThe source matrix container.
Returns
A reference to itself.

◆ operator=() [4/5]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::operator= ( InitializerListType  l)
inline

Assigns the rows in l to this matrix (resizes to match).

Parameters
lThe initializer list of rows.
Returns
A reference to itself.

◆ operator=() [5/5]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::operator= ( const MatrixExpression< E > &  e)
inline

Assigns the matrix expression e to this matrix (via a temporary to handle aliasing).

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ operator+=() [1/3]

template<typename T , typename A = std::vector<T>>
template<typename C >
Matrix& CDPL::Math::Matrix< T, A >::operator+= ( const MatrixContainer< C > &  c)
inline

Adds the contents of the matrix container c element-wise to this matrix (no alias check needed).

Template Parameters
CThe source matrix container type.
Parameters
cThe source matrix container.
Returns
A reference to itself.

◆ operator+=() [2/3]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::operator+= ( InitializerListType  l)
inline

Adds the rows in l element-wise to this matrix.

Parameters
lThe initializer list of rows providing the addends.
Returns
A reference to itself.

◆ operator+=() [3/3]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::operator+= ( const MatrixExpression< E > &  e)
inline

Adds the matrix expression e element-wise to this matrix (via a temporary to handle aliasing).

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ operator-=() [1/3]

template<typename T , typename A = std::vector<T>>
template<typename C >
Matrix& CDPL::Math::Matrix< T, A >::operator-= ( const MatrixContainer< C > &  c)
inline

Subtracts the contents of the matrix container c element-wise from this matrix (no alias check needed).

Template Parameters
CThe source matrix container type.
Parameters
cThe source matrix container.
Returns
A reference to itself.

◆ operator-=() [2/3]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::operator-= ( InitializerListType  l)
inline

Subtracts the rows in l element-wise from this matrix.

Parameters
lThe initializer list of rows providing the subtrahends.
Returns
A reference to itself.

◆ operator-=() [3/3]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::operator-= ( const MatrixExpression< E > &  e)
inline

Subtracts the matrix expression e element-wise from this matrix (via a temporary to handle aliasing).

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ operator*=()

template<typename T , typename A = std::vector<T>>
template<typename T1 >
std::enable_if<IsScalar<T1>::value, Matrix>::type& CDPL::Math::Matrix< T, A >::operator*= ( const T1 &  t)
inline

Multiplies every element by the scalar t.

Template Parameters
T1The scalar type.
Parameters
tThe scalar multiplier.
Returns
A reference to itself.

◆ operator/=()

template<typename T , typename A = std::vector<T>>
template<typename T1 >
std::enable_if<IsScalar<T1>::value, Matrix>::type& CDPL::Math::Matrix< T, A >::operator/= ( const T1 &  t)
inline

Divides every element by the scalar t.

Template Parameters
T1The scalar type.
Parameters
tThe scalar divisor.
Returns
A reference to itself.

◆ assign() [1/2]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::assign ( const MatrixExpression< E > &  e)
inline

Resizes this matrix to match the dimensions of e and assigns its elements (without intermediate temporary).

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ assign() [2/2]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::assign ( InitializerListType  l)
inline

Resizes this matrix to match l and assigns its elements.

Parameters
lThe initializer list of rows providing the new elements.
Returns
A reference to itself.

◆ plusAssign() [1/2]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::plusAssign ( const MatrixExpression< E > &  e)
inline

Adds the elements of the matrix expression e to this matrix without intermediate temporary.

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ plusAssign() [2/2]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::plusAssign ( InitializerListType  l)
inline

Adds the rows in l element-wise to this matrix without intermediate temporary.

Parameters
lThe initializer list of rows providing the addends.
Returns
A reference to itself.

◆ minusAssign() [1/2]

template<typename T , typename A = std::vector<T>>
template<typename E >
Matrix& CDPL::Math::Matrix< T, A >::minusAssign ( const MatrixExpression< E > &  e)
inline

Subtracts the elements of the matrix expression e from this matrix without intermediate temporary.

Template Parameters
EThe source matrix expression type.
Parameters
eThe source matrix expression.
Returns
A reference to itself.

◆ minusAssign() [2/2]

template<typename T , typename A = std::vector<T>>
Matrix& CDPL::Math::Matrix< T, A >::minusAssign ( InitializerListType  l)
inline

Subtracts the rows in l element-wise from this matrix without intermediate temporary.

Parameters
lThe initializer list of rows providing the subtrahends.
Returns
A reference to itself.

◆ swap()

template<typename T , typename A = std::vector<T>>
void CDPL::Math::Matrix< T, A >::swap ( Matrix< T, A > &  m)
inline

Swaps the contents of this matrix with those of m.

Parameters
mThe matrix to swap with.

◆ clear()

template<typename T , typename A = std::vector<T>>
void CDPL::Math::Matrix< T, A >::clear ( const ValueType v = ValueType())
inline

Sets every element of the matrix to the value v.

Parameters
vThe fill value.

◆ resize()

template<typename T , typename A = std::vector<T>>
void CDPL::Math::Matrix< T, A >::resize ( SizeType  m,
SizeType  n,
bool  preserve = true,
const ValueType v = ValueType() 
)
inline

Resizes the matrix to \( m \times n \) elements.

Parameters
mThe new row count.
nThe new column count.
preserveIf true, existing element values at indices that remain valid are kept; if false, all elements are set to v.
vThe fill value for newly added elements (or for all elements when preserve is false).

Friends And Related Function Documentation

◆ swap

template<typename T , typename A = std::vector<T>>
void swap ( Matrix< T, A > &  m1,
Matrix< T, A > &  m2 
)
friend

ADL-enabled free-function form of swap().

Parameters
m1The first matrix.
m2The second matrix.

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