Ocean
Ocean::SparseMatrixT< T > Class Template Reference

This class implements a sparse matrix using a float type for its elements that is specified by T. More...

Data Structures

class  Entry
 This class implements a triple object for matrix entries. More...
 

Public Types

typedef std::vector< EntryEntries
 Definition of a vector holding entries. More...
 

Public Member Functions

 SparseMatrixT ()
 Creates an empty sparse matrix object. More...
 
 SparseMatrixT (const SparseMatrixT< T > &matrix)
 Copy constructor. More...
 
 SparseMatrixT (SparseMatrixT< T > &&matrix) noexcept
 Move constructor. More...
 
 SparseMatrixT (const size_t rows, const size_t columns)
 Creates a new spare matrix with given dimensions. More...
 
 SparseMatrixT (const size_t rows, const size_t columns, const size_t nonZeroElements)
 Creates a new spare matrix with given dimensions. More...
 
 SparseMatrixT (const size_t rows, const size_t columns, const Entries &entries)
 Creates a new spare matrix with given dimensions. More...
 
 SparseMatrixT (const size_t rows, const size_t columns, const MatrixT< T > &diagonal, const bool forceNonZeros=false)
 Creates a new sparse matrix with defined rows and columns and initializes the diagonal with small sub matrices. More...
 
 SparseMatrixT (const MatrixT< T > &denseMatrix)
 Creates a new sparse matrix with defined rows and columns and copies all non-zero values from a dense matrix. More...
 
 ~SparseMatrixT ()
 Destructs a sparse matrix object. More...
 
size_t rows () const
 Returns the number of rows this matrix has. More...
 
size_t columns () const
 Returns the number of columns this matrix has. More...
 
size_t nonZeroElements () const
 Returns the number of non zero elements stored in this matrix. More...
 
MatrixT< T > diagonal () const
 Returns a vector containing the values of the diagonal. More...
 
void reserve (const size_t elements)
 Reserves memory for a specified number of non zero elements. More...
 
bool isZero (const size_t row, const size_t column) const
 Returns whether a specified elements is zero. More...
 
bool isEqual (const SparseMatrixT< T > &matrix, const T eps) const
 Returns whether two matrices are almost identical up to a specified epsilon. More...
 
bool isEqual (const MatrixT< T > &matrix, const T eps) const
 Returns whether two matrices are almost identical up to a specified epsilon. More...
 
void setEntries (const Entries &entries)
 (Re-)sets the non-zero entries of this sparse matrix. More...
 
SparseMatrixT< T > submatrix (const size_t row, const size_t column, const size_t rows, const size_t columns) const
 Returns a submatrix of this matrix. More...
 
SparseMatrixT< T > transposed () const
 Returns the transposes matrix of this matrix. More...
 
void transpose ()
 Transposes this matrix. More...
 
bool solve (const MatrixT< T > &b, MatrixT< T > &x) const
 Solves the given linear system. More...
 
size_t rank () const
 Computes the rank of this matrix. More...
 
sum () const
 Determines the sum of all elements of this matrix. More...
 
bool invertDiagonal ()
 Inverts this square diagonal matrix. More...
 
bool invertBlockDiagonal3 ()
 Inverts this square block diagonal matrix with 3x3 block size. More...
 
bool invertBlockDiagonal (const size_t size)
 Inverts this square block diagonal matrix with size x size block size. More...
 
MatrixT< T > denseMatrix () const
 Returns the dense matrix of this matrix. More...
 
bool nonNegativeMatrixFactorization (MatrixT< T > &subcomponents, MatrixT< T > &weights, const unsigned int components=0u, const unsigned int iterations=100u, const T convergenceThreshold=T(0.0001)) const
 Performs a non-negative matrix factorization with multiplicative update rules V = W * H, V is a matrix containing non-negative values
This matrix is V, and will be factorized into two matrices W (weights) and H (subcomponents). More...
 
