forked from LS-Computer-Vision/Python-Basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlowMatrix.py
More file actions
112 lines (100 loc) · 2.99 KB
/
SlowMatrix.py
File metadata and controls
112 lines (100 loc) · 2.99 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
class SlowMatrix:
## The constructor
# @param matrix A 2d Python list containing data
def __init__(self, matrix):
self.a=matrix
## Matrix multiplication
# @param self SlowMatrix1
# @param mat2 SlowMatrix2
def __matmul__(self, mat2):
arr=[]
for i in range(len(self.a)):
col=[]
for j in range(len(mat2[0])):
sum=0
for x in range(len(mat2)):
sum=sum+(self.a[i][x] * mat2[x][j])
#print(self.a[i][x], mat2[x][j])
col.append(sum)
arr.append(col)
return arr
## Element wise multiplication
# @param self SlowMatrix1
# @param mat2 SlowMatrix2
def __mul__(self, mat2):
arr=[]
for i in range(len(mat2)):
col=[]
for j in range(len(mat2[0])):
mult=self.a[i][j]*mat2[i][j]
col.append(mult)
arr.append(col)
return arr
## Element wise addition
# @param self SlowMatrix1
# @param mat2 SlowMatrix2
def __add__(self, mat2):
arr=[]
for i in range(len(mat2)):
col=[]
for j in range(len(mat2[0])):
ad=self.a[i][j]+mat2[i][j]
col.append(ad)
arr.append(col)
return arr
## Element wise subtraction
# @param self SlowMatrix1
# @param mat2 SlowMatrix2
def __sub__(self, mat2):
arr=[]
for i in range(len(mat2)):
col=[]
for j in range(len(mat2[0])):
ad=self.a[i][j]-mat2[i][j]
col.append(ad)
arr.append(col)
return arr
## Equality operator
# @param self SlowMatrix1
# @param mat2 SlowMatrix2
def __eq__(self, mat2):
if self.a==mat2:
return True
else :
return False
## Calculate transpose
def transpose(self):
arr=[]
for i in range(len(self.a[0])):
col=[]
for j in range(len(self.a)):
col.append(self.a[j][i])
arr.append(col)
return arr
## Creates a SlowMatrix of 1s
# @param shape A python pair (row, col)
def ones(shape):
row,col=shape
arr=[[1]*col]*row
return arr[:]
## Creates a SlowMatrix of 0s
# @param shape A python pair (row, col)
def zeros(shape):
row,col=shape
arr=[[0]*col]*row
return arr[:]
## Returns i,jth element
# @param key A python pair (i,j)
def __getitem__(self, key):
i,j=key
return self.a[i][j]
## Sets i,jth element
# @param key A python pair (i,j)
# #param value Value to set
def __setitem__(self, key, value):
i,j=key
self.a[i][j]=value
## Converts SlowMatrix to a Python string
def __str__(self):
res=str(self.a)
return res