Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hypergeometric functions #1383

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions mathics/builtin/specialfns/bessel.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,39 +566,6 @@ class HankelH2(_Bessel):
sympy_name = "hankel2"


class HypergeometricU(MPMathFunction):
"""
<url>
:Confluent hypergeometric function: https://en.wikipedia.org/wiki/Confluent_hypergeometric_function</url> (<url>
:mpmath: https://mpmath.org/doc/current/functions/bessel.html#mpmath.hyperu</url>, <url>
:WMA: https://reference.wolfram.com/language/ref/HypergeometricU.html</url>)
<dl>
<dt>'HypergeometricU'[$a$, $b$, $z$]
<dd>returns $U(a, b, z)$.
</dl>

>> HypergeometricU[3, 2, 1.]
= 0.105479

Plot 'U'[3, 2, x] from 0 to 2 in steps of 0.5:
>> Plot[HypergeometricU[3, 2, x], {x, 0.5, 2}]
= -Graphics-

We handle this special case:
>> HypergeometricU[0, c, z]
= 1
"""

attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
mpmath_name = "hyperu"
nargs = {3}
rules = {
"HypergeometricU[0, c_, z_]": "1",
}
summary_text = "Tricomi confluent hypergeometric function"
sympy_name = ""


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't revise functions until we've mastered the existing ones.

# Kelvin Functions


Expand Down
106 changes: 102 additions & 4 deletions mathics/builtin/specialfns/hypergeom.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class HypergeometricPFQ(MPMathFunction):
:sympy: https://docs.sympy.org/latest/modules/functions/special.html#sympy.functions.special.hyper.hyper</url>, <url>
:WMA: https://reference.wolfram.com/language/ref/HypergeometricPFQ.html</url>)
<dl>
<dt>'HypergeometricPFQ'[${a_1, a_2, a_p}, {b_1, b_2, b_q}, z$]
<dd>returns ${}_p F_q({a_1, a_2, a_p}, {b_1, b_2, b_q}, z)$.
<dt>'HypergeometricPFQ'[${a_1, ..., a_p}, {b_1, ..., b_q}, z$]
<dd>returns ${}_p F_q({a_1, ..., a_p}; {b_1, ..., b_q}; z)$.
</dl>
>> HypergeometricPFQ[{2}, {2}, 1]
= E
Expand Down Expand Up @@ -93,7 +93,7 @@ class Hypergeometric1F1(MPMathFunction):
:WMA: https://reference.wolfram.com/language/ref/Hypergeometric1F1.html</url>)
<dl>
<dt>'Hypergeometric1F1'[$a$, $b$, $z$]
<dd>returns $M(a, b, z)$.
<dd>returns ${}_1 F_1(a; b; z)$.
</dl>

