Heidelberg Educational Numerics Library Version 0.27 (from 15 March 2021)
Classes | Public Types | Public Member Functions | Static Public Member Functions | Related Symbols | List of all members
hdnum::SparseMatrix< REAL > Class Template Reference

Sparse matrix Class with mathematical matrix operations. More...

#include <sparsematrix.hh>

Classes

class  builder
 
class  column_index_iterator
 
class  const_column_index_iterator
 
class  const_row_iterator
 
class  row_iterator
 

Public Types

using size_type = std::size_t
 Types used for array indices.
 
using column_iterator = typename std::vector<REAL>::iterator
 type of a regular column iterator (no access to indices)
 
using const_column_iterator = typename std::vector<REAL>::const_iterator
 type of a const regular column iterator (no access to indices)
 

Public Member Functions

 SparseMatrix ()=default
 default constructor (empty SparseMatrix)
 
 SparseMatrix (const size_type _rows, const size_type _cols)
 constructor with added dimensions and columns
 
size_type rowsize () const
 get number of rows of the matrix
 
size_type colsize () const
 get number of columns of the matrix
 
bool scientific () const
 pretty-print output properties
 
row_iterator begin ()
 get a (possibly modifying) row iterator for the sparse matrix
 
row_iterator end ()
 get a (possibly modifying) row iterator for the sparse matrix
 
const_row_iterator cbegin () const
 get a (non modifying) row iterator for the sparse matrix
 
const_row_iterator cend () const
 get a (non modifying) row iterator for the sparse matrix
 
const_row_iterator begin () const
 
const_row_iterator end () const
 
void scientific (bool b) const
 Switch between floating point (default=true) and fixed point (false) display.
 
size_type iwidth () const
 get index field width for pretty-printing
 
size_type width () const
 get data field width for pretty-printing
 
size_type precision () const
 get data precision for pretty-printing
 
void iwidth (size_type i) const
 set index field width for pretty-printing
 
void width (size_type i) const
 set data field width for pretty-printing
 
void precision (size_type i) const
 set data precision for pretty-printing
 
column_iterator find (const size_type row_index, const size_type col_index) const
 
bool exists (const size_type row_index, const size_type col_index) const
 
REALget (const size_type row_index, const size_type col_index)
 write access on matrix element A_ij using A.get(i,j)
 
const REALoperator() (const size_type row_index, const size_type col_index) const
 read-access on matrix element A_ij using A(i,j)
 
bool operator== (const SparseMatrix &other) const
 checks whether two matricies are equal based on values and dimension
 
bool operator!= (const SparseMatrix &other) const
 checks whether two matricies are unequal based on values and dimension
 
bool operator< (const SparseMatrix &other)=delete
 
bool operator> (const SparseMatrix &other)=delete
 
bool operator<= (const SparseMatrix &other)=delete
 
bool operator>= (const SparseMatrix &other)=delete
 
SparseMatrix transpose () const
 
SparseMatrix operator*= (const REAL scalar)
 
SparseMatrix operator/= (const REAL scalar)
 
template<class V >
void mv (Vector< V > &result, const Vector< V > &x) const
 matrix vector product y = A*x
 
Vector< REALoperator* (const Vector< REAL > &x) const
 matrix vector product A*x
 
template<class V >
void umv (Vector< V > &result, const Vector< V > &x) const
 update matrix vector product y += A*x
 
auto norm_infty () const
 calculate row sum norm
 
std::string to_string () const noexcept
 
void print () const noexcept
 
SparseMatrix< REALmatchingIdentity () const
 creates a matching identity
 

Static Public Member Functions

static SparseMatrix identity (const size_type dimN)
 identity for the matrix
 

Related Symbols

(Note that these are not member symbols.)

template<class REAL >
void identity (SparseMatrix< REAL > &A)
 

Detailed Description

template<typename REAL>
class hdnum::SparseMatrix< REAL >

Sparse matrix Class with mathematical matrix operations.

