View Javadoc

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 java.io.Serializable;
20  
21  import org.apache.commons.math.MathException;
22  
23  /**
24   * This is a utility class that provides computation methods related to the
25   * error functions.
26   *
27   * @version $Revision: 506601 $ $Date: 2007-02-12 12:37:49 -0700 (Mon, 12 Feb 2007) $
28   */
29  public class Erf implements Serializable {
30  
31      /** Serializable version identifier */
32      private static final long serialVersionUID = 490960015010326571L;
33  
34      /**
35       * Default constructor.  Prohibit instantiation.
36       */
37      private Erf() {
38          super();
39      }
40  
41      /**
42       * Returns the error function erf(x).
43       * 
44       * The implementation of this method is based on:
45       * <ul>
46       * <li>
47       * <a href="http://mathworld.wolfram.com/Erf.html">
48       * Erf</a>, equation (3).</li>
49       * </ul>
50       * 
51       * @param x the value.
52       * @return the error function erf(x)
53       * @throws MathException if the algorithm fails to converge.
54       */
55      public static double erf(double x) throws MathException {
56          double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
57          if (x < 0) {
58              ret = -ret;
59          }
60          return ret;
61      }
62  }