Question

I am trying to manipulate variables in sympy, so that after I can input them into a Python function which requires the input to be “normal” Python code. For example:

I would like to input (where x is a sympy symbol):

y = x**3 +  x**2 + 3*x +5

Then, in the same code I would later like to be able to insert y into another function, lets say:

OtherFunction(y)

where y is no longer the “type” sympy symbol, but rather interpreted as if I had directly input

OtherFunction(x**3 + x**2 + 3*x +5)

Is there a way to do this?

Était-ce utile?

La solution

This is already how Python works. When you define y as you show, you are creating a Python variable, y, that has the value of a SymPy expression. So if you call some function, passing y, you are actually passin the expression:

>>> y = x**3 +  x**2 + 3*x +5
>>> def foo(x):
...  print 'got',x
...
>>> foo(y)
got x**3 + x**2 + 3*x + 5
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top