commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1358033 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/
Date Fri, 06 Jul 2012 03:48:05 GMT
Author: celestin
Date: Fri Jul  6 03:48:04 2012
New Revision: 1358033

URL: http://svn.apache.org/viewvc?rev=1358033&view=rev
Log:
MATH-795:  refactored unit tests for double RealVector.cosine(RealVector).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1358033&r1=1358032&r2=1358033&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
Fri Jul  6 03:48:04 2012
@@ -317,6 +317,10 @@ public abstract class RealVector {
      *
      * @param v Vector.
      * @return the cosine of the angle between this vector and {@code v}.
+     * @throws MathArithmeticException if {@code this} or {@code v} is the null
+     * vector
+     * @throws DimensionMismatchException if the dimensions of {@code this} and
+     * {@code v} do not match
      */
     public double cosine(RealVector v) {
         final double norm = getNorm();

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java?rev=1358033&r1=1358032&r2=1358033&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java
Fri Jul  6 03:48:04 2012
@@ -21,6 +21,7 @@ import java.util.Iterator;
 
 import org.apache.commons.math3.analysis.UnivariateFunction;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.apache.commons.math3.util.FastMath;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -204,7 +205,11 @@ public class ArrayRealVectorTest extends
 
         @Override
         public double getNorm() {
-            throw unsupported();
+            double sqrNorm = 0.0;
+            for (int i = 0; i < data.length; i++) {
+                sqrNorm += data[i] * data[i];
+            }
+            return FastMath.sqrt(sqrNorm);
         }
 
         @Override

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java?rev=1358033&r1=1358032&r2=1358033&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java
Fri Jul  6 03:48:04 2012
@@ -1247,6 +1247,68 @@ public abstract class RealVectorAbstract
         doTestDotProductDimensionMismatch(true);
     }
 
+    private void doTestCosine(final boolean mixed) {
+        final double x = getPreferredEntryValue();
+        final double[] data1 = {
+            x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x
+        };
+        final double[] data2 = {
+            5d, -6d, 7d, x, x, -8d, -9d, 10d, 11d, x, 12d, 13d, -15d
+        };
+        double norm1 = 0d;
+        double norm2 = 0d;
+        double dotProduct = 0d;
+        for (int i = 0; i < data1.length; i++){
+            norm1 += data1[i] * data1[i];
+            norm2 += data2[i] * data2[i];
+            dotProduct += data1[i] * data2[i];
+        }
+        norm1 = FastMath.sqrt(norm1);
+        norm2 = FastMath.sqrt(norm2);
+        final double expected = dotProduct / (norm1 * norm2);
+        final RealVector v1 = create(data1);
+        final RealVector v2;
+        if (mixed) {
+            v2 = createAlien(data2);
+        } else {
+            v2 = create(data2);
+        }
+        final double actual = v1.cosine(v2);
+        Assert.assertEquals("", expected, actual, 0d);
+
+    }
+
+    @Test
+    public void testCosineSameType() {
+        doTestCosine(false);
+    }
+
+    @Test
+    public void testCosineMixedTypes() {
+        doTestCosine(true);
+    }
+
+    @Test(expected=MathArithmeticException.class)
+    public void testCosineLeftNullVector() {
+        final RealVector v = create(new double[] {0, 0, 0});
+        final RealVector w = create(new double[] {1, 0, 0});
+        v.cosine(w);
+    }
+
+    @Test(expected=MathArithmeticException.class)
+    public void testCosineRightNullVector() {
+        final RealVector v = create(new double[] {0, 0, 0});
+        final RealVector w = create(new double[] {1, 0, 0});
+        w.cosine(v);
+    }
+
+    @Test(expected=DimensionMismatchException.class)
+    public void testCosineDimensionMismatch() {
+        final RealVector v = create(new double[] {1, 2, 3});
+        final RealVector w = create(new double[] {1, 2, 3, 4});
+        v.cosine(w);
+    }
+
     @Test
     public void testBasicFunctions() {
         final RealVector v1 = create(vec1);
@@ -1341,48 +1403,6 @@ public abstract class RealVectorAbstract
         Assert.assertTrue(Double.isNaN(v4.getMaxValue()));
     }
 
-    @Test
-    public void testCosine() {
-        final RealVector v = create(new double[] {1, 0, 0});
-
-        double[] wData = new double[] {1, 1, 0};
-        RealVector w = create(wData);
-        Assert.assertEquals(FastMath.sqrt(2) / 2, v.cosine(w), normTolerance);
-
-        wData = new double[] {1, 0, 0};
-        w = create(wData);
-        Assert.assertEquals(1, v.cosine(w), normTolerance);
-
-        wData = new double[] {0, 1, 0};
-        w = create(wData);
-        Assert.assertEquals(0, v.cosine(w), 0);
-
-        wData = new double[] {-1, 0, 0};
-        w = create(wData);
-        Assert.assertEquals(-1, v.cosine(w), normTolerance);
-    }
-
-    @Test(expected=MathArithmeticException.class)
-    public void testCosinePrecondition1() {
-        final RealVector v = create(new double[] {0, 0, 0});
-        final RealVector w = create(new double[] {1, 0, 0});
-        v.cosine(w);
-    }
-
-    @Test(expected=MathArithmeticException.class)
-    public void testCosinePrecondition2() {
-        final RealVector v = create(new double[] {0, 0, 0});
-        final RealVector w = create(new double[] {1, 0, 0});
-        w.cosine(v);
-    }
-
-    @Test(expected=DimensionMismatchException.class)
-    public void testCosinePrecondition3() {
-        final RealVector v = create(new double[] {1, 2, 3});
-        final RealVector w = create(new double[] {1, 2, 3, 4});
-        v.cosine(w);
-    }
-
     /*
      * TESTS OF THE VISITOR PATTERN
      */

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java?rev=1358033&r1=1358032&r2=1358033&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java
Fri Jul  6 03:48:04 2012
@@ -20,6 +20,7 @@ import java.io.Serializable;
 import java.util.Iterator;
 
 import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.util.FastMath;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -142,7 +143,11 @@ public class SparseRealVectorTest extend
 
         @Override
         public double getNorm() {
-            throw unsupported();
+            double sqrNorm = 0.0;
+            for (int i = 0; i < data.length; i++) {
+                sqrNorm += data[i] * data[i];
+            }
+            return FastMath.sqrt(sqrNorm);
         }
 
         @Override



Mime
View raw message