Compressed sparse row format (source code)

= Compressed sparse row format

= CSR
{c}
{synonym}

<Compressed sparse row format> stores a sparse $m\times n$ <matrix> using arrays of nonzero values, their column indices, and an array of $m+1$ row offsets. For zero-based row $i$, its entries occupy positions from $\mathrm{rowptr}[i]$ through $\mathrm{rowptr}[i+1]-1$.

For $A=\left(\begin{smallmatrix}0&5&0\\2&0&7\end{smallmatrix}\right)$, one representation is $\mathrm{values}=(5,2,7)$, $\mathrm{col}=(1,0,2)$, $\mathrm{rowptr}=(0,1,3)$. Equal adjacent offsets encode an empty row. This permits <matrix-vector product> in $O(m+\mathrm{nnz})$ time without storing zeros. Whether columns are sorted or duplicates are permitted is an implementation convention.