Overview
Linear system solvers find solutions to matrix equations. These functions handle square systems, overdetermined systems (more equations than unknowns), and underdetermined systems (fewer equations than unknowns).solve
Solve linear system A × x = b. Finds vector x that satisfies the equation using LU decomposition with partial pivoting.Signature
Parameters
Coefficient matrix of shape (N, N). Must be square and non-singular.
Right-hand side of shape (N,) or (N, K) for multiple RHS.
Returns
Solution x of same shape as bAlgorithm
LU decomposition with partial pivoting (Golub & Van Loan, Algorithm 3.4.1):- Factor A into P × L × U
- Solve L × y = P × b by forward substitution
- Solve U × x = y by backward substitution
Requirements
- A must be square matrix
- A must be non-singular (invertible)
- Number of rows in A must equal length of b
Errors
- ShapeError: If A is not square or dimensions don’t match
- DTypeError: If input has string dtype
- DataValidationError: If A is singular
- DataValidationError: If input contains non-finite values (NaN, Infinity)
solveTriangular
Solve triangular system. More efficient thansolve() when A is already triangular (from QR or Cholesky decomposition).
Signature
Parameters
Triangular matrix of shape (N, N).
Right-hand side of shape (N,) or (N, K).
If true, A is lower triangular; if false, upper triangular.
Returns
Solution x of same shape as bAlgorithm
- Lower triangular: Forward substitution
- Upper triangular: Backward substitution
Requirements
- A must be square matrix
- A must be triangular (elements ignored based on lower parameter)
- Diagonal elements must be non-zero
Errors
- ShapeError: If A is not square or dimensions don’t match
- DTypeError: If input has string dtype
- DataValidationError: If A is singular (zero diagonal)
- DataValidationError: If input contains non-finite values (NaN, Infinity)
lstsq
Least squares solution to A × x = b. Finds x that minimizes ||A×x - b||² (Euclidean norm). Works for overdetermined (M > N), underdetermined (M < N), and square systems.Signature
Parameters
Coefficient matrix of shape (M, N). Can be any M × N matrix.
Target values of shape (M,) or (M, K).
Cutoff for small singular values. Default: machine epsilon × max(M, N).
Singular values smaller than rcond × largest_singular_value are treated as zero.
Returns
Object with four properties:Least squares solution of shape (N,) or (N, K)
Sum of squared residuals ||b - A×x||² for each column.
Scalar for 1D b, vector for 2D b.
Effective rank of matrix A (number of singular values > rcond threshold)
Singular values of A in descending order
Algorithm
SVD-based least squares (Golub & Van Loan, Algorithm 5.5.4):- Compute SVD: A = U × Σ × V^T
- Compute pseudo-inverse: A^+ = V × Σ^+ × U^T
- Σ^+ inverts non-zero singular values
- Solution: x = A^+ × b
- Overdetermined systems (M > N): Minimizes error
- Underdetermined systems (M < N): Returns minimum norm solution
- Rank-deficient systems: Automatically handles singular matrices
Use Cases
Overdetermined System (M > N)
More equations than unknowns. Finds best fit solution that minimizes error.Underdetermined System (M < N)
Fewer equations than unknowns. Finds minimum norm solution.Rank-Deficient System
Linearly dependent rows/columns.Requirements
- A can be any M × N matrix
- First dimension of A must match first dimension of b
Errors
- ShapeError: If A is not 2D matrix or dimensions don’t match
- DTypeError: If input has string dtype
- InvalidParameterError: If rcond is negative or non-finite
- DataValidationError: If input contains non-finite values (NaN, Infinity)
Comparison with solve()
| Feature | solve() | lstsq() |
|---|---|---|
| Input | Square N×N | Any M×N |
| Requirement | Non-singular | Any rank |
| Output | Exact solution | Best fit |
| Speed | Fast (LU) | Slower (SVD) |
| Use Case | Exact systems | Fitting, overdetermined |