求解方程并使用其他计算中获得的值 - SAGE
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(1) 您应该访问 ask.sagemath.org,这是一个面向 Sage 用户、专家和技术人员的类似于 Stack Overflow 的论坛开发商!
(2) 如果您想在某些内容中使用solve()调用的值,那么使用solution_dict标志可能是最简单的:
此选项将解决方案作为列表提供字典而不是方程列表。我们可以像访问任何其他字典一样访问结果:
然后如果我们愿意,我们可以将其分配给其他内容:
并替换:
等等。虽然恕我直言,上面的方法更方便,但您也可以通过 .rhs() 访问相同的结果,而无需使用 Solution_dict:
(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:
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:
and then we can assign that to something else if we like:
and substitute:
et cetera. While IMHO the above is more convenient, you could also access the same results without solution_dict via .rhs():
如果您有未知数量的变量,您可以使用 **kwargs 将使用solve计算的数据传递到下一个表达式。这是示例:
B_set 是一个变量列表,它在运行时填充,所以我不知道
编写代码时变量的名称及其数量
例如,这给了我:
求解方程组的结果
我无法使用这个答案进行进一步的计算,所以让我们将其转换为可用的形式
这给了我们:
结果转换为带有字符串键的字典
然后我们将其传递为 **kwargs
并将其转换为:
没有解决方案中的值的近似
对此:
使用解决方案中的值进行近似
注意:如果您使用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
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
Which gives us:
result converted to dictionary with string keys
Then we just pass this as **kwargs
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.