-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix multiplication.py
More file actions
65 lines (58 loc) · 2.06 KB
/
matrix multiplication.py
File metadata and controls
65 lines (58 loc) · 2.06 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
class Matrix:
def __init__(self, a: list, b: list):
self.a, self.b = a, b
self.A_ORDER = len(self.a), len(self.a[0]) if len(self.a) else 0
self.B_ORDER = len(self.b), len(self.b[0]) if len(self.b) else 0
def add(self):
if self.A_ORDER != self.B_ORDER:
return
return_list = [[]]
for i in range(self.A_ORDER[1]):
for j in range(self.B_ORDER[0]):
return_list[-1].append(self.a[i][j] + self.b[i][j])
return_list.append([])
return [i for i in return_list if i]
def subtract(self):
if self.A_ORDER != self.B_ORDER:
return
return_list = []
for i in range(self.A_ORDER[1]):
return_list.append([])
for j in range(self.B_ORDER[0]):
return_list[-1].append(self.a[i][j] - self.b[i][j])
return return_list
def transpose(self):
if self.A_ORDER[1] != self.B_ORDER[0]:
return
B_transpose = []
for i in range(self.B_ORDER[0]):
B_transpose.append([])
for j in self.b:
B_transpose[i].append(j[i])
return B_transpose
def multiply(self):
if self.A_ORDER[1] != self.B_ORDER[0]:
return
return_list = []
B_transpose = self.transpose()
for i in range(self.A_ORDER[0]):
return_list.append([])
for j in range(self.B_ORDER[1]):
return_list[-1].append(
sum(
[
self.a[i][k] * B_transpose[j][k]
for k in range(self.A_ORDER[1])
]
)
)
return return_list
def dotProduct(self):
if self.A_ORDER != self.B_ORDER:
return
return sum([self.a[i][i] * self.b[i][i] for i in range(self.A_ORDER[0])])
if __name__ == "__main__":
matrix = Matrix([[1, 0], [0, 1]], [[2, 3], [4, 5]])
print(*matrix.multiply(), sep="\n")
print()
print(*matrix.add(), sep="\n")