Dynamical system model
Loading...
Searching...
No Matches
dsm::SparseMatrix< T > Class Template Reference

The SparseMatrix class represents a sparse matrix. More...

#include <SparseMatrix.hpp>

Public Member Functions

 SparseMatrix (Id rows, Id cols)
 SparseMatrix constructor.
 
 SparseMatrix (Id index)
 SparseMatrix constructor - colum.
 
void insert (Id i, Id j, T value)
 insert a value in the matrix
 
void insert (Id i, T value)
 insert a value in the matrix
 
void insert_or_assign (Id i, Id j, T value)
 insert a value in the matrix. If the element already exist, it overwrites it
 
void insert_or_assign (Id index, T value)
 insert a value in the matrix. If the element already exist, it overwrites it
 
void insert_and_expand (Id i, Id j, T value)
 insert a value in the matrix and expand the matrix if necessary.
 
void erase (Id i, Id j)
 remove a value from the matrix
 
void erase (Id index)
 remove a value from the matrix
 
void eraseRow (Id index)
 remove a row from the matrix
 
void eraseColumn (Id index)
 remove a column from the matrix
 
void emptyRow (Id index)
 set to 0 all the elements in a row
 
void emptyColumn (Id index)
 set to 0 all the elements in a column
 
void clear ()
 empty the matrix and set the dimensions to zero
 
bool contains (Id i, Id j) const
 check if the element is non zero
 
bool contains (Id const index) const
 check if the element is non zero
 
SparseMatrix< int > getDegreeVector () const
 get the input degree of all nodes
 
SparseMatrix< double > getStrengthVector () const
 get the strength of all nodes
 
SparseMatrix< int > getLaplacian () const
 get the laplacian matrix
 
SparseMatrix getRow (Id index, bool keepId=false) const
 get a row as a row vector
 
SparseMatrix getCol (Id index, bool keepId=false) const
 get a column as a column vector
 
SparseMatrix< double > getNormRows () const
 get a matrix of double with every row normalized to 1
 
SparseMatrix< double > getNormCols () const
 get a matrix of double with every column normalized to 1
 
Id getRowDim () const
 get the number of rows
 
Id getColDim () const
 get the number of columns
 
Id size () const
 get the number of non zero elements in the matrix
 
Id max_size () const
 get the maximum number of elements in the matrix
 
void symmetrize ()
 symmetrize the matrix
 
void reshape (Id rows, Id cols)
 reshape the matrix
 
void reshape (Id dim)
 reshape the matrix
 
std::unordered_map< Id, T >::const_iterator begin () const
 return the begin iterator of the matrix
 
std::unordered_map< Id, T >::const_iterator end () const
 return the end iterator of the matrix
 
const T & operator() (Id i, Id j) const
 access an element of the matrix
 
T & operator() (Id i, Id j)
 access an element of the matrix
 
const T & operator() (Id index) const
 access an element of the matrix
 
T & operator() (Id index)
 access an element of the matrix
 
template<typename U >
SparseMatrix< T > operator+ (const SparseMatrix< U > &other)
 sum of two matrices
 
template<typename U >
SparseMatrix< T > operator- (const SparseMatrix< U > &other)
 difference of two matrices
 
SparseMatrix operator++ ()
 transpose the matrix
 
template<typename U >
SparseMatrixoperator+= (const SparseMatrix< U > &other)
 sum of two matrices
 
template<typename U >
SparseMatrixoperator-= (const SparseMatrix< U > &other)
 difference of two matrices
 
template<typename U >
SparseMatrix< T > & operator+= (const SparseMatrix< U > &other)
 
template<typename U >
SparseMatrix< T > & operator-= (const SparseMatrix< U > &other)
 

Detailed Description

template<typename T>
class dsm::SparseMatrix< T >

The SparseMatrix class represents a sparse matrix.

Template Parameters
IdThe type of the matrix's index. It must be an unsigned integral type.
TThe type of the matrix's value.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/2]

template<typename T >
dsm::SparseMatrix< T >::SparseMatrix ( Id rows,
Id cols )

SparseMatrix constructor.

Parameters
rowsnumber of rows
colsnumber of columns
Exceptions
std::invalid_argumentif rows or cols are < 0

◆ SparseMatrix() [2/2]

template<typename T >
dsm::SparseMatrix< T >::SparseMatrix ( Id index)
explicit

SparseMatrix constructor - colum.

