Mathematical Equations - Rendering and Evaluation with Python and QT (and sympy?)

你离开我真会死。 提交于 2019-12-14 02:06:43

问题


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. symbolic: sigma=N/A
  2. with values: sigma=200kN/20cm²
  3. as a result: sigma=10kN/cm²

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)

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?

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


回答1:


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.

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.

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.

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.



来源:https://stackoverflow.com/questions/21466204/mathematical-equations-rendering-and-evaluation-with-python-and-qt-and-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!