Skip to main content

floating_zone_walk

Function floating_zone_walk 

Source
pub fn floating_zone_walk<E, Err>(
    local_dims: &[usize],
    init_p: &MultiIndex,
    max_sweeps: usize,
    early_stop_tol: f64,
    eval_batch: E,
) -> Result<(MultiIndex, f64), Err>
where E: FnMut(Option<usize>, &[MultiIndex]) -> Result<Vec<f64>, Err>,
Expand description

Walk one floating-zone search trajectory.

Mirrors TensorCrossInterpolation.jl’s _floatingzone: starting from init_p, each sweep visits every site in order and moves that site’s coordinate to the value with the largest error (as measured by eval_batch), keeping the running maximum error monotonically non-decreasing. The walk stops when a sweep does not increase the maximum error (the trajectory is stuck on a local maximum) or when the maximum error exceeds early_stop_tol (the point is already significant), or after max_sweeps sweeps as a safety bound.

§Arguments

  • local_dims - Local dimension of each site.
  • init_p - Starting multi-index; must have length local_dims.len().
  • max_sweeps - Upper bound on the number of coordinate sweeps. The no-improvement early stop almost always fires first.
  • early_stop_tol - Stop walking once the maximum error exceeds this value; the caller has found a significantly wrong point.
  • eval_batch - Evaluates the error magnitude |f - tt| at a batch of multi-indices. It is called once for the starting point with a scan site of None, and then once per site per sweep with Some(site) and that site’s local_dims[site] - 1 other candidate points: the candidate equal to the current pivot is not re-evaluated, because the walk already knows its error from the step that moved there. The scan site is passed so that a caller whose evaluator exploits scan structure can declare it rather than infer it from the batch, which a single-point batch cannot support.

§Returns

The final pivot and the maximum error encountered along the walk. The returned error may exceed early_stop_tol; the caller decides whether the point is significant.

§Errors

Propagates the error returned by eval_batch unchanged - typically an operation failure or an index mismatch from the underlying evaluator.

§Examples

use tensor4all_core::floating_zone_walk;

// A separable error surface whose maximum is the all-last-coordinate
// point, so a greedy coordinate walk must find it exactly.
let local_dims = [3usize, 4, 2];
let error_at = |point: &Vec<usize>| point.iter().map(|&c| c as f64).sum::<f64>();
let mut evaluated = 0usize;
let (pivot, error) = floating_zone_walk::<_, std::convert::Infallible>(
    &local_dims,
    &vec![0usize, 0, 0],
    16,
    f64::INFINITY,
    |_site, points| {
        evaluated += points.len();
        Ok(points.iter().map(error_at).collect())
    },
)?;

assert_eq!(pivot, vec![2, 3, 1]);
assert_eq!(error, 6.0);
// One point for the start, then `local_dims[site] - 1` per site scan: the
// coordinate the pivot already holds is never re-evaluated.
assert_eq!(evaluated, 1 + 2 * ((3 - 1) + (4 - 1) + (2 - 1)));