Skip to main content

svd_with

Function svd_with 

Source
pub fn svd_with<T>(
    t: &IdxTensor,
    left_inds: &[DynIndex],
    options: &SvdOptions,
) -> Result<(IdxTensor, IdxTensor, IdxTensor), SvdError>
Expand description

Compute SVD decomposition of a tensor with arbitrary rank, returning (U, S, V).

This function allows per-call control of the truncation policy via SvdOptions. If options.policy is None, it uses the global default policy.

§Errors

Returns an error when the operation fails (a shape or index mismatch, or /// a backend failure).

§Examples

use tensor4all_core::{DynIndex, IdxTensor};
use tensor4all_core::svd::{SvdOptions, svd_with};

let i = DynIndex::new_dyn(4);
let j = DynIndex::new_dyn(4);
// Rank-1 matrix
let mut data = vec![0.0_f64; 16];
data[0] = 1.0;
let tensor = IdxTensor::from_dense(vec![i.clone(), j.clone()], data).unwrap();

use tensor4all_core::SvdTruncationPolicy;

// Truncate with a relative per-value threshold => rank 1
let opts = SvdOptions::new().with_policy(SvdTruncationPolicy::new(1e-10));
let (u, s, _v) = svd_with::<f64>(&tensor, &[i.clone()], &opts).unwrap();
assert_eq!(s.dims()[0], 1);  // rank-1

// Truncate with max_bond_dim => capped
let opts = SvdOptions::new().with_max_bond_dim(2);
let (_u, s, _v) = svd_with::<f64>(&tensor, &[i.clone()], &opts).unwrap();
assert!(s.dims()[0] <= 2);