Python中的数学方程式处理 [英] Mathematical equation manipulation in Python
问题描述
我想开发一个显示给定数学方程的GUI应用程序.当您单击方程式中的某个特定变量以表示它是未知变量(即要计算)时,该方程式将自行转换以评估所需的未知变量.
I want to develop a GUI application which displays a given mathematical equation. When you click upon a particular variable in the equation to signify that it is the unknown variable ie., to be calculated, the equation transforms itself to evaluate the required unknown variable.
例如:
a = (b+c*d)/e
让我们假设我单击"d"表示它是未知变量.然后,应将方程式重新构建为:
d = (a*e - b)/c
到目前为止,我只想知道如何根据用户输入重新排列给定的方程式.我从哥哥那里得到的一个建议是在后端使用前缀/后缀符号表示法对其进行评估.
As of now, I just want to know how I can go about rearranging the given equation based on user input. One suggestion I got from my brother was to use pre-fix/post-fix notational representation in back end to evaluate it.
这是唯一的解决方法还是有更简单的建议? 另外,我不仅将使用基本的数学函数,而且还将使用三角学和微积分(我认为是基本的.没有偏微分微积分等等).我认为,前/后注记法评估可能对评估更高的数学函数没有帮助.
Is that the only way to go or is there any simpler suggestion? Also, I will be using not only basic mathematical functions but also trignometric and calculus (basic I think. No partial differential calculus and all that) as well. I think that the pre/post-fix notation evaluation might not be helpful in evaluation higher mathematical functions.
但这只是我的意见,所以请指出我是否错. 另外,我将使用 SymPy 进行数学评估,因此对给定数学方程的评估不会有问题,从给定的通用方程式创建一个特定的方程式是我的主要问题.
But that is just my opinion, so please point out if I am wrong. Also, I will be using SymPy for mathematical evaluation so evaluation of a given mathematical equation is not a problem, creating a specific equation from a given generic one is my main problem.
推荐答案
使用 SymPy ,您的示例将会有所帮助像这样:
Using SymPy, your example would go something like this:
>>> import sympy
>>> a,b,c,d,e = sympy.symbols('abcde')
>>> r = (b+c*d)/e
>>> l = a
>>> r = sympy.solve(l-r,d)
>>> l = d
>>> r
[(-b + a*e)/c]
>>>
它似乎也适用于三角函数:
It seems to work for trigonometric functions too:
>>> l = a
>>> r = b*sympy.sin(c)
>>> sympy.solve(l-r,c)
[asin(a/b)]
>>>
由于您使用的是GUI,因此(可能)您希望将字符串从字符串转换为表达式:
And since you are working with a GUI, you'll (probably) want to convert back and forth from strings to expressions:
>>> r = '(b+c*d)/e'
>>> sympy.sympify(r)
(b + c*d)/e
>>> sympy.sstr(_)
'(b + c*d)/e'
>>>
,或者您可能希望将它们显示为呈现的 LaTeX或MathML .
or you may prefer to display them as rendered LaTeX or MathML.
这篇关于Python中的数学方程式处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!