Skip to contents

Letting be or , the full QR decomposition of a matrix is defined as


linalg_qr(A, mode = "reduced")



(Tensor): tensor of shape (*, m, n) where * is zero or more batch dimensions.


(str, optional): one of 'reduced', 'complete', 'r'. Controls the shape of the returned tensors. Default: 'reduced'.


A list (Q, R).


A=QRQKm×m,RKm×n A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times m}, R \in \mathbb{K}^{m \times n}}

where is orthogonal in the real case and unitary in the complex case, and is upper triangular. When m > n (tall matrix), as R is upper triangular, its last m - n rows are zero. In this case, we can drop the last m - n columns of Q to form the reduced QR decomposition:

A=QRQKm×n,RKn×n A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times n}, R \in \mathbb{K}^{n \times n}}

The reduced QR decomposition agrees with the full QR decomposition when n >= m (wide matrix). Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions. The parameter mode chooses between the full and reduced QR decomposition.

If A has shape (*, m, n), denoting k = min(m, n)

  • mode = 'reduced' (default): Returns (Q, R) of shapes (*, m, k), (*, k, n) respectively.

  • mode = 'complete': Returns (Q, R) of shapes (*, m, m), (*, m, n) respectively.

  • mode = 'r': Computes only the reduced R. Returns (Q, R) with Q empty and R of shape (*, k, n).


if (torch_is_installed()) {
a <- torch_tensor(rbind(c(12., -51, 4), c(6, 167, -68), c(-4, 24, -41)))
qr <- linalg_qr(a)

torch_mm(qr[[1]], qr[[2]])$round()
torch_mm(qr[[1]]$t(), qr[[1]])$round()
#> torch_tensor
#>  1  0  0
#>  0  1  0
#>  0  0  1
#> [ CPUFloatType{3,3} ]