求解方程并使用其他计算中获得的值 - SAGE

发布于 2024-11-14 04:04:25 字数 392 浏览 7 评论 0原文

SAGE 中的函数solve() 返回我求解方程的变量的符号值。例如:

sage: s=solve(eqn,y)
sage: s
[y == -1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) + 8*x^4 + 11*x^3 + 12*x^2)/(15*x + 1), y == 1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) - 8*x^4 - 11*x^3 - 12*x^2)/(15*x + 1)]

我的问题是我需要在其他计算中使用 y 获得的值,但我无法将这些值分配给任何其他变量。有人可以帮我解决这个问题吗?

the function solve() in SAGE returns symbolic values for the variables i solve the equations for. for e.g:

sage: s=solve(eqn,y)
sage: s
[y == -1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) + 8*x^4 + 11*x^3 + 12*x^2)/(15*x + 1), y == 1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) - 8*x^4 - 11*x^3 - 12*x^2)/(15*x + 1)]

My problem is that i need to use the values obtained for y in other calculations, but I cannot assign these values to any other variable. Could someone please help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-11-21 04:04:25

(1) 您应该访问 ask.sagemath.org,这是一个面向 Sage 用户、专家和技术人员的类似于 Stack Overflow 的论坛开发商!

(2) 如果您想在某些内容中使用solve()调用的值,那么使用solution_dict标志可能是最简单的:

sage: x,y = var("x, y")
sage: eqn = x**4+5*x*y+3*x-y==17
sage: solve(eqn,y)
[y == -(x^4 + 3*x - 17)/(5*x - 1)]
sage: solve(eqn,y,solution_dict=True)
[{y: -(x^4 + 3*x - 17)/(5*x - 1)}]

此选项将解决方案作为列表提供字典而不是方程列表。我们可以像访问任何其他字典一样访问结果:

sage: sols = solve(eqn,y,solution_dict=True)
sage: sols[0][y]
-(x^4 + 3*x - 17)/(5*x - 1)

然后如果我们愿意,我们可以将其分配给其他内容:

sage: z = sols[0][y]
sage: z
-(x^4 + 3*x - 17)/(5*x - 1)

并替换:

sage: eqn2 = y*(5*x-1)
sage: eqn2.subs(y=z)
-x^4 - 3*x + 17

等等。虽然恕我直言,上面的方法更方便,但您也可以通过 .rhs() 访问相同的结果,而无需使用 Solution_dict:

sage: solve(eqn,y)[0].rhs()
-(x^4 + 3*x - 17)/(5*x - 1)

(1) You should visit ask.sagemath.org, the Stack Overflow-like forum for Sage users, experts, and developers! </plug>

(2) If you want to use the values of a solve() call in something, then it's probably easiest to use the solution_dict flag:

sage: x,y = var("x, y")
sage: eqn = x**4+5*x*y+3*x-y==17
sage: solve(eqn,y)
[y == -(x^4 + 3*x - 17)/(5*x - 1)]
sage: solve(eqn,y,solution_dict=True)
[{y: -(x^4 + 3*x - 17)/(5*x - 1)}]

This option gives the solutions as a list of dictionaries instead of a list of equations. We can access the results like we would any other dictionary:

sage: sols = solve(eqn,y,solution_dict=True)
sage: sols[0][y]
-(x^4 + 3*x - 17)/(5*x - 1)

and then we can assign that to something else if we like:

sage: z = sols[0][y]
sage: z
-(x^4 + 3*x - 17)/(5*x - 1)

and substitute:

sage: eqn2 = y*(5*x-1)
sage: eqn2.subs(y=z)
-x^4 - 3*x + 17

et cetera. While IMHO the above is more convenient, you could also access the same results without solution_dict via .rhs():

sage: solve(eqn,y)[0].rhs()
-(x^4 + 3*x - 17)/(5*x - 1)
踏雪无痕 2024-11-21 04:04:25

如果您有未知数量的变量,您可以使用 **kwargs 将使用solve计算的数据传递到下一个表达式。这是示例:

B_set 是一个变量列表,它在运行时填充,所以我不知道
编写代码时变量的名称及其数量

solution = solve(system_of_equations, B_set)[0]
pretty_print(solution)

例如,这给了我:
求解方程组的结果

我无法使用这个答案进行进一步的计算,所以让我们将其转换为可用的形式

solution = {str(elem.lhs()): elem.rhs() for elem in solution}

这给了我们:
结果转换为带有字符串键的字典

然后我们将其传递为 **kwargs

approximation = approximation_function(**solution)
pretty_print(approximation)

并将其转换为:
没有解决方案中的值的近似

对此:
使用解决方案中的值进行近似

注意:如果您使用solve() 的字典输出,您仍然需要将键转换为字符串。

If you have unknown number of variables you can use **kwargs to pass data calculated with solve to next expressions. Here's example:

B_set is an list of variables and its filled in runtime so i dont know
names of variables and their quantity at the time of writing the code

solution = solve(system_of_equations, B_set)[0]
pretty_print(solution)

For example this gives me:
result of solving system of equations

I cant use this answer for further calculations so lets convert it to usable form

solution = {str(elem.lhs()): elem.rhs() for elem in solution}

Which gives us:
result converted to dictionary with string keys

Then we just pass this as **kwargs

approximation = approximation_function(**solution)
pretty_print(approximation)

And that converts this:
approximation without values from solution

Into this:
approximation with values from solution

Note: if you use dictionary output from solve() you still need to convert keys to string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文