On this page
Matrices for heat flow on a grid, a circuit or a network have a few non-zeros per row and zeros everywhere else. Here we store one by hand in the two common formats and see where the trick breaks down.
What is a sparse matrix?
A matrix is sparse when so many entries are zero that it pays to store only the non-zeros and their positions. Here is the 5×5 example we will use, rows and columns numbered from 0:
Row 0: 4, 0, 0, 2, 0
Row 1: 0, 3, 0, 0, 0
Row 2: 1, 0, 5, 0, 0
Row 3: 0, 0, 0, 2, 6
Row 4: 0, 7, 0, 0, 1
It has 25 entries but only 9 non-zeros.
How do COO and CSR store a matrix?
The coordinate format (COO), which SciPy's documentation also calls the ijv or triplet format, keeps three lists of equal length: row, column and value. Row by row:
row = [0, 0, 1, 2, 2, 3, 3, 4, 4]
col = [0, 3, 1, 0, 2, 3, 4, 1, 4]
val = [4, 2, 3, 1, 5, 2, 6, 7, 1]
Compressed sparse row (CSR) squeezes the repeated row list into pointers. In SciPy's definition of CSR, the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their values in data[indptr[i]:indptr[i+1]]. For our matrix:
data = [4, 2, 3, 1, 5, 2, 6, 7, 1]
indices = [0, 3, 1, 0, 2, 3, 4, 1, 4]
indptr = [0, 2, 3, 5, 7, 9]
Row 2 runs from position 3 up to (not including) 5: values 1 and 5, in columns 0 and 2.
COO stores 27 numbers and CSR 9 + 9 + 6 = 24, against 25 for the full matrix: no saving yet. The saving comes with size, because a full matrix grows like n² while CSR grows with the non-zeros plus n + 1 pointers. SciPy lists COO as fast for building a matrix and CSR as fast for matrix–vector products.
How does CSR matrix–vector multiplication work?
To compute y = Ax, walk each row's slice once:
For row i, set y[i] = 0.
For each position k from indptr[i] to indptr[i+1] − 1, add data[k] × x[indices[k]] to y[i].
Move to the next row.
Take x = (1, 2, 3, 4, 5). Row 0 holds positions 0 and 1, so y[0] = 4×1 + 2×4 = 12. Then y[1] = 3×2 = 6, y[2] = 1×1 + 5×3 = 16, y[3] = 2×4 + 6×5 = 38 and y[4] = 7×2 + 1×5 = 19. That is 9 multiplications, one per non-zero, instead of 25.
What is fill-in during elimination?
Solving Ax = b by Gaussian elimination (or the LU decomposition it builds) can turn zeros into non-zeros. These new entries are called fill-in.
Take a 5×5 arrow matrix: 5 in the top-left corner, 2 on the rest of the diagonal, and 1s along the first row and column (13 non-zeros). Eliminating the first variable subtracts one fifth of row 0 from every other row, so all 12 off-diagonal entries of the lower 4×4 block become −0.2: that block is now full. Put the busy variable last instead and elimination creates no fill-in at all.
Ordering matters even more on grids. Alan George's 1973 paper on nested dissection of a regular finite element mesh showed that on an n × n mesh, row-by-row numbering needs O(n⁴) operations and O(n³) storage to factorise, while his nested-dissection numbering needs O(n³) operations and O(n² log₂ n) storage.
Why does a 10⁶ × 10⁶ grid matrix fit in memory?
Put one unknown at each point of a 1000 × 1000 grid and link each to its four neighbours. The 10⁶ × 10⁶ matrix has 10⁶ diagonal entries plus two entries for each of the 2 × 1000 × 999 neighbour links: 4,996,000 non-zeros.
In NumPy, a float64 value takes 64 bits (8 bytes) and an int32 index 32 bits (4 bytes), so:
Full matrix: 10¹² entries × 8 bytes = 8 TB, far beyond any laptop.
CSR data: 4,996,000 × 8 bytes ≈ 40 MB.
CSR indices: 4,996,000 × 4 bytes ≈ 20 MB.
CSR indptr: 1,000,001 × 4 bytes ≈ 4 MB.
That is about 64 MB in all, roughly 125,000 times less, and one matrix–vector product costs about five million multiply-adds. Iterative solvers that only multiply by A rely on this; how fast they converge depends on the condition number of the matrix.
If row reduction still feels shaky, the Fermi Concept Library has its own lesson for every concept in mathematics and computing, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning.
Next, write the CSR arrays of a 6×6 tridiagonal matrix by hand.



