How to calculate expression using sympy in python
Asked Answered
L

5

25

I need a calculate below expression using sympy in python?

exp = '(a+b)*40-(c-a)/0.5'

In a=6, b=5, c=2 this case how to calculate expression using sympy in python? Please help me.

Lamothe answered 10/8, 2011 at 6:18 Comment(0)
N
38

The documentation is here: http://docs.sympy.org/. You should really read it!

To "calculate" your expression, write something like this:

from sympy import Symbol
a = Symbol("a")
b = Symbol("b")
c = Symbol("c")
exp = (a+b)*40-(c-a)/0.5

And that's it. If you meant something else by "calculate", you could also solve exp = 0:

sympy.solve(exp)
> {a: [0.0476190476190476*c - 0.952380952380952*b],
>  b: [0.05*c - 1.05*a],
>  c: [20.0*b + 21.0*a]}

For everything else, you should really read the docs. Maybe start here: http://docs.sympy.org/0.7.1/tutorial.html#tutorial

UPDATE: since you added the values for a, b, c to the question, you can add this to the solution:

exp.evalf(subs={a:6, b:5, c:2})
Norman answered 10/8, 2011 at 6:32 Comment(3)
Thank you for reply. But I have a problem. Now I need a convert string to sympy object /convert string expression to sympy object/. How to convert it?Lamothe
I found a solution. First "from sympy import S, Symbol" then "exp = S('a+5')". In finally "exp.evalf(subs={'a':7})". Thank you guys.Lamothe
That's a pretty awesome solution - looks like it's essentially the functionality of sympify(). And if you want to do more custom things, you could look into Python's tokenize library as well (just thought that was interesting).Kohler
C
15

You can convert your string into a sympy expression using the parse_expr() function in the module sympy.parsing.sympy_parser.

>>> from sympy.abc import a, b, c
>>> from sympy.parsing.sympy_parser import parse_expr
>>> sympy_exp = parse_expr('(a+b)*40-(c-a)/0.5')
>>> sympy_exp.evalf(subs={a:6, b:5, c:2})
448.000000000000
Codon answered 5/11, 2012 at 3:28 Comment(1)
The SymPy developers do not recommend using parse_expr() as a user level function. groups.google.com/forum/#!topic/sympy/o6vsS8VC9msWasherwoman
B
7

I realise this was already answered above, but in the case of getting a string expression with unknown symbols and needing access to those symbols, here is the code I used

# sympy.S is a shortcut to sympify
from sympy import S, Symbol

# load the string as an expression
expression = S('avar**2 + 3 * (anothervar / athirdvar)')

# get the symbols from the expression and convert to a list
# all_symbols = ['avar', 'anothervar', 'athirdvar']
all_symbols = [str(x) for x in expression.atoms(Symbol)]

# do something with the symbols to get them into a dictionary of values
# then we can find the result. e.g.
# symbol_vals = {'avar': 1, 'anothervar': 2, 'athirdvar': 99}
result = expression.subs(symbols_vals)
Burgomaster answered 11/2, 2013 at 23:10 Comment(0)
H
6

Well, I know that eval is evil, but if you have a,b and c defined in your program, and you can make sure that it's safe to do the eval, you don't need sympy.

>>> a=5
>>> b=5
>>> c=2
>>> exp = '(a+b)*40-(c-a)/0.5'
>>> eval(exp)
406.0
Horned answered 10/8, 2011 at 6:31 Comment(1)
I don't see why this answer would be down-voted. It achieves what the OP was asking for, while also giving a word of caution.Kohler
T
6
>>> a, b, c = sympy.symbols('a b c')
>>> exp = (a + b) * 40 - (c - a) / 0.5
>>> exp.evalf(6, subs={a:6, b:5, c:2})
448.000
Tabbie answered 10/8, 2011 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.