sympy icon indicating copy to clipboard operation
sympy copied to clipboard

Lambdify doesn't recognize derivative symbol if cse is enabled

Open tjstienstra opened this issue 11 months ago • 12 comments

Here is a minimal reproducer:

>>> import sympy as sm   
>>> t = sm.symbols("t")
>>> x = sm.Function("x")(t)
>>> xd = x.diff(t)
>>> sm.lambdify((xd, x), xd + x)(1, 1)
2
>>> sm.lambdify((xd, x), xd, cse=True)(1, 1)     
1
>>> sm.lambdify((xd, x), xd + x, cse=True)(1, 1) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\sympy\utilities\lambdify.py", line 875, in lambdify
    funcstr = funcprinter.doprint(funcname, iterable_args, _expr, cses=cses)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\sympy\utilities\lambdify.py", line 1166, in doprint
    str_expr = _recursive_to_string(self._exprrepr, expr)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\sympy\utilities\lambdify.py", line 961, in _recursive_to_string
    return doprint(arg)
           ^^^^^^^^^^^^
  File "...\sympy\printing\codeprinter.py", line 172, in doprint
    lines = self._print(expr).splitlines()
            ^^^^^^^^^^^^^^^^^
  File "...\sympy\printing\printer.py", line 331, in _print
    return printmethod(expr, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\sympy\printing\str.py", line 57, in _print_Add
    t = self._print(term)
        ^^^^^^^^^^^^^^^^^
  File "...\sympy\printing\printer.py", line 331, in _print
    return printmethod(expr, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\sympy\printing\codeprinter.py", line 582, in _print_not_supported
    raise PrintMethodNotImplementedError("Unsupported by %s: %s" % (str(type(self)), str(type(expr))) + \
sympy.printing.codeprinter.PrintMethodNotImplementedError: Unsupported by <class 'sympy.printing.numpy.SciPyPrinter'>: <class 'sympy.core.function.Derivative'>
Set the printer option 'strict' to False in order to generate partially printed code.

I am not sure if this issue is easily solvable as the problem seems to be that it tries to print the expression x0 + Derivative(x0, t) with the [(x0, x(t))] as subexpressions.

tjstienstra avatar Mar 25 '24 16:03 tjstienstra