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 2017/05/14 22:52:15 UTC
commons-numbers git commit: NUMBERS-39: "Erf" family of functions.
Repository: commons-numbers
Updated Branches:
refs/heads/master 61d27bd9e -> 4de442f68
NUMBERS-39: "Erf" family of functions.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/4de442f6
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/4de442f6
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/4de442f6
Branch: refs/heads/master
Commit: 4de442f68f7debc828e202e3b89e2e4d0de1e4d8
Parents: 61d27bd
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Mon May 15 00:51:33 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Mon May 15 00:51:33 2017 +0200
----------------------------------------------------------------------
.../org/apache/commons/numbers/gamma/Erf.java | 50 +++++++
.../commons/numbers/gamma/ErfDifference.java | 65 +++++++++
.../org/apache/commons/numbers/gamma/Erfc.java | 53 +++++++
.../commons/numbers/gamma/InverseErf.java | 124 ++++++++++++++++
.../commons/numbers/gamma/InverseErfc.java | 39 ++++++
.../numbers/gamma/ErfDifferenceTest.java | 43 ++++++
.../apache/commons/numbers/gamma/ErfTest.java | 140 +++++++++++++++++++
.../apache/commons/numbers/gamma/ErfcTest.java | 85 +++++++++++
.../commons/numbers/gamma/InverseErfTest.java | 48 +++++++
.../commons/numbers/gamma/InverseErfcTest.java | 48 +++++++
10 files changed, 695 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
new file mode 100644
index 0000000..7892219
--- /dev/null
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erf.java
@@ -0,0 +1,50 @@
+/*
+ * 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.numbers.gamma;
+
+/**
+ * <a href="http://mathworld.wolfram.com/Erf.html">Error function</a>.
+ */
+public class Erf {
+ /**
+ * <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 returned value is always between -1 and 1 (inclusive).
+ * If {@code abs(x) > 40}, then {@code Erf.value(x)} is indistinguishable from
+ * either 1 or -1 at {@code double} precision, so the appropriate extreme value
+ * is returned.
+ * </p>
+ *
+ * @param x the value.
+ * @return the error function.
+ * @throws ArithmeticException if the algorithm fails to converge.
+ *
+ * @see RegularizedGamma.P#value(double, double, double, int)
+ */
+ public static double value(double x) {
+ if (Math.abs(x) > 40) {
+ return x > 0 ? 1 : -1;
+ }
+ final double ret = RegularizedGamma.P.value(0.5, x * x, 1e-15, 10000);
+ return x < 0 ? -ret : ret;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java
new file mode 100644
index 0000000..31e1f59
--- /dev/null
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/ErfDifference.java
@@ -0,0 +1,65 @@
+/*
+ * 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.numbers.gamma;
+
+/**
+ * Computes the difference between {@link Erf error function values}.
+ */
+public class ErfDifference {
+ /**
+ * This number solves {@code erf(x) = 0.5} within 1 ulp.
+ * More precisely, the current implementations of
+ * {@link Erf#value(double)} and {@link Erfc#value(double)} satisfy:
+ * <ul>
+ * <li>{@code Erf.value(X_CRIT) < 0.5},</li>
+ * <li>{@code Erf.value(Math.nextUp(X_CRIT) > 0.5},</li>
+ * <li>{@code Erfc.value(X_CRIT) = 0.5}, and</li>
+ * <li>{@code Erfc.value(Math.nextUp(X_CRIT) < 0.5}</li>
+ * </ul>
+ */
+ private static final double X_CRIT = 0.4769362762044697;
+
+ /**
+ * The implementation uses either {@link Erf} or {@link Erfc},
+ * depending on which provides the most precise result.
+ *
+ * @param x1 First value.
+ * @param x2 Second value.
+ * @return {@link Erf#value(double) Erf.value(x2) - Erf.value(x1)}.
+ */
+ public static double value(double x1,
+ double x2) {
+ if (x1 > x2) {
+ return -value(x2, x1);
+ } else {
+ if (x1 < -X_CRIT) {
+ if (x2 < 0) {
+ return Erfc.value(-x2) - Erfc.value(-x1);
+ } else {
+ return Erf.value(x2) - Erf.value(x1);
+ }
+ } else {
+ if (x2 > X_CRIT &&
+ x1 > 0) {
+ return Erfc.value(x1) - Erfc.value(x2);
+ } else {
+ return Erf.value(x2) - Erf.value(x1);
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
new file mode 100644
index 0000000..674f582
--- /dev/null
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/Erfc.java
@@ -0,0 +1,53 @@
+/*
+ * 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.numbers.gamma;
+
+/**
+ * <a href="http://mathworld.wolfram.com/Erfc.html">Complementary error function</a>.
+ */
+public class Erfc {
+ /**
+ * <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 at {@code double} precision, so the appropriate extreme
+ * value is returned.
+ * </p>
+ *
+ * @param x Value.
+ * @return the complementary error function.
+ * @throws ArithmeticExceptionException if the algorithm fails to converge.
+ *
+ * @see RegularizedGamma.Q#value(double, double, double, int)
+ */
+ public static double value(double x) {
+ if (Math.abs(x) > 40) {
+ return x > 0 ? 0 : 2;
+ }
+ final double ret = RegularizedGamma.Q.value(0.5, x * x, 1e-15, 10000);
+ return x < 0 ?
+ 2 - ret :
+ ret;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
new file mode 100644
index 0000000..b149fa5
--- /dev/null
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErf.java
@@ -0,0 +1,124 @@
+/*
+ * 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.numbers.gamma;
+
+/**
+ * Inverse of the <a href="http://mathworld.wolfram.com/Erf.html">error function</a>.
+ * <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>
+ */
+public class InverseErf {
+ /**
+ * Returns the inverse error function.
+ *
+ * @param x Value.
+ * @return t such that {@code x =} {@link Erf#value(double) Erf.value(t)}.
+ */
+ public static double value(final double x) {
+ // Beware that the logarithm argument must be
+ // commputed as (1 - x) * (1 + x),
+ // it must NOT be simplified as 1 - x * x as this
+ // would induce rounding errors near the boundaries +/-1
+ double w = -Math.log((1 - x) * (1 + 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 = Math.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 = Math.sqrt(w) - 5;
+ 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;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErfc.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErfc.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErfc.java
new file mode 100644
index 0000000..e1a4359
--- /dev/null
+++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/InverseErfc.java
@@ -0,0 +1,39 @@
+/*
+ * 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.numbers.gamma;
+
+/**
+ * Inverse of the <a href="http://mathworld.wolfram.com/Erfc.html">complementary error function</a>.
+ * <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>
+ */
+public class InverseErfc {
+ /**
+ * Returns the inverse complementary error function.
+ *
+ * @param x Value.
+ * @return t such that {@code x =} {@link Erfc#value(double) Erfc.value(t)}.
+ */
+ public static double value(double x) {
+ return InverseErf.value(1 - x);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfDifferenceTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfDifferenceTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfDifferenceTest.java
new file mode 100644
index 0000000..8e9afad
--- /dev/null
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfDifferenceTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.numbers.gamma;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link ErfDifference}.
+ */
+public class ErfDifferenceTest {
+ /**
+ * Test the implementation of ErfDifference.value(double, double) for consistency
+ * with results obtained from Erf.value(double) and Erfc.value(double).
+ */
+ @Test
+ public void testTwoArgumentErf() {
+ final double[] xi = new double[] { -2, -1, -0.9, -0.1, 0, 0.1, 0.9, 1, 2 };
+ for(double x1 : xi) {
+ for(double x2 : xi) {
+ double a = ErfDifference.value(x1, x2);
+ double b = Erf.value(x2) - Erf.value(x1);
+ double c = Erfc.value(x1) - Erfc.value(x2);
+ Assert.assertEquals(a, b, 1e-15);
+ Assert.assertEquals(a, c, 1e-15);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfTest.java
new file mode 100644
index 0000000..11fff47
--- /dev/null
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.numbers.gamma;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link Erf}.
+ */
+public class ErfTest {
+ @Test
+ public void testErf0() {
+ double actual = Erf.value(0);
+ double expected = 0;
+ Assert.assertEquals(expected, actual, 1e-15);
+ Assert.assertEquals(1 - expected, Erfc.value(0), 1e-15);
+ }
+
+ @Test
+ public void testErf1960() {
+ double x = 1.960 / Math.sqrt(2);
+ double actual = Erf.value(x);
+ double expected = 0.95;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(x), 1e-15);
+
+ actual = Erf.value(-x);
+ expected = -expected;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(-x), 1e-15);
+ }
+
+ @Test
+ public void testErf2576() {
+ double x = 2.576 / Math.sqrt(2);
+ double actual = Erf.value(x);
+ double expected = 0.99;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(x), 1e-15);
+
+ actual = Erf.value(-x);
+ expected = -expected;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(-x), 1e-15);
+ }
+
+ @Test
+ public void testErf2807() {
+ double x = 2.807 / Math.sqrt(2);
+ double actual = Erf.value(x);
+ double expected = 0.995;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(x), 1e-15);
+
+ actual = Erf.value(-x);
+ expected = -expected;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - actual, Erfc.value(-x), 1e-15);
+ }
+
+ @Test
+ public void testErf3291() {
+ double x = 3.291 / Math.sqrt(2);
+ double actual = Erf.value(x);
+ double expected = 0.999;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - expected, Erfc.value(x), 1e-5);
+
+ actual = Erf.value(-x);
+ expected = -expected;
+ Assert.assertEquals(expected, actual, 1e-5);
+ Assert.assertEquals(1 - expected, Erfc.value(-x), 1e-5);
+ }
+
+ /**
+ * MATH-301, MATH-456
+ */
+ @Test
+ public void testLargeValues() {
+ for (int i = 1; i < 200; i *= 10) {
+ double result = Erf.value(i);
+ Assert.assertFalse(Double.isNaN(result));
+ Assert.assertTrue(result > 0 && result <= 1);
+ result = Erf.value(-i);
+ Assert.assertFalse(Double.isNaN(result));
+ Assert.assertTrue(result >= -1 && result < 0);
+ result = Erfc.value(i);
+ Assert.assertFalse(Double.isNaN(result));
+ Assert.assertTrue(result >= 0 && result < 1);
+ result = Erfc.value(-i);
+ Assert.assertFalse(Double.isNaN(result));
+ Assert.assertTrue(result >= 1 && result <= 2);
+ }
+ Assert.assertEquals(-1, Erf.value(Double.NEGATIVE_INFINITY), 0);
+ Assert.assertEquals(1, Erf.value(Double.POSITIVE_INFINITY), 0);
+ Assert.assertEquals(2, Erfc.value(Double.NEGATIVE_INFINITY), 0);
+ Assert.assertEquals(0, Erfc.value(Double.POSITIVE_INFINITY), 0);
+ }
+
+ /**
+ * Compare Erf.value 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 = -10;
+ for (int i = 0; i < 41; i++) {
+ Assert.assertEquals(gnuValues[i], Erf.value(x), tol);
+ x += 0.5d;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfcTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfcTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfcTest.java
new file mode 100644
index 0000000..e68ee18
--- /dev/null
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/ErfcTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.numbers.gamma;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link Erfc}.
+ */
+public class ErfcTest {
+ /**
+ * Compare 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 = -10;
+ for (int i = 0; i < 41; i++) {
+ Assert.assertEquals(gnuValues[i], Erfc.value(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 * Erfc.value(ref[i][0] / Math.sqrt(2));
+ Assert.assertEquals(ref[i][1], result, 1e-15);
+ Assert.assertEquals(1, ref[i][1] / result, 1e-13);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
new file mode 100644
index 0000000..e22ee5e
--- /dev/null
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.numbers.gamma;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link InverseErf}.
+ */
+public class InverseErfTest {
+ @Test
+ public void testErfInvNaN() {
+ Assert.assertTrue(Double.isNaN(InverseErf.value(-1.001)));
+ Assert.assertTrue(Double.isNaN(InverseErf.value(+1.001)));
+ }
+
+ @Test
+ public void testErfInvInfinite() {
+ Assert.assertTrue(Double.isInfinite(InverseErf.value(-1)));
+ Assert.assertTrue(InverseErf.value(-1) < 0);
+ Assert.assertTrue(Double.isInfinite(InverseErf.value(+1)));
+ Assert.assertTrue(InverseErf.value(+1) > 0);
+ }
+
+ @Test
+ public void testErfInv() {
+ for (double x = -5.9; x < 5.9; x += 0.01) {
+ final double y = Erf.value(x);
+ final double dydx = 2 * Math.exp(-x * x) / Math.sqrt(Math.PI);
+ Assert.assertEquals(x, InverseErf.value(y), 1.0e-15 / dydx);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/4de442f6/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfcTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfcTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfcTest.java
new file mode 100644
index 0000000..79c5c11
--- /dev/null
+++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/InverseErfcTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.numbers.gamma;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link InverseErfc}.
+ */
+public class InverseErfcTest {
+ @Test
+ public void testErfcInvNaN() {
+ Assert.assertTrue(Double.isNaN(InverseErfc.value(-0.001)));
+ Assert.assertTrue(Double.isNaN(InverseErfc.value(+2.001)));
+ }
+
+ @Test
+ public void testErfcInvInfinite() {
+ Assert.assertTrue(Double.isInfinite(InverseErfc.value(-0)));
+ Assert.assertTrue(InverseErfc.value( 0) > 0);
+ Assert.assertTrue(Double.isInfinite(InverseErfc.value(+2)));
+ Assert.assertTrue(InverseErfc.value(+2) < 0);
+ }
+
+ @Test
+ public void testErfcInv() {
+ for (double x = -5.85; x < 5.9; x += 0.01) {
+ final double y = Erfc.value(x);
+ final double dydxAbs = 2 * Math.exp(-x * x) / Math.sqrt(Math.PI);
+ Assert.assertEquals(x, InverseErfc.value(y), 1.0e-15 / dydxAbs);
+ }
+ }
+}