Constructor & Destructor Documentation

◆ SparseMatrix()

template<typename REAL >
hdnum::SparseMatrix< REAL >::SparseMatrix ( )
default

default constructor (empty SparseMatrix)

Example:

auto nRows = A.rowsize();
std::cout << "Matrix A has " << nRows << " rows." << std::endl;
Class with mathematical matrix operations.
Definition densematrix.hh:33
size_t rowsize() const
get number of rows of the matrix
Definition densematrix.hh:136

Output:

Matrix A has 0 rows.

Member Function Documentation

◆ begin() [1/2]

template<typename REAL >
row_iterator hdnum::SparseMatrix< REAL >::begin ( )
inline

get a (possibly modifying) row iterator for the sparse matrix

The iterator points to the first row in the matrix.

Example:

// A is of type hdnum::SparseMatrix<int> and contains some values
// the deduced variable type for row_it is
// hdnum::SparseMatrix<int>::row_iterator
// but thats way to long to type out ;)
for(auto row_it = A.begin(); row_it != A.end(); row_it++) {
for(auto val_it = row_it.begin(); val_it != row_it.end(); val_it++) {
*val_it = 1;
}
}

◆ begin() [2/2]

template<typename REAL >
const_row_iterator hdnum::SparseMatrix< REAL >::begin ( ) const
inline
See also
cbegin() const

◆ cbegin()

template<typename REAL >
const_row_iterator hdnum::SparseMatrix< REAL >::cbegin ( ) const
inline

get a (non modifying) row iterator for the sparse matrix

The iterator points to the first row in the matrix.

◆ cend()

template<typename REAL >
const_row_iterator hdnum::SparseMatrix< REAL >::cend ( ) const
inline

get a (non modifying) row iterator for the sparse matrix

The iterator points to the row one after the last one.

◆ colsize()

template<typename REAL >
size_type hdnum::SparseMatrix< REAL >::colsize ( ) const
inline

get number of columns of the matrix

Example:

auto nRows = A.colsize();
std::cout << "Matrix A has " << nRows << " rows." << std::endl;
size_t colsize() const
get number of columns of the matrix
Definition densematrix.hh:153

Output:

Matrix A has 4 rows.

◆ end() [1/2]

template<typename REAL >
row_iterator hdnum::SparseMatrix< REAL >::end ( )
inline

get a (possibly modifying) row iterator for the sparse matrix

The iterator points to the row one after the last one.

Example:

// A is of type hdnum::SparseMatrix<int> and contains some values
// the deduced variable type for row_it is
// hdnum::SparseMatrix<int>::row_iterator
// but thats way to long to type out ;)
for(auto row_it = A.begin(); row_it != A.end(); row_it++) {
for(auto val_it = row_it.begin(); val_it != row_it.end(); val_it++) {
*val_it = 1;
}
}

◆ end() [2/2]

template<typename REAL >
const_row_iterator hdnum::SparseMatrix< REAL >::end ( ) const
inline
See also
cend() const

◆ identity()

template<typename REAL >
static SparseMatrix hdnum::SparseMatrix< REAL >::identity ( const size_type dimN)
inlinestatic

identity for the matrix

Example:

// fixed point representation for all SparseMatrix objects
A.scientific(false);
A.width(8);
A.precision(3);
std::cout << "A=" << A << std::endl;
static SparseMatrix identity(const size_type dimN)
identity for the matrix
Definition sparsematrix.hh:886

Output:

A=
0        1        2        3
0     1.000    0.000    0.000    0.000
1     0.000    1.000    0.000    0.000
2     0.000    0.000    1.000    0.000
3     0.000    0.000    0.000    1.000

◆ matchingIdentity()

template<typename REAL >
SparseMatrix< REAL > hdnum::SparseMatrix< REAL >::matchingIdentity ( ) const
inline

creates a matching identity

Example:

