Repository: sympy/sympy

Issue:
lambdify loggamma doesn't work for mpmath
Lambdifying an expression with loggamma doesn't work for mpmath
```julia
In [6]: lambdify(x, loggamma(x), 'mpmath')                                                                                        
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-02428e0a17ae> in <module>
----> 1 lambdify(x, loggamma(x), 'mpmath')

~/current/sympy/sympy/sympy/utilities/lambdify.py in lambdify(args, expr, modules, printer, use_imps, dummify)
    766                 imp_mod_lines.append("from %s import %s" % (mod, k))
    767     for ln in imp_mod_lines:
--> 768         exec_(ln, {}, namespace)
    769 
    770     # Provide lambda expression with builtins, and compatible implementation of range

<string> in <module>

ImportError: cannot import name 'lgamma'
```
I think this is because it's called loggamma rather than lgamma:
```julia
In [7]: import mpmath                                                                                                             

In [8]: mpmath.loggamma(1)                                                                                                        
Out[8]: mpf('0.0')
```

I originally got this error from
```julia
In [11]: nsolve(im(loggamma(I*x)), x, 3.439*I)                                                                                    
---------------------------------------------------------------------------
ImportError
...
```

Discussion:
sympy-bot: :white_check_mark:

Hi, I am the [SymPy bot](https://github.com/sympy/sympy-bot) (v160). I'm here to help you write a release notes entry. Please read the [guide on how to write release notes](https://github.com/sympy/sympy/wiki/Writing-Release-Notes).



Your release notes are in good order.

Here is what the release notes will look like:
* utilities
  * added "lgamma":"loggamma" in MPMATH_TRANSLATIONS of lambdify.py file ([#19913](https://github.com/sympy/sympy/pull/19913) by [@abhaydhiman](https://github.com/abhaydhiman))

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.7.

Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. <details><summary>Click here to see the pull request description that was parsed.</summary>

    <!-- 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
    <!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
    format, e.g. "Fixes #1234" (see
    https://tinyurl.com/auto-closing for more information). Also, please
    write a comment on that issue linking back to this pull request once it is
    open. -->
    Fixed #17411

    #### Brief description of what is fixed or changed
    Lambdifying an expression with loggamma doesn't work for mpmath previously but after adding `"lgamma":"loggamma"` in `MPMATH_TRANSLATIONS` dictionary it works now

    #### Other comments
    Nan

    #### Release Notes

    <!-- Write the release notes for this release below. 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 -->
    * utilities
      * added "lgamma":"loggamma" in MPMATH_TRANSLATIONS of lambdify.py file
    <!-- END RELEASE NOTES -->

</details><p>

codecov[bot]: # [Codecov](https://codecov.io/gh/sympy/sympy/pull/19913?src=pr&el=h1) Report
> Merging [#19913](https://codecov.io/gh/sympy/sympy/pull/19913?src=pr&el=desc) into [master](https://codecov.io/gh/sympy/sympy/commit/089bef23e02b311bd8a5b4400ccd8a5d427005d5&el=desc) will **decrease** coverage by `16.136%`.
> The diff coverage is `n/a`.

```diff
@@              Coverage Diff               @@
##            master    #19913        +/-   ##
==============================================
- Coverage   75.783%   59.646%   -16.137%     
==============================================
  Files          668       668                
  Lines       173217    173217                
  Branches     40837     40837                
==============================================
- Hits        131270    103318     -27952     
- Misses       36218     64031     +27813     
- Partials      5729      5868       +139     
```

abhaydhiman: As `sympy.__dict__` doesn't contains `lgamma`. Its used to pass the test case for `lgamma` as main function for it is defined in `mpmath.__dict__`.
eric-wieser: You need to change the mpmath specific lines I linked to above.
abhaydhiman: > You need to change the mpmath specific lines I linked to above.

What exactly do I have to change in that line of code.
eric-wieser: Updating the dictionary to correct the lgamma key on the next line would probably do the trick.
abhaydhiman: well, I actually did that.
eric-wieser: You should add to `_known_functions_mpmath`
abhaydhiman: `'lgamma': 'loggamma'` if I add this in `_known_functions_mpmath` that again gives the same error.
eric-wieser: > Or I have to add `"loggamma": "loggamma"` without deleting the previous `"loggamma": "lgamma"`, I tried this and this works perfectly fine.

This sounds ideal to me - this effectively _does_ delete the previous entry.
abhaydhiman: Is it good to go now?
eric-wieser: No. You need to add `"loggamma": "loggamma"` to `_known_functions_mpmath`
abhaydhiman: oh! Sorry, I am on it.

Patch:
diff --git a/sympy/printing/pycode.py b/sympy/printing/pycode.py
index 5862c8fb51..c35def62cd 100644
--- a/sympy/printing/pycode.py
+++ b/sympy/printing/pycode.py
@@ -502,6 +502,7 @@ def pycode(expr, **settings):
     'fresnelc': 'fresnelc',
     'fresnels': 'fresnels',
     'sign': 'sign',
+    'loggamma': 'loggamma',
 })
 _known_constants_mpmath = {
     'Exp1': 'e',


Test patch:
diff --git a/sympy/printing/tests/test_pycode.py b/sympy/printing/tests/test_pycode.py
index 48fb134da5..486d208c93 100644
--- a/sympy/printing/tests/test_pycode.py
+++ b/sympy/printing/tests/test_pycode.py
@@ -17,6 +17,7 @@
 from sympy.tensor import IndexedBase
 from sympy.testing.pytest import skip
 from sympy.external import import_module
+from sympy.functions.special.gamma_functions import loggamma
 
 x, y, z = symbols('x y z')
 p = IndexedBase("p")
@@ -75,6 +76,7 @@ def test_MpmathPrinter():
     assert p.doprint(S.NaN) == 'mpmath.nan'
     assert p.doprint(S.Infinity) == 'mpmath.inf'
     assert p.doprint(S.NegativeInfinity) == 'mpmath.ninf'
+    assert p.doprint(loggamma(x)) == 'mpmath.loggamma(x)'
 
 
 def test_NumPyPrinter():
diff --git a/sympy/utilities/tests/test_lambdify.py b/sympy/utilities/tests/test_lambdify.py
index 4b61384bec..7889244209 100644
--- a/sympy/utilities/tests/test_lambdify.py
+++ b/sympy/utilities/tests/test_lambdify.py
@@ -905,6 +905,19 @@ def intervalrepr(expr):
     assert isinstance(func1(), mpi)
     assert isinstance(func2(), mpi)
 
+    # To check Is lambdify loggamma works for mpmath or not
+    exp1 = lambdify(x, loggamma(x), 'mpmath')(5)
+    exp2 = lambdify(x, loggamma(x), 'mpmath')(1.8)
+    exp3 = lambdify(x, loggamma(x), 'mpmath')(15)
+    exp_ls = [exp1, exp2, exp3]
+
+    sol1 = mpmath.loggamma(5)
+    sol2 = mpmath.loggamma(1.8)
+    sol3 = mpmath.loggamma(15)
+    sol_ls = [sol1, sol2, sol3]
+
+    assert exp_ls == sol_ls
+
 
 def test_true_false():
     # We want exact is comparison here, not just ==