192
192
"""
193
193
stirling(x)
194
194
195
- Compute log(gamma(x)) - (x-0.5)*log(x) + x - log(2pi)/2 using stirling series for asymptotic part
195
+ Compute `` \\ ln{ \\ Gamma(x)} - (x-0.5)*\\ ln{x} + x - \\ ln{(2 \\ pi)}/2`` using stirling series for asymptotic part
196
196
in https://dlmf.nist.gov/5.11.1
197
197
"""
198
198
function stirling (x:: Float64 )
219
219
"""
220
220
gammax(x)
221
221
222
- gammax(x) = ``e^{stirling(x)}`` if x>0
223
- else ``\\ Gamma(x)/(e^{-x + (x-0.5)*log(x)}/\\ sqrt{2 \\ pi}``
222
+ ` gammax(x)` = ``e^{stirling(x)}`` if ` x>0`,
223
+ else ``\\ Gamma(x)/(e^{-x + (x-0.5)*log(x)}/\\ sqrt{2 \\ pi}``.
224
224
"""
225
225
function gammax (x:: Float64 )
226
226
if x >= 3
234
234
"""
235
235
lambdaeta(eta)
236
236
237
- Function to compute the value of eta satisfying ``eta^{2}/2 = \\ lambda-1-log( \\ lambda) ``
237
+ Function to compute the value of eta satisfying ``eta^{2}/2 = \\ lambda-1-\\ ln{ \\ lambda} ``
238
238
"""
239
239
function lambdaeta (eta:: Float64 )
240
240
s = eta* eta* 0.5
@@ -383,12 +383,15 @@ end
383
383
"""
384
384
gamma_inc_taylor(a, x, ind)
385
385
386
- Compute P(a,x) using Taylor Series for P/R given by :
386
+ Compute `` P(a,x)`` using Taylor Series for P/R given by :
387
387
```math
388
- R(a,x)/a * (1 + \\ sum_{1}^{\\ infty} x^{n}/((a+1)(a+2)...(a+n)))
388
+ R(a,x)/a * (1 + \\ sum_{n= 1}^{\\ infty} x^{n}/((a+1)(a+2)...(a+n)))
389
389
```
390
- Used when 1 <= a <= BIG and x <= max{a, ln 10}.
391
- DLMF : https://dlmf.nist.gov/8.11#E2
390
+ Used when `1 <= a <= BIG` and `x <= max{a, ln 10}`.
391
+
392
+ External links: [DLMF](https://dlmf.nist.gov/8.11#E2)
393
+
394
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
392
395
"""
393
396
function gamma_inc_taylor (a:: Float64 , x:: Float64 , ind:: Integer )
394
397
acc = acc0[ind + 1 ]
@@ -430,12 +433,15 @@ end
430
433
"""
431
434
gamma_inc_asym(a, x, ind)
432
435
433
- Compute P(a,x) using asymptotic expansion given by :
436
+ Compute `` P(a,x)`` using asymptotic expansion given by:
434
437
```math
435
- R(a,x)/a * (1 + \\ sum_{1}^{N-1}(a_{n}/x^{n} + \\ Theta _{n}a_{n}/x^{n}))
438
+ R(a,x)/a * (1 + \\ sum_{n= 1}^{N-1}(a_{n}/x^{n} + \\ Theta _{n}a_{n}/x^{n}))
436
439
```
437
- where R(a,x) = rgammax(a,x). Used when 1 <= a <= BIG and x >= x0.
438
- DLMF : https://dlmf.nist.gov/8.11#E2
440
+ where `R(a,x) = rgammax(a,x)`. Used when `1 <= a <= BIG` and `x >= x0`.
441
+
442
+ External links: [DLMF](https://dlmf.nist.gov/8.11#E2)
443
+
444
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
439
445
"""
440
446
function gamma_inc_asym (a:: Float64 , x:: Float64 , ind:: Integer )
441
447
wk = zeros (30 )
@@ -476,16 +482,18 @@ end
476
482
"""
477
483
gamma_inc_taylor_x(a,x,ind)
478
484
479
- Computes P(a,x) based on Taylor expansion of P(a,x)/x**a given by:
485
+ Computes `` P(a,x)`` based on Taylor expansion of `` P(a,x)/x**a`` given by:
480
486
```math
481
487
J = -a * \\ sum_{1}^{\\ infty} (-x)^{n}/((a+n)n!)
482
488
```
483
- and P(a,x)/x**a is given by :
489
+ and `` P(a,x)/x**a`` is given by :
484
490
```math
485
491
(1 - J)/ \\ Gamma(a+1)
486
492
```
487
- resulting from term-by-term integration of gamma_inc(a,x,ind).
488
- This is used when a < 1 and x < 1.1 - Refer Eqn (9) in the paper.
493
+ resulting from term-by-term integration of `gamma_inc(a,x,ind)`.
494
+ This is used when `a < 1` and `x < 1.1` - Refer Eqn (9) in the paper.
495
+
496
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
489
497
"""
490
498
function gamma_inc_taylor_x (a:: Float64 , x:: Float64 , ind:: Integer )
491
499
acc = acc0[ind + 1 ]
@@ -520,16 +528,20 @@ end
520
528
"""
521
529
gamma_inc_minimax(a,x,z)
522
530
523
- Compute P(a,x) using minimax approximations given by :
531
+ Compute `` P(a,x)`` using minimax approximations given by :
524
532
```math
525
533
1/2 * erfc(\\ sqrt{y}) - e^{-y}/\\ sqrt{2\\ pi*a}* T(a,\\ lambda)
526
534
``` where
527
535
```math
528
536
T(a,\\ lambda) = \\ sum_{0}^{N} c_{k}(z)a^{-k}
529
537
```
530
- DLMF : https://dlmf.nist.gov/8.12#E8
531
- This is a higher accuracy approximation of Temme expansion, which deals with the region near a ≈ x with a large.
538
+
539
+ This is a higher accuracy approximation of Temme expansion, which deals with the region near ` a ≈ x` with `a` large.
532
540
Refer Appendix F in the paper for the extensive set of coefficients calculated using Brent's multiple precision arithmetic(set at 50 digits) in BRENT, R. P. A FORTRAN multiple-precision arithmetic package, ACM Trans. Math. Softw. 4(1978), 57-70 .
541
+
542
+ External links: [DLMF](https://dlmf.nist.gov/8.12#E8)
543
+
544
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
533
545
"""
534
546
function gamma_inc_minimax (a:: Float64 , x:: Float64 , z:: Float64 )
535
547
l = x/ a
@@ -579,15 +591,18 @@ end
579
591
"""
580
592
gamma_inc_temme(a, x, z)
581
593
582
- Compute P(a,x) using Temme's expansion given by :
594
+ Compute `` P(a,x)`` using Temme's expansion given by :
583
595
```math
584
596
1/2 * erfc(\\ sqrt{y}) - e^{-y}/\\ sqrt{2\\ pi*a}* T(a,\\ lambda)
585
597
``` where
586
598
```math
587
599
T(a,\\ lambda) = \\ sum_{0}^{N} c_{k}(z)a^{-k}
588
600
```
589
- DLMF : https://dlmf.nist.gov/8.12#E8
590
- This mainly solves the problem near the region when a ≈ x with a large, and is a lower accuracy version of the minimax approximation.
601
+ This mainly solves the problem near the region when `a ≈ x` with a large, and is a lower accuracy version of the minimax approximation.
602
+
603
+ External links: [DLMF](https://dlmf.nist.gov/8.12#E8)
604
+
605
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
591
606
"""
592
607
function gamma_inc_temme (a:: Float64 , x:: Float64 , z:: Float64 )
593
608
l = x/ a
@@ -609,15 +624,17 @@ end
609
624
"""
610
625
gamma_inc_temme_1(a, x, z, ind)
611
626
612
- Computes P(a,x) using simplified Temme expansion near y=0 by :
627
+ Computes `` P(a,x)`` using simplified Temme expansion near `` y=0`` by :
613
628
```math
614
629
E(y) - (1 - y)/\\ sqrt{2\\ pi*a} * T(a,\\ lambda)
615
- ``` where
630
+ ```
631
+ where
616
632
```math
617
633
E(y) = 1/2 - (1 - y/3)*(\\ sqrt(y/\\ pi))
618
634
```
619
635
Used instead of it's previous function when ``\\ sigma <= e_{0}/\\ sqrt{a}``.
620
- DLMF : https://dlmf.nist.gov/8.12#E8
636
+
637
+ External links: [DLMF](https://dlmf.nist.gov/8.12#E8)
621
638
"""
622
639
function gamma_inc_temme_1 (a:: Float64 , x:: Float64 , z:: Float64 , ind:: Integer )
623
640
iop = ind + 1
660
677
"""
661
678
gamma_inc_fsum(a,x)
662
679
663
- Compute using Finite Sums for Q(a,x) when a >= 1 && 2a is integer
664
- Used when a <= x <= x0 and a = n/2.
680
+ Compute using Finite Sums for `` Q(a,x)`` when ` a >= 1 && 2a` is integer.
681
+ Used when ` a <= x <= x0` and ` a = n/2` .
665
682
Refer Eqn (14) in the paper.
683
+
684
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
666
685
"""
667
686
function gamma_inc_fsum (a:: Float64 , x:: Float64 )
668
687
if isinteger (a)
@@ -694,13 +713,13 @@ end
694
713
"""
695
714
gamma_inc_inv_psmall(a,p)
696
715
697
- Compute x0 - initial approximation when `p` is small.
716
+ Compute `x0` - initial approximation when `p` is small.
698
717
Here we invert the series in Eqn (2.20) in the paper and write the inversion problem as:
699
718
```math
700
- x = r( 1 + \\ sum_{1}^{\\ infty}a(-1 )^{n}x^{n}/ (a+n)n!) ^{-1/a}
719
+ x = r\\ left[ 1 + a \\ sum_{k= 1}^{\\ infty}\\ frac{(-x )^{n}}{ (a+n)n!} \\ right] ^{-1/a},
701
720
```
702
721
where ``r = (p\\ Gamma(1+a))^{1/a}``
703
- Inverting this relation we obtain ``x = r + \\ sum_{2}^{\\ infty}c_{k}r^{k}``
722
+ Inverting this relation we obtain ``x = r + \\ sum_{k= 2}^{\\ infty}c_{k}r^{k}``.
704
723
"""
705
724
function gamma_inc_inv_psmall (a:: Float64 , p:: Float64 )
706
725
logr = (1.0 / a)* (log (p) + logabsgamma (a + 1.0 )[1 ])
@@ -722,12 +741,12 @@ end
722
741
"""
723
742
gamma_inc_inv_qsmall(a,q)
724
743
725
- Compute x0 - initial approximation when `q` is small from ``e^{-x_{0}} x_{0}^{a} = q \\ Gamma(a)``.
744
+ Compute `x0` - initial approximation when `q` is small from ``e^{-x_{0}} x_{0}^{a} = q \\ Gamma(a)``.
726
745
Asymptotic expansions Eqn (2.29) in the paper is used here and higher approximations are obtained using
727
746
```math
728
- x \\ sim x_{0} - L + b* \\ sum_{1}^{\\ infty} d_{k}/x_{0}^{k}
747
+ x \\ sim x_{0} - L + b \\ sum_{k= 1}^{\\ infty} d_{k}/x_{0}^{k}
729
748
```
730
- where b = 1-a, L = ln(x0)
749
+ where `` b = 1-a``, `` L = \\ ln{x_0}``.
731
750
"""
732
751
function gamma_inc_inv_qsmall (a:: Float64 , q:: Float64 )
733
752
b= 1.0 - a
@@ -753,30 +772,29 @@ end
753
772
"""
754
773
gamma_inc_inv_asmall(a,p,q,pcase)
755
774
756
- Compute x0 - initial approximation when `a` is small.
757
- Here the solution `x` of P(a,x)=p satisfies ``x_{l} < x < x_{u}``
758
- where ``x_{l} = (p\\ Gamma(a+1))^{1/a}`` and ``x_{u} = -log(1 - p\\ Gamma(a+1))`` and is used as starting value for Newton iteration.
759
- """
775
+ Compute `x0` - initial approximation when `a` is small.
776
+ Here the solution `x` of `` P(a,x)=p`` satisfies ``x_{l} < x < x_{u}``
777
+ where ``x_{l} = (p\\ Gamma(a+1))^{1/a}`` and ``x_{u} = -\\ log{ (1 - p\\ Gamma(a+1))}``, and is used as starting value for Newton iteration.
778
+ """
760
779
function gamma_inc_inv_asmall (a:: Float64 , p:: Float64 , q:: Float64 , pcase:: Bool )
761
780
logp = (pcase) ? log (p) : log1p (- q)
762
781
return exp ((1.0 / a)* (logp + loggamma1p (a)))
763
782
end
764
783
"""
765
784
gamma_inc_inv_alarge(a,porq,s)
766
785
767
- Compute x0 - initial approximation when `a` is large.
786
+ Compute `x0` - initial approximation when `a` is large.
768
787
The inversion problem is rewritten as :
769
788
```math
770
- 0.5 erfc(\\ eta \\ sqrt{a/2}) + R_{a}(\\ eta) = q
789
+ 0.5 \\ text{ erfc} (\\ eta \\ sqrt{a/2}) + R_{a}(\\ eta) = q
771
790
```
772
791
For large values of `a` we can write: ``\\ eta(q,a) = \\ eta_{0}(q,a) + \\ epsilon(\\ eta_{0},a)``
773
792
and it is possible to expand:
774
793
```math
775
794
\\ epsilon(\\ eta_{0},a) = \\ epsilon_{1}(\\ eta_{0},a)/a + \\ epsilon_{2}(\\ eta_{0},a)/a^{2} + \\ epsilon_{3}(\\ eta_{0},a)/a^{3} + ...
776
795
```
777
796
which is calculated by coeff1, coeff2 and coeff3 functions below.
778
- This returns a tuple of (x0,fp) where `fp` is computed since it's an approximation for the coefficient after inverting the original power series.
779
- `fp` is computed using `eta` which comes from the original inversion problem mentioned above and is computed using `lambdaeta(eta)`.
797
+ This returns a tuple `(x0,fp)`, where `fp` is computed since it's an approximation for the coefficient after inverting the original power series.
780
798
"""
781
799
function gamma_inc_inv_alarge (a:: Float64 , porq:: Float64 , s:: Integer )
782
800
r = erfcinv (2 * porq)
@@ -794,12 +812,21 @@ end
794
812
"""
795
813
gamma_inc(a,x,IND)
796
814
797
- DLMF: https://dlmf.nist.gov/8.2#E4 , https://dlmf.nist.gov/8.2#E5
798
- Wikipedia: https://en.wikipedia.org/wiki/Incomplete_gamma_function
799
- IND --> Accuracy desired ; IND=0 means 14 significant digits accuracy , IND=1 means 6 significant digit and IND=2 means only 3 digit accuracy suffices.
800
- gamma_inc(a,x) or P(a,x) is the Incomplete gamma function ratio given by : ``1/\\ Gamma (a) \\ int_{0}^{x} e^{-t}t^{a-1} dt``
801
- gamma_q(a,x) or Q(a,x) is the Incomplete gamma function ratio given by : 1 - P(a,x) -> ``1/\\ Gamma (a) \\ int_{x}^{\\ infty} e^{-t}t^{a-1} dt``
802
- Returns a tuple (gamma_inc, gamma_q) where gamma_inc + gamma_q = 1.0
815
+ Returns a tuple ``(p, q)`` where ``p + q = 1``, and
816
+ ``p=P(a,x)`` is the Incomplete gamma function ratio given by:
817
+ ```math
818
+ P(a,x)=\\ frac{1}{\\ Gamma (a)} \\ int_{0}^{x} e^{-t}t^{a-1} dt.
819
+ ```
820
+ and ``q=Q(a,x)`` is the Incomplete gamma function ratio given by:
821
+ ```math
822
+ Q(x,a)=\\ frac{1}{\\ Gamma (a)} \\ int_{x}^{\\ infty} e^{-t}t^{a-1} dt.
823
+ ```
824
+
825
+ `IND ∈ [0,1,2]` sets accuracy: `IND=0` means 14 significant digits accuracy, `IND=1` means 6 significant digit, and `IND=2` means only 3 digit accuracy.
826
+
827
+ External links: [DLMF](https://dlmf.nist.gov/8.2#E4), [NIST](https://dlmf.nist.gov/8.2#E5), [Wikipedia](https://en.wikipedia.org/wiki/Incomplete_gamma_function)
828
+
829
+ See also [`gamma(z)`](@ref SpecialFunctions.gamma), [`gamma_inc_inv(a,p,q)`](@ref SpecialFunctions.gamma_inc_inv)
803
830
"""
804
831
function gamma_inc (a:: Float64 ,x:: Float64 ,ind:: Integer )
805
832
iop = ind + 1
@@ -917,11 +944,12 @@ gamma_inc(a::AbstractFloat,x::AbstractFloat,ind::Integer) = throw(MethodError(ga
917
944
"""
918
945
gamma_inc_inv(a,p,q)
919
946
920
- DLMF: https://dlmf.nist.gov/8.2#E4 , https://dlmf.nist.gov/8.2#E5
921
- Wiki: https://en.wikipedia.org/wiki/Incomplete_gamma_function
947
+ Inverts the `gamma_inc(a,x)` function, by computing `x` given `a`,`p`,`q` in ``P(a,x)=p`` and ``Q(a,x)=q``.
948
+
949
+ External links: [DLMF](https://dlmf.nist.gov/8.2#E4), [NIST](https://dlmf.nist.gov/8.2#E5),
950
+ Wikipedia](https://en.wikipedia.org/wiki/Incomplete_gamma_function)
922
951
923
- gamma_inc(a,x) or (P(a,x),Q(a,x)) is the Incomplete gamma function ratio given by : ``1/\\ Gamma (a) \\ int_{0}^{x} e^{-t}t^{a-1} dt``
924
- gamma_inc_inv(a,p,q) inverts the gamma_inc function, by computing `x` given `a`,`p`,`q` in P(a,x)=p and Q(a,x)=q.
952
+ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc).
925
953
"""
926
954
function gamma_inc_inv (a:: Float64 , p:: Float64 , q:: Float64 )
927
955
if p < 0.5
0 commit comments