Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit ced566c

Browse files
committed
Basic infrastructure
1 parent 7b1b3ec commit ced566c

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

+18-3
Original file line numberDiff line numberDiff line change
@@ -3918,6 +3918,19 @@ cdef class Polynomial(CommutativeAlgebraElement):
39183918
if self.degree() == 0:
39193919
return Factorization([], unit=self[0])
39203920

3921+
# Use multivariate implementations for polynomials over polynomial rings
3922+
variables = self._parent.variable_names_recursive()
3923+
if len(variables) > 1:
3924+
base = self._parent._mpoly_base_ring()
3925+
ring = PolynomialRing(base, variables)
3926+
if ring._has_singular:
3927+
try:
3928+
d = self._mpoly_dict_recursive()
3929+
F = ring(d).factor(**kwargs)
3930+
return Factorization([(self._parent(f),m) for (f,m) in F], unit = F.unit())
3931+
except NotImplementedError:
3932+
pass
3933+
39213934
R = self.parent().base_ring()
39223935
if hasattr(R, '_factor_univariate_polynomial'):
39233936
return R._factor_univariate_polynomial(self, **kwargs)
@@ -6291,7 +6304,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
62916304

62926305
return self.parent()(v)
62936306

6294-
def roots(self, ring=None, multiplicities=True, algorithm=None):
6307+
def roots(self, ring=None, multiplicities=True, algorithm=None, **kwds):
62956308
"""
62966309
Return the roots of this polynomial (by default, in the base ring
62976310
of this polynomial).
@@ -6873,7 +6886,10 @@ cdef class Polynomial(CommutativeAlgebraElement):
68736886
"""
68746887
K = self.parent().base_ring()
68756888
if hasattr(K, '_roots_univariate_polynomial'):
6876-
return K._roots_univariate_polynomial(self, ring=ring, multiplicities=multiplicities, algorithm=algorithm)
6889+
return K._roots_univariate_polynomial(self, ring=ring, multiplicities=multiplicities, algorithm=algorithm, **kwds)
6890+
6891+
if kwds:
6892+
raise TypeError("roots() got unexpected keyword argument(s): {}".format(kwds.keys()))
68776893

68786894
L = K if ring is None else ring
68796895

@@ -8621,7 +8637,6 @@ cdef class Polynomial(CommutativeAlgebraElement):
86218637
raise ValueError("not a %s power"%m.ordinal_str())
86228638
raise ValueError("not a %s power"%m.ordinal_str())
86238639

8624-
86258640
# ----------------- inner functions -------------
86268641
# Cython can't handle function definitions inside other function
86278642

src/sage/rings/polynomial/polynomial_ring.py

+36
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,42 @@ def weyl_algebra(self):
15091509
from sage.algebras.weyl_algebra import DifferentialWeylAlgebra
15101510
return DifferentialWeylAlgebra(self)
15111511

1512+
def _roots_univariate_polynomial(self, p, ring=None, multiplicities=True, algorithm=None, degree_bound=None):
1513+
"""
1514+
Return the list of roots of ``p``.
1515+
1516+
INPUT:
1517+
1518+
- ``p`` -- the polynomial whose roots are computed
1519+
- ``ring`` -- the ring to find roots (default is the base ring of ``p``)
1520+
- ``multiplicities`` -- bool (default: True): if ``True``, return a list of pairs ``(root, multiplicity)``; if ``False`` return a list of roots
1521+
- ``algorithm`` -- ignored (TODO: remove)
1522+
- ``degree_bound``-- if not ``None``, return only roots of degree at most ``degree_bound``
1523+
1524+
EXAMPLES::
1525+
1526+
sage: R.<x> = QQ[]
1527+
sage: S.<y> = R[]
1528+
sage: p = y^3 + (-x^2 - 3)*y^2 + (2*x^3 - x^2 + 3)*y - x^4 + 2*x^2 - 1
1529+
sage: p.roots()
1530+
[(x^2 - 2*x + 1, 1), (x + 1, 2)]
1531+
sage: p.roots(multiplicities=False)
1532+
[x^2 - 2*x + 1, x + 1]
1533+
sage: p.roots(degree_bound=1)
1534+
[(x + 1, 2)]
1535+
"""
1536+
if ring is not None and ring is not self:
1537+
p = p.change_ring(ring)
1538+
return p.roots(multiplicities, algorithm, degree_bound)
1539+
roots = p._roots_from_factorization(p.factor(), multiplicities)
1540+
if degree_bound is not None:
1541+
if multiplicities:
1542+
roots = [(r,m) for (r,m) in roots if r.degree() <= degree_bound]
1543+
else:
1544+
roots = [r for r in roots if r.degree() <= degree_bound]
1545+
return roots
1546+
1547+
15121548
class PolynomialRing_integral_domain(PolynomialRing_commutative, ring.IntegralDomain):
15131549
def __init__(self, base_ring, name="x", sparse=False, implementation=None,
15141550
element_class=None, category=None):

0 commit comments

Comments
 (0)