Processing math: 100%
Dynamical system model
All Classes Namespaces Files Functions Pages
dsm::SparseMatrix< T > Class Template Reference

The SparseMatrix class represents a sparse square matrix in CSR format. More...

#include <SparseMatrix.hpp>

Public Member Functions

 SparseMatrix ()
 Construct a new SparseMatrix object.
 
 SparseMatrix (std::string const &fileName, std::string const &fileType="bin")
 Construct a new SparseMatrix object using the read method.
 
 SparseMatrix (const SparseMatrix &other)=default
 Copy constructor.
 
 SparseMatrix (SparseMatrix &&other)=default
 Move constructor.
 
operator() (Id row, Id col) const
 Returns the element at the specified row and column.
 
SparseMatrixoperator= (const SparseMatrix &other)=default
 
SparseMatrixoperator= (SparseMatrix &&other)=default
 
bool operator== (const SparseMatrix &other) const
 
size_t size () const noexcept
 Returns the number of (non-zero) elements in the matrix.
 
bool empty () const noexcept
 Returns true if the matrix is empty.
 
size_t n () const noexcept
 Returns the number of rows (columns) in the matrix.
 
std::map< Id, T > row (Id idRow) const
 Returns a row of the matrix as a map of column indices and values.
 
std::map< Id, T > col (Id idCol) const
 Returns a column of the matrix as a map of row indices and values.
 
void setDefaultValue (T value)
 Set the default value of the matrix (default is 0)
 
void insert (Id row, Id col, T value)
 Insert a new element of given value at the specified row and column.
 
void normalize (const axis_t axis=true, const T value=static_cast< T >(1))
 Normalize the rows of the matrix.
 
void read (std::string const &fileName, std::string const &fileType="bin", std::unordered_map< std::string, Id > const &mapping={})
 Read the sparse matrix from a binary file.
 
void save (std::string const &fileName) const
 Save the sparse matrix to a binary file.
 

Detailed Description

template<typename T>
requires (std::is_arithmetic_v<T>)
class dsm::SparseMatrix< T >

The SparseMatrix class represents a sparse square matrix in CSR format.

This implementations is optimized for row access.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/3]

template<typename T>
requires (std::is_arithmetic_v<T>)
dsm::SparseMatrix< T >::SparseMatrix ( std::string const & fileName,
std::string const & fileType = "bin" )

Construct a new SparseMatrix object using the read method.

Parameters
fileNameThe name of the file containing the sparse matrix
fileTypeThe type of the file (default is "bin")

◆ SparseMatrix() [2/3]

template<typename T>
dsm::SparseMatrix< T >::SparseMatrix ( const SparseMatrix< T > & other)
default

Copy constructor.

Parameters
otherThe SparseMatrix to copy

◆ SparseMatrix() [3/3]

template<typename T>
dsm::SparseMatrix< T >::SparseMatrix ( SparseMatrix< T > && other)
default

Move constructor.

Parameters
otherThe SparseMatrix to move

Member Function Documentation

◆ col()

template<typename T>
requires (std::is_arithmetic_v<T>)
std::map< Id, T > dsm::SparseMatrix< T >::col ( Id idCol) const

Returns a column of the matrix as a map of row indices and values.

Parameters
idColThe column index
Returns
A map of row indices and values for the specified column

◆ empty()

template<typename T>
bool dsm::SparseMatrix< T >::empty ( ) const
inlinenoexcept

Returns true if the matrix is empty.

Returns
true if the matrix is empty, false otherwise

◆ insert()

template<typename T>
requires (std::is_arithmetic_v<T>)
void dsm::SparseMatrix< T >::insert ( Id row,
Id col,
T value )

Insert a new element of given value at the specified row and column.

Parameters
rowThe row index of the element
colThe column index of the element
valueThe value of the element

This function actually inserts element a_{ij} of the sparse matrix. Where i is the row index and j is the column index. The function will automatically resize the matrix to fit the new element.

◆ n()

template<typename T>
size_t dsm::SparseMatrix< T >::n ( ) const
inlinenoexcept

Returns the number of rows (columns) in the matrix.

Returns
The number of rows (columns) in the matrix

◆ normalize()

template<typename T>
requires (std::is_arithmetic_v<T>)
void dsm::SparseMatrix< T >::normalize ( const axis_t axis = true,
const T value = static_cast<T>(1) )

Normalize the rows of the matrix.

Parameters
axisIf 1, normalize the rows, otherwise (0) normalize the columns
valueThe value to normalize the rows to (default is 1)

The function will normalize the rows of the matrix to have the sum equal to the given value.

◆ operator()()

template<typename T>
requires (std::is_arithmetic_v<T>)
T dsm::SparseMatrix< T >::operator() ( Id row,
Id col ) const

Returns the element at the specified row and column.

Parameters
rowThe row index of the element
colThe column index of the element
Returns
The element at the specified row and column

◆ read()

template<typename T>
requires (std::is_arithmetic_v<T>)
void dsm::SparseMatrix< T >::read ( std::string const & fileName,
std::string const & fileType = "bin",
std::unordered_map< std::string, Id > const & mapping = {} )

Read the sparse matrix from a binary file.

Parameters
fileNameThe name of the file containing the sparse matrix
fileTypeThe type of the file (default is "bin")
mappingA mapping of the indices from strings to 0 -> N-1
Exceptions
std::runtime_errorif the file cannot be opened

◆ row()

template<typename T>
requires (std::is_arithmetic_v<T>)
std::map< Id, T > dsm::SparseMatrix< T >::row ( Id idRow) const

Returns a row of the matrix as a map of column indices and values.

Parameters
idRowThe row index
Returns
A map of column indices and values for the specified row

◆ save()

template<typename T>
requires (std::is_arithmetic_v<T>)
void dsm::SparseMatrix< T >::save ( std::string const & fileName) const

Save the sparse matrix to a binary file.

Parameters
fileNameThe name of the file to save the sparse matrix
Exceptions
std::runtime_errorif the file cannot be opened

◆ setDefaultValue()

template<typename T>
void dsm::SparseMatrix< T >::setDefaultValue ( T value)
inline

Set the default value of the matrix (default is 0)

Parameters
valueThe default value of the matrix

◆ size()

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

Returns the number of (non-zero) elements in the matrix.

Returns
The number of elements in the matrix

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