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

84 lines
2.9 KiB
Rust

//! Shared fixtures: textbook parameterized vector fields with known bifurcations.
use lawsynth_core::Identifier;
use lawsynth_expr::{BinaryOperator, Expr};
/// Convenience: an identifier that is known to be valid.
pub fn id(name: &str) -> Identifier {
Identifier::new(name).unwrap()
}
/// `x^n` with a constant exponent.
fn power(base: Expr, exponent: f64) -> Expr {
Expr::binary(BinaryOperator::Power, base, Expr::constant(exponent))
}
/// Saddle-node normal form: `x' = mu - x^2`.
///
/// No real fixed point for `mu < 0`; two (`x = ±√mu`) for `mu > 0`; they collide
/// at `mu = 0`.
pub fn saddle_node() -> (Vec<(Identifier, Expr)>, Vec<Identifier>, Identifier) {
let x = id("x");
let mu = id("mu");
let field = Expr::difference(Expr::symbol(mu.clone()), power(Expr::symbol(x.clone()), 2.0));
(vec![(x.clone(), field)], vec![x], mu)
}
/// Transcritical normal form: `x' = mu*x - x^2`.
///
/// Fixed points `x = 0` and `x = mu` for all `mu`; they exchange stability at
/// `mu = 0`.
pub fn transcritical() -> (Vec<(Identifier, Expr)>, Vec<Identifier>, Identifier) {
let x = id("x");
let mu = id("mu");
let field = Expr::difference(
Expr::product(Expr::symbol(mu.clone()), Expr::symbol(x.clone())),
power(Expr::symbol(x.clone()), 2.0),
);
(vec![(x.clone(), field)], vec![x], mu)
}
/// Supercritical pitchfork normal form: `x' = mu*x - x^3`.
///
/// One fixed point (`x = 0`) for `mu < 0`; three (`x = 0, ±√mu`) for `mu > 0`.
pub fn pitchfork() -> (Vec<(Identifier, Expr)>, Vec<Identifier>, Identifier) {
let x = id("x");
let mu = id("mu");
let field = Expr::difference(
Expr::product(Expr::symbol(mu.clone()), Expr::symbol(x.clone())),
power(Expr::symbol(x.clone()), 3.0),
);
(vec![(x.clone(), field)], vec![x], mu)
}
/// Hopf normal form (2D):
/// `x' = mu*x - y - x*(x^2 + y^2)`, `y' = x + mu*y - y*(x^2 + y^2)`.
///
/// The origin is a fixed point for all `mu`, with eigenvalues `mu ± i`: a
/// complex pair crosses the imaginary axis at `mu = 0`.
pub fn hopf() -> (Vec<(Identifier, Expr)>, Vec<Identifier>, Identifier) {
let x = id("x");
let y = id("y");
let mu = id("mu");
// r^2 = x^2 + y^2
let radius_sq =
Expr::sum(power(Expr::symbol(x.clone()), 2.0), power(Expr::symbol(y.clone()), 2.0));
// x' = mu*x - y - x*r^2
let dx = Expr::difference(
Expr::difference(
Expr::product(Expr::symbol(mu.clone()), Expr::symbol(x.clone())),
Expr::symbol(y.clone()),
),
Expr::product(Expr::symbol(x.clone()), radius_sq.clone()),
);
// y' = x + mu*y - y*r^2
let dy = Expr::difference(
Expr::sum(
Expr::symbol(x.clone()),
Expr::product(Expr::symbol(mu.clone()), Expr::symbol(y.clone())),
),
Expr::product(Expr::symbol(y.clone()), radius_sq),
);
(vec![(x.clone(), dx), (y.clone(), dy)], vec![x, y], mu)
}