Skip to content

Commit

Permalink
Add unit tests for np.convolve()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Nov 8, 2022
1 parent b131da1 commit 8a692ff
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 1 deletion.
27 changes: 26 additions & 1 deletion scripts/generate_field_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import sage
import numpy as np
from sage.all import GF, PolynomialRing, copy, log, matrix, vector, xgcd, lcm, prod, crt
from sage.all import GF, PolynomialRing, copy, log, matrix, vector, xgcd, lcm, prod, crt, convolution

FIELD = None
RING = None
Expand Down Expand Up @@ -187,6 +187,10 @@ def make_luts(field, sub_folder, seed, sparse=False):
}
save_json(d, folder, "properties.json", indent=True)

###############################################################################
# Arithmetic
###############################################################################

set_seed(seed + 1)
X, Y, XX, YY, ZZ = io_2d(0, order, 0, order, sparse=sparse)
for i in range(ZZ.shape[0]):
Expand Down Expand Up @@ -340,6 +344,27 @@ def make_luts(field, sub_folder, seed, sparse=False):
d = {"X": X, "Z": Z}
save_pickle(d, folder, "field_norm.pkl")

###############################################################################
# Advanced arithmetic
###############################################################################

set_seed(seed + 101)
X = []
Y = []
Z = []
for i in range(3):
x = randint_matrix(0, order, (10,))
y = randint_matrix(0, order, (10,))
X.append(x)
Y.append(y)
x = vector(FIELD, [F(xi) for xi in x])
y = vector(FIELD, [F(yi) for yi in y])
z = convolution(x, y)
z = np.array([I(zi) for zi in z], dtype)
Z.append(z)
d = {"X": X, "Y": Y, "Z": Z}
save_pickle(d, folder, "convolve.pkl")

###############################################################################
# Linear algebra
###############################################################################
Expand Down
14 changes: 14 additions & 0 deletions tests/fields/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ def field_log(field_folder):
return d


###############################################################################
# Fixtures for advanced arithmetic over finite fields
###############################################################################

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


###############################################################################
# Fixtures for linear algebra over finite fields
###############################################################################
Expand Down
Binary file added tests/fields/data/GF(109987^4)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2147483647)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^100)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^2)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^3)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^32)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(2^8, 283, 19)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(31)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(3191)/convolve.pkl
Binary file not shown.
Binary file not shown.
Binary file added tests/fields/data/GF(5)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7^3)/convolve.pkl
Binary file not shown.
Binary file added tests/fields/data/GF(7^3, 643, 244)/convolve.pkl
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/fields/test_advanced_arithmetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
A pytest module to test the accuracy of FieldArray advanced arithmetic.
"""
import random

import pytest
import numpy as np

import galois


def test_convolve(field_convolve):
GF, X, Y, Z = field_convolve["GF"], field_convolve["X"], field_convolve["Y"], field_convolve["Z"]
for i in range(len(X)):
dtype = random.choice(GF.dtypes)
x = X[i].astype(dtype)
y = Y[i].astype(dtype)

z = np.convolve(x, y)
assert np.array_equal(z, Z[i])
assert type(z) is GF
assert z.dtype == dtype

0 comments on commit 8a692ff

Please sign in to comment.