Skip to contents

This helper function allows the extraction of a sparse matrix from a matrix using rstan::extract_sparse_parsts() and returns these elements in a named list for use in stan.

Usage

extract_sparse_matrix(mat, prefix = "")

Arguments

mat

A matrix to extract the sparse matrix from.

prefix

A character string to prefix the names of the returned list.

Value

Return a list that describes the sparse matrix this includes:

  • nw the number of non-zero elements in the matrix.

  • w the non-zero elements of the matrix.

  • nv the number of non-zero row identifiers in the matrix.

  • v the non-zero row identifiers of the matrix.

  • nu the number of non-zero column identifiers in the matrix.

  • u the non-zero column identifiers of the matrix.

See also

Examples

mat <- matrix(1:9, nrow = 3)
extract_sparse_matrix(mat)
#> $nw
#> [1] 9
#> 
#> $w
#> [1] 1 4 7 2 5 8 3 6 9
#> 
#> $nv
#> [1] 9
#> 
#> $v
#> [1] 1 2 3 1 2 3 1 2 3
#> 
#> $nu
#> [1] 4
#> 
#> $u
#> [1]  1  4  7 10
#>