PyAMG: Algebraic Multigrid Solvers in Python

5.3.0 · active · verified Thu Apr 16

PyAMG is a Python library providing implementations of Algebraic Multigrid (AMG) solvers and supporting tools for approximating solutions to large, sparse linear systems of algebraic equations (Ax=b). It is actively maintained, with the current version being 5.3.0, and receives regular updates to support newer Python and SciPy versions and introduce new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a 2D Poisson problem using PyAMG's gallery, construct a `smoothed_aggregation_solver`, and solve the resulting sparse linear system. It highlights the typical workflow for using PyAMG as a standalone solver.

import numpy as np
from scipy.sparse import csr_matrix
from pyamg import gallery
from pyamg import smoothed_aggregation_solver

# 1. Create a sample sparse matrix (e.g., 2D Poisson problem)
A = gallery.poisson((100, 100), format='csr')

# 2. Create a right-hand side vector
b = np.random.rand(A.shape[0])

# 3. Construct the Algebraic Multigrid solver
# max_coarse controls the size of the coarsest grid
ml = smoothed_aggregation_solver(A, max_coarse=10)

# 4. Solve the linear system Ax = b
x = ml.solve(b, tol=1e-10)

print(f"Matrix shape: {A.shape}")
print(f"Number of nonzeros: {A.nnz}")
print(f"Solution vector norm: {np.linalg.norm(x)}")
print(f"Residual norm: {np.linalg.norm(b - A @ x)}")

view raw JSON →