Parameters
indexnumber of rows
Exceptions
std::invalid_argumentif index is < 0

Member Function Documentation

◆ begin()

template<typename T >
std::unordered_map< Id, T >::const_iterator dsm::SparseMatrix< T >::begin ( ) const
inline

return the begin iterator of the matrix

Returns
the begin iterator

◆ contains() [1/2]

template<typename T >
bool dsm::SparseMatrix< T >::contains ( Id const index) const

check if the element is non zero

Parameters
indexindex in vectorial form
Returns
true if the element is non zero
Exceptions
std::out_of_rangeif the index is out of range

◆ contains() [2/2]

template<typename T >
bool dsm::SparseMatrix< T >::contains ( Id i,
Id j ) const

check if the element is non zero

Parameters
irow index
jcolumn index
Returns
true if the element is non zero
Exceptions
std::out_of_rangeif the index is out of range

◆ end()

template<typename T >
std::unordered_map< Id, T >::const_iterator dsm::SparseMatrix< T >::end ( ) const
inline

return the end iterator of the matrix

Returns
the end iterator

◆ erase() [1/2]

template<typename T >
void dsm::SparseMatrix< T >::erase ( Id i,
Id j )

remove a value from the matrix

Parameters
irow index
jcolumn index
Exceptions
std::out_of_rangeif the index is out of range
std::runtime_errorif the element is not found

◆ erase() [2/2]

template<typename T >
void dsm::SparseMatrix< T >::erase ( Id index)

remove a value from the matrix

Parameters
indexindex in vectorial form
Exceptions
std::out_of_rangeif the index is out of range
std::runtime_errorif the element is not found

◆ eraseColumn()

template<typename T >
void dsm::SparseMatrix< T >::eraseColumn ( Id index)

remove a column from the matrix

Parameters
indexcolumn index
Exceptions
std::out_of_rangeif the index is out of range

◆ eraseRow()

template<typename T >
void dsm::SparseMatrix< T >::eraseRow ( Id index)

remove a row from the matrix

Parameters
indexrow index
Exceptions
std::out_of_rangeif the index is out of range

◆ getCol()

template<typename T >
SparseMatrix< T > dsm::SparseMatrix< T >::getCol ( Id index,
bool keepId = false ) const

get a column as a column vector

Parameters
indexcolumn index
keepIdif true, the index of the elements in the column will be the same as the index of the elements in the matrix
Returns
a column vector if keepId is false, otherwise a matrix with the same dimensions as the original matrix
Exceptions
std::out_of_rangeif the index is out of range

◆ getColDim()

template<typename T >
Id dsm::SparseMatrix< T >::getColDim ( ) const
inline

get the number of columns

Returns
number of columns

◆ getDegreeVector()

template<typename T >
SparseMatrix< int > dsm::SparseMatrix< T >::getDegreeVector ( ) const

get the input degree of all nodes

Returns
a SparseMatrix vector with the input degree of all nodes
Exceptions
std::runtime_errorif the matrix is not square

◆ getLaplacian()

template<typename T >
SparseMatrix< int > dsm::SparseMatrix< T >::getLaplacian ( ) const

get the laplacian matrix

Returns
the laplacian matrix
Exceptions
std::runtime_errorif the matrix is not square

◆ getNormCols()

template<typename T >
SparseMatrix< double > dsm::SparseMatrix< T >::getNormCols ( ) const

get a matrix of double with every column normalized to 1

Returns
a matrix of double

◆ getNormRows()

template<typename T >
SparseMatrix< double > dsm::SparseMatrix< T >::getNormRows ( ) const

get a matrix of double with every row normalized to 1

Returns
a matrix of double

◆ getRow()

template<typename T >
SparseMatrix< T > dsm::SparseMatrix< T >::getRow ( Id index,
bool keepId = false ) const

get a row as a row vector

Parameters
indexrow index
keepIdif true, the index of the elements in the row will be the same as the index of the elements in the matrix
Returns
a row vector if keepId is false, otherwise a matrix with the same dimensions as the original matrix
Exceptions
std::out_of_rangeif the index is out of range

◆ getRowDim()

template<typename T >
Id dsm::SparseMatrix< T >::getRowDim ( ) const
inline

get the number of rows

Returns
number of rows

◆ getStrengthVector()

template<typename T >
SparseMatrix< double > dsm::SparseMatrix< T >::getStrengthVector ( ) const

get the strength of all nodes

Returns
a SparseMatrix vector with the strength of all nodes
Exceptions
std::runtime_errorif the matrix is not square

◆ insert() [1/2]

template<typename T >
void dsm::SparseMatrix< T >::insert ( Id i,
Id j,
T value )

insert a value in the matrix

Parameters
irow index
jcolumn index
valuevalue to insert
Exceptions
std::out_of_rangeif the index is out of range

◆ insert() [2/2]

template<typename T >
void dsm::SparseMatrix< T >::insert ( Id i,
T value )

insert a value in the matrix

Parameters
iindex
valuevalue to insert
Exceptions
std::out_of_rangeif the index is out of range

◆ insert_and_expand()

template<typename T >
void dsm::SparseMatrix< T >::insert_and_expand ( Id i,
Id j,
T value )

insert a value in the matrix and expand the matrix if necessary.

Parameters
irow index
jcolumn index
valuevalue to insert

◆ insert_or_assign() [1/2]

template<typename T >
void dsm::SparseMatrix< T >::insert_or_assign ( Id i,
Id j,
T value )

insert a value in the matrix. If the element already exist, it overwrites it

Parameters
irow index
jcolumn index
valuevalue to insert
Exceptions
std::out_of_rangeif the index is out of range

◆ insert_or_assign() [2/2]

template<typename T >
void dsm::SparseMatrix< T >::insert_or_assign ( Id index,
T value )

insert a value in the matrix. If the element already exist, it overwrites it

Parameters
indexindex in vectorial form
valuevalue to insert
Exceptions
std::out_of_rangeif the index is out of range

◆ max_size()

template<typename T >
Id dsm::SparseMatrix< T >::max_size ( ) const
inline

get the maximum number of elements in the matrix

Returns
maximum number of elements

◆ operator()() [1/4]

template<typename T >
T & dsm::SparseMatrix< T >::operator() ( Id i,
Id j )

access an element of the matrix

Parameters
irow index
jcolumn index
Returns
the element
Exceptions
std::out_of_rangeif the index is out of range

◆ operator()() [2/4]

template<typename T >
const T & dsm::SparseMatrix< T >::operator() ( Id i,
Id j ) const

access an element of the matrix

Parameters
irow index
jcolumn index
Returns
the element
Exceptions
std::out_of_rangeif the index is out of range

◆ operator()() [3/4]

template<typename T >
T & dsm::SparseMatrix< T >::operator() ( Id index)

access an element of the matrix

Parameters
indexindex in vectorial form
Returns
the element
Exceptions
std::out_of_rangeif the index is out of range

◆ operator()() [4/4]

template<typename T >
const T & dsm::SparseMatrix< T >::operator() ( Id index) const

access an element of the matrix

Parameters
indexindex in vectorial form
Returns
the element
Exceptions
std::out_of_rangeif the index is out of range

◆ operator+()

template<typename T >
template<typename U >
SparseMatrix< T > dsm::SparseMatrix< T >::operator+ ( const SparseMatrix< U > & other)
inline

sum of two matrices

Parameters
otherthe other matrix
Returns
the sum of the two matrices
Exceptions
std::runtime_errorif the dimensions do not match

◆ operator++()

template<typename T >
SparseMatrix< T > dsm::SparseMatrix< T >::operator++ ( )

transpose the matrix

Returns
the transposed matrix

◆ operator+=()

template<typename T >
template<typename U >
SparseMatrix & dsm::SparseMatrix< T >::operator+= ( const SparseMatrix< U > & other)

sum of two matrices

Parameters
otherthe other matrix
Returns
the sum of the two matrices
Exceptions
std::runtime_errorif the dimensions do not match

◆ operator-()

template<typename T >
template<typename U >
SparseMatrix< T > dsm::SparseMatrix< T >::operator- ( const SparseMatrix< U > & other)
inline

difference of two matrices

Parameters
otherthe other matrix
Returns
the difference of the two matrices
Exceptions
std::runtime_errorif the dimensions do not match

◆ operator-=()

template<typename T >
template<typename U >
SparseMatrix & dsm::SparseMatrix< T >::operator-= ( const SparseMatrix< U > & other)

difference of two matrices

Parameters
otherthe other matrix
Returns
the difference of the two matrices
Exceptions
std::runtime_errorif the dimensions do not match

◆ size()

template<typename T >
Id dsm::SparseMatrix< T >::size ( ) const
inline

get the number of non zero elements in the matrix

Returns
number of non zero elements

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