auto B = A.matchingIdentity();
// fixed point representation for all SparseMatrix objects
A.scientific(false);
A.width(8);
A.precision(3);
std::cout << "A=" << A << std::endl;
std::size_t width() const
get data field width for pretty-printing
Definition densematrix.hh:185

Output:

A=
0        1        2        3
0     1.000    0.000    0.000    0.000
1     0.000    1.000    0.000    0.000
2     0.000    0.000    1.000    0.000
3     0.000    0.000    0.000    1.000

◆ mv()

template<typename REAL >
template<class V >
void hdnum::SparseMatrix< REAL >::mv ( Vector< V > & result,
const Vector< V > & x ) const
inline

matrix vector product y = A*x

Implements y = A*x where x and y are a vectors and A is a matrix

Parameters
[in]resultreference to the resulting Vector
[in]xconstant reference to a Vector

◆ norm_infty()

template<typename REAL >
auto hdnum::SparseMatrix< REAL >::norm_infty ( ) const
inline

calculate row sum norm

\[
  ||A||_{\infty} = max_{i = 1...m} \sum_{j=1}^n |a_{ij}|
\]

◆ operator*()

template<typename REAL >
Vector< REAL > hdnum::SparseMatrix< REAL >::operator* ( const Vector< REAL > & x) const
inline

matrix vector product A*x

Implements A*x where x is a vectors and A is a matrix

Parameters
[in]xconstant reference to a Vector

◆ operator*=()

template<typename REAL >
SparseMatrix hdnum::SparseMatrix< REAL >::operator*= ( const REAL scalar)
inline

Element-wise multiplication of the matrix

Parameters
[in]scalarwith same type as the matrix elements

◆ operator/=()

template<typename REAL >
SparseMatrix hdnum::SparseMatrix< REAL >::operator/= ( const REAL scalar)
inline

Element-wise division of the matrix

Parameters
[in]scalarwith same type as the matrix elements

◆ rowsize()

template<typename REAL >
size_type hdnum::SparseMatrix< REAL >::rowsize ( ) const
inline

get number of rows of the matrix

Example:

auto nRows = A.rowsize();
std::cout << "Matrix A has " << nRows << " rows." << std::endl;

Output:

Matrix A has 4 rows.

◆ scientific()

template<typename REAL >
void hdnum::SparseMatrix< REAL >::scientific ( bool b) const
inline

Switch between floating point (default=true) and fixed point (false) display.

Example:

// fixed point representation for all SparseMatrix objects objects
A.scientific(false);
A.width(8); A.precision(3); identity(A);
// Defines the identity matrix of the same dimension
std::cout << "A=" << A << std::endl;

Output:

A=
0        1        2        3
0     1.000    0.000    0.000    0.000
1     0.000    1.000    0.000    0.000
2     0.000    0.000    1.000    0.000
3     0.000    0.000    0.000    1.000

◆ umv()

template<typename REAL >
template<class V >
void hdnum::SparseMatrix< REAL >::umv ( Vector< V > & result,
const Vector< V > & x ) const
inline

update matrix vector product y += A*x

Implements y += A*x where x and y are a vectors and A is a matrix

Parameters
[in]resultreference to the resulting Vector
[in]xconstant reference to a Vector

Friends And Related Symbol Documentation

◆ identity()

template<class REAL >
void identity ( SparseMatrix< REAL > & A)
related


Function: make identity matrix

template<class T>
inline void identity (SparseMatrix<T> &A)
Parameters
[in]Areference to a SparseMatrix that shall be filled with entries

Example:

// fixed point representation for all DenseMatrix objects
A.scientific(false);
A.width(10);
A.precision(5);
std::cout << "A=" << A << std::endl;
std::size_t precision() const
get data precision for pretty-printing
Definition densematrix.hh:188

Output:

A=
0          1          2          3
0     1.00000    0.00000    0.00000    0.00000
1     0.00000    1.00000    0.00000    0.00000
2     0.00000    0.00000    1.00000    0.00000
3     0.00000    0.00000    0.00000    1.00000

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