-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
217 lines (162 loc) · 4.41 KB
/
matrix.py
File metadata and controls
217 lines (162 loc) · 4.41 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
import copy
class Matrix:
def __init__(self, values):
"""
Summary:
Initialize a matrix.
Parameters:
values (list of lists): The values for the matrix.
"""
self.values = values
self.rows, self.cols = self.check_values()
def __eq__(self, other):
"""
Summary:
Check if two matrices are equal.
Parameters:
self (Matrix): This matrix.
other (Matrix): The other matrix.
Returns:
True if they are equal else false.
"""
if self.rows != other.rows or self.cols != other.cols:
return False
equal = True
for x in range(len(self.values)):
for y in range(len(self.values[x])):
if self.values[x][y] != other.values[x][y]:
equal = False
return equal
def __str__(self):
"""
Summary:
Generate string that represents the matrix.
Returns:
A string that represents the matrix.
"""
# Find length of largest element
largest_element = 0
for x in range(len(self.values)):
for y in range(len(self.values[x])):
length = len(str(self.values[x][y]))
if length > largest_element:
largest_element = length
# Generate string
output = ""
for x in range(len(self.values)):
for y in range(len(self.values[x])):
# Spaces before element
for z in range(largest_element - len(str(self.values[x][y]))):
output = output + " "
# Element
output = output + str(self.values[x][y])
# Space or newline at end
if y + 1 == len(self.values[x]):
output = output + "\n"
else:
output = output + " "
return output
def __mul__(self, other):
"""
Summary:
Overload the matrix multiplication.
Returns:
Returns the product of self and other.
"""
if type(other) is Matrix:
return self.multiply_matrix_matrix(other)
elif type(other) is int:
return self.multiply_matrix_scalar(other)
else:
return NotImplemented()
def __rmul__(self, other):
"""
Summary:
Overload the matrix multiplication.
Returns:
Returns the product of self and other.
"""
if type(other) is Matrix:
return other.multiply_matrix_matrix(self)
elif type(other) is int:
return self.multiply_matrix_scalar(other)
else:
return NotImplemented()
def check_values(self):
"""
Summary:
Check if the given values are valid for a matrix.
Returns:
A tuple (rows, cols).
"""
valid = True
line_length = len(self.values[0])
for x in range(len(self.values)):
length = len(self.values[x])
if length != line_length:
valid = False
if not valid:
raise ValueError("Values do not form a square.")
return (len(self.values), line_length)
def generate_empty_values(self, rows, cols):
"""
Summary:
Generate empty values of the given size.
Parameters:
rows (int): The number of rows.
cols (int): The number of columns.
Returns:
Empty set of values.
"""
values = []
for x in range(rows):
values.append([None] * cols)
return values
def multiply_matrix_matrix(self, other):
"""
Summary:
Calculate the product of two matrices.
Parameters:
self (Matrix): This matrix.
other (Matrix): The other matrix.
Returns:
The product of the two matrices.
"""
if self.cols != other.rows:
raise ValueError("Matrices not correct sizes for multiplication.")
new_values = self.generate_empty_values(self.rows, other.cols)
for x in range(len(new_values)):
for y in range(len(new_values[x])):
new_values[x][y] = self.values[x][0] * other.values[0][y]
for z in range(1, self.cols):
new_values[x][y] = new_values[x][y] + self.values[x][z] * other.values[z][y]
return Matrix(new_values)
def multiply_matrix_scalar(self, s):
new_values = self.generate_empty_values(self.rows, self.cols)
for x in range(self.rows):
for y in range(self.cols):
new_values[x][y] = self.values[x][y] * s
return Matrix(new_values)
def copy(self):
"""
Summary:
Copy the matrix.
Returns:
A copy of the matrix.
"""
new_values = copy.deepcopy(self.values)
return Matrix(new_values)
def multiply_elements(self, other):
"""
Summary:
Multiply the elements of two matrices of the same dimension.
Parameters:
other (Matrix): The other matrix.
"""
if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Matrices are not matching dimensions.")
new_values = self.generate_empty_values(self.rows, self.cols)
for x in range(self.rows):
for y in range(self.cols):
new_values[x][y] = self.values[x][y] * other.values[x][y]
return Matrix(new_values)