qr

Function qr 

Source
pub fn qr<T>(
    t: &TensorDynLen,
    left_inds: &[DynIndex],
) -> Result<(TensorDynLen, TensorDynLen), QrError>
Expand description

Compute QR decomposition of a tensor with arbitrary rank, returning (Q, R).

This function uses the global default rtol for truncation. See qr_with for per-call rtol control.

This function computes the thin QR decomposition, where for an unfolded matrix A (m×n), we return Q (m×k) and R (k×n) with k = min(m, n).

The input tensor can have any rank >= 2, and indices are split into left and right groups. The tensor is unfolded into a matrix by grouping left indices as rows and right indices as columns.

Truncation is performed based on R’s row norms: rows whose norm is below rtol * max_row_norm are discarded.

For the mathematical convention: [ A = Q * R ] where Q is orthogonal (or unitary for complex) and R is upper triangular.

§Arguments

  • t - Input tensor with DenseF64 or DenseC64 storage
  • left_inds - Indices to place on the left (row) side of the unfolded matrix

§Returns

A tuple (Q, R) where:

  • Q is a tensor with indices [left_inds..., bond_index] and dimensions [left_dims..., r]
  • R is a tensor with indices [bond_index, right_inds...] and dimensions [r, right_dims...] where r is the retained rank (≤ min(m, n)) determined by rtol truncation.

§Errors

Returns QrError if:

  • The tensor rank is < 2
  • Storage is not DenseF64 or DenseC64
  • left_inds is empty or contains all indices
  • left_inds contains indices not in the tensor or duplicates
  • The QR computation fails

§Examples

use tensor4all_core::{TensorDynLen, DynIndex, qr};

// Create a 4x3 matrix
let i = DynIndex::new_dyn(4);
let j = DynIndex::new_dyn(3);
// Identity-like data (4x3 column-major)
let data: Vec<f64> = (0..12).map(|x| x as f64).collect();
let t = TensorDynLen::from_dense(vec![i.clone(), j.clone()], data).unwrap();

let (q, r) = qr::<f64>(&t, &[i.clone()]).unwrap();

// Q has shape (4, bond) and R has shape (bond, 3)
assert_eq!(q.dims()[0], 4);
assert_eq!(r.dims()[r.dims().len() - 1], 3);