Author: billbarker
Date: Mon Dec 14 02:53:40 2009
New Revision: 890159

URL: http://svn.apache.org/viewvc?rev=890159&view=rev
Log:
Add an optimized dotProduct to OpenMapRealVector.
Slightly modified from contribution by Jake Mannix.
JIRA: MATH-317

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java?rev=890159&r1=890158&r2=890159&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
 Mon Dec 14 02:53:40 2009
@@ -196,7 +196,7 @@
     public double getDistance(RealVector v) throws IllegalArgumentException {
         checkVectorDimensions(v);
         double d = 0;
-        Iterator<Entry> it = sparseIterator();
+        Iterator<Entry> it = iterator();
         Entry e;
         while (it.hasNext() && (e = it.next()) != null) {
             final double diff = e.getValue() - v.getEntry(e.getIndex());
@@ -207,15 +207,7 @@
 
     /** {...@inheritdoc} */
     public double getDistance(double[] v) throws IllegalArgumentException {
-        checkVectorDimensions(v.length);
-        double d = 0;
-        Iterator<Entry> 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} */

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java?rev=890159&r1=890158&r2=890159&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
 Mon Dec 14 02:53:40 2009
@@ -303,6 +303,34 @@
         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 <code>this</code> and <code>v</code>
+     * @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 {


Reply via email to