MATH-1416: Delete functionality now in "Commons Numbers".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/b81be1fe Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/b81be1fe Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/b81be1fe Branch: refs/heads/master Commit: b81be1fea344c07c3024011066f0c8bb8865cc65 Parents: 7f74708 Author: Gilles <er...@apache.org> Authored: Mon May 15 01:15:47 2017 +0200 Committer: Gilles <er...@apache.org> Committed: Mon May 15 01:15:47 2017 +0200 ---------------------------------------------------------------------- .../org/apache/commons/math4/special/Erf.java | 243 ----------------- .../apache/commons/math4/special/ErfTest.java | 261 ------------------- 2 files changed, 504 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/b81be1fe/src/main/java/org/apache/commons/math4/special/Erf.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/special/Erf.java b/src/main/java/org/apache/commons/math4/special/Erf.java deleted file mode 100644 index 47d6ed4..0000000 --- a/src/main/java/org/apache/commons/math4/special/Erf.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.special; - -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.numbers.gamma.RegularizedGamma; - -/** - * This is a utility class that provides computation methods related to the - * error functions. - */ -public class Erf { - - /** - * The number {@code X_CRIT} is used by {@link #erf(double, double)} internally. - * This number solves {@code erf(x)=0.5} within 1ulp. - * More precisely, the current implementations of - * {@link #erf(double)} and {@link #erfc(double)} satisfy:<br> - * {@code erf(X_CRIT) < 0.5},<br> - * {@code erf(Math.nextUp(X_CRIT) > 0.5},<br> - * {@code erfc(X_CRIT) = 0.5}, and<br> - * {@code erfc(Math.nextUp(X_CRIT) < 0.5} - */ - private static final double X_CRIT = 0.4769362762044697; - - /** - * Default constructor. Prohibit instantiation. - */ - private Erf() {} - - /** - * Returns the error function. - * - * <p>erf(x) = 2/√π <sub>0</sub>∫<sup>x</sup> e<sup>-t<span style="position: relative; top: -.5em">2</span></sup>dt </p> - * - * <p>This implementation computes erf(x) using the - * {@link RegularizedGamma.P.value(double, double, double, int) regularized gamma function}, - * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3)</p> - * - * <p>The value returned is always between -1 and 1 (inclusive). - * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from - * either 1 or -1 as a double, so the appropriate extreme value is returned. - * </p> - * - * @param x the value. - * @return the error function erf(x) - * @throws org.apache.commons.math4.exception.MaxCountExceededException - * if the algorithm fails to converge. - * @see RegularizedGamma.P#value(double, double, double, int) - */ - public static double erf(double x) { - if (FastMath.abs(x) > 40) { - return x > 0 ? 1 : -1; - } - final double ret = RegularizedGamma.P.value(0.5, x * x, 1.0e-15, 10000); - return x < 0 ? -ret : ret; - } - - /** - * Returns the complementary error function. - * - * <p>erfc(x) = 2/√π <sub>x</sub>∫<sup>∞</sup> e<sup>-t<span style="position: relative; top: -.5em">2</span></sup>dt - * <br> - * = 1 - {@link #erf(double) erf(x)} </p> - * - * <p>This implementation computes erfc(x) using the - * {@link RegularizedGamma.Q#value(double, double, double, int) regularized gamma function}, - * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3).</p> - * - * <p>The value returned is always between 0 and 2 (inclusive). - * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from - * either 0 or 2 as a double, so the appropriate extreme value is returned. - * </p> - * - * @param x the value - * @return the complementary error function erfc(x) - * @throws org.apache.commons.math4.exception.MaxCountExceededException - * if the algorithm fails to converge. - * @see RegularizedGamma.Q#value(double, double, double, int) - * @since 2.2 - */ - public static double erfc(double x) { - if (FastMath.abs(x) > 40) { - return x > 0 ? 0 : 2; - } - final double ret = RegularizedGamma.Q.value(0.5, x * x, 1.0e-15, 10000); - return x < 0 ? 2 - ret : ret; - } - - /** - * Returns the difference between erf(x1) and erf(x2). - * - * The implementation uses either erf(double) or erfc(double) - * depending on which provides the most precise result. - * - * @param x1 the first value - * @param x2 the second value - * @return erf(x2) - erf(x1) - */ - public static double erf(double x1, double x2) { - if(x1 > x2) { - return -erf(x2, x1); - } - - return - x1 < -X_CRIT ? - x2 < 0.0 ? - erfc(-x2) - erfc(-x1) : - erf(x2) - erf(x1) : - x2 > X_CRIT && x1 > 0.0 ? - erfc(x1) - erfc(x2) : - erf(x2) - erf(x1); - } - - /** - * Returns the inverse erf. - * <p> - * This implementation is described in the paper: - * <a href="http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf">Approximating - * the erfinv function</a> by Mike Giles, Oxford-Man Institute of Quantitative Finance, - * which was published in GPU Computing Gems, volume 2, 2010. - * The source code is available <a href="http://gpucomputing.net/?q=node/1828">here</a>. - * </p> - * @param x the value - * @return t such that x = erf(t) - * @since 3.2 - */ - public static double erfInv(final double x) { - - // beware that the logarithm argument must be - // commputed as (1.0 - x) * (1.0 + x), - // it must NOT be simplified as 1.0 - x * x as this - // would induce rounding errors near the boundaries +/-1 - double w = - FastMath.log((1.0 - x) * (1.0 + x)); - double p; - - if (w < 6.25) { - w -= 3.125; - p = -3.6444120640178196996e-21; - p = -1.685059138182016589e-19 + p * w; - p = 1.2858480715256400167e-18 + p * w; - p = 1.115787767802518096e-17 + p * w; - p = -1.333171662854620906e-16 + p * w; - p = 2.0972767875968561637e-17 + p * w; - p = 6.6376381343583238325e-15 + p * w; - p = -4.0545662729752068639e-14 + p * w; - p = -8.1519341976054721522e-14 + p * w; - p = 2.6335093153082322977e-12 + p * w; - p = -1.2975133253453532498e-11 + p * w; - p = -5.4154120542946279317e-11 + p * w; - p = 1.051212273321532285e-09 + p * w; - p = -4.1126339803469836976e-09 + p * w; - p = -2.9070369957882005086e-08 + p * w; - p = 4.2347877827932403518e-07 + p * w; - p = -1.3654692000834678645e-06 + p * w; - p = -1.3882523362786468719e-05 + p * w; - p = 0.0001867342080340571352 + p * w; - p = -0.00074070253416626697512 + p * w; - p = -0.0060336708714301490533 + p * w; - p = 0.24015818242558961693 + p * w; - p = 1.6536545626831027356 + p * w; - } else if (w < 16.0) { - w = FastMath.sqrt(w) - 3.25; - p = 2.2137376921775787049e-09; - p = 9.0756561938885390979e-08 + p * w; - p = -2.7517406297064545428e-07 + p * w; - p = 1.8239629214389227755e-08 + p * w; - p = 1.5027403968909827627e-06 + p * w; - p = -4.013867526981545969e-06 + p * w; - p = 2.9234449089955446044e-06 + p * w; - p = 1.2475304481671778723e-05 + p * w; - p = -4.7318229009055733981e-05 + p * w; - p = 6.8284851459573175448e-05 + p * w; - p = 2.4031110387097893999e-05 + p * w; - p = -0.0003550375203628474796 + p * w; - p = 0.00095328937973738049703 + p * w; - p = -0.0016882755560235047313 + p * w; - p = 0.0024914420961078508066 + p * w; - p = -0.0037512085075692412107 + p * w; - p = 0.005370914553590063617 + p * w; - p = 1.0052589676941592334 + p * w; - p = 3.0838856104922207635 + p * w; - } else if (!Double.isInfinite(w)) { - w = FastMath.sqrt(w) - 5.0; - p = -2.7109920616438573243e-11; - p = -2.5556418169965252055e-10 + p * w; - p = 1.5076572693500548083e-09 + p * w; - p = -3.7894654401267369937e-09 + p * w; - p = 7.6157012080783393804e-09 + p * w; - p = -1.4960026627149240478e-08 + p * w; - p = 2.9147953450901080826e-08 + p * w; - p = -6.7711997758452339498e-08 + p * w; - p = 2.2900482228026654717e-07 + p * w; - p = -9.9298272942317002539e-07 + p * w; - p = 4.5260625972231537039e-06 + p * w; - p = -1.9681778105531670567e-05 + p * w; - p = 7.5995277030017761139e-05 + p * w; - p = -0.00021503011930044477347 + p * w; - p = -0.00013871931833623122026 + p * w; - p = 1.0103004648645343977 + p * w; - p = 4.8499064014085844221 + p * w; - } else { - // this branch does not appears in the original code, it - // was added because the previous branch does not handle - // x = +/-1 correctly. In this case, w is positive infinity - // and as the first coefficient (-2.71e-11) is negative. - // Once the first multiplication is done, p becomes negative - // infinity and remains so throughout the polynomial evaluation. - // So the branch above incorrectly returns negative infinity - // instead of the correct positive infinity. - p = Double.POSITIVE_INFINITY; - } - - return p * x; - - } - - /** - * Returns the inverse erfc. - * @param x the value - * @return t such that x = erfc(t) - * @since 3.2 - */ - public static double erfcInv(final double x) { - return erfInv(1 - x); - } - -} - http://git-wip-us.apache.org/repos/asf/commons-math/blob/b81be1fe/src/test/java/org/apache/commons/math4/special/ErfTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/special/ErfTest.java b/src/test/java/org/apache/commons/math4/special/ErfTest.java deleted file mode 100644 index 3bd9bf0..0000000 --- a/src/test/java/org/apache/commons/math4/special/ErfTest.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math4.special; - -import org.apache.commons.math4.TestUtils; -import org.apache.commons.math4.special.Erf; -import org.apache.commons.math4.util.FastMath; -import org.junit.Test; -import org.junit.Assert; - -/** - */ -public class ErfTest { - @Test - public void testErf0() { - double actual = Erf.erf(0.0); - double expected = 0.0; - Assert.assertEquals(expected, actual, 1.0e-15); - Assert.assertEquals(1 - expected, Erf.erfc(0.0), 1.0e-15); - } - - @Test - public void testErf1960() { - double x = 1.960 / FastMath.sqrt(2.0); - double actual = Erf.erf(x); - double expected = 0.95; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(x), 1.0e-15); - - actual = Erf.erf(-x); - expected = -expected; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); - } - - @Test - public void testErf2576() { - double x = 2.576 / FastMath.sqrt(2.0); - double actual = Erf.erf(x); - double expected = 0.99; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(x), 1e-15); - - actual = Erf.erf(-x); - expected = -expected; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); - } - - @Test - public void testErf2807() { - double x = 2.807 / FastMath.sqrt(2.0); - double actual = Erf.erf(x); - double expected = 0.995; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(x), 1.0e-15); - - actual = Erf.erf(-x); - expected = -expected; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); - } - - @Test - public void testErf3291() { - double x = 3.291 / FastMath.sqrt(2.0); - double actual = Erf.erf(x); - double expected = 0.999; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - expected, Erf.erfc(x), 1.0e-5); - - actual = Erf.erf(-x); - expected = -expected; - Assert.assertEquals(expected, actual, 1.0e-5); - Assert.assertEquals(1 - expected, Erf.erfc(-x), 1.0e-5); - } - - /** - * MATH-301, MATH-456 - */ - @Test - public void testLargeValues() { - for (int i = 1; i < 200; i*=10) { - double result = Erf.erf(i); - Assert.assertFalse(Double.isNaN(result)); - Assert.assertTrue(result > 0 && result <= 1); - result = Erf.erf(-i); - Assert.assertFalse(Double.isNaN(result)); - Assert.assertTrue(result >= -1 && result < 0); - result = Erf.erfc(i); - Assert.assertFalse(Double.isNaN(result)); - Assert.assertTrue(result >= 0 && result < 1); - result = Erf.erfc(-i); - Assert.assertFalse(Double.isNaN(result)); - Assert.assertTrue(result >= 1 && result <= 2); - } - Assert.assertEquals(-1, Erf.erf(Double.NEGATIVE_INFINITY), 0); - Assert.assertEquals(1, Erf.erf(Double.POSITIVE_INFINITY), 0); - Assert.assertEquals(2, Erf.erfc(Double.NEGATIVE_INFINITY), 0); - Assert.assertEquals(0, Erf.erfc(Double.POSITIVE_INFINITY), 0); - } - - /** - * Compare Erf.erf against reference values computed using GCC 4.2.1 (Apple OSX packaged version) - * erfl (extended precision erf). - */ - @Test - public void testErfGnu() { - final double tol = 1E-15; - final double[] gnuValues = new double[] {-1, -1, -1, -1, -1, - -1, -1, -1, -0.99999999999999997848, - -0.99999999999999264217, -0.99999999999846254017, -0.99999999980338395581, -0.99999998458274209971, - -0.9999992569016276586, -0.99997790950300141459, -0.99959304798255504108, -0.99532226501895273415, - -0.96610514647531072711, -0.84270079294971486948, -0.52049987781304653809, 0, - 0.52049987781304653809, 0.84270079294971486948, 0.96610514647531072711, 0.99532226501895273415, - 0.99959304798255504108, 0.99997790950300141459, 0.9999992569016276586, 0.99999998458274209971, - 0.99999999980338395581, 0.99999999999846254017, 0.99999999999999264217, 0.99999999999999997848, - 1, 1, 1, 1, - 1, 1, 1, 1}; - double x = -10d; - for (int i = 0; i < 41; i++) { - Assert.assertEquals(gnuValues[i], Erf.erf(x), tol); - x += 0.5d; - } - } - - /** - * Compare Erf.erfc against reference values computed using GCC 4.2.1 (Apple OSX packaged version) - * erfcl (extended precision erfc). - */ - @Test - public void testErfcGnu() { - final double tol = 1E-15; - final double[] gnuValues = new double[] { 2, 2, 2, 2, 2, - 2, 2, 2, 1.9999999999999999785, - 1.9999999999999926422, 1.9999999999984625402, 1.9999999998033839558, 1.9999999845827420998, - 1.9999992569016276586, 1.9999779095030014146, 1.9995930479825550411, 1.9953222650189527342, - 1.9661051464753107271, 1.8427007929497148695, 1.5204998778130465381, 1, - 0.47950012218695346194, 0.15729920705028513051, 0.033894853524689272893, 0.0046777349810472658333, - 0.00040695201744495893941, 2.2090496998585441366E-05, 7.4309837234141274516E-07, 1.5417257900280018858E-08, - 1.966160441542887477E-10, 1.5374597944280348501E-12, 7.3578479179743980661E-15, 2.1519736712498913103E-17, - 3.8421483271206474691E-20, 4.1838256077794144006E-23, 2.7766493860305691016E-26, 1.1224297172982927079E-29, - 2.7623240713337714448E-33, 4.1370317465138102353E-37, 3.7692144856548799402E-41, 2.0884875837625447567E-45}; - double x = -10d; - for (int i = 0; i < 41; i++) { - Assert.assertEquals(gnuValues[i], Erf.erfc(x), tol); - x += 0.5d; - } - } - - /** - * Tests erfc against reference data computed using Maple reported in Marsaglia, G,, - * "Evaluating the Normal Distribution," Journal of Statistical Software, July, 2004. - * http//www.jstatsoft.org/v11/a05/paper - */ - @Test - public void testErfcMaple() { - double[][] ref = new double[][] - {{0.1, 4.60172162722971e-01}, - {1.2, 1.15069670221708e-01}, - {2.3, 1.07241100216758e-02}, - {3.4, 3.36929265676881e-04}, - {4.5, 3.39767312473006e-06}, - {5.6, 1.07175902583109e-08}, - {6.7, 1.04209769879652e-11}, - {7.8, 3.09535877195870e-15}, - {8.9, 2.79233437493966e-19}, - {10.0, 7.61985302416053e-24}, - {11.1, 6.27219439321703e-29}, - {12.2, 1.55411978638959e-34}, - {13.3, 1.15734162836904e-40}, - {14.4, 2.58717592540226e-47}, - {15.5, 1.73446079179387e-54}, - {16.6, 3.48454651995041e-62} - }; - for (int i = 0; i < 15; i++) { - final double result = 0.5*Erf.erfc(ref[i][0]/FastMath.sqrt(2)); - Assert.assertEquals(ref[i][1], result, 1E-15); - TestUtils.assertRelativelyEquals(ref[i][1], result, 1E-13); - } - } - - /** - * Test the implementation of Erf.erf(double, double) for consistency with results - * obtained from Erf.erf(double) and Erf.erfc(double). - */ - @Test - public void testTwoArgumentErf() { - double[] xi = new double[]{-2.0, -1.0, -0.9, -0.1, 0.0, 0.1, 0.9, 1.0, 2.0}; - for(double x1 : xi) { - for(double x2 : xi) { - double a = Erf.erf(x1, x2); - double b = Erf.erf(x2) - Erf.erf(x1); - double c = Erf.erfc(x1) - Erf.erfc(x2); - Assert.assertEquals(a, b, 1E-15); - Assert.assertEquals(a, c, 1E-15); - } - } - } - - @Test - public void testErfInvNaN() { - Assert.assertTrue(Double.isNaN(Erf.erfInv(-1.001))); - Assert.assertTrue(Double.isNaN(Erf.erfInv(+1.001))); - } - - @Test - public void testErfInvInfinite() { - Assert.assertTrue(Double.isInfinite(Erf.erfInv(-1))); - Assert.assertTrue(Erf.erfInv(-1) < 0); - Assert.assertTrue(Double.isInfinite(Erf.erfInv(+1))); - Assert.assertTrue(Erf.erfInv(+1) > 0); - } - - @Test - public void testErfInv() { - for (double x = -5.9; x < 5.9; x += 0.01) { - final double y = Erf.erf(x); - final double dydx = 2 * FastMath.exp(-x * x) / FastMath.sqrt(FastMath.PI); - Assert.assertEquals(x, Erf.erfInv(y), 1.0e-15 / dydx); - } - } - - @Test - public void testErfcInvNaN() { - Assert.assertTrue(Double.isNaN(Erf.erfcInv(-0.001))); - Assert.assertTrue(Double.isNaN(Erf.erfcInv(+2.001))); - } - - @Test - public void testErfcInvInfinite() { - Assert.assertTrue(Double.isInfinite(Erf.erfcInv(-0))); - Assert.assertTrue(Erf.erfcInv( 0) > 0); - Assert.assertTrue(Double.isInfinite(Erf.erfcInv(+2))); - Assert.assertTrue(Erf.erfcInv(+2) < 0); - } - - @Test - public void testErfcInv() { - for (double x = -5.85; x < 5.9; x += 0.01) { - final double y = Erf.erfc(x); - final double dydxAbs = 2 * FastMath.exp(-x * x) / FastMath.sqrt(FastMath.PI); - Assert.assertEquals(x, Erf.erfcInv(y), 1.0e-15 / dydxAbs); - } - } -}