commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1097088 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/MathUtils.java test/java/org/apache/commons/math/util/MathUtilsTest.java
Date Wed, 27 Apr 2011 11:54:17 GMT
Author: erans
Date: Wed Apr 27 11:54:16 2011
New Revision: 1097088

URL: http://svn.apache.org/viewvc?rev=1097088&view=rev
Log:
MATH-561
Map a value to the interval [O, period).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1097088&r1=1097087&r2=1097088&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Wed
Apr 27 11:54:16 2011
@@ -1281,6 +1281,22 @@ public final class MathUtils {
          return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI);
      }
 
+    /**
+     * Reduce to the primary interval {@code [0 period)}.
+     *
+     * @param a Value to reduce.
+     * @param period Period.
+     * @param offset Value that will be mapped to {@code 0}.
+     * @return the value, within the interval {@code [0 period)},
+     * that corresponds to {@code a}.
+     */
+    public static double reduce(double a,
+                                double period,
+                                double offset) {
+        final double p = Math.abs(period);
+        return a - p * Math.floor((a - offset) / p) - offset;
+    }
+
      /**
       * <p>Normalizes an array to make it sum to a specified value.
       * Returns the result of the transformation <pre>

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1097088&r1=1097087&r2=1097088&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
Wed Apr 27 11:54:16 2011
@@ -1054,6 +1054,55 @@ public final class MathUtilsTest {
     }
 
     @Test
+    public void testReduce() {
+        final double period = -12.222;
+        final double offset = 13;
+
+        final double delta = 1.5;
+
+        double orig = offset + 122456789 * period + delta;
+        double expected = delta;
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, period, offset),
+                            1e-7);
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, -period, offset),
+                            1e-7);
+
+        orig = offset - 123356789 * period - delta;
+        expected = Math.abs(period) - delta;
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, period, offset),
+                            1e-6);
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, -period, offset),
+                            1e-6);
+
+        orig = offset - 123446789 * period + delta;
+        expected = delta;
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, period, offset),
+                            1e-6);
+        Assert.assertEquals(expected,
+                            MathUtils.reduce(orig, -period, offset),
+                            1e-6);
+    }
+
+    @Test
+    public void testReduceComparedWithNormalizeAngle() {
+        final double tol = Math.ulp(1d);
+        final double period = 2 * Math.PI;
+        for (double a = -15; a <= 15; a += 0.5) {
+            for (double center = -15; center <= 15; center += 1) {
+                final double nA = MathUtils.normalizeAngle(a, center);
+                final double offset = center - Math.PI;
+                final double r = MathUtils.reduce(a, period, offset);
+                Assert.assertEquals(nA, r + offset, tol);
+            }
+        }
+    }
+
+    @Test
     public void testNormalizeArray() {
         double[] testValues1 = new double[] {1, 1, 2};
         TestUtils.assertEquals(



Mime
View raw message