SymPy 根据另一个表达变量 [英] SymPy express variable in terms of another

查看:33
本文介绍了SymPy 根据另一个表达变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 的 SymPy 库.我有两个 sympy 符号和表达式来绑定它们:

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

我如何用x"来表达y",即获得相等性:

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

y = (2/7) * x

谢谢.

推荐答案

这是你如何用 x 来表达这个方程:

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]

这是可行的,因为如果 solve() 函数提供的不是完整方程,它会假定提供的表达式等于 0.换句话说,写

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

上面相当于写

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

这会告诉 SymPy

which would tell SymPy that

2x - 7y = 0.

这篇关于SymPy 根据另一个表达变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