-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_matrix.py
153 lines (138 loc) · 5.41 KB
/
test_matrix.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random
import numpy as np
from matrix import Matrix
from vector import Vector
class TestMatrix:
def test_sum(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
data_x = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
data_y = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
x = Matrix(rows, cols, data_x)
y = Matrix(rows, cols, data_y)
z = x + y
x_np = np.array([x._row(i) for i in range(x.n_rows)])
y_np = np.array([y._row(i) for i in range(y.n_rows)])
z_np = x_np + y_np
z_np_flat = [float(x) for x in z_np.flatten()]
assert z_np_flat == z.data
def test_sub(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
data_x = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
data_y = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
x = Matrix(rows, cols, data_x)
y = Matrix(rows, cols, data_y)
z = x - y
x_np = np.array([x._row(i) for i in range(x.n_rows)])
y_np = np.array([y._row(i) for i in range(y.n_rows)])
z_np = x_np - y_np
z_np_flat = [float(x) for x in z_np.flatten()]
assert z_np_flat == z.data
def test_transpose(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
assert [
float(i)
for i in np.array([[x._row(i) for i in range(x.n_rows)]]).T.flatten()
] == x.transpose().data
def test_matmul(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
data_x = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
data_y = [float(random.randint(-20, 20)) for _ in range(rows * cols)]
x = Matrix(rows, cols, data_x)
y = Matrix(cols, rows, data_y)
z = x * y
x_np = np.array([x._row(i) for i in range(x.n_rows)])
y_np = np.array([y._row(i) for i in range(y.n_rows)])
z_np = x_np @ y_np
z_np_flat = [float(x) for x in z_np.flatten()]
assert z_np_flat == z.data
def test_matrix_scalar_mul(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
scalar = random.randint(1, 10)
assert [
float(i)
for i in (
np.array([[x._row(i) for i in range(x.n_rows)]]) * scalar
).flatten()
] == (x * scalar).data
def test_matrix_vec_mul(self):
rows = random.randint(1, 50)
cols = random.randint(1, 50)
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
vec = Vector([float(random.randint(-20, 20)) for _ in range(cols)])
assert [
float(i)
for i in (
np.array([[x._row(i) for i in range(x.n_rows)]]) @ np.array(vec.data)
).flatten()
] == (x * vec).data
def test_rank(self):
rows = 4
cols = 6
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
assert (
np.linalg.matrix_rank(np.array([x._row(i) for i in range(x.n_rows)]))
== x.rank()
)
def test_det(self):
rows = 4
cols = rows
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
x_det = x.det()
np_det = float(np.linalg.det(np.array([x._row(i) for i in range(x.n_rows)])))
assert abs(x_det - np_det) < 1e-2
def test_inv(self):
rows = 4
cols = rows
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
np_x = np.array([x._row(i) for i in range(x.n_rows)])
x_inv = x.inv()
np_x_inv = np.linalg.inv(np_x)
np_x_inv_flat = [float(x) for x in np_x_inv.flatten()]
assert len(np_x_inv_flat) == len(x_inv.data)
for x, y in zip(np_x_inv_flat, x_inv.data):
assert abs(x - y) < 1e-2
def test_trace(self):
rows = random.randint(1, 50)
cols = rows
x = Matrix(
rows, cols, [float(random.randint(-20, 20)) for _ in range(rows * cols)]
)
assert np.trace(np.array([x._row(i) for i in range(x.n_rows)])) == x.trace()
def test_solve(self):
# test case taken from: https://www.youtube.com/watch?v=m0rg10KX_sI
rows = 4
cols = rows
x = Matrix(rows, cols, [1, 0, 1, 2, 0, 1, -2, 0, 1, 2, -1, 0, 2, 1, 3, -2])
np_x = np.array([x._row(i) for i in range(x.n_rows)])
b = Vector([6, -3, -2, 0])
solutions = x.solve(b)
np_solutions = np.linalg.solve(np_x, np.array(b.data))
assert len(np_solutions.flatten()) == solutions.dim
for x, y in zip(np_solutions.flatten(), solutions.data):
assert abs(x - y) < 1e-2
def test_eig(self):
x = Matrix(3, 3, [21, 20, 7, 3, 6, 2, 11, 15, 7])
eigens = x.eig()
np_eigens = np.linalg.eig(np.array([x._row(i) for i in range(x.n_rows)]))[0]
assert len(np_eigens) == eigens.dim
for x, y in zip(np_eigens, eigens.data):
assert abs(x - y) < 1e-2