简体   繁体   English

如何使用 Sympy 根据指定变量重新排列方程?

[英]How do I rearrange an equation to be in terms of a specified variable with Sympy?

I can write, for example,例如,我可以写,

Line(Point(3,-4), Point(-2,2)).equation()

to generate an equation of a line that passes through those points, but the output is given as生成通过这些点的直线方程,但 output 给出为

-6x - 5y - 2

presumably being equivalent to -6x - 5y - 2 = 0 .大概相当于-6x - 5y - 2 = 0 How can I instead set the output to be我怎样才能将 output 设置为

y = (-6/5)x - (2/5)

I thought it might have to do with some formatting settings in the equation() method, so I checked the documention , but it didn't say anything about it.我认为这可能与equation()方法中的一些格式设置有关,所以我检查了文档,但它没有说明任何内容。

EDIT 1: It appears that if I input编辑 1:看来,如果我输入

solve(Eq(Line(Point(3,-4), Point(-2,2)).equation()))

I will get the (albeit ugly) output我会得到(虽然很丑)output

⎡⎧     5⋅y   1⎫⎤
⎢⎨x: - ─── - ─⎬⎥
⎣⎩      6    3⎭⎦

This does give a rearranged solution, but the issue is that this is only in the form of x=f(y) .这确实给出了重新排列的解决方案,但问题是这只是x=f(y)的形式。 I'm not sure how I would get it to instead be in terms of y=f(x) .我不确定我将如何得到它,而不是y=f(x)

EDIT 2: I think this might actually be a bug with solve() , or Eq() .编辑 2:我认为这实际上可能是solve()Eq()的错误。 If I instead manually type如果我改为手动输入

solve(-6*x-5*y-2,y)

or或者

solve(Eq(-6*x-5*y-2),y)

I will get the (somewhat ugly, but correct) output of我会得到(有点难看,但正确的)output

⎡  6⋅x   2⎤
⎢- ─── - ─⎥
⎣   5    5⎦

Now if I were to instead type现在,如果我改为输入

solve(Eq(Line(Point(3,-4), Point(-2,2)).equation()),y)

or或者

solve(Eq(Line(Point(3,-4), Point(-2,2)).equation()),x)

I get the output of我得到了 output

[]

This is rather strange, though, because不过,这很奇怪,因为

Eq(-6*x-5*y-2)

and

Eq(Line(Point(3,-4),Point(-2,2)).equation())

both output output

-6⋅x - 5⋅y - 2 = 0

so I'm really not sure what Is going on here.所以我真的不确定这里发生了什么。

Since you need to pass the symbol you want to solve for, you can do something like this (Although the equation function expects strings )由于您需要传递要求解的符号,因此可以执行以下操作(尽管方程式function 需要字符串)

x, y = symbols('x, y')
eq = (Line(Point(3,-4), Point(-2,2)).equation(x,y))

print(solve(eq, y))# prints [-6*x/5 - 2/5]

or you can get the symbol from the expression and pass it to solve like或者您可以从表达式中获取符号并将其传递给解决

eq = (Line(Point(3,-4), Point(-2,2)).equation())

print(solve((eq), list(eq.atoms(Symbol))[1])) # prints [-6*x/5 - 2/5]

The symbols used in the equation have assumptions on them and that makes them different from the plain symbols you might create with Symbol('x') .等式中使用的符号对它们有假设,这使得它们与您可能使用Symbol('x')创建的普通符号不同。 So that's why it has the option to pass in the symbols you want to use for x and y .这就是为什么它可以选择传入要用于xy的符号。

>>> var('x y')
(x, y)
>>> Line(Point(3,-4), Point(-2,2)).equation(x,y)
-6*x - 5*y - 2
>>> Eq(y, solve(_, y)[0])
Eq(y, -6*x/5 - 2/5)

That's also why you didn't get a solution in one of your examples -- the variable you were solving for wasn't in the equation.这也是为什么您在其中一个示例中没有得到解决方案的原因——您要解决的变量不在等式中。 It looked the same but it wasn't since it had different assumptions on it.看起来一样,但事实并非如此,因为它有不同的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM