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

LambertW displaying in jupyter lab #21812

Closed
vasarm opened this issue Aug 2, 2021 · 4 comments · Fixed by #21849
Closed

LambertW displaying in jupyter lab #21812

vasarm opened this issue Aug 2, 2021 · 4 comments · Fixed by #21849
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. printing.latex

Comments

@vasarm
Copy link

vasarm commented Aug 2, 2021

When using JupyterLab and trying to raise LambertW function to some power (for example multiplying with itself) then it raises error when trying to display it. Same thing happens if I use sp.Pow() method as well.

For example sp.Pow(sp.LambertW(2), 2) causes the error as well.

In [1]: import sympy as sp

In [2]: function = sp.LambertW(2) * sp.LambertW(2)

In [3]: function

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\miniconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~\miniconda3\lib\site-packages\sympy\core\_print_helpers.py in _repr_latex_(self)
     62         """
     63         from sympy.printing.latex import latex
---> 64         s = latex(self, mode='plain')
     65         return "$\\displaystyle %s$" % s

~\miniconda3\lib\site-packages\sympy\printing\printer.py in __call__(self, *args, **kwargs)
    371 
    372     def __call__(self, *args, **kwargs):
--> 373         return self.__wrapped__(*args, **kwargs)
    374 
    375     @property

~\miniconda3\lib\site-packages\sympy\printing\latex.py in latex(expr, **settings)
   2946 
   2947     """
-> 2948     return LatexPrinter(settings).doprint(expr)
   2949 
   2950 

~\miniconda3\lib\site-packages\sympy\printing\latex.py in doprint(self, expr)
    252 
    253     def doprint(self, expr):
--> 254         tex = Printer.doprint(self, expr)
    255 
    256         if self._settings['mode'] == 'plain':

~\miniconda3\lib\site-packages\sympy\printing\printer.py in doprint(self, expr)
    289     def doprint(self, expr):
    290         """Returns printer's representation for expr (as a string)"""
--> 291         return self._str(self._print(expr))
    292 
    293     def _print(self, expr, **kwargs):

~\miniconda3\lib\site-packages\sympy\printing\printer.py in _print(self, expr, **kwargs)
    327                 printmethod = '_print_' + cls.__name__
    328                 if hasattr(self, printmethod):
--> 329                     return getattr(self, printmethod)(expr, **kwargs)
    330             # Unknown object, fall back to the emptyPrinter.
    331             return self.emptyPrinter(expr)

~\miniconda3\lib\site-packages\sympy\printing\latex.py in _print_Pow(self, expr)
    649         else:
    650             if expr.base.is_Function:
--> 651                 return self._print(expr.base, exp=self._print(expr.exp))
    652             else:
    653                 tex = r"%s^{%s}"

~\miniconda3\lib\site-packages\sympy\printing\printer.py in _print(self, expr, **kwargs)
    327                 printmethod = '_print_' + cls.__name__
    328                 if hasattr(self, printmethod):
--> 329                     return getattr(self, printmethod)(expr, **kwargs)
    330             # Unknown object, fall back to the emptyPrinter.
    331             return self.emptyPrinter(expr)

TypeError: _print_LambertW() got an unexpected keyword argument 'exp'
@oscarbenjamin
Copy link
Contributor

This can be fixed with something like:

diff --git a/sympy/printing/latex.py b/sympy/printing/latex.py
index debc81ca8d..5bf0cff782 100644
--- a/sympy/printing/latex.py
+++ b/sympy/printing/latex.py
@@ -2422,11 +2422,15 @@ def _print_DMF(self, p):
     def _print_Object(self, object):
         return self._print(Symbol(object.name))
 
-    def _print_LambertW(self, expr):
+    def _print_LambertW(self, expr, exp=None):
+        arg0 = self._print(expr.args[0])
+        exp = r'^{%s}' % (exp,) if exp is not None else ''
         if len(expr.args) == 1:
-            return r"W\left(%s\right)" % self._print(expr.args[0])
-        return r"W_{%s}\left(%s\right)" % \
-            (self._print(expr.args[1]), self._print(expr.args[0]))
+            result = r"W%s\left(%s\right)" % (exp, arg0)
+        else:
+            arg1 = self._print(expr.args[1])
+            result = r"W_{%s}%s\left(%s\right)" % (exp, arg0, arg1)
+        return result
 
     def _print_Morphism(self, morphism):
         domain = self._print(morphism.domain)

@oscarbenjamin oscarbenjamin added printing.latex Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. labels Aug 2, 2021
skirpichev added a commit to skirpichev/diofant that referenced this issue Aug 3, 2021
@Arnab1181412
Copy link

Hi Can I get to solve this problem I am a new contributor. Can you guide me for the same.

@mohajain
Copy link
Contributor

mohajain commented Aug 3, 2021

This can be fixed with something like:

I have made the changes suggested by @oscarbenjamin. Is there anything else that needs to be done?

@BalasubramanyamEvani
Copy link
Contributor

Hello @oscarbenjamin could you please review my PR - #21849

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. printing.latex
Projects
None yet
5 participants