-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_matrices.py
469 lines (441 loc) · 19.5 KB
/
test_matrices.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import unittest
import matrices
import errors
import decimal
class TestMatrix(unittest.TestCase):
"""Here we testing matrix class"""
def test_1(self):
A = matrices.Matrix([[1, 2, 3], [4, 5, 6]], 2, 3)
with self.assertRaises(errors.WrongDimension):
B = matrices.Matrix([[1, 2], [3, 4, 5]])
with self.assertRaises(errors.WrongDimension):
C = matrices.Matrix([[1, 2, 3], [4, 5]])
def test_2(self):
"""Checking for not numeric type"""
with self.assertRaises(errors.WrongElementType):
A = matrices.Matrix([["fgg", 2, 3], [4, 5, 6]], 2, 3)
B = matrices.Matrix([[1, 8, None, 9], [7, 3, 4, 5]])
def test_3(self):
"""Checking for errors with passing wrong dimension"""
with self.assertRaises(errors.WrongDimension):
A= matrices.Matrix([[1, 2, 3], [4, 5, 6]], 2, 2)
def test_4(self):
"""Checking for errors with wrong input type"""
with self.assertRaises(errors.WrongInputType):
A = matrices.Matrix(1, 1, 1)
A = matrices.Matrix({"1": 1}, 1, 1)
A = matrices.Matrix((1, ), 1, 1)
def test_5(self):
"""Checking is_zero_matrix"""
A = matrices.Matrix([[0, 1], [3, 2]])
self.assertFalse(A.is_zero_matrix())
B = matrices.Matrix([[0], [0]])
self.assertTrue(B.is_zero_matrix())
def test_6(self):
"""Checking for columns and rows input in __init__"""
with self.assertRaises(errors.WrongDimension):
A = matrices.Matrix([[1, 2], [3, 4]], 2, "gg")
def test_7(self):
"""Checking is_square_matrix method"""
A = matrices.Matrix([[1, 2], [3, 4]])
self.assertTrue(A.is_square_matrix())
B = matrices.Matrix([[1]])
self.assertTrue(B.is_square_matrix())
C = matrices.Matrix([[1, 4, 5], [3, 6, 7]])
self.assertFalse(C.is_square_matrix())
def test_8(self):
""" test for is_diagonal_matrix method"""
A = matrices.Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 9]])
self.assertTrue(A.is_diagonal_matrix())
B = matrices.Matrix([[1, 0, 0], [0, 5, 6], [0, 0, 9]])
self.assertFalse(B.is_diagonal_matrix())
C = matrices.Matrix([[1, 0, 0, 0], [0, 5, 0, 0], [0, 0, 9, 0]])
# Legacy test for square matrix commented below
# with self.assertRaises(errors.WrongInputType):
# self.assertTrue(C.is_diagonal_matrix())
self.assertFalse(C.is_diagonal_matrix())
def test_9(self):
"""Testing is_identity_matrix method"""
A = matrices.Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
self.assertTrue(A.is_identity_matrix())
B = matrices.Matrix([[1, 0, 0], [0, 5, 0], [0, 0, 9]])
self.assertFalse(B.is_identity_matrix())
C = matrices.Matrix([[1, 0, 0], [0, 1, 0], [1, 0, 1]])
self.assertFalse(C.is_identity_matrix())
D = matrices.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
# with self.assertRaises(errors.WrongInputType):
# D.is_identity_matrix()
self.assertFalse(D.is_identity_matrix())
def test_10(self):
"""Testing is_equal method"""
A = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
self.assertTrue(A.matrix_is_equal(B))
self.assertTrue(B.matrix_is_equal(A))
C = matrices.Matrix([[1, 2, 3], [4, 5, 0], [7, 8, 9]])
self.assertFalse(A.matrix_is_equal(C))
self.assertTrue(A.matrix_is_equal(A))
I = matrices.IdentityMatrix(2)
self.assertFalse(A.matrix_is_equal(I))
V = matrices.Matrix([[1, 2, 3]])
self.assertFalse(A.matrix_is_equal(V))
def test_11(self):
"""Testing matrix_transposition method"""
A = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
AT = matrices.Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
self.assertTrue(AT.matrix_is_equal(A.matrix_transposition()))
B = A.matrix_transposition()
C = B.matrix_transposition()
self.assertTrue(A.matrix_is_equal(C))
def test_12(self):
"""Testing matrix_is_transpose method"""
A = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
AT = matrices.Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
self.assertTrue(A.matrix_is_transpose(AT))
B = A.matrix_transposition()
self.assertTrue(B.matrix_is_transpose(A))
C = matrices.Matrix([[1, 3, 2], [2, 5, 8], [3, 6, 9]])
self.assertFalse(C.matrix_is_transpose(A))
D = matrices.IdentityMatrix(5)
self.assertFalse(D.matrix_is_transpose(A))
self.assertTrue(D.matrix_is_transpose(D))
def test_13(self):
"""Testing matrices addition"""
A = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = matrices.Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 2]])
C = matrices.Matrix([[2, 3, 4], [5, 6, 7], [8, 9, 11]])
AB = A.matrix_addition(B)
self.assertTrue(AB.matrix_is_equal(C))
I = matrices.IdentityMatrix(2)
with self.assertRaises(errors.WrongDimension):
AI = A.matrix_addition(I)
BA = B.matrix_addition(A)
self.assertTrue(C.matrix_is_equal(BA))
O = matrices.ZeroMatrix(3, 3)
AO = A.matrix_addition(O)
self.assertTrue(AO.matrix_is_equal(A))
D = matrices.Matrix([[0, 2, 3], [3, 5, -7]])
E = matrices.Matrix([[-1, -2, 4], [-4, 5, 8]])
F = matrices.Matrix([[-1, 0, 7], [-1, 10, 1]])
DE = D.matrix_addition(E)
self.assertTrue(DE.matrix_is_equal(F))
def test_14(self):
"""Testing multiplication by number"""
I = matrices.IdentityMatrix(3)
Im = I.multiply_by_number(3)
IM = matrices.Matrix([[3, 0, 0], [0, 3, 0], [0, 0, 3]])
self.assertTrue(IM.matrix_is_equal(Im))
with self.assertRaises(errors.WrongInputType):
I.multiply_by_number([3])
A = matrices.Matrix([[2, 4], [-5, -7]])
AM = matrices.Matrix([[-2, -4], [5, 7]])
Am = A.multiply_by_number(-1)
self.assertTrue(Am.matrix_is_equal(AM))
# lets try to mess with precision
with self.assertRaises(errors.WrongDimension):
I.multiply_by_number(22, -57)
with self.assertRaises(errors.WrongInputType):
I.multiply_by_number(-.78, "fg")
# is Am still equal to AM even with precision = 20?
Am = A.multiply_by_number(-1, 20)
self.assertTrue(Am.matrix_is_equal(AM))
n = decimal.Decimal('-1.0')
Am = A.multiply_by_number(n)
self.assertTrue(Am.matrix_is_equal(AM))
def test_15(self):
"""Testing subtraction method"""
A = matrices.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = matrices.Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 2]])
C = matrices.Matrix([[0, 1, 2], [3, 4, 5], [6, 7, 7]])
AB = A.matrix_subtraction(B)
self.assertTrue(AB.matrix_is_equal(C))
I = matrices.IdentityMatrix(2)
with self.assertRaises(errors.WrongDimension):
AI = A.matrix_subtraction(I)
O = matrices.ZeroMatrix(3, 3)
AO = A.matrix_subtraction(O)
self.assertTrue(AO.matrix_is_equal(A))
D = matrices.Matrix([[0, 2, 3], [3, 5, -7]])
E = matrices.Matrix([[-1, -2, 4], [-4, 5, 8]])
F = matrices.Matrix([[1, 4, -1], [7, 0, -15]])
DE = D.matrix_subtraction(E)
self.assertTrue(DE.matrix_is_equal(F))
def test_16(self):
"""Testing division by number method"""
A = matrices.Matrix([[0.0, 0.3, 0.6], [-0.6, -0.3, 1.2]])
AD = matrices.Matrix([[0, 1, 2], [-2, -1, 4]])
ad = A.divide_by_number(0.3, 3)
self.assertTrue(ad.matrix_is_equal(AD))
Am = A.multiply_by_number(1/0.3)
self.assertTrue(Am.matrix_is_equal(ad))
# now lets higher up precision to 19 for false result
# higher value of 'precision' doesn't mean better result, try to stick to reasonable number
ad = A.divide_by_number(0.3, 19)
self.assertFalse(ad.matrix_is_equal(AD))
C = matrices.Matrix([[4, 6, 8], [56, 44, 32]])
CD = matrices.Matrix([[2, 3, 4], [28, 22, 16]])
Cd = C.divide_by_number(2)
self.assertTrue(CD.matrix_is_equal(Cd))
def test_17(self):
"""testing matrices multiplication"""
K = matrices.Matrix([[1, 2], [1, 3]])
L = matrices.Matrix([[3, 4], [1, 4]])
kl = matrices.Matrix([[5, 12], [6, 16]])
KL = K.matrix_multiplication(L)
self.assertTrue(KL.matrix_is_equal(kl))
M = matrices.Matrix([[1, 2, 7], [3, 4, 7], [5, 6, 7]])
with self.assertRaises(errors.WrongDimension):
K.matrix_multiplication(M)
# checking the transposition multiplication: tran(AB) = tran(B)*tran(A)
A = matrices.Matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
B = matrices.Matrix([[2, 4],
[5, 6],
[3, -1]])
AB = A.matrix_multiplication(B)
ABt = AB.matrix_transposition()
Bt = B.matrix_transposition()
At = A.matrix_transposition()
BtAt = Bt.matrix_multiplication(At)
self.assertTrue(ABt.matrix_is_equal(BtAt))
C = matrices.Matrix([[1, 2],
[3, 4],
[5, 6]])
D = matrices.Matrix([[4, 7, 8],
[3, 2, 1]])
CD = C.matrix_multiplication(D)
DC = D.matrix_multiplication(C)
def test_18(self):
""" testing matrix_determinant method"""
A = matrices.Matrix([[1, 4],
[3, 6]])
B = matrices.Matrix([[5, -2, 1],
[3, 1, -4],
[6, 0, -3]])
self.assertEqual(A.matrix_determinant(0), -6)
self.assertEqual(B.matrix_determinant(), 9)
C = matrices.Matrix([[3, 5, 7, 8],
[-1, 7, 0, 1],
[0, 5, 3, 2],
[1, -1, 7, 4]])
self.assertEqual(C.matrix_determinant(5), 122)
D = matrices.Matrix([[1, 2, 0, 0, 0],
[3, 2, 3, 0, 0],
[0, 4, 3, 4, 0],
[0, 0, 5, 4, 5],
[0, 0, 0, 6, 5]])
self.assertEqual(D.matrix_determinant(), 640)
E = matrices.Matrix([[0, 6, -2, -1, 5],
[0, 0, 0, -9, -7],
[0, 15, 35, 0, 0],
[0, -1, -11, -2, 1],
[-2, -2, 3, 0, -2]])
self.assertEqual(E.matrix_determinant(), 2480)
Em = matrices.Matrix([[0, 6, -2, -1, 5],
[0, 0, 0, -9, -7],
[0, 15, 35, 0, 0],
[0, -1, -11, -2, 1],
[0, -2, 3, 0, -2]])
# determinant of matrix with 0 on main diagonal should be a 0
self.assertEqual(Em.matrix_determinant(), 0)
F = matrices.Matrix([[1, 2, 3, 0, 0, 0],
[4, 3, 0, 0, -1, -2],
[1, 2, 1, 1, 1, 0],
[3, -2, -2, 0, 0, 0],
[4, 1, -1, -5, 5, 0],
[0, 0, 6, 5, 4, 0]])
self.assertEqual(F.matrix_determinant(), -2346)
G = matrices.Matrix([[2, 0, 4, 0, 5, 0, 1],
[0, 3, -5, -1, 2, 0, 3],
[4, 5, 1, 1, 2, 3, 0],
[2, -4, 4, 4, 4, 2, 2],
[2, 0, 3, 4, 1, 3, 0],
[0, -5, -5, 6, 1, 4, 0],
[3, 0, 4, -1, 2, 0, 7]])
GT = G.matrix_transposition()
self.assertEqual(G.matrix_determinant(), 4030)
# determinant of the transpose matrix should be equal to her original determinant
self.assertEqual(G.matrix_determinant(), GT.matrix_determinant())
I = matrices.Matrix([[1, 2, 3],
[2, 4, 6],
[7, 8, 9]])
# determinant of matrix with proportional rows should be a zero
self.assertEqual(I.matrix_determinant(), 0)
# and some more sets of matrices
a = matrices.Matrix([[1, -2, 4],
[-5, 2, 0],
[1, 0, 3]])
self.assertEqual(a.matrix_determinant(), -32)
# det(a * B) = det(a) * det(B)
aB = a.matrix_multiplication(B)
self.assertEqual(aB.matrix_determinant(), -288)
# big one incoming (13x13):
F = matrices.Matrix([[24, -13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[-24, 46, -24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, -33, 64, -33, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, -40, 78, -40, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, -45, 88, -45, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, -48, 94, -48, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, -49, 96, -49, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, -48, 94, -48, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, -45, 88, -45, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, -40, 78, -40, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, -33, 64, -33, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -24, 46, -24],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, -13]])
self.assertEqual(F.matrix_determinant(), 93383110850641920000)
o = matrices.ZeroMatrix(4, 4)
# determinant for zero matrix = 0
self.assertEqual(o.matrix_determinant(), 0)
i = matrices.IdentityMatrix(4)
# determinant of identity matrix equal to 1
self.assertEqual(i.matrix_determinant(), 1)
n = decimal.Decimal('4')
with self.assertRaises(errors.WrongInputType):
self.assertEqual(D.matrix_determinant(n), 640)
def test_19(self):
""" Testing add_number method"""
A = matrices.Matrix([[3, 2],
[4, 4]])
B = matrices.Matrix([[4.1, 3.1],
[5.1, 5.1]])
C = A.add_number(1.1)
self.assertTrue(C.matrix_is_equal(B))
O = matrices.ZeroMatrix(2, 2, number=1.1)
AO = A.matrix_addition(O)
self.assertTrue(B.matrix_is_equal(AO))
D = matrices.Matrix([[1, 2, 3],
[4, 5, 6]])
E = matrices.Matrix([[0, 1, 2],
[3, 4, 5]])
F = D.add_number(-1)
self.assertTrue(F.matrix_is_equal(E))
with self.assertRaises(errors.WrongElementType):
A.add_number(B)
with self.assertRaises(errors.WrongElementType):
A.add_number('one')
n = decimal.Decimal('1.1')
CA = A.add_number(n)
self.assertTrue(CA.matrix_is_equal(B))
def test_20(self):
""" testing subtract_number method"""
A = matrices.Matrix([[3, 2],
[4, 4]])
B = matrices.Matrix([[1.9, 0.9],
[2.9, 2.9]])
C = A.subtract_number(1.1)
self.assertTrue(B.matrix_is_equal(C))
D = matrices.Matrix([[4.1, 3.1],
[5.1, 5.1]])
C = A.subtract_number(-1.1)
self.assertTrue(D.matrix_is_equal(C))
n = decimal.Decimal('1.1')
Cn = A.subtract_number(n)
self.assertTrue(B.matrix_is_equal(Cn))
def test_21(self):
"""testing override standard math operations"""
# addition
A = matrices.Matrix([[1, 2],
[3, 4]])
B = matrices.Matrix([[3, 2],
[4, 7]])
C = matrices.Matrix([[4, 4],
[7, 11]])
AB = A + B
BA = B + A
self.assertTrue(AB.matrix_is_equal(C))
self.assertTrue(BA.matrix_is_equal(C))
self.assertTrue(AB.matrix_is_equal(A.matrix_addition(B)))
D = matrices.Matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
with self.assertRaises(errors.WrongDimension):
AD = A + D
E = matrices.Matrix([[3.1, 4.1],
[5.1, 6.1]])
k = 2.1
Ak = A + k
self.assertTrue(Ak.matrix_is_equal(E))
# we cant add matrix to a number
with self.assertRaises(TypeError):
Ak = k + A
# subtraction
F = matrices.Matrix([[-2, 0],
[-1, -3]])
G = matrices.Matrix([[2, 0],
[1, 3]])
AB = A - B
self.assertTrue(AB.matrix_is_equal(F))
BA = B - A
self.assertTrue(BA.matrix_is_equal(G))
Fk = matrices.Matrix([[-0.1, 0.9],
[1.9, 2.9]])
k = 1.1
Ak = A - k
self.assertTrue(Ak.matrix_is_equal(Fk))
# cant subtract matrix from a number
with self.assertRaises(TypeError):
Ak = k - A
# multiplication
A = matrices.Matrix([[1, 2],
[3, 4]])
B = matrices.Matrix([[5, 6],
[7, 8],
[1, 1]])
C = matrices.Matrix([[1, 2, 3],
[4, 5, 6]])
D = matrices.Matrix([[9, 12, 15],
[19, 26, 33]])
AC = A * C
self.assertTrue(AC.matrix_is_equal(D))
with self.assertRaises(errors.WrongDimension):
AB = A * B
k = -1.1
E = matrices.Matrix([[-1.1, -2.2],
[-3.3, -4.4]])
Ak = A*k
self.assertTrue(Ak.matrix_is_equal(E))
with self.assertRaises(TypeError):
Ak = k*A
# division
k = decimal.Decimal(1.1)
E = matrices.Matrix([[0.91, 1.82],
[2.73, 3.64]])
Ak = A/k
self.assertTrue(Ak.matrix_is_equal(E))
# division matrix on matrix not supported
with self.assertRaises(errors.GeneralMatrixError):
AB = A/B
class TestZeroMatrix(unittest.TestCase):
"""class for testing ZeroMatrix subclass of Matrix"""
def test_1(self):
A = matrices.ZeroMatrix(3, 4)
self.assertTrue(A.is_zero_matrix())
with self.assertRaises(errors.WrongInputType):
B = matrices.ZeroMatrix(6, "f")
C = matrices.ZeroMatrix(2, 2)
self.assertTrue(C.is_square_matrix())
self.assertFalse(C.is_diagonal_matrix())
Z = matrices.Matrix([[0, 0], [0, 0]])
self.assertTrue(C.matrix_is_equal(Z))
D = matrices.ZeroMatrix(3, 1, number=-4.5)
E = matrices.Matrix([[-4.5],
[-4.5],
[-4.5]])
self.assertTrue(D.matrix_is_equal(E))
with self.assertRaises(errors.WrongElementType):
F = matrices.ZeroMatrix(3, 4, number='ffg')
class TestIdentityMatrix(unittest.TestCase):
"""class for testing IdentityMatrix subclass of Matrix"""
def test_1(self):
A = matrices.IdentityMatrix(3)
B = matrices.Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
self.assertTrue(A.is_identity_matrix())
self.assertFalse(A.is_zero_matrix())
self.assertTrue(A.is_diagonal_matrix())
self.assertTrue(A.is_square_matrix())
self.assertTrue(A.matrix_is_equal(B))
# transpose identity matrix ill be equal to self
C = A.matrix_transposition()
self.assertTrue(C.matrix_is_equal(A))