Skip to content
fn

qr_op

→std::vector<TensorImplPtr>
int qr_op(const int & a)

Python wrappers

Public Python APIs implemented by this engine symbol.

Compute the reduced QR factorisation of a float matrix.

Forward calls LAPACK *geqrf + *orgqr on the CPU stream or mlx::core::linalg::qr on the GPU stream and returns the two factors as a length-2 vector {Q, R}. Autograd is not wired — both outputs are leaves in the gradient graph.

Math

A=QR,Q⊤Q=Ik,R=triu(R). A = Q R, \qquad Q^\top Q = I_k, \qquad R = \mathrm{triu}(R).

Shape

  • a: (..., m, n).
  • Q: (..., m, k), R: (..., k, n) with k=min⁡(m,n)k = \min(m, n).

References

Golub & Van Loan, Matrix Computations (4th ed.), Algorithm 5.2.1 (Householder QR). Walter & Lehmann, "Algorithmic Differentiation of Linear Algebra Functions" (2010), §3. Bettale, "Differentiating the QR Decomposition" (2013).

See Also

inv_op : Matrix inverse, often composed with QR for least squares. Lstsq.h : Direct least-squares solver built on top of QR.

Parameters

aTensorImplPtr
Input matrix of shape (..., m, n). Must be at least 2-D and have a floating-point dtype. Leading dimensions are treated as independent batch dimensions.

Returns

std::vector<TensorImplPtr>

Length-2 vector {Q, R} where - Q has shape (..., m, k) with Q⊤Q=IkQ^\top Q = I_k, - R has shape (..., k, n) and is upper-triangular, and k=min⁡(m,n)k = \min(m, n).

Raises

ValueError
If a is fewer than 2 dimensions or has a non-float dtype.

Notes

  • Only the reduced ("thin") factorisation is produced; mode='complete' parity with the reference framework is not currently exposed.
  • No backward is registered. Wrapping qr_op inside a graph where Q or R requires gradients will yield leaf tensors.