fn

qr

Tensor
qr(x: Tensor, mode: str = 'reduced')
source

QR decomposition of a matrix.

Factorizes a matrix ARm×nA \in \mathbb{R}^{m \times n} as

A=QR,A \,=\, Q\,R,

where QQ has orthonormal columns (QQ=IQ^\top Q = I) and RR is upper-triangular. Used for orthogonalizing a basis, solving least-squares (minAxb2\min \|Ax - b\|_2), and computing eigenvalues via QR iteration.

Parameters

xTensor
Input matrix of shape (*, m, n).
mode(reduced, complete, r)= "reduced"
"reduced" (default): QQ is (m, k) and RR is (k, n) with k=min(m,n)k = \min(m, n). "complete": QQ is (m, m) and RR is (m, n). "r": return only RR (Q is an empty tensor).

Returns

Tensor

Orthogonal factor.

Notes

Computed via Householder reflections (LAPACK geqrf). Cost is O(2mn223n3)O(2 m n^2 - \tfrac{2}{3} n^3) for mnm \ge n.

The diagonal of RR may carry arbitrary signs (LAPACK convention) — the factorization is unique only up to a diagonal sign matrix. Backward routes RR through a Cholesky of AAA^\top A (sign-robust) and QQ through the Stiefel-manifold tangent projection.

Examples

>>> import lucid
>>> from lucid.linalg import qr
>>> A = lucid.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
>>> Q, R = qr(A)
>>> Q.T @ Q
Tensor([[1.0000, 0.0000],
        [0.0000, 1.0000]])