    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.math.special;
    018
    019    import org.apache.commons.math.MathException;
    020    import org.apache.commons.math.util.FastMath;
    021
    022    /**
    023     * This is a utility class that provides computation methods related to the
    024     * error functions.
    025     *
    026     * @version $Revision: 1054186 $ $Date: 2011-01-01 03:28:46 +0100 (sam. 01 janv. 2011) $
    027     */
    028    public class Erf {
    029
    030        /**
    031         * Default constructor.  Prohibit instantiation.
    032         */
    033        private Erf() {
    034            super();
    035        }
    036
    037        /**
    038         * <p>Returns the error function</p>
    039         * <p>erf(x) = 2/&radic;&pi; <sub>0</sub>&int;<sup>x</sup> e<sup>-t<sup>2</sup></sup>dt </p>
    040         *
    041         * <p>This implementation computes erf(x) using the
    042         * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function},
    043         * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3)</p>
    044         *
    045         * <p>The value returned is always between -1 and 1 (inclusive).  If {@code abs(x) > 40}, then
    046         * {@code erf(x)} is indistinguishable from either 1 or -1 as a double, so the appropriate extreme
    047         * value is returned.</p>
    048         *
    049         * @param x the value.
    050         * @return the error function erf(x)
    051         * @throws MathException if the algorithm fails to converge.
    052         * @see Gamma#regularizedGammaP(double, double, double, int)
    053         */
    054        public static double erf(double x) throws MathException {
    055            if (FastMath.abs(x) > 40) {
    056                return x > 0 ? 1 : -1;
    057            }
    058            double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000);
    059            if (x < 0) {
    060                ret = -ret;
    061            }
    062            return ret;
    063        }
    064
    065        /**
    066         * <p>Returns the complementary error function</p>
    067         * <p>erfc(x) = 2/&radic;&pi; <sub>x</sub>&int;<sup>&infin;</sup> e<sup>-t<sup>2</sup></sup>dt <br/>
    068         *    = 1 - {@link #erf(double) erf(x)} </p>
    069         *
    070         * <p>This implementation computes erfc(x) using the
    071         * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function},
    072         * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3).</p>
    073         *
    074         * <p>The value returned is always between 0 and 2 (inclusive).  If {@code abs(x) > 40}, then
    075         * {@code erf(x)} is indistinguishable from either 0 or 2 as a double, so the appropriate extreme
    076         * value is returned.</p>
    077         *
    078         * @param x the value
    079         * @return the complementary error function erfc(x)
    080         * @throws MathException if the algorithm fails to converge
    081         * @see Gamma#regularizedGammaQ(double, double, double, int)
    082         * @since 2.2
    083         */
    084        public static double erfc(double x) throws MathException {
    085            if (FastMath.abs(x) > 40) {
    086                return x > 0 ? 0 : 2;
    087            }
    088            final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000);
    089            return x < 0 ? 2 - ret : ret;
    090        }
    091    }
    092