英文:
Get independent variable from SymPy Poly
问题
我可以创建一个sympy Poly对象。
```python
from sympy.abc import x, mu
my_poly = poly(x ** 2 + x + mu, x)
print(my_poly)
它会打印
Poly(x**2 + x + mu, x, domain='ZZ[mu]')
如何从my_poly
中获取我的符号变量x
。在这种情况下,我知道它是x
,但我的其他代码需要适用于通用多项式。
我可以使用list(my_poly.free_symbols - my_poly.free_symbols_in_domain)[0]
来获取它,是否有更简单的方法?
<details>
<summary>英文:</summary>
I can create a sympy Poly object.
```python
from sympy.abc import x, mu
my_poly = poly(x ** 2 + x + mu, x)
print(my_poly)
Which prints
Poly(x**2 + x + mu, x, domain='ZZ[mu]')
How do I get my symbolic variable x
from my_poly
. In this case I know it is x
but my other code needs to work for generic polynomials.
I can get it with list(my_poly.free_symbols - my_poly.free_symbols_in_domain)[0]
, is there a simpler way?
答案1
得分: 2
gens
属性将为您提供生成器元组。如果您始终只有一个生成器,那么p.gens[0]
就足以获取您在实例化时指定的生成器x
。
英文:
The gens
property will give you a tuple of generators. If you always have only one then p.gens[0]
is all you need to get x
, the generator you specified at instantiation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论