View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.math.special;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.math.MathException;
21  
22  /**
23   * This is a utility class that provides computation methods related to the
24   * error functions.
25   *
26   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
27   */
28  public class Erf implements Serializable {
29  
30      /**
31       * Default constructor.  Prohibit instantiation.
32       */
33      private Erf() {
34          super();
35      }
36  
37      /**
38       * Returns the error function erf(x).
39       * 
40       * The implementation of this method is based on:
41       * <ul>
42       * <li>
43       * <a href="http://mathworld.wolfram.com/Erf.html">
44       * Erf</a>, equation (3).</li>
45       * </ul>
46       * 
47       * @param x the value.
48       * @return the error function erf(x)
49       * @throws MathException if the algorithm fails to converge.
50       */
51      public static double erf(double x) throws MathException {
52          double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
53          if (x < 0) {
54              ret = -ret;
55          }
56          return ret;
57      }
58  }