Python equation solver

For a simple equation such as:

x = y + z

You can get a third variable if you bind the other two (i.e.: y = x - zand z = x - y). A simple way to put this in code:

def solve(args):
    if 'x' not in args:
        return args['y'] + args['z']
    elif 'z' not in args:
        return args['x'] - args['y']
    elif 'y' not in args:
        return args['x'] - args['z']
    else:
        raise SomeError  

Obviously, I can take the equation, analyze it and simplify it to achieve the same effect. But I believe that I will reinvent the wheel. So where is my finished wheel?

+3
source share
1 answer

Consider using Sympy . It includes various tools for solving equations and much more.

The following is a snippet of docs :

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

>>> solve(x**4-1, x)
[1, -1, -I, I]
+9
source

All Articles