use la_stack::prelude::*;
fn main() -> Result<(), LaError> {
let perturbation = f64::from_bits(0x3CD0_0000_0000_0000); let a = Matrix::<3>::try_from_rows([
[1.0 + perturbation, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
])?;
let b = Vector::<3>::try_new([1.0, 2.0, 3.0])?;
let lu_x = a.lu(Tolerance::new(0.0)?)?.solve_vec(b)?.into_array();
let exact_x = a.solve_exact(b)?;
let exact_x_f64 = a.solve_exact_f64(b)?.into_array();
println!("Near-singular 3×3 system (perturbation = 2^-50 ≈ {perturbation:.2e}):");
for r in 0..3 {
print!(" [");
for c in 0..3 {
if c > 0 {
print!(", ");
}
print!("{:22.18}", a.get_checked(r, c)?);
}
println!("]");
}
println!(
"b = [{}, {}, {}]",
b.as_array()[0],
b.as_array()[1],
b.as_array()[2]
);
println!();
println!(
"f64 LU solve: x = [{:+.6e}, {:+.6e}, {:+.6e}]",
lu_x[0], lu_x[1], lu_x[2]
);
println!(
"solve_exact(): x = [{}, {}, {}]",
exact_x[0], exact_x[1], exact_x[2]
);
println!(
"solve_exact_f64(): x = [{:+.6e}, {:+.6e}, {:+.6e}]",
exact_x_f64[0], exact_x_f64[1], exact_x_f64[2]
);
Ok(())
}