Linear Algebra
Operations from mechestim.linalg. Cost formulas vary per operation — see each function's docstring.
mechestim.linalg._svd
Truncated SVD with FLOP counting.
svd(a, k=None)
Truncated singular value decomposition.
Computes the top-k singular values and corresponding vectors
of a 2-D matrix, wrapping numpy.linalg.svd with FLOP counting.
FLOP Cost
m × n × k FLOPs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
Input matrix of shape |
required |
k
|
int
|
Number of singular values/vectors to return. Must satisfy
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
U |
ndarray
|
Left singular vectors, shape |
S |
ndarray
|
Singular values in descending order, shape |
Vt |
ndarray
|
Right singular vectors (transposed), shape |
Raises:
| Type | Description |
|---|---|
BudgetExhaustedError
|
If the operation would exceed the FLOP budget. |
ValueError
|
If a is not 2-D or k is out of range. |
Notes
mechestim cost: m × n × k FLOPs.
See Also
numpy.linalg.svd : Full NumPy SVD documentation.
Source code in src/mechestim/linalg/_svd.py
mechestim.linalg._decompositions
Matrix decomposition wrappers with FLOP counting.
Each operation has a co-located cost function documenting the formula, source, and assumptions. Cost functions are pure (shape params) -> int.
cholesky(a)
Cholesky decomposition with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
cholesky_cost(n)
FLOP cost of Cholesky decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(n^3 / 3\). |
Notes
Source: Golub & Van Loan, Matrix Computations, 4th ed., §4.2. Assumes standard column-outer-product Cholesky algorithm.
Source code in src/mechestim/linalg/_decompositions.py
eig(a)
Eigendecomposition with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
eig_cost(n)
FLOP cost of eigendecomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(10n^3\). |
Notes
Source: Golub & Van Loan, Matrix Computations, 4th ed., §7.5. Assumes Francis double-shift QR algorithm. The constant ~10 accounts for Hessenberg reduction (~\(10n^3/3\)) plus ~2 QR iterations per eigenvalue. This is an accepted asymptotic estimate; actual count is data-dependent.
Source code in src/mechestim/linalg/_decompositions.py
eigh(a, UPLO='L')
Symmetric eigendecomposition with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
eigh_cost(n)
FLOP cost of symmetric eigendecomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \((4/3)n^3\). |
Notes
Source: Golub & Van Loan, Matrix Computations, 4th ed., §8.3. Assumes tridiagonalization via Householder followed by implicit QR sweeps.
Source code in src/mechestim/linalg/_decompositions.py
eigvals(a)
Eigenvalues (nonsymmetric) with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
eigvals_cost(n)
FLOP cost of computing eigenvalues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(7n^3\). |
Notes
Same algorithm as eig (Francis QR), but eigenvectors are not
accumulated. Without back-accumulation of eigenvectors the cost
is dominated by Hessenberg reduction (~(10/3)n^3) plus QR iterations
(~4n^3), totalling ~7n^3.
Source code in src/mechestim/linalg/_decompositions.py
eigvalsh(a, UPLO='L')
Eigenvalues (symmetric) with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
eigvalsh_cost(n)
FLOP cost of computing eigenvalues of a symmetric matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \((4/3)n^3\). |
Notes
Same algorithm as eigh, but eigenvectors are not accumulated.
Source code in src/mechestim/linalg/_decompositions.py
qr(a, mode='reduced')
QR decomposition with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
qr_cost(m, n)
FLOP cost of QR decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(2mn^2 - (2/3)n^3\) (for \(m \geq n\)). |
Notes
Source: Golub & Van Loan, Matrix Computations, 4th ed., §5.2. Assumes Householder QR. For m < n, roles are swapped.
Source code in src/mechestim/linalg/_decompositions.py
svdvals(a)
Singular values with FLOP counting.
Source code in src/mechestim/linalg/_decompositions.py
svdvals_cost(m, n)
FLOP cost of computing singular values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: m * n * min(m, n). |
Notes
Source: Golub-Reinsch bidiagonalization. Same cost model as full SVD.
Source code in src/mechestim/linalg/_decompositions.py
mechestim.linalg._solvers
Linear solver wrappers with FLOP counting.
inv(a)
Matrix inverse with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
inv_cost(n, symmetric=False)
FLOP cost of matrix inverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
symmetric
|
bool
|
If True, assume symmetric input. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Uses \(n^3/3 + n^3\) for symmetric input (Cholesky factorization + n triangular solves against identity), or \(n^3\) for general input (LU-based).
Source code in src/mechestim/linalg/_solvers.py
lstsq(a, b, rcond=None)
Least-squares solution with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
lstsq_cost(m, n)
FLOP cost of least-squares solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: m * n * min(m, n). |
Notes
NumPy uses LAPACK gelsd (SVD-based) by default.
Source code in src/mechestim/linalg/_solvers.py
pinv(a, rcond=None, hermitian=False)
Pseudoinverse with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
pinv_cost(m, n)
FLOP cost of pseudoinverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: m * n * min(m, n). |
Notes
Computed via SVD.
Source code in src/mechestim/linalg/_solvers.py
solve(a, b)
Solve linear system with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
solve_cost(n, nrhs=1, symmetric=False)
FLOP cost of solving a linear system Ax = b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
nrhs
|
int
|
Number of right-hand sides. Default is 1. |
1
|
symmetric
|
bool
|
If True, assume Cholesky factorization. Default is False (LU). |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Uses \(n^3/3 + 2n^2 \cdot n_{\text{rhs}}\) for Cholesky (symmetric; two triangular solves), or \(2n^3/3 + 2n^2 \cdot n_{\text{rhs}}\) for LU (general; two triangular solves). Source: Golub & Van Loan, Matrix Computations, 4th ed.
Source code in src/mechestim/linalg/_solvers.py
tensorinv(a, ind=2)
Tensor inverse with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
tensorinv_cost(a_shape, ind=2)
FLOP cost of tensor inverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_shape
|
tuple of int
|
Shape of the input tensor. |
required |
ind
|
int
|
Number of leading indices. Default is 2. |
2
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(n^3\) where \(n\) = product of leading dims. |
Notes
Reduces to a standard matrix inverse after reshaping.
Source code in src/mechestim/linalg/_solvers.py
tensorsolve(a, b, axes=None)
Tensor solve with FLOP counting.
Source code in src/mechestim/linalg/_solvers.py
tensorsolve_cost(a_shape, ind=None)
FLOP cost of tensor solve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_shape
|
tuple of int
|
Shape of the coefficient tensor. |
required |
ind
|
int or None
|
Number of leading indices for the solution. Default is 2. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: \(n^3\) where \(n\) = product of trailing dims. |
Notes
Reduces to a standard linear solve after reshaping.
Source code in src/mechestim/linalg/_solvers.py
mechestim.linalg._properties
Matrix property wrappers with FLOP counting.
cond(x, p=None)
Condition number with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
cond_cost(m, n, p=None)
FLOP cost of condition number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
p
|
(None, 2, -2, 1, -1, inf, -inf)
|
Norm type. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
For p=None, p=2, or p=-2, computed via SVD (cost mnmin(m,n)).
For p=1, p=-1, p=inf, or p=-inf, computed via LU factorization
(cost ~min(m,n)^3 + m*n for norm).
Source code in src/mechestim/linalg/_properties.py
det(a)
Determinant with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
det_cost(n, symmetric=False)
FLOP cost of determinant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
symmetric
|
bool
|
If True, assume symmetric input. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Uses \(n^3/3\) for symmetric input (Cholesky), or \(2n^3/3\) for general input (LU factorization).
Source code in src/mechestim/linalg/_properties.py
matrix_norm(x, ord='fro', keepdims=False)
Matrix norm with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
matrix_norm_cost(shape, ord=None)
FLOP cost of matrix norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple of int
|
Shape of the input array (last two dims are the matrix). |
required |
ord
|
(None, 'fro', 'nuc', inf, -inf, 1, -1, 2, -2)
|
Order of the norm. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
SVD-based norms (2-norm, nuclear norm) cost m * n * min(m, n). Frobenius norm costs 2mn. Entry-sum norms cost mn.
Source code in src/mechestim/linalg/_properties.py
matrix_rank(A, tol=None, hermitian=False)
Matrix rank with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
matrix_rank_cost(m, n)
FLOP cost of matrix rank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: m * n * min(m, n). |
Notes
Computed via SVD.
Source code in src/mechestim/linalg/_properties.py
norm(x, ord=None, axis=None, keepdims=False)
Matrix or vector norm with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
norm_cost(shape, ord=None)
FLOP cost of matrix or vector norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple of int
|
Shape of the input array (or effective shape along norm axes). |
required |
ord
|
(None, 'fro', 'nuc', inf, -inf, int)
|
Order of the norm. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Cost depends on the ord parameter and input dimensionality.
SVD-based norms (2-norm, nuclear norm) cost m * n * min(m, n).
Source code in src/mechestim/linalg/_properties.py
slogdet(a)
Sign and log-determinant with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
slogdet_cost(n, symmetric=False)
FLOP cost of sign and log-determinant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
symmetric
|
bool
|
If True, assume symmetric input. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Uses \(n^3/3\) for symmetric input (Cholesky), or \(2n^3/3\) for general input (LU factorization).
Source code in src/mechestim/linalg/_properties.py
trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
Matrix trace with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
trace_cost(n)
FLOP cost of matrix trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of diagonal elements to sum. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: n. |
Notes
Simply sums n diagonal elements.
Source code in src/mechestim/linalg/_properties.py
vector_norm(x, ord=2, axis=None, keepdims=False)
Vector norm with FLOP counting.
Source code in src/mechestim/linalg/_properties.py
vector_norm_cost(shape, ord=None)
FLOP cost of vector norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple of int
|
Shape of the input array (or effective shape along norm axes). |
required |
ord
|
(None, inf, -inf, int, float)
|
Order of the norm. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Most norms cost n FLOPs (one pass over elements). General p-norms cost 2n due to exponentiation.
Source code in src/mechestim/linalg/_properties.py
mechestim.linalg._compound
Compound linalg operations with FLOP counting.
matrix_power(a, n)
Matrix power with FLOP counting.
Source code in src/mechestim/linalg/_compound.py
matrix_power_cost(n, k)
FLOP cost of matrix power A**k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Matrix dimension. |
required |
k
|
int
|
Exponent. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Notes
Uses exponentiation by repeated squaring. For \(k < 0\), adds \(n^3\) for the initial matrix inversion.
Source code in src/mechestim/linalg/_compound.py
multi_dot(arrays, *, out=None)
Efficient multi-matrix dot product with FLOP counting.
Source code in src/mechestim/linalg/_compound.py
multi_dot_cost(shapes)
FLOP cost of optimal matrix chain multiplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes
|
list of tuple of int
|
Shapes of the matrices to be multiplied. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count using optimal parenthesization. |
Notes
Uses dynamic programming for optimal parenthesization. Source: Cormen et al., Introduction to Algorithms (CLRS), §15.2.
Source code in src/mechestim/linalg/_compound.py
mechestim.linalg._aliases
Linalg namespace aliases that delegate to top-level mechestim operations.
These functions exist in numpy.linalg as convenience aliases. FLOP costs are handled by the delegated top-level implementations.