Linear Algebra
Use this page to learn how to use fnp.linalg operations and their FLOP costs.
You will learn:
- How to use decompositions, solvers, and property operations in fnp.linalg
- How symmetric inputs reduce linalg costs
- How to query linalg costs before running them
Prerequisites
Available operations
Decompositions
| Operation | Cost | Weight | Notes |
|---|---|---|---|
fnp.linalg.svd(A, k=k) | 1.0 | Truncated, with vectors | |
fnp.linalg.eig(A) | 4.0 | General eigendecomposition | |
fnp.linalg.eigh(A) | 4.0 | Symmetric eigendecomposition | |
fnp.linalg.cholesky(A) | 4.0 | Cholesky (symmetric positive definite) | |
fnp.linalg.qr(A) | 4.0 | Householder QR (FMA=2) | |
fnp.linalg.eigvals(A) | 4.0 | Eigenvalues only | |
fnp.linalg.eigvalsh(A) | 4.0 | Symmetric eigenvalues only | |
fnp.linalg.svdvals(A) | 1.0 | Full singular values only |
For SVD, a=max(m,n) and b=min(m,n). Truncated values-only calls
(svd(A, compute_uv=False, k=k) or svdvals(A, k=k)) bill
min(4mnk+2(m+n)k²+2k³, 2ab²+2b³) for 1 <= k < b. At k=b, the
corresponding full economy price applies. Full calls without k retain their
thin/full-vector/values-only formulas, including the rectangular full-vector
premium. Multiply per-matrix prices by the batch size and apply dtype/complex
rates. The backend returns exact factors; pricing is an analytical convention.
See the cost model
for details and the limits of the near-full-rank accounting guarantee.
Solvers
solve_cost(n) always returns n^3 regardless of the symmetric or nrhs
parameters — those arguments exist for API compatibility but are currently
ignored in the cost model.
| Operation | Cost | Weight |
|---|---|---|
fnp.linalg.solve(A, b) | 4.0 | |
fnp.linalg.inv(A) | 4.0 | |
fnp.linalg.lstsq(A, b) | 4.0 | |
fnp.linalg.pinv(A) | 4.0 |
inv of a symmetric matrix returns a SymmetricTensor.
Properties
| Operation | Cost | Weight |
|---|---|---|
fnp.linalg.det(A) | 4.0 | |
fnp.linalg.slogdet(A) | 4.0 | |
fnp.linalg.norm(x) | depends on ord | varies |
fnp.linalg.cond(A) | varies | |
fnp.linalg.matrix_rank(A) | Values-only SVD per matrix + comparisons per broadcast (matrix,tol/rtol) pair | varies |
fnp.linalg.trace(A) | varies |
Compound
| Operation | Cost | Notes |
|---|---|---|
fnp.linalg.multi_dot(arrays) | Optimal chain ordering | Uses np.linalg.multi_dot |
fnp.linalg.matrix_power(A, n) | Repeated squaring |
Symmetric input savings
Pass a SymmetricTensor to get automatic cost reductions:
import flopscope as flops
import flopscope.numpy as fnp
with flops.BudgetContext(flop_budget=10**8) as budget:
A = flops.as_symmetric(fnp.multiply(fnp.eye(10), 2.0), symmetry=(0, 1))
# solve_cost(n=10) = n^3 = 1000 FLOPs (symmetric/nrhs params are currently ignored)
x = fnp.linalg.solve(A, fnp.ones(10))
# inv returns SymmetricTensor
A_inv = fnp.linalg.inv(A)
print(isinstance(A_inv, flops.SymmetricTensor)) # TrueSee Exploit Symmetry Savings for full details.
Query cost before running
cost = flops.accounting.svd_cost(m=256, n=256, k=10)
print(f"SVD cost: {cost:,}") # 2,725,840 (values-only default; dtype-neutral query)
cost = flops.accounting.solve_cost(n=256)
print(f"Solve cost: {cost:,}") # 16,777,216 (= 256^3; symmetric/nrhs params currently ignored)Common pitfalls
Symptom: Using numpy.linalg.svd instead of fnp.linalg.svd
Fix: Operations called through numpy directly bypass FLOP counting. Always use fnp.linalg.*.
Related pages
- Exploit Symmetry Savings — symmetry-aware cost reductions
- Plan Your Budget — query costs before running
- API Reference — full list of supported operations