Skip to content

Commit

Permalink
Add more characteristic_poly() unit tests for matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent 8147946 commit 4879587
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 92 deletions.
29 changes: 28 additions & 1 deletion scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import sage
import numpy as np
from sage.all import GF, PolynomialRing, log
from sage.all import GF, PolynomialRing, log, matrix

FIELD = None
SPARSE_SIZE = 20
Expand Down Expand Up @@ -76,6 +76,18 @@ def arange(low, high, sparse=False):
return X


def randint_matrix(low, high, shape):
if high <= np.iinfo(np.int64).max:
X = np.random.randint(low, high, shape, dtype=np.int64)
else:
X = np.empty(shape, dtype=object)
iterator = np.nditer(X, flags=["multi_index", "refs_ok"])
for i in iterator:
X[iterator.multi_index] = random.randint(low, high - 1)

return X


def io_1d(low, high, sparse=False):
X = arange(low, high, sparse=sparse)

Expand Down Expand Up @@ -253,6 +265,21 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "characteristic_poly_element.pkl")

set_seed(seed + 13)
shapes = [(2,2), (3,3), (4,4), (5,5), (6,6)]
X = []
Z = []
for i in range(len(shapes)):
x = randint_matrix(0, order, shapes[i])
X.append(x)
x = matrix(FIELD, [[F(e) for e in row] for row in x])
p = x.charpoly()
z = np.array([I(e) for e in p.list()[::-1]], dtype=dtype).tolist()
z = z if z != [] else [0]
Z.append(z)
d = {"X": X, "Z": Z}
save_pickle(d, folder, "characteristic_poly_matrix.pkl")

###############################################################################
# Polynomial arithmetic
###############################################################################
Expand Down
12 changes: 12 additions & 0 deletions tests/fields/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,15 @@ def field_characteristic_poly_element(field_folder):
d["X"] = GF(d["X"])
d["Z"] = [galois.Poly(p, field=GF.prime_subfield) for p in d["Z"]]
return d


@pytest.fixture(scope="session")
def field_characteristic_poly_matrix(field_folder):
GF, folder = field_folder
with open(os.path.join(folder, "characteristic_poly_matrix.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
d["GF"] = GF
d["X"] = [GF(x) for x in d["X"]]
d["Z"] = [galois.Poly(p, field=GF) for p in d["Z"]]
return d
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
20 changes: 19 additions & 1 deletion tests/fields/test_arithmetic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ def test_characteristic_poly_element(field_characteristic_poly_element):
zi = x[i].characteristic_poly()
assert zi == Z[i]

# Only 0-D or 2-D arrays are allowed
# Only 0-D arrays are allowed
with pytest.raises(ValueError):
A = GF.Random(5)
A.characteristic_poly()


def test_characteristic_poly_matrix(field_characteristic_poly_matrix):
GF, X, Z = field_characteristic_poly_matrix["GF"], field_characteristic_poly_matrix["X"], field_characteristic_poly_matrix["Z"]

for i in range(len(X)):
dtype = random.choice(GF.dtypes)
xi = X[i].astype(dtype)
zi = xi.characteristic_poly()
assert zi == Z[i]

# Only 2-D square arrays are allowed
with pytest.raises(ValueError):
A = GF.Random(5)
A.characteristic_poly()
with pytest.raises(ValueError):
A = GF.Random((2,3))
A.characteristic_poly()
90 changes: 0 additions & 90 deletions tests/polys/test_characteristic_poly_matrix.py

This file was deleted.

0 comments on commit 4879587

Please sign in to comment.