@@ -57,9 +57,9 @@ Accurate version of `erf(y) - erf(x)` (for real arguments only).
57
57
58
58
External links: [DLMF](https://dlmf.nist.gov/7.2.1), [Wikipedia](https://en.wikipedia.org/wiki/Error_function).
59
59
60
- See also: [`erfc(x)`](@ref SpecialFunctions. erfc), [`erfcx(x)`](@ref SpecialFunctions. erfcx),
61
- [`erfi(x)`](@ref SpecialFunctions. erfi), [`dawson(x)`](@ref SpecialFunctions. dawson),
62
- [`erfinv(x)`](@ref SpecialFunctions. erfinv), [`erfcinv(x)`](@ref SpecialFunctions. erfcinv).
60
+ See also: [`erfc(x)`](@ref erfc), [`erfcx(x)`](@ref erfcx),
61
+ [`erfi(x)`](@ref erfi), [`dawson(x)`](@ref dawson),
62
+ [`erfinv(x)`](@ref erfinv), [`erfcinv(x)`](@ref erfcinv).
63
63
64
64
# Implementation by
65
65
- `Float32`/`Float64`: C standard math library
@@ -97,7 +97,7 @@ This is the accurate version of `1-erf(x)` for large ``x``.
97
97
External links: [DLMF](https://dlmf.nist.gov/7.2.2),
98
98
[Wikipedia](https://en.wikipedia.org/wiki/Error_function#Complementary_error_function).
99
99
100
- See also: [`erf(x)`](@ref SpecialFunctions. erf).
100
+ See also: [`erf(x)`](@ref erf).
101
101
102
102
# Implementation by
103
103
- `Float32`/`Float64`: C standard math library
@@ -123,7 +123,7 @@ Note also that ``\operatorname{erfcx}(-ix)`` computes the Faddeeva function `w(x
123
123
External links: [DLMF](https://dlmf.nist.gov/7.2.3),
124
124
[Wikipedia](https://en.wikipedia.org/wiki/Error_function#Complementary_error_function).
125
125
126
- See also: [`erfc(x)`](@ref SpecialFunctions. erfc).
126
+ See also: [`erfc(x)`](@ref erfc).
127
127
128
128
# Implementation by
129
129
- `Float32`/`Float64`: C standard math library
@@ -147,7 +147,7 @@ Compute the imaginary error function of ``x``, defined by
147
147
External links:
148
148
[Wikipedia](https://en.wikipedia.org/wiki/Error_function#Imaginary_error_function).
149
149
150
- See also: [`erf(x)`](@ref SpecialFunctions. erf).
150
+ See also: [`erf(x)`](@ref erf).
151
151
152
152
# Implementation by
153
153
- `Float32`/`Float64`: C standard math library
@@ -172,7 +172,7 @@ for large ``x``.
172
172
External links: [DLMF](https://dlmf.nist.gov/7.2.5),
173
173
[Wikipedia](https://en.wikipedia.org/wiki/Dawson_function).
174
174
175
- See also: [`erfi(x)`](@ref SpecialFunctions. erfi).
175
+ See also: [`erfi(x)`](@ref erfi).
176
176
177
177
# Implementation by
178
178
- `Float32`/`Float64`: C standard math library
@@ -193,7 +193,7 @@ Compute the inverse error function of a real ``x``, that is
193
193
External links:
194
194
[Wikipedia](https://en.wikipedia.org/wiki/Error_function#Inverse_functions).
195
195
196
- See also: [`erf(x)`](@ref SpecialFunctions. erf).
196
+ See also: [`erf(x)`](@ref erf).
197
197
198
198
# Implementation
199
199
Using the rational approximants tabulated in:
@@ -202,6 +202,7 @@ Using the rational approximants tabulated in:
202
202
> Math. Comp. 30, pp. 827--830 (1976).
203
203
> <https://doi.org/10.1090/S0025-5718-1976-0421040-7>,
204
204
> <http://www.jstor.org/stable/2005402>
205
+ combined with Newton iterations for `BigFloat`.
205
206
"""
206
207
function erfinv (x:: Float64 )
207
208
a = abs (x)
@@ -314,7 +315,7 @@ function erfinv(x::Float32)
314
315
end
315
316
end
316
317
317
- erfinv (x:: Integer ) = erfinv (float (x))
318
+ erfinv (x:: Union{ Integer,Rational} ) = erfinv (float (x))
318
319
319
320
@doc raw """
320
321
erfcinv(x)
@@ -329,7 +330,7 @@ Compute the inverse error complementary function of a real ``x``, that is
329
330
External links:
330
331
[Wikipedia](https://en.wikipedia.org/wiki/Error_function#Inverse_functions).
331
332
332
- See also: [`erfc(x)`](@ref SpecialFunctions. erfc).
333
+ See also: [`erfc(x)`](@ref erfc).
333
334
334
335
# Implementation
335
336
Using the rational approximants tabulated in:
@@ -338,6 +339,7 @@ Using the rational approximants tabulated in:
338
339
> Math. Comp. 30, pp. 827--830 (1976).
339
340
> <https://doi.org/10.1090/S0025-5718-1976-0421040-7>,
340
341
> <http://www.jstor.org/stable/2005402>
342
+ combined with Newton iterations for `BigFloat`.
341
343
"""
342
344
function erfcinv (y:: Float64 )
343
345
if y > 0.0625
@@ -413,7 +415,55 @@ function erfcinv(y::Float32)
413
415
end
414
416
end
415
417
416
- erfcinv (x:: Integer ) = erfcinv (float (x))
418
+ function erfinv (y:: BigFloat )
419
+ xfloat = erfinv (Float64 (y))
420
+ sqrtπ = sqrt (big (pi ))
421
+ if isfinite (xfloat)
422
+ x = BigFloat (xfloat)
423
+ else
424
+ # Float64 overflowed, use asymptotic estimate instead
425
+ # from erfc(x) ≈ exp(-x²)/x√π ≈ y ⟹ -log(yπ) ≈ x² + log(x) ≈ x²
426
+ x = copysign (sqrt (- log ((1 - abs (y))* sqrtπ)), y)
427
+ end
428
+ isfinite (x) || return x
429
+ sqrtπhalf = sqrtπ * 0.5
430
+ tol = 2 eps (abs (x))
431
+ while true # Newton iterations
432
+ Δx = sqrtπhalf * (erf (x) - y) * exp (x^ 2 )
433
+ x -= Δx
434
+ abs (Δx) < tol && break
435
+ end
436
+ return x
437
+ end
438
+
439
+ function erfcinv (y:: BigFloat )
440
+ yfloat = Float64 (y)
441
+ xfloat = erfcinv (yfloat)
442
+ sqrtπ = sqrt (big (pi ))
443
+ if isfinite (xfloat)
444
+ x = BigFloat (xfloat)
445
+ else
446
+ # Float64 overflowed, use asymptotic estimate instead
447
+ # from erfc(x) ≈ exp(-x²)/x√π ≈ y ⟹ -log(yπ) ≈ x² + log(x) ≈ x²
448
+ if yfloat < 1
449
+ x = sqrt (- log (y* sqrtπ))
450
+ else # y must be close to 2
451
+ @show x = - sqrt (- log ((2 - y)* sqrtπ))
452
+ end
453
+ # TODO : Newton convergence is slow near y=0 singularity; accelerate?
454
+ end
455
+ isfinite (x) || return x
456
+ sqrtπhalf = sqrtπ * 0.5
457
+ tol = 2 eps (abs (x))
458
+ while true # Newton iterations
459
+ Δx = sqrtπhalf * (erfc (x) - y) * exp (x^ 2 )
460
+ x += Δx
461
+ abs (Δx) < tol && break
462
+ end
463
+ return x
464
+ end
465
+
466
+ erfcinv (x:: Union{Integer,Rational} ) = erfcinv (float (x))
417
467
418
468
# MPFR has an open TODO item for this function
419
469
# until then, we use [DLMF 7.12.1](https://dlmf.nist.gov/7.12.1) for the tail
@@ -457,10 +507,10 @@ This is the accurate version of ``\operatorname{ln}(\operatorname{erfc}(x))`` fo
457
507
458
508
External links: [Wikipedia](https://en.wikipedia.org/wiki/Error_function).
459
509
460
- See also: [`erfcx(x)`](@ref SpecialFunctions. erfcx).
510
+ See also: [`erfcx(x)`](@ref erfcx).
461
511
462
512
# Implementation
463
- Based on the [`erfc(x)`](@ref SpecialFunctions. erfc) and [`erfcx(x)`](@ref SpecialFunctions. erfcx) functions.
513
+ Based on the [`erfc(x)`](@ref erfc) and [`erfcx(x)`](@ref erfcx) functions.
464
514
Currently only implemented for `Float32`, `Float64`, and `BigFloat`.
465
515
"""
466
516
function logerfc (x:: Union{Float32, Float64, BigFloat} )
@@ -489,10 +539,10 @@ This is the accurate version of ``\operatorname{ln}(\operatorname{erfcx}(x))`` f
489
539
490
540
External links: [Wikipedia](https://en.wikipedia.org/wiki/Error_function).
491
541
492
- See also: [`erfcx(x)`](@ref SpecialFunctions. erfcx).
542
+ See also: [`erfcx(x)`](@ref erfcx).
493
543
494
544
# Implementation
495
- Based on the [`erfc(x)`](@ref SpecialFunctions. erfc) and [`erfcx(x)`](@ref SpecialFunctions. erfcx) functions.
545
+ Based on the [`erfc(x)`](@ref erfc) and [`erfcx(x)`](@ref erfcx) functions.
496
546
Currently only implemented for `Float32`, `Float64`, and `BigFloat`.
497
547
"""
498
548
function logerfcx (x:: Union{Float32, Float64, BigFloat} )
@@ -515,7 +565,7 @@ Compute the natural logarithm of two-argument error function. This is an accurat
515
565
516
566
External links: [Wikipedia](https://en.wikipedia.org/wiki/Error_function).
517
567
518
- See also: [`erf(x,y)`](@ref SpecialFunctions. erf).
568
+ See also: [`erf(x,y)`](@ref erf).
519
569
"""
520
570
function logerf (a:: Real , b:: Real )
521
571
if abs (a) ≤ 1 /√ 2 && abs (b) ≤ 1 /√ 2
0 commit comments