Getting started¶
Installation¶
Install from PyPI:
pip install flashquad
Or with uv:
uv add flashquad
GPU backends¶
FlashQuad works with any of these backends. Install the one you need:
Backend |
Install command |
GPU support |
|---|---|---|
NumPy |
included with flashquad |
CPU only |
PyTorch |
|
CUDA |
CuPy |
|
CUDA |
JAX |
|
CUDA |
Basic usage¶
Every workflow starts by creating a FlashQuad instance bound to a backend:
from flashquad import FlashQuad
fq = FlashQuad(backend="numpy")
Then call any integration method. They all share the same signature:
result = fq.trapz(
func=lambda x: x ** 2,
intervals=[[0, 1]],
num_points=[101],
)
Switching to GPU¶
Just change the backend string:
fq = FlashQuad(backend="torch") # auto-selects CUDA if available
fq = FlashQuad(backend="torch", device="cpu") # force CPU
Multidimensional integrals¶
Pass multiple intervals and grid sizes:
import numpy as np
fq = FlashQuad(backend="numpy")
result = fq.simpson(
func=lambda x, y: np.sin(x) * np.cos(y),
intervals=[[0, np.pi], [0, np.pi]],
num_points=[101, 101],
)
Batched parameter sweeps¶
Pass a params array to evaluate the same integral over many parameter sets at once:
import numpy as np
fq = FlashQuad(backend="numpy")
params = np.array([[1.0], [2.0], [3.0]]) # 3 parameter sets
result = fq.trapz(
func=lambda x, a: a * x ** 2,
intervals=[[0, 1]],
num_points=[101],
params=params,
)
# result.shape == (3,)
Boundary masking¶
Use the boundary argument to restrict integration to a sub-region:
import numpy as np
fq = FlashQuad(backend="numpy")
result = fq.simpson(
func=lambda x, y: 1.0,
intervals=[[-1, 1], [-1, 1]],
num_points=[101, 101],
boundary=lambda x, y: x ** 2 + y ** 2 <= 1, # unit disk
)
Available methods¶
Method |
Call |
Notes |
|---|---|---|
Trapezoidal |
|
Simple, general-purpose |
Simpson’s 1/3 |
|
Higher order; |
Boole’s |
|
Higher order; |
Gauss-Legendre |
|
High accuracy for smooth functions |
Monte Carlo |
|
|