-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_matrix_operations.py
54 lines (41 loc) · 1.23 KB
/
test_matrix_operations.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
# use pytest to test various matrix operations
from pytest import fixture, raises
import script
@fixture
def matrix():
matrix_ = [
[1, 0, 1],
[-1, 1, 1],
[0, 1, 0]
]
return matrix_
def test_get_determinant(matrix):
assert script.get_determinant(matrix) == -2
def test_get_minor(matrix):
assert script.get_minor(matrix, 0, 0) == [[1, 1], [1, 0]]
assert script.get_minor(matrix, 0, 1) == [[-1, 1], [0, 0]]
assert script.get_minor(matrix, 1, 1) == [[1, 1], [0, 0]]
assert script.get_minor(matrix, 2, 2) == [[1, 0], [-1, 1]]
def test_transpose_matrix(matrix):
transposed_matrix = [
[1, -1, 0],
[0, 1, 1],
[1, 1, 0]
]
assert transposed_matrix == script.transpose_matrix(matrix)
def test_inverse_matrix(matrix):
inverse_matrix = [
[1/2, -1/2, 1/2],
[0, 0, 1],
[1/2, 1/2, -1/2]
]
assert inverse_matrix == script.inverse_matrix(matrix)
singular_matrix = [
[1, 0, 1],
[0, 1, 0],
[1, 0, 1]
]
with raises(ValueError) as ex:
script.inverse_matrix(singular_matrix)
# check exception message
assert str(ex.value) == 'The matrix is not invertible. The determinant is 0.'