API Reference¶
Data Structures¶
- class linearsystems.LinearSystemResult(x, residual, success=True, message='', n_iter=0)¶
Bases:
objectResult of a linear system solve.
- Parameters:
x (ndarray)
residual (float)
success (bool)
message (str)
n_iter (int)
- x¶
Solution vector of shape (n,).
- Type:
np.ndarray
- residual¶
Euclidean norm of the residual
‖Ax − b‖.- Type:
float
- success¶
Whether the solve succeeded.
- Type:
bool
- message¶
Descriptive status message.
- Type:
str
- n_iter¶
Number of iterations (iterative solvers only).
- Type:
int
Dense Direct Solvers¶
- linearsystems.lslrg(a, b)¶
Solve a general real linear system Ax = b.
Uses
numpy.linalg.solve()(LU decomposition with partial pivoting).- Parameters:
a (np.ndarray) – Real coefficient matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,) or matrix of shape (n, k).
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.lscrg(a, b)¶
Solve a general complex linear system Ax = b.
Uses
numpy.linalg.solve()on complex arrays.- Parameters:
a (np.ndarray) – Complex coefficient matrix of shape (n, n).
b (np.ndarray) – Complex right-hand side vector of shape (n,) or matrix of shape (n, k).
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.lslpb(a, b)¶
Solve a positive definite banded linear system Ax = b.
Accepts a as a full square matrix or as a banded storage array
(lower + upper + 1, n)compatible withscipy.linalg.solve_banded(). When a full matrix is provided it is converted to banded form with bandwidth inferred from the non-zero pattern.- Parameters:
a (np.ndarray) – Coefficient matrix of shape (n, n) (full) or banded storage of shape (lower + upper + 1, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If the system dimensions are inconsistent.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.lslsf(a, b)¶
Solve a symmetric positive definite linear system Ax = b.
Uses
scipy.linalg.solve()withassume_a='pos'to exploit the Cholesky factorization.- Parameters:
a (np.ndarray) – Symmetric positive definite matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,) or matrix of shape (n, k).
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular or not positive definite.
- linearsystems.lsltr(a, b, lower=True)¶
Solve a triangular linear system Ax = b.
Uses
scipy.linalg.solve_triangular().- Parameters:
a (np.ndarray) – Triangular matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,) or matrix of shape (n, k).
lower (bool) – If
True(default), treat a as lower triangular; otherwise upper triangular.
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.lsltc(a, b, lower=True)¶
Solve a complex triangular linear system Ax = b.
Uses
scipy.linalg.solve_triangular()on complex arrays.- Parameters:
a (np.ndarray) – Complex triangular matrix of shape (n, n).
b (np.ndarray) – Complex right-hand side vector of shape (n,) or matrix of shape (n, k).
lower (bool) – If
True(default), treat a as lower triangular; otherwise upper triangular.
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
Factorization-Based Solvers¶
- linearsystems.lufact(a)¶
Compute the LU factorization of a square matrix.
Wraps
scipy.linalg.lu_factor().- Parameters:
a (np.ndarray) – Real matrix of shape (n, n) to factor.
- Returns:
(lu, piv)compatible withlusolve()/scipy.linalg.lu_solve().
- Return type:
tuple
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.lusolve(lu_piv, b)¶
Solve Ax = b using a pre-computed LU factorization.
Wraps
scipy.linalg.lu_solve().- Parameters:
lu_piv (tuple) –
(lu, piv)tuple returned bylufact().b (np.ndarray) – Right-hand side vector of shape (n,) or matrix of shape (n, k).
- Returns:
Solution vector
xof shape (n,).- Return type:
np.ndarray
- linearsystems.cholfact(a)¶
Compute the Cholesky factorization of a symmetric positive definite matrix.
Wraps
scipy.linalg.cho_factor().- Parameters:
a (np.ndarray) – Symmetric positive definite matrix of shape (n, n).
- Returns:
(c, lower)compatible withcholsolve()/scipy.linalg.cho_solve().
- Return type:
tuple
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is not positive definite.
- linearsystems.cholsolve(c_low, b)¶
Solve Ax = b using a pre-computed Cholesky factorization.
Wraps
scipy.linalg.cho_solve().- Parameters:
c_low (tuple) –
(c, lower)tuple returned bycholfact().b (np.ndarray) – Right-hand side vector of shape (n,) or matrix of shape (n, k).
- Returns:
Solution vector
xof shape (n,).- Return type:
np.ndarray
- linearsystems.qrfact(a)¶
Compute the QR factorization of a matrix.
Wraps
scipy.linalg.qr()withmode='economic'.- Parameters:
a (np.ndarray) – Matrix of shape (m, n) to factor.
- Returns:
(Q, R)whereQis (m, k) orthogonal andRis (k, n) upper triangular,k = min(m, n).
- Return type:
tuple
- linearsystems.qrsolve(qr_result, b)¶
Solve (or least-squares fit) Ax = b using a pre-computed QR factorization.
- Parameters:
qr_result (tuple) –
(Q, R)tuple returned byqrfact().b (np.ndarray) – Right-hand side vector of shape (m,).
- Returns:
Solution (or least-squares solution) vector of shape (n,).
- Return type:
np.ndarray
Iterative Solvers¶
- linearsystems.lsitcg(a, b, tol=1e-10, maxiter=None)¶
Solve a symmetric positive definite system using Conjugate Gradient.
Wraps
scipy.sparse.linalg.cg().- Parameters:
a (np.ndarray) – Symmetric positive definite matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.maxiter (int | None) – Maximum number of iterations. Defaults to
None(scipy default).
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
- linearsystems.lsibcg(a, b, tol=1e-10, maxiter=None)¶
Solve a linear system using BiConjugate Gradient.
Wraps
scipy.sparse.linalg.bicg().- Parameters:
a (np.ndarray) – Square matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.maxiter (int | None) – Maximum number of iterations. Defaults to
None(scipy default).
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
- linearsystems.lsigmr(a, b, tol=1e-10, maxiter=None)¶
Solve a linear system using GMRES (Generalized Minimal Residual).
Wraps
scipy.sparse.linalg.gmres().- Parameters:
a (np.ndarray) – Square matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.maxiter (int | None) – Maximum number of restarts. Defaults to
None(scipy default).
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
- linearsystems.lsiccg(a, b, tol=1e-10, maxiter=None)¶
Solve a symmetric positive definite system using CG with IC preconditioner.
Computes an incomplete Cholesky factorisation via
scipy.sparse.linalg.spiluand uses it as a left preconditioner forscipy.sparse.linalg.cg().- Parameters:
a (np.ndarray) – Symmetric positive definite matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.maxiter (int | None) – Maximum number of iterations. Defaults to
None(scipy default).
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square.
- linearsystems.lsisor(a, b, omega=1.5, tol=1e-10, maxiter=1000)¶
Solve a linear system using Successive Over-Relaxation (SOR).
- Parameters:
a (np.ndarray) – Square matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
omega (float) – Relaxation factor (0 < omega < 2). Defaults to
1.5.tol (float) – Convergence tolerance on the residual norm. Defaults to
1e-10.maxiter (int) – Maximum number of iterations. Defaults to
1000.
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- Raises:
ValueError – If a is not square or
omegais out of range.
Least Squares¶
- linearsystems.lsqrr(a, b)¶
Solve the least-squares problem min‖Ax − b‖₂.
Wraps
numpy.linalg.lstsq().- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
b (np.ndarray) – Right-hand side vector of shape (m,) or matrix of shape (m, k).
- Returns:
(x, residuals, rank, sv)matching thenumpy.linalg.lstsq()return signature.
xis the least-squares solution of shape (n,),residualsare the squared residual norms,rankis the effective rank, andsvare the singular values.
- Return type:
tuple
- linearsystems.lsqnr(a, b)¶
Solve the non-negative least-squares problem min‖Ax − b‖₂ subject to x ≥ 0.
Wraps
scipy.optimize.nnls().- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
b (np.ndarray) – Right-hand side vector of shape (m,).
- Returns:
(x, residual_norm)wherexis the non-negative solutionof shape (n,) and
residual_normis‖Ax − b‖₂.
- Return type:
tuple
- linearsystems.lsqcr(a, b, constraints=None)¶
Solve a (possibly constrained) least-squares problem.
Without constraints delegates to
scipy.linalg.lstsq(). With equality constraintsC @ x = dthe problem is solved via the augmented normal equations.- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
b (np.ndarray) – Right-hand side vector of shape (m,).
constraints (dict | None) – Optional equality constraints. Expected keys are
"C"(np.ndarray, shape (p, n)) and"d"(np.ndarray, shape (p,)). Defaults toNone.
- Returns:
(x, residuals, rank, sv)matching thescipy.linalg.lstsq()return signature.
- Return type:
tuple
- linearsystems.lsbrr(a, b, lb=None, ub=None)¶
Solve a bounded least-squares problem using SLSQP.
- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
b (np.ndarray) – Right-hand side vector of shape (m,).
lb (np.ndarray | None) – Lower bounds of shape (n,). Defaults to
-inffor each component.ub (np.ndarray | None) – Upper bounds of shape (n,). Defaults to
+inffor each component.
- Returns:
(x, residual_norm)wherexis the boundedleast-squares solution of shape (n,) and
residual_normis‖Ax − b‖₂.
- Return type:
tuple
- linearsystems.ridge_regression(a, b, alpha=1.0)¶
Solve a Tikhonov-regularised (ridge) least-squares problem.
Minimises
‖Ax − b‖₂² + alpha * ‖x‖₂².- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
b (np.ndarray) – Right-hand side vector of shape (m,).
alpha (float) – Regularisation parameter (must be positive). Defaults to
1.0.
- Returns:
Ridge solution vector of shape (n,).
- Return type:
np.ndarray
- Raises:
ValueError – If
alphais not positive.
Matrix Inverse and Utility¶
- linearsystems.matrix_inverse(a)¶
Compute the inverse of a square matrix.
Wraps
numpy.linalg.inv().- Parameters:
a (np.ndarray) – Square real matrix of shape (n, n).
- Returns:
Inverse matrix of shape (n, n).
- Return type:
np.ndarray
- Raises:
ValueError – If a is not square.
numpy.linalg.LinAlgError – If a is singular.
- linearsystems.pseudo_inverse(a, rcond=None)¶
Compute the Moore-Penrose pseudoinverse of a matrix.
Wraps
numpy.linalg.pinv().- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
rcond (float | None) – Cutoff for small singular values. Values smaller than
rcond * largest_svare set to zero. Defaults toNone(numpy default).
- Returns:
Pseudoinverse of shape (n, m).
- Return type:
np.ndarray
- linearsystems.matrix_power(a, n)¶
Raise a square matrix to an integer power.
Wraps
numpy.linalg.matrix_power().- Parameters:
a (np.ndarray) – Square real matrix of shape (k, k).
n (int) – Integer exponent (may be negative).
- Returns:
Matrix
a**nof shape (k, k).- Return type:
np.ndarray
- Raises:
ValueError – If a is not square.
- linearsystems.solve_upper_triangular(a, b)¶
Solve an upper triangular system by back substitution.
- Parameters:
a (np.ndarray) – Upper triangular matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
Solution vector of shape (n,).
- Return type:
np.ndarray
- Raises:
ValueError – If a is not square.
- linearsystems.solve_lower_triangular(a, b)¶
Solve a lower triangular system by forward substitution.
- Parameters:
a (np.ndarray) – Lower triangular matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
Solution vector of shape (n,).
- Return type:
np.ndarray
- Raises:
ValueError – If a is not square.
Norms and Conditioning¶
- linearsystems.vector_norm(x, ord=2)¶
Compute the norm of a vector.
Wraps
numpy.linalg.norm().- Parameters:
x (np.ndarray) – Input vector of shape (n,).
ord (int | float) – Order of the norm (e.g., 1, 2,
np.inf). Defaults to2.
- Returns:
Norm value.
- Return type:
float
- linearsystems.matrix_norm(a, ord=None)¶
Compute the norm of a matrix.
Wraps
numpy.linalg.norm().- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
ord (int | float | str | None) – Order of the norm. Supported values:
None(Frobenius),'fro',1,-1,2,-2,np.inf,-np.inf. Defaults toNone.
- Returns:
Norm value.
- Return type:
float
- linearsystems.condition_number(a, ord=None)¶
Compute the condition number of a matrix.
Wraps
numpy.linalg.cond().- Parameters:
a (np.ndarray) – Square matrix of shape (n, n).
ord (int | float | str | None) – Norm order used to compute the condition number. Defaults to
None(2-norm).
- Returns:
Condition number.
- Return type:
float
- linearsystems.rcond_estimate(a)¶
Estimate the reciprocal of the condition number of a matrix.
Uses the ratio of the smallest to the largest singular value.
- Parameters:
a (np.ndarray) – Matrix of shape (m, n).
- Returns:
- Reciprocal condition number estimate in [0, 1]. A value
close to 0 indicates near-singularity.
- Return type:
float
Banded Matrix Utilities¶
- linearsystems.to_banded(a, lower, upper)¶
Convert a full matrix to banded (LAPACK) storage.
The output has shape
(lower + upper + 1, n)whereab[u + i - j, j]storesa[i, j].- Parameters:
a (np.ndarray) – Dense matrix of shape (n, n).
lower (int) – Number of sub-diagonals to store.
upper (int) – Number of super-diagonals to store.
- Returns:
- Banded storage array of shape
(lower + upper + 1, n).
- Return type:
np.ndarray
- Raises:
ValueError – If a is not square.
- linearsystems.from_banded(ab, lower, upper, n)¶
Convert banded (LAPACK) storage back to a full matrix.
- Parameters:
ab (np.ndarray) – Banded storage array of shape
(lower + upper + 1, n).lower (int) – Number of sub-diagonals stored.
upper (int) – Number of super-diagonals stored.
n (int) – Dimension of the output square matrix.
- Returns:
Dense matrix of shape (n, n).
- Return type:
np.ndarray
- linearsystems.lslbd(ab, b, lower, upper)¶
Solve a banded linear system from banded storage.
Wraps
scipy.linalg.solve_banded().- Parameters:
ab (np.ndarray) – Banded storage array of shape
(lower + upper + 1, n)as used by LAPACK.b (np.ndarray) – Right-hand side vector of shape (n,).
lower (int) – Number of sub-diagonals.
upper (int) – Number of super-diagonals.
- Returns:
- Solution with
x,residual,success, and
messagefields.
- Solution with
- Return type:
Sparse System Utilities¶
- linearsystems.sparse_solve(a_sparse, b)¶
Solve a sparse linear system Ax = b.
Wraps
scipy.sparse.linalg.spsolve().- Parameters:
a_sparse (scipy.sparse.spmatrix) – Sparse square matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
Solution vector of shape (n,).
- Return type:
np.ndarray
- linearsystems.sparse_cg(a_sparse, b, tol=1e-10)¶
Solve a sparse SPD system using Conjugate Gradient.
Wraps
scipy.sparse.linalg.cg().- Parameters:
a_sparse (scipy.sparse.spmatrix) – Sparse symmetric positive definite matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
- linearsystems.sparse_gmres(a_sparse, b, tol=1e-10)¶
Solve a sparse linear system using GMRES.
Wraps
scipy.sparse.linalg.gmres().- Parameters:
a_sparse (scipy.sparse.spmatrix) – Sparse square matrix of shape (n, n).
b (np.ndarray) – Right-hand side vector of shape (n,).
tol (float) – Convergence tolerance. Defaults to
1e-10.
- Returns:
- Solution with
x,residual,success, message, andn_iterfields.
- Solution with
- Return type:
Tridiagonal Solvers¶
- linearsystems.tridiag_solve(lower, main, upper, b)¶
Solve a tridiagonal linear system using the Thomas algorithm.
- Parameters:
lower (np.ndarray) – Sub-diagonal of length n-1.
main (np.ndarray) – Main diagonal of length n.
upper (np.ndarray) – Super-diagonal of length n-1.
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
Solution vector of shape (n,).
- Return type:
np.ndarray
- Raises:
ValueError – If the diagonal lengths are inconsistent.
- linearsystems.cyclic_tridiag_solve(lower, main, upper, b)¶
Solve a cyclic tridiagonal system using the Sherman-Morrison formula.
The cyclic system has extra corner entries:
A[0, n-1] = lower[-1]andA[n-1, 0] = upper[-1].- Parameters:
lower (np.ndarray) – Sub-diagonal entries including the wrap-around corner; length n.
main (np.ndarray) – Main diagonal of length n.
upper (np.ndarray) – Super-diagonal entries including the wrap-around corner; length n.
b (np.ndarray) – Right-hand side vector of shape (n,).
- Returns:
Solution vector of shape (n,).
- Return type:
np.ndarray
- Raises:
ValueError – If the diagonal lengths are inconsistent.
- linearsystems.block_tridiag_solve(blocks, b)¶
Solve a block tridiagonal system via block-LU elimination (numpy).
Each element of blocks is a triple
(L_i, D_i, U_i)whereD_iis the diagonal block andL_i,U_iare the off-diagonal blocks below and above the diagonal respectively. The first block has noL_0(passNone) and the last block has noU_last(passNone).- Parameters:
blocks (list[tuple[np.ndarray, np.ndarray, np.ndarray]]) – Block triplets
(L_i, D_i, U_i)of length N (number of blocks).L_0andU_{N-1}should beNone.b (np.ndarray) – Right-hand side vector of shape (total_rows,) where
total_rowsis the sum of all diagonal block row counts.
- Returns:
Solution vector of shape (total_rows,).
- Return type:
np.ndarray