v / vlib / math / erf.v
374 lines · 316 sloc · 11.45 KB · 390efe46a1f46f302ae98c803b8ffbbb333fdb28
Raw
1module math
2
3/*
4* x
5 * 2 |\
6 * erf(x) = --------- | exp(-t*t)dt
7 * sqrt(pi) \|
8 * 0
9 *
10 * erfc(x) = 1-erf(x)
11 * Note that
12 * erf(-x) = -erf(x)
13 * erfc(-x) = 2 - erfc(x)
14 *
15 * Method:
16 * 1. For |x| in [0, 0.84375]
17 * erf(x) = x + x*R(x**2)
18 * erfc(x) = 1 - erf(x) if x in [-.84375,0.25]
19 * = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375]
20 * where R = P/Q where P is an odd poly of degree 8 and
21 * Q is an odd poly of degree 10.
22 * -57.90
23 * | R - (erf(x)-x)/x | <= 2
24 *
25 *
26 * Remark. The formula is derived by noting
27 * erf(x) = (2/sqrt(pi))*(x - x**3/3 + x**5/10 - x**7/42 + ....)
28 * and that
29 * 2/sqrt(pi) = 1.128379167095512573896158903121545171688
30 * is close to one. The interval is chosen because the fix
31 * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
32 * near 0.6174), and by some experiment, 0.84375 is chosen to
33 * guarantee the error is less than one ulp for erf.
34 *
35 * 2. For |x| in [0.84375,1.25], let s_ = |x| - 1, and
36 * c = 0.84506291151 rounded to single (24 bits)
37 * erf(x) = sign(x) * (c + P1(s_)/Q1(s_))
38 * erfc(x) = (1-c) - P1(s_)/Q1(s_) if x > 0
39 * 1+(c+P1(s_)/Q1(s_)) if x < 0
40 * |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
41 * Remark: here we use the taylor series expansion at x=1.
42 * erf(1+s_) = erf(1) + s_*Poly(s_)
43 * = 0.845.. + P1(s_)/Q1(s_)
44 * That is, we use rational approximation to approximate
45 * erf(1+s_) - (c = (single)0.84506291151)
46 * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
47 * where
48 * P1(s_) = degree 6 poly in s_
49 * Q1(s_) = degree 6 poly in s_
50 *
51 * 3. For x in [1.25,1/0.35(~2.857143)],
52 * erfc(x) = (1/x)*exp(-x*x-0.5625+R1/s1)
53 * erf(x) = 1 - erfc(x)
54 * where
55 * R1(z) = degree 7 poly in z, (z=1/x**2)
56 * s1(z) = degree 8 poly in z
57 *
58 * 4. For x in [1/0.35,28]
59 * erfc(x) = (1/x)*exp(-x*x-0.5625+R2/s2) if x > 0
60 * = 2.0 - (1/x)*exp(-x*x-0.5625+R2/s2) if -6<x<0
61 * = 2.0 - tiny (if x <= -6)
62 * erf(x) = sign(x)*(1.0 - erfc(x)) if x < 6, else
63 * erf(x) = sign(x)*(1.0 - tiny)
64 * where
65 * R2(z) = degree 6 poly in z, (z=1/x**2)
66 * s2(z) = degree 7 poly in z
67 *
68 * Note1:
69 * To compute exp(-x*x-0.5625+R/s), let s_ be a single
70 * precision number and s_ := x; then
71 * -x*x = -s_*s_ + (s_-x)*(s_+x)
72 * exp(-x*x-0.5626+R/s) =
73 * exp(-s_*s_-0.5625)*exp((s_-x)*(s_+x)+R/s);
74 * Note2:
75 * Here 4 and 5 make use of the asymptotic series
76 * exp(-x*x)
77 * erfc(x) ~ ---------- * ( 1 + Poly(1/x**2) )
78 * x*sqrt(pi)
79 * We use rational approximation to approximate
80 * g(s_)=f(1/x**2) = log(erfc(x)*x) - x*x + 0.5625
81 * Here is the error bound for R1/s1 and R2/s2
82 * |R1/s1 - f(x)| < 2**(-62.57)
83 * |R2/s2 - f(x)| < 2**(-61.52)
84 *
85 * 5. For inf > x >= 28
86 * erf(x) = sign(x) *(1 - tiny) (raise inexact)
87 * erfc(x) = tiny*tiny (raise underflow) if x > 0
88 * = 2 - tiny if x<0
89 *
90 * 7. special case:
91 * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
92 * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
93 * erfc/erf(nan) is nan
94*/
95const erx = 8.45062911510467529297e-01 // 0x3FEB0AC160000000
96
97// Coefficients for approximation to erf in [0, 0.84375]
98const efx = 1.28379167095512586316e-01 // 0x3FC06EBA8214DB69
99
100const efx8 = 1.02703333676410069053e+00 // 0x3FF06EBA8214DB69
101
102const pp0 = 1.28379167095512558561e-01 // 0x3FC06EBA8214DB68
103
104const pp1 = -3.25042107247001499370e-01 // 0xBFD4CD7D691CB913
105
106const pp2 = -2.84817495755985104766e-02 // 0xBF9D2A51DBD7194F
107
108const pp3 = -5.77027029648944159157e-03 // 0xBF77A291236668E4
109
110const pp4 = -2.37630166566501626084e-05 // 0xBEF8EAD6120016AC
111
112const qq1 = 3.97917223959155352819e-01 // 0x3FD97779CDDADC09
113
114const qq2 = 6.50222499887672944485e-02 // 0x3FB0A54C5536CEBA
115
116const qq3 = 5.08130628187576562776e-03 // 0x3F74D022C4D36B0F
117
118const qq4 = 1.32494738004321644526e-04 // 0x3F215DC9221C1A10
119
120const qq5 = -3.96022827877536812320e-06 // 0xBED09C4342A26120
121
122// Coefficients for approximation to erf in [0.84375, 1.25]
123const pa0 = -2.36211856075265944077e-03 // 0xBF6359B8BEF77538
124
125const pa1 = 4.14856118683748331666e-01 // 0x3FDA8D00AD92B34D
126
127const pa2 = -3.72207876035701323847e-01 // 0xBFD7D240FBB8C3F1
128
129const pa3 = 3.18346619901161753674e-01 // 0x3FD45FCA805120E4
130
131const pa4 = -1.10894694282396677476e-01 // 0xBFBC63983D3E28EC
132
133const pa5 = 3.54783043256182359371e-02 // 0x3FA22A36599795EB
134
135const pa6 = -2.16637559486879084300e-03 // 0xBF61BF380A96073F
136
137const qa1 = 1.06420880400844228286e-01 // 0x3FBB3E6618EEE323
138
139const qa2 = 5.40397917702171048937e-01 // 0x3FE14AF092EB6F33
140
141const qa3 = 7.18286544141962662868e-02 // 0x3FB2635CD99FE9A7
142
143const qa4 = 1.26171219808761642112e-01 // 0x3FC02660E763351F
144
145const qa5 = 1.36370839120290507362e-02 // 0x3F8BEDC26B51DD1C
146
147const qa6 = 1.19844998467991074170e-02 // 0x3F888B545735151D
148
149// Coefficients for approximation to erfc in [1.25, 1/0.35]
150const ra0 = -9.86494403484714822705e-03 // 0xBF843412600D6435
151
152const ra1 = -6.93858572707181764372e-01 // 0xBFE63416E4BA7360
153
154const ra2 = -1.05586262253232909814e+01 // 0xC0251E0441B0E726
155
156const ra3 = -6.23753324503260060396e+01 // 0xC04F300AE4CBA38D
157
158const ra4 = -1.62396669462573470355e+02 // 0xC0644CB184282266
159
160const ra5 = -1.84605092906711035994e+02 // 0xC067135CEBCCABB2
161
162const ra6 = -8.12874355063065934246e+01 // 0xC054526557E4D2F2
163
164const ra7 = -9.81432934416914548592e+00 // 0xC023A0EFC69AC25C
165
166const sa1 = 1.96512716674392571292e+01 // 0x4033A6B9BD707687
167
168const sa2 = 1.37657754143519042600e+02 // 0x4061350C526AE721
169
170const sa3 = 4.34565877475229228821e+02 // 0x407B290DD58A1A71
171
172const sa4 = 6.45387271733267880336e+02 // 0x40842B1921EC2868
173
174const sa5 = 4.29008140027567833386e+02 // 0x407AD02157700314
175
176const sa6 = 1.08635005541779435134e+02 // 0x405B28A3EE48AE2C
177
178const sa7 = 6.57024977031928170135e+00 // 0x401A47EF8E484A93
179
180const sa8 = -6.04244152148580987438e-02 // 0xBFAEEFF2EE749A62
181
182// Coefficients for approximation to erfc in [1/.35, 28]
183const rb0 = -9.86494292470009928597e-03 // 0xBF84341239E86F4A
184
185const rb1 = -7.99283237680523006574e-01 // 0xBFE993BA70C285DE
186
187const rb2 = -1.77579549177547519889e+01 // 0xC031C209555F995A
188
189const rb3 = -1.60636384855821916062e+02 // 0xC064145D43C5ED98
190
191const rb4 = -6.37566443368389627722e+02 // 0xC083EC881375F228
192
193const rb5 = -1.02509513161107724954e+03 // 0xC09004616A2E5992
194
195const rb6 = -4.83519191608651397019e+02 // 0xC07E384E9BDC383F
196
197const sb1 = 3.03380607434824582924e+01 // 0x403E568B261D5190
198
199const sb2 = 3.25792512996573918826e+02 // 0x40745CAE221B9F0A
200
201const sb3 = 1.53672958608443695994e+03 // 0x409802EB189D5118
202
203const sb4 = 3.19985821950859553908e+03 // 0x40A8FFB7688C246A
204
205const sb5 = 2.55305040643316442583e+03 // 0x40A3F219CEDF3BE6
206
207const sb6 = 4.74528541206955367215e+02 // 0x407DA874E79FE763
208
209const sb7 = -2.24409524465858183362e+01
210
211// erf returns the error function of x.
212//
213// special cases are:
214// erf(+inf) = 1
215// erf(-inf) = -1
216// erf(nan) = nan
217pub fn erf(a f64) f64 {
218 mut x := a
219 very_tiny := 2.848094538889218e-306 // 0x0080000000000000
220 small_ := 1.0 / f64(u64(1) << 28) // 2**-28
221 if is_nan(x) {
222 return nan()
223 }
224 if is_inf(x, 1) {
225 return 1.0
226 }
227 if is_inf(x, -1) {
228 return f64(-1)
229 }
230 mut neg := false
231 if x < 0 {
232 x = -x
233 neg = true
234 }
235 if x < 0.84375 { // |x| < 0.84375
236 mut temp := 0.0
237 if x < small_ { // |x| < 2**-28
238 if x < very_tiny {
239 temp = 0.125 * (8.0 * x + efx8 * x) // avoid underflow
240 } else {
241 temp = x + efx * x
242 }
243 } else {
244 z := x * x
245 r := pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4)))
246 s_ := 1.0 + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))))
247 y := r / s_
248 temp = x + x * y
249 }
250 if neg {
251 return -temp
252 }
253 return temp
254 }
255 if x < 1.25 { // 0.84375 <= |x| < 1.25
256 s_ := x - 1
257 p := pa0 + s_ * (pa1 + s_ * (pa2 + s_ * (pa3 + s_ * (pa4 + s_ * (pa5 + s_ * pa6)))))
258 q := 1.0 + s_ * (qa1 + s_ * (qa2 + s_ * (qa3 + s_ * (qa4 + s_ * (qa5 + s_ * qa6)))))
259 if neg {
260 return -erx - p / q
261 }
262 return erx + p / q
263 }
264 if x >= 6 { // inf > |x| >= 6
265 if neg {
266 return -1
267 }
268 return 1.0
269 }
270 s_ := 1.0 / (x * x)
271 mut r := 0.0
272 mut s := 0.0
273 if x < 1.0 / 0.35 { // |x| < 1 / 0.35 ~ 2.857143
274 tmp41 := s_ * (ra5 + s_ * (ra6 + s_ * ra7))
275 r = ra0 + s_ * (ra1 + s_ * (ra2 + s_ * (ra3 + s_ * (ra4 + tmp41))))
276 tmp42 := s_ * (sa5 + s_ * (sa6 + s_ * (sa7 + s_ * sa8)))
277 s = 1.0 + s_ * (sa1 + s_ * (sa2 + s_ * (sa3 + s_ * (sa4 + tmp42))))
278 } else { // |x| >= 1 / 0.35 ~ 2.857143
279 tmp31 := rb4 + s_ * (rb5 + s_ * rb6)
280 r = rb0 + s_ * (rb1 + s_ * (rb2 + s_ * (rb3 + s_ * tmp31)))
281 tmp32 := sb4 + s_ * (sb5 + s_ * (sb6 + s_ * sb7))
282 s = 1.0 + s_ * (sb1 + s_ * (sb2 + s_ * (sb3 + s_ * tmp32)))
283 }
284 z := f64_from_bits(f64_bits(x) & 0xffffffff00000000) // pseudo-single (20-bit) precision x
285 r_ := exp(-z * z - 0.5625) * exp((z - x) * (z + x) + r / s)
286 if neg {
287 return r_ / x - 1.0
288 }
289 return 1.0 - r_ / x
290}
291
292// erfc returns the complementary error function of x.
293//
294// special cases are:
295// erfc(+inf) = 0
296// erfc(-inf) = 2
297// erfc(nan) = nan
298pub fn erfc(a f64) f64 {
299 mut x := a
300 tiny := 1.0 / f64(u64(1) << 56) // 2**-56
301 // special cases
302 if is_nan(x) {
303 return nan()
304 }
305 if is_inf(x, 1) {
306 return 0.0
307 }
308 if is_inf(x, -1) {
309 return 2.0
310 }
311 mut neg := false
312 if x < 0 {
313 x = -x
314 neg = true
315 }
316 if x < 0.84375 { // |x| < 0.84375
317 mut temp := 0.0
318 if x < tiny { // |x| < 2**-56
319 temp = x
320 } else {
321 z := x * x
322 r := pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4)))
323 s_ := 1.0 + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))))
324 y := r / s_
325 if x < 0.25 { // |x| < 1.0/4
326 temp = x + x * y
327 } else {
328 temp = 0.5 + (x * y + (x - 0.5))
329 }
330 }
331 if neg {
332 return 1.0 + temp
333 }
334 return 1.0 - temp
335 }
336 if x < 1.25 { // 0.84375 <= |x| < 1.25
337 s_ := x - 1
338 p := pa0 + s_ * (pa1 + s_ * (pa2 + s_ * (pa3 + s_ * (pa4 + s_ * (pa5 + s_ * pa6)))))
339 q := 1.0 + s_ * (qa1 + s_ * (qa2 + s_ * (qa3 + s_ * (qa4 + s_ * (qa5 + s_ * qa6)))))
340 if neg {
341 return 1.0 + erx + p / q
342 }
343 return 1.0 - erx - p / q
344 }
345 if x < 28 { // |x| < 28
346 s_ := 1.0 / (x * x)
347 mut r := 0.0
348 mut s := 0.0
349 if x < 1.0 / 0.35 { // |x| < 1 / 0.35 ~ 2.857143
350 tmp281 := ra4 + s_ * (ra5 + s_ * (ra6 + s_ * ra7))
351 r = ra0 + s_ * (ra1 + s_ * (ra2 + s_ * (ra3 + s_ * tmp281)))
352 tmp282 := sa4 + s_ * (sa5 + s_ * (sa6 + s_ * (sa7 + s_ * sa8)))
353 s = 1.0 + s_ * (sa1 + s_ * (sa2 + s_ * (sa3 + s_ * tmp282)))
354 } else { // |x| >= 1 / 0.35 ~ 2.857143
355 if neg && x > 6 {
356 return 2.0 // x < -6
357 }
358 tmp291 := rb3 + s_ * (rb4 + s_ * (rb5 + s_ * rb6))
359 r = rb0 + s_ * (rb1 + s_ * (rb2 + s_ * tmp291))
360 tmp292 := sb3 + s_ * (sb4 + s_ * (sb5 + s_ * (sb6 + s_ * sb7)))
361 s = 1.0 + s_ * (sb1 + s_ * (sb2 + s_ * tmp292))
362 }
363 z := f64_from_bits(f64_bits(x) & 0xffffffff00000000) // pseudo-single (20-bit) precision x
364 r_ := exp(-z * z - 0.5625) * exp((z - x) * (z + x) + r / s)
365 if neg {
366 return 2.0 - r_ / x
367 }
368 return r_ / x
369 }
370 if neg {
371 return 2.0
372 }
373 return 0.0
374}
375