-
Notifications
You must be signed in to change notification settings - Fork 1
/
augmented_matrix.py
122 lines (90 loc) · 3.48 KB
/
augmented_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
116
117
118
119
120
121
122
"""
augmented_matrix.py
authored by Vincent
authored by Vincent
version 1.0.2
last modified Nov 28, 2020 at 3:59 PM (UTC + 7)
Copyright © 2020 Vincent. All rights reserved.
"""
def main():
""" Main function """
system = [
[1, 0, 0, 9],
[1, 1, 1, 10],
[1, 0, 1, 12]
]
aug_system = AugMatrix([row[:-1] for row in system], [row[-1] for row in system])
aug_system.echelon_with_steps()
echelon_with_steps(system)
class AugMatrix:
""" Augmented Matrix """
def __init__(self, core, res):
""" Constructor """
self.matrix = core
self.result = res
def echelon(self):
""" Row echelon form of self """
new_value = [self.matrix[i] + [self.result[i]] for i in range(len(self.matrix))]
# Loop through each row
for i, current in enumerate(new_value):
# Check if it's a potential pivot
if i < len(new_value) - 1:
# Elimate all in the same column beside the pivot
for j in range(i + 1, len(new_value)):
coef = new_value[j][i] / current[i]
new_value[j] = minus(new_value[j], times(coef, current))
return new_value
def echelon_with_steps(self):
""" Row echelon form of self """
new_value = [self.matrix[i] + [self.result[i]] for i in range(len(self.matrix))]
# Loop through each row
print("Step begin here")
for i, current in enumerate(new_value):
# Check if it's a potential pivot
if i < len(new_value) - 1:
# Elimate all in the same column beside the pivot
for j in range(i + 1, len(new_value)):
coef = new_value[j][i] / current[i]
new_value[j] = minus(new_value[j], times(coef, current))
# printing the grid in 2D
for row in new_value:
print(row)
print("vvvvvvv")
return new_value
def echelon(aug_matrix: list) -> list:
""" Return a row echelon form of block matrix """
new_value = aug_matrix[:]
# Loop through each row
for i, current in enumerate(new_value):
# Check if it's a potential pivot
if i < len(new_value) - 1:
# Elimate all in the same column beside the pivot
for j in range(i + 1, len(new_value)):
coef = new_value[j][i] / current[i]
new_value[j] = minus(new_value[j], times(coef, current))
return new_value
def echelon_with_steps(aug_matrix: list) -> list:
""" Return a row echelon form of block matrix """
new_value = aug_matrix[:]
# Loop through each row
print("Steps begins here")
for i, current in enumerate(new_value):
# Check if it's a potential pivot
if i < len(new_value) - 1:
# Elimate all in the same column beside the pivot
for j in range(i + 1, len(new_value)):
coef = new_value[j][i] / current[i]
new_value[j] = minus(new_value[j], times(coef, current))
# printing the grid in 2D
for row in new_value:
print(row)
print("vvvvvvv")
return new_value
def times(coef: int, array: list) -> list:
""" Time an array with an int """
return [(item * coef) for item in array]
def minus(lhs: list, rhs: list) -> list:
""" Reduce an array by another one"""
return [lhs[i] - rhs[i] for i in range(len(lhs))]
if __name__ == '__main__':
main()