Result is symbollicaly simplified by default:
Expand All @@ -106,7 +106,7 @@ class Hypergeometric1F1(MPMathFunction):
Plot 'M'[3, 2, x] from 0 to 2 in steps of 0.5:
>> Plot[Hypergeometric1F1[3, 2, x], {x, 0.5, 2}]
= -Graphics-
Here, plot explicitly requests numerical results.
Here, plot explicitly requests a numerical evaluation.
"""

attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED

In contrast to HypergeometricPFQ, WMA does not list this as being ReadProtected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rocky I've these changes on my radar. Kindly wait until I've marked this PR for review. Before that, I'm just focusing on the important functional aspects. But, I'll fix the documentation and the attributes before completion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I'll limit myself to these four functions, which I will also be needing at work right now. But, eventually, other hypergeometric functions can be implemented just via rules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that later in another PR.

Expand All @@ -117,3 +117,101 @@ class Hypergeometric1F1(MPMathFunction):
}
summary_text = "compute Kummer confluent hypergeometric function"
sympy_name = ""


class MeijerG(MPMathFunction):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not start more functions until we have mastered the ones in progress.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rocky, please kindly wait with the reviews. I'll let you know once I'm done with this PR and put it out of draft mode. Otherwise, my development flow may be confusing to you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already too large. Please reduce the scope.

Copy link
Member

@rocky rocky Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want folks to see what's up. People get notifications even when working in draft mode.

Instead, fork the code and work in your private fork. Thanks.

"""
<url>
:Meijer G-function: https://en.wikipedia.org/wiki/Meijer_G-function</url> (<url>
:mpmath: https://mpmath.org/doc/current/functions/hypergeometric.html#meijerg</url>, <url>
:sympy: https://docs.sympy.org/latest/modules/functions/special.html#sympy.functions.special.hyper.meijerg</url>, <url>
:WMA: https://reference.wolfram.com/language/ref/MeijerG.html</url>)
<dl>
<dt>'MeijerG'[${{a_1, ..., a_n}, {a_{n+1}, ..., a_p}}, {{b_1, ..., b_m}, {b_{m+1}, ..., a_q}}, z$]
<dd>returns $G^{m,n}_{p,q}(z | {a_1, ..., a_p}; {b_1, ..., b_q})$.
</dl>
Result is symbollicaly simplified by default:
>> MeijerG[{{1, 2}, {}}, {{3}, {}}, 1]
= MeijerG[{{1, 2}, {}}, {{3}, {}}, 1]
unless a numerical evaluation is explicitly requested:
>> MeijerG[{{1, 2},{}}, {{3},{}}, 1] // N
= 0.210958
"""

attributes = A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
mpmath_name = "meijerg"
nargs = {3}
rules = {}
summary_text = "compute the Meijer G-function"
sympy_name = "meijerg"

def eval(self, a, b, z, evaluation: Evaluation):
"MeijerG[a_, b_, z_]"
try:
a_sympy = [e.to_sympy() for e in a]
b_sympy = [e.to_sympy() for e in b]
return from_sympy(sympy.meijerg(a_sympy, b_sympy, z.to_sympy()))
except Exception:
pass

def eval_N(self, a, b, z, evaluation: Evaluation):
"N[MeijerG[a_, b_, z_]]"
try:
return run_mpmath(
mpmath.meijerg,
tuple([a.to_python(), b.to_python(), z.to_python()]),
FP_MANTISA_BINARY_DIGITS,
)
except Exception:
pass


class HypergeometricU(MPMathFunction):
"""
<url>
:Confluent hypergeometric function: https://en.wikipedia.org/wiki/Confluent_hypergeometric_function</url> (<url>
:mpmath: https://mpmath.org/doc/current/functions/bessel.html#mpmath.hyperu</url>, <url>
:WMA: https://reference.wolfram.com/language/ref/HypergeometricU.html</url>)
<dl>
<dt>'HypergeometricU'[$a$, $b$, $z$]
<dd>returns $U(a, b, z)$.
</dl>
Result is symbollicaly simplified by default:
>> HypergeometricU[3, 2, 1]
= MeijerG[{{1, 2}, {}}, {{3}, {}}, 1]
unless a numerical evaluation is explicitly requested:
>> MeijerG[{{1, 2},{}}, {{3},{}}, 1] // N
= 0.210958

>> HypergeometricU[3, 2, 1.]
= 0.105479

Plot 'U'[3, 2, x] from 0 to 2 in steps of 0.5:
>> Plot[HypergeometricU[3, 2, x], {x, 0.5, 2}]
= -Graphics-

We handle this special case:
>> HypergeometricU[0, b, z]
= 1
"""

attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | A_READ_PROTECTED
mpmath_name = "hyperu"
nargs = {3}
rules = {
"HypergeometricU[0, c_, z_]": "1",
"HypergeometricU[a_, b_, z_]": "MeijerG[{{1-a},{}},{{0,1-b},{}},z]/Gamma[a]/Gamma[a-b+1]",
}
summary_text = "Tricomi confluent hypergeometric function"
sympy_name = ""

def eval_N(self, a, b, z, evaluation: Evaluation):
"N[HypergeometricU[a_, b_, z_]]"
try:
return run_mpmath(
mpmath.hyperu,
tuple([a.to_python(), b.to_python(), z.to_python()]),
FP_MANTISA_BINARY_DIGITS,
)
except Exception:
pass
Loading