从由 Sympy 作为有限集返回的解决方案集中获取一个值 [英] Get a value from solution set returned as finiteset by Sympy
问题描述
我正在 Python Sympy 库中创建一个脚本并尝试访问由 solveset() 和 linsolve() 函数返回的结果.我的问题是这些函数返回的对象是有限集类型,我想自动选择一些结果以将其重新输入到其他方程中.任何机构都可以帮助我吗?
<预><代码>>>>a1, a2 = 符号('a1, a2')>>>eq2_1 = Eq(-3*a1/10 - 3*a2/20 + 1/12)>>>eq2_2 = Eq(-3*a1/20 - 13*a2/105 + 1/20)>>>列表 = [eq2_1,eq2_2]>>>字符串(列表)[-3*a1/10 - 3*a2/20 + 1/12,-3*a1/20 - 13*a2/105 + 1/20]然后用 linsolve() 方法解决它.
<预><代码>>>>a = linsolve(lista,a1,a2)>>>一种{(71/369, 7/41)}结果是正确的,但我无法将这些结果放入变量中.
O 尝试了 dics、列表、元组、索引命令,但总是返回错误."有限集对象没有属性 'command'"
我在这个链接中找到了 sympy 库方式 http://docs.sympy.org/latest/tutorial/manipulation.html
在函数或结果对象中使用 .args 属性.如果我有一个函数:
>>>>func = Eq(u(x),−x+sin(x))>>>功能u(x) = -x + sin(x)>>>func.args[0]u(x)>>>func.args[1]-x+sin(x)
同样适用于有限集类型的结果.
I`m creating a script in Python Sympy library and trying to access the result returned by solveset() and linsolve() functions. My problem is that the object returned by these functions is of type finiteset and I want to select some results automaticaly to re-enter it in other equations. Any body could help me?
An example: I create a list of equations with two unknown variables:
>>> a1, a2 = symbols('a1, a2')
>>> eq2_1 = Eq(-3*a1/10 - 3*a2/20 + 1/12)
>>> eq2_2 = Eq(-3*a1/20 - 13*a2/105 + 1/20)
>>> lista = [eq2_1,eq2_2]
>>> str(lista)
[-3*a1/10 - 3*a2/20 + 1/12, -3*a1/20 - 13*a2/105 + 1/20]
Then a solve it with the linsolve() method.
>>> a = linsolve(lista,a1,a2)
>>> a
{(71/369, 7/41)}
The result is correct, but I'm unable to get these results in to a variable.
O tried dics, lists, tuples, indexing commands, but always return the error. "Finiteset objects has no attribute 'command'"
I found the sympy library way in this link http://docs.sympy.org/latest/tutorial/manipulation.html
Use .args atribute in the function or result object. If I have a function:
>>>func = Eq(u(x),−x+sin(x))
>>>func
u(x) = -x + sin(x)
>>>func.args[0]
u(x)
>>>func.args[1]
-x+sin(x)
The same applies for a result that is a finite set type.
这篇关于从由 Sympy 作为有限集返回的解决方案集中获取一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!