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/04/29 21:10:02 UTC

svn commit: r769880 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/geometry/Vector3D.java site/xdoc/changes.xml test/org/apache/commons/math/geometry/Vector3DTest.java

Author: luc
Date: Wed Apr 29 19:10:01 2009
New Revision: 769880

URL: http://svn.apache.org/viewvc?rev=769880&view=rev
Log:
added getNorm1, getNormInf, distance1 and distanceInf to the Vector3D class

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/geometry/Vector3D.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/geometry/Vector3D.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/geometry/Vector3D.java?rev=769880&r1=769879&r2=769880&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/geometry/Vector3D.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/geometry/Vector3D.java Wed Apr 29 19:10:01 2009
@@ -196,7 +196,14 @@
     return z;
   }
 
-  /** Get the norm for the vector.
+  /** Get the L<sub>1</sub> norm for the vector.
+   * @return L<sub>1</sub> norm for the vector
+   */
+  public double getNorm1() {
+    return Math.abs(x) + Math.abs(y) + Math.abs(z);
+  }
+
+  /** Get the L<sub>2</sub> norm for the vector.
    * @return euclidian norm for the vector
    */
   public double getNorm() {
@@ -210,6 +217,13 @@
     return x * x + y * y + z * z;
   }
 
+  /** Get the L<sub>&infin;</sub> norm for the vector.
+   * @return L<sub>&infin;</sub> norm for the vector
+   */
+  public double getNormInf() {
+    return Math.max(Math.max(Math.abs(x), Math.abs(y)), Math.abs(z));
+  }
+
   /** Get the azimuth of the vector.
    * @return azimuth (&alpha;) of the vector, between -&pi; and +&pi;
    * @see #Vector3D(double, double)
@@ -309,7 +323,7 @@
   /** Compute the angular separation between two vectors.
    * <p>This method computes the angular separation between two
    * vectors using the dot product for well separated vectors and the
-   * cross product for almost aligned vectors. This allow to have a
+   * cross product for almost aligned vectors. This allows to have a
    * good accuracy in all cases, even for vectors very close to each
    * other.</p>
    * @param v1 first vector
@@ -454,13 +468,28 @@
                         v1.x * v2.y - v1.y * v2.x);
   }
 
-  /** Compute the distance between two vectors.
+  /** Compute the distance between two vectors according to the L<sub>1</sub> norm.
+   * <p>Calling this method is equivalent to calling:
+   * <code>v1.subtract(v2).getNorm1()</code> except that no intermediate
+   * vector is built</p>
+   * @param v1 first vector
+   * @param v2 second vector
+   * @return the distance between v1 and v2 according to the L<sub>1</sub> norm
+   */
+  public static double distance1(Vector3D v1, Vector3D v2) {
+    final double dx = Math.abs(v2.x - v1.x);
+    final double dy = Math.abs(v2.y - v1.y);
+    final double dz = Math.abs(v2.z - v1.z);
+    return dx + dy + dz;
+  }
+
+  /** Compute the distance between two vectors according to the L<sub>2</sub> norm.
    * <p>Calling this method is equivalent to calling:
    * <code>v1.subtract(v2).getNorm()</code> except that no intermediate
    * vector is built</p>
    * @param v1 first vector
    * @param v2 second vector
-   * @return the distance between v1 and v2
+   * @return the distance between v1 and v2 according to the L<sub>2</sub> norm
    */
   public static double distance(Vector3D v1, Vector3D v2) {
     final double dx = v2.x - v1.x;
@@ -469,6 +498,21 @@
     return Math.sqrt(dx * dx + dy * dy + dz * dz);
   }
 
