From 2002b1faa8dc515e5994e0e84f4dbeb90ccf9e0a Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Mon, 7 Nov 2022 19:38:41 +0100 Subject: [PATCH] Added proper unit tests for gradient calculation --- sw/test/test_proximal.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/sw/test/test_proximal.py b/sw/test/test_proximal.py index 6c87cf6..2e1572a 100644 --- a/sw/test/test_proximal.py +++ b/sw/test/test_proximal.py @@ -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."""