-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsr_test.py
70 lines (52 loc) · 2.47 KB
/
rsr_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import unittest
import numpy as np
from multipliers import (
RSRBinaryMultiplier,
NaiveMultiplier,
RSRTernaryMultiplier,
RSRPlusPlusBinaryMultiplier,
RSRPlusPlusTernaryMultiplier,
)
class TestRSRMultiplier(unittest.TestCase):
def setUp(self):
def generate_random_int_vector(size, low=0, high=100):
random_vector = np.random.randint(low, high, size)
return random_vector
self.n = 2 ** 12
self.v = generate_random_int_vector(self.n)
def test_rsr_multiplier(self):
def generate_random_binary_matrix(n):
binary_matrix = np.random.randint(2, size=(n, n))
return binary_matrix
A = generate_random_binary_matrix(self.n)
expected_result = NaiveMultiplier(A).multiply(self.v)
rsr_multiplier = RSRBinaryMultiplier(A)
rsr_result = rsr_multiplier.multiply(self.v)
np.testing.assert_allclose(rsr_result, expected_result, rtol=1e-6, atol=1e-6)
def test_rsr_ternary_multiplier(self):
def generate_random_ternary_matrix(n):
ternary_matrix = np.random.randint(low=-1, high=2, size=(n, n))
return ternary_matrix
A = generate_random_ternary_matrix(self.n)
expected_result = NaiveMultiplier(A).multiply(self.v)
rsr_multiplier = RSRTernaryMultiplier(A)
rsr_result = rsr_multiplier.multiply(self.v)
np.testing.assert_allclose(rsr_result, expected_result, rtol=1e-6, atol=1e-6)
def test_rsr_pp_binary_multiplier(self):
def generate_random_binary_matrix(n):
binary_matrix = np.random.randint(2, size=(n, n))
return binary_matrix
A = generate_random_binary_matrix(self.n)
expected_result = NaiveMultiplier(A).multiply(self.v)
rsr_pp_multiplier = RSRPlusPlusBinaryMultiplier(A)
rsr_pp_result = rsr_pp_multiplier.multiply(self.v)
np.testing.assert_allclose(rsr_pp_result, expected_result, rtol=1e-6, atol=1e-6)
def test_rsr_pp_ternary_multiplier(self):
def generate_random_ternary_matrix(n):
ternary_matrix = np.random.randint(low=-1, high=2, size=(n, n))
return ternary_matrix
A = generate_random_ternary_matrix(self.n)
expected_result = NaiveMultiplier(A).multiply(self.v)
rsr_pp_multiplier = RSRPlusPlusTernaryMultiplier(A)
rsr_pp_result = rsr_pp_multiplier.multiply(self.v)
np.testing.assert_allclose(rsr_pp_result, expected_result, rtol=1e-6, atol=1e-6)