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

Commit d03a75e

Browse files
committed
#19171: New method divides
1 parent d091c43 commit d03a75e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

+42
Original file line numberDiff line numberDiff line change
@@ -8496,6 +8496,48 @@ cdef class Polynomial(CommutativeAlgebraElement):
84968496
raise ValueError("not a %s power"%m.ordinal_str())
84978497

84988498

8499+
def divides(self, p):
8500+
r"""
8501+
Return `True` if this polynomial divides `p`.
8502+
8503+
EXAMPLES::
8504+
8505+
sage: R.<x> = ZZ[]
8506+
sage: (2*x + 1).divides(4*x**2 - 1)
8507+
True
8508+
sage: (2*x + 1).divides(4*x**2 + 1)
8509+
False
8510+
sage: (2*x + 1).divides(R(0))
8511+
True
8512+
sage: R(0).divides(2*x + 1)
8513+
False
8514+
sage: R(0).divides(R(0))
8515+
True
8516+
sage: R.<x> = PolynomialRing(ZZ, implementation="NTL")
8517+
sage: (2*x + 1).divides(4*x**2 + 1)
8518+
False
8519+
"""
8520+
parent = self.parent()
8521+
if p.parent() is parent:
8522+
if p.is_zero(): return True # everything divides 0
8523+
if self.is_zero(): return False # 0 only divides 0
8524+
try:
8525+
if self.is_unit(): return True # units divide everything
8526+
except NotImplementedError:
8527+
pass
8528+
if self.is_one(): return True # if is_unit is not implemented
8529+
8530+
try:
8531+
return (p % self) == 0 # if quo_rem is defined
8532+
except ArithmeticError:
8533+
return False # if division is not exact
8534+
8535+
else:
8536+
cm = sage.structure.element.get_coercion_model()
8537+
a, b = cm.canonical_coercion(self, p)
8538+
return a.divides(b)
8539+
8540+
84998541
# ----------------- inner functions -------------
85008542
# Cython can't handle function definitions inside other function
85018543

0 commit comments

Comments
 (0)