Understanding Sparse Matrix Formats: COO, CSR, and CSC

1. Introduction: The Problem of “Zero”

In many fields of computational science—Deep Learning, Physics Simulations, and Graph Theory—matrices are often massive but mostly empty. We call a matrix dense if most elements are non-zero, and sparse if most elements are zero.

Consider a matrix representing a social network of 1 million users. A dense representation would store entries (mostly zeros, since no one knows everyone). Storing these zeros is a waste of memory and computation.

Sparse formats solve this by storing only the non-zero values ().

The Example Matrix

Throughout this report, we will use the following matrix to demonstrate each format. Note that it has 5 non-zero values ().


2. Coordinate List (COO)

The Intuitive Approach

COO is the simplest format. It represents the matrix as a list of tuples . It is often called the “triplet” format.

Data Structure

It requires three arrays of length :

  1. row: The row index of the element.
  2. col: The column index of the element.
  3. data: The actual value of the element.

Representation of

IndexrowcoldataInterpretation
00010
11120
21230
32040
42250

Pros and Cons

  • Pros: Flexible. The order does not matter (you can append new values to the end easily). Excellent for initially constructing a matrix.
  • Cons: Inefficient for arithmetic. To perform , the computer must jump randomly around memory to find which row it is currently processing.
  • Memory Cost: integers/floats.

3. Compressed Sparse Row (CSR)

The Standard for Arithmetic

CSR is the industry standard for deep learning (e.g., PyTorch, CUDA) because it optimizes for row-based operations, such as Matrix-Vector multiplication. It compresses the row array from COO.

Data Structure

  1. data: The non-zero values (size ). Must be sorted by row.
  2. indices: The column indices for the values (size ).
  3. indptr (Index Pointer): An array of size . This is the “bookmark” array.

Understanding indptr (Row Offsets)

The indptr array defines the range of indices in the data array that belong to a specific row.

Representation of

  • data:
  • indices: (The columns associated with the data above)
  • indptr:

Decoding indptr:

  • Row 0: Look at indptr[0] to indptr[1] range .
    • Take index 0 from data: 10.
  • Row 1: Look at indptr[1] to indptr[2] range .
    • Take indices 1 and 2 from data: 20, 30.
  • Row 2: Look at indptr[2] to indptr[3] range .
    • Take indices 3 and 4 from data: 40, 50.

Pros and Cons

  • Pros: Fast arithmetic. The CPU/GPU can stream through data linearly to calculate row dot-products. Memory efficient.
  • Cons: Rigid. Adding a new non-zero value requires shifting all subsequent data in the arrays.

4. Compressed Sparse Column (CSC)

The Column-Major Alternative

CSC is identical to CSR, but transposed. It compresses columns instead of rows. It is the default in systems like MATLAB.

Data Structure

  1. data: The non-zero values (size ). Must be sorted by column.
  2. indices: The row indices for the values (size ).
  3. indptr: An array of size . Defines column boundaries.

Representation of

Note: We must re-order the data to traverse down columns (Column 0, then Column 1, etc).

  • data:
    • (Col 0 has 10, 40. Col 1 has 20. Col 2 has 30, 50)
  • indices:
    • (Row indices for the data above)
  • indptr:

Pros and Cons

  • Pros: Extremely fast for column slicing (e.g., “Get all data for Feature X”) or matrix addition.
  • Cons: Slow for row-based operations.

5. Comparative Analysis

Memory Efficiency (The Math)

Let be the number of rows/cols (assuming square) and be the number of non-zeros.

Let be the size of a value (e.g., 64-bit float) and be the size of an index (e.g., 32-bit int).

  1. COO Memory:

    • Explanation: We store (row, col) for every value.
  2. CSR/CSC Memory:

    • Explanation: We store only one index (col or row) per value, plus the small indptr array.

Conclusion: CSR/CSC is more memory efficient than COO as long as the matrix is sufficiently sparse. Specifically, CSR wins when:

Since most Deep Learning matrices have millions of non-zeros and only thousands of rows, CSR saves roughly 33% of the index memory overhead.

Summary Table

FeatureCOOCSRCSC
CompressionNoneRow CompressedColumn Compressed
Data OrderArbitraryRow-MajorCol-Major
Insertion (Fast) (Slow) (Slow)
AccessSlowFast (Rows)Fast (Cols)
Best ForConstructionTraining/MathSlicing