Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 43146 invoked from network); 1 May 2009 19:30:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 1 May 2009 19:30:09 -0000 Received: (qmail 16534 invoked by uid 500); 1 May 2009 19:30:09 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 16450 invoked by uid 500); 1 May 2009 19:30:08 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 16441 invoked by uid 99); 1 May 2009 19:30:08 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 May 2009 19:30:08 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 May 2009 19:30:06 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2A2312388A23; Fri, 1 May 2009 19:29:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Fri, 01 May 2009 19:29:44 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090501192945.2A2312388A23@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 L1 (sum of abs) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L1 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 L1 (sum of abs) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L1 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 L2 (Euclidean) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L2 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 L2 (Euclidean) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L2 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 (max of abs) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L 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 (max of abs) distance between two points. + * + * @param p1 the first point + * @param p2 the second point + * @return the L 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 @@ + + Added distance1, distance and distanceInf utility methods for double and + int arrays in MathUtils + 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)); + } + +}