从SymPy Poly获取独立变量

huangapple go评论48阅读模式
英文:

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=&#39;ZZ[mu]&#39;)

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.

huangapple
  • 本文由 发表于 2023年3月31日 03:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定