-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests_linsys.py
151 lines (123 loc) · 7.14 KB
/
tests_linsys.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
import unittest
from decimal import Decimal
from line import MyDecimal
from linsys import LinearSystem
from plane import Plane
from vector import Vector
class LinearSystemTestCase(unittest.TestCase):
def test_something(self):
p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
s = LinearSystem([p0, p1, p2, p3])
print(s.indices_of_first_nonzero_terms_in_each_row())
print('{},{},{},{}'.format(s[0], s[1], s[2], s[3]))
print(len(s))
print(s)
s[0] = p1
print(s)
print(MyDecimal('1e-9').is_near_zero())
print(MyDecimal('1e-11').is_near_zero())
def test_quiz(self):
p0 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p1 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p2 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p3 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
s = LinearSystem([p0, p1, p2, p3])
s.swap_rows(0, 1)
self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3)
s.swap_rows(1, 3)
self.assertTrue(s[0] == p1 and s[1] == p3 and s[2] == p2 and s[3] == p0)
s.swap_rows(3, 1)
self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3)
s.multiply_coefficient_and_row(1, 0)
self.assertTrue(s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3)
s.multiply_coefficient_and_row(-1, 2)
self.assertTrue(s[0] == p1 and
s[1] == p0 and
s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') and
s[3] == p3)
s.multiply_coefficient_and_row(10, 1)
self.assertTrue(s[0] == p1 and
s[1] == Plane(normal_vector=Vector(['10', '10', '10']), constant_term='10') and
s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') and
s[3] == p3)
s.add_multiple_times_row_to_row(0, 0, 1)
self.assertTrue(s[0] == p1 and
s[1] == Plane(normal_vector=Vector(['10', '10', '10']), constant_term='10') and
s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') and
s[3] == p3)
s.add_multiple_times_row_to_row(1, 0, 1)
self.assertTrue(s[0] == p1 and
s[1] == Plane(normal_vector=Vector(['10', '11', '10']), constant_term='12') and
s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') and
s[3] == p3)
s.add_multiple_times_row_to_row(-1, 1, 0)
self.assertTrue(s[0] == Plane(normal_vector=Vector(['-10', '-10', '-10']), constant_term='-10') and
s[1] == Plane(normal_vector=Vector(['10', '11', '10']), constant_term='12') and
s[2] == Plane(normal_vector=Vector(['-1', '-1', '1']), constant_term='-3') and
s[3] == p3)
def test_compute_triangular_form(self):
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
self.assertTrue(t[0] == p1 and t[1] == p2)
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
t = s.compute_triangular_form()
self.assertTrue(t[0] == p1 and t[1] == Plane(constant_term='1'))
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
s = LinearSystem([p1, p2, p3, p4])
t = s.compute_triangular_form()
self.assertTrue(t[0] == p1 and
t[1] == p2 and
t[2] == Plane(normal_vector=Vector(['0', '0', '-2']), constant_term='2') and
t[3] == Plane())
p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '2', '-5']), constant_term='3')
s = LinearSystem([p1, p2, p3])
t = s.compute_triangular_form()
self.assertTrue(t[0] == Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2') and
t[1] == Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1') and
t[2] == Plane(normal_vector=Vector(['0', '0', '-9']), constant_term='-2'))
def test_compute_rref(self):
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
r = s.compute_rref()
self.assertTrue(r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term='-1') and
r[1] == p2)
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='2')
s = LinearSystem([p1, p2])
r = s.compute_rref()
self.assertTrue(r[0] == p1 and
r[1] == Plane(constant_term='1'))
p1 = Plane(normal_vector=Vector(['1', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['0', '1', '0']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '1', '-1']), constant_term='3')
p4 = Plane(normal_vector=Vector(['1', '0', '-2']), constant_term='2')
s = LinearSystem([p1, p2, p3, p4])
r = s.compute_rref()
self.assertTrue(r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term='0') and
r[1] == p2 and
r[2] == Plane(normal_vector=Vector(['0', '0', '-2']), constant_term='2') and
r[3] == Plane())
p1 = Plane(normal_vector=Vector(['0', '1', '1']), constant_term='1')
p2 = Plane(normal_vector=Vector(['1', '-1', '1']), constant_term='2')
p3 = Plane(normal_vector=Vector(['1', '2', '-5']), constant_term='3')
s = LinearSystem([p1, p2, p3])
r = s.compute_rref()
self.assertTrue(
r[0] == Plane(normal_vector=Vector(['1', '0', '0']), constant_term=Decimal('23') / Decimal('9')) and
r[1] == Plane(normal_vector=Vector(['0', '1', '0']), constant_term=Decimal('7') / Decimal('9')) and
r[2] == Plane(normal_vector=Vector(['0', '0', '1']), constant_term=Decimal('2') / Decimal('9')))
if __name__ == '__main__':
unittest.main()