Repository navigation
Description
Summary
analyze_graphical_function_polarity in src/simlin-engine/src/ltm/polarity.rs (the for i in 1..table.y.len() loop, currently ~lines 630-674) classifies a lookup table's monotonicity by comparing the y-delta dy = table.y[i] - table.y[i-1] against an epsilon -- not the slope dy/dx = (table.y[i] - table.y[i-1]) / (table.x[i] - table.x[i-1]).
#492 (commit ee07c935, branch ltm-arrays-hardening, LTM-arrays-hardening Phase 7 Task 1) made the epsilon y-range-relative -- epsilon = (1e-6 * (y_max - y_min)).max(1e-12) -- which is the right fix for round-trip numeric-import noise. But that change deliberately did not address the deeper issue, and the new code comment in analyze_graphical_function_polarity says so explicitly:
// y-range-relative tolerance so tables that are monotone modulo round-trip numeric-import noise keep their polarity (#492); the non-uniform-x-spacing concern --dyvs slopedy/dx-- is out of scope, tracked separately.
The design plan (docs/implementation-plans/2026-05-11-ltm-arrays-hardening/phase_07.md, Task 1, and the AC8.1 "done when" item) calls for filing this as a separate item.
The problem
For a table with non-uniform x-spacing, a small dy over a small dx is a large slope but reads as a "plateau" (within ±epsilon). So:
- a genuinely fast-changing segment over a narrow x-interval can be misclassified as a plateau, and
- conversely, a slowly-changing segment over a wide x-interval can be flagged as a real change when it is effectively flat.
Either way the table's classification (Positive / Negative / Unknown monotonicity) can be wrong, which propagates through analyze_link_polarity -> the link's static polarity -> loop classification (an Undetermined or wrong-sign loop where a correct classification was derivable, or vice versa).
Why it matters (and why it's low priority)
- Low severity. Most real GF tables in practice have roughly-uniform x-spacing, so the misclassification is occasional. The consequence is a heuristic getting a link polarity wrong -- never a crash, never a wrong simulation result. Static link polarity is already a best-effort heuristic that the runtime LTM scores supersede.
- It is, however, silently wrong when it happens -- worth a precise fix when the polarity code is next touched.
Component(s) affected
src/simlin-engine/src/ltm/polarity.rs--analyze_graphical_function_polarity(thefor i in 1..table.y.len()loop).src/simlin-engine/src/variable.rs--Table(x: Vec<f64>,y: Vec<f64>); the x-coordinates are right there alongside the y-coordinates, the loop just doesn't use them.
Suggested fix
Compare the per-segment slope (table.y[i] - table.y[i-1]) / (table.x[i] - table.x[i-1]) against a slope-appropriate tolerance instead of comparing the raw dy against a y-range-relative epsilon. Guard the table.x[i] == table.x[i-1] case (a vertical segment -- degenerate input; treat it as a non-determining segment, or bail to Unknown). Pick the slope tolerance to remain noise-tolerant the way #492's y-range-relative epsilon is (e.g. scale by (y_max - y_min) / (x_max - x_min), the table's average-magnitude slope, with a small absolute floor). The < 2 early-return and the all_increasing/all_decreasing/all_constant bookkeeping stay as they are.
Tests: a monotone table with deliberately non-uniform x-spacing where the y-delta heuristic and the slope heuristic disagree; the existing test_graphical_function_polarity cases (uniform spacing) must stay green.
Context
Identified during the LTM-arrays-hardening Phase 7 work (#492 / commit ee07c935); explicitly deferred there per docs/implementation-plans/2026-05-11-ltm-arrays-hardening/phase_07.md Task 1.
References
- ltm: GF strict-monotonicity check (EPSILON=1e-10) flags numeric-import noise as Unknown #492 -- the y-range-relative-epsilon fix that this builds on (and that explicitly scoped this out)
- LTM tracking epic: epic: Loops that Matter (LTM) -- open work #488
- Related (different code site): ltm: support per-element graphical functions in static link polarity analysis #502 -- per-element graphical-function static link polarity
- Tech-debt note:
docs/tech-debt.mdfix new user creation flow #21 (mentions GF monotonicity epsilon, magnitude angle only)
Severity
low
Activity
engine: classify GF monotonicity by slope, not y-delta
engine: fix #536 slope tolerance regression on finely-sampled tables