SparseMatrixT< T > & operator= (const SparseMatrixT< T > &matrix)
 Assign operator. More...
 
SparseMatrixT< T > & operator= (SparseMatrixT< T > &&matrix) noexcept
 Move operator. More...
 
SparseMatrixT< T > operator* (const SparseMatrixT< T > &matrix) const
 Multiplies two sparse matrix objects and returns the matrix result. More...
 
SparseMatrixT< T > & operator*= (const SparseMatrixT< T > &matrix)
 Multiplies a second sparse matrix object to this matrix object. More...
 
SparseMatrixT< T > operator+ (const SparseMatrixT< T > &matrix) const
 Adds two sparse matrix objects and returns the matrix result. More...
 
SparseMatrixT< T > & operator+= (const SparseMatrixT< T > &matrix)
 Adds a second sparse matrix object to this matrix. More...
 
SparseMatrixT< T > operator- (const SparseMatrixT< T > &matrix) const
 Subtracts two sparse matrix objects and returns the matrix result. More...
 
SparseMatrixT< T > & operator-= (const SparseMatrixT< T > &matrix)
 Subtracts a second sparse matrix object from this matrix. More...
 
MatrixT< T > operator* (const MatrixT< T > &matrix) const
 Multiplies a dense matrix object on this sparse matrix object and returns the dense matrix result. More...
 
bool operator== (const SparseMatrixT< T > &matrix) const
 Returns whether this matrix is equal to an other one up to an epsilon value. More...
 
bool operator== (const MatrixT< T > &matrix) const
 Returns whether this matrix is equal to an other one up to an epsilon value. More...
 
operator() (const size_t row, const size_t column) const
 Returns a specific element of the sparse matrix. More...
 
T & operator() (const size_t row, const size_t column)
 Returns the reference to a specific element of the sparse matrix. More...
 

Private Member Functions

 SparseMatrixT (void *matrix)
 Creates a new sparse matrix object by the internal matrix object data directly. More...
 

Private Attributes

void * internalMatrix
 Abstract pointer to the internal matrix object. More...
 

Detailed Description

template<typename T>
class Ocean::SparseMatrixT< T >

This class implements a sparse matrix using a float type for its elements that is specified by T.

Template Parameters
TData type of matrix elements
See also
SparseMatrix, SparseMatrixF, SparseMatrixD, Matrix.

Member Typedef Documentation

◆ Entries

template<typename T >
typedef std::vector<Entry> Ocean::SparseMatrixT< T >::Entries

Definition of a vector holding entries.

Constructor & Destructor Documentation

