SymPy expresses a variable in terms of another

I am using SymPy lib for Python. I have two simplex characters and expressions that bind them:

x = Symbol('x')
y = Symbol('y')
expr = 2 * x - 7 * y

How can I express "y" in terms of "x", then we get the equality:

y = (2/7) * x

Thank.

+3
source share
1 answer

Here is how you can express this equation in terms of x:

In [1]: from sympy import *

In [2]: x, y = symbols('x, y')

In [3]: expr = 2*x - 7*y

In [4]: solve(expr, y)
Out[4]: [2*x/7]

This works because if solve () is represented by something that is not a complete equation, it assumes that the expression provided is zero. In other words, the record

expr = 2*x - 7*y

above is equivalent to writing

expr = Eq(2*x - 7*y, 0)

which tells SymPy that

2x - 7y = 0.
+5
source

Source: https://habr.com/ru/post/1666921/


All Articles