You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/05/01 21:29:44 UTC

svn commit: r770798 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/util/MathUtils.java site/xdoc/changes.xml test/org/apache/commons/math/util/MathUtilsTest.java

Author: luc
Date: Fri May  1 19:29:44 2009
New Revision: 770798

URL: http://svn.apache.org/viewvc?rev=770798&view=rev
Log:
Added distance1, distance and distanceInf utility methods for double and int arrays in MathUtils
JIRA: MATH-265

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?rev=770798&r1=770797&r2=770798&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java Fri May  1 19:29:44 2009
@@ -1483,4 +1483,97 @@
 
     }
 
+    /**
+     * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>1</sub> distance between the two points
+     */
+    public static final double distance1(double[] p1, double[] p2) {
+        double sum = 0;
+        for (int i = 0; i < p1.length; i++) {
+            sum += Math.abs(p1[i] - p2[i]);
+        }
+        return sum;
+    }
+    
+    /**
+     * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>1</sub> distance between the two points
+     */
+    public static final int distance1(int[] p1, int[] p2) {
+      int sum = 0;
+      for (int i = 0; i < p1.length; i++) {
+          sum += Math.abs(p1[i] - p2[i]);
+      }
+      return sum;
+    }
+
+    /**
+     * Calculates the L<sub>2</sub> (Euclidean) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>2</sub> distance between the two points
+     */
+    public static final double distance(double[] p1, double[] p2) {
+        double sum = 0;
+        for (int i = 0; i < p1.length; i++) {
+            final double dp = p1[i] - p2[i];
+            sum += dp * dp;
+        }
+        return Math.sqrt(sum);
+    }
+    
+    /**
+     * Calculates the L<sub>2</sub> (Euclidean) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>2</sub> distance between the two points
+     */
+    public static final double distance(int[] p1, int[] p2) {
+      int sum = 0;
+      for (int i = 0; i < p1.length; i++) {
+          final int dp = p1[i] - p2[i];
+          sum += dp * dp;
+      }
+      return Math.sqrt(sum);
+    }
+    
+    /**
+     * Calculates the L<sub>&infin;</sub> (max of abs) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>&infin;</sub> distance between the two points
+     */
+    public static final double distanceInf(double[] p1, double[] p2) {
+        double max = 0;
+        for (int i = 0; i < p1.length; i++) {
+            max = Math.max(max, Math.abs(p1[i] - p2[i]));
+        }
+        return max;
+    }
+    
+    /**
+     * Calculates the L<sub>&infin;</sub> (max of abs) distance between two points.
+     *
+     * @param p1 the first point
+     * @param p2 the second point
+     * @return the L<sub>&infin;</sub> distance between the two points
+     */
+    public static final int distanceInf(int[] p1, int[] p2) {
+        int max = 0;
+        for (int i = 0; i < p1.length; i++) {
+            max = Math.max(max, Math.abs(p1[i] - p2[i]));
+        }
+        return max;
+    }
+
+    
 }

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=770798&r1=770797&r2=770798&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri May  1 19:29:44 2009
@@ -39,6 +39,10 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="fix" issue="MATH-265" due-to="Benjamin McCann">
+        Added distance1, distance and distanceInf utility methods for double and
+        int arrays in MathUtils
+      </action>
       <action dev="luc" type="fix" issue="MATH-264" due-to="Gilles Sadowski">
         Added an utility equality method between double numbers using tolerance
         in ulps (Units in Last Position)

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=770798&r1=770797&r2=770798&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java Fri May  1 19:29:44 2009
@@ -1179,4 +1179,41 @@
         assertEquals(bigOne, MathUtils.pow(twentyOne, BigInteger.valueOf(103l)));
         
     }
-}
\ No newline at end of file
+
+    public void testL1DistanceDouble() {
+        double[] p1 = { 2.5,  0.0 };
+        double[] p2 = { -0.5, 4.0 };
+        assertEquals(7.0, MathUtils.distance1(p1, p2));
+    }
+
+    public void testL1DistanceInt() {
+        int[] p1 = { 3, 0 };
+        int[] p2 = { 0, 4 };
+        assertEquals(7, MathUtils.distance1(p1, p2));
+    }
+
+    public void testL2DistanceDouble() {
+        double[] p1 = { 2.5,  0.0 };
+        double[] p2 = { -0.5, 4.0 };
+        assertEquals(5.0, MathUtils.distance(p1, p2));
+    }
+
+    public void testL2DistanceInt() {
+        int[] p1 = { 3, 0 };
+        int[] p2 = { 0, 4 };
+        assertEquals(5.0, MathUtils.distance(p1, p2));
+    }
+
+    public void testLInfDistanceDouble() {
+        double[] p1 = { 2.5,  0.0 };
+        double[] p2 = { -0.5, 4.0 };
+        assertEquals(4.0, MathUtils.distanceInf(p1, p2));
+    }
+
+    public void testLInfDistanceInt() {
+        int[] p1 = { 3, 0 };
+        int[] p2 = { 0, 4 };
+        assertEquals(4, MathUtils.distanceInf(p1, p2));
+    }
+
+}