Skip to content

Commit

Permalink
factor out computation of content, check for is_unit instead of is_one
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Nov 8, 2024
1 parent 099f74f commit a2b12d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/sage/categories/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def gcd(self,other):
from sage.rings.integer_ring import ZZ
try:
return P(ZZ(self).gcd(ZZ(other)))
except TypeError:
except (TypeError, ValueError):
pass

if self == P.zero() and other == P.zero():
Expand Down
55 changes: 29 additions & 26 deletions src/sage/categories/unique_factorization_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,23 @@ def _gcd_univariate_polynomial(self, f, g):
sage: P = PolynomialRing(QQbar, 4, "x")
sage: p = sum(QQbar.zeta(i + 1) * P.gen(i) for i in range(4))
sage: (p^4 - 1).gcd(p^3 + 1) == p + 1
sage: ((p^4 - 1).gcd(p^3 + 1) / (p + 1)).is_unit()
True
"""
def content(X):
"""
Return the content of ``X`` up to a unit.
"""
X_it = iter(X.coefficients())
x = next(X_it)
if x.is_unit():
return None
for c in X_it:
x = x.gcd(c)
if x.is_unit():
return None
return x

if f.degree() < g.degree():
A, B = g, f
else:
Expand All @@ -183,26 +197,19 @@ def _gcd_univariate_polynomial(self, f, g):
if B.is_zero():
return A

A_it = iter(A.coefficients())
a = next(A_it)
for c in A_it:
a = a.gcd(c)
if a.is_one():
break
else:
a = content(A)
if a is not None:
A //= a

B_it = iter(B.coefficients())
b = next(B_it)
for c in B_it:
b = b.gcd(c)
if b.is_one():
break
else:
b = content(B)
if b is not None:
B //= b

d = a.gcd(b)
g = h = 1
one = A.base_ring().one()
if a is not None and b is not None:
d = a.gcd(b)
else:
d = one
g = h = one
delta = A.degree() - B.degree()
_, R = A.pseudo_quo_rem(B)
while R.degree() > 0:
Expand All @@ -214,14 +221,10 @@ def _gcd_univariate_polynomial(self, f, g):
_, R = A.pseudo_quo_rem(B)

if R.is_zero():
B_it = iter(B.coefficients())
b = next(B_it)
for c in B_it:
b = b.gcd(c)
if b.is_one():
return d * B
return d * B // b # TODO: does b divide d?

b = content(B)
if b is None:
return d * B
return d * B // b
return f.parent()(d)

class ElementMethods:
Expand Down

0 comments on commit a2b12d8

Please sign in to comment.