You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/04/27 13:54:17 UTC
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
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(
Re: 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
Posted by Phil Steitz <ph...@gmail.com>.
On 4/27/11 4:54 AM, erans@apache.org wrote:
> 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}.
What about infinities / NaNs as arguments? Should we throw
IllegalArgumentException or just return NaNs / infinities?
We should also clarify in javadoc what happens when period < 0
(mapping is into (period, 0]) and make it clearer exactly what this
function computes. Probably best to just include the formula in the
javadoc.
Phil
> + */
> + 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(
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: 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
Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Wed, Apr 27, 2011 at 02:04:43PM +0200, Luc Maisonobe wrote:
> Le 27/04/2011 13:54, erans@apache.org a écrit :
> > Author: erans
>
> Hi Gilles,
>
> > 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;
>
> Shouldn't this be replaced by FastMath ?
Done in revision 1097094.
Best,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: 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
Posted by Luc Maisonobe <Lu...@free.fr>.
Le 27/04/2011 13:54, erans@apache.org a écrit :
> Author: erans
Hi Gilles,
> 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;
Shouldn't this be replaced by FastMath ?
Luc
> + }
> +
> /**
> * <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(
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org