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

Commit

Permalink
trac #17671: fix xgcd of polynomial_field_generic
Browse files Browse the repository at this point in the history
As a consequence we can revert some of the modifications from
function_field.py.
  • Loading branch information
videlec committed Jan 28, 2015
1 parent db90fdd commit 673bca9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/sage/categories/principal_ideal_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _test_gcd_vs_xgcd(self, **options):
sage: ZZ._test_gcd_vs_xgcd()
sage: QQ._test_gcd_vs_xgcd()
sage: QQ['x']._test_gcd_vs_xgcd()
sage: QQbar['x']._test_gcd_vs_xgcd()
A slightly more involved example of polynomial ring with a non UFD
base ring::
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/function_field/function_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
It is not the fault of the function field code so this will
be fixed in another ticket::
sage: TestSuite(R).run(skip = ('_test_elements', '_test_gcd_vs_xgcd'))
sage: TestSuite(S).run(skip = ('_test_elements', '_test_gcd_vs_xgcd'))
sage: TestSuite(R).run(skip = '_test_elements')
sage: TestSuite(S).run(skip = '_test_elements')
"""
#*****************************************************************************
# Copyright (C) 2010 William Stein <wstein@gmail.com>
Expand Down
45 changes: 35 additions & 10 deletions src/sage/rings/polynomial/polynomial_element_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,13 @@ def gcd(self, other):
sage: (2*x).gcd(2*x^2)
x
sage: zero = R.zero_element()
sage: zero.gcd(2*x)
x
sage: (2*x).gcd(zero)
x
sage: zero.gcd(zero)
0
"""
from sage.categories.euclidean_domains import EuclideanDomains
g = EuclideanDomains().ElementMethods().gcd(self, other)
Expand Down Expand Up @@ -774,25 +781,43 @@ def xgcd(self, other):
sage: g == u*P(0) + v*x
True
TESTS:
We check that the behavior of xgcd with zero elements is compatible with
gcd (:trac:`17671`)::
sage: R.<x> = QQbar[]
sage: zero = R.zero_element()
sage: zero.xgcd(2*x)
(x, 0, 1/2)
sage: (2*x).xgcd(zero)
(x, 1/2, 0)
sage: zero.xgcd(zero)
(0, 0, 0)
"""
R = self.parent()
zero = R.zero_element()
one = R.one_element()
if other.is_zero():
R = self.parent()
return self, R.one_element(), R.zero_element()
if self.is_zero():
return (zero, zero, zero)
else:
c = self.leading_coefficient()
return (self/c, one/c, zero)
elif self.is_zero():
c = other.leading_coefficient()
return (other/c, zero, one/c)

# Algorithm 3.2.2 of Cohen, GTM 138
R = self.parent()
A = self
B = other
U = R.one_element()
U = one
G = A
V1 = R.zero_element()
V1 = zero
V3 = B
while not V3.is_zero():
Q, R = G.quo_rem(V3)
T = U - V1*Q
U = V1
G = V3
V1 = T
V3 = R
G, U, V1, V3 = V3, V1, U-V1*Q, R
V = (G-A*U)//B
lc = G.leading_coefficient()
return G/lc, U/lc, V/lc
Expand Down

0 comments on commit 673bca9

Please sign in to comment.