Skip to content

ltm: graphical-function monotonicity compares the y-delta, not the slope dy/dx (non-uniform x-spacing) #536

Closed

Description

@bpowers

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 (the for 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

Severity

low

Activity

  1. added
    engine
    ltm
    on May 13, 2026
  2. added 2 commits that reference this issue on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineltm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions