ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BasicCsrMatrix< T, I, P > Class Template Reference

a sparse \( m \times n \) matrix of values T in compressed sparse rows, the columns of the type I and the offsets of the rows of the type P More...

#include <ADS/numeric/CsrMatrix.h>

Public types

using Value = T
 the type of the values
using Index = I
 the type of the columns
using Offset = P
 the type of the offsets of the rows
using Triplet = std::tuple< std::size_t , std::size_t , T >
 an entry \( ( i , j , v ) \)

Public member functions

 BasicCsrMatrix (std::size_t m=0, std::size_t n=0)
 the \( m \times n \) zero matrix
 BasicCsrMatrix (std::size_t m, std::size_t n, std::vector< Triplet > E)
 the \( m \times n \) matrix of the triplets \( ( i , j , v ) \), the values of equal places summed
 BasicCsrMatrix (const SparseMatrix &S)
 the matrix of the orthogonal lists of the book, row by row
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 the entries kept
T get (std::size_t i, std::size_t j) const
 the entry \( ( i , j ) \), by a binary search in the row
std::vector< T > multiply (const std::vector< T > &x) const
 the product \( y = A x \), a new vector
void multiply (const T *x, T *y) const
 the product \( y = A x \) into \( y \), which must have \( m \) entries
BasicCsrMatrix transpose (void) const
 the transpose, \( n \times m \)
const P * rowStart (void) const
 the \( m + 1 \) offsets of the rows
const I * columns (void) const
 the column of each nonzero, row by row
const T * values (void) const
 the value of each nonzero, row by row
bool operator== (const BasicCsrMatrix &) const =default
 true if the two matrices have the same sizes and the same entries kept
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]

Static public member functions

static constexpr std::uint64_t persistTag (void)
 the types of the values, of the columns and of the offsets, in a file

Static public attributes

static constexpr std::string_view persistName = "CsrMatrix"
 the name of the structure in a file [see Persistence.h]

Detailed description

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
class ADS::BasicCsrMatrix< T, I, P >

a sparse \( m \times n \) matrix of values T in compressed sparse rows, the columns of the type I and the offsets of the rows of the type P

Member typedef documentation

◆ Index

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
using ADS::BasicCsrMatrix< T, I, P >::Index = I

the type of the columns

◆ Offset

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
using ADS::BasicCsrMatrix< T, I, P >::Offset = P

the type of the offsets of the rows

◆ Triplet

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
using ADS::BasicCsrMatrix< T, I, P >::Triplet = std::tuple< std::size_t , std::size_t , T >

an entry \( ( i , j , v ) \)

◆ Value

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
using ADS::BasicCsrMatrix< T, I, P >::Value = T

the type of the values

Constructor and destructor documentation

◆ BasicCsrMatrix() [1/3]

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
ADS::BasicCsrMatrix< T, I, P >::BasicCsrMatrix ( std::size_t m = 0,
std::size_t n = 0 )

the \( m \times n \) zero matrix

◆ BasicCsrMatrix() [2/3]

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
ADS::BasicCsrMatrix< T, I, P >::BasicCsrMatrix ( std::size_t m,
std::size_t n,
std::vector< Triplet > E )

the \( m \times n \) matrix of the triplets \( ( i , j , v ) \), the values of equal places summed

In pseudocode (Saad, section 3.4, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{CsrMatrix}( m , n , T ) \cr 2 & \quad \mathit{row}[ i + 1 ] \gets \text{the number of the triplets} \text{ of the row } i \text{, for each } i \cr 3 & \quad \mathit{row} \gets \text{its prefix sums}\quad \triangleright \ \text{the counting sort} \cr 4 & \quad E \gets \text{the pairs } ( j , v ) \text{ of } T \text{ put } \text{by row at the offsets} \cr 5 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 1\ \textbf{do} \cr 6 & \qquad \text{sort the pairs of the row } i \text{ by column}; \ \mathit{row}[ i ] \gets | \mathit{col} | \cr 7 & \qquad \text{append them to } \mathit{col} \text{ and } \mathit{val} \text{, a repeated column added to the last} \cr 8 & \quad \mathit{row}[ m ] \gets | \mathit{col} | \end{array}} \]

Precondition
\( i < m \) and \( j < n \) for each triplet, the columns fit I and the number of the triplets fits P

◆ BasicCsrMatrix() [3/3]

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
ADS::BasicCsrMatrix< T, I, P >::BasicCsrMatrix ( const SparseMatrix & S)
explicit

