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.

Zeck
  • 6,433
  • 21
  • 71
  • 111

5 Answers5

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})
xubuntix
  • 2,333
  • 18
  • 19
  • 1
    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? – Zeck Aug 10 '11 at 07:04
  • 6
    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. – Zeck Aug 10 '11 at 07:11
  • That's a pretty awesome solution - looks like it's essentially the functionality of [`sympify()`](http://docs.sympy.org/dev/modules/core.html). And if you want to do more custom things, you could look into Python's [`tokenize`](http://docs.python.org/library/tokenize.html#module-tokenize) library as well (just thought that was interesting). – eacousineau Oct 22 '12 at 12:45
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
jamesadney
  • 275
  • 2
  • 6
  • 1
    The SymPy developers do not recommend using parse_expr() as a user level function. https://groups.google.com/forum/#!topic/sympy/o6vsS8VC9ms –  Nov 10 '13 at 04:11
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)
will-hart
  • 3,742
  • 2
  • 38
  • 48
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
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • 4
    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. – eacousineau Oct 22 '12 at 12:39
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
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111