lstsq

Contents

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 x that minimizes ||a @ x - b||. The resulting unit of x is b.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 than rcond * largest_singular_value are treated as zero. If None (default), the optimal value is used to reduce floating point errors.

  • numpy_resid (bool) – If True, compute and return residuals in the same way as NumPy’s linalg.lstsq. If False (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 is b.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.solve

Solve a square linear system exactly.

saiunit.linalg.pinv

Compute 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")