uNxe uNxe - 7 months ago 21
Python Question

sympy expression to symbols

I want to take an expression and split it by symbols. For example

rate = symbols('0')
def add_rate(self, new_rate):
self.rate = self.rate + symbols(new_rate)

add_rate('-k\*A\*E')


So the whole thing is
0 + -k\*A\*E
. After that I want to change
k
to
0.01
with method
subs(symbol('k'), 0.01)
to be
0 + -0.01\*A\*E
and I can't cause whole expression
-k\*A\*E
is the symbol.

Maybe do you know how to change
k
to
0.01
?

Answer

Solusion is using the sympy.parsing.sympy_parser opportunities.

from sympy.parsing.sympy_parser import *
rate = symbols('0')
def add_rate(self, new_rate):
self.rate = self.rate + parse_expr(new_rate.rate_string)