Learn R Programming

rstan (version 2.9.0-3)

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

Value

  • A named list with components
    1. wA numeric vector containing the non-zero elements ofA
    2. vAn integer vector containing the column indices of the non-zero elements ofA
    3. uAn integer vector indicating where inwa given row's non-zero values start

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 utilize this 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 to the data block of the Stan program.

Examples

Run this code
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))

Run the code above in your browser using DataLab