Skip to main content

svd_with

Function svd_with 

Source
pub fn svd_with<T>(
    t: &TensorDynLen,
    left_inds: &[DynIndex],
    options: &SvdOptions,
) -> Result<(TensorDynLen, TensorDynLen, TensorDynLen), 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.

ยงExamples

use tensor4all_core::{DynIndex, TensorDynLen};
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 = TensorDynLen::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_rank => capped
let opts = SvdOptions::new().with_max_rank(2);
let (_u, s, _v) = svd_with::<f64>(&tensor, &[i.clone()], &opts).unwrap();
assert!(s.dims()[0] <= 2);