Daniel Lubenstein
Daniel Lubenstein

Reputation: 13

Is there an easy way to automatically solve for non-given variables?

so let's say you have z = x^2+y, is there an easy way to make python solve for any one of the three non-given variables given the other one? That is, I do not want to specify a function for each i.e.:

z = f(x,y) = x^2+y
y = h(x,z) = z-x^2
x = g(x,z) = sqrt(z-y)

rather I want the universal relation: z=x^2 + y and then python automatically identifying which one to solve for (without me typing a bunch of else if statements).

Upvotes: 0

Views: 71

Answers (1)

tozCSS
tozCSS

Reputation: 6114

from sympy import sympify
from sympy.solvers import solve

expr = sympify('x**2+y-z')
res = expr.subs({'x':2,'y':3})

print(res)  # prints 7 − z
solve(res)  # outputs [7]

solve algebraically solves equations and systems of equations. See more here.

Upvotes: 3

Related Questions