caelum0x 771f89b099 feat(v2-A/v2-B): weak-form, implicit dynamics, FROLS/SSR/trapping, SRBench harness
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>
2026-08-18 14:49:27 +03:00

50 lines
1.8 KiB
Rust

//! Deterministic data generators shared across the integration tests.
//!
//! Trajectories are produced by a fixed-step classical RK4 integrator so the
//! samples are reproducible bit-for-bit and free of any external dependency.
//!
//! Each integration-test binary links this module independently, so helpers
//! unused by a given binary would otherwise warn.
#![allow(dead_code)]
use lawsynth_core::Identifier;
use lawsynth_data::{Dataset, NumericColumn, TimeAxis};
/// Integrates `dx/dt = f(x)` with fixed-step RK4 and returns `(time, x)`.
pub fn integrate(f: impl Fn(f64) -> f64, x0: f64, dt: f64, steps: usize) -> (Vec<f64>, Vec<f64>) {
let mut time = Vec::with_capacity(steps + 1);
let mut xs = Vec::with_capacity(steps + 1);
let mut x = x0;
for step in 0..=steps {
time.push(step as f64 * dt);
xs.push(x);
let k1 = f(x);
let k2 = f(x + 0.5 * dt * k1);
let k3 = f(x + 0.5 * dt * k2);
let k4 = f(x + dt * k3);
x += dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
}
(time, xs)
}
/// Builds a single-state dataset named `x` from a trajectory.
pub fn dataset_x(time: Vec<f64>, xs: Vec<f64>) -> Dataset {
let x = Identifier::new("x").unwrap();
Dataset::new(TimeAxis::new(time).unwrap(), [NumericColumn::new(x, xs)]).unwrap()
}
/// Michaelis-Menten decay `ẋ = -Vmax·x / (Km + x)`.
pub fn michaelis_menten(vmax: f64, km: f64) -> impl Fn(f64) -> f64 {
move |x: f64| -vmax * x / (km + x)
}
/// Linear decay `ẋ = -k·x`.
pub fn linear_decay(k: f64) -> impl Fn(f64) -> f64 {
move |x: f64| -k * x
}
/// Looks up a numerator/denominator coefficient by term name, defaulting to 0.
pub fn coefficient(terms: &[lawsynth_implicit::MonomialTerm], name: &str) -> f64 {
terms.iter().find(|term| term.name == name).map(|term| term.coefficient).unwrap_or(0.0)
}