Rate this Page

torch.linalg.qr#

torch.linalg.qr(A,mode='reduced',*,out=None)#

Computes the QR decomposition of a matrix.

LettingK\mathbb{K} beR\mathbb{R} orC\mathbb{C},thefull QR decomposition of a matrixAKm×nA \in \mathbb{K}^{m \times n} is defined as

whereQQ is orthogonal in the real case and unitary in the complex case,andRR is upper triangular with real diagonal (even in the complex case).

Whenm > n (tall matrix), asR is upper triangular, its lastm - n rows are zero.In this case, we can drop the lastm - n columns ofQ to form thereduced QR decomposition:

The reduced QR decomposition agrees with the full QR decomposition whenn >= m (wide matrix).

Supports input of float, double, cfloat and cdouble dtypes.Also supports batches of matrices, and ifA is a batch of matrices thenthe output has the same batch dimensions.

The parametermode chooses between the full and reduced QR decomposition.IfA has shape(*, m, n), denotingk = min(m, n)

  • mode= ‘reduced’ (default): Returns(Q, R) of shapes(*, m, k),(*, k, n) respectively.It is always differentiable.

  • mode= ‘complete’: Returns(Q, R) of shapes(*, m, m),(*, m, n) respectively.It is differentiable form <= n.

  • mode= ‘r’: Computes only the reducedR. Returns(Q, R) withQ empty andR of shape(*, k, n).It is never differentiable.

Differences withnumpy.linalg.qr:

  • mode= ‘raw’ is not implemented.

  • Unlikenumpy.linalg.qr, this function always returns a tuple of two tensors.Whenmode= ‘r’, theQ tensor is an empty tensor.

Warning

The elements in the diagonal ofR are not necessarily positive.As such, the returned QR decomposition is only unique up to the sign of the diagonal ofR.Therefore, different platforms, like NumPy, or inputs on different devices,may produce different valid decompositions.

Warning

The QR decomposition is only well-defined if the firstk = min(m, n) columnsof every matrix inA are linearly independent.If this condition is not met, no error will be thrown, but the QR producedmay be incorrect and its autodiff may fail or produce incorrect results.

Parameters
  • A (Tensor) – tensor of shape(*, m, n) where* is zero or more batch dimensions.

  • mode (str,optional) – one of‘reduced’,‘complete’,‘r’.Controls the shape of the returned tensors. Default:‘reduced’.

Keyword Arguments

out (tuple,optional) – output tuple of two tensors. Ignored ifNone. Default:None.

Returns

A named tuple(Q, R).

Examples:

>>>A=torch.tensor([[12.,-51,4],[6,167,-68],[-4,24,-41]])>>>Q,R=torch.linalg.qr(A)>>>Qtensor([[-0.8571,  0.3943,  0.3314],        [-0.4286, -0.9029, -0.0343],        [ 0.2857, -0.1714,  0.9429]])>>>Rtensor([[ -14.0000,  -21.0000,   14.0000],        [   0.0000, -175.0000,   70.0000],        [   0.0000,    0.0000,  -35.0000]])>>>(Q@R).round()tensor([[  12.,  -51.,    4.],        [   6.,  167.,  -68.],        [  -4.,   24.,  -41.]])>>>(Q.T@Q).round()tensor([[ 1.,  0.,  0.],        [ 0.,  1., -0.],        [ 0., -0.,  1.]])>>>Q2,R2=torch.linalg.qr(A,mode='r')>>>Q2tensor([])>>>torch.equal(R,R2)True>>>A=torch.randn(3,4,5)>>>Q,R=torch.linalg.qr(A,mode='complete')>>>torch.dist(Q@R,A)tensor(1.6099e-06)>>>torch.dist(Q.mT@Q,torch.eye(4))tensor(6.2158e-07)