lstsq#
- class saiunit.linalg.lstsq(a, b, rcond=None, *, numpy_resid=False, **kwargs)#
Return the least-squares solution to a linear equation.
SaiUnit implementation of
numpy.linalg.lstsq().Finds
xthat minimizes||a @ x - b||. The resulting unit ofxisb.unit / a.unit.- Parameters:
a (
Array|ndarray|bool|number|bool|int|float|complex| saiunit.Quantity) – Coefficient matrix of shape(M, N).b (
Array|ndarray|bool|number|bool|int|float|complex| saiunit.Quantity) – Right-hand side of shape(M,)or(M, K).rcond (
float|None) – Cut-off ratio for small singular values. Singular values smaller thanrcond * largest_singular_valueare treated as zero. IfNone(default), the optimal value is used to reduce floating point errors.numpy_resid (
bool) – IfTrue, compute and return residuals in the same way as NumPy’slinalg.lstsq. IfFalse(default), a more efficient method is used to compute residuals.
- Return type:
tuple[Array|ndarray|bool|number|bool|int|float|complex| saiunit.Quantity,Array,Array,Array]- Returns:
x (ndarray or Quantity) – Least-squares solution of shape
(N,)or(N, K). The resulting unit isb.unit / a.unit.residuals (ndarray) – Sum of squared residuals of shape
()or(K,).rank (ndarray) – Effective rank of
a.s (ndarray) – Singular values of
a.
See also
saiunit.linalg.solveSolve a square linear system exactly.
saiunit.linalg.pinvCompute the Moore-Penrose pseudo-inverse.
Examples
>>> import saiunit as u >>> import jax.numpy as jnp >>> a = jnp.array([[1, 2], ... [3, 4]]) * u.second >>> b = jnp.array([5, 6]) * u.meter >>> x, residuals, rank, s = u.linalg.lstsq(a, b) >>> x.unit Unit("m / s")