Batch 12 of expansion-v2. Adds chaos/coupling/basin diagnostics on discovered
models and makes invariants + model-selection CLI-reachable. Deterministic/
offline/std-only.
crates/lawsynth-lyapunov (new): Lyapunov spectrum via the Benettin/QR method on
the variational flow (analytic Jacobian, shared RK4, periodic
reorthonormalization) + Kaplan-Yorke dimension. Lorenz largest ≈0.906 (chaos)
with the tight sum≈−13.667 divergence anchor; harmonic ≈0, damped sum=−0.3.
39 tests.
crates/lawsynth-network (new): coupling-graph discovery by per-node sparse
regression over an all-nodes library; adjacency read structurally from surviving
cross terms (spoof-proof). Recovers chain/ring/star topologies, guards against
false edges on decoupled nodes, honors an edge-strength threshold. Uses multiple
ICs for rank-deficient symmetric networks. Correlational-not-causal (points to
lawsynth-causal). 23 tests.
crates/lawsynth-basins (new): basin-of-attraction mapping — stable attractors
from lawsynth-stability, a deterministic IC grid integrated forward by RK4,
endpoints classified with honest Escaped/Undetermined outcomes + per-attractor
fractions. Bistable ẋ=x−x³ splits 0.5/0.5 (saddle boundary Undetermined),
Duffing two spirals, single global attractor → 1.0. Limit cycles out of scope.
32 tests.
crates/lawsynth-cli: new `invariants` (renders conserved quantities like
1.00·x^2 + 1.00·y^2) and `select` (CV audit table + winner) commands. 123 lib +
4 new integration tests.
specs/{lyapunov-exponents,network-discovery,basin-mapping}: boundary specs.
9.6 KiB
Basin-of-attraction mapping boundary (v2-A)
This directory specifies deterministic basin-of-attraction mapping — the
global question of which initial conditions flow to which stable attractor of a
discovered vector field, implemented in crates/lawsynth-basins. It is a
boundary specification in the house style: it states what a conforming
implementation MUST do, and — crucially — what a basin map is and is not allowed
to claim.
Motivation
Stability analysis finds a discovered law's fixed points and classifies each locally (stable node/spiral, saddle, …). Bifurcation analysis tracks how that local picture changes as a parameter moves. Neither answers the global question: given several coexisting stable attractors, from where does the system reach each one?
That question is the basin of attraction — the set of initial conditions whose forward trajectory converges to a given attractor. Basins partition state space (up to the boundary sets between them) and are what decides the long-term fate of a system started from a particular state. A bistable switch, a double-well memory, a species that survives or collapses depending on its starting population — all are basin questions.
LawSynth is deterministic and offline. This stage reuses the fixed-point
and linear-stability analysis of crates/lawsynth-stability to find the
attractors, and the crates/lawsynth-expr IR to evaluate the field; the only
local numerics are a fixed-step RK4 forward flow. Identical inputs MUST yield
bit-identical output.
What a basin map IS
A basin map is a finite, deterministic sampling of the flow's long-term fate, read off a fixed grid of initial conditions. The contract is:
- Attractors are the stable fixed points, found not proven exhaustive. The
attractor set is exactly the STABLE fixed points located by
analyze_stabilityinside the search box — stable nodes and stable spirals. Saddles, unstable points, and non-hyperbolic (Center/Marginal) points are not attractors and are never mapped. If no stable fixed point is found, the report is honestly empty rather than inventing a basin. - Labels are the flow's verdict, not a nearest-attractor guess. Each initial
condition is integrated forward in time; it is labelled with an attractor
only if its trajectory actually comes within a tolerance of that attractor. A
trajectory that leaves the box or diverges is
Escaped; one that does neither withinmax_timeisUndetermined. Classification is never forced. - A property of the field, not of any data. The mapping integrates whatever field it is handed; it carries no discovery confidence or fit residual. Those are separate, upstream concerns.
- Honest about the sampling. The report records the grid resolution, the box,
and the per-attractor settled fractions alongside the
escapedandundeterminedcounts, so a lopsided or empty result reads as "this is what the grid at this resolution found", not "this is the exact basin geometry".
Requirements
-
Attractors from stability. The attractor set MUST be obtained by delegating to
lawsynth_stability::analyze_stabilityover the configuredStabilityConfig, then retaining exactly the fixed points classifiedStableNodeorStableSpiral, in the order stability reports them (lexicographic by coordinate). The attractor index used in labels is the position in that filtered, ordered list. -
Deterministic initial-condition grid. Initial conditions MUST be a fixed, content-independent even lattice over the search box:
resolutionsamples per axis, enumerated as a Cartesian product in a fixed row-major order (first axis varies slowest). The reportedgrid_labelsMUST follow exactly that order. Unlike the Newton seed set, the origin is NOT specially appended — the grid is precisely the lattice. -
Fixed-step RK4 forward flow. Each initial condition MUST be integrated with the classical fourth-order Runge–Kutta method at a fixed step
dt, evaluating the field with thelawsynth-exprevaluator. The number of steps isceil(max_time / dt). The arithmetic MUST be performed in a fixed order so the flow is bit-reproducible. -
Endpoint classification with honest outcomes. A trajectory MUST be labelled:
Attractor(i)if at any checked step it comes withinconvergence_tolerance(measured in the‖·‖∞/ Chebyshev metric) of attractori; the nearest attractor wins, ties resolve to the lowest index;Escapedif it leaves the search box padded byescape_marginon every axis, exceedsdivergence_limitin magnitude, becomes non-finite, or the field is undefined along the step (e.g.logof a non-positive argument);Undeterminedif neither happens withinmax_time.
An implementation MUST NOT coerce an
EscapedorUndeterminedtrajectory into a basin. -
Fractions over the settled population. The per-attractor
fractionsMUST becount(attractor i) / settled_total, wheresettled_totalis the number of trajectories labelled with some attractor (escaped and undetermined excluded). If nothing settled, every fraction MUST be0.0. Theescapedandundeterminedcounts MUST be reported separately. -
Determinism. Attractor detection, grid generation, RK4 integration, and classification MUST be deterministic. Identical
(fields, states, BasinConfig)inputs MUST produce a bit-identicalBasinReport: identical attractor coordinates (tof64bit patterns), identical fractions (to bit patterns), and identical labels. -
Autonomy and totality. The field MUST be autonomous: every symbol it references MUST be one of the states, otherwise there is no value to integrate at and the implementation MUST return a typed error (surfaced through the stability layer as
UnknownSymbol). Structural faults (dimension mismatch between states and box, inverted search interval, out-of-range scalar knob, empty state space) MUST surface as distinct typed errors — never a silently dropped or fabricated result.
Public API
map_basins(&[(Identifier, Expr)], &[Identifier], &BasinConfig)
-> Result<BasinReport, BasinError>
BasinConfig::new(search_box) -> Self // + with_* builder setters
BasinReport {
states, attractors, grid_labels, fractions,
escaped, undetermined, resolution, search_box,
}
Attractor { coordinates, classification }
Label = Attractor(usize) | Escaped | Undetermined
BasinReport::to_canonical_string() -> String // determinism fingerprint
BasinConfig carries the search box (for both the IC grid and the escape
region), the grid resolution, the RK4 dt, the max_time, the
convergence_tolerance, the escape_margin and divergence_limit, and the
StabilityConfig used to find the attractors. This crate delivers the basin
mapping library only; visualising, refining boundaries, or wiring it into a
discovery report is downstream and out of scope here.
Honest scope & limits
- Only STABLE fixed-point attractors are recognized. This is the most
important limit. A limit cycle or a strange (chaotic) attractor is a
perfectly real attractor, but it is not a fixed point, so
analyze_stabilitydoes not find it and this crate does not classify its basin. Initial conditions that converge to such an attractor stay bounded without approaching any listed fixed point, so they read asUndetermined(orEscapedif they wander past the box). The report is honest about this — it never labels those trajectories with a fixed-point basin — but it does not map the true attractor. Deciding those basins needs periodic-orbit or Poincaré-section machinery this crate does not perform. - Resolution bounds boundary accuracy. Basin boundaries (the stable manifolds
of the intervening saddles) can be intricate, even fractal for forced or
chaotic systems. A finite grid can only resolve them to its spacing; a point
near a boundary may fall on either side. A finer grid resolves more, at
resolution^ncost. Nothing here proves a basin's exact geometry. - A fixed
max_timemay under-settle slow systems. A trajectory that is still drifting toward an attractor when the clock runs out is reportedUndetermined, not mislabelled. Slowly-converging (e.g. weakly-damped spiral) systems need a largermax_timeand/or a looserconvergence_tolerance; the implementation exposes both and MUST NOT silently extend the horizon to force a verdict. - Fixed-step RK4 carries truncation error. The flow is a fourth-order
approximation at step
dt; stiff or fast fields need a smallerdt. The approximation is deterministic but not exact, and the crate performs no adaptive step control or error estimation. - The map is box-bounded. The IC grid, the escape test, and the attractor
search all live inside the search box. Attractors outside the box are not found,
and a trajectory that exits the box is
Escapedeven if it would have converged to something further out. Widening the box sees more, at more cost. - Supported functions are exactly the IR's (
+ − × ÷,^, negate,exp,log,sin,cos), inherited fromlawsynth-expr; a field outside that set cannot be integrated.
Non-goals
- No limit-cycle, periodic-orbit, or strange-attractor detection or basin mapping; only STABLE fixed-point attractors are recognized.
- No adaptive-step or stiff integration, no trajectory export, no boundary refinement or manifold tracing.
- No global proof of basin geometry, measure, or completeness — the map is a finite deterministic sample, not a theorem.