Skip to content

Commit

Permalink
Rename poly_gcd() to poly_egcd()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Jul 1, 2021
1 parent cfe8190 commit c21c1c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/api/polys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Polynomial functions
.. autosummary::
:toctree:

poly_gcd
poly_egcd
poly_pow
poly_factors

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/intro-to-extension-fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Now the entire multiplication table can be shown for completeness.
Division, as in :math:`\mathrm{GF}(p)`, is a little more difficult. Fortunately the Extended Euclidean Algorithm, which
was used in prime fields on integers, can be used for extension fields on polynomials. Given two polynomials :math:`a`
and :math:`b`, the Extended Euclidean Algorithm finds the polynomials :math:`x` and :math:`y` such that
:math:`xa + yb = \textrm{gcd}(a, b)`. This algorithm is implemented in :func:`galois.poly_gcd`.
:math:`xa + yb = \textrm{gcd}(a, b)`. This algorithm is implemented in :func:`galois.poly_egcd`.

If :math:`a` is a field element of :math:`\mathrm{GF}(3^2)` and :math:`b = p(x)`, the field's irreducible polynomial, then :math:`x = a^{-1}` in :math:`\mathrm{GF}(3^2)`.
Note, the GCD will always be :math:`1` because :math:`p(x)` is irreducible.
Expand All @@ -186,7 +186,7 @@ Note, the GCD will always be :math:`1` because :math:`p(x)` is irreducible.
p = GF9.irreducible_poly; p
a = galois.Poly([1, 2], field=GF3); a
gcd, x, y = galois.poly_gcd(a, p); gcd, x, y
gcd, x, y = galois.poly_egcd(a, p); gcd, x, y
The claim is that :math:`(x + 2)^{-1} = x` in :math:`\mathrm{GF}(3^2)` or, equivalently, :math:`(x + 2)(x)\ \equiv 1\ \textrm{mod}\ p(x)`. This
can be easily verified with :obj:`galois`.
Expand Down
12 changes: 6 additions & 6 deletions galois/field/poly_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .poly import Poly

__all__ = [
"poly_gcd", "poly_pow", "poly_factors",
"poly_egcd", "poly_pow", "poly_factors",
"irreducible_poly", "irreducible_polys", "primitive_poly", "primitive_polys",
"matlab_primitive_poly", "conway_poly", "minimal_poly",
"is_monic", "is_irreducible", "is_primitive",
Expand All @@ -22,7 +22,7 @@


@set_module("galois")
def poly_gcd(a, b):
def poly_egcd(a, b):
"""
Finds the greatest common divisor of two polynomials :math:`a(x)` and :math:`b(x)`
over :math:`\\mathrm{GF}(q)`.
Expand Down Expand Up @@ -55,7 +55,7 @@ def poly_gcd(a, b):
# a(x) and b(x) only share the root 2 in common
b = galois.Poly.Roots([1,2], field=GF); b
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
# The GCD has only 2 as a root with multiplicity 1
gcd.roots(multiplicity=True)
Expand Down Expand Up @@ -228,11 +228,11 @@ def square_free_factorization(c, r):
i = 1
a = c.copy()
b = c.derivative()
d = poly_gcd(a, b)[0]
d = poly_egcd(a, b)[0]
w = a / d

while w != one:
y = poly_gcd(w, d)[0]
y = poly_egcd(w, d)[0]
z = w / y
if z != one and i % p != 0:
L *= z**(i * p**r)
Expand Down Expand Up @@ -803,7 +803,7 @@ def is_irreducible(poly):
for ni in sorted([m // pi for pi in primes]):
# The GCD of f(x) and (x^(p^(m/pi)) - x) must be 1 for f(x) to be irreducible, where pi are the prime factors of m
hi = poly_pow(h0, p**(ni - n0), poly)
g = poly_gcd(poly, hi - x)[0]
g = poly_egcd(poly, hi - x)[0]
if g != one:
return False
h0, n0 = hi, ni
Expand Down
26 changes: 13 additions & 13 deletions tests/polys/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
import galois


def test_poly_gcd():
def test_poly_egcd():
a = galois.Poly.Random(5)
b = galois.Poly.Zero()
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert gcd == a
assert a*x + b*y == gcd

a = galois.Poly.Zero()
b = galois.Poly.Random(5)
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert gcd == b
assert a*x + b*y == gcd

# Example 2.223 from https://cacr.uwaterloo.ca/hac/about/chap2.pdf
a = galois.Poly.Degrees([10, 9, 8, 6, 5, 4, 0])
b = galois.Poly.Degrees([9, 6, 5, 3, 2, 0])
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert gcd == galois.Poly.Degrees([3, 1, 0])
assert x == galois.Poly.Degrees([4])
assert y == galois.Poly.Degrees([5, 4, 3, 2, 1, 0])
Expand All @@ -34,7 +34,7 @@ def test_poly_gcd():
GF = galois.GF(3)
a = galois.Poly.Degrees([11, 9, 8, 6, 5, 3, 2, 0], [1, 2, 2, 1, 1, 2, 2, 1], field=GF)
b = a.derivative()
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert gcd == galois.Poly.Degrees([9, 6, 3, 0], [1, 2, 1, 2], field=GF)
assert x == galois.Poly([2], field=GF)
assert y == galois.Poly([2, 0], field=GF)
Expand All @@ -43,33 +43,33 @@ def test_poly_gcd():
GF = galois.GF(7)
a = galois.Poly.Roots([2,2,2,3,5], field=GF)
b = galois.Poly.Roots([1,2,6], field=GF)
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert a*x + b*y == gcd

GF = galois.GF(2**5, irreducible_poly=galois.Poly([1, 0, 1, 0, 0, 1], order="asc"))
a = galois.Poly([1, 2, 4, 1], field=GF)
b = galois.Poly([9, 3], field=GF)
gcd, x, y = galois.poly_gcd(a, b)
gcd, x, y = galois.poly_egcd(a, b)
assert a*x + b*y == gcd

def test_poly_gcd_unit():
def test_poly_egcd_unit():
GF = galois.GF(7)
a = galois.conway_poly(7, 10)
b = galois.conway_poly(7, 5)
gcd = galois.poly_gcd(a, b)[0]
gcd = galois.poly_egcd(a, b)[0]
assert gcd == galois.Poly([1], field=GF) # Note, not 3


def test_poly_gcd_exceptions():
def test_poly_egcd_exceptions():
a = galois.Poly.Degrees([10, 9, 8, 6, 5, 4, 0])
b = galois.Poly.Degrees([9, 6, 5, 3, 2, 0])

with pytest.raises(TypeError):
galois.poly_gcd(a.coeffs, b)
galois.poly_egcd(a.coeffs, b)
with pytest.raises(TypeError):
galois.poly_gcd(a, b.coeffs)
galois.poly_egcd(a, b.coeffs)
with pytest.raises(ValueError):
galois.poly_gcd(a, galois.Poly(b.coeffs, field=galois.GF(3)))
galois.poly_egcd(a, galois.Poly(b.coeffs, field=galois.GF(3)))


def test_poly_pow():
Expand Down

0 comments on commit c21c1c7

Please sign in to comment.