Repository: sympy/sympy
Pull request: SciPyPrinter: polygamma (fix gh-23924)
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
~~Fixes #23924~~


#### Brief description of what is fixed or changed
~~Attempt to fix CI failure.~~
Add polygamma to explicit list of functions that SciPy can evaluate.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->


Problem statement:
CI failure: lambdify: NameError: name 'polygamma' is not defined
This failure is seen in CI for the 3.11 optional dependency tests:
```
____________ sympy/utilities/tests/test_lambdify.py:test_scipy_fns _____________
Traceback (most recent call last):
  File "/home/runner/work/sympy/sympy/sympy/utilities/tests/test_lambdify.py", line 1103, in test_scipy_fns
    assert abs(f(tv) - sympy_result) < 1e-13*(1 + abs(sympy_result))
               ^^^^^
  File "<lambdifygenerated-604>", line 2, in _lambdifygenerated
    return polygamma(0, x)
           ^^^^^^^^^
NameError: name 'polygamma' is not defined
```
That CI job uses latest cython, numpy and scipy from git so this is likely due to a change in scipy.

Hints from issue discussion:
This job is not "required" so a PR can still be merged if it fails. Resolving the cause of this though is a release blocker for 1.11. If the next SciPy release is going to break `lambdify` then we might need a fix for that in SymPy 1.11.

I don't know `lambdify` very well though so I would appreciate it if someone who does takes a look at what happens with lambdifying polygamma using the latest scipy from git.
Polygamma is still in the same location in SciPy if nothing else. https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.polygamma.html

Commit message: SciPyPrinter: polygamma (fix gh-23924)

Patch to review:
diff --git a/sympy/printing/numpy.py b/sympy/printing/numpy.py
index 5ab29585a17a..a42d1df5d26d 100644
--- a/sympy/printing/numpy.py
+++ b/sympy/printing/numpy.py
@@ -290,6 +290,7 @@ def _print_NDimArray(self, expr):
     'gamma': 'gamma',
     'loggamma': 'gammaln',
     'digamma': 'psi',
+    'polygamma': 'polygamma',
     'RisingFactorial': 'poch',
     'jacobi': 'eval_jacobi',
     'gegenbauer': 'eval_gegenbauer',
diff --git a/sympy/utilities/tests/test_lambdify.py b/sympy/utilities/tests/test_lambdify.py
index 50b285711830..f8269323ce83 100644
--- a/sympy/utilities/tests/test_lambdify.py
+++ b/sympy/utilities/tests/test_lambdify.py
@@ -23,7 +23,7 @@
 from sympy.functions.special.beta_functions import (beta, betainc, betainc_regularized)
 from sympy.functions.special.delta_functions import (Heaviside)
 from sympy.functions.special.error_functions import (Ei, erf, erfc, fresnelc, fresnels)
-from sympy.functions.special.gamma_functions import (digamma, gamma, loggamma)
+from sympy.functions.special.gamma_functions import (digamma, gamma, loggamma, polygamma)
 from sympy.integrals.integrals import Integral
 from sympy.logic.boolalg import (And, false, ITE, Not, Or, true)
 from sympy.matrices.expressions.dotproduct import DotProduct
@@ -1077,7 +1077,7 @@ def test_scipy_fns():
     if not scipy:
         skip("scipy not installed")
 
-    single_arg_sympy_fns = [Ei, erf, erfc, factorial, gamma, loggamma, digamma]
+    single_arg_sympy_fns = [Ei, erf, erfc, factorial, gamma, loggamma, digamma, polygamma]
     single_arg_scipy_fns = [scipy.special.expi, scipy.special.erf, scipy.special.erfc,
         scipy.special.factorial, scipy.special.gamma, scipy.special.gammaln,
         scipy.special.psi]
@@ -1097,7 +1097,7 @@ def test_scipy_fns():
                 tv = numpy.abs(tv)
             # SymPy's digamma evaluates as polygamma(0, z)
             # which SciPy supports for real arguments only
-            if sympy_fn == digamma:
+            if sympy_fn in (digamma, polygamma):
                 tv = numpy.real(tv)
             sympy_result = sympy_fn(tv).evalf()
             assert abs(f(tv) - sympy_result) < 1e-13*(1 + abs(sympy_result))


Code review comments:
diff_hunk: @@ -1077,7 +1077,7 @@ def test_scipy_fns():
     if not scipy:
         skip("scipy not installed")
 
-    single_arg_sympy_fns = [Ei, erf, erfc, factorial, gamma, loggamma, digamma]
+    single_arg_sympy_fns = [Ei, erf, erfc, factorial, gamma, loggamma, digamma, polygamma]
path: sympy/utilities/tests/test_lambdify.py
text: @user1:
I have no idea how this test works, but does this "guarantee" that polygamma works when both arguments are used? (Just considering the variable name...)

@author:
No, I didn't read the test code carefully enough, next commit now passes for me locally.