All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.math3.special.Erf Maven / Gradle / Ivy

Go to download

The Apache Commons Math project is a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang.

There is a newer version: 3.6.1
Show newest version
/*
 * 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.math3.special;

import org.apache.commons.math3.util.FastMath;

/**
 * This is a utility class that provides computation methods related to the
 * error functions.
 *
 * @version $Id: Erf.java 1416643 2012-12-03 19:37:14Z tn $
 */
public class Erf {

    /**
     * The number {@code X_CRIT} is used by {@link #erf(double, double)} internally.
     * This number solves {@code erf(x)=0.5} within 1ulp.
     * More precisely, the current implementations of
     * {@link #erf(double)} and {@link #erfc(double)} satisfy:
* {@code erf(X_CRIT) < 0.5},
* {@code erf(Math.nextUp(X_CRIT) > 0.5},
* {@code erfc(X_CRIT) = 0.5}, and
* {@code erfc(Math.nextUp(X_CRIT) < 0.5} */ private static final double X_CRIT = 0.4769362762044697; /** * Default constructor. Prohibit instantiation. */ private Erf() {} /** * Returns the error function. * *

erf(x) = 2/√π 0x e-t2dt

* *

This implementation computes erf(x) using the * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function}, * following Erf, equation (3)

* *

The value returned is always between -1 and 1 (inclusive). * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from * either 1 or -1 as a double, so the appropriate extreme value is returned. *

* * @param x the value. * @return the error function erf(x) * @throws org.apache.commons.math3.exception.MaxCountExceededException * if the algorithm fails to converge. * @see Gamma#regularizedGammaP(double, double, double, int) */ public static double erf(double x) { if (FastMath.abs(x) > 40) { return x > 0 ? 1 : -1; } final double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000); return x < 0 ? -ret : ret; } /** * Returns the complementary error function. * *

erfc(x) = 2/√π x e-t2dt *
* = 1 - {@link #erf(double) erf(x)}

* *

This implementation computes erfc(x) using the * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function}, * following Erf, equation (3).

* *

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 as a double, so the appropriate extreme value is returned. *

* * @param x the value * @return the complementary error function erfc(x) * @throws org.apache.commons.math3.exception.MaxCountExceededException * if the algorithm fails to converge. * @see Gamma#regularizedGammaQ(double, double, double, int) * @since 2.2 */ public static double erfc(double x) { if (FastMath.abs(x) > 40) { return x > 0 ? 0 : 2; } final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000); return x < 0 ? 2 - ret : ret; } /** * Returns the difference between erf(x1) and erf(x2). * * The implementation uses either erf(double) or erfc(double) * depending on which provides the most precise result. * * @param x1 the first value * @param x2 the second value * @return erf(x2) - erf(x1) */ public static double erf(double x1, double x2) { if(x1 > x2) { return -erf(x2, x1); } return x1 < -X_CRIT ? x2 < 0.0 ? erfc(-x2) - erfc(-x1) : erf(x2) - erf(x1) : x2 > X_CRIT && x1 > 0.0 ? erfc(x1) - erfc(x2) : erf(x2) - erf(x1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy