-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_crs_matrix.py
115 lines (102 loc) · 3.64 KB
/
test_crs_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
from crsmatrix import Matrix
import numpy
class TestMatrix:
def test_symmetric(self):
A = Matrix.tridiagonal(3)
print A.a
print A.ia
print A.ja
assert A.symmetric() == True
a = [1, 2, 1, 1]
ia = [0, 2, 3, 4]
ja = [0, 1, 0, 0]
A = Matrix(3, 3, a, ia, ja)
assert A.symmetric() == False
A = Matrix.from_mm_file('data/ash958.mtx')
assert A.symmetric() == False
def test_left_mult(self):
A = Matrix.tridiagonal(3)
a_inv = [0.75, 0.5, 0.25, 0.5 , 1., 0.5, 0.25, 0.5, 0.75]
ia_inv = [0, 3, 6, 9]
ja_inv = [0, 1, 2, 0, 1, 2, 0, 1, 2]
a = [1.0, 1.0, 1.0]
ia = [0, 1, 2, 3]
ja = [0, 1, 2]
A_inv = Matrix(3, 3, a_inv, ia_inv, ja_inv)
I = Matrix(3, 3, a, ia, ja)
assert A * A_inv == I
def test_transpose(self):
m = n = 3
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ia = [0, 3, 6, 9]
ja = [0, 1, 2, 0, 1, 2, 0, 1, 2]
initial_matrix = Matrix(m, n, a, ia, ja)
a_trans = [1, 4, 7, 2, 5, 8, 3, 6, 9]
expected_matrix = Matrix(m, n, a_trans, ia, ja)
output_matrix = initial_matrix.transpose()
assert expected_matrix == output_matrix
def test_from_column_vector(self):
m = 3
n = 1
a = [1, 2, 3]
ia = [0, 1, 2, 3]
ja = [0, 0, 0]
input_vector = numpy.matrix([[1],[2],[3]])
expected_matrix = Matrix(m, n, a, ia, ja)
output_matrix = Matrix.from_column_vector(input_vector)
assert expected_matrix == output_matrix
def test_push_column(self):
m = 3
n = 2
a = [1, 4, 2, 5, 3, 6]
ia = [0, 2, 4, 6]
ja = [0, 1, 0, 1, 0, 1]
first_vector = numpy.matrix([[1],[2],[3]])
second_vector = numpy.matrix([[4],[5],[6]])
expected_matrix = Matrix(m, n, a, ia, ja)
output_matrix = Matrix.from_column_vector(first_vector)
output_matrix.push_column(second_vector)
assert expected_matrix == output_matrix
def test_push_column_with_zeros(self):
m = 3
n = 2
a = [1, 2, 5, 3]
ia = [0, 1, 3, 4]
ja = [0, 0, 1, 0]
first_vector = numpy.matrix([[1],[2],[3]])
second_vector = numpy.matrix([[0],[5],[0]])
expected_matrix = Matrix(m, n, a, ia, ja)
output_matrix = Matrix.from_column_vector(first_vector)
output_matrix.push_column(second_vector)
assert expected_matrix == output_matrix
def test_from_mm_file(self):
file_path = './data/ash958.mtx'
ash958 = Matrix.from_mm_file(file_path)
assert ash958.shape() == (958, 292)
assert len(ash958) == 1916
def test_tridiagonal_generation(self):
m = 3
a = Matrix.tridiagonal(m)
assert len(a.ia) == m + 1
assert len(a) == 3 * m - 2
assert a.shape() == (m, m)
assert a.a == [2, -1, -1, 2, -1, -1, 2]
assert a.ia == [0, 2, 5, 7]
assert a.ja == [0, 1, 0, 1, 2, 1, 2]
def test_eq_and_ne(self):
m = 3
m1 = Matrix.tridiagonal(m)
a = [2, -1, -1, 2, -1, -1, 2]
ia = [0, 2, 5, 7]
ja = [0, 1, 0, 1, 2, 1, 2]
m2 = Matrix(m, m, a, ia, ja)
assert m1 == m2
a = [1, 1, 1, 1, 1, 1, 1]
m3 = Matrix(m, m, a, ia, ja)
assert m2 != m3
def test_mult_vect_right(self):
input_vector = numpy.matrix([1,1,1,1,1]).T
expected_vector = numpy.matrix([1,0,0,0,1]).T
test_matrix = Matrix.tridiagonal(5)
output_vector = test_matrix.mult_left_vector(input_vector)
assert (expected_vector == output_vector).all()