commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1082442 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/linear/ site/xdoc/ test/java/org/apache/commons/math/linear/
Date Thu, 17 Mar 2011 10:38:20 GMT
Author: erans
Date: Thu Mar 17 10:38:19 2011
New Revision: 1082442

URL: http://svn.apache.org/viewvc?rev=1082442&view=rev
Log:
MATH-545
Added "cosine" method in "RealVector" interface and default implementation
in "AbstractRealVector".

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVector.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java?rev=1082442&r1=1082441&r2=1082442&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
Thu Mar 17 10:38:19 2011
@@ -23,6 +23,7 @@ import java.util.NoSuchElementException;
 import org.apache.commons.math.exception.MathUnsupportedOperationException;
 import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.exception.OutOfRangeException;
+import org.apache.commons.math.exception.MathArithmeticException;
 import org.apache.commons.math.analysis.FunctionUtils;
 import org.apache.commons.math.analysis.function.Add;
 import org.apache.commons.math.analysis.function.Multiply;
@@ -184,6 +185,23 @@ public abstract class AbstractRealVector
     }
 
     /** {@inheritDoc} */
+    public double cosine(RealVector v) {
+        final double norm = getNorm();
+        final double vNorm = v.getNorm();
+
+        if (norm == 0 ||
+            vNorm == 0) {
+            throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
+        }
+        return dotProduct(v) / (norm * vNorm);
+    }
+
+    /** {@inheritDoc} */
+    public double cosine(double[] v) {
+        return cosine(new ArrayRealVector(v, false));
+    }
+
+    /** {@inheritDoc} */
     public RealVector ebeDivide(double[] v) {
         return ebeDivide(new ArrayRealVector(v, false));
     }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVector.java?rev=1082442&r1=1082441&r2=1082442&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVector.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVector.java
Thu Mar 17 10:38:19 2011
@@ -307,6 +307,24 @@ public interface RealVector {
     double dotProduct(double[] v);
 
     /**
+     * Computes the cosine of the angle between this vector and the
+     * argument.
+     *
+     * @param v Vector.
+     * @return the cosine of the angle between this vector and {@code v}.
+     */
+    double cosine(RealVector v);
+
+    /**
+     * Computes the cosine of the angle between this vector and the
+     * vector whose components are given as argument.
+     *
+     * @param v Components of a vector.
+     * @return the cosine of the angle between this vector and {@code v}.
+     */
+    double cosine(double[] v);
+
+    /**
      * Returns the L<sub>2</sub> norm of the vector.
      * <p>The L<sub>2</sub> norm is the root of the sum of
      * the squared elements.</p>

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1082442&r1=1082441&r2=1082442&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Mar 17 10:38:19 2011
@@ -52,6 +52,10 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="erans" type="update" issue="MATH-545">
+        Added "cosine" method in "RealVector" interface and default implementation
+        in "AbstractRealVector".
+      </action>
       <action dev="erans" type="fix" issue="MATH-546" due-to="Nate Paymer">
         Fixed bug in "KMeansPlusPlusClusterer".
       </action>

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=1082442&r1=1082441&r2=1082442&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
Thu Mar 17 10:38:19 2011
@@ -27,6 +27,7 @@ import org.apache.commons.math.util.Fast
 import org.apache.commons.math.exception.OutOfRangeException;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.apache.commons.math.exception.MathArithmeticException;
+import org.apache.commons.math.exception.DimensionMismatchException;
 import org.apache.commons.math.analysis.function.Abs;
 import org.apache.commons.math.analysis.function.Acos;
 import org.apache.commons.math.analysis.function.Asin;
@@ -221,6 +222,14 @@ public class ArrayRealVectorTest {
             return dot;
         }
 
+        public double cosine(RealVector v) {
+            throw unsupported();
+        }
+
+        public double cosine(double[] v) {
+            throw unsupported();
+        }
+
         public double getNorm() {
             throw unsupported();
         }
@@ -1137,6 +1146,49 @@ public class ArrayRealVectorTest {
         Assert.assertTrue(Double.isNaN(v4.getMaxValue()));
     }
 
+    @Test
+    public void testCosine() {
+        final ArrayRealVector v = new ArrayRealVector(new double[] {1, 0, 0});
+
+        double[] wData = new double[] {1, 1, 0};
+        RealVector w = new ArrayRealVector(wData);
+        Assert.assertEquals(FastMath.sqrt(2) / 2, v.cosine(wData), normTolerance);
+        Assert.assertEquals(FastMath.sqrt(2) / 2, v.cosine(w), normTolerance);
+
+        wData = new double[] {1, 0, 0};
+        w = new ArrayRealVector(wData);
+        Assert.assertEquals(1, v.cosine(wData), normTolerance);
+        Assert.assertEquals(1, v.cosine(w), normTolerance);
+
+        wData = new double[] {0, 1, 0};
+        w = new ArrayRealVector(wData);
+        Assert.assertEquals(0, v.cosine(wData), normTolerance);
+        Assert.assertEquals(0, v.cosine(w), 0);
+
+        wData = new double[] {-1, 0, 0};
+        w = new ArrayRealVector(wData);
+        Assert.assertEquals(-1, v.cosine(wData), normTolerance);
+        Assert.assertEquals(-1, v.cosine(w), normTolerance);
+    }
+
+    @Test(expected=MathArithmeticException.class)
+    public void testCosinePrecondition1() {
+        final ArrayRealVector v = new ArrayRealVector(new double[] {0, 0, 0});
+        final ArrayRealVector w = new ArrayRealVector(new double[] {1, 0, 0});
+        v.cosine(w);
+    }
+    @Test(expected=MathArithmeticException.class)
+    public void testCosinePrecondition2() {
+        final ArrayRealVector v = new ArrayRealVector(new double[] {0, 0, 0});
+        final ArrayRealVector w = new ArrayRealVector(new double[] {1, 0, 0});
+        w.cosine(v);
+    }
+    @Test(expected=DimensionMismatchException.class)
+    public void testCosinePrecondition3() {
+        final ArrayRealVector v = new ArrayRealVector(new double[] {1, 2, 3});
+        final ArrayRealVector w = new ArrayRealVector(new double[] {1, 2, 3, 4});
+        v.cosine(w);
+    }
 
     /** verifies that two vectors are close (sup norm) */
     protected void assertClose(String msg, double[] m, double[] n,



Mime
View raw message