Algorithm to invert strings of algebraic expressions in Python
Asked Answered
T

2

5

Is there an easy way to make a function to inverse an algorithm for example like this:

>>> value = inverse("y = 2*x+3")
>>> print(value)
"x = (y-3)/2"

If you can't make actual code for the function, please recommend me tools that would make this task easier. The function would be only used to inverse algorithms with +, -, * and /

Tillie answered 5/6, 2013 at 20:12 Comment(1)
Parse the expression into a AST. Walk the AST, inverting it. Decompile the AST. Profit.Russom
N
7

You should try SymPy for doing that:

from sympy import solve
from sympy.abc import x, y

e = 2*x+3-y

solve(e,x)
#[y/2 - 3/2]
solve(e,y)
#[2*x + 3]

Based on this, you can build your inverse() like (works for two variables):

def inverse(string, left_string=None):
    from sympy import solve, Symbol, sympify
    string = '-' + string
    e = sympify(string.replace('=','+'))
    if left_string:
        ans = left_string + ' = ' + str(solve(e, sympify(left_string))[0])
    else:
        left = sympify(string.split('=')[0].strip().replace('-',''))
        symbols = e.free_symbols
        symbols.remove( left )
        right = list(symbols)[0]
        ans = str(right) + ' = ' + str(solve(e, right)[0])
    return ans

Examples:

inverse(' x = 4*y/2')
#'y = x/2'

inverse(' y = 100/x + x**2')
#'x = -y/(3*(sqrt(-y**3/27 + 2500) + 50)**(1/3)) - (sqrt(-y**3/27 + 2500) + 50)**(1/3)'

inverse("screeny = (isox+isoy)*29/2.0344827586206895", "isoy")
#'isoy = -isox + 0.0701545778834721*screeny'
Neomineomycin answered 5/6, 2013 at 20:17 Comment(3)
I think using sympy.sympify and .free_symbols could simplify this a bit.Cortico
DSM, could you give an example, please? Thank you sgpc, I will try this for my project.Tillie
@DSM, thank you! I updated the answer using sympify, but what about free_symbols?Neomineomycin
C
3

This is a little long for a comment, but here's the sort of thing I had in mind:

import sympy

def inverse(s):
    terms = [sympy.sympify(term) for term in s.split("=")]
    eqn = sympy.Eq(*terms)
    var_to_solve_for = min(terms[1].free_symbols)
    solns = sympy.solve(eqn, var_to_solve_for)
    output_eqs = [sympy.Eq(var_to_solve_for, soln) for soln in solns]
    return output_eqs

After which we have

>>> inverse("y = 2*x+3")
[x == y/2 - 3/2]
>>> inverse("x = 100/z + z**2")
[z == -x/(3*(sqrt(-x**3/27 + 2500) + 50)**(1/3)) - (sqrt(-x**3/27 + 2500) + 50)**(1/3), z == -x/(3*(-1/2 - sqrt(3)*I/2)*(sqrt(-x**3/27 + 2500) + 50)**(1/3)) - (-1/2 - sqrt(3)*I/2)*(sqrt(-x**3/27 + 2500) + 50)**(1/3), 
z == -x/(3*(-1/2 + sqrt(3)*I/2)*(sqrt(-x**3/27 + 2500) + 50)**(1/3)) - (-1/2 + sqrt(3)*I/2)*(sqrt(-x**3/27 + 2500) + 50)**(1/3)]

etc.

Cortico answered 5/6, 2013 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.