Olivier
Olivier

Reputation: 87

Convert string expression to mathematical expression?

I'm looking for something that would take a mathematical expression string as an input and would change it by adding parentheses to it.

For examples :

Input 1 : "2*4^7"

Output 1 : "(2*(4^7))"

Input 2 : "sin(x)*3-8^9"

Output 2 : "((sin(x)*3)-(8^9))"

Input 3 : "(2-8)/6"

Output 3 : "((2-8)/6)"

I'm currently trying to code it, but it's taking too much time and I would rather focus on other things, so if you guys know some module that could do that, that would be great.

I'm working with python 3

Thank you!

Upvotes: 2

Views: 3511

Answers (2)

shadowmesh
shadowmesh

Reputation: 1

simply use eval(expression)

Replace character in a = '2*4^7' by b=a.replace('^','**') and so on eval(b)

some variable can be used

ex:

x= 3
eval("3 * x **2")

Upvotes: 0

L3viathan
L3viathan

Reputation: 27273

Using the ast module, you can parse your string and then traverse it to build a string representation you require:

import ast

def represent(tree):
    t = type(tree)
    if t == ast.Module:
        return represent(tree.body)
    elif t == list:
        # arithmetic expression should only be one thing
        return ", ".join(represent(part) for part in tree)
    elif t == ast.Expr:
        return represent(tree.value)
    elif t == ast.BinOp:
        o = type(tree.op)
        if o == ast.BitXor:
            op = "^"
        elif o == ast.Mult:
            op = "*"
        elif o == ast.Add:
            op = "+"
        ...
        return "({}{}{})".format(
                represent(tree.left),
                op,
                represent(tree.right),
                )
    elif t == ast.Num:
        return str(tree.n)
    elif t == ast.Name:
        return tree.id
    elif t == ast.Call:
        return "{}({})".format(
                represent(tree.func),
                represent(tree.args),
                )
    ...

# prints (((sin(x)*2)*x)+4)
print(represent(ast.parse("sin(x)*2*x+4")))

As @user2357112 noted in the comments, this will only work if you limit yourself to use Python's expression syntax, most importantly by using ** instead of ^ for exponentiation.

Upvotes: 2

Related Questions