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 */ 017package org.apache.commons.math3.special; 018 019import org.apache.commons.math3.util.FastMath; 020 021/** 022 * This is a utility class that provides computation methods related to the 023 * error functions. 024 * 025 */ 026public class Erf { 027 028 /** 029 * The number {@code X_CRIT} is used by {@link #erf(double, double)} internally. 030 * This number solves {@code erf(x)=0.5} within 1ulp. 031 * More precisely, the current implementations of 032 * {@link #erf(double)} and {@link #erfc(double)} satisfy:<br/> 033 * {@code erf(X_CRIT) < 0.5},<br/> 034 * {@code erf(Math.nextUp(X_CRIT) > 0.5},<br/> 035 * {@code erfc(X_CRIT) = 0.5}, and<br/> 036 * {@code erfc(Math.nextUp(X_CRIT) < 0.5} 037 */ 038 private static final double X_CRIT = 0.4769362762044697; 039 040 /** 041 * Default constructor. Prohibit instantiation. 042 */ 043 private Erf() {} 044 045 /** 046 * Returns the error function. 047 * 048 * <p>erf(x) = 2/√π <sub>0</sub>∫<sup>x</sup> e<sup>-t<sup>2</sup></sup>dt </p> 049 * 050 * <p>This implementation computes erf(x) using the 051 * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function}, 052 * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3)</p> 053 * 054 * <p>The value returned is always between -1 and 1 (inclusive). 055 * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from 056 * either 1 or -1 as a double, so the appropriate extreme value is returned. 057 * </p> 058 * 059 * @param x the value. 060 * @return the error function erf(x) 061 * @throws org.apache.commons.math3.exception.MaxCountExceededException 062 * if the algorithm fails to converge. 063 * @see Gamma#regularizedGammaP(double, double, double, int) 064 */ 065 public static double erf(double x) { 066 if (FastMath.abs(x) > 40) { 067 return x > 0 ? 1 : -1; 068 } 069 final double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000); 070 return x < 0 ? -ret : ret; 071 } 072 073 /** 074 * Returns the complementary error function. 075 * 076 * <p>erfc(x) = 2/√π <sub>x</sub>∫<sup>∞</sup> e<sup>-t<sup>2</sup></sup>dt 077 * <br/> 078 * = 1 - {@link #erf(double) erf(x)} </p> 079 * 080 * <p>This implementation computes erfc(x) using the 081 * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function}, 082 * following <a href="http://mathworld.wolfram.com/Erf.html"> Erf</a>, equation (3).</p> 083 * 084 * <p>The value returned is always between 0 and 2 (inclusive). 085 * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from 086 * either 0 or 2 as a double, so the appropriate extreme value is returned. 087 * </p> 088 * 089 * @param x the value 090 * @return the complementary error function erfc(x) 091 * @throws org.apache.commons.math3.exception.MaxCountExceededException 092 * if the algorithm fails to converge. 093 * @see Gamma#regularizedGammaQ(double, double, double, int) 094 * @since 2.2 095 */ 096 public static double erfc(double x) { 097 if (FastMath.abs(x) > 40) { 098 return x > 0 ? 0 : 2; 099 } 100 final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000); 101 return x < 0 ? 2 - ret : ret; 102 } 103 104 /** 105 * Returns the difference between erf(x1) and erf(x2). 106 * 107 * The implementation uses either erf(double) or erfc(double) 108 * depending on which provides the most precise result. 109 * 110 * @param x1 the first value 111 * @param x2 the second value 112 * @return erf(x2) - erf(x1) 113 */ 114 public static double erf(double x1, double x2) { 115 if(x1 > x2) { 116 return -erf(x2, x1); 117 } 118 119 return 120 x1 < -X_CRIT ? 121 x2 < 0.0 ? 122 erfc(-x2) - erfc(-x1) : 123 erf(x2) - erf(x1) : 124 x2 > X_CRIT && x1 > 0.0 ? 125 erfc(x1) - erfc(x2) : 126 erf(x2) - erf(x1); 127 } 128 129 /** 130 * Returns the inverse erf. 131 * <p> 132 * This implementation is described in the paper: 133 * <a href="http://people.maths.ox.ac.uk/gilesm/files/gems_erfinv.pdf">Approximating 134 * the erfinv function</a> by Mike Giles, Oxford-Man Institute of Quantitative Finance, 135 * which was published in GPU Computing Gems, volume 2, 2010. 136 * The source code is available <a href="http://gpucomputing.net/?q=node/1828">here</a>. 137 * </p> 138 * @param x the value 139 * @return t such that x = erf(t) 140 * @since 3.2 141 */ 142 public static double erfInv(final double x) { 143 144 // beware that the logarithm argument must be 145 // commputed as (1.0 - x) * (1.0 + x), 146 // it must NOT be simplified as 1.0 - x * x as this 147 // would induce rounding errors near the boundaries +/-1 148 double w = - FastMath.log((1.0 - x) * (1.0 + x)); 149 double p; 150 151 if (w < 6.25) { 152 w -= 3.125; 153 p = -3.6444120640178196996e-21; 154 p = -1.685059138182016589e-19 + p * w; 155 p = 1.2858480715256400167e-18 + p * w; 156 p = 1.115787767802518096e-17 + p * w; 157 p = -1.333171662854620906e-16 + p * w; 158 p = 2.0972767875968561637e-17 + p * w; 159 p = 6.6376381343583238325e-15 + p * w; 160 p = -4.0545662729752068639e-14 + p * w; 161 p = -8.1519341976054721522e-14 + p * w; 162 p = 2.6335093153082322977e-12 + p * w; 163 p = -1.2975133253453532498e-11 + p * w; 164 p = -5.4154120542946279317e-11 + p * w; 165 p = 1.051212273321532285e-09 + p * w; 166 p = -4.1126339803469836976e-09 + p * w; 167 p = -2.9070369957882005086e-08 + p * w; 168 p = 4.2347877827932403518e-07 + p * w; 169 p = -1.3654692000834678645e-06 + p * w; 170 p = -1.3882523362786468719e-05 + p * w; 171 p = 0.0001867342080340571352 + p * w; 172 p = -0.00074070253416626697512 + p * w; 173 p = -0.0060336708714301490533 + p * w; 174 p = 0.24015818242558961693 + p * w; 175 p = 1.6536545626831027356 + p * w; 176 } else if (w < 16.0) { 177 w = FastMath.sqrt(w) - 3.25; 178 p = 2.2137376921775787049e-09; 179 p = 9.0756561938885390979e-08 + p * w; 180 p = -2.7517406297064545428e-07 + p * w; 181 p = 1.8239629214389227755e-08 + p * w; 182 p = 1.5027403968909827627e-06 + p * w; 183 p = -4.013867526981545969e-06 + p * w; 184 p = 2.9234449089955446044e-06 + p * w; 185 p = 1.2475304481671778723e-05 + p * w; 186 p = -4.7318229009055733981e-05 + p * w; 187 p = 6.8284851459573175448e-05 + p * w; 188 p = 2.4031110387097893999e-05 + p * w; 189 p = -0.0003550375203628474796 + p * w; 190 p = 0.00095328937973738049703 + p * w; 191 p = -0.0016882755560235047313 + p * w; 192 p = 0.0024914420961078508066 + p * w; 193 p = -0.0037512085075692412107 + p * w; 194 p = 0.005370914553590063617 + p * w; 195 p = 1.0052589676941592334 + p * w; 196 p = 3.0838856104922207635 + p * w; 197 } else if (!Double.isInfinite(w)) { 198 w = FastMath.sqrt(w) - 5.0; 199 p = -2.7109920616438573243e-11; 200 p = -2.5556418169965252055e-10 + p * w; 201 p = 1.5076572693500548083e-09 + p * w; 202 p = -3.7894654401267369937e-09 + p * w; 203 p = 7.6157012080783393804e-09 + p * w; 204 p = -1.4960026627149240478e-08 + p * w; 205 p = 2.9147953450901080826e-08 + p * w; 206 p = -6.7711997758452339498e-08 + p * w; 207 p = 2.2900482228026654717e-07 + p * w; 208 p = -9.9298272942317002539e-07 + p * w; 209 p = 4.5260625972231537039e-06 + p * w; 210 p = -1.9681778105531670567e-05 + p * w; 211 p = 7.5995277030017761139e-05 + p * w; 212 p = -0.00021503011930044477347 + p * w; 213 p = -0.00013871931833623122026 + p * w; 214 p = 1.0103004648645343977 + p * w; 215 p = 4.8499064014085844221 + p * w; 216 } else { 217 // this branch does not appears in the original code, it 218 // was added because the previous branch does not handle 219 // x = +/-1 correctly. In this case, w is positive infinity 220 // and as the first coefficient (-2.71e-11) is negative. 221 // Once the first multiplication is done, p becomes negative 222 // infinity and remains so throughout the polynomial evaluation. 223 // So the branch above incorrectly returns negative infinity 224 // instead of the correct positive infinity. 225 p = Double.POSITIVE_INFINITY; 226 } 227 228 return p * x; 229 230 } 231 232 /** 233 * Returns the inverse erfc. 234 * @param x the value 235 * @return t such that x = erfc(t) 236 * @since 3.2 237 */ 238 public static double erfcInv(final double x) { 239 return erfInv(1 - x); 240 } 241 242} 243