|
ADS
Algorithms and Data Structures
|
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 | |
a sparse matrix on orthogonal circular lists
| using ADS::SparseMatrix::const_iterator = detail::OrthogonalIterator< Node > |
| 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
|
nodiscard |
the nonzeros of the column \( j \), by row
|
nodiscard |
the nonzeros of the column \( j \) as (row, value), by row
|
nodiscard |
the number \( n \) of columns
|
nodiscard |
the entry \( ( i , j ) \), 0 if it has no node, by a walk of the row \( i \)
|
nodiscard |
the number \( \mathit{nnz} \) of nonzero entries
| 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}} \]
|
nodiscard |
the nonzeros of the row \( i \), by column
|
nodiscard |
the nonzeros of the row \( i \) as (column, value), by column
|
nodiscard |
the number \( m \) of rows
| 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}} \]