Traffic Flow Dynamics Model
SparseMatrix< T > Class Template Reference

SparseMatrix class v1.7.0 by Grufoony. More...

#include <SparseMatrix.hpp>

Public Member Functions

 SparseMatrix (int rows, int cols)
 SparseMatrix constructor. More...
 
 SparseMatrix (int index)
 SparseMatrix constructor - colum. More...
 
void setSeed (int seed) noexcept
 Set random seed. More...
 
void insert (int i, int j, T value)
 insert a value in the matrix More...
 
void insert (int i, T value)
 insert a value in the matrix More...
 
void insert_or_assign (int i, int j, T value)
 insert a value in the matrix. If the element already exist, it overwrites it More...
 
void insert_or_assign (int index, T value)
 insert a value in the matrix. If the element already exist, it overwrites it More...
 
void erase (int i, int j)
 remove a value from the matrix More...
 
void eraseRow (int index)
 remove a row from the matrix More...
 
void eraseColumn (int index)
 remove a column from the matrix More...
 
void clear () noexcept
 
bool contains (int i, int j) const
 check if the element is non zero More...
 
bool contains (int const index) const
 check if the element is non zero More...
 
SparseMatrix< int > getDegreeVector ()
 get the input degree of all nodes More...
 
SparseMatrix< double > getStrengthVector ()
 get the strength of all nodes More...
 
SparseMatrix< int > getLaplacian ()
 get the laplacian matrix More...
 
SparseMatrix getRow (int index) const
 get a row as a row vector More...
 
SparseMatrix getCol (int index) const
 get a column as a column vector More...
 
std::pair< int, T > getRndRowElement (int index)
 get a random non-zero element from a row More...
 
std::pair< int, T > getRndColElement (int index)
 get a random non-zero element from a column More...
 
std::pair< int, T > getRndElement ()
 get a random non-zero element from the matrix More...
 
SparseMatrix< double > getNormRows () const
 get a matrix of double with every row normalized to 1 More...
 
SparseMatrix< double > getNormCols () const
 get a matrix of double with every column normalized to 1 More...
 
int getRowDim () const noexcept
 get the number of rows More...
 
int getColDim () const noexcept
 get the number of columns More...
 
int size () const noexcept
 get the number of non zero elements in the matrix More...
 
int max_size () const noexcept
 get the maximum number of elements in the matrix More...
 
at (int i, int j) const
 access an element of the matrix More...
 
at (int index) const
 access an element of the matrix More...
 
void symmetrize ()
 symmetrize the matrix
 
void print () const noexcept
 print the matrix in standard output
 
void fprint (std::string const &filename) const noexcept
 print the matrix on a file More...
 
std::unordered_map< int, T >::const_iterator begin () const noexcept
 return the begin iterator of the matrix More...
 
std::unordered_map< int, T >::const_iterator end () const noexcept
 return the end iterator of the matrix More...
 
T const & operator() (int i, int j)
 access an element of the matrix More...
 
T const & operator() (int index)
 access an element of the matrix More...
 
template<typename U >
SparseMatrix operator+ (const SparseMatrix< U > &other) const
 sum of two matrices More...
 
template<typename U >
SparseMatrix operator- (const SparseMatrix< U > &other) const
 difference of two matrices More...
 
SparseMatrix operator++ ()
 transpose the matrix More...
 
template<typename U >
SparseMatrixoperator+= (const SparseMatrix< U > &other)
 sum of two matrices More...
 
template<typename U >
SparseMatrixoperator-= (const SparseMatrix< U > &other)
 difference of two matrices More...
 

Static Public Member Functions

static void encode (std::string const &filename)
 Function to read a matrix from a file and rewrite it in a listed way. More...
 
static void decode (std::string const &filename)
 Function to read a matrix from a file and rewrite it as a full matrix. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const SparseMatrix &m)
 print the matrix on a stream
 
std::istream & operator>> (std::istream &is, SparseMatrix &m)
 read the matrix from a stream
 

Detailed Description

template<typename T>
class SparseMatrix< T >

SparseMatrix class v1.7.0 by Grufoony.

This class implements a sparse matrix. The matrix is stored in a compressed row format. ++ 20 requiered.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/2]

template<typename T >
SparseMatrix< T >::SparseMatrix ( int  rows,
int  cols 
)
inline

SparseMatrix constructor.

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

◆ SparseMatrix() [2/2]

template<typename T >
SparseMatrix< T >::SparseMatrix ( int  index)
inline

SparseMatrix constructor - colum.

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

Member Function Documentation

◆ at() [1/2]

template<typename T >
T SparseMatrix< T >::at ( int  i,
int  j 
) const
inline

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

◆ at() [2/2]

template<typename T >
T SparseMatrix< T >::at ( int  index) const
inline

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

◆ begin()

template<typename T >
std::unordered_map<int, T>::const_iterator SparseMatrix< T >::begin ( ) const
inlinenoexcept

return the begin iterator of the matrix

Returns
the begin iterator

◆ contains() [1/2]

