Turn non-automated tests into Cargo examples #24
38
app-proto/examples/point-on-sphere.rs
Normal file
38
app-proto/examples/point-on-sphere.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
40
app-proto/examples/three-spheres.rs
Normal file
40
app-proto/examples/three-spheres.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
use nalgebra::DMatrix;
|
||||||
|
|
||||||
|
use dyna3::engine::{Q, realize_gram, sphere, PartialMatrix};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let gram = {
|
||||||
|
let mut gram_to_be = PartialMatrix::new();
|
||||||
|
for j in 0..3 {
|
||||||
|
for k in j..3 {
|
||||||
|
gram_to_be.push_sym(j, k, if j == k { 1.0 } else { -1.0 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gram_to_be
|
||||||
|
};
|
||||||
|
let guess = {
|
||||||
|
let a: f64 = 0.75_f64.sqrt();
|
||||||
|
DMatrix::from_columns(&[
|
||||||
|
sphere(1.0, 0.0, 0.0, 1.0),
|
||||||
|
sphere(-0.5, a, 0.0, 1.0),
|
||||||
|
sphere(-0.5, -a, 0.0, 1.0)
|
||||||
|
])
|
||||||
|
};
|
||||||
|
println!();
|
||||||
|
let (config, success, history) = realize_gram(
|
||||||
|
&gram, guess, &[],
|
||||||
|
1.0e-12, 0.5, 0.9, 1.1, 200, 110
|
||||||
|
);
|
||||||
|
print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
# based on "Enabling print statements in Cargo tests", by Jon Almeida
|
# run all Cargo examples, as described here:
|
||||||
#
|
#
|
||||||
# https://jonalmeida.com/posts/2015/01/23/print-cargo/
|
# Karol Kuczmarski. "Add examples to your Rust libraries"
|
||||||
|
# http://xion.io/post/code/rust-examples.html
|
||||||
#
|
#
|
||||||
|
|
||||||
cargo test -- --nocapture engine::tests::irisawa_hexlet_test
|
cargo run --example three-spheres
|
||||||
cargo test -- --nocapture engine::tests::three_spheres_example
|
cargo run --example point-on-sphere
|
||||||
cargo test -- --nocapture engine::tests::point_on_sphere_example
|
|
@ -4,7 +4,9 @@ use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */
|
|||||||
|
|
||||||
// --- elements ---
|
// --- elements ---
|
||||||
|
|
||||||
#[cfg(test)]
|
/* KLUDGE */
|
||||||
|
// temporarily remove test gate so we can use `point` in examples
|
||||||
|
/* #[cfg(test)] */
|
||||||
pub fn point(x: f64, y: f64, z: f64) -> DVector<f64> {
|
pub fn point(x: f64, y: f64, z: f64) -> DVector<f64> {
|
||||||
DVector::from_column_slice(&[x, y, z, 0.5, 0.5*(x*x + y*y + z*z)])
|
DVector::from_column_slice(&[x, y, z, 0.5, 0.5*(x*x + y*y + z*z)])
|
||||||
}
|
}
|
||||||
@ -113,7 +115,7 @@ impl DescentHistory {
|
|||||||
|
|
||||||
// the Lorentz form
|
// the Lorentz form
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref Q: DMatrix<f64> = DMatrix::from_row_slice(5, 5, &[
|
pub static ref Q: DMatrix<f64> = DMatrix::from_row_slice(5, 5, &[
|
||||||
1.0, 0.0, 0.0, 0.0, 0.0,
|
1.0, 0.0, 0.0, 0.0, 0.0,
|
||||||
0.0, 1.0, 0.0, 0.0, 0.0,
|
0.0, 1.0, 0.0, 0.0, 0.0,
|
||||||
0.0, 0.0, 1.0, 0.0, 0.0,
|
0.0, 0.0, 1.0, 0.0, 0.0,
|
||||||
@ -414,96 +416,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- process inspection examples ---
|
|
||||||
|
|
||||||
// these tests are meant for human inspection, not automated use. run them
|
|
||||||
// one at a time in `--nocapture` mode and read through the results and
|
|
||||||
// optimization histories that they print out. the `run-examples` script
|
|
||||||
// will run all of them
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn three_spheres_example() {
|
|
||||||
let gram = PartialMatrix({
|
|
||||||
let mut entries = Vec::<MatrixEntry>::new();
|
|
||||||
for j in 0..3 {
|
|
||||||
for k in 0..3 {
|
|
||||||
entries.push(MatrixEntry {
|
|
||||||
index: (j, k),
|
|
||||||
value: if j == k { 1.0 } else { -1.0 }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entries
|
|
||||||
});
|
|
||||||
let guess = {
|
|
||||||
let a: f64 = 0.75_f64.sqrt();
|
|
||||||
DMatrix::from_columns(&[
|
|
||||||
sphere(1.0, 0.0, 0.0, 1.0),
|
|
||||||
sphere(-0.5, a, 0.0, 1.0),
|
|
||||||
sphere(-0.5, -a, 0.0, 1.0)
|
|
||||||
])
|
|
||||||
};
|
|
||||||
println!();
|
|
||||||
let (config, success, history) = realize_gram(
|
|
||||||
&gram, guess, &[],
|
|
||||||
1.0e-12, 0.5, 0.9, 1.1, 200, 110
|
|
||||||
);
|
|
||||||
print!("\nCompleted Gram matrix:{}", config.tr_mul(&*Q) * &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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn point_on_sphere_example() {
|
|
||||||
let gram = PartialMatrix({
|
|
||||||
let mut entries = Vec::<MatrixEntry>::new();
|
|
||||||
for j in 0..2 {
|
|
||||||
for k in 0..2 {
|
|
||||||
entries.push(MatrixEntry {
|
|
||||||
index: (j, k),
|
|
||||||
value: if (j, k) == (1, 1) { 1.0 } else { 0.0 }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entries
|
|
||||||
});
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TO DO */
|
|
||||||
// --- new test placed here to avoid merge conflict ---
|
|
||||||
|
|
||||||
// at the frozen indices, the optimization steps should have exact zeros,
|
// at the frozen indices, the optimization steps should have exact zeros,
|
||||||
// and the realized configuration should match the initial guess
|
// and the realized configuration should match the initial guess
|
||||||
#[test]
|
#[test]
|
||||||
|
1
app-proto/src/lib.rs
Normal file
1
app-proto/src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod engine;
|
Loading…
Reference in New Issue
Block a user