Which solver should I use?
| Your matrix | Use | Why |
|---|---|---|
| Dense, square | linalg.solve(A, b) | LU with partial pivoting (LAPACK dgesv). |
| Dense, tall (least squares) | decomp.QR(A).solve(b) or decomp.SVD(A).solve(b) | QR for full column rank; SVD for rank-deficient problems or the minimum-norm solution. |
| Sparse, symmetric positive definite | sparse.cholesky(A) | Simplicial LDLᵀ with AMD ordering. Reusable, with refactorize, slogdet and shift. |
| Sparse SPD with lots of fill (3D meshes) | sparse.cholesky(A, mode="multifrontal") | Dense frontal matrices and level-3 BLAS: 3–9× faster than simplicial on 3D meshes. |
| Sparse, symmetric indefinite (KKT, saddle point) | sparse.cholesky(A, mode="multifrontal") | Pivoted LDLᵀ with 1×1 and 2×2 pivots; also reports the inertia. |
| Sparse, nonsymmetric | sparse.factorize(A) | Eigen SparseLU (supernodal LU with partial pivoting). |
| Sparse SPD, too large to factor | sparse.solve(A, b, method="cg") | Conjugate gradient, optionally with a Jacobi preconditioner. |
| Sparse nonsymmetric, too large to factor | sparse.solve(A, b, method="bicgstab") | BiCGSTAB with Eigen's default diagonal preconditioner. |
Dense linear algebra: peigen.linalg
The functions follow NumPy's conventions: a 1D right-hand side returns a 1D solution, and shapes match numpy.linalg. Where both are available, method="auto" picks LAPACK; the release wheels always link it.
| Function | Computes | Backends (method=) | Notes |
|---|---|---|---|
matmul(a, b) | a @ b | Eigen GEMM, which calls BLAS dgemm when linked | Returns a new C-ordered array. |
solve(a, b) | x with a x = b | lapack: dgesv · eigen: PartialPivLU | Raises ValueError for singular or numerically singular a. |
qr(a, mode="reduced") | Q, R with a = Q R | Eigen HouseholderQR | mode="complete" returns the full m×m Q. |
svd(a, full_matrices=False) | U, s, Vt | lapack: dgesdd · bdcsvd: Eigen BDCSVD | Singular values in descending order, as in NumPy. |
eigh(a, lower=True) | w, V for symmetric a | lapack: dsyevd · eigen: SelfAdjointEigenSolver | Only the chosen triangle is read. Eigenvalues ascending. |
eighvals(a, lower=True) | w | as eigh | Skips the eigenvectors. |
norm(a, ord=None, axis=None) | Frobenius norm (default) | Eigen reduction | Other ord/axis combinations defer to numpy.linalg.norm. |
Sparse Cholesky: sparse.cholesky
sparse.cholesky(a, mode="ldlt", ordering="amd", lower=True, shift=0.0) factors P (A + shift·I) Pᵀ once and returns a reusable SparseCholesky. Only one triangle of A is read, so you can pass just that triangle. It needs no SuiteSparse.
| Mode | Factorization | Eigen class | Use it for |
|---|---|---|---|
"ldlt" (default) | L D Lᵀ, no square roots | SimplicialLDLT | SPD matrices with low fill: 2D meshes, graph Laplacians, banded systems. Solves with many right-hand sides use a blocked kernel. |
"llt" | L Lᵀ | SimplicialLLT | Classic Cholesky. Raises if the matrix isn't positive definite, so it doubles as a definiteness test. |
"multifrontal" experimental | Pivoted L D Lᵀ, with D made of 1×1 and 2×2 blocks | MultifrontalLDLT | High-fill problems (3D meshes) and symmetric indefinite matrices. Solves with several right-hand sides use a supernodal blocked kernel. |
Orderings
ordering="amd" (approximate minimum degree) reduces fill-in and is the right choice almost always. ordering="natural" keeps the input order, which is only useful for banded matrices or debugging.
What a factorization can do
| Member | Does |
|---|---|
solve(b) | Solves for a 1D or 2D b. Releases the GIL, so threads can share one factorization. |
refactorize(a, shift=None) | New values with the same sparsity pattern: reruns only the numeric phase. Raises ValueError if the pattern differs. |
slogdet() / logdet() | (sign, log|det|) as in numpy.linalg.slogdet, without overflow. logdet() requires a positive determinant. |
inertia | (positive, negative, zero) eigenvalue counts, read off the pivots of D (Sylvester's law of inertia). |
shift, mode, ordering, shape, factor_nnz | Settings, and the number of stored nonzeros in L (a measure of fill-in). |
Indefinite matrices: use mode="multifrontal". "ldlt" doesn't pivot for stability, so it can be inaccurate or fail on them. Multifrontal's threshold pivoting doesn't rescale the matrix, so equilibrate badly scaled input first.
One-shot use: sparse.solve(A, b, method="cholesky") is the same as sparse.cholesky(A).solve(b).
Sparse LU: sparse.factorize
sparse.factorize(A) wraps Eigen's SparseLU: a supernodal LU with partial pivoting and a COLAMD column ordering, for general square matrices. It returns a SparseFactorized object whose solve(b) accepts 1D or 2D right-hand sides. sparse.solve(A, b) (method="auto" or "lu") does the same in one call.
Iterative solvers: sparse.solve
method= | Matrix | Eigen class | Preconditioners |
|---|---|---|---|
"cg" | Symmetric positive definite | ConjugateGradient | "none", "jacobi" (diagonal), "ilu" (IncompleteLUT) |
"bicgstab" | General square | BiCGSTAB | Diagonal (Eigen's default) |
tol is the relative residual ‖Ax − b‖ / ‖b‖ (default 1e-8), and maxiter defaults to 2n. The solvers raise RuntimeError if they don't converge. sparse.solve_stats(A, b, method="cg", ...) returns the iteration count and estimated error, for comparing preconditioners.
ILU with CG: IncompleteLUT is not a symmetric preconditioner, and CG assumes one. With the default ilu_fill_factor=10 it can fail to converge even on a 2D Laplacian. Raise ilu_fill_factor (for example to 40), use "jacobi", or factor the matrix with sparse.cholesky instead.
Sparse products and helpers
| Function | Computes | Returns |
|---|---|---|
spmm(a, b) | sparse @ dense | NumPy array |
spspmm(a, b) | sparse @ sparse | scipy.sparse CSC matrix, pruned |
to_dense(a) | sparse to dense | NumPy array |
from_coo(data, row, col, shape) | build from triplets (duplicates summed) | scipy.sparse CSC matrix |
For plain products, SciPy's own @ is currently faster; see the benchmarks. These functions are mainly here for API completeness.
Reusable decompositions: peigen.decomp
| Class | Holds | solve |
|---|---|---|
SVD(a, full_matrices=False) | U, s, Vt | Minimum-norm least squares; singular values <= rcond · max(s) count as zero (rcond is relative, as in numpy.linalg.lstsq). |
QR(a, mode="reduced") | Q, R | Square or tall a with full column rank: exact or least-squares solution, from either mode. |
SparseFactorized(a) | Sparse LU | Same as sparse.factorize. |
SparseCholesky(a, ...) | Sparse Cholesky / LDLᵀ | Same as sparse.cholesky. |
Conventions
- Real double precision. Everything runs in
float64. Integer andfloat32inputs are converted; complex inputs raiseTypeErrorrather than silently dropping the imaginary part. - Any SciPy sparse format.
*_matrixor*_array,int32orint64indices. Unsorted or duplicate entries are canonicalized on a copy; your matrix is never modified. - Size limit. Sparse dimensions and nonzero counts must fit in 32-bit indices; larger matrices raise
ValueError. - Errors.
ValueErrorfor bad shapes or arguments,numpy.linalg.LinAlgErrorwhen a Cholesky factorization breaks down,RuntimeErrorwhen an iterative solver doesn't converge or a sparse LU fails. - Licensing. pEigen is MIT. It compiles Eigen with
EIGEN_MPL2_ONLY, so the wheels contain only MPL-2.0 Eigen code.