Skip to content

OpenMapRealVector: fix missing abs in getLInfDistance(OpenMapRealVector) - #361

Open
simpleqt wants to merge 1 commit into
apache:masterfrom
simpleqt:fix/openmap-linf-distance-negative
Open

simpleqt wants to merge 1 commit into
apache:masterfrom
simpleqt:fix/openmap-linf-distance-negative

Conversation

@simpleqt

Copy link
Copy Markdown
Contributor

OpenMapRealVector.getLInfDistance(OpenMapRealVector) takes abs(this[i] - v[i]) for the keys stored in this, but for keys stored only in v it compares the raw v[i] — although this[i] is the default 0 there. A negative value can never raise the maximum, so two vectors that differ only by a negative entry of v report an L-inf distance of 0:

new OpenMapRealVector(new double[] {0d, 0d})
    .getLInfDistance(new OpenMapRealVector(new double[] {0d, -5d})) // 0.0, expected 5.0

The same input against an ArrayRealVector returns 5.0 (the generic RealVector#getLInfDistance(RealVector) path), so the sparse overload also disagrees with the generic one depending on the argument type. getDistance squares the v-only entries and getL1Distance takes their absolute value; only the L-inf variant misses it.

Fix: take the absolute value of the entries found only in v before comparing. Regression test added to RealVectorAbstractTest (runs for the Array / generic / sparse fixtures; fails on the unfixed sparse implementation with expected:<5.0> but was:<0.0>).

Local checks: mvn -pl commons-math-legacy test — 3268 tests, 0 failures (30 skipped, 3 flaky rerun-passes in SimplexOptimizer [POWELL], pre-existing); checkstyle:check — 0 violations. SpotBugs/pmd not run locally.

The optimized sparse L-inf distance took abs(this[i] - v[i]) for the keys
stored in this, but for keys stored only in v it compared and assigned the
raw value v[i] without the absolute value, even though this[i] is the
default 0 there. A negative value therefore never raised the maximum: two
vectors that differ only by a negative entry of v reported an L-inf
distance of 0, and the sparse overload disagreed with the generic
RealVector#getLInfDistance(RealVector) on identical inputs (5 vs 0).
getDistance squares the values and getL1Distance takes their absolute
value, so only the L-inf variant was affected.

Take the absolute value of the entries found only in v before comparing.
Regression test added to RealVectorAbstractTest so all three fixtures
(Array, generic RealVector, sparse) run it; it fails on the unfixed sparse
implementation with expected 5.0 but was 0.0.
Assisted-By: DeepSeek-V4.1 (deepseek-ai) via DeepSeek Harness

Assisted-By: DeepSeek-V4.1 (deepseek-ai) via DeepSeek Harness
Signed-off-by: simpleqt <2015900818@qq.com>
Copilot AI balanced review requested due to automatic review settings November 22, 2026 10:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants