Python 解一元方程

看到这个解法惊呆了:

def solve(eq,var='x'):
    eq1 = eq.replace("=","-(")+")"
    c = eval(eq1,{var:1j})
    return -c.real/c.imag

>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0

数学真是奇妙啊!

4 comments