Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

extract_sparse_parts

Extract the compressed representation of a sparse matrix


Description

Create a list of vectors that represents a sparse matrix.

Usage

extract_sparse_parts(A)

Arguments

A

A matrix or Matrix.

Details

The Stan Math Library has a function called csr_matrix_times_vector, which inputs a matrix in compressed row storage form and a dense vector and returns their product without fillin. To use the csr_matrix_times_vector function with a large sparse matrix, it is optimal in terms of memory to simply pass the three vectors that characterize the compressed row storage form of the matrix to the data block of the Stan program. The extract_sparse_parts function provides a convenient means of obtaining these vectors.

Value

A named list with components

  1. w A numeric vector containing the non-zero elements of A.

  2. v An integer vector containing the column indices of the non-zero elements of A.

  3. u An integer vector indicating where in w a given row's non-zero values start.

Examples

A <- rbind(
    c(19L, 27L,  0L,  0L),
    c( 0L,  0L,  0L,  0L),
    c( 0L,  0L,  0L, 52L),
    c(81L,  0L, 95L, 33L)
  )
  str(extract_sparse_parts(A))

rstan

R Interface to Stan

v2.21.2
GPL (>= 3)
Authors
Jiqiang Guo [aut], Jonah Gabry [aut], Ben Goodrich [cre, aut], Sebastian Weber [aut], Daniel Lee [ctb], Krzysztof Sakrejda [ctb], Modrak Martin [ctb], Trustees of Columbia University [cph], Oleg Sklyar [cph] (R/cxxfunplus.R), The R Core Team [cph] (R/pairs.R, R/dynGet.R), Jens Oehlschlaegel-Akiyoshi [cph] (R/pairs.R), John Maddock [cph] (gamma.hpp), Paul Bristow [cph] (gamma.hpp), Nikhar Agrawal [cph] (gamma.hpp), Christopher Kormanyos [cph] (gamma.hpp), Bronder Steve [ctb]
Initial release
2020-07-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.