Skip to main content

qr

Function qr 

pub fn qr<const D: usize>(
    tensor: Tensor<D>,
    reduced: bool,
) -> (Tensor<D>, Tensor<D>)
Expand description

Computes the QR decomposition of a square or rectangular matrix using Householder reflections.

This function decomposes the input tensor A into two tensors Q, R such that A = QR, where Q is an orthonormal matrix and R is an upper triangular matrix. If reduced is true then it returns reduced Q, R. The reduced QR decomposition agrees with the full QR decomposition when n_cols >= n_rows (wide matrix).

§Arguments

  • tensor - The input tensor of shape [..., n_rows, n_cols].
  • reduced - The bool value

§Returns

A tuple of two tensors (Q, R):

  • Q - The orthonormal tensor of shape [..., n_rows, n_rows] in case of n_cols >= n_rows or reduced=false otherwise [..., n_rows, n_cols]
  • R - The upper triangular tensor of shape [..., n_rows, n_cols] in case of n_cols >= n_rows or reduced=false otherwise [..., n_cols, n_cols]

§Generic Parameters

  • D: The number of dimensions of the input tensor.

§Panics

This function will panic if the tensor checks fail:

  • The input tensor has less than 2 dimensions (D < 2).
  • The input is a quantized tensor with dtype DType::QFloat.

§Example

ⓘ
use burn::tensor::Tensor;
use burn::backend::Flex;
use burn::linalg;

fn example() {
    let device = Default::default();
    let tensor = Tensor::<2>::from_data([[3.0, 2.0], [4.0, 6.0]], &device);

    // Compute Q, R
    let (q, r) = linalg::qr::<2>(tensor);

    // Expected Output:
    // q: [[-0.6, 0.8],
    //     [-0.8, -0.6]]
    //
    // r: [[-5.0, -6.0],
    //     [0.0, -2.0]]
}