/
linesys.py
304 lines (260 loc) · 10.1 KB
/
linesys.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
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
from typing import Iterable, Union
from copy import deepcopy
from decimal import Decimal
from plane import Plane
from tools import first_nonzero_index, is_zero
from vector import Vector
class Parametrization:
def __init__(self, basepoint: Vector, direction_vectors: Iterable[Vector]):
self.basepoint = basepoint
self.direction_vectors = direction_vectors
self.dimension = self.basepoint.dimension
for v in direction_vectors:
assert v.dimension == self.dimension
def __str__(self):
res = "[" + \
", ".join([f"x_{i}" for i in range(self.dimension)]) + \
f"] = {self.basepoint}"
for i, vector in enumerate(self.direction_vectors):
if vector:
res += f" + x_{i} * {vector}"
return res
class LinearSystem:
def __init__(self,
*planes: Union[Plane, Vector]):
self.planes = list(planes)
self.dimension = self.planes[0].dimension
for p in self.planes[1:]:
assert p.dimension == self.dimension, \
'All planes in the system should live in the same dimension'
def swap_rows(self, index1, index2):
"""
>>> p0 = Plane(Vector(1, 1, 1), 1)
>>> p1 = Plane(Vector(0, 1, 0), 2)
>>> p2 = Plane(Vector(1, 1, -1), 3)
>>> p3 = Plane(Vector(1, 0, -2), 2)
>>> s = LinearSystem(p0, p1, p2, p3)
>>> s.swap_rows(0, 1)
>>> s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3
True
>>> s.swap_rows(1, 3)
>>> s[0] == p1 and s[1] == p3 and s[2] == p2 and s[3] == p0
True
>>> s.swap_rows(3, 1)
>>> s[0] == p1 and s[1] == p0 and s[2] == p2 and s[3] == p3
True
"""
self[index1], self[index2] = self[index2], self[index1]
def multiply_coefficient_and_row(self, coefficient, index):
"""
>>> p0 = Plane(Vector(1, 1, 1), 1)
>>> p1 = Plane(Vector(0, 1, 0), 2)
>>> p2 = Plane(Vector(1, 1, -1), 3)
>>> p3 = Plane(Vector(1, 0, -2), 2)
>>> s = LinearSystem(p1, p0, p2, p3)
>>> s.multiply_coefficient_and_row(1, 0)
>>> s.multiply_coefficient_and_row(-1, 2)
>>> s.multiply_coefficient_and_row(10, 1)
>>> s[0] == p1
True
>>> s[1] == Plane(Vector(10, 10, 10), 10)
True
>>> s[2] == Plane(Vector(-1, -1, 1), -3)
True
>>> s[3] == p3
True
"""
self[index] = self[index] * coefficient
def add_multiple_times_row_to_row(self, coefficient, index_to_add,
index_to_be_added):
"""
>>> p0 = Plane(Vector(10, 10, 10), 10)
>>> p1 = Plane(Vector(0, 1, 0), 2)
>>> p2 = Plane(Vector(-1, -1, 1), -3)
>>> p3 = Plane(Vector(1, 0, -2), 2)
>>> s = LinearSystem(p1, p0, p2, p3)
>>> s.add_multiple_times_row_to_row(0, 0, 1)
>>> s[0] == p1
True
>>> s[1] == Plane(Vector(10, 10, 10), 10)
True
>>> s[2] == Plane(Vector(-1, -1, 1), -3)
True
>>> s[3] == p3
True
>>> s.add_multiple_times_row_to_row(1, 0, 1)
>>> s[0] == p1
True
>>> s[1] == Plane(Vector(10, 11, 10), 12)
True
>>> s[2] == Plane(Vector(-1, -1, 1), -3)
True
>>> s[3] == p3
True
>>> s.add_multiple_times_row_to_row(-1, 1, 0)
>>> s[0] == Plane(Vector(-10, -10, -10), -10)
True
>>> s[1] == Plane(Vector(10, 11, 10), 12)
True
>>> s[2] == Plane(Vector(-1, -1, 1), -3)
True
>>> s[3] == p3
True
"""
scaled_row = self[index_to_add] * coefficient
self[index_to_be_added] = self[index_to_be_added] + scaled_row
def indices_of_first_nonzero_term_in_each_row(self):
num_equations = len(self)
indices = [-1] * num_equations
for i, p in enumerate(self):
index = first_nonzero_index(p.normal_vector)
if index is None:
continue
indices[i] = index
return indices
def compute_triangular_form(self):
"""
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(0, 1, 1), 2))
>>> t = s.compute_triangular_form()
>>> t[0], t[1]
(Plane(Vector(1, 1, 1), 1), Plane(Vector(0, 1, 1), 2))
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(1, 1, 1), 2))
>>> t = s.compute_triangular_form()
>>> t[0], t[1]
(Plane(Vector(1, 1, 1), 1), Plane(Vector(0, 0, 0), 1))
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(0, 1, 0), 2),
... Plane(Vector(1, 1, -1), 3),
... Plane(Vector(1, 0, -2), 2))
>>> t = s.compute_triangular_form()
>>> t[0], t[1]
(Plane(Vector(1, 1, 1), 1), Plane(Vector(0, 1, 0), 2))
>>> t[2], t[3]
(Plane(Vector(0, 0, -2), 2), Plane(Vector(0, 0, 0), 0))
>>> s = LinearSystem(Plane(Vector(0, 1, 1), 1),
... Plane(Vector(1, -1, 1), 2),
... Plane(Vector(1, 2, -5), 3))
>>> t = s.compute_triangular_form()
>>> t[0]
Plane(Vector(1, -1, 1), 2)
>>> t[1]
Plane(Vector(0, 1, 1), 1)
>>> t[2]
Plane(Vector(0, 0, -9), -2)
"""
system = deepcopy(self)
equations = len(system)
cycles = min([system.dimension, equations])
for i in range(cycles):
if is_zero(system[i].normal_vector[i]):
for j in range(i + 1, equations):
if not is_zero(system[j].normal_vector[i]):
system.swap_rows(i, j)
break
else:
continue
for j in range(i + 1, equations):
k = -system[j].normal_vector[i] / system[i].normal_vector[i]
system.add_multiple_times_row_to_row(k, i, j)
return system
def compute_rref(self):
"""
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(0, 1, 1), 2))
>>> r = s.compute_rref()
>>> r[0] == Plane(Vector(1, 0, 0), -1)
True
>>> r[1] == Plane(Vector(0, 1, 1), 2)
True
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(1, 1, 1), 2))
>>> r = s.compute_rref()
>>> r[0], r[1]
(Plane(Vector(1, 1, 1), 1), Plane(Vector(0, 0, 0), 1))
>>> s = LinearSystem(Plane(Vector(1, 1, 1), 1),
... Plane(Vector(0, 1, 0), 2),
... Plane(Vector(1, 1, -1), 3),
... Plane(Vector(1, 0, -2), 2))
>>> r = s.compute_rref()
>>> r[0] == Plane(Vector(1, 0, 0), 0)
True
>>> r[1] == Plane(Vector(0, 1, 0), 2)
True
>>> r[2] == Plane(Vector(0, 0, -2), 2)
True
>>> r[3] == Plane(Vector(0, 0, 0), 0)
True
>>> s = LinearSystem(Plane(Vector(0, 1, 1), 1),
... Plane(Vector(1, -1, 1), 2),
... Plane(Vector(1, 2, -5), 3))
>>> r = s.compute_rref()
>>> r[0] == Plane(Vector(1, 0, 0), Decimal(23)/Decimal(9))
True
>>> r[1] == Plane(Vector(0, 1, 0), Decimal(7)/Decimal(9))
True
>>> r[2] == Plane(Vector(0, 0, 1), Decimal(2)/Decimal(9))
True
"""
system = self.compute_triangular_form()
for i, plane in reversed(list(enumerate(system))):
j = first_nonzero_index(plane.normal_vector)
if j is None:
if plane.constant_term:
c = 1 / plane.constant_term
system.multiply_coefficient_and_row(c, i)
continue
system.multiply_coefficient_and_row(1 / plane.normal_vector[j], i)
for k in range(i - 1, -1, -1):
c = -system[k].normal_vector[j]
system.add_multiple_times_row_to_row(c, i, k)
return system
def solve(self):
system = self.compute_rref()
answer = [0] * system.dimension
useful_equations = []
for i, plane in enumerate(system):
j = first_nonzero_index(plane.normal_vector)
if j is None:
if not is_zero(plane.constant_term):
return None
continue
useful_equations.append(plane)
answer[j] = plane.constant_term
if len(useful_equations) < self.dimension:
return self.build_parametrization(useful_equations)
return Vector(*answer)
def build_parametrization(self, equations: Iterable[Plane]):
free_variables = []
for equation in equations:
free_variables.append(first_nonzero_index(equation.normal_vector))
vectors = [[0] * self.dimension for _ in range(self.dimension)]
basepoint = [0] * self.dimension
for equation in equations:
j = first_nonzero_index(equation.normal_vector)
basepoint[j] = equation.constant_term
for i, k in enumerate(equation.normal_vector[j+1:]):
if k:
vectors[i + j + 1][j] = -k
vectors[i + j + 1][i + j + 1] = 1
return Parametrization(Vector(*basepoint),
[Vector(*vector) for vector in vectors])
def __len__(self):
return len(self.planes)
def __iter__(self):
return iter(self.planes)
def __getitem__(self, i):
return self.planes[i]
def __setitem__(self, i, x):
assert x.dimension == self.dimension, \
'All planes in the system should live in the same dimension'
self.planes[i] = x
def __str__(self):
ret = 'Linear System:\n'
temp = ['Equation {}: {}'.format(i + 1, p) for i, p in
enumerate(self.planes)]
ret += '\n'.join(temp)
return ret