本文整理汇总了Python中sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing.roots方法的典型用法代码示例。如果您正苦于以下问题:Python PolynomialRing.roots方法的具体用法?Python PolynomialRing.roots怎么用?Python PolynomialRing.roots使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing
的用法示例。
在下文中一共展示了PolynomialRing.roots方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _roots_univariate_polynomial
# 需要导入模块: from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing [as 别名]
# 或者: from sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing import roots [as 别名]
def _roots_univariate_polynomial(self, p, ring=None, multiplicities=None, algorithm=None):
r"""
Return a list of pairs ``(root,multiplicity)`` of roots of the polynomial ``p``.
If the argument ``multiplicities`` is set to ``False`` then return the
list of roots.
.. SEEALSO::
:meth:`_factor_univariate_polynomial`
EXAMPLES::
sage: R.<x> = PolynomialRing(GF(5),'x')
sage: K = GF(5).algebraic_closure('t')
sage: sorted((x^6 - 1).roots(K,multiplicities=False))
[1, 4, 2*t2 + 1, 2*t2 + 2, 3*t2 + 3, 3*t2 + 4]
sage: ((K.gen(2)*x - K.gen(3))**2).roots(K)
[(3*t6^5 + 2*t6^4 + 2*t6^2 + 3, 2)]
sage: for _ in xrange(10):
....: p = R.random_element(degree=randint(2,8))
....: for r in p.roots(K, multiplicities=False):
....: assert p(r).is_zero()
"""
from sage.rings.arith import lcm
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
# first build a polynomial over some finite field
coeffs = [v.as_finite_field_element(minimal=True) for v in p.list()]
l = lcm([c[0].degree() for c in coeffs])
F, phi = self.subfield(l)
P = p.parent().change_ring(F)
new_coeffs = [self.inclusion(c[0].degree(), l)(c[1]) for c in coeffs]
roots = [] # a list of pair (root,multiplicity)
for g, m in P(new_coeffs).factor():
if g.degree() == 1:
r = phi(-g.constant_coefficient())
roots.append((r,m))
else:
ll = l * g.degree()
psi = self.inclusion(l, ll)
FF, pphi = self.subfield(ll)
gg = PolynomialRing(FF, 'x')(map(psi, g))
for r, _ in gg.roots(): # note: we know that multiplicity is 1
roots.append((pphi(r), m))
if multiplicities:
return roots
else:
return [r[0] for r in roots]