-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.py
223 lines (179 loc) · 6.54 KB
/
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
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
from math import *
EPSILON = 1e-7
class Matrix:
def __init__(self, r: int, c: int):
self.data = [[0. for x in range(c)] for y in range(r)]
self.row_size = r
self.col_size = c
@staticmethod
def identify(n: int):
ret = Matrix(n, n)
for i in range(n):
ret.set_element(i, i, 1.)
return ret
@staticmethod
def from_input(r=-1, c=-1):
if r == -1:
r = int(input("row_size: "))
c = int(input("col_size: "))
elif c == -1:
c = r
ret = Matrix(r, c)
for i in range(r):
arr = input(f"{i}\'s row : ").split()
for j in range(c):
ret.set_element(i, j, float(eval(arr[j])))
return ret
@staticmethod
def from_1d_array(arr: list, is_col_vec=True):
if is_col_vec:
ret = Matrix(len(arr), 1)
else:
ret = Matrix(1, len(arr))
for i in range(len(arr)):
if is_col_vec:
ret.set_element(i, 0, float(arr[i]))
else:
ret.set_element(0, i, float(arr[i]))
return ret
@staticmethod
def from_2d_array(arr: list):
r = len(arr)
c = len(arr[0])
ret = Matrix(r, c)
for i in range(r):
for j in range(c):
ret.set_element(i, j, float(arr[i][j]))
return ret
@staticmethod
def from_col_vec_list(arr: list):
r = arr[0].row_size
c = len(arr)
ret = Matrix(r, c)
for j in range(c):
for i in range(r):
ret.set_element(i, j, arr[j].get_element(i, 0))
return ret
def T(self):
ret = Matrix(self.col_size, self.row_size)
for r in range(self.row_size):
for c in range(self.col_size):
ret.set_element(c, r, self.get_element(r, c))
return ret
def copy(self):
ret = Matrix(self.row_size, self.col_size)
for r in range(self.row_size):
for c in range(self.col_size):
ret.set_element(r, c, self.get_element(r, c))
return ret
def eo1_row_swap(self, r1: int, r2: int) -> None:
r3 = [ self.get_element(r2, c) for c in range(self.col_size) ]
for c in range(self.col_size):
self.set_element(r2, c, self.get_element(r1, c))
self.set_element(r1, c, r3[c])
def eo2_row_multiple_x(self, r: int, x: float) -> None:
for c in range(self.col_size):
self.set_element(r, c, x*self.get_element(r, c))
def eo3_r1_multiple_x_add_r2(self, r1: int, x: float, r2: int) -> None:
for c in range(self.col_size):
ori = self.get_element(r2, c)
delta = self.get_element(r1, c)*x
self.set_element(r2, c, ori + delta)
def set_element(self, r: int, c: int, val: float) -> None:
self.data[r][c] = val
def get_element(self, r: int, c: int) -> float:
return self.data[r][c]
def set_col_vec(self, c: int, val) -> None:
for r in range(self.row_size):
self.set_element(r, c, val.get_element(r, 0))
return None
def append_col_vec(self, val) -> None:
self.col_size += 1
for r in range(self.row_size):
self.data[r].append(val.get_element(r, 0))
return None
def get_col_vec(self, c: int):
ret = Matrix(self.row_size, 1)
for r in range(self.row_size):
ret.set_element(r, 0, self.get_element(r, c))
return ret
def get_sub_col_vec(self, c: int, offset: int):
ret = Matrix(self.row_size-offset, 1)
for r in range(offset, self.row_size):
ret.set_element(r-offset, 0, self.get_element(r, c))
return ret
def set_row_vec(self, r: int, val) -> None:
for c in range(self.col_size):
self.set_element(r, c, val.get_element(0, c))
return None
def get_row_vec(self, r: int):
ret = Matrix(1, self.col_size)
for c in range(self.col_size):
ret.set_element(0, c, self.get_element(r, c))
return ret
def __mul__(self, other):
ret = Matrix(self.row_size, other.col_size)
for r in range(self.row_size):
for c in range(other.col_size):
s = 0.
for k in range(self.col_size):
s += self.get_element(r, k)*other.get_element(k, c)
ret.set_element(r, c, s)
return ret
def __rmul__(self, other: float):
ret = Matrix(self.row_size, self.col_size)
for r in range(self.row_size):
for c in range(self.col_size):
ret.set_element(r, c, self.get_element(r, c)*other)
return ret
def __neg__(self):
ret = Matrix(self.row_size, self.col_size)
for r in range(self.row_size):
for c in range(self.col_size):
ret.set_element(r, c, -self.get_element(r, c))
return ret
def __add__(self, other):
ret = Matrix(self.row_size, self.col_size)
for r in range(self.row_size):
for c in range(self.col_size):
ret.set_element(r, c, self.get_element(r, c) + other.get_element(r, c))
return ret
def __sub__(self, other):
return self+(-other)
def __eq__(self, other):
for r in range(self.row_size):
for c in range(self.col_size):
if abs(self.get_element(r, c) - other.get_element(r, c)) > EPSILON:
return False
return True
def __ne__(self, other):
return not (self == other)
def __gt__(self, other):
for r in range(self.row_size):
for c in range(self.col_size):
if self.get_element(r, c) - other.get_element(r, c) <= EPSILON:
return False
return True
def __ge__(self, other):
return self == other or self > other
def __lt__(self, other):
return not (self >= other)
def __le__(self, other):
return self == other or self < other
def __pow__(self, y):
if y == 0:
return Matrix.identify(self.row_size)
if y % 2:
return self*(self**(y-1))
x = (self**(y//2))
return x*x
def __repr__(self):
ret = "\n"
ret += "---------------------------------------\n"
ret += f"row_size: {self.row_size}, col_size: {self.col_size}\n"
ret += "[\n"
for r in range(self.row_size):
ret += str(self.data[r])
ret += "\n"
ret += "]\n"
return ret