Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determining numerator and denominator in a relatively complex mathematical expression using python

I'm trying to convert calculator input to LaTeX. if a user inputs this:

(3x^(x+(5/x)+(x/33))+y)/(32 + 5)

I have to convert it to this:

frac{3x^(x+frac{5}{x}+frac{x}{33})+y}{32 + 5x}

however I am having issues with determining when a numerator begins and ends. Any suggestions?

like image 669
tipu Avatar asked Feb 09 '11 08:02

tipu


1 Answers

Have a look at compiler

compiler.parse('(3*x**(x+(5/x)+(x/33))+y)/(32 + 5)')

returns

Module(None, Stmt([Discard(Div((Add((Mul((Const(3), Power((Name('x'), Add((Add((Name('x'), Div((Const(5), Name('x'))))), Div((Name('x'), Const(33))))))))), Name('y'))), Add((Const(32), Const(5))))))]))

which could be more easily converted to LaTeX code. You will have to write methods, that handle each code (Div, Add, Const, Name, Power,...) and its parameters recursively and return appropriate LateX code.

like image 79
eumiro Avatar answered Sep 22 '22 10:09

eumiro