lstsq(input: Tensor, b: Tensor, rcond: float | None = None, driver: str | None = None)Solve a least-squares linear system.
Finds minimising the squared residual
where may be over- or underdetermined. For full-rank and the solution is unique:
obtained more stably via QR or SVD without forming the normal equations.
Parameters
inputTensor(*, m, n).(*, m, k) (or (*, m)).rcondfloat or None= NoneNone selects the default driver heuristic.driver(str or None, keyword - only)= None"gels", "gelsy", "gelsd", "gelss").
None selects "gelsy" on CPU and "gels" on Metal.Returns
TensorLeast-squares solution of shape (*, n, k) (or (*, n)).
Notes
The solution is , the minimum-norm least-squares solution for
every shape and rank, and differentiates in input and b (twice).
"gels" and "gelsy" (so the default) factor a full-rank input
with a QR decomposition, as the reference's default driver does. Every
matrix is first checked for full rank from R (the singular values of the
triangle for a small batch, a condition-number estimate otherwise); one
that does not pass — rank deficient, ill-conditioned, non-finite — takes the
SVD route, which decides the rank. "gelsd" and "gelss" return the
singular values and always take the SVD route.
Examples
>>> import lucid
>>> from lucid.linalg import lstsq
>>> A = lucid.tensor([[1.0, 1.0], [1.0, 2.0], [1.0, 3.0]])
>>> b = lucid.tensor([[6.0], [9.0], [12.0]])
>>> sol, *_ = lstsq(A, b)
>>> sol
tensor([[3.], [3.]])