Added proper unit tests for gradient calculation

This commit is contained in:
Andreas Tsouchlos 2022-11-07 19:38:41 +01:00
parent 97acaaafd3
commit 2002b1faa8

View File

@ -42,17 +42,26 @@ class GradientTestCase(unittest.TestCase):
"""Test case for the calculation of the gradient of the code-constraint-polynomial."""
def test_grad_h(self):
"""Test the gradient of the code-constraint polynomial."""
H = np.array([[1, 0, 0],
[0, 1, 0]])
x = np.array([1, 2, 2])
R = np.array([0])
# Hamming(7,4) code
G = np.array([[1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 1, 0, 1, 0],
[1, 1, 0, 1, 0, 0, 1]])
H = np.array([[1, 0, 1, 0, 1, 0, 1],
[0, 1, 1, 0, 0, 1, 1],
[0, 0, 0, 1, 1, 1, 1]])
R = np.array([[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1]])
x = np.array([1, 2, -1, -2, 2, 1, -1]) # Some randomly chosen vector
expected_grad_h = np.array([4, 26, -8, -36, 38, 28, -32]) # Manually calculated result
decoder = proximal.ProximalDecoder(H, R)
grad = decoder._grad_h(x)
grad_h = decoder._grad_h(x)
expected = 4 * (x**2 - 1)*x + 2 / x * np.array([0, 2, 0])
self.assertEqual(np.array_equal(grad, expected), True)
self.assertEqual(np.array_equal(grad_h, expected_grad_h), True)
def test_gen_A_B(self):
"""Test the generation of the A and B sets used for the gradient calculation."""