Sparse Matrices & CSR Representation
In computational mathematics, a sparse matrix is a matrix in which most of the elements are zero. Storing all these zeros is highly inefficient in terms of memory and compute. Therefore, we utilize specialized data structures to store only the non-zero elements.
One of the most widely used formats is the Compressed Sparse Row (CSR) format.
To see basic matrix operations and step-by-step products in action, you can load the 🔬Matrix Multiplier Visualizer directly in the browser sandbox.
The Core Concept
A sparse matrix with non-zero elements is represented in CSR format using three one-dimensional arrays:
val: A floating-point array of size containing the values of the non-zero elements.col_ind: An integer array of size containing the column indices of the elements inval.row_ptr: An integer array of size containing the index invalandcol_indwhere each row begins. The last elementrow_ptr[m]is equal to .
Mathematical Definition of Arrays
Given a matrix , the array values are defined as follows:
Where is the index of the non-zero element in row-major order, and:
Practical Example
Consider the following sparse matrix:
Let's identify the non-zero values in row-major order:
- Row 0: (at col 0), (at col 3)
- Row 1: (at col 1)
- Row 2: (at col 2), (at col 3)
The total number of non-zero elements is .
The CSR arrays are computed as:
val=[10, 20, 30, 50, 60]col_ind=[0, 3, 1, 2, 3]row_ptr=[0, 2, 3, 5]
Sparse Matrix-Vector Multiplication (SpMV)
Evaluating is a central routine in solver pipelines. The CSR representation allows us to compute this product very efficiently using the following algorithm:
def spmv_csr(val, col_ind, row_ptr, x, num_rows):
# Initialize output vector y with zeros
y = [0.0] * num_rows
# Iterate through each row
for i in range(num_rows):
row_start = row_ptr[i]
row_end = row_ptr[i + 1]
row_sum = 0.0
# Accumulate dot product for the row
for k in range(row_start, row_end):
col = col_ind[k]
row_sum += val[k] * x[col]
y[i] = row_sum
return y
Computational Complexity
Compared to dense matrix-vector multiplication which takes operations, SpMV using CSR takes:
Since , this represents an enormous reduction in execution time and memory footprint for large systems.
🔬 Interactive Laboratory Sandbox
Run practical simulations and numerical verifications associated with the mathematical equations derived in this note: