From 6983d81680483705c0e2cf4d62b15feba4e6e3ab Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sat, 13 Aug 2011 18:10:13 +0000 Subject: [PATCH] MATH-645 Iterate on the original vector, not on the copy that is modified. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1157403 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/OpenMapRealVector.java | 8 +++--- .../math/linear/SparseRealVectorTest.java | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java b/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java index 5db488466..13ebfd24d 100644 --- a/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java +++ b/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java @@ -342,7 +342,7 @@ public class OpenMapRealVector extends AbstractRealVector public OpenMapRealVector ebeDivide(RealVector v) { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); - Iterator iter = res.entries.iterator(); + Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key())); @@ -355,7 +355,7 @@ public class OpenMapRealVector extends AbstractRealVector public OpenMapRealVector ebeDivide(double[] v) { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); - Iterator iter = res.entries.iterator(); + Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() / v[iter.key()]); @@ -367,7 +367,7 @@ public class OpenMapRealVector extends AbstractRealVector public OpenMapRealVector ebeMultiply(RealVector v) { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); - Iterator iter = res.entries.iterator(); + Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key())); @@ -380,7 +380,7 @@ public class OpenMapRealVector extends AbstractRealVector public OpenMapRealVector ebeMultiply(double[] v) { checkVectorDimensions(v.length); OpenMapRealVector res = new OpenMapRealVector(this); - Iterator iter = res.entries.iterator(); + Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() * v[iter.key()]); diff --git a/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java b/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java index e23bfa01c..8ab628c68 100644 --- a/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java +++ b/src/test/java/org/apache/commons/math/linear/SparseRealVectorTest.java @@ -998,4 +998,30 @@ public class SparseRealVectorTest { Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance); } } + + /* Check that the operations do not throw an exception (cf. MATH-645). */ + @Test + public void testConcurrentModification() { + final RealVector u = new OpenMapRealVector(3, 1e-6); + u.setEntry(0, 1); + u.setEntry(1, 0); + u.setEntry(2, 2); + + final RealVector v1 = new OpenMapRealVector(3, 1e-6); + final double[] v2 = new double[3]; + v1.setEntry(0, 0); + v2[0] = 0; + v1.setEntry(1, 3); + v2[1] = 3; + v1.setEntry(2, 0); + v2[2] = 0; + + RealVector w; + + w = u.ebeMultiply(v1); + w = u.ebeMultiply(v2); + + w = u.ebeDivide(v1); + w = u.ebeDivide(v2); + } }