Skip to content

Commit

Permalink
Add more is_monic() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent e61ba58 commit 0748408
Show file tree
Hide file tree
Showing 20 changed files with 33 additions and 16 deletions.
5 changes: 4 additions & 1 deletion galois/_fields/_poly_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,9 @@ def equal_degree_factorization(poly: Poly, degree: int) -> List[Poly]:
@set_module("galois")
def is_monic(poly: Poly) -> bool:
r"""
Determines whether the polynomial is monic, i.e. having leading coefficient equal to 1.
Determines whether the polynomial is monic.
A monic polynomial has a highest-degree coefficient of 1.
Parameters
----------
Expand All @@ -580,6 +582,7 @@ def is_monic(poly: Poly) -> bool:
"""
if not isinstance(poly, Poly):
raise TypeError(f"Argument `poly` must be a galois.Poly, not {type(poly)}.")

return poly.nonzero_coeffs[0] == 1


Expand Down
12 changes: 12 additions & 0 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@ def make_luts(field, sub_folder, seed, sparse=False):
# Special polynomials
###############################################################################

set_seed(seed + 501)
X = [random_coeffs(0, order, 1, 6) for _ in range(20)]
Z = [False,]*len(X)
for i in range(len(X)):
if random.choice(["one", "other"]) == "one":
X[i][0] = 1
x = list_to_poly(X[i])
z = x.is_monic()
Z[i] = bool(z)
d = {"X": X, "Z": Z}
save_pickle(d, folder, "is_monic.pkl")

set_seed(seed + 502)
IS = []
IS_NOT = []
Expand Down
9 changes: 9 additions & 0 deletions tests/polys/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ def poly_modular_power(field_folder):
# Fixtures for special polynomials
###############################################################################

@pytest.fixture(scope="session")
def poly_is_monic(field_folder):
GF, d = read_pickle(field_folder, "is_monic.pkl")
d["GF"] = GF
d["X"] = [galois.Poly(p, field=GF) for p in d["X"]]
d["Z"] = d["Z"]
return d


@pytest.fixture(scope="session")
def poly_is_irreducible(field_folder):
GF, d = read_pickle(field_folder, "is_irreducible.pkl")
Expand Down
Binary file added tests/polys/data/GF(109987^4)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2147483647)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^100)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^2)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^3)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^32)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(2^8, 283, 19)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(31)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(3191)/is_monic.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/polys/data/GF(5)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3)/is_monic.pkl
Binary file not shown.
Binary file added tests/polys/data/GF(7^3, 643, 244)/is_monic.pkl
Binary file not shown.
23 changes: 8 additions & 15 deletions tests/polys/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@


def test_is_monic_exceptions():
GF = galois.GF(7)
p = galois.Poly([1,0,4,5], field=GF)

with pytest.raises(TypeError):
galois.is_monic(p.coeffs)

galois.is_monic([1, 0, 1, 1])

def test_is_monic():
GF = galois.GF(7)
p = galois.Poly([1,0,4,5], field=GF)
assert galois.is_monic(p)

GF = galois.GF(7)
p = galois.Poly([3,0,4,5], field=GF)
assert not galois.is_monic(p)
def test_is_monic(poly_is_monic):
X, Z = poly_is_monic["X"], poly_is_monic["Z"]
for i in range(len(X)):
assert galois.is_monic(X[i]) == Z[i]


def test_is_irreducible_exceptions():
Expand All @@ -30,7 +23,7 @@ def test_is_irreducible_exceptions():


def test_is_irreducible(poly_is_irreducible):
GF, IS, IS_NOT = poly_is_irreducible["GF"], poly_is_irreducible["IS"], poly_is_irreducible["IS_NOT"]
IS, IS_NOT = poly_is_irreducible["IS"], poly_is_irreducible["IS_NOT"]
for i in range(len(IS)):
p = IS[i]
assert galois.is_irreducible(p)
Expand All @@ -44,7 +37,7 @@ def test_is_primitive_exceptions():


def test_is_primitive(poly_is_primitive):
GF, IS, IS_NOT = poly_is_primitive["GF"], poly_is_primitive["IS"], poly_is_primitive["IS_NOT"]
IS, IS_NOT = poly_is_primitive["IS"], poly_is_primitive["IS_NOT"]
for i in range(len(IS)):
p = IS[i]
assert galois.is_primitive(p)
Expand All @@ -58,6 +51,6 @@ def test_is_square_free_exceptions():


def test_is_square_free(poly_is_square_free):
GF, X, Z = poly_is_square_free["GF"], poly_is_square_free["X"], poly_is_square_free["Z"]
X, Z = poly_is_square_free["X"], poly_is_square_free["Z"]
for i in range(len(X)):
assert galois.is_square_free(X[i]) == Z[i]

0 comments on commit 0748408

Please sign in to comment.