caelum0x 0c49b23900 feat: product surface + analysis layer — CLI wiring, bifurcation, feedback design, coefficient uncertainty
Batch 6 of expansion-v2. Makes the new engine reachable from the CLI and adds
the dynamics analysis layer on top of it. Deterministic/offline/std-only.

lawsynth-cli: exposes the just-built engine crates as real, tested commands.
  - `lawsynth stability WORLD --box LOW:HIGH` — fixed points + eigenvalues +
    classification (honest marginal/no-root/seed accounting).
  - `lawsynth control OBS.csv --state.. --control.. [--validate]` — SINDYc
    controlled discovery + in-sample R²/RMSE (dedicated subcommand so
    discover's .lsworld contract stays clean).
  - `lawsynth domains [show|run NAME]` — list/inspect/round-trip presets.
  - `discover` template-prior flags (--max-degree/--allow-vars/--allow-kinds/
    --forbid-interactions/--max-active/--require-kind) with honest drop report.
  - `simplify` upgraded to the egraph simplify_law pass.
  101 unit + 8 new end-to-end integration tests.

crates/lawsynth-bifurcation (new): parameter continuation + bifurcation
detection on a discovered field f(x;μ). Deterministic grid sweep, proximity
branch matching, eigenvalue-crossing detection with bisection localization.
Detects saddle-node/transcritical/pitchfork (generic Fold) and Hopf at μ*≈0 on
the textbook normal forms. 32 tests.

crates/lawsynth-feedback (new): linear feedback design from a discovered
linearization (A,B). Ackermann SISO pole placement (double integrator → exact
K, poles matched <1e-10) and LQR via Kleinman/Bass Riccati iteration
(analytic K=[1,√3], CARE residual ~4e-16). Closed-loop poles verified through
koopman::eigen. 28 tests.

crates/lawsynth-uncertainty: deterministic bootstrap for discovered
coefficients — per-term CIs + inclusion probability. True terms hit inclusion
1.0 with covering CIs, spurious 0.0; CI width shrinks ~4x with n. Reuses the
crate's SplitMix64, seeded per replicate by (seed,b). 24 tests.

specs/{bifurcation-analysis,control-design,coefficient-uncertainty}: boundary
specs with honest limits (grid-bounded continuation, SISO/continuous-time
feedback, approximate bootstrap CIs under time correlation).
2026-08-18 16:02:35 +03:00

59 lines
2.1 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Shared helpers for the feedback integration tests.
//!
//! Each integration binary pulls in the whole module but uses a different
//! subset, so unused-helper warnings are expected and silenced here.
#![allow(dead_code)]
use lawsynth_feedback::{Complex, Matrix};
/// Builds a matrix from row-major data, panicking on malformed input.
pub fn matrix(rows: Vec<Vec<f64>>) -> Matrix {
Matrix::from_rows(rows).expect("valid matrix")
}
/// Sorts complex numbers into a canonical (re, then im) order for comparison.
pub fn sorted(mut values: Vec<Complex>) -> Vec<Complex> {
values.sort_by(|a, b| a.re.total_cmp(&b.re).then_with(|| a.im.total_cmp(&b.im)));
values
}
/// Asserts two pole multisets match to `tolerance`, order-independent.
///
/// Each expected pole is matched greedily to the nearest unused actual pole,
/// which is robust to tiny numerical reordering of near-equal poles (where a
/// sort-then-zip would break on the tie-break coordinate).
pub fn assert_poles_match(actual: &[Complex], expected: &[Complex], tolerance: f64) {
assert_eq!(actual.len(), expected.len(), "pole count differs");
let mut used = vec![false; actual.len()];
for want in expected {
let mut best: Option<(usize, f64)> = None;
for (index, got) in actual.iter().enumerate() {
if used[index] {
continue;
}
let distance = (got.re - want.re).hypot(got.im - want.im);
if best.is_none_or(|(_, d)| distance < d) {
best = Some((index, distance));
}
}
let (index, distance) = best.expect("an unused actual pole");
assert!(
distance < tolerance,
"pole mismatch: want {want}, nearest {} at distance {distance}",
actual[index]
);
used[index] = true;
}
}
/// The largest absolute entry of `a − b` for equally shaped matrices.
pub fn max_abs_diff(a: &Matrix, b: &Matrix) -> f64 {
let mut best = 0.0_f64;
for i in 0..a.rows() {
for j in 0..a.cols() {
best = best.max((a.get(i, j) - b.get(i, j)).abs());
}
}
best
}