0

Given an expression, we can convert it into a function using sympy.lambdify. Similarly, given a function, we can convert it into an expression by evaluating it at symbol x. We would naturally expect that these two operations are inverses of each other. And, this expected behaviour is displayed when I use polynomial expressions. For example,

import sympy as sym
x = sym.symbols('x')
expr = 5*x**2 + 2*x + 3
f = sym.lambdify([x],expr)
f_expr = f(x)
print(expr == f_expr) 

gives True as its output.

On the other hand, the following code does not run

import sympy as sym 
x = sym.symbols('x')
expr = sym.sin(x)
f = sym.lambdify([x],expr)
f_expr = f(x)
print(expr == f_expr)

and throws the error "TypeError: loop of ufunc does not support argument 0 of type Symbol which has no callable sin method". Could you please explain why this is happening? My guess would be that sym.sin(x) does not return an "expression" analogous to 5x**2 + 2x + 3. But, I would like to understand it a bit better. Thanks in advance.

  • 1
    If you write `f = sym.lambdify([x],expr)`, then `f` shouldn't be called with sympy expressions. `lambdify()` converts a sympy expression to the standard Python world. Note that sympy doesn't work with e.g. numpy arrays, and that numpy functions don't work on sympy expressions. You need to see sympy as a library that lives in its own world. Sympy is compatible with pure Python, but is complicated to make to work with numeric libraries (such as numpy, scipy, sklearn, ....). `lambdify` is used to make the bridge. – JohanC Mar 29 '22 at 11:46

1 Answers1

1

For a non-numeric object the lambdify code tries to do x.sin() with making sure the sin function is from library sympy not numpy to avoid confusions.

you can try :

import sympy as sym 
from sympy import sin

x = sym.symbols('x')
expr = sin(x)

# f = sym.lambdify(x,expr)
f = lambda x:sin(x)
f_expr = f(x)

print(expr == f_expr)
Ran A
  • 746
  • 3
  • 7
  • 19
  • Interesting. But I am confused about two points: 1) I thought lambdify(x,expr) is same as lambda x:expr 2)The code does not work if we replace "lambda x:sin(x)" by "lambda x:expr" even though expr = sin(x). – Divakaran Divakaran Mar 29 '22 at 11:12
  • 1
    Note that apart from the similar name, `lambdify()` is very different from `lambda`. `lambdify()` tries to convert a sympy expression to a Python function eliminating sympy, to make it work e.g. with numpy and matplotlib. `lambda` is a pure Python construction similar to writing a function, e.g. `def my_func(x): return something(x)` could be written as `lambda x: something(x)`. – JohanC Mar 29 '22 at 11:40
  • 2
    `lambdify` translates a sympy expression to a python/numpy function. It is not identical to using the python `lambda` syntax, though sometimes it look similar. Study its docs more. – hpaulj Mar 29 '22 at 11:43
  • for 2) the code does work if we replace "lambda x:sin(x)" by "lambda x:expr" , recheck it – Ran A Mar 29 '22 at 13:14
  • That is weird. It is not working for me. – Divakaran Divakaran Mar 31 '22 at 05:27
  • weird, try it in a different environment , try google colab – Ran A Mar 31 '22 at 08:59