hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yxji...@apache.org
Subject svn commit: r1577886 - in /hama/trunk: CHANGES.txt commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java
Date Sat, 15 Mar 2014 15:53:47 GMT
Author: yxjiang
Date: Sat Mar 15 15:53:47 2014
New Revision: 1577886

URL: http://svn.apache.org/r1577886
Log:
HAMA-888: Add more test cases for DenseDoubleVector

Modified:
    hama/trunk/CHANGES.txt
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java
    hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java

Modified: hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hama/trunk/CHANGES.txt?rev=1577886&r1=1577885&r2=1577886&view=diff
==============================================================================
--- hama/trunk/CHANGES.txt (original)
+++ hama/trunk/CHANGES.txt Sat Mar 15 15:53:47 2014
@@ -7,6 +7,7 @@ Release 0.7.0 (unreleased changes)
   BUG FIXES
   
    HAMA-889: NonDefaultIterator of DenseDoubleVector never reaches the end (Yexi Jiang)
+   HAMA-888: Add more test cases for DenseDoubleVector (Yexi Jiang)
    HAMA-885: Semi-Clustering is not producing expected output (Renil J via edwardyoon)
 
   IMPROVEMENTS

Modified: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java?rev=1577886&r1=1577885&r2=1577886&view=diff
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java (original)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java Sat
Mar 15 15:53:47 2014
@@ -103,26 +103,30 @@ public final class DenseDoubleVector imp
   }
 
   /**
-   * {@inheritDoc}
+   * Apply a function to the element of the vector and returns a result vector.
+   * Note that the function is applied on the copy of the original vector.
    */
   @Override
   public DoubleVector applyToElements(DoubleFunction func) {
+    DoubleVector newVec = new DenseDoubleVector(this.getDimension());
     for (int i = 0; i < vector.length; i++) {
-      this.vector[i] = func.apply(vector[i]);
+      newVec.set(i, func.apply(vector[i]));
     }
-    return this;
+    return newVec;
   }
 
   /**
-   * {@inheritDoc}
+   * Apply a function to the element of the vector and another vector, and then returns a
result vector.
+   * Note that the function is applied on the copy of the original vectors.
    */
   @Override
   public DoubleVector applyToElements(DoubleVector other,
       DoubleDoubleFunction func) {
+    DoubleVector newVec = new DenseDoubleVector(this.getDimension());
     for (int i = 0; i < vector.length; i++) {
-      this.vector[i] = func.apply(vector[i], other.get(i));
+      newVec.set(i, func.apply(vector[i], other.get(i)));
     }
-    return this;
+    return newVec;
   }
 
   /*
@@ -354,7 +358,8 @@ public final class DenseDoubleVector imp
   }
 
   /**
-   * {@inheritDoc}
+   * Get a subset of the original vector starting from 'start' and end to 'end', 
+   * with both ends inclusive.
    */
   @Override
   public DoubleVector sliceUnsafe(int start, int end) {
@@ -367,8 +372,7 @@ public final class DenseDoubleVector imp
   }
 
   /*
-   * (non-Javadoc)
-   * @see de.jungblut.math.DoubleVector#max()
+   * Return the maximum.
    */
   @Override
   public double max() {
@@ -383,6 +387,7 @@ public final class DenseDoubleVector imp
   }
 
   /**
+   * Return the index of the first maximum.
    * @return the index where the maximum resides.
    */
   public int maxIndex() {
@@ -399,8 +404,7 @@ public final class DenseDoubleVector imp
   }
 
   /*
-   * (non-Javadoc)
-   * @see de.jungblut.math.DoubleVector#min()
+   * Return the minimum.
    */
   @Override
   public double min() {
@@ -415,6 +419,7 @@ public final class DenseDoubleVector imp
   }
 
   /**
+   * Return the index of the first minimum.
    * @return the index where the minimum resides.
    */
   public int minIndex() {
@@ -431,6 +436,7 @@ public final class DenseDoubleVector imp
   }
 
   /**
+   * Round each of the element in the vector to the integer.
    * @return a new vector which has rinted each element.
    */
   public DenseDoubleVector rint() {
@@ -603,6 +609,7 @@ public final class DenseDoubleVector imp
   }
 
   /**
+   * Generate a vector with all element to be 1.
    * @return a new vector with dimension num and a default value of 1.
    */
   public static DenseDoubleVector ones(int num) {
@@ -610,6 +617,8 @@ public final class DenseDoubleVector imp
   }
 
   /**
+   * Generate a vector whose elements are in increasing order,
+   * where the start value is 'from', end value is 'to', with increment 'stepsize'.
    * @return a new vector filled from index, to index, with a given stepsize.
    */
   public static DenseDoubleVector fromUpTo(double from, double to,

Modified: hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java?rev=1577886&r1=1577885&r2=1577886&view=diff
==============================================================================
--- hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java
(original)
+++ hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java
Sat Mar 15 15:53:47 2014
@@ -19,11 +19,13 @@ package org.apache.hama.commons.math;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
+import java.util.Comparator;
 import java.util.Iterator;
+import java.util.List;
 
 import org.apache.hama.commons.math.DoubleVector.DoubleVectorElement;
-import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -33,57 +35,6 @@ import org.junit.Test;
 public class TestDenseDoubleVector {
 
   @Test
-  public void testApplyDoubleFunction() {
-    double[] values = new double[] {1, 2, 3, 4, 5};
-    double[] result = new double[] {2, 3, 4, 5, 6};
-    
-    DoubleVector vec1 = new DenseDoubleVector(values);
-    
-    vec1.applyToElements(new DoubleFunction() {
-
-      @Override
-      public double apply(double value) {
-        return value + 1;
-      }
-
-      @Override
-      public double applyDerivative(double value) {
-        throw new UnsupportedOperationException("Not supported.");
-      }
-      
-    });
-    
-    assertArrayEquals(result, vec1.toArray(), 0.0001);
-  }
-  
-  @Test
-  public void testApplyDoubleDoubleFunction() {
-    double[] values1 = new double[] {1, 2, 3, 4, 5, 6};
-    double[] values2 = new double[] {7, 8, 9, 10, 11, 12};
-    double[] result = new double[] {8, 10, 12, 14, 16, 18};
-    
-    DoubleVector vec1 = new DenseDoubleVector(values1);
-    DoubleVector vec2 = new DenseDoubleVector(values2);
-    
-    vec1.applyToElements(vec2, new DoubleDoubleFunction() {
-
-      @Override
-      public double apply(double x1, double x2) {
-        return x1 + x2;
-      }
-
-      @Override
-      public double applyDerivative(double x1, double x2) {
-        throw new UnsupportedOperationException("Not supported");
-      }
-      
-    });
-    
-    assertArrayEquals(result, vec1.toArray(), 0.0001);
-    
-  }
-  
-  @Test
   public void testAddNormal() {
     double[] arr1 = new double[] {1, 2, 3};
     double[] arr2 = new double[] {4, 5, 6};
@@ -232,6 +183,362 @@ public class TestDenseDoubleVector {
       }
       assertEquals(expectedRes[curIdx++], itrNonZero.next().getValue(), 0.000001);
     }
+  }
+  
+  @Test
+  public void testApply() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    DoubleVector timesTwoVec = vec.applyToElements(new DoubleFunction() {
+      @Override
+      public double apply(double value) {
+        return value * 3;
+      }
+      @Override
+      public double applyDerivative(double value) {
+        return 0;
+      }
+    });
+    
+    DoubleVector timesVec = timesTwoVec.applyToElements(vec, new DoubleDoubleFunction() {
+      @Override
+      public double apply(double x1, double x2) {
+        return x1 * x2;
+      }
+      @Override
+      public double applyDerivative(double x1, double x2) {
+        return 0;
+      }
+    });
+    
+    for (int i = 0; i < vals.length; ++i) {
+      assertEquals(vals[i] * 3, timesTwoVec.get(i), 0.000001);
+      assertEquals(vals[i] * timesTwoVec.get(i), timesVec.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testAdd() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    
+    DoubleVector vec3 = vec1.addUnsafe(vec2);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(vec3.get(i), vec1.get(i) + vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testAddWithException() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    vec1.add(vec2);
+  }
+  
+  @Test
+  public void testSubtract() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    
+    DoubleVector vec3 = vec1.subtractUnsafe(vec2);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(vec3.get(i), vec1.get(i) - vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testSubtractWithException() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    vec1.subtract(vec2);
+  }
+  
+  @Test
+  public void testSubtractFrom() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    double constant = 10;
+    
+    DoubleVector vec3 = vec1.subtractFrom(constant);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(constant - vec1.get(i), vec3.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testMultiply() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    
+    DoubleVector vec3 = vec1.multiplyUnsafe(vec2);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(vec3.get(i), vec1.get(i) * vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testMultiplyWithException() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    vec1.multiply(vec2);
+  }
+  
+  @Test
+  public void testDivide() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    double constant = 10;
+    
+    DoubleVector vec3 = vec1.divide(constant);
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(vec1.get(i) / constant, vec3.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testPow() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    int constant = 5;
+    
+    DoubleVector vec3 = vec1.pow(constant);
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(Math.pow(vec1.get(i), 5), vec3.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testSqrt() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    
+    DoubleVector vec3 = vec1.sqrt();
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(Math.sqrt(vec1.get(i)), vec3.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testSum() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    
+    double expected = 0;
+    double res = vec.sum();
+    for (int i = 0; i < vals.length; ++i) {
+      expected += vec.get(i);
+    }
+    assertEquals(expected, res, 0.000001);
+  }
+  
+  @Test
+  public void testAbs() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    
+    DoubleVector vec2 = vec.abs();
+    for (int i = 0; i < vals.length; ++i) {
+      assertEquals(Math.abs(vec.get(i)), vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testDivideFrom() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    double constant = 10;
+    
+    DoubleVector vec3 = vec1.divideFrom(constant);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      assertEquals(constant / vec1.get(i), vec3.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testDot() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    
+    double expected = 0.0;
+    double res = vec1.dotUnsafe(vec2);
+    
+    for (int i = 0; i < vals1.length; ++i) {
+      expected += vec1.get(i) * vec2.get(i);
+    }
+    assertEquals(expected, res, 0.000001);
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testDotWithException() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    vec1.dot(vec2);
+  }
+  
+  @Test
+  public void testSlice() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {2, 3, 4, 5, 6};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
     
+    DoubleVector vec3 = vec1.sliceUnsafe(1, 5);
+    assertEquals(vec2, vec3);
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testSliceWithNegativeIndices() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    vec.slice(-1, -9);
+  }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testSliceWithOutofBounds() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    vec.slice(1, 9);
   }
+  
+  @Test(expected = IllegalArgumentException.class)
+  public void testSliceWithNegativeLength() {
+    double[] vals1 = {1, 2, 3, 4, 5, 6, 7, 8};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    vec1.slice(3, 2);
+  }
+  
+  @Test
+  public void testMinMax() {
+    double[] vals = {11, 2, 3, 4, 15, 6, 7, 8};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    assertEquals(15, vec.max(), 0.000001);
+    assertEquals(2, vec.min(), 0.000001);
+  }
+  
+  @Test
+  public void testMaxIndexMinIndex() {
+    double[] vals = {11, 2, 3, 4, 15, 6, 7, 8};
+    DenseDoubleVector vec = new DenseDoubleVector(vals);
+    assertEquals(4, vec.maxIndex());
+    assertEquals(1, vec.minIndex());
+  }
+  
+  @Test
+  public void testRint() {
+    double[] vals = {11, 2, 3, 4, 15, 6, 7, 8};
+    DenseDoubleVector vec = new DenseDoubleVector(vals);
+    DenseDoubleVector vec2 = vec.rint();
+    for (int i = 0; i < vec.getDimension(); ++i) {
+      assertEquals(Math.rint(vec.get(i)), vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testRound() {
+    double[] vals = {11, 2, 3, 4, 15, 6, 7, 8};
+    DenseDoubleVector vec = new DenseDoubleVector(vals);
+    DenseDoubleVector vec2 = vec.round();
+    for (int i = 0; i < vec.getDimension(); ++i) {
+      assertEquals(Math.round(vec.get(i)), vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testCeil() {
+    double[] vals = {11, 2, 3, 4, 15, 6, 7, 8};
+    DenseDoubleVector vec = new DenseDoubleVector(vals);
+    DenseDoubleVector vec2 = vec.ceil();
+    for (int i = 0; i < vec.getDimension(); ++i) {
+      assertEquals(Math.ceil(vec.get(i)), vec2.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testToArray() {
+    double[] vals = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+    DenseDoubleVector vec = new DenseDoubleVector(vals);
+    double[] vals2 = vec.toArray();
+    assertEquals(vals, vals2);
+  }
+  
+  @Test(expected = AssertionError.class)
+  public void deepCopy() {
+    double[] vals1 = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+    double[] vals2 = {8, 7, 6, 5, 4, 3, 2, 1, 0};
+    DoubleVector vec1 = new DenseDoubleVector(vals1);
+    DoubleVector vec2 = new DenseDoubleVector(vals2);
+    
+    DoubleVector vec3 = vec1.deepCopy();
+    vec1 = vec1.add(vec2);
+    assertEquals(vec1, vec3);
+  }
+  
+  @Test
+  public void testOnes() {
+    DoubleVector vec = DenseDoubleVector.ones(10);
+    for (int i = 0; i < vec.getDimension(); ++i) {
+      assertEquals(1, vec.get(i), 0.000001);
+    }
+  }
+  
+  @Test
+  public void testFromUpTo() {
+    double from = 11;
+    double to = 111.5;
+    double stepsize = 2.5;
+    
+    DoubleVector vec = DenseDoubleVector.fromUpTo(from, to, stepsize);
+    
+    int curIndex = 0;
+    double cur = 11;
+    while (cur <= to) {
+      assertEquals(cur, vec.get(curIndex), 0.000001);
+      cur += stepsize;
+      ++curIndex;
+    }
+    
+  }
+  
+  @Test
+  public void testSort() {
+    double[] vals = {12, 32, 31, 11, 52, 13, -1, -222, 2};
+    DoubleVector vec = new DenseDoubleVector(vals);
+    
+    Comparator<Double> comparator = new Comparator<Double>() {
+      @Override
+      public int compare(Double arg0, Double arg1) {
+        return Double.compare(arg0, arg1);
+      }
+    };
+    
+    List<Tuple<Double, Integer>> sorted = DenseDoubleVector.sort(vec, comparator);
+    for (int i = 1; i < sorted.size(); ++i) {
+      Tuple<Double, Integer> previous = sorted.get(i - 1);
+      Tuple<Double, Integer> cur = sorted.get(i);
+      assertTrue(previous.getFirst() <= cur.getFirst());
+    }
+    
+  }
+  
 }



Mime
View raw message