commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From billbar...@apache.org
Subject svn commit: r744614 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/linear/SparseRealVector.java java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java test/org/apache/commons/math/linear/SparseRealVectorTest.java
Date Sun, 15 Feb 2009 06:46:15 GMT
Author: billbarker
Date: Sun Feb 15 06:46:15 2009
New Revision: 744614

URL: http://svn.apache.org/viewvc?rev=744614&view=rev
Log:
Add working equals to SparseRealVector.

hashCode implementation is minimal, but I can't see a use case for using a SparseRealVector
as a key.

Remove unused imports from OpenInToDoubleHashMap.

Some cleanup for optimized methods, and javadoc fixes.

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java?rev=744614&r1=744613&r2=744614&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
Sun Feb 15 06:46:15 2009
@@ -21,7 +21,7 @@
 import org.apache.commons.math.util.OpenIntToDoubleHashMap.Iterator;
 
 /**
- * This class implements the {@link RealVector} interface with a {@link OpenIntToDoubleHashMap}.
+ * This class implements the {@link RealVector} interface with a {@link OpenIntToDoubleHashMap}
backing store.
  * @version $Revision: 728186 $ $Date$
  * @since 2.0
 */
@@ -230,23 +230,18 @@
      * Optimized method to add two SparseRealVectors
      * @param v Vector to add with
      * @return The sum of <code>this</code> with <code>v</code>
+     * @throws IllegalArgumentException If the dimensions don't match
      */
-    public SparseRealVector add(SparseRealVector v) {
+    public SparseRealVector add(SparseRealVector v) throws IllegalArgumentException{
         checkVectorDimensions(v.getDimension());
-        SparseRealVector res = (SparseRealVector) copy();
-        Iterator iter = res.getEntries().iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            int key = iter.key();
-            if (v.getEntries().containsKey(key)) {
-                res.setEntry(key, iter.value() + v.getEntry(key));
-            }
-        }
-        iter = v.getEntries().iterator();
+        SparseRealVector res = (SparseRealVector)copy();
+        Iterator iter = v.getEntries().iterator();
         while (iter.hasNext()) {
             iter.advance();
             int key = iter.key();
-            if (!entries.containsKey(key)) {
+            if (entries.containsKey(key)) {
+                res.setEntry(key, entries.get(key) + iter.value());
+            } else {
                 res.setEntry(key, iter.value());
             }
         }
@@ -419,8 +414,9 @@
      * Optimized method to compute distance
      * @param v The vector to compute distance to
      * @return The distance from <code>this</code> and <code>v</code>
+     * @throws IllegalArgumentException If the dimensions don't match
      */
-    public double getDistance(SparseRealVector v) {
+    public double getDistance(SparseRealVector v) throws IllegalArgumentException {
         Iterator iter = entries.iterator();
         double res = 0;
         while (iter.hasNext()) {
@@ -1013,8 +1009,9 @@
      * Optimized method to compute the outer product
      * @param v The vector to comput the outer product on
      * @return The outer product of <code>this</code> and <code>v</code>
+     * @throws IllegalArgumentException If the dimensions don't match
      */
-    public SparseRealMatrix outerproduct(SparseRealVector v){
+    public SparseRealMatrix outerproduct(SparseRealVector v) throws IllegalArgumentException{
         checkVectorDimensions(v.getDimension());
         SparseRealMatrix res = new SparseRealMatrix(virtualSize, virtualSize);
         Iterator iter = entries.iterator();
@@ -1109,19 +1106,23 @@
         }
     }
 
-    /** {@inheritDoc} */
-    public SparseRealVector subtract(SparseRealVector v) {
+    /**
+     * Optimized method to subtract SparseRealVectors
+     * @param v The vector to subtract from <code>this</code>
+     * @return The difference of <code>this</code> and <code>v</code>
+     * @throws IllegalArgumentException If the dimensions don't match
+     */
+    public SparseRealVector subtract(SparseRealVector v) throws IllegalArgumentException{
         checkVectorDimensions(v.getDimension());
-        SparseRealVector res = new SparseRealVector(this);
+        SparseRealVector res = (SparseRealVector)copy();
         Iterator iter = v.getEntries().iterator();
-        OpenIntToDoubleHashMap values = res.getEntries();
         while (iter.hasNext()) {
             iter.advance();
             int key = iter.key();
             if (entries.containsKey(key)) {
-                values.put(key, entries.get(key) - iter.value());
+                res.setEntry(key, entries.get(key) - iter.value());
             } else {
-                values.put(key, -iter.value());
+                res.setEntry(key, -iter.value());
             }
         }
         return res;
@@ -1210,4 +1211,52 @@
         return getData();
     }
 
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        long temp;
+        temp = Double.doubleToLongBits(epsilon);
+        result = prime * result + (int) (temp ^ (temp >>> 32));
+        result = prime * result + virtualSize;
+        return result;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (!(obj instanceof SparseRealVector))
+            return false;
+        SparseRealVector other = (SparseRealVector) obj;
+        if (virtualSize != other.virtualSize)
+            return false;
+        if (Double.doubleToLongBits(epsilon) != Double
+                .doubleToLongBits(other.epsilon))
+            return false;
+        Iterator iter = entries.iterator();
+        while(iter.hasNext()){
+            iter.advance();
+            double test = iter.value() - other.getEntry(iter.key());
+            if(Math.abs(test) > epsilon)
+                return false;
+        }
+        iter = other.getEntries().iterator();
+        while(iter.hasNext()){
+            iter.advance();
+            double test = iter.value() - getEntry(iter.key());
+            if(!isZero(test))
+                return false;
+        }
+        return true;
+    }
+
 }

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java?rev=744614&r1=744613&r2=744614&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/OpenIntToDoubleHashMap.java
Sun Feb 15 06:46:15 2009
@@ -20,7 +20,6 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.Serializable;
-import java.util.Arrays;
 import java.util.ConcurrentModificationException;
 import java.util.NoSuchElementException;
 
@@ -476,6 +475,7 @@
         return h ^ (h >>> 7) ^ (h >>> 4);
     }
 
+    
     /** Iterator class for the map. */
     public class Iterator {
 
@@ -595,5 +595,5 @@
         count = 0;
     }
 
-    
+            
 }

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java?rev=744614&r1=744613&r2=744614&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SparseRealVectorTest.java
Sun Feb 15 06:46:15 2009
@@ -1124,12 +1124,11 @@
         v.setEntry(1, 1);
         assertTrue(v.isInfinite());
 
-        //TODO: backing store doesn't implement equals
         //TODO: differeciate from resetting to zero
-        //v.setEntry(0, 0);
-        //assertEquals(v, new SparseRealVector(new double[] { 0, 1, 2 }));
-        //assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
-        //assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 }));
+        v.setEntry(0, 0);
+        assertEquals(v, new SparseRealVector(new double[] { 0, 1, 2 }));
+        assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
+        assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 }));
 
         //assertEquals(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(),
         //              new SparseRealVector(new double[] { 0, Double.NaN, 2 }).hashCode());



Mime
View raw message