Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python math expressions to latex, verbatim (no reordering, factoring, etc.)

Tags:

python

latex

I have input of a whole lot of math expressions and equations, and I'd like to print out latex representation for each on them. So far I have tried Sage and sympy, but the tricky part is to not-reorder terms in expressions.

So, if my input is this, something that can be eval-ed in python:

(C - A*x) / B

I want output that will be something like this:

\frac{C - A x}{B}

What I don't want is something like this:

\frac{-(A x - C)}{B}
\frac{1}{B}(C - A x)
etc...

Can this be achieved? I'm slowly losing hope...


EDIT:

The input expressions are diverse, some containing square roots, nested parentheses, exponents etc. Looking for a generic solution.

Here is what doesn't work so far:

1) Sage:

sage: var('A B C x y') 
(A, B, C, x, y) 
sage: latex(y == (C - A*x) / B)
y = -\frac{A x - C}{B}

2) sympy:

>>> from sympy import *
>>> x = Symbol('x')
>>> A = Symbol('A')
>>> B = Symbol('B')
>>> C = Symbol('C')
>>> latex((C - A*x) / B)
'\\frac{1}{B} \\left(- A x + C\\right)'
like image 928
frnhr Avatar asked Oct 30 '13 20:10

frnhr


2 Answers

Short of writing your own parser, I believe the only real way to do this is to use python's built-in compile() function and process the returned abstract syntax tree.

like image 103
Jim Garrison Avatar answered Sep 29 '22 16:09

Jim Garrison


\You can do this by creating Symbol and Operator classes that implement the standard python data model (http://docs.python.org/2/reference/datamodel.html). This will keep things in the same order of python operator precedence, although you can rearrange via parens:

class Symbol(object):
    def __init__(self, name):
        self._name = name

    def __str__(self):
        return str(self._name)

    def __div__(self, other):
        return Div(self, other)

    def __mul__(self, other):
        return Mult(self, other)

    def __add__(self, other):
        return Add(self, other)

    def __sub__(self, other):
        return Sub(self, other)

    def __rdiv__(self, other):
        return Div(other, self)

    def __rmul__(self, other):
        return Mult(other, self)

    def __radd__(self, other):
        return Add(other, self)

    def __rsub__(self, other):
        return Sub(other, self)

class Operation(Symbol):
    def __init__(self, a, b, op):
        self._a = a
        self._b = b
        self._op = op

    def __str__(self):
        return self._op.format(self._a, self._b)

class Add(Operation):
    precedence = 0

    def __init__(self, a, b):
        super(Add, self).__init__(a, b, "{0} + {1}")

class Sub(Operation):
    precedence = 0
    def __init__(self, a, b):
        super(Sub, self).__init__(a, b, "{0} - {1}")

class Mult(Operation):
    precedence = 1
    def __init__(self, a, b):
        if isinstance(a, Operation) and a.precedence < Mult.precedence:
            a_form = "({0})"
        else:
            a_form = "{0}"
        if isinstance(b, Operation) and b.precedence < Mult.precedence:
            b_form = "({1})"
        else:
            b_form = "{1}"
        super(Mult, self).__init__(a, b, a_form + " " + b_form)

class Div(Operation):
    precedence = 1
    def __init__(self, a, b):
        super(Div, self).__init__(a, b, "\\frac{{{0}}}{{{1}}}")


A = Symbol('A')
B = Symbol('B')
C = Symbol('C')
x = Symbol('x')

Then:

>>> print (C - A * x) / (B)
\frac{C - A x}{B}
>>> print (C * (A + B))
C (A + B)
>>> print (C * (A + B + A + B + C + x))
C (A + B + A + B + C + x)
like image 36
John Spong Avatar answered Sep 29 '22 16:09

John Spong