Skip to content

Commit

Permalink
Add unit tests for null_space()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 12, 2022
1 parent 71dd31c commit cf97f21
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 1 deletion.
49 changes: 48 additions & 1 deletion scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def make_luts(field, sub_folder, seed, sparse=False):
z = np.array([[I(e) for e in row] for row in z], dtype)
Z.append(z)

# Reduce the row space by 1 by copying the 0th row to the jth row
# Reduce the left null space by 1 by copying the 0th row to the jth row
for j in range(1, shapes[i][0]):
x = copy(x)
x[j,:] = F(random.randint(0, order-1)) * x[0,:]
Expand Down Expand Up @@ -616,6 +616,53 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "left_null_space.pkl")

set_seed(seed + 211)
shapes = [(2,2), (2,3), (2,4), (3,2), (4,2), (3,3)]
X = []
Z = []
for i in range(len(shapes)):
deg = shapes[i][1] # The degree of the vector space

# Random matrix
x = randint_matrix(0, order, shapes[i])
X.append(x)
x = matrix(FIELD, [[F(e) for e in row] for row in x])
z = x.right_kernel()
if z.dimension() == 0:
z = randint_matrix(0, 1, (0, deg))
else:
z = z.basis_matrix()
z = np.array([[I(e) for e in row] for row in z], dtype)
Z.append(z)

# Reduce the null space by 1 by copying the 0th column to the jth column
for j in range(1, shapes[i][1]):
x = copy(x)
x[:,j] = F(random.randint(0, order-1)) * x[:,0]

z = x.right_kernel()
if z.dimension() == 0:
z = randint_matrix(0, 1, (0, deg))
else:
z = z.basis_matrix()
z = np.array([[I(e) for e in row] for row in z], dtype)
X.append(np.array([[I(e) for e in row] for row in x], dtype))
Z.append(z)

# Zero matrix
x = copy(x)
x[:] = F(0)
z = x.right_kernel()
if z.dimension() == 0:
z = randint_matrix(0, 1, (0, deg))
else:
z = z.basis_matrix()
z = np.array([[I(e) for e in row] for row in z], dtype)
X.append(np.array([[I(e) for e in row] for row in x], dtype))
Z.append(z)

d = {"X": X, "Z": Z}
save_pickle(d, folder, "null_space.pkl")

###############################################################################
# Polynomial arithmetic
Expand Down
9 changes: 9 additions & 0 deletions tests/fields/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ def field_left_null_space(field_folder):
return d


@pytest.fixture(scope="session")
def field_null_space(field_folder):
GF, d = read_pickle(field_folder, "null_space.pkl")
d["GF"] = GF
d["X"] = [GF(x) for x in d["X"]]
d["Z"] = [GF(z) for z in d["Z"]]
return d


###############################################################################
# Fixtures for arithmetic methods over finite fields
###############################################################################
Expand Down
Binary file added tests/fields/data/GF(109987^4)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2147483647)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^100)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^2)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^3)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^32)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8, 283, 19)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(31)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(3191)/null_space.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(5)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7)/null_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7^3)/null_space.pkl
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/fields/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,24 @@ def test_left_null_space(field_left_null_space):
z = x.left_null_space()
assert np.array_equal(z, Z[i])
assert type(z) is GF


def test_null_space_exceptions():
GF = galois.GF(2**8)
with pytest.raises(ValueError):
A = GF.Random(5)
A.null_space()
with pytest.raises(ValueError):
A = GF.Random((2,2,2))
A.null_space()


def test_null_space(field_null_space):
GF, X, Z = field_null_space["GF"], field_null_space["X"], field_null_space["Z"]

for i in range(len(X)):
dtype = random.choice(GF.dtypes)
x = X[i].astype(dtype)
z = x.null_space()
assert np.array_equal(z, Z[i])
assert type(z) is GF

0 comments on commit cf97f21

Please sign in to comment.