Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions src/sage/rings/polynomial/laurent_polynomial.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,93 @@ cdef class LaurentPolynomial_univariate(LaurentPolynomial):
sage: q = (y^2-x^2) * z**-2 + z + x-y
sage: p.divides(q), p.divides(p*q) # needs sage.libs.singular
(False, True)
"""
p = self.polynomial_construction()[0]
q = other.polynomial_construction()[0]
return p.divides(q)

Divisibility works over reduced rings (rings without nilpotent elements)::

sage: R.<y> = LaurentPolynomialRing(Zmod(6))
sage: (y + 2).divides(y^2 + 4*y + 4)
True
sage: (y + 2).divides(y^2 + 3)
False

TESTS:

Check that :issue:`40372` is fixed::

sage: R.<y> = LaurentPolynomialRing(Zmod(4))
sage: a = 2+y
sage: a.divides(a)
Traceback (most recent call last):
...
NotImplementedError: divisibility test not implemented for Laurent polynomials over rings with nilpotent elements
"""
# Check if the base ring has nilpotent elements
# For such rings, the degree bound is not sufficient
if self.base_ring().is_integral_domain() is True:
p = self.polynomial_construction()[0]
q = other.polynomial_construction()[0]
return p.divides(q)
try:
if not self.base_ring().nilradical().is_zero():
raise NotImplementedError("divisibility test not implemented for Laurent polynomials over rings with nilpotent elements")
except (AttributeError, ArithmeticError):
# If we can't determine, be conservative and reject
raise NotImplementedError("divisibility test not implemented for Laurent polynomials over rings with nilpotent elements")

# Handle zero cases
if other.is_zero():
return True # everything divides 0
if self.is_zero():
return False # 0 only divides 0

# Handle unit case
try:
if self.is_unit():
return True # units divide everything
except (AttributeError, NotImplementedError):
pass

p, n_p = self.polynomial_construction()
q, n_q = other.polynomial_construction()

# Special case: both are constant (monomials with degree 0 polynomial part)
if p.degree() == 0 and q.degree() == 0:
# For constants, divisibility depends only on the coefficients
return p[0].divides(q[0])

# When checking divisibility of Laurent polynomials, we need to account
# for the fact that polynomial_construction normalizes by extracting
# powers of the variable. If self = x^{n_p} * p and other = x^{n_q} * q,
# then self | other iff there exists a Laurent polynomial b = x^{n_b} * r
# such that self * b = other.
#
# This means x^{n_p + n_b} * p * r = x^{n_q} * q.
# The product p * r might have a factor x^m (where m >= 0), so we get:
# x^{n_p + n_b + m} * (p*r / x^m) = x^{n_q} * q
#
# So we need p | x^m * q for some m >= 0 such that the quotient is a
# polynomial (no negative powers).

# Try shifting q by increasing powers of x until either:
# 1. We find that p divides x^m * q, or
# 2. The degree of x^m * q exceeds what's reasonable (degree bound)
x = p.parent().gen()
deg_bound = p.degree() + 1

for m in range(deg_bound):
q_shifted = q * x**m
try:
if p.divides(q_shifted):
return True
except NotImplementedError:
# For non-integral domains, p.divides may not be implemented
# Fall back to quo_rem and verify
try:
quotient, remainder = q_shifted.quo_rem(p)
if remainder.is_zero() and quotient * p == q_shifted:
return True
except (ArithmeticError, NotImplementedError, ValueError):
# quo_rem may fail for non-invertible leading coefficients
pass

return False
28 changes: 28 additions & 0 deletions src/sage/rings/polynomial/laurent_polynomial_mpair.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,35 @@ cdef class LaurentPolynomial_mpair(LaurentPolynomial):
False
sage: f1.divides(3)
False

Zero is divisible by everything, and only zero is divisible by zero::

sage: R.<x,y> = LaurentPolynomialRing(ZZ)
sage: x.divides(R(0))
True
sage: R(0).divides(x)
False
sage: R(0).divides(R(0))
True

Monomials divide when the exponents allow::

sage: (x*y^-1).divides(x^2*y^-2)
True
sage: (x^2).divides(x)
True

TESTS:

Multivariate Laurent polynomial rings require an integral domain base ring::

sage: R.<x,y> = LaurentPolynomialRing(Zmod(4))
Traceback (most recent call last):
...
ValueError: base ring must be an integral domain
"""
# Note: Multivariate Laurent polynomial rings already reject non-integral
# domain base rings at construction time, but we keep this check for safety.
p = self.monomial_reduction()[0]
q = other.monomial_reduction()[0]
return p.divides(q)
Loading