Skip to main content

qr_with_in

Function qr_with_in 

Source
pub fn qr_with_in<T>(
    t: &IdxTensor,
    left_inds: &[DynIndex],
    options: &QrOptions,
    context: &ExecutionContext,
) -> Result<(IdxTensor, IdxTensor), QrError>
Expand description

Compute QR decomposition in a caller-owned execution context.

The factorization, truncation slicing, and result assembly all execute in context. With a CUDA context the retained-rank decision reads back only the k-element row-norm vector through IdxTensor::read_decision_data; with a CPU context the rank decision uses the same host read as qr_with.

§Arguments

  • t - Input tensor, which must belong to context.
  • left_inds - Indices to place on the left (row) side of the unfolded matrix.
  • options - QR options including rtol for truncation control.
  • context - Caller-owned execution context owning the input and results.

§Examples

use std::sync::Arc;
use tensor4all_core::qr::{QrOptions, qr_with_in};
use tensor4all_core::{DynIndex, ExecutionContext, IdxTensor, TensorContractionLike};
use tensor4all_tensorbackend::CpuExecutionContext;
use tensor4all_tensorbackend::CpuBackend;

let context = ExecutionContext::Cpu(Arc::new(
    CpuExecutionContext::from_backend(CpuBackend::new()),
));
let i = DynIndex::new_dyn(4);
let j = DynIndex::new_dyn(3);
let data: Vec<f64> = (0..12).map(|x| x as f64).collect();
let t = IdxTensor::from_dense_in(&context, vec![i.clone(), j.clone()], data)?;
let (q, r) = qr_with_in::<f64>(&t, &[i.clone()], &QrOptions::default(), &context)?;
let recovered = q.contract_pair(&r)?;
let expected = t.to_vec::<f64>()?;
let actual = recovered.to_vec::<f64>()?;
let residual = expected
    .iter()
    .zip(actual.iter())
    .map(|(a, b)| (a - b).abs())
    .fold(0.0_f64, f64::max);
assert!(residual < 1e-12, "QR reconstruction residual {residual}");

§Errors

Returns QrError when the tensor does not belong to context, when the indices, storage, or options are invalid, or when the factorization or explicit decision readback fails. When this build cannot execute in context, the rejection carries IdxTensorError::UnsupportedExecutionContext as its source.