caelum0x 220552d530 feat: SDE discovery + discrete-time control + Python/TS reach
Batch 10 of expansion-v2. Extends discovery into stochastic dynamics, adds
discrete-time control, and completes the Python/TS reach for the analysis
commands. Deterministic/offline/std-only (Rust) or typed/tested (Python/TS).

crates/lawsynth-sde (new): stochastic law discovery via Kramers-Moyal
conditional moments. Drift a(x)=E[ΔX|x]/Δt and diffusion b²(x)=E[ΔX²|x]/Δt
estimated by binning, occupancy-gated, then sparse-fit to a polynomial library.
Recovers OU (linear drift, constant diffusion), GBM (state-dependent quadratic
diffusion), and double-well (cubic drift) from seeded Euler-Maruyama paths.
Reports raw binned table + fitted laws; honest that it's a statistical
estimator (accuracy scales with path length / bin occupancy). Closes the loop
with the existing SDE simulator. 29 tests.

crates/lawsynth-discrete (new): discrete-time DLQR + Kalman/observer. DARE by
deterministic value iteration (reused on the dual for the filter); discrete
stability = spectral radius < 1, verified via koopman::eigen. DARE residuals
1e-12..5e-14, stabilizes an open-loop-unstable plant, matches the analytic
scalar P=2+√5 case. 34 tests.

python/lawsynth analysis.py: bifurcation/sensitivity/estimate/reduce client
methods over the CLI --json (single source of truth). test_analysis 36 passed;
full SDK suite 130 passed. Honest engine constraints surfaced (pole placement
needs observability; reduce needs a diagonalizable Hurwitz linearization).

packages/world-schema + api-client: typed models + parsers for the four new
analysis-command JSON shapes, verbatim-CLI fixtures, token strings confirmed
against source. api-client 24 tests.

specs/{sde-discovery,discrete-time-control}: boundary specs with honest limits.
2026-08-18 17:21:57 +03:00

44 lines
1.7 KiB
Rust

//! Bit-for-bit determinism of all three discrete designs.
mod common;
use common::{bits_equal, matrix, poles_bits_equal};
use lawsynth_discrete::{Complex, Matrix, discrete_kalman, discrete_observer_from_poles, dlqr};
#[test]
fn dlqr_is_bit_identical() {
let a = matrix(vec![vec![1.1, 0.2], vec![0.0, 1.05]]);
let b = matrix(vec![vec![1.0], vec![1.0]]);
let q = Matrix::identity(2);
let r = matrix(vec![vec![1.0]]);
let first = dlqr(&a, &b, &q, &r).unwrap();
let second = dlqr(&a, &b, &q, &r).unwrap();
assert!(bits_equal(&first.k, &second.k));
assert!(bits_equal(&first.p, &second.p));
assert!(poles_bits_equal(&first.achieved_poles, &second.achieved_poles));
}
#[test]
fn discrete_kalman_is_bit_identical() {
let a = matrix(vec![vec![0.9, 0.1], vec![0.0, 0.8]]);
let c = matrix(vec![vec![1.0, 0.0]]);
let q = Matrix::identity(2);
let r = matrix(vec![vec![0.1]]);
let first = discrete_kalman(&a, &c, &q, &r).unwrap();
let second = discrete_kalman(&a, &c, &q, &r).unwrap();
assert!(bits_equal(&first.l, &second.l));
assert!(bits_equal(first.p.as_ref().unwrap(), second.p.as_ref().unwrap()));
assert!(poles_bits_equal(&first.error_poles, &second.error_poles));
}
#[test]
fn observer_placement_is_bit_identical() {
let a = matrix(vec![vec![0.9, 0.1], vec![0.0, 0.8]]);
let c = matrix(vec![vec![1.0, 0.0]]);
let desired = [Complex::new(0.3, 0.2), Complex::new(0.3, -0.2)];
let first = discrete_observer_from_poles(&a, &c, &desired).unwrap();
let second = discrete_observer_from_poles(&a, &c, &desired).unwrap();
assert!(bits_equal(&first.l, &second.l));
assert!(poles_bits_equal(&first.error_poles, &second.error_poles));
}