Skip to content

Commit 8f45adc

Browse files
authored
Merge pull request #1340 from mathics/gamma-fns
Split out gamma functions
2 parents b162c07 + cb56d13 commit 8f45adc

2 files changed

Lines changed: 116 additions & 103 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,109 +1858,6 @@ class Factorial(PostfixOperator, _MPMathFunction):
18581858
precedence = 610
18591859
mpmath_name = "factorial"
18601860

1861-
1862-
class Gamma(_MPMathMultiFunction):
1863-
"""
1864-
<dl>
1865-
<dt>'Gamma[$z$]'
1866-
<dd>is the gamma function on the complex number $z$.
1867-
<dt>'Gamma[$z$, $x$]'
1868-
<dd>is the upper incomplete gamma function.
1869-
<dt>'Gamma[$z$, $x0$, $x1$]'
1870-
<dd>is equivalent to 'Gamma[$z$, $x0$] - Gamma[$z$, $x1$]'.
1871-
</dl>
1872-
1873-
'Gamma[$z$]' is equivalent to '($z$ - 1)!':
1874-
>> Simplify[Gamma[z] - (z - 1)!]
1875-
= 0
1876-
1877-
Exact arguments:
1878-
>> Gamma[8]
1879-
= 5040
1880-
>> Gamma[1/2]
1881-
= Sqrt[Pi]
1882-
>> Gamma[1, x]
1883-
= E ^ (-x)
1884-
>> Gamma[0, x]
1885-
= ExpIntegralE[1, x]
1886-
1887-
Numeric arguments:
1888-
>> Gamma[123.78]
1889-
= 4.21078*^204
1890-
>> Gamma[1. + I]
1891-
= 0.498016 - 0.15495 I
1892-
1893-
Both 'Gamma' and 'Factorial' functions are continuous:
1894-
>> Plot[{Gamma[x], x!}, {x, 0, 4}]
1895-
= -Graphics-
1896-
1897-
## Issue 203
1898-
#> N[Gamma[24/10], 100]
1899-
= 1.242169344504305404913070252268300492431517240992022966055507541481863694148882652446155342679460339
1900-
#> N[N[Gamma[24/10],100]/N[Gamma[14/10],100],100]
1901-
= 1.400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1902-
#> % // Precision
1903-
= 100.
1904-
1905-
#> Gamma[1.*^20]
1906-
: Overflow occurred in computation.
1907-
= Overflow[]
1908-
1909-
## Needs mpmath support for lowergamma
1910-
#> Gamma[1., 2.]
1911-
= Gamma[1., 2.]
1912-
"""
1913-
1914-
mpmath_names = {
1915-
1: "gamma",
1916-
}
1917-
sympy_names = {
1918-
1: "gamma",
1919-
2: "uppergamma",
1920-
}
1921-
1922-
rules = {
1923-
"Gamma[z_, x0_, x1_]": "Gamma[z, x0] - Gamma[z, x1]",
1924-
"Gamma[1 + z_]": "z!",
1925-
"Derivative[1][Gamma]": "(Gamma[#1]*PolyGamma[0, #1])&",
1926-
"Derivative[1, 0][Gamma]": "(Gamma[#1, #2]*Log[#2] + MeijerG[{{}, {1, 1}}, {{0, 0, #1}, {}}, #2])&",
1927-
"Derivative[0, 1][Gamma]": "(-(#2^(-1 + #1)/E^#2))&",
1928-
}
1929-
1930-
def get_sympy_names(self):
1931-
return ["gamma", "uppergamma", "lowergamma"]
1932-
1933-
def from_sympy(self, sympy_name, leaves):
1934-
if sympy_name == "lowergamma":
1935-
# lowergamma(z, x) -> Gamma[z, 0, x]
1936-
z, x = leaves
1937-
return Expression(self.get_name(), z, Integer0, x)
1938-
else:
1939-
return Expression(self.get_name(), *leaves)
1940-
1941-
1942-
class Pochhammer(SympyFunction):
1943-
"""
1944-
<dl>
1945-
<dt>'Pochhammer[$a$, $n$]'
1946-
<dd>is the Pochhammer symbol (a)_n.
1947-
</dl>
1948-
1949-
>> Pochhammer[4, 8]
1950-
= 6652800
1951-
"""
1952-
1953-
attributes = ("Listable", "NumericFunction", "Protected")
1954-
1955-
sympy_name = "RisingFactorial"
1956-
1957-
rules = {
1958-
"Pochhammer[a_, n_]": "Gamma[a + n] / Gamma[a]",
1959-
"Derivative[1,0][Pochhammer]": "(Pochhammer[#1, #2]*(-PolyGamma[0, #1] + PolyGamma[0, #1 + #2]))&",
1960-
"Derivative[0,1][Pochhammer]": "(Pochhammer[#1, #2]*PolyGamma[0, #1 + #2])&",
1961-
}
1962-
1963-
19641861
class HarmonicNumber(_MPMathFunction):
19651862
"""
19661863
<dl>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)