This is an old version, view current version.

8.2 Conversion functions

Conversion functions between dense and sparse matrices are provided.

8.2.1 Dense to sparse conversion

Converting a dense matrix \(m\) to a sparse representation produces a vector \(w\) and two integer arrays, \(u\) and \(v\).

vector csr_extract_w(matrix a)
Return non-zero values in matrix a; see section compressed row storage.
Available since 2.8

array[] int csr_extract_v(matrix a)
Return column indices for values in csr_extract_w(a); see compressed row storage.
Available since 2.8

array[] int csr_extract_u(matrix a)
Return array of row starting indices for entries in csr_extract_w(a) followed by the size of csr_extract_w(a) plus one; see section compressed row storage.
Available since 2.8

tuple(vector, array[] int, array[] int) csr_extract(matrix a)
Return all three components of the CSR representation of the matrix a; see section compressed row storage. This function is equivalent to (csr_extract_w(a), csr_extract_v(a), csr_extract_u(a)).
Available since 2.33

8.2.2 Sparse to dense conversion

To convert a sparse matrix representation to a dense matrix, there is a single function.

matrix csr_to_dense_matrix(int m, int n, vector w, array[] int v, array[] int u)
Return dense \(\text{m} \times \text{n}\) matrix with non-zero matrix entries w, column indices v, and row starting indices u; the vector w and array v must be the same size (corresponding to the total number of nonzero entries in the matrix), array v must have index values bounded by m, array u must have length equal to m + 1 and contain index values bounded by the number of nonzeros (except for the last entry, which must be equal to the number of nonzeros plus one). See section compressed row storage for more details.
Available since 2.10