From 01e5f22cc54c1fb8667545142e23e57032b3b4b3 Mon Sep 17 00:00:00 2001 From: William Barker Date: Mon, 14 Dec 2009 02:53:40 +0000 Subject: [PATCH] Add an optimized dotProduct to OpenMapRealVector. Slightly modified from contribution by Jake Mannix. JIRA: MATH-317 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@890159 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/AbstractRealVector.java | 12 ++------ .../math/linear/OpenMapRealVector.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java b/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java index 51d4440c6..85eb1e0a2 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java @@ -196,7 +196,7 @@ public abstract class AbstractRealVector implements RealVector { public double getDistance(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v); double d = 0; - Iterator it = sparseIterator(); + Iterator it = iterator(); Entry e; while (it.hasNext() && (e = it.next()) != null) { final double diff = e.getValue() - v.getEntry(e.getIndex()); @@ -207,15 +207,7 @@ public abstract class AbstractRealVector implements RealVector { /** {@inheritDoc} */ public double getDistance(double[] v) throws IllegalArgumentException { - checkVectorDimensions(v.length); - double d = 0; - Iterator it = iterator(); - Entry e; - while (it.hasNext() && (e = it.next()) != null) { - final double diff = e.getValue() - v[e.getIndex()]; - d += diff * diff; - } - return Math.sqrt(d); + return getDistance(new ArrayRealVector(v,false)); } /** {@inheritDoc} */ 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 2a9db4ba2..ee06fb4fb 100644 --- a/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java +++ b/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java @@ -303,6 +303,34 @@ public class OpenMapRealVector extends AbstractRealVector implements SparseRealV return new OpenMapRealVector(this); } + /** + * Optimized method to compute the dot product with an OpenMapRealVector. + * Iterates over the smaller of the two. + * @param v The vector to compute the dot product with + * @return The dot product of this and v + * @throws IllegalArgumentException If the dimensions don't match + */ + public double dotProduct(OpenMapRealVector v) throws IllegalArgumentException { + 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} */ + public double dotProduct(RealVector v) throws IllegalArgumentException { + if(v instanceof OpenMapRealVector) { + return dotProduct((OpenMapRealVector)v); + } else { + return super.dotProduct(v); + } + } /** {@inheritDoc} */ public OpenMapRealVector ebeDivide(RealVector v) throws IllegalArgumentException {