Author: billbarker
Date: Mon Feb  9 04:50:27 2009
New Revision: 742257

URL: http://svn.apache.org/viewvc?rev=742257&view=rev
Log:
Mostly completing the methods that don't make sense if using a sparse vector in 
the first place.  Initial implementations for equals on the backing store, but 
it seems it will need more work (since still have to comment out junit tests).  
Making  checkVectorDimensions protected instead of public on all RealVector 
classes.

Modified:
    
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealVectorImpl.java
    
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/RealVectorImpl.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealVectorImpl.java?rev=742257&r1=742256&r2=742257&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealVectorImpl.java
 (original)
+++ 
commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealVectorImpl.java
 Mon Feb  9 04:50:27 2009
@@ -1270,7 +1270,7 @@
      * @exception IllegalArgumentException if the vectors do not
      * have the same dimension
      */
-    public void checkVectorDimensions(RealVector v)
+    protected void checkVectorDimensions(RealVector v)
         throws IllegalArgumentException {
         checkVectorDimensions(v.getDimension());
     }
@@ -1282,7 +1282,7 @@
      * @exception IllegalArgumentException if the dimension is
      * inconsistent with vector size
      */
-    public void checkVectorDimensions(int n)
+    protected void checkVectorDimensions(int n)
         throws IllegalArgumentException {
         if (data.length != n) {
             throw new IllegalArgumentException("vector dimension is " + 
data.length +

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=742257&r1=742256&r2=742257&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
 Mon Feb  9 04:50:27 2009
@@ -232,6 +232,7 @@
      * @return The sum of <code>this</code> with <code>v</code>
      */
     public SparseRealVector add(SparseRealVector v) {
+        checkVectorDimensions(v.getDimension());
         SparseRealVector res = (SparseRealVector) copy();
         Iterator iter = res.getEntries().iterator();
         while (iter.hasNext()) {
@@ -717,10 +718,8 @@
 
     /** {...@inheritdoc} */
     public RealVector mapCosToSelf() {
-        Iterator iter = entries.iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            entries.put(iter.key(), Math.cos(iter.value()));
+        for(int i=0; i < virtualSize; i++){
+            setEntry(i, Math.cos(getEntry(i)));
         }
         return this;
     }
@@ -732,10 +731,8 @@
 
     /** {...@inheritdoc} */
     public RealVector mapCoshToSelf() {
-        Iterator iter = entries.iterator();
-        while (iter.hasNext()) {
-            iter.advance();
-            entries.put(iter.key(), Math.cosh(iter.value()));
+        for(int i = 0; i < virtualSize; i++){
+            setEntry(i, Math.cosh(getEntry(i)));
         }
         return this;
     }
@@ -1201,7 +1198,7 @@
      * @exception IllegalArgumentException
      *                if the dimension is inconsistent with vector size
      */
-    public void checkVectorDimensions(int n) throws IllegalArgumentException {
+    protected void checkVectorDimensions(int n) throws 
IllegalArgumentException {
         if (getDimension() != n) {
             throw new IllegalArgumentException("vector dimension is "
                     + getDimension() + ", not " + n + " as expected");
@@ -1212,4 +1209,47 @@
     public double[] toArray() {
         return getData();
     }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((entries == null) ? 0 : entries.hashCode());
+        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) {
+        System.out.println("Checking equality of "+obj);
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (!(obj instanceof SparseRealVector))
+            return false;
+        System.out.println("is a sparse vector");
+        SparseRealVector other = (SparseRealVector) obj;
+        if (entries == null) {
+            if (other.entries != null)
+                return false;
+        } else if (!entries.equals(other.entries)){
+            System.out.println("no entries match");
+            return false;
+        }if (Double.doubleToLongBits(epsilon) != Double
+                .doubleToLongBits(other.epsilon))
+            return false;
+        if (virtualSize != other.virtualSize)
+            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=742257&r1=742256&r2=742257&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
 Mon Feb  9 04:50:27 2009
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.ConcurrentModificationException;
 import java.util.NoSuchElementException;
 
@@ -594,4 +595,61 @@
         count = 0;
     }
 
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + Arrays.hashCode(keys);
+        result = prime * result + mask;
+        long temp;
+        temp = Double.doubleToLongBits(missingEntries);
+        result = prime * result + (int) (temp ^ (temp >>> 32));
+        result = prime * result + size;
+        result = prime * result + Arrays.hashCode(states);
+        result = prime * result + Arrays.hashCode(values);
+        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 (getClass() != obj.getClass())
+            return false;
+        OpenIntToDoubleHashMap other = (OpenIntToDoubleHashMap) obj;
+        if (!Arrays.equals(keys, other.keys))
+            return false;
+        if (mask != other.mask)
+            return false;
+        if (Double.doubleToLongBits(missingEntries) != Double
+                .doubleToLongBits(other.missingEntries))
+            return false;
+        if (size != other.size)
+            return false;
+        if (!Arrays.equals(states, other.states)){
+            System.out.println("states not match:" );
+            for(byte e : states){
+                System.out.print(e+" ");
+            }
+            System.out.println();
+            for(byte e : other.states){
+                System.out.print(e+" ");
+            }
+            return false;
+        }
+        if (!Arrays.equals(values, other.values)){
+            System.out.println("values don't match");
+            return false;
+        }
+        return true;
+    }
+
 }

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=742257&r1=742256&r2=742257&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
 Mon Feb  9 04:50:27 2009
@@ -473,11 +473,6 @@
         assertEquals("testData len", 7, v1.getDimension());
         assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
 
-        /* TODO: make this supported */
-        //SparseRealVector v2 = new SparseRealVector(5, 1.23);
-        //assertEquals("testData len", 5, v2.getDimension());
-        //assertEquals("testData is 1.23 ", 1.23, v2.getEntry(4));
-
         SparseRealVector v3 = new SparseRealVector(vec1);
         assertEquals("testData len", 3, v3.getDimension());
         assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
@@ -560,26 +555,10 @@
         assertEquals("testData len", 6, v_append_3.getDimension());
         assertEquals("testData is  ", 4.0, v_append_3.getEntry(3));
 
-       /* TODO: fixme */
-        //RealVector v_append_4 = v1.append(v2_t);
-        //assertEquals("testData len", 6, v_append_4.getDimension());
-        //assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
-
-        /* TODO: fixme */
-        //RealVector v_copy = v1.copy();
-        //assertEquals("testData len", 3, v_copy.getDimension());
-        //assertNotSame("testData not same object ", v1.data, 
v_copy.getData());
-        /* TODO: fixme */
-        //double[] a_double = v1.toArray();
-        //assertEquals("testData len", 3, a_double.length);
-        //assertNotSame("testData not same object ", v1.data, a_double);
-
-
-//      SparseRealVector vout4 = (SparseRealVector) v1.clone();
-//      assertEquals("testData len", 3, vout4.getDimension());
-//      assertEquals("testData not same object ", v1.data, vout4.data);
-
-
+           RealVector v_append_4 = v1.append(v2_t);
+        assertEquals("testData len", 6, v_append_4.getDimension());
+        assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
+        
         RealVector vout5 = v4.getSubVector(3, 3);
         assertEquals("testData len", 3, vout5.getDimension());
         assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1));
@@ -1125,27 +1104,8 @@
             // expected behavior
         } catch (Exception e) {
             fail("wrong exception caught");
-        } 
-
-       /* TODO: fixme */
-       //try {
-        //    v1.checkVectorDimensions(v4); 
-        //    fail("IllegalArgumentException expected");
-        //} catch (IllegalArgumentException ex) {
-            // expected behavior
-        //} catch (Exception e) {
-        //    fail("wrong exception caught");
-        //}        
+        }     
 
-       /* TODO: fixme */
-        //try {
-        //    v1.checkVectorDimensions(v4_2); 
-        //    fail("IllegalArgumentException expected");
-        //} catch (IllegalArgumentException ex) {
-            // expected behavior
-       // } catch (Exception e) {
-        //    fail("wrong exception caught");
-        //}        
 
     }
 
@@ -1159,16 +1119,16 @@
 
         assertFalse(v.isInfinite());
         v.setEntry(0, Double.POSITIVE_INFINITY);
-        // TODO: fixme
+        // TODO: why is this test here
         //assertFalse(v.isInfinite());
         v.setEntry(1, 1);
         assertTrue(v.isInfinite());
 
-        v.setEntry(0, 0);
-        // TODO: backing store doesn't yet 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 }));
+        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());


Reply via email to