This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
194 lines (101 loc) · 3.68 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
#Vector class
class Vector:
def __init__(self, array1D):
if type(array1D) != list:
raise Exception("INPUT MUST BE A LIST OF INTEGERS")
for i in array1D:
if type(i) != int:
raise Exception("INPUT MUST BE A LIST OF INTEGERS")
self.values = array1D
def __repr__(self):
out = ""
for i in self.values:
out += "[" + str(i) + "]\n"
return out[:-1]
def __mul__(self, other):
if type(other) == int:
res = []
for i in self.values:
res += [i * other]
return Vector(res)
else:
raise Exception("OPERAND MUST BE INTEGER")
def __len__(self):
return len(self.values)
def __getitem__(self, key):
return self.values[key]
def dot(self, other):
"""returns the dot product of two vectors"""
if type(self) != type(other):
raise Exception("OPERAND MUST BE VECTOR")
if len(self) != len(other):
raise Exception("VECTORS MUST BE OF EQUAL DIMENSION")
res = 0
for i in range(len(self)):
res += self[i] * other[i]
return res
#Matrix class
class Matrix:
def __init__(self, array2D):
for i in range(len(array2D)):
if len (array2D[i]) != len(array2D[0]):
raise Exception('INVALID MATRIX')
self.values = array2D
#override to-string method
def __repr__(self):
out = ""
for i in self.values:
out += str(i) + "\n"
return out[0:-1]
#override A * B operator
def __mul__(self, other):
res = []
#for scalar multiplication
if type(other) == 'int':
for i in range(len(self.values)):
res += [[]]
for j in range(len(self.values[i])):
res[i] += [self.values[i][j] * 2]
return res
#for matrix multiplication
if type(self) == type(other):
if self.numCols() != other.numRows():
raise Exception("INVALID METRIX DIMENSIONS")
res = []
for i in range(self.numRows()):
res += [[]]
for i in range(self.numRows()):
for j in range(other.numCols()):
res[i] += [Vector(self[i]).dot(Vector(other.T()[j]))]
return Matrix(res)
__rmul__ = __mul__
def __getitem__(self, key):
return self.values[key]
def __setitem__(self, key, value):
self.values[key] = value
def transpose(self):
"""returns the transpose matrix"""
return Matrix(self.getCols())
T = transpose
def getRows(self):
"""returns list of rows"""
return self.values
def getCols(self):
"""returns list of columns"""
res = []
for i in range(self.numCols()):
res += [[]]
for i in range(self.numRows()):
for j in range(self.numCols()):
res[j] += [self.values[i][j]]
return res
def numRows(self):
return len(self.values)
def numCols(self):
return len(self.values[0])
a = Vector([1, 2, 3])
b = Vector([10, 20, 30])
#A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
#B = Matrix([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
A = Matrix([[1, 4], [2, 5], [3, 6]])
B = Matrix([[10, 20, 30], [40, 50, 60]])