gberth
gberth

Reputation: 762

Sympy - How to solve a system of equations in terms of x

I want to solve a system of equations with multiple variables.
The solution I'm looking for is in this form y = f(z).

The problem is: sympy won't solve completely my equations.

Here's a small example to illustrate my problem.

x = sympy.symbols("x", real=True)
y = sympy.symbols("y", real=True)
z = sympy.symbols("z", real=True)

eq0 = y - x**2
eq1 = x - z + 4

system = [eq0, eq1]
sympy.solve(system, y)[y]

This code produces y = x^2, which is mathematically correct, but not what I wanted.
What I want to see is my system solved like this y = f(z) therefore (y = (z - 4)^2).

Is there a way to define input and output in sympy's solvers?
Something like this:

sympy.solve(equations, input=z, ouptut=y)

Small note about substitution:
In this naive example, I could substitute the intermediate variable x with z+4 to solve the problem.
That said, I would prefer an alternative solution because, in my real problem, my system has 36 equations and substituting would be difficult.

Upvotes: 1

Views: 2008

Answers (1)

Davide_sd
Davide_sd

Reputation: 13185

You need to tell solve the variables to solve for.

eq0 = y - x**2
eq1 = x - z + 4
system = [eq0, eq1]
# solve your system of equation for x, y
solve(system, [x, y], dict=True)[0][y]
# out: (z - 4)^2

Upvotes: 3

Related Questions