-
Notifications
You must be signed in to change notification settings - Fork 135
/
test.py
156 lines (108 loc) · 5.36 KB
/
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
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
154
155
156
import unittest
import numpy as np
from decimal import *
class LinearRegressionTestCase(unittest.TestCase):
"""Test for linear regression project"""
def test_shape(self):
for _ in range(10):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.randint(low=-10,high=10,size=(r,c))
self.assertEqual(shape(matrix.tolist()),(r,c),'Wrong answer')
def test_matxRound(self):
for decpts in range(10):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.random((r,c))
mat = matrix.tolist()
dec_true = [[Decimal(str(round(num,decpts))) for num in row] for row in mat]
matxRound(mat,decpts)
dec_test = [[Decimal(str(num)) for num in row] for row in mat]
res = Decimal('0')
for i in range(len(mat)):
for j in range(len(mat[0])):
res += dec_test[i][j].compare_total(dec_true[i][j])
self.assertEqual(res,Decimal('0'),'Wrong answer')
def test_transpose(self):
for _ in range(100):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.random((r,c))
mat = matrix.tolist()
t = np.array(transpose(mat))
self.assertEqual(t.shape,(c,r),"Expected shape{}, but got shape{}".format((c,r),t.shape))
self.assertTrue((matrix.T == t).all(),'Wrong answer')
def test_matxMultiply(self):
for _ in range(100):
r,d,c = np.random.randint(low=1,high=25,size=3)
mat1 = np.random.randint(low=-10,high=10,size=(r,d))
mat2 = np.random.randint(low=-5,high=5,size=(d,c))
dotProduct = np.dot(mat1,mat2)
dp = np.array(matxMultiply(mat1.tolist(),mat2.tolist()))
self.assertEqual(dotProduct.shape, dp.shape,
'Wrong answer, expected shape{}, but got shape{}'.format(dotProduct.shape, dp.shape))
self.assertTrue((dotProduct == dp).all(),'Wrong answer')
mat1 = np.random.randint(low=-10,high=10,size=(r,5))
mat2 = np.random.randint(low=-5,high=5,size=(4,c))
mat3 = np.random.randint(low=-5,high=5,size=(6,c))
with self.assertRaises(ValueError,msg="Matrix A\'s column number doesn\'t equal to Matrix b\'s row number"):
matxMultiply(mat1.tolist(),mat2.tolist())
with self.assertRaises(ValueError,msg="Matrix A\'s column number doesn\'t equal to Matrix b\'s row number"):
matxMultiply(mat1.tolist(),mat3.tolist())
def test_augmentMatrix(self):
for _ in range(50):
r,c = np.random.randint(low=1,high=25,size=2)
A = np.random.randint(low=-10,high=10,size=(r,c))
b = np.random.randint(low=-10,high=10,size=(r,1))
Amat = A.tolist()
bmat = b.tolist()
Ab = np.array(augmentMatrix(Amat,bmat))
ab = np.hstack((A,b))
self.assertTrue(A.tolist() == Amat,"Matrix A shouldn't be modified")
self.assertEqual(Ab.shape, ab.shape,
'Wrong answer, expected shape{}, but got shape{}'.format(ab.shape, Ab.shape))
self.assertTrue((Ab == ab).all(),'Wrong answer')
def test_swapRows(self):
for _ in range(10):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.random((r,c))
mat = matrix.tolist()
r1, r2 = np.random.randint(0,r, size = 2)
swapRows(mat,r1,r2)
matrix[[r1,r2]] = matrix[[r2,r1]]
self.assertTrue((matrix == np.array(mat)).all(),'Wrong answer')
def test_scaleRow(self):
for _ in range(10):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.random((r,c))
mat = matrix.tolist()
rr = np.random.randint(0,r)
with self.assertRaises(ValueError):
scaleRow(mat,rr,0)
scale = np.random.randint(low=1,high=10)
scaleRow(mat,rr,scale)
matrix[rr] *= scale
self.assertTrue((matrix == np.array(mat)).all(),'Wrong answer')
def test_addScaledRow(self):
for _ in range(10):
r,c = np.random.randint(low=1,high=25,size=2)
matrix = np.random.random((r,c))
mat = matrix.tolist()
r1,r2 = np.random.randint(0,r,size=2)
scale = np.random.randint(low=1,high=10)
addScaledRow(mat,r1,r2,scale)
matrix[r1] += scale * matrix[r2]
self.assertTrue((matrix == np.array(mat)).all(),'Wrong answer')
def test_gj_Solve(self):
for _ in range(9999):
r = np.random.randint(low=3,high=10)
A = np.random.randint(low=-10,high=10,size=(r,r))
b = np.arange(r).reshape((r,1))
x = gj_Solve(A.tolist(),b.tolist(),epsilon=1.0e-8)
if np.linalg.matrix_rank(A) < r:
self.assertEqual(x,None,"Matrix A is singular")
else:
self.assertNotEqual(x,None,"Matrix A is not singular")
self.assertEqual(np.array(x).shape,(r,1),"Expected shape({},1), but got shape{}".format(r,np.array(x).shape))
Ax = np.dot(A,np.array(x))
loss = np.mean((Ax - b)**2)
self.assertTrue(loss<0.1,"Bad result.")
if __name__ == '__main__':
unittest.main()