◆ SparseMatrixT() [1/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( )

Creates an empty sparse matrix object.

◆ SparseMatrixT() [2/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const SparseMatrixT< T > &  matrix)

Copy constructor.

Parameters
matrixSparse matrix to be copied

◆ SparseMatrixT() [3/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( SparseMatrixT< T > &&  matrix)
noexcept

Move constructor.

Parameters
matrixSparse matrix to be movied

◆ SparseMatrixT() [4/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const size_t  rows,
const size_t  columns 
)

Creates a new spare matrix with given dimensions.

Parameters
rowsNumber of rows to create a sparse matrix for
columnsNumber of columns to create a sparse matrix for

◆ SparseMatrixT() [5/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const size_t  rows,
const size_t  columns,
const size_t  nonZeroElements 
)

Creates a new spare matrix with given dimensions.

Parameters
rowsNumber of rows to create a sparse matrix for
columnsNumber of columns to create a sparse matrix for
nonZeroElementsNumber of expected non zero elements to reserve memory for

◆ SparseMatrixT() [6/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const size_t  rows,
const size_t  columns,
const Entries entries 
)

Creates a new spare matrix with given dimensions.

Parameters
rowsNumber of rows to create a sparse matrix for
columnsNumber of columns to create a sparse matrix for
entriesAll non-zero entries of the new matrix

◆ SparseMatrixT() [7/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const size_t  rows,
const size_t  columns,
const MatrixT< T > &  diagonal,
const bool  forceNonZeros = false 
)

Creates a new sparse matrix with defined rows and columns and initializes the diagonal with small sub matrices.

The number of columns of the given diagonal vector matrix defines the size of the small sub matrices.
The number of rows of the diagonal vector matrix must be a multiple of the number of rows.

Parameters
rowsRows of the new matrix
columnsColumns of the new matrix
diagonalDiagonal vector matrix holding the new diagonal sub matrices
forceNonZerosTrue, to force all diagonal elements to be an epsilon larger than zero (if the given element is zero)

◆ SparseMatrixT() [8/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( const MatrixT< T > &  denseMatrix)
explicit

Creates a new sparse matrix with defined rows and columns and copies all non-zero values from a dense matrix.

Parameters
denseMatrixDense matrix from that the non-zero values are copied

◆ ~SparseMatrixT()

template<typename T >
Ocean::SparseMatrixT< T >::~SparseMatrixT ( )

Destructs a sparse matrix object.

◆ SparseMatrixT() [9/9]

template<typename T >
Ocean::SparseMatrixT< T >::SparseMatrixT ( void *  matrix)
explicitprivate

Creates a new sparse matrix object by the internal matrix object data directly.

Parameters
matrixInternal matrix object

Member Function Documentation

◆ columns()

template<typename T >
size_t Ocean::SparseMatrixT< T >::columns ( ) const

Returns the number of columns this matrix has.

Returns
Number of columns

◆ denseMatrix()

template<typename T >
MatrixT<T> Ocean::SparseMatrixT< T >::denseMatrix ( ) const

Returns the dense matrix of this matrix.

Returns
Dense matrix

◆ diagonal()

template<typename T >
MatrixT<T> Ocean::SparseMatrixT< T >::diagonal ( ) const

Returns a vector containing the values of the diagonal.

Returns
Vector with diagonal values with dimension n x 1

◆ invertBlockDiagonal()

template<typename T >
bool Ocean::SparseMatrixT< T >::invertBlockDiagonal ( const size_t  size)

Inverts this square block diagonal matrix with size x size block size.

The size of the matrix must be a multiple of size.
Each of the size x size block is inverted individually.

Parameters
sizeThe size of the block (size x size) with range (2, infinity)
Returns
True, if succeeded

◆ invertBlockDiagonal3()

template<typename T >
bool Ocean::SparseMatrixT< T >::invertBlockDiagonal3 ( )

Inverts this square block diagonal matrix with 3x3 block size.

The size of the matrix must be a multiple of three.
Each of the 3x3 block is inverted individually.

Returns
True, if succeeded

◆ invertDiagonal()

template<typename T >
bool Ocean::SparseMatrixT< T >::invertDiagonal ( )

Inverts this square diagonal matrix.

Returns
True, if succeeded

◆ isEqual() [1/2]

template<typename T >
bool Ocean::SparseMatrixT< T >::isEqual ( const MatrixT< T > &  matrix,
const T  eps 
) const

Returns whether two matrices are almost identical up to a specified epsilon.

Parameters
matrixSecond matrix that will be checked
epsEpsilon to be used
Returns
True, if so

◆ isEqual() [2/2]

template<typename T >
bool Ocean::SparseMatrixT< T >::isEqual ( const SparseMatrixT< T > &  matrix,
const T  eps 
) const

Returns whether two matrices are almost identical up to a specified epsilon.

Parameters
matrixSecond matrix that will be checked
epsEpsilon to be used
Returns
True, if so

◆ isZero()

template<typename T >
bool Ocean::SparseMatrixT< T >::isZero ( const size_t  row,
const size_t  column 
) const

Returns whether a specified elements is zero.

Parameters
rowThe row of the element to be checked
columnThe column of the element to be checked
Returns
True, if so

◆ nonNegativeMatrixFactorization()

template<typename T >
bool Ocean::SparseMatrixT< T >::nonNegativeMatrixFactorization ( MatrixT< T > &  subcomponents,
MatrixT< T > &  weights,
const unsigned int  components = 0u,
const unsigned int  iterations = 100u,
const T  convergenceThreshold = T(0.0001) 
) const

Performs a non-negative matrix factorization with multiplicative update rules V = W * H, V is a matrix containing non-negative values
This matrix is V, and will be factorized into two matrices W (weights) and H (subcomponents).

Parameters
subcomponentsSolution matrix containing base component vectors
weightsSolution matrix containing weights to the component vectors
componentsNumber of base component vectors (Number of components is usally much smaller than its rank). If set to 0, the rank of matrix is used [0, rank]
iterationsNumber of iterations maximal performed [1, infinity)
convergenceThresholdDifferential threshold as convergence criterion (0, infinity)
Returns
True, if succeeded

◆ nonZeroElements()

template<typename T >
size_t Ocean::SparseMatrixT< T >::nonZeroElements ( ) const

Returns the number of non zero elements stored in this matrix.

Returns
Number of non zero elements

◆ operator()() [1/2]

template<typename T >
T& Ocean::SparseMatrixT< T >::operator() ( const size_t  row,
const size_t  column 
)

Returns the reference to a specific element of the sparse matrix.

Beware: The element must be non zero, thus the element must have been inserted before!

Parameters
rowElement row to be returned
columnElement column to be returned
Returns
Matrix element
See also
insert().

◆ operator()() [2/2]

template<typename T >
T Ocean::SparseMatrixT< T >::operator() ( const size_t  row,
const size_t  column 
) const

Returns a specific element of the sparse matrix.

Parameters
rowElement row to be returned
columnElement column to be returned
Returns
Matrix element

◆ operator*() [1/2]

template<typename T >
MatrixT<T> Ocean::SparseMatrixT< T >::operator* ( const MatrixT< T > &  matrix) const

Multiplies a dense matrix object on this sparse matrix object and returns the dense matrix result.

Parameters
matrixRight matrix to multiply
Returns
Matrix product result

◆ operator*() [2/2]

template<typename T >
SparseMatrixT<T> Ocean::SparseMatrixT< T >::operator* ( const SparseMatrixT< T > &  matrix) const

Multiplies two sparse matrix objects and returns the matrix result.

Parameters
matrixRight sparse matrix to multiply
Returns
Matrix product result

◆ operator*=()

template<typename T >
SparseMatrixT< T > & Ocean::SparseMatrixT< T >::operator*= ( const SparseMatrixT< T > &  matrix)
inline

Multiplies a second sparse matrix object to this matrix object.

Parameters
matrixRight sparse matrix to multiply
Returns
Reference to this (modified) sparse matrix

◆ operator+()

template<typename T >
SparseMatrixT<T> Ocean::SparseMatrixT< T >::operator+ ( const SparseMatrixT< T > &  matrix) const

Adds two sparse matrix objects and returns the matrix result.

Parameters
matrixRight sparse matrix to add
Returns
Matrix result

◆ operator+=()

template<typename T >
SparseMatrixT<T>& Ocean::SparseMatrixT< T >::operator+= ( const SparseMatrixT< T > &  matrix)

Adds a second sparse matrix object to this matrix.

Parameters
matrixRight sparse matrix to add
Returns
Reference to this (modified) matrix

◆ operator-()

template<typename T >
SparseMatrixT<T> Ocean::SparseMatrixT< T >::operator- ( const SparseMatrixT< T > &  matrix) const

Subtracts two sparse matrix objects and returns the matrix result.

Parameters
matrixRight sparse matrix to subtract
Returns
Matrix result

◆ operator-=()

template<typename T >
SparseMatrixT<T>& Ocean::SparseMatrixT< T >::operator-= ( const SparseMatrixT< T > &  matrix)

Subtracts a second sparse matrix object from this matrix.

Parameters
matrixRight sparse matrix to subtract
Returns
Reference to this (modified) matrix

◆ operator=() [1/2]

template<typename T >
SparseMatrixT<T>& Ocean::SparseMatrixT< T >::operator= ( const SparseMatrixT< T > &  matrix)

Assign operator.

Parameters
matrixSparse matrix to be assigned to this matrix
Returns
Reference to this matrix

◆ operator=() [2/2]

template<typename T >
SparseMatrixT<T>& Ocean::SparseMatrixT< T >::operator= ( SparseMatrixT< T > &&  matrix)
noexcept

Move operator.

Parameters
matrixSparse matrix to be move to this matrix
Returns
Reference to this matrix

◆ operator==() [1/2]

template<typename T >
bool Ocean::SparseMatrixT< T >::operator== ( const MatrixT< T > &  matrix) const
inline

Returns whether this matrix is equal to an other one up to an epsilon value.

Parameters
matrixSecond matrix to compare
Returns
True, if succeeded

◆ operator==() [2/2]

template<typename T >
bool Ocean::SparseMatrixT< T >::operator== ( const SparseMatrixT< T > &  matrix) const
inline

Returns whether this matrix is equal to an other one up to an epsilon value.

Parameters
matrixSecond matrix to compare
Returns
True, if succeeded

◆ rank()

template<typename T >
size_t Ocean::SparseMatrixT< T >::rank ( ) const

Computes the rank of this matrix.

The matrix must be valid.

Returns
The matrix's rank with range [0, min(rows(), columns())]

◆ reserve()

template<typename T >
void Ocean::SparseMatrixT< T >::reserve ( const size_t  elements)

Reserves memory for a specified number of non zero elements.

Parameters
elementsNumber of non zero elements.

◆ rows()

template<typename T >
size_t Ocean::SparseMatrixT< T >::rows ( ) const

Returns the number of rows this matrix has.

Returns
Number of rows

◆ setEntries()

template<typename T >
void Ocean::SparseMatrixT< T >::setEntries ( const Entries entries)

(Re-)sets the non-zero entries of this sparse matrix.

All previous non-zero entries will be removed.
If the given set of entries contains zero-values these zero values will be skipped.

Parameters
entriesThe entries to set

◆ solve()

template<typename T >
bool Ocean::SparseMatrixT< T >::solve ( const MatrixT< T > &  b,
MatrixT< T > &  x 
) const

Solves the given linear system.

M * x = b, with M and b known.
This matrix is M, the given vector is b and the result will be x.

Parameters
bVector defining the linear system
xSolution vector receiving the solution if existing
Returns
True, if succeeded

◆ submatrix()

template<typename T >
SparseMatrixT<T> Ocean::SparseMatrixT< T >::submatrix ( const size_t  row,
const size_t  column,
const size_t  rows,
const size_t  columns 
) const

Returns a submatrix of this matrix.

Parameters
rowThe index of the row which will be the first row of the new submatrix, with range [0, rows())
columnThe index of the column which will be the first column of the new sub-matrix, with range [0, colums())
rowsThe number of rows of the new sub-matrix, with range [1, rows() - row]
columnsThe number of columns of the new sub-matrix, with range [1, columns() - column]
Returns
The specified submatrix of this matrix

◆ sum()

template<typename T >
T Ocean::SparseMatrixT< T >::sum ( ) const
inline

Determines the sum of all elements of this matrix.

Returns
Matrix sum

◆ transpose()

template<typename T >
void Ocean::SparseMatrixT< T >::transpose ( )

Transposes this matrix.

◆ transposed()

template<typename T >
SparseMatrixT<T> Ocean::SparseMatrixT< T >::transposed ( ) const

Returns the transposes matrix of this matrix.

Returns
Transposes matrix of this matrix

Field Documentation

◆ internalMatrix

template<typename T >
void* Ocean::SparseMatrixT< T >::internalMatrix
private

Abstract pointer to the internal matrix object.


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