commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r619861 - 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, 08 Feb 2008 12:36:34 GMT
Author: luc
Date: Fri Feb  8 04:36:33 2008
New Revision: 619861

URL: http://svn.apache.org/viewvc?rev=619861&view=rev
Log:
added an angle normalization method to MathUtils

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=619861&r1=619860&r2=619861&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 Feb
 8 04:36:33 2008
@@ -43,6 +43,9 @@
     /** 0.0 cast as a short. */
     private static final short ZS = (short)0;
 
+    /** 2 π. */
+    private static final double TWO_PI = 2 * Math.PI;
+
     /**
      * Private Constructor
      */
@@ -706,6 +709,28 @@
     }
 
     /**
+     * Normalize an angle in a 2&pi wide interval around a center value.
+     * <p>This method has three main uses:</p>
+     * <ul>
+     *   <li>normalize an angle between 0 and 2&pi;:<br/>
+     *       <code>a = MathUtils.normalizeAngle(a, Math.PI);</code></li>
+     *   <li>normalize an angle between -&pi; and +&pi;<br/>
+     *       <code>a = MathUtils.normalizeAngle(a, 0.0);</code></li>
+     *   <li>compute the angle between two defining angular positions:<br>
+     *       <code>angle = MathUtils.normalizeAngle(end, start) - start;</code></li>
+     * </ul>
+     * <p>Note that due to numerical accuracy and since &pi; cannot be represented
+     * exactly, the result interval is <em>closed</em>, it cannot be half-closed
+     * as would be more satisfactory in a purely mathematical view.</p>
+     * @param a angle to normalize
+     * @param center center of the desired 2&pi; interval for the result
+     * @return a-2k&pi; with integer k and center-&pi; &lt;= a-2k&pi; &lt;=
center+&pi;
+     */
+     public static double normalizeAngle(double a, double center) {
+         return a - TWO_PI * Math.floor((a + Math.PI - center) / TWO_PI);
+     }
+
+    /**
      * Round the given value to the specified number of decimal places. The
      * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method.
      * 
@@ -1007,4 +1032,5 @@
         }
         return ret;
     }
+
 }

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=619861&r1=619860&r2=619861&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Feb  8 04:36:33 2008
@@ -146,6 +146,10 @@
       <action dev="luc" type="update" >
         Added a equals and hash methods in MathUtils to check for double arrays
       </action> 
+      <action dev="luc" type="update" >
+        Added an angle normalization method in MathUtils to force angles in some
+        user-defined interval
+      </action> 
     </release>
     <release version="1.1" date="2005-12-17"  
  description="This is a maintenance release containing bug fixes and enhancements.

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=619861&r1=619860&r2=619861&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
Feb  8 04:36:33 2008
@@ -20,6 +20,7 @@
 import junit.framework.TestSuite;
 
 import org.apache.commons.math.TestUtils;
+import org.apache.commons.math.stat.descriptive.SummaryStatistics;
 
 /**
  * Test cases for the MathUtils class.
@@ -519,6 +520,20 @@
         assertEquals(-Double.MIN_VALUE, MathUtils.nextAfter(0, -1), 0);
         assertEquals(0, MathUtils.nextAfter(Double.MIN_VALUE, -1), 0);
         assertEquals(0, MathUtils.nextAfter(-Double.MIN_VALUE, 1), 0);
+    }
+
+    public void testNormalizeAngle() {
+        SummaryStatistics stat = new SummaryStatistics();
+        for (double a = -15.0; a <= 15.0; a += 0.1) {
+            for (double b = -15.0; b <= 15.0; b += 0.2) {
+                double c = MathUtils.normalizeAngle(a, b);
+                assertTrue((b - Math.PI) <= c);
+                assertTrue(c <= (b + Math.PI));
+                double twoK = Math.rint((a - c) / Math.PI);
+                stat.addValue(c - a + twoK * Math.PI);
+                assertEquals(c, a - twoK * Math.PI, 1.0e-14);
+            }
+        }
     }
 
     public void testRoundDouble() {



Mime
View raw message