Skip to Content
ModulesSciPy TutorialSparse Matrices (sparse)

Sparse Matrices & Large Systems (scipy.sparse)

In 1D, 2D, and 3D finite-difference or finite-element simulations with NN unknowns, each node interacts only with a few neighbors. Storing a dense 105×10510^5 \times 10^5 matrix requires 80 GB80\text{ GB} of RAM, whereas a sparse matrix uses only a few megabytes.


1. Sparse Matrix Formats in SciPy

FormatNameBest For
coo_matrix / coo_arrayCoordinateBuilding matrices from (row, col, data) triplets
csr_matrix / csr_arrayCompressed Sparse RowFast row slicing, matrix-vector products A @ x, arithmetic
csc_matrix / csc_arrayCompressed Sparse ColumnFast column slicing, sparse linear solves (spsolve)
dia_matrix / dia_arrayDiagonalBanded finite-difference stencils
💡

Always build matrices in coo_matrix or diags(), and convert to .tocsr() or .tocsc() before performing matrix multiplications or calling solvers.

Constructing and Inspecting Sparse Matrices


Example

Output:



2. Solving Large Sparse Systems (scipy.sparse.linalg)

SciPy provides both direct and iterative solvers for sparse systems A x = b:

Exact factorization via SuperLU. Highly efficient for 1D and moderate 2D grids:

from scipy.sparse.linalg import spsolve x = spsolve(A.tocsc(), b)

Solving a 1D Poisson Boundary Value System

Let’s discretize −u′′(x)=1-u''(x) = 1 with boundary conditions u(0)=u(1)=0u(0) = u(1) = 0 using central differences:


Example

Output:



3. Sparse Eigenvalue Problems (eigsh)

When finding the lowest vibrational modes or buckling loads of huge systems, you rarely need all 10510^5 eigenvalues. scipy.sparse.linalg.eigsh (Lanczos algorithm) extracts the few eigenvalues you specify:


Example

Output:



4. Try It Yourself

Problem: Build and Solve a Sparse Diagonal System

Create a tridiagonal matrix of size N=5N=5 with 4 on the main diagonal and -1 on both off-diagonals. Solve Ax=1A x = \mathbf{1} using sparse.linalg.spsolve.

Show solution

import numpy as np from scipy import sparse from scipy.sparse.linalg import spsolve N = 5 diags = [-np.ones(N-1), 4.0 * np.ones(N), -np.ones(N-1)] A = sparse.diags(diags, [-1, 0, 1], format="csc") b = np.ones(N) x = spsolve(A, b) print(f"x: {np.round(x, 4)}")

PygroundTry It Out

Construct a 5x5 tridiagonal sparse matrix and solve A x = b.

Output:

Last updated on