-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
253 lines (222 loc) · 6.71 KB
/
matrix.py
File metadata and controls
253 lines (222 loc) · 6.71 KB
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
import math
from math import sqrt
import numbers
def zeroes(height, width):
"""
Creates a matrix of zeroes.
"""
z = [[0.0 for _ in range(width)] for __ in range(height)]
return Matrix(z)
def identity(n):
"""
Creates a n x n identity matrix.
"""
I = zeroes(n, n)
for i in range(n):
I.g[i][i] = 1.0
return I
def get_row(matrix, row_number):
row = matrix[row_number]
return row
def get_col(matrix, col_number):
col = []
for r in range(len(matrix)):
col.append(matrix[r][col_number])
return col
def dot_product(vector1, vector2):
dot = 0
for i in range(len(vector1)):
dot += vector1[i] * vector2[i]
return dot
class Matrix(object):
# Constructor
def __init__(self, grid):
self.g = grid
self.h = len(grid)
self.w = len(grid[0])
#
# Primary matrix math methods
#
def determinant(self):
"""
Calculates the determinant of a 1x1 or 2x2 matrix.
"""
if not self.is_square():
raise(ValueError, "Cannot calculate determinant of non-square matrix.")
if self.h > 2:
raise(NotImplementedError, "Calculating determinant not implemented for matrices largerer than 2x2.")
# TODO - your code here
if self.h == 1:
a = self.g[0][0]
return a
if self.h == 2:
a = self.g[0][0]
b = self.g[0][1]
c = self.g[1][0]
d = self.g[1][1]
return a * d - b * c
def trace(self):
"""
Calculates the trace of a matrix (sum of diagonal entries).
"""
if not self.is_square():
raise(ValueError, "Cannot calculate the trace of a non-square matrix.")
# TODO - your code here
trace = 0
for i in range(self.h):
trace += self.g[i][i]
return trace
def inverse(self):
"""
Calculates the inverse of a 1x1 or 2x2 Matrix.
"""
if not self.is_square():
raise(ValueError, "Non-square Matrix does not have an inverse.")
if self.h > 2:
raise(NotImplementedError, "inversion not implemented for matrices larger than 2x2.")
# TODO - your code here
if self.h == 1:
a = self.g[0][0]
inverse = 1/a
return Matrix([[inverse]])
if self.h == 2:
a = self.g[0][0]
b = self.g[0][1]
c = self.g[1][0]
d = self.g[1][1]
if a * d == b * c:
raise(ValueError, "The matrix is not invertible.")
else:
inverse = zeroes(2, 2)
for r in range(self.h):
for c in range(self.w):
inverse[r][c] = (1 / self.determinant()) * (self.trace() * identity(2)[r][c] - self.g[r][c])
return inverse
def T(self):
"""
Returns a transposed copy of this Matrix.
"""
# TODO - your code here
transpose = []
for c in range(self.w):
rowT = []
for r in range(self.h):
rowT.append(self.g[r][c])
transpose.append(rowT)
return Matrix(transpose)
def is_square(self):
return self.h == self.w
#
# Begin Operator Overloading
############################
def __getitem__(self,idx):
"""
Defines the behavior of using square brackets [] on instances
of this class.
Example:
> my_matrix = Matrix([ [1, 2], [3, 4] ])
> my_matrix[0]
[1, 2]
> my_matrix[0][0]
1
"""
return self.g[idx]
def __repr__(self):
"""
Defines the behavior of calling print on an instance of this class.
"""
s = ""
for row in self.g:
s += " ".join(["{} ".format(x) for x in row])
s += "\n"
return s
def __add__(self,other):
"""
Defines the behavior of the + operator
"""
if self.h != other.h or self.w != other.w:
raise(ValueError, "Matrices can only be added if the dimensions are the same")
#
# TODO - your code here
#
add = []
for r in range(self.h):
row = []
for c in range(self.w):
row.append(self.g[r][c] + other.g[r][c])
add.append(row)
return Matrix(add)
def __neg__(self):
"""
Defines the behavior of - operator (NOT subtraction)
Example:
> my_matrix = Matrix([ [1, 2], [3, 4] ])
> negative = -my_matrix
> print(negative)
-1.0 -2.0
-3.0 -4.0
"""
#
# TODO - your code here
#
negative = []
for r in range(self.h):
row = []
for c in range(self.w):
row.append(-self.g[r][c])
negative.append(row)
return Matrix(negative)
def __sub__(self, other):
"""
Defines the behavior of - operator (as subtraction)
"""
#
# TODO - your code here
#
if self.h != other.h or self.w != other.w:
raise(ValueError, "Matrices can only be subtracted if the dimensions are the same")
subtract = []
for r in range(self.h):
row = []
for c in range(self.w):
row.append(self.g[r][c] - other.g[r][c])
subtract.append(row)
return Matrix(subtract)
def __mul__(self, other):
"""
Defines the behavior of * operator (matrix multiplication)
"""
#
# TODO - your code here
#
mul = []
for r in range(self.h):
row_result = []
row_self = get_row(self.g, r)
for c in range(other.w):
col_other = get_col(other.g, c)
dot = dot_product(row_self, col_other)
row_result.append(dot)
mul.append(row_result)
return Matrix(mul)
def __rmul__(self, other):
"""
Called when the thing on the left of the * is not a matrix.
Example:
> identity = Matrix([ [1,0], [0,1] ])
> doubled = 2 * identity
> print(doubled)
2.0 0.0
0.0 2.0
"""
if isinstance(other, numbers.Number):
#
# TODO - your code here
#
rmul = []
for r in range(self.h):
row = []
for c in range(self.w):
row.append(other * self.g[r][c])
rmul.append(row)
return Matrix(rmul)