commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From billbar...@apache.org
Subject svn commit: r890159 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear: AbstractRealVector.java OpenMapRealVector.java
Date Mon, 14 Dec 2009 02:53:41 GMT
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 {



Mime
View raw message