如何使用python中的sympy来计算表达式 [英] How to calculate expression using sympy in python

查看:221
本文介绍了如何使用python中的sympy来计算表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在python中使用sympy来计算以下表达式?

I need a calculate below expression using sympy in python?

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

a = 6 b = 5 c = 2 这种情况下如何使用python中的sympy来计算表达式?请帮助我。

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

推荐答案

文档在这里: http://docs.sympy.org/ 。你应该真正阅读它!

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

就是这样。如果你的意思是计算,你也可以解决exp = 0:

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]}

对于其他一切,你应该真正阅读文档。也许从这里开始: http://docs.sympy.org/0.7.1/tutorial .html#tutorial

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

更新:由于您将a,b,c的值添加到该问题中,您可以将其添加到解决方案中: p>

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})

这篇关于如何使用python中的sympy来计算表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