the matrix of the orthogonal lists of the book, row by row

Precondition
the columns fit I and the nonzeros P

Member function documentation

◆ cols()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
std::size_t ADS::BasicCsrMatrix< T, I, P >::cols ( void ) const
inlinenodiscard

the number \( n \) of columns

◆ columns()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
const I * ADS::BasicCsrMatrix< T, I, P >::columns ( void ) const
inlinenodiscard

the column of each nonzero, row by row

◆ get()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
T ADS::BasicCsrMatrix< T, I, P >::get ( std::size_t i,
std::size_t j ) const
nodiscard

the entry \( ( i , j ) \), by a binary search in the row

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

◆ multiply() [1/2]

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
std::vector< T > ADS::BasicCsrMatrix< T, I, P >::multiply ( const std::vector< T > & x) const
nodiscard

the product \( y = A x \), a new vector

Precondition
\( x \) has \( n \) entries

◆ multiply() [2/2]

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
void ADS::BasicCsrMatrix< T, I, P >::multiply ( const T * x,
T * y ) const

the product \( y = A x \) into \( y \), which must have \( m \) entries

In pseudocode (Saad, section 3.4, beyond the book), one pass on the nonzeros:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{multiply}( x , y ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 1\ \textbf{do} \cr 3 & \qquad y_i \gets \sum_{p = \mathit{row}[ i ]}^{\mathit{row}[ i + 1 ] - 1} \mathit{val}[ p ] \, x_{\mathit{col}[ p ]} \end{array}} \]

Precondition
\( x \) has \( n \) entries and \( y \) \( m \) entries, apart from \( x \)

◆ nonzeros()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
std::size_t ADS::BasicCsrMatrix< T, I, P >::nonzeros ( void ) const
inlinenodiscard

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

◆ operator==()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
bool ADS::BasicCsrMatrix< T, I, P >::operator== ( const BasicCsrMatrix< T, I, P > & ) const
default

true if the two matrices have the same sizes and the same entries kept

◆ persist()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
template<class A>
void ADS::BasicCsrMatrix< T, I, P >::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ persistTag()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
constexpr std::uint64_t ADS::BasicCsrMatrix< T, I, P >::persistTag ( void )
inlinestaticnodiscardconstexpr

the types of the values, of the columns and of the offsets, in a file

◆ rows()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
std::size_t ADS::BasicCsrMatrix< T, I, P >::rows ( void ) const
inlinenodiscard

the number \( m \) of rows

◆ rowStart()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
const P * ADS::BasicCsrMatrix< T, I, P >::rowStart ( void ) const
inlinenodiscard

the \( m + 1 \) offsets of the rows

◆ transpose()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
BasicCsrMatrix ADS::BasicCsrMatrix< T, I, P >::transpose ( void ) const
nodiscard

the transpose, \( n \times m \)

In pseudocode (beyond the book), by a counting sort on the columns, stable on the rows:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{transpose}() \cr 2 & \quad \mathit{trow} \gets \text{the prefix sums of the counts of } \text{the columns} \cr 3 & \quad \mathit{at} \gets \mathit{trow} \cr 4 & \quad \textbf{if}\ \text{the rows span columns far apart} \ \textbf{then} \cr 5 & \qquad \text{scatter the entries, in the order of the rows, into } \text{256 buckets of columns} \cr 6 & \quad \textbf{foreach}\ \text{entry } ( i , j , v ) \text{ by row, } \text{or by bucket}\ \textbf{do} \cr 7 & \qquad q \gets \mathit{at}[ j ];\ \mathit{at}[ j ] \gets q + 1; \ \mathit{tcol}[ q ] \gets i;\ \mathit{tval}[ q ] \gets v \cr 8 & \quad \textbf{return}\ ( \mathit{trow} , \mathit{tcol} , \mathit{tval} ) \end{array}} \]

Precondition
the rows fit I

◆ values()

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
const T * ADS::BasicCsrMatrix< T, I, P >::values ( void ) const
inlinenodiscard

the value of each nonzero, row by row

Member data documentation

◆ persistName

template<class T = double, std::integral I = std::uint32_t, std::integral P = detail::CsrOffset< I >>
std::string_view ADS::BasicCsrMatrix< T, I, P >::persistName = "CsrMatrix"
staticconstexpr

the name of the structure in a file [see Persistence.h]


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