Skip to content

Commit

Permalink
Add unit tests for column_space()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 12, 2022
1 parent 3c8ced9 commit 839d621
Show file tree
Hide file tree
Showing 35 changed files with 78 additions and 3 deletions.
51 changes: 48 additions & 3 deletions scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ def make_luts(field, sub_folder, seed, sparse=False):
X.append(np.array([[I(e) for e in row] for row in x], dtype))
Z.append(z)

x = copy(x)
x[j,:] = FIELD.random_element() * x[0,:]

# Zero matrix
x = copy(x)
x[:] = F(0)
Expand All @@ -523,6 +520,54 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "row_space.pkl")

set_seed(seed + 209)
shapes = [(2,2), (2,3), (2,4), (3,2), (4,2), (3,3)]
X = []
Z = []
for i in range(len(shapes)):
deg = shapes[i][0] # 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.column_space()
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 column 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] = FIELD.random_element() * x[:,0]

z = x.column_space()
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.column_space()
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, "column_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 @@ -335,6 +335,15 @@ def field_row_space(field_folder):
return d


@pytest.fixture(scope="session")
def field_column_space(field_folder):
GF, d = read_pickle(field_folder, "column_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)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(109987^4)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2147483647)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2147483647)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^100)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2^100)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^2)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2^2)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^3)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2^3)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^32)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2^32)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(2^8)/row_space.pkl
Binary file not shown.
Binary file not shown.
Binary file modified tests/fields/data/GF(2^8, 283, 19)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(31)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(31)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(3191)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(3191)/row_space.pkl
Binary file not shown.
Binary file not shown.
Binary file modified tests/fields/data/GF(36893488147419103183)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(5)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(5)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(7)/row_space.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7^3)/column_space.pkl
Binary file not shown.
Binary file modified tests/fields/data/GF(7^3)/row_space.pkl
Binary file not shown.
Binary file not shown.
Binary file modified tests/fields/data/GF(7^3, 643, 244)/row_space.pkl
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 @@ -447,3 +447,24 @@ def test_row_space(field_row_space):
z = x.row_space()
assert np.array_equal(z, Z[i])
assert type(z) is GF


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


def test_column_space(field_column_space):
GF, X, Z = field_column_space["GF"], field_column_space["X"], field_column_space["Z"]

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

0 comments on commit 839d621

Please sign in to comment.