Skip to content

Commit acdb831

Browse files
AshtonSBradleysimonbyrne
authored andcommitted
update docstrings. mainly for gamma_inc, gamma_inc_inv (#197)
1 parent 94cbee9 commit acdb831

File tree

2 files changed

+81
-53
lines changed

2 files changed

+81
-53
lines changed

src/ellip.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Using piecewise approximation polynomial as given in
3131
> DOI 10.1007/s10569-009-9228-z,
3232
> <https://pdfs.semanticscholar.org/8112/c1f56e833476b61fc54d41e194c962fbe647.pdf>
3333
34-
For ``m<0```, followed
34+
For ``m<0``, followed by
3535
> Fukushima, Toshio. (2014).
3636
> 'Precise, compact, and fast computation of complete elliptic integrals by piecewise
3737
> minimax rational function approximation'.
@@ -207,7 +207,7 @@ Using piecewise approximation polynomial as given in
207207
> DOI 10.1007/s10569-009-9228-z,
208208
> <https://pdfs.semanticscholar.org/8112/c1f56e833476b61fc54d41e194c962fbe647.pdf>
209209
210-
For ``m<0```, followed
210+
For ``m<0``, followed by
211211
> Fukushima, Toshio. (2014).
212212
> 'Precise, compact, and fast computation of complete elliptic integrals by piecewise
213213
> minimax rational function approximation'.

src/gamma_inc.jl

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ end
192192
"""
193193
stirling(x)
194194
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
196196
in https://dlmf.nist.gov/5.11.1
197197
"""
198198
function stirling(x::Float64)
@@ -219,8 +219,8 @@ end
219219
"""
220220
gammax(x)
221221
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}``.
224224
"""
225225
function gammax(x::Float64)
226226
if x >= 3
@@ -234,7 +234,7 @@ end
234234
"""
235235
lambdaeta(eta)
236236
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}``
238238
"""
239239
function lambdaeta(eta::Float64)
240240
s = eta*eta*0.5
@@ -383,12 +383,15 @@ end
383383
"""
384384
gamma_inc_taylor(a, x, ind)
385385
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 :
387387
```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)))
389389
```
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)
392395
"""
393396
function gamma_inc_taylor(a::Float64, x::Float64, ind::Integer)
394397
acc = acc0[ind + 1]
@@ -430,12 +433,15 @@ end
430433
"""
431434
gamma_inc_asym(a, x, ind)
432435
433-
Compute P(a,x) using asymptotic expansion given by :
436+
Compute ``P(a,x)`` using asymptotic expansion given by:
434437
```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}))
436439
```
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)
439445
"""
440446
function gamma_inc_asym(a::Float64, x::Float64, ind::Integer)
441447
wk = zeros(30)
@@ -476,16 +482,18 @@ end
476482
"""
477483
gamma_inc_taylor_x(a,x,ind)
478484
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:
480486
```math
481487
J = -a * \\sum_{1}^{\\infty} (-x)^{n}/((a+n)n!)
482488
```
483-
and P(a,x)/x**a is given by :
489+
and ``P(a,x)/x**a`` is given by :
484490
```math
485491
(1 - J)/ \\Gamma(a+1)
486492
```
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)
489497
"""
490498
function gamma_inc_taylor_x(a::Float64, x::Float64, ind::Integer)
491499
acc = acc0[ind + 1]
@@ -520,16 +528,20 @@ end
520528
"""
521529
gamma_inc_minimax(a,x,z)
522530
523-
Compute P(a,x) using minimax approximations given by :
531+
Compute ``P(a,x)`` using minimax approximations given by :
524532
```math
525533
1/2 * erfc(\\sqrt{y}) - e^{-y}/\\sqrt{2\\pi*a}* T(a,\\lambda)
526534
``` where
527535
```math
528536
T(a,\\lambda) = \\sum_{0}^{N} c_{k}(z)a^{-k}
529537
```
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.
532540
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)
533545
"""
534546
function gamma_inc_minimax(a::Float64, x::Float64, z::Float64)
535547
l = x/a
@@ -579,15 +591,18 @@ end
579591
"""
580592
gamma_inc_temme(a, x, z)
581593
582-
Compute P(a,x) using Temme's expansion given by :
594+
Compute ``P(a,x)`` using Temme's expansion given by :
583595
```math
584596
1/2 * erfc(\\sqrt{y}) - e^{-y}/\\sqrt{2\\pi*a}* T(a,\\lambda)
585597
``` where
586598
```math
587599
T(a,\\lambda) = \\sum_{0}^{N} c_{k}(z)a^{-k}
588600
```
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)
591606
"""
592607
function gamma_inc_temme(a::Float64, x::Float64, z::Float64)
593608
l = x/a
@@ -609,15 +624,17 @@ end
609624
"""
610625
gamma_inc_temme_1(a, x, z, ind)
611626
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 :
613628
```math
614629
E(y) - (1 - y)/\\sqrt{2\\pi*a} * T(a,\\lambda)
615-
``` where
630+
```
631+
where
616632
```math
617633
E(y) = 1/2 - (1 - y/3)*(\\sqrt(y/\\pi))
618634
```
619635
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)
621638
"""
622639
function gamma_inc_temme_1(a::Float64, x::Float64, z::Float64, ind::Integer)
623640
iop = ind + 1
@@ -660,9 +677,11 @@ end
660677
"""
661678
gamma_inc_fsum(a,x)
662679
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`.
665682
Refer Eqn (14) in the paper.
683+
684+
See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
666685
"""
667686
function gamma_inc_fsum(a::Float64, x::Float64)
668687
if isinteger(a)
@@ -694,13 +713,13 @@ end
694713
"""
695714
gamma_inc_inv_psmall(a,p)
696715
697-
Compute x0 - initial approximation when `p` is small.
716+
Compute `x0` - initial approximation when `p` is small.
698717
Here we invert the series in Eqn (2.20) in the paper and write the inversion problem as:
699718
```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},
701720
```
702721
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}``.
704723
"""
705724
function gamma_inc_inv_psmall(a::Float64, p::Float64)
706725
logr = (1.0/a)*(log(p) + logabsgamma(a + 1.0)[1])
@@ -722,12 +741,12 @@ end
722741
"""
723742
gamma_inc_inv_qsmall(a,q)
724743
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)``.
726745
Asymptotic expansions Eqn (2.29) in the paper is used here and higher approximations are obtained using
727746
```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}
729748
```
730-
where b = 1-a, L = ln(x0)
749+
where ``b = 1-a``, ``L = \\ln{x_0}``.
731750
"""
732751
function gamma_inc_inv_qsmall(a::Float64, q::Float64)
733752
b=1.0-a
@@ -753,30 +772,29 @@ end
753772
"""
754773
gamma_inc_inv_asmall(a,p,q,pcase)
755774
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+
"""
760779
function gamma_inc_inv_asmall(a::Float64, p::Float64, q::Float64, pcase::Bool)
761780
logp = (pcase) ? log(p) : log1p(-q)
762781
return exp((1.0/a)*(logp +loggamma1p(a)))
763782
end
764783
"""
765784
gamma_inc_inv_alarge(a,porq,s)
766785
767-
Compute x0 - initial approximation when `a` is large.
786+
Compute `x0` - initial approximation when `a` is large.
768787
The inversion problem is rewritten as :
769788
```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
771790
```
772791
For large values of `a` we can write: ``\\eta(q,a) = \\eta_{0}(q,a) + \\epsilon(\\eta_{0},a)``
773792
and it is possible to expand:
774793
```math
775794
\\epsilon(\\eta_{0},a) = \\epsilon_{1}(\\eta_{0},a)/a + \\epsilon_{2}(\\eta_{0},a)/a^{2} + \\epsilon_{3}(\\eta_{0},a)/a^{3} + ...
776795
```
777796
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.
780798
"""
781799
function gamma_inc_inv_alarge(a::Float64, porq::Float64, s::Integer)
782800
r = erfcinv(2*porq)
@@ -794,12 +812,21 @@ end
794812
"""
795813
gamma_inc(a,x,IND)
796814
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)
803830
"""
804831
function gamma_inc(a::Float64,x::Float64,ind::Integer)
805832
iop = ind + 1
@@ -917,11 +944,12 @@ gamma_inc(a::AbstractFloat,x::AbstractFloat,ind::Integer) = throw(MethodError(ga
917944
"""
918945
gamma_inc_inv(a,p,q)
919946
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)
922951
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).
925953
"""
926954
function gamma_inc_inv(a::Float64, p::Float64, q::Float64)
927955
if p < 0.5

0 commit comments

Comments
 (0)