数学方程式-使用Python和QT渲染和评估(以及sympy?) [英] Mathematical Equations - Rendering and Evaluation with Python and QT (and sympy?)

查看:88
本文介绍了数学方程式-使用Python和QT渲染和评估(以及sympy?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python3和QT开发GUI应用程序(在土木工程环境中),并希望以三种不同的方式显示方程式:

I am developing a GUI application (in the civil engineering context) with python3 and QT and want to display an equation in three different ways:

  1. 符号:sigma = N/A
  2. 值:sigma = 200kN/20cm²
  3. 结果为:
  4. :sigma = 10kN/cm²

(1)和(2)的等式布局和符号顺序必须相同,但是我只想在源代码中输入一次等式.我搜索了很多,这是我能得到的最好的结果:

The layout of the equation and the order of symbols has to be the same for both (1) and (2), but i only want to enter the equation once in my sourcecode. I searched a lot, this is the best i could get:

class myfancy_equation():
    def __init__(self):
        self.a = Symbol('a')
        self.b = Symbol('b',commutative=False)
        self.x = Symbol('x')
        self.expr = (self.b * self.a)/self.x
    def mlatex(self):
        return latex(self.expr)
    def mevaluate(self,a_in,b_in,x_in):
        unev = self.expr.subs({self.a:a_in,self.b:b_in,self.x:x_in})
        symb_names = dict()
        symb_names[self.a] = str(a_in) 
        symb_names[self.b] = str(b_in) 
        symb_names[self.x] = str(x_in) 
        # unev_latex = latex(self.expr.subs({self.a:a_in,self.b:b_in,self.x:x_in}))
        unev_latex = latex(self.expr,symbol_names=symb_names)
        ev = self.expr.subs({self.a:a_in,self.b:b_in,self.x:x_in}).evalf()
        return unev,unev_latex,ev

mfe = myfancy_equation()

lat = mfe.mlatex()
un,unl,ev = mfe.mevaluate(5,7,3)

print("Original, unevaluated, evaluated:")
print( lat,"=",unl,'=',ev)

我已经读过sympy并不是最初为显示方程式而开发的,但是对于更复杂的方程式,结果几乎是不可读的(并且是不可预测的).我尝试使用可交换的"参数,但总是以这样的混合方程式结束: http://www.oberthanner.at/render.png 我是否错过了要点,或者sympy不可能吗?

I have read that sympy was not primarly developed for displaying equations, but the result is hardly readable (and unpredictable) for more complex equations. i tried playing around with the "commutative" parameter, but always end up with a mixed equation like this: http://www.oberthanner.at/render.png am i missing the point or is it just impossible with sympy?

顺便说一句:使用python2时,我遇到了换向参数的另一种行为.

btw: i encountered a different behaviour of the commutative parameter when using python2.

推荐答案

commutative=False只会将该一个符号标记为不可交换的. SymPy将把通勤部分(在本例中为其他所有部分)放在首位,并以正确的顺序紧跟其后的是非通勤符号.

commutative=False will only mark that one symbol as non-commutative. SymPy will put the commuting part (in this case, everything else) first, and follow it by the non-commuting symbols in the correct order.

但是您不应该使用它.它不会提供您想要的东西(例如,如果ab是不可交换的,则会得到a*b**-1而不是a/b,因为a*b**-1b**-1*a是不同的).

But you shouldn't use that anyhow. It will not give you what you want (e.g., you'll get a*b**-1 instead of a/b if a and b are noncommutative, since a*b**-1 and b**-1*a are different).

我建议只获取想要的各个部分的乳胶,然后使用字符串格式按照想要的顺序将它们拼接在一起.

I would recommend just getting the latex for the individual parts that you want, and piecing them together in the order you want using string formatting.

或者,您可以编写自己的打印机,以所需的方式订购商品.参见 http://docs.sympy.org/latest/modules/printing.html如果您对采用这种方法感兴趣,并且还应该阅读现有打印机的源代码,因为您可能只想采用已有的内容并对其进行一些修改.如果您希望更通用,则此方法很好,并且基本字符串连接太混乱了.如果您显示的示例非常复杂,则可能会适得其反,但是如果您需要支持可能任意复杂的表达式,则最好这样做.

Alternately, you can write your own printer that orders things the way you want. See http://docs.sympy.org/latest/modules/printing.html if you are interested in taking that route, and you should also read the source code for the existing printer, since you'll probably want to just take what is already there and modify it a little. This method is good if you want to be more general, and the basic string concatenation gets too messy. If the example you showed is as complicated as it will get, it may be overkill, but if you need to support potentially arbitrarily complicated expressions, it may be better to do it this way.

如果您决定采用第二种方法,并且需要编写自定义打印机的帮助,请随时在此处或在 SymPy邮件列表.

If you decide to take that second route and need help writing a custom printer, feel free to ask here or on the SymPy mailing list.

这篇关于数学方程式-使用Python和QT渲染和评估(以及sympy?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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