ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SparseAssembly.h

The assembly of a sparse matrix from its triplets, beyond the book. More...

Files

file  ADS/numeric/SparseAssembly.h
 The assembly of a sparse matrix from its triplets, beyond the book: the entries \( ( i , j , v ) \) in any order, with repetitions, become the arrays of the compressed form that the solvers of linear programs take (\( \mathit{beg} \), \( \mathit{ind} \) and \( \mathit{val} \), by columns or by rows), written in the arrays of the caller, with no matrix object in between and no other allocation than a workspace that a caller may keep from one assembly to the next.

Classes

struct  ADS::AssemblyWorkspace< I, V >
 the scratch of compressTriplets(), which keeps its memory from one call to the next More...

Functions

template<class I, class V, class J>
std::size_t ADS::compressTriplets (std::size_t nmajor, std::size_t nminor, std::size_t z, const J *major, const J *minor, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed form, by major index, of the \( z \) triplets \( ( \mathit{major}_k , \mathit{minor}_k , \mathit{value}_k ) \) of a matrix of \( \mathit{nmajor} \times \mathit{nminor} \), the values of equal places summed; the number of the entries
template<class I, class V, class J>
std::size_t ADS::compressTriplets (std::size_t nmajor, std::size_t nminor, std::size_t z, const J *major, const J *minor, const V *value, I *beg, I *ind, V *val)
 the form of compressTriplets() above, with a workspace of its own
template<class I, class V, class J>
std::size_t ADS::assembleCsc (std::size_t m, std::size_t n, std::size_t z, const J *row, const J *col, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed sparse columns of the \( m \times n \) matrix of the \( z \) triplets \( ( \mathit{row}[ k ] , \mathit{col}[ k ] , \mathit{value}[ k ] ) \): beg has \( n + 1 \) places, ind holds the rows; the number of the entries
template<class I, class V, class J>
std::size_t ADS::assembleCsr (std::size_t m, std::size_t n, std::size_t z, const J *row, const J *col, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed sparse rows of the same: beg has \( m + 1 \) places, ind holds the columns; the number of the entries

Detailed description

The assembly of a sparse matrix from its triplets, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Function documentation

◆ assembleCsc()

template<class I, class V, class J>
std::size_t ADS::assembleCsc ( std::size_t m,
std::size_t n,
std::size_t z,
const J * row,
const J * col,
const V * value,
I * beg,
I * ind,
V * val,
AssemblyWorkspace< I, V > & work )

the compressed sparse columns of the \( m \times n \) matrix of the \( z \) triplets \( ( \mathit{row}[ k ] , \mathit{col}[ k ] , \mathit{value}[ k ] ) \): beg has \( n + 1 \) places, ind holds the rows; the number of the entries

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{assembleCsc}( m , n , \mathit{row} , \mathit{col} , \mathit{value} ) \cr 2 & \quad \textbf{return}\ \mathrm{compressTriplets}( \mathit{col} , \mathit{row} , \mathit{value} )\quad \triangleright\ n \times m \end{array}} \]

◆ assembleCsr()

template<class I, class V, class J>
std::size_t ADS::assembleCsr ( std::size_t m,
std::size_t n,
std::size_t z,
const J * row,
const J * col,
const V * value,
I * beg,
I * ind,
V * val,
AssemblyWorkspace< I, V > & work )

the compressed sparse rows of the same: beg has \( m + 1 \) places, ind holds the columns; the number of the entries

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{assembleCsr}( m , n , \mathit{row} , \mathit{col} , \mathit{value} ) \cr 2 & \quad \textbf{return}\ \mathrm{compressTriplets}( \mathit{row} , \mathit{col} , \mathit{value} )\quad \triangleright\ m \times n \end{array}} \]

◆ compressTriplets() [1/2]

template<class I, class V, class J>
std::size_t ADS::compressTriplets ( std::size_t nmajor,
std::size_t nminor,
std::size_t z,
const J * major,
const J * minor,
const V * value,
I * beg,
I * ind,
V * val )

the form of compressTriplets() above, with a workspace of its own

◆ compressTriplets() [2/2]

template<class I, class V, class J>
std::size_t ADS::compressTriplets ( std::size_t nmajor,
std::size_t nminor,
std::size_t z,
const J * major,
const J * minor,
const V * value,
I * beg,
I * ind,
V * val,
AssemblyWorkspace< I, V > & work )

the compressed form, by major index, of the \( z \) triplets \( ( \mathit{major}_k , \mathit{minor}_k , \mathit{value}_k ) \) of a matrix of \( \mathit{nmajor} \times \mathit{nminor} \), the values of equal places summed; the number of the entries

The result is \( \mathit{beg}[ 0 .. \mathit{nmajor} ] \), and the minor indices and the values in ind and val, increasing in each major index. In pseudocode (the counting sort of section 5.2 of Knuth and the sum of the duplicates of Davis, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{compressTriplets}( \mathit{major} , \mathit{minor} , \mathit{value} ) \cr 2 & \quad \mathit{beg} \gets \text{the prefix sums of the counts of the } \text{major indices} \cr 3 & \quad \textbf{for}\ k \gets 0\ \textbf{to}\ z - 1\ \textbf{do} \cr 4 & \qquad q \gets \mathit{at}[ \mathit{major}[ k ] ]++; \ \mathit{ind}[ q ] \gets \mathit{minor}[ k ];\ \mathit{val}[ q ] \gets \mathit{value}[ k ] \cr 5 & \quad o \gets 0;\ \mathit{mark} \gets \text{all } 0 \cr 6 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ \mathit{nmajor} - 1 \ \textbf{do} \cr 7 & \qquad f \gets o \cr 8 & \qquad \textbf{foreach}\ \text{entry } ( j , v ) \text{ of } i \text{, in the order of the input}\ \textbf{do} \cr 9 & \qquad\quad \textbf{if}\ \mathit{mark}[ j ] > f\ \textbf{then} \ \mathit{val}[ \mathit{mark}[ j ] - 1 ] \gets \mathit{val}[ \mathit{mark}[ j ] - 1 ] + v \cr 10 & \qquad\quad \textbf{else}\ \mathit{mark}[ j ] \gets o + 1;\ ( \mathit{ind}[ o ] , \mathit{val}[ o ] ) \gets ( j , v );\ o \gets o + 1 \cr 11 & \qquad \text{sort the entries } f .. o - 1 \text{ by minor index} \text{, by insertion up to 32} \cr 12 & \qquad \mathit{beg}[ i ] \gets f \cr 13 & \quad \mathit{beg}[ \mathit{nmajor} ] \gets o;\ \textbf{return}\ o \end{array}} \]

Precondition
the indices are below \( \mathit{nmajor} \) and \( \mathit{nminor} \), beg has \( \mathit{nmajor} + 1 \) places, ind and val at least \( z \), and \( z \) fits I