Author: celestin Date: Thu Jul 12 13:17:08 2012 New Revision: 1360662 URL: http://svn.apache.org/viewvc?rev=1360662&view=rev Log: MATH-812: fixed a bug in RealVector.dotProduct(RealVector).
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java?rev=1360662&r1=1360661&r2=1360662&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java Thu Jul 12 13:17:08 2012 @@ -455,16 +455,8 @@ public class ArrayRealVector extends Rea dot += data[i] * vData[i]; } return dot; - } else { - checkVectorDimensions(v); - double dot = 0; - Iterator<Entry> it = v.sparseIterator(); - while (it.hasNext()) { - final Entry e = it.next(); - dot += data[e.getIndex()] * e.getValue(); - } - return dot; } + return super.dotProduct(v); } /** {@inheritDoc} */ Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java?rev=1360662&r1=1360661&r2=1360662&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java Thu Jul 12 13:17:08 2012 @@ -304,38 +304,6 @@ public class OpenMapRealVector extends S return new OpenMapRealVector(this); } - /** - * Optimized method to compute the dot product with an OpenMapRealVector. - * It iterates over the smallest of the two. - * - * @param v Cector to compute the dot product with. - * @return the dot product of {@code this} and {@code v}. - * @throws org.apache.commons.math3.exception.DimensionMismatchException - * if the dimensions do not match. - */ - public double dotProduct(OpenMapRealVector v) { - checkVectorDimensions(v.getDimension()); - boolean thisIsSmaller = entries.size() < v.entries.size(); - Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator(); - OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries; - double d = 0; - while(iter.hasNext()) { - iter.advance(); - d += iter.value() * larger.get(iter.key()); - } - return d; - } - - /** {@inheritDoc} */ - @Override - public double dotProduct(RealVector v) { - if(v instanceof OpenMapRealVector) { - return dotProduct((OpenMapRealVector)v); - } else { - return super.dotProduct(v); - } - } - /** {@inheritDoc} */ @Override public OpenMapRealVector ebeDivide(RealVector v) { Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1360662&r1=1360661&r2=1360662&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Thu Jul 12 13:17:08 2012 @@ -303,10 +303,9 @@ public abstract class RealVector { public double dotProduct(RealVector v) { checkVectorDimensions(v); double d = 0; - Iterator<Entry> it = sparseIterator(); - while (it.hasNext()) { - final Entry e = it.next(); - d += e.getValue() * v.getEntry(e.getIndex()); + final int n = getDimension(); + for (int i = 0; i < n; i++) { + d += getEntry(i) * v.getEntry(i); } return d; }