This is an old version, view current version.

## 7.2 Conversion functions

Conversion functions between dense and sparse matrices are provided.

### 7.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

### 7.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