Batch 3 of the expansion-v2 roadmap, all deterministic/offline/std-only with honest capability boundaries. crates/lawsynth-reduce (new): deterministic symmetry & separability detection. Numerical-derivative probe (finite differences replace AI-Feynman's trained NN gradients) over a reconstructed Cartesian grid; additive/multiplicative separability + difference/sum/product/ratio symmetries. Every detection is a HYPOTHESIS verified by reconstruction residual, never a proof; non-grid data is honestly rejected (NotReconstructed). 17 tests. crates/lawsynth-score: two-part MDL objective (L_data + L_model in nats) and O(n log n) pareto_front_2d (sort-then-sweep), verified equal to the O(n^2) front across 12 deterministic sets. +5 tests; crate fmt-normalized to config. python/lawsynth export.py (new): to_sympy/to_torch/to_jax + dep-free numeric_derivatives anchor. Constants+params hoisted into one trainable vector so autograd reaches every coefficient (Neural-ODE vector field). Backends absent here degrade gracefully (MissingDependencyError subclasses ImportError). Faithful to native simulate at 1.1e-16. 10 tests + 6 honest skips. benchmarks: SRBench Feynman (15: 12 static algebraic relations as honest capability-boundaries, 3 dynamics-framed ODEs executed at R2~=1.0) and black-box (4 systems, predictive R2 + parsimony, no ground truth) families. Fixes a pre-existing _runner.py failure where Strogatz cases leaked into the legacy contract suite. 31 benchmark tests. specs/structural-reductions: boundary spec (detection-is-a-hypothesis contract).
54 lines
1.9 KiB
Rust
54 lines
1.9 KiB
Rust
//! Symmetry demos: a translational (x − y) target is detected as a Difference
|
||
//! symmetry; a product target is detected as a Product symmetry.
|
||
|
||
mod common;
|
||
|
||
use common::{axis, grid_dataset_2d};
|
||
use lawsynth_reduce::{ReduceConfig, SymmetryKind, detect_reductions};
|
||
|
||
#[test]
|
||
fn detects_translational_difference_symmetry() {
|
||
// f = (x - y)^2 depends only on (x - y).
|
||
let xs = axis(0.0, 0.2, 12);
|
||
let ys = axis(0.0, 0.2, 12);
|
||
let dataset = grid_dataset_2d(&xs, &ys, |x, y| (x - y) * (x - y));
|
||
|
||
let report = detect_reductions(&dataset, &ReduceConfig::with_target("f")).unwrap();
|
||
assert!(report.grid.is_reconstructed());
|
||
|
||
let diff = report
|
||
.symmetries
|
||
.iter()
|
||
.find(|s| s.kind == SymmetryKind::Difference)
|
||
.expect("difference symmetry should be detected");
|
||
assert_eq!(diff.variables, ("x".to_string(), "y".to_string()));
|
||
assert!(diff.residual < 1e-9, "difference residual too high: {}", diff.residual);
|
||
|
||
// The other symmetries must NOT fire for (x - y)^2.
|
||
assert!(
|
||
!report.symmetries.iter().any(|s| matches!(
|
||
s.kind,
|
||
SymmetryKind::Sum | SymmetryKind::Product | SymmetryKind::Ratio
|
||
)),
|
||
"only the difference symmetry should be reported"
|
||
);
|
||
// (x - y)^2 is not additively separable (mixed partial = -2).
|
||
assert!(report.separabilities.is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn detects_product_symmetry() {
|
||
// f = (x * y)^2 depends only on (x * y): x*f_x - y*f_y = 0 exactly.
|
||
let xs = axis(0.5, 0.2, 12);
|
||
let ys = axis(0.5, 0.2, 12);
|
||
let dataset = grid_dataset_2d(&xs, &ys, |x, y| (x * y) * (x * y));
|
||
|
||
let report = detect_reductions(&dataset, &ReduceConfig::with_target("f")).unwrap();
|
||
let product = report
|
||
.symmetries
|
||
.iter()
|
||
.find(|s| s.kind == SymmetryKind::Product)
|
||
.expect("product symmetry should be detected");
|
||
assert!(product.residual < 1e-9, "product residual too high: {}", product.residual);
|
||
}
|