• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.math.special;
18 
19 import org.apache.commons.math.MathException;
20 import org.apache.commons.math.util.FastMath;
21 
22 /**
23  * This is a utility class that provides computation methods related to the
24  * error functions.
25  *
26  * @version $Revision: 1054186 $ $Date: 2011-01-01 03:28:46 +0100 (sam. 01 janv. 2011) $
27  */
28 public class Erf {
29 
30     /**
31      * Default constructor.  Prohibit instantiation.
32      */
Erf()33     private Erf() {
34         super();
35     }
36 
37     /**
38      * <p>Returns the error function</p>
39      * <p>erf(x) = 2/&radic;&pi; <sub>0</sub>&int;<sup>x</sup> e<sup>-t<sup>2</sup></sup>dt </p>
40      *
41      * <p>This implementation computes erf(x) using the
42      * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function},
43      * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3)</p>
44      *
45      * <p>The value returned is always between -1 and 1 (inclusive).  If {@code abs(x) > 40}, then
46      * {@code erf(x)} is indistinguishable from either 1 or -1 as a double, so the appropriate extreme
47      * value is returned.</p>
48      *
49      * @param x the value.
50      * @return the error function erf(x)
51      * @throws MathException if the algorithm fails to converge.
52      * @see Gamma#regularizedGammaP(double, double, double, int)
53      */
erf(double x)54     public static double erf(double x) throws MathException {
55         if (FastMath.abs(x) > 40) {
56             return x > 0 ? 1 : -1;
57         }
58         double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
59         if (x < 0) {
60             ret = -ret;
61         }
62         return ret;
63     }
64 
65     /**
66      * <p>Returns the complementary error function</p>
67      * <p>erfc(x) = 2/&radic;&pi; <sub>x</sub>&int;<sup>&infin;</sup> e<sup>-t<sup>2</sup></sup>dt <br/>
68      *    = 1 - {@link #erf(double) erf(x)} </p>
69      *
70      * <p>This implementation computes erfc(x) using the
71      * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function},
72      * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3).</p>
73      *
74      * <p>The value returned is always between 0 and 2 (inclusive).  If {@code abs(x) > 40}, then
75      * {@code erf(x)} is indistinguishable from either 0 or 2 as a double, so the appropriate extreme
76      * value is returned.</p>
77      *
78      * @param x the value
79      * @return the complementary error function erfc(x)
80      * @throws MathException if the algorithm fails to converge
81      * @see Gamma#regularizedGammaQ(double, double, double, int)
82      * @since 2.2
83      */
erfc(double x)84     public static double erfc(double x) throws MathException {
85         if (FastMath.abs(x) > 40) {
86             return x > 0 ? 0 : 2;
87         }
88         final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000);
89         return x < 0 ? 2 - ret : ret;
90     }
91 }
92 
93