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); + } }