template<typename T >
bool SparseMatrix< T >::contains ( int const  index) const
inline

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 SparseMatrix< T >::contains ( int  i,
int  j 
) const
inline

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

◆ decode()

template<typename T >
static void SparseMatrix< T >::decode ( std::string const &  filename)
inlinestatic

Function to read a matrix from a file and rewrite it as a full matrix.

Parameters
filenamename of the file

◆ encode()

template<typename T >
static void SparseMatrix< T >::encode ( std::string const &  filename)
inlinestatic

Function to read a matrix from a file and rewrite it in a listed way.

Parameters
filenamename of the file

◆ end()

template<typename T >
std::unordered_map<int, T>::const_iterator SparseMatrix< T >::end ( ) const
inlinenoexcept

return the end iterator of the matrix

Returns
the end iterator

◆ erase()

template<typename T >
void SparseMatrix< T >::erase ( int  i,
int  j 
)
inline

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

◆ eraseColumn()

template<typename T >
void SparseMatrix< T >::eraseColumn ( int  index)
inline

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 SparseMatrix< T >::eraseRow ( int  index)
inline

remove a row from the matrix

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

◆ fprint()

template<typename T >
void SparseMatrix< T >::fprint ( std::string const &  filename) const
inlinenoexcept

print the matrix on a file

Parameters
filenamename of the file

◆ getCol()

template<typename T >
SparseMatrix SparseMatrix< T >::getCol ( int  index) const
inline

get a column as a column vector

Parameters
indexcolumn index
Returns
a column vector
Exceptions
std::out_of_rangeif the index is out of range

◆ getColDim()

template<typename T >
int SparseMatrix< T >::getColDim ( ) const
inlinenoexcept

get the number of columns

Returns
number of columns

◆ getDegreeVector()

template<typename T >
SparseMatrix<int> SparseMatrix< T >::getDegreeVector ( )
inline

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> SparseMatrix< T >::getLaplacian ( )
inline

get the laplacian matrix

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

◆ getNormCols()

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

get a matrix of double with every column normalized to 1

Returns
a matrix of double

◆ getNormRows()

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

get a matrix of double with every row normalized to 1

Returns
a matrix of double

◆ getRndColElement()

template<typename T >
std::pair<int, T> SparseMatrix< T >::getRndColElement ( int  index)
inline

get a random non-zero element from a column

Parameters
indexcolumn index
Returns
a pair containing the row index and the value
Exceptions
std::out_of_rangeif the index is out of range

◆ getRndElement()

template<typename T >
std::pair<int, T> SparseMatrix< T >::getRndElement ( )
inline

get a random non-zero element from the matrix

Returns
a pair containing the row index and the value
Exceptions
std::runtime_errorif the matrix is empty

◆ getRndRowElement()

template<typename T >
std::pair<int, T> SparseMatrix< T >::getRndRowElement ( int  index)
inline

get a random non-zero element from a row

Parameters
indexrow index
Returns
a pair containing the column index and the value
Exceptions
std::out_of_rangeif the index is out of range

◆ getRow()

template<typename T >
SparseMatrix SparseMatrix< T >::getRow ( int  index) const
inline

get a row as a row vector

Parameters
indexrow index
Returns
a row vector
Exceptions
std::out_of_rangeif the index is out of range

◆ getRowDim()

template<typename T >
int SparseMatrix< T >::getRowDim ( ) const
inlinenoexcept

get the number of rows

Returns
number of rows

◆ getStrengthVector()

template<typename T >
SparseMatrix<double> SparseMatrix< T >::getStrengthVector ( )
inline

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 SparseMatrix< T >::insert ( int  i,
int  j,
value 
)
inline

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 SparseMatrix< T >::insert ( int  i,
value 
)
inline

insert a value in the matrix

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

◆ insert_or_assign() [1/2]

template<typename T >
void SparseMatrix< T >::insert_or_assign ( int  i,
int  j,
value 
)
inline

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 SparseMatrix< T >::insert_or_assign ( int  index,
value 
)
inline

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 >
int SparseMatrix< T >::max_size ( ) const
inlinenoexcept

get the maximum number of elements in the matrix

Returns
maximum number of elements

◆ operator()() [1/2]

template<typename T >
T const& SparseMatrix< T >::operator() ( int  i,
int  j 
)
inline

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/2]

template<typename T >
T const& SparseMatrix< T >::operator() ( int  index)
inline

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 SparseMatrix< T >::operator+ ( const SparseMatrix< U > &  other) const
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 SparseMatrix< T >::operator++ ( )
inline

transpose the matrix

Returns
the transposed matrix

◆ operator+=()

template<typename T >
template<typename U >
SparseMatrix& 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 >
template<typename U >
SparseMatrix SparseMatrix< T >::operator- ( const SparseMatrix< U > &  other) const
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& 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

◆ setSeed()

template<typename T >
void SparseMatrix< T >::setSeed ( int  seed)
inlinenoexcept

Set random seed.

Parameters
seedseed

◆ size()

template<typename T >
int SparseMatrix< T >::size ( ) const
inlinenoexcept

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: