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

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

#include <SparseMatrix.hpp>

Public Member Functions

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

Detailed Description

template<typename Index, typename T>
requires (std::unsigned_integral<Index>)
class dsm::SparseMatrix< Index, T >

The SparseMatrix class represents a sparse matrix.

Template Parameters
IndexThe 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 Index , typename T >
requires (std::unsigned_integral<Index>)
dsm::SparseMatrix< Index, T >::SparseMatrix ( Index rows,
Index cols )

SparseMatrix constructor.

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

◆ SparseMatrix() [2/2]

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
dsm::SparseMatrix< Index, T >::SparseMatrix ( Index index)
explicit

SparseMatrix constructor - colum.

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

Member Function Documentation

◆ begin()

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

return the begin iterator of the matrix

Returns
the begin iterator

◆ contains() [1/2]

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
bool dsm::SparseMatrix< Index, T >::contains ( Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
bool dsm::SparseMatrix< Index, T >::contains ( Index i,
Index 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 Index , typename T >
std::unordered_map< Index, T >::const_iterator dsm::SparseMatrix< Index, T >::end ( ) const
inline

return the end iterator of the matrix

Returns
the end iterator

◆ erase() [1/2]

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::erase ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::erase ( Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::eraseColumn ( Index index)

remove a column from the matrix

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

◆ eraseRow()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::eraseRow ( Index index)

remove a row from the matrix

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

◆ getCol()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, T > dsm::SparseMatrix< Index, T >::getCol ( Index index,
bool keepIndex = false ) const

get a column as a column vector

Parameters
indexcolumn index
keepIndexif 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 keepIndex 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 Index , typename T >
Index dsm::SparseMatrix< Index, T >::getColDim ( ) const
inline

get the number of columns

Returns
number of columns

◆ getDegreeVector()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, int > dsm::SparseMatrix< Index, 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 Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, int > dsm::SparseMatrix< Index, T >::getLaplacian ( ) const

get the laplacian matrix

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

◆ getNormCols()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, double > dsm::SparseMatrix< Index, T >::getNormCols ( ) const

get a matrix of double with every column normalized to 1

Returns
a matrix of double

◆ getNormRows()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, double > dsm::SparseMatrix< Index, T >::getNormRows ( ) const

get a matrix of double with every row normalized to 1

Returns
a matrix of double

◆ getRow()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, T > dsm::SparseMatrix< Index, T >::getRow ( Index index,
bool keepIndex = false ) const

get a row as a row vector

Parameters
indexrow index
keepIndexif 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 keepIndex 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 Index , typename T >
Index dsm::SparseMatrix< Index, T >::getRowDim ( ) const
inline

get the number of rows

Returns
number of rows

◆ getStrengthVector()

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, double > dsm::SparseMatrix< Index, 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::insert ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::insert ( Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::insert_and_expand ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::insert_or_assign ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
void dsm::SparseMatrix< Index, T >::insert_or_assign ( Index 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 Index , typename T >
Index dsm::SparseMatrix< Index, T >::max_size ( ) const
inline

get the maximum number of elements in the matrix

Returns
maximum number of elements

◆ operator()() [1/4]

template<typename Index , typename T >
requires (std::unsigned_integral<Index>)
T & dsm::SparseMatrix< Index, T >::operator() ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
const T & dsm::SparseMatrix< Index, T >::operator() ( Index i,
Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
T & dsm::SparseMatrix< Index, T >::operator() ( Index 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 Index , typename T >
requires (std::unsigned_integral<Index>)
const T & dsm::SparseMatrix< Index, T >::operator() ( Index 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 Index , typename T >
template<typename I , typename U >
requires std::unsigned_integral<I>
SparseMatrix< Index, T > dsm::SparseMatrix< Index, T >::operator+ ( const SparseMatrix< I, 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 Index , typename T >
requires (std::unsigned_integral<Index>)
SparseMatrix< Index, T > dsm::SparseMatrix< Index, T >::operator++ ( )

transpose the matrix

Returns
the transposed matrix

◆ operator+=()

template<typename Index , typename T >
template<typename I , typename U >
requires std::unsigned_integral<I>
SparseMatrix & dsm::SparseMatrix< Index, T >::operator+= ( const SparseMatrix< I, 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 Index , typename T >
template<typename I , typename U >
requires std::unsigned_integral<I>
SparseMatrix< Index, T > dsm::SparseMatrix< Index, T >::operator- ( const SparseMatrix< I, 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 Index , typename T >
template<typename I , typename U >
requires std::unsigned_integral<I>
SparseMatrix & dsm::SparseMatrix< Index, T >::operator-= ( const SparseMatrix< I, 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 Index , typename T >
Index dsm::SparseMatrix< Index, 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: