Implementaion of particular cases in a mathtext to wxbitmap function

放肆的年华 提交于 2019-12-25 04:31:22

问题


I am displaying a bitmap like this

using the code :

mt =r'$\frac{%.5fx^{2} + %.5fx + %.5f}{%.5fx^{2} + %.5fx + %.5f}$'%(self.z1[-3],self.z1[-2],self.z1[-1],self.z3[-3],self.z3[-2],self.z3[-1])
bm = mathtext_to_wxbitmap(mt)
self.picture.SetBitmap(bm)  

Here the function mathtext_to_wxbitmap is implemented as :

from matplotlib.mathtext import MathTextParser
mathtext_parser = MathTextParser("Bitmap")
def mathtext_to_wxbitmap(s):
    ftimage, depth = mathtext_parser.parse(s, 150)
    return wx.BitmapFromBufferRGBA(
        ftimage.get_width(), ftimage.get_height(),
        ftimage.as_rgba_str())

The z1 and z3 in the first code snippet are lists whose value keep changing and accordingly the bitmap keeps updating. This all works fine.

The problem is in the following cases :

Case 1:

When one of the coefficient is 0.

I want that the term 0.00000x2 should not be displayed at all if the coefficient is 0.

Case 2:

When a coefficient is negative.

Instead of +-2.12321 I want only -2.12321 if the coefficient is negative.

Case 3:

When all the coefficients are integers

I am getting this -

But I want it to be like-

Any help in solving any of the above cases is appreciated. I am open to implementing this in a completely different way if there is one.


回答1:


Yes, I also was noting that 0 * x**2 does not look very nice. I think what you want to do here is out of the scope of libraries like matplotlib.

You need something which "understands" mathematic formulas, like Mathematica or Mathcad or …. You would end up writing your own output formatter.

The only ready-made thing which comes to my mind in python is the sympy library. It can render formulas to text and will also do simplification on formulas. If you want to give it a shot, it also has a PrettyPrinter renderer outputting unicode formulas for unicode-aware terminals.

I presume its output could be displayed 1:1 in a wxPython text box if you can use the same unicode font (untested!).



来源:https://stackoverflow.com/questions/31269509/implementaion-of-particular-cases-in-a-mathtext-to-wxbitmap-function

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