Automatic Differentiation
 
Loading...
Searching...
No Matches
Compressed Sparse Row matrix format.

Detailed Description

A compressed Sparse Row (CSR) sparse matrix is defined by four component vectors labeled w, v, and u.

Return the multiplication of the sparse matrix (specified by by values and indexing) by the specified dense vector.

They are defined as:

With only m/n/w/v/u in hand, it is possible to check all dimensions are sane except the column dimension since it is implicit. The error-checking strategy is to check all dimensions except the column dimension before any work is done inside a function. The column index is checked as it is constructed and used for each entry. If the column index is not needed it is not checked. As a result indexing mistakes might produce non-sensical operations but out-of-bounds indexing will be caught.

Except for possible garbage values in w/v/u, memory usage is calculated from the number of non-zero entries (NNZE) and the number of rows (NR): 2*NNZE + 2*NR + 1.

The sparse matrix X of dimension m by n is represented by the vector w (of values), the integer array v (containing one-based column index of each value), the integer array u (containing one-based indexes of where each row starts in w).

Template Parameters
T1type of the sparse matrix
T2type of the dense vector
Parameters
mNumber of rows in matrix.
nNumber of columns in matrix.
wVector of non-zero values in matrix.
vColumn index of each non-zero value, same length as w.
uIndex of where each row starts in w, length equal to the number of rows plus one.
bEigen vector which the matrix is multiplied by.
Returns
Dense vector for the product.
Exceptions
std::domain_errorif m and n are not positive or are nan.
std::domain_errorif the implied sparse matrix and b are not multiplicable.
std::invalid_argumentif m/n/w/v/u are not internally consistent, as defined by the indexing scheme. Extractors are defined in Stan which guarantee a consistent set of m/n/w/v/u for a given sparse matrix.
std::out_of_rangeif any of the indexes are out of range.

Functions

template<typename T >
const std::tuple< Eigen::Matrix< T, Eigen::Dynamic, 1 >, std::vector< int >, std::vector< int > > stan::math::csr_extract (const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
 Extract the non-zero values, column indexes for non-zero values, and the NZE index for each entry from a sparse matrix.
 
template<typename T , require_eigen_dense_base_t< T > * = nullptr>
const std::tuple< Eigen::Matrix< scalar_type_t< T >, Eigen::Dynamic, 1 >, std::vector< int >, std::vector< int > > stan::math::csr_extract (const T &A)
 
template<typename T >
const std::vector< int > stan::math::csr_extract_u (const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
 Extract the NZE index for each entry from a sparse matrix.
 
template<typename T , require_eigen_dense_base_t< T > * = nullptr>
const std::vector< int > stan::math::csr_extract_u (const T &A)
 Extract the NZE index for each entry from a sparse matrix.
 
template<typename T >
const std::vector< int > stan::math::csr_extract_v (const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
 Extract the column indexes for non-zero value from a sparse matrix.
 
template<typename T , require_eigen_dense_base_t< T > * = nullptr>
const std::vector< int > stan::math::csr_extract_v (const T &A)
 Extract the column indexes for non-zero values from a dense matrix by converting to sparse and calling the sparse matrix extractor.
 
template<typename T >
const Eigen::Matrix< T, Eigen::Dynamic, 1 > stan::math::csr_extract_w (const Eigen::SparseMatrix< T, Eigen::RowMajor > &A)
 
template<typename T , require_eigen_dense_base_t< T > * = nullptr>
const Eigen::Matrix< scalar_type_t< T >, Eigen::Dynamic, 1 > stan::math::csr_extract_w (const T &A)
 
template<typename T >
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, Eigen::Dynamic > stan::math::csr_to_dense_matrix (int m, int n, const T &w, const std::vector< int > &v, const std::vector< int > &u)
 Construct a dense Eigen matrix from the CSR format components.
 
int stan::math::csr_u_to_z (const std::vector< int > &u, int i)
 Return the z vector computed from the specified u vector at the index for the z vector.