Four expansion-v2 items (finished after a session-limit interruption): Weak/integral-form discovery (crates/lawsynth-weakform, specs/weak-form) — the top noise-robustness technique from PySINDy: integrate candidates against compactly- supported test functions, NO differentiation of noisy data. ~23x lower coefficient error than finite-difference strong-form under noise. 20 tests, deterministic. Implicit/rational dynamics (crates/lawsynth-implicit, specs/implicit-dynamics) — SINDy-PI alternating-LHS nullspace solve; recovers rational laws (Michaelis-Menten) explicit regression can't express. Honest degree-2 identifiability limit documented (multi-dimensional nullspace); residual-floor + derivative-preferring tie-break for canonical selection. 10 tests. Optimizer breadth (lawsynth-sparse) — FROLS (orthogonal-least-squares, ERR ordering), SSR (stepwise), trapping (stability-biased); --solver frols|ssr|trapping. 24 tests; default path byte-identical. SRBench harness (benchmarks/) — 18 Strogatz ODE systems + scoring + a determinism column (byte-identical on replay — a metric no stochastic competitor can report). 3/3 Strogatz recovered at R²=1.0. srbench_report.py + 5 tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
62 lines
2.2 KiB
Rust
62 lines
2.2 KiB
Rust
//! Weak-form discovery recovers the exact oscillator coefficients on clean data.
|
|
|
|
mod common;
|
|
|
|
use common::{DAMPING, dataset, oscillator};
|
|
use lawsynth_weakform::{WeakConfig, weak_discover};
|
|
|
|
fn term_index(names: &[String], target: &str) -> usize {
|
|
names
|
|
.iter()
|
|
.position(|name| name == target)
|
|
.unwrap_or_else(|| panic!("library is missing term `{target}` in {names:?}"))
|
|
}
|
|
|
|
#[test]
|
|
fn recovers_the_damped_oscillator_on_clean_data() {
|
|
let (time, xs, ys) = oscillator(1500, 0.01);
|
|
let data = dataset(time, xs, ys);
|
|
|
|
let result = weak_discover(&data, &WeakConfig::default()).unwrap();
|
|
|
|
assert_eq!(result.state_names, vec!["x".to_string(), "y".to_string()]);
|
|
let idx_x = term_index(&result.term_names, "x");
|
|
let idx_y = term_index(&result.term_names, "y");
|
|
|
|
// ẋ = y → coefficient of `y` is 1, coefficient of `x` is 0.
|
|
let x_law = &result.coefficients[0];
|
|
assert!((x_law[idx_y] - 1.0).abs() < 2e-2, "x'→y = {}", x_law[idx_y]);
|
|
assert!(x_law[idx_x].abs() < 2e-2, "x'→x = {}", x_law[idx_x]);
|
|
|
|
// ẏ = -x - 0.3 y.
|
|
let y_law = &result.coefficients[1];
|
|
assert!((y_law[idx_x] + 1.0).abs() < 2e-2, "y'→x = {}", y_law[idx_x]);
|
|
assert!((y_law[idx_y] + DAMPING).abs() < 2e-2, "y'→y = {}", y_law[idx_y]);
|
|
|
|
// Every term outside the true support was pruned to exactly zero.
|
|
for (index, &coefficient) in x_law.iter().enumerate() {
|
|
if index != idx_y {
|
|
assert!(coefficient.abs() < 2e-2, "spurious x' term {index} = {coefficient}");
|
|
}
|
|
}
|
|
for (index, &coefficient) in y_law.iter().enumerate() {
|
|
if index != idx_x && index != idx_y {
|
|
assert!(coefficient.abs() < 2e-2, "spurious y' term {index} = {coefficient}");
|
|
}
|
|
}
|
|
|
|
// Diagnostics are well-formed.
|
|
assert_eq!(result.diagnostics.test_functions, 16);
|
|
assert!(result.diagnostics.condition.is_finite());
|
|
assert!(result.diagnostics.max_residual < 1.0);
|
|
}
|
|
|
|
#[test]
|
|
fn renders_a_readable_law() {
|
|
let (time, xs, ys) = oscillator(1500, 0.01);
|
|
let data = dataset(time, xs, ys);
|
|
let result = weak_discover(&data, &WeakConfig::default()).unwrap();
|
|
let rendered = result.laws[0].render();
|
|
assert!(rendered.starts_with("d/dt x = "), "{rendered}");
|
|
}
|