ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SparseMatrix Class Reference

a sparse matrix on orthogonal circular lists More...

#include <ADS/numeric/SparseMatrix.h>

Public types

using const_iterator = detail::OrthogonalIterator< Node >

Public member functions

 SparseMatrix (std::size_t m, std::size_t n)
 the zero matrix of \( m \) rows and \( n \) columns, the \( m + n \) head nodes alone
std::size_t rows (void) const
 the number \( m \) of rows
std::size_t cols (void) const
 the number \( n \) of columns
std::size_t nonzeros (void) const
 the number \( \mathit{nnz} \) of nonzero entries
double get (std::size_t i, std::size_t j) const
 the entry \( ( i , j ) \), 0 if it has no node, by a walk of the row \( i \)
void set (std::size_t i, std::size_t j, double v)
 sets the entry \( ( i , j ) \) to \( v \), removing its node if \( v \) is 0
std::vector< std::pair< std::size_t, double > > row (std::size_t i) const
 the nonzeros of the row \( i \), by column
std::vector< std::pair< std::size_t, double > > col (std::size_t j) const
 the nonzeros of the column \( j \), by row
void pivot (std::size_t r, std::size_t c, double eps=0)
 the pivot step on the entry \( ( r , c ) \); the entries of absolute value at most \( \varepsilon \) vanish
std::ranges::subrange< const_iterator > rowEntries (std::size_t i) const
 the nonzeros of the row \( i \) as (column, value), by column
std::ranges::subrange< const_iterator > colEntries (std::size_t j) const
 the nonzeros of the column \( j \) as (row, value), by row

Detailed description

a sparse matrix on orthogonal circular lists

Member typedef documentation

◆ const_iterator

using ADS::SparseMatrix::const_iterator = detail::OrthogonalIterator< Node >

Constructor and destructor documentation

◆ SparseMatrix()

ADS::SparseMatrix::SparseMatrix ( std::size_t m,
std::size_t n )

the zero matrix of \( m \) rows and \( n \) columns, the \( m + n \) head nodes alone

Member function documentation

◆ col()

std::vector< std::pair< std::size_t, double > > ADS::SparseMatrix::col ( std::size_t j) const
nodiscard

the nonzeros of the column \( j \), by row

Precondition
\( j < n \)

◆ colEntries()

std::ranges::subrange< const_iterator > ADS::SparseMatrix::colEntries ( std::size_t j) const
nodiscard

the nonzeros of the column \( j \) as (row, value), by row

Precondition
\( j < n \)

◆ cols()

std::size_t ADS::SparseMatrix::cols ( void ) const
nodiscard

the number \( n \) of columns

◆ get()

double ADS::SparseMatrix::get ( std::size_t i,
std::size_t j ) const
nodiscard

the entry \( ( i , j ) \), 0 if it has no node, by a walk of the row \( i \)

Precondition
\( i < m \) and \( j < n \)

◆ nonzeros()

std::size_t ADS::SparseMatrix::nonzeros ( void ) const
nodiscard

the number \( \mathit{nnz} \) of nonzero entries

◆ pivot()

void ADS::SparseMatrix::pivot ( std::size_t r,
std::size_t c,
double eps = 0 )

the pivot step on the entry \( ( r , c ) \); the entries of absolute value at most \( \varepsilon \) vanish

In pseudocode (Algorithm 2.2.6S of Knuth, beyond the book), \( R \) and \( C \) the nonzeros of the pivot row and column before the step, by increasing index; lines 5 and 7 read and write the entry by walks of its row and of its column that resume where the previous entry left them:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{pivot}( r , c , \varepsilon ) \cr 2 & \quad a \gets \mathrm{get}( r , c ) \cr 3 & \quad \textbf{foreach}\ ( i , c_i ) \in C \text{, } i \ne r \ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ ( j , b_j ) \in R \text{, } j \ne c \ \textbf{do} \cr 5 & \qquad\quad d \gets \mathrm{get}( i , j ) - b_j c_i / a \cr 6 & \qquad\quad \textbf{if}\ |d| \le \varepsilon\ \textbf{then}\ d \gets 0 \cr 7 & \qquad\quad \mathrm{set}( i , j , d ) \cr 8 & \quad \textbf{foreach}\ ( j , b_j ) \in R \text{, } j \ne c \ \textbf{do}\ \mathrm{set}( r , j , b_j / a ) \cr 9 & \quad \textbf{foreach}\ ( i , c_i ) \in C \text{, } i \ne r \ \textbf{do}\ \mathrm{set}( i , c , - c_i / a ) \cr 10 & \quad \mathrm{set}( r , c , 1 / a ) \end{array}} \]

Precondition
\( r < m \), \( c < n \) and the entry \( ( r , c ) \) is not 0

◆ row()

std::vector< std::pair< std::size_t, double > > ADS::SparseMatrix::row ( std::size_t i) const
nodiscard

the nonzeros of the row \( i \), by column

Precondition
\( i < m \)

◆ rowEntries()

std::ranges::subrange< const_iterator > ADS::SparseMatrix::rowEntries ( std::size_t i) const
nodiscard

the nonzeros of the row \( i \) as (column, value), by column

Precondition
\( i < m \)

◆ rows()

std::size_t ADS::SparseMatrix::rows ( void ) const
nodiscard

the number \( m \) of rows

◆ set()

void ADS::SparseMatrix::set ( std::size_t i,
std::size_t j,
double v )

sets the entry \( ( i , j ) \) to \( v \), removing its node if \( v \) is 0

In pseudocode (section 2.2.6 of Knuth, beyond the book), \( p \) and \( u \) the nodes before the place on its row and on its column:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{set}( i , j , v ) \cr 2 & \quad q \gets \text{the node of } ( i , j ) \text{ on the row } i \text{, or } \textbf{nil} \cr 3 & \quad \textbf{if}\ q \ne \textbf{nil}\ \textbf{and}\ v \ne 0 \ \textbf{then}\ q.\mathit{val} \gets v \cr 4 & \quad \textbf{else if}\ q \ne \textbf{nil}\ \textbf{then} \ p.\mathit{right} \gets q.\mathit{right};\ u.\mathit{down} \gets q.\mathit{down} \cr 5 & \quad \textbf{else if}\ v \ne 0\ \textbf{then} \cr 6 & \qquad q \gets \text{a new node } ( i , j , v ) \text{ with } q.\mathit{right} \gets p.\mathit{right} \text{, } q.\mathit{down} \gets u.\mathit{down} \cr 7 & \qquad p.\mathit{right} \gets q;\ u.\mathit{down} \gets q \end{array}} \]

Precondition
\( i < m \) and \( j < n \)

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