use super::{francis, standardize};
const HESSENBERG: [f64; 16] = [
1.5, 0.5, -0.25, 1.0, 1.0, 1.25, 0.5, -0.75, 0.0, 0.625, -1.0, 0.5, 0.0, 0.0, 0.875, 1.75,
];
fn identity(n: usize) -> Vec<f64> {
(0..n * n)
.map(|k| if k % (n + 1) == 0 { 1.0 } else { 0.0 })
.collect()
}
fn scaled(values: &[f64], exponent: i32) -> Vec<f64> {
values.iter().map(|v| v * 2.0_f64.powi(exponent)).collect()
}
#[test]
fn francis_iteration_is_power_of_two_equivariant_beyond_the_gate() {
let n = 4;
let mut unit = HESSENBERG.to_vec();
let mut unit_q = identity(n);
francis::run::<f64, true>(&mut unit, &mut unit_q, n).expect("unit scale converges");
for exponent in [600, -600] {
let mut h = scaled(&HESSENBERG, exponent);
let mut q = identity(n);
francis::run::<f64, true>(&mut h, &mut q, n)
.unwrap_or_else(|error| panic!("2^{exponent}: {error}"));
assert_eq!(scaled(&h, -exponent), unit, "2^{exponent}: T");
assert_eq!(q, unit_q, "2^{exponent}: Q");
}
}
#[test]
fn standardization_is_power_of_two_equivariant_beyond_the_gate() {
let block = [1.5, 1.0, 0.5, 1.25];
let mut unit = block.to_vec();
let mut unit_q = identity(2);
standardize::standardize(&mut unit, &mut unit_q, 2);
assert_eq!(unit[2], 0.0, "unit block triangularized");
for exponent in [600, -600] {
let mut t = scaled(&block, exponent);
let mut q = identity(2);
standardize::standardize(&mut t, &mut q, 2);
assert_eq!(scaled(&t, -exponent), unit, "2^{exponent}: T");
assert_eq!(q, unit_q, "2^{exponent}: Q");
}
}
#[test]
fn gate_keeps_the_deflation_floor_below_epsilon_times_the_norm() {
use crate::application::linalg::scaling;
use eunomia::{FloatElement, NumericElement, F16};
let n = 64;
let mut values = vec![F16::from_f64(0.0); n * n];
values[0] = F16::from_f64(0.3);
values[1] = F16::from_f64(0.225);
let frobenius = |scale: i32| {
values
.iter()
.map(|v| v.scale_binary(scale).to_f64().powi(2))
.sum::<f64>()
.sqrt()
};
let (safmin, eps) = (2.0_f64.powi(-14), 2.0_f64.powi(-10));
let joint_floor = (n as f64).sqrt() * safmin;
assert!(joint_floor > eps * frobenius(0), "the input must violate");
let exponent = scaling::gate_exponent(&values, 2, super::francis_bound(n))
.expect("the range is non-empty")
.expect("0.3 is below the floor end");
assert!(joint_floor <= eps * frobenius(-exponent), "{exponent}");
let ones = vec![F16::from_f64(0.3); n * n];
let unmoved = scaling::gate_exponent(&ones, 2, super::francis_bound(n)).expect("non-empty");
assert_eq!(unmoved, None);
}
#[test]
fn bulge_starts_below_a_negligible_subdiagonal() {
let n = 4;
let mut h = vec![
0.001_110_045_906_518_637_5,
-0.875,
-0.125,
0.875,
1.086_723_651_168_370_5e-17,
0.000_575_212_256_743_316_8,
-0.375,
-0.625,
0.0,
0.375,
-0.375,
0.125,
0.0,
0.0,
0.875,
-0.625,
];
let original = h[0];
let mut q = identity(n);
francis::run::<f64, true>(&mut h, &mut q, n).expect("converges");
assert_eq!(h[0].to_bits(), original.to_bits(), "{h:?}");
}