|
| 1 | +""" |
| 2 | +Gamma and Related Functions |
| 3 | +""" |
| 4 | + |
| 5 | +from mathics.version import __version__ # noqa used in loading to check consistency. |
| 6 | + |
| 7 | +from mathics.builtin.arithmetic import _MPMathMultiFunction |
| 8 | +from mathics.builtin.base import SympyFunction |
| 9 | +from mathics.core.expression import Expression, Integer0 |
| 10 | + |
| 11 | +class Gamma(_MPMathMultiFunction): |
| 12 | + """ |
| 13 | + In number theory the logarithm of the gamma function often appears. For positive real numbers, this can be evaluated as 'Log[Gamma[$z$]]'. |
| 14 | +
|
| 15 | + <dl> |
| 16 | + <dt>'Gamma[$z$]' |
| 17 | + <dd>is the gamma function on the complex number $z$. |
| 18 | +
|
| 19 | + <dt>'Gamma[$z$, $x$]' |
| 20 | + <dd>is the upper incomplete gamma function. |
| 21 | +
|
| 22 | + <dt>'Gamma[$z$, $x0$, $x1$]' |
| 23 | + <dd>is equivalent to 'Gamma[$z$, $x0$] - Gamma[$z$, $x1$]'. |
| 24 | + </dl> |
| 25 | +
|
| 26 | + 'Gamma[$z$]' is equivalent to '($z$ - 1)!': |
| 27 | + >> Simplify[Gamma[z] - (z - 1)!] |
| 28 | + = 0 |
| 29 | +
|
| 30 | + Exact arguments: |
| 31 | + >> Gamma[8] |
| 32 | + = 5040 |
| 33 | + >> Gamma[1/2] |
| 34 | + = Sqrt[Pi] |
| 35 | + >> Gamma[1, x] |
| 36 | + = E ^ (-x) |
| 37 | + >> Gamma[0, x] |
| 38 | + = ExpIntegralE[1, x] |
| 39 | +
|
| 40 | + Numeric arguments: |
| 41 | + >> Gamma[123.78] |
| 42 | + = 4.21078*^204 |
| 43 | + >> Gamma[1. + I] |
| 44 | + = 0.498016 - 0.15495 I |
| 45 | +
|
| 46 | + Both 'Gamma' and 'Factorial' functions are continuous: |
| 47 | + >> Plot[{Gamma[x], x!}, {x, 0, 4}] |
| 48 | + = -Graphics- |
| 49 | +
|
| 50 | + ## Issue 203 |
| 51 | + #> N[Gamma[24/10], 100] |
| 52 | + = 1.242169344504305404913070252268300492431517240992022966055507541481863694148882652446155342679460339 |
| 53 | + #> N[N[Gamma[24/10],100]/N[Gamma[14/10],100],100] |
| 54 | + = 1.400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
| 55 | + #> % // Precision |
| 56 | + = 100. |
| 57 | +
|
| 58 | + #> Gamma[1.*^20] |
| 59 | + : Overflow occurred in computation. |
| 60 | + = Overflow[] |
| 61 | +
|
| 62 | + ## Needs mpmath support for lowergamma |
| 63 | + #> Gamma[1., 2.] |
| 64 | + = Gamma[1., 2.] |
| 65 | + """ |
| 66 | + |
| 67 | + mpmath_names = { |
| 68 | + 1: "gamma", |
| 69 | + } |
| 70 | + sympy_names = { |
| 71 | + 1: "gamma", |
| 72 | + 2: "uppergamma", |
| 73 | + } |
| 74 | + |
| 75 | + rules = { |
| 76 | + "Gamma[z_, x0_, x1_]": "Gamma[z, x0] - Gamma[z, x1]", |
| 77 | + "Gamma[1 + z_]": "z!", |
| 78 | + "Derivative[1][Gamma]": "(Gamma[#1]*PolyGamma[0, #1])&", |
| 79 | + "Derivative[1, 0][Gamma]": "(Gamma[#1, #2]*Log[#2] + MeijerG[{{}, {1, 1}}, {{0, 0, #1}, {}}, #2])&", |
| 80 | + "Derivative[0, 1][Gamma]": "(-(#2^(-1 + #1)/E^#2))&", |
| 81 | + } |
| 82 | + |
| 83 | + def get_sympy_names(self): |
| 84 | + return ["gamma", "uppergamma", "lowergamma"] |
| 85 | + |
| 86 | + def from_sympy(self, sympy_name, leaves): |
| 87 | + if sympy_name == "lowergamma": |
| 88 | + # lowergamma(z, x) -> Gamma[z, 0, x] |
| 89 | + z, x = leaves |
| 90 | + return Expression(self.get_name(), z, Integer0, x) |
| 91 | + else: |
| 92 | + return Expression(self.get_name(), *leaves) |
| 93 | + |
| 94 | + |
| 95 | +class Pochhammer(SympyFunction): |
| 96 | + """ |
| 97 | + The Pochhammer symbol or rising factorial often appears in series expansions for hypergeometric functions. |
| 98 | + The Pochammer symbol has a definie value even when the gamma functions which appear in its definition are infinite. |
| 99 | + <dl> |
| 100 | + <dt>'Pochhammer[$a$, $n$]' |
| 101 | + <dd>is the Pochhammer symbol (a)_n. |
| 102 | + </dl> |
| 103 | +
|
| 104 | + >> Pochhammer[4, 8] |
| 105 | + = 6652800 |
| 106 | + """ |
| 107 | + |
| 108 | + attributes = ("Listable", "NumericFunction", "Protected") |
| 109 | + |
| 110 | + sympy_name = "RisingFactorial" |
| 111 | + |
| 112 | + rules = { |
| 113 | + "Pochhammer[a_, n_]": "Gamma[a + n] / Gamma[a]", |
| 114 | + "Derivative[1,0][Pochhammer]": "(Pochhammer[#1, #2]*(-PolyGamma[0, #1] + PolyGamma[0, #1 + #2]))&", |
| 115 | + "Derivative[0,1][Pochhammer]": "(Pochhammer[#1, #2]*PolyGamma[0, #1 + #2])&", |
| 116 | + } |
0 commit comments