From 9b4adf4df94f8cfaf66d389cb2617c7036959fdc Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sat, 18 Apr 2009 15:17:12 +0000 Subject: [PATCH] fixed an error in SparseRealVector.isInfinite, NaN was not checked beforehand fixed an error in SparseRealVector.hashcode, code did not depend on vector entries fixed tests accordingly git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@766337 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/linear/SparseRealVector.java | 18 +++++++++++++++--- .../math/linear/SparseRealVectorTest.java | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/commons/math/linear/SparseRealVector.java b/src/java/org/apache/commons/math/linear/SparseRealVector.java index 6204daf77..7e8e1681f 100644 --- a/src/java/org/apache/commons/math/linear/SparseRealVector.java +++ b/src/java/org/apache/commons/math/linear/SparseRealVector.java @@ -593,14 +593,20 @@ public class SparseRealVector implements RealVector { /** {@inheritDoc} */ public boolean isInfinite() { + boolean infiniteFound = false; + boolean nanFound = false; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); - if (Double.isInfinite(iter.value())) { - return true; + final double value = iter.value(); + if (Double.isNaN(value)) { + nanFound = true; + } + if (Double.isInfinite(value)) { + infiniteFound = true; } } - return false; + return infiniteFound && (!nanFound); } /** {@inheritDoc} */ @@ -1228,6 +1234,12 @@ public class SparseRealVector implements RealVector { temp = Double.doubleToLongBits(epsilon); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + virtualSize; + Iterator iter = entries.iterator(); + while (iter.hasNext()) { + iter.advance(); + temp = Double.doubleToLongBits(iter.value()); + result = prime * result + (int) (temp ^ (temp >>> 32)); + } return result; } diff --git a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java index c83197ac0..f7d1ee8e8 100644 --- a/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java +++ b/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java @@ -1082,7 +1082,7 @@ public class SparseRealVectorTest extends TestCase { assertFalse(v.isInfinite()); v.setEntry(0, Double.POSITIVE_INFINITY); - assertFalse(v.isInfinite()); // NaN is checked before infinity + assertFalse(v.isInfinite()); // NaN has higher priority than infinity v.setEntry(1, 1); assertTrue(v.isInfinite()); @@ -1091,7 +1091,7 @@ public class SparseRealVectorTest extends TestCase { assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)})); assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 })); - assertEquals(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(), + assertTrue(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() != new SparseRealVector(new double[] { 0, Double.NaN, 2 }).hashCode()); assertTrue(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() !=