use nalgebra::DMatrix;

use dyna3::engine::{Q, point, realize_gram, sphere, PartialMatrix};

fn main() {
    let gram = {
        let mut gram_to_be = PartialMatrix::new();
        for j in 0..2 {
            for k in j..2 {
                gram_to_be.push_sym(j, k, if (j, k) == (1, 1) { 1.0 } else { 0.0 });
            }
        }
        gram_to_be
    };
    let guess = DMatrix::from_columns(&[
        point(0.0, 0.0, 2.0),
        sphere(0.0, 0.0, 0.0, 1.0)
    ]);
    let frozen = [(3, 0)];
    println!();
    let (config, success, history) = realize_gram(
        &gram, guess, &frozen,
        1.0e-12, 0.5, 0.9, 1.1, 200, 110
    );
    print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &config);
    print!("Configuration:{}", config);
    if success {
        println!("Target accuracy achieved!");
    } else {
        println!("Failed to reach target accuracy");
    }
    println!("Steps: {}", history.scaled_loss.len() - 1);
    println!("Loss: {}", history.scaled_loss.last().unwrap());
    println!("\nStep │ Loss\n─────┼────────────────────────────────");
    for (step, scaled_loss) in history.scaled_loss.into_iter().enumerate() {
        println!("{:<4} │ {}", step, scaled_loss);
    }
}