+  /** Compute the distance between two vectors according to the L<sub>&infin;</sub> norm.
+   * <p>Calling this method is equivalent to calling:
+   * <code>v1.subtract(v2).getNormInf()</code> except that no intermediate
+   * vector is built</p>
+   * @param v1 first vector
+   * @param v2 second vector
+   * @return the distance between v1 and v2 according to the L<sub>&infin;</sub> norm
+   */
+  public static double distanceInf(Vector3D v1, Vector3D v2) {
+    final double dx = Math.abs(v2.x - v1.x);
+    final double dy = Math.abs(v2.y - v1.y);
+    final double dz = Math.abs(v2.z - v1.z);
+    return Math.max(Math.max(dx, dy), dz);
+  }
+
   /** Compute the square of the distance between two vectors.
    * <p>Calling this method is equivalent to calling:
    * <code>v1.subtract(v2).getNormSq()</code> except that no intermediate

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=769880&r1=769879&r2=769880&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Apr 29 19:10:01 2009
@@ -41,7 +41,10 @@
     <release version="2.0" date="TBD" description="TBD">
       <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) 
+        in ulps (Units in Last Position)
+      </action>
+      <action dev="luc" type="fix" issue="MATH-263">
+        Added getNorm1, getNormInf, distance1 and distanceInf to the Vector3D class
       </action>
       <action dev="luc" type="add" >
         Added support for any type of field in linear algebra (FielxMatrix, FieldVector,

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java?rev=769880&r1=769879&r2=769880&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java Wed Apr 29 19:10:01 2009
@@ -53,10 +53,52 @@
     assertTrue(Math.abs(v.getZ() - 3) < 1.0e-12);
   }
   
+  public void testNorm1() {
+    assertEquals(0.0, Vector3D.ZERO.getNorm1());
+    assertEquals(6.0, new Vector3D(1, -2, 3).getNorm1(), 0);
+  }
+
   public void testNorm() {
-    assertTrue(Math.abs(Vector3D.ZERO.getNorm()) < 1.0e-12);
-    assertTrue(Math.abs(new Vector3D(1, 2, 3).getNorm() - Math.sqrt(14))
-               < 1.0e-12);
+      assertEquals(0.0, Vector3D.ZERO.getNorm());
+      assertEquals(Math.sqrt(14), new Vector3D(1, 2, 3).getNorm(), 1.0e-12);
+    }
+
+  public void testNormInf() {
+      assertEquals(0.0, Vector3D.ZERO.getNormInf());
+      assertEquals(3.0, new Vector3D(1, -2, 3).getNormInf(), 0);
+    }
+
+  public void testDistance1() {
+      Vector3D v1 = new Vector3D(1, -2, 3);
+      Vector3D v2 = new Vector3D(-4, 2, 0);
+      assertEquals(0.0, Vector3D.distance1(Vector3D.MINUS_I, Vector3D.MINUS_I), 0);
+      assertEquals(12.0, Vector3D.distance1(v1, v2), 1.0e-12);
+      assertEquals(v1.subtract(v2).getNorm1(), Vector3D.distance1(v1, v2), 1.0e-12);
+  }
+
+  public void testDistance() {
+      Vector3D v1 = new Vector3D(1, -2, 3);
+      Vector3D v2 = new Vector3D(-4, 2, 0);
+      assertEquals(0.0, Vector3D.distance(Vector3D.MINUS_I, Vector3D.MINUS_I), 0);
+      assertEquals(Math.sqrt(50), Vector3D.distance(v1, v2), 1.0e-12);
+      assertEquals(v1.subtract(v2).getNorm(), Vector3D.distance(v1, v2), 1.0e-12);
+  }
+
+  public void testDistanceSq() {
+      Vector3D v1 = new Vector3D(1, -2, 3);
+      Vector3D v2 = new Vector3D(-4, 2, 0);
+      assertEquals(0.0, Vector3D.distanceSq(Vector3D.MINUS_I, Vector3D.MINUS_I), 0);
+      assertEquals(50.0, Vector3D.distanceSq(v1, v2), 1.0e-12);
+      assertEquals(Vector3D.distance(v1, v2) * Vector3D.distance(v1, v2),
+                   Vector3D.distanceSq(v1, v2), 1.0e-12);
+  }
+
+  public void testDistanceInf() {
+      Vector3D v1 = new Vector3D(1, -2, 3);
+      Vector3D v2 = new Vector3D(-4, 2, 0);
+      assertEquals(0.0, Vector3D.distanceInf(Vector3D.MINUS_I, Vector3D.MINUS_I), 0);
+      assertEquals(5.0, Vector3D.distanceInf(v1, v2), 1.0e-12);
+      assertEquals(v1.subtract(v2).getNormInf(), Vector3D.distanceInf(v1, v2), 1.0e-12);
   }
 
   public void testSubtract() {