commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1358044 - 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 05:18:40 GMT
Author: celestin
Date: Fri Jul  6 05:18:39 2012
New Revision: 1358044

URL: http://svn.apache.org/viewvc?rev=1358044&view=rev
Log:
MATH-795:
  - Added default implementation to RealVector RealVector.projection(RealVector)
  - Removed implementation in concrete classes (to avoid code duplication).
  - Created unit tests accordingly.
 

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java
    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/RealVectorTest.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/ArrayRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java?rev=1358044&r1=1358043&r2=1358044&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java
Fri Jul  6 05:18:39 2012
@@ -568,12 +568,6 @@ public class ArrayRealVector extends Rea
 
     /** {@inheritDoc} */
     @Override
-    public RealVector projection(RealVector v) {
-        return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
-    }
-
-    /** {@inheritDoc} */
-    @Override
     public RealMatrix outerProduct(RealVector v) {
         if (v instanceof ArrayRealVector) {
             final double[] vData = ((ArrayRealVector) v).data;

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java?rev=1358044&r1=1358043&r2=1358044&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java
Fri Jul  6 05:18:39 2012
@@ -588,13 +588,6 @@ public class OpenMapRealVector extends S
 
     /** {@inheritDoc} */
     @Override
-    public RealVector projection(RealVector v) {
-        checkVectorDimensions(v.getDimension());
-        return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
-    }
-
-    /** {@inheritDoc} */
-    @Override
     public void setEntry(int index, double value) {
         checkIndex(index);
         if (!isDefaultValue(value)) {

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=1358044&r1=1358043&r2=1358044&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 05:18:39 2012
@@ -671,10 +671,18 @@ public abstract class RealVector {
      *
      * @param v vector onto which instance must be projected.
      * @return projection of the instance onto {@code v}.
+     * @throws MathArithmeticException if {@code this} or {@code v} is the null
+     * vector
      * @throws org.apache.commons.math3.exception.DimensionMismatchException
      * if {@code v} is not the same size as this vector.
      */
-    public abstract RealVector projection(RealVector v);
+    public RealVector projection(final RealVector v) {
+        final double norm2 = v.dotProduct(v);
+        if (norm2 == 0.0) {
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
+        }
+        return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
+    }
 
     /**
      * Set all elements to a single value.
@@ -1371,12 +1379,6 @@ public abstract class RealVector {
 
             /** {@inheritDoc} */
             @Override
-            public RealVector projection(RealVector w) {
-                return v.projection(w);
-            }
-
-            /** {@inheritDoc} */
-            @Override
             public RealMatrix outerProduct(RealVector w) {
                 return v.outerProduct(w);
             }

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=1358044&r1=1358043&r2=1358044&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 05:18:39 2012
@@ -259,11 +259,6 @@ public class ArrayRealVectorTest extends
             throw unsupported();
         }
 
-        @Override
-        public RealVector projection(RealVector v) {
-            throw unsupported();
-        }
-
         public RealVector projection(double[] v) {
             throw unsupported();
         }

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=1358044&r1=1358043&r2=1358044&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 05:18:39 2012
@@ -964,6 +964,59 @@ public abstract class RealVectorAbstract
         doTestOuterProduct(true);
     }
 
+    private void doTestProjection(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 dotProduct = 0d;
+        double norm2 = 0d;
+        for (int i = 0; i < data1.length; i++){
+            dotProduct += data1[i] * data2[i];
+            norm2 += data2[i] * data2[i];
+        }
+        final double s = dotProduct / norm2;
+        final double[] expected = new double[data1.length];
+        for (int i = 0; i < data2.length; i++) {
+            expected[i] = s * data2[i];
+        }
+        final RealVector v1 = create(data1);
+        final RealVector v2;
+        if (mixed) {
+            v2 = createAlien(data2);
+        } else {
+            v2 = create(data2);
+        }
+        final RealVector actual = v1.projection(v2);
+        TestUtils.assertEquals("", expected, actual, 0d);
+    }
+
+    @Test
+    public void testProjectionSameType() {
+        doTestProjection(false);
+    }
+
+    @Test
+    public void testProjectionMixedTypes() {
+        doTestProjection(true);
+    }
+
+    @Test(expected = MathArithmeticException.class)
+    public void testProjectionNullVector() {
+        create(new double[4]).projection(create(new double[4]));
+    }
+
+    @Test(expected = DimensionMismatchException.class)
+    public void testProjectionDimensionMismatch() {
+        final RealVector v1 = create(new double[4]);
+        final RealVector v2 = create(new double[5]);
+        v2.set(1.0);
+        v1.projection(v2);
+    }
+
     @Test
     public void testSet() {
         for (int i = 0; i < values.length; i++) {

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java?rev=1358044&r1=1358043&r2=1358044&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java
Fri Jul  6 05:18:39 2012
@@ -109,11 +109,6 @@ public class RealVectorTest extends Real
         }
 
         @Override
-        public RealVector projection(RealVector v) {
-            throw unsupported();
-        }
-
-        @Override
         public double getEntry(int index) {
             try {
                 return values[index];

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=1358044&r1=1358043&r2=1358044&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 05:18:39 2012
@@ -186,11 +186,6 @@ public class SparseRealVectorTest extend
         }
 
         @Override
-        public RealVector projection(RealVector v) {
-            throw unsupported();
-        }
-
-        @Override
         public RealMatrix outerProduct(RealVector v) {
             throw unsupported();
         }



Mime
View raw message