SymPy express variable in terms of another

一笑奈何 提交于 2019-12-10 17:20:28

问题


I am using SymPy lib for Python. I have two sympy symbols and expression that binds them:

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

How can i express 'y' in terms of 'x', i.e get the equality:

y = (2/7) * x

Thanks.


回答1:


This 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 the solve() function is presented with something that is not a full equation, it assumes that the provided expression is equal to zero. In other words, writing

expr = 2*x - 7*y

above is equivalent to writing

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

which would tell SymPy that

2x - 7y = 0.


来源:https://stackoverflow.com/questions/29645999/sympy-express-variable-in-terms-of-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!