-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
132 lines (103 loc) · 3.53 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
import random
import numpy as np
supported_constants = [int, float, np.float64]
class Matrix:
def __init__(self, rows, cols, rand=True):
self.rows = rows
self.cols = cols
self.data = (2 * (np.random.rand(rows, cols) - 0.5)) if rand else np.zeros(shape=(rows, cols))
def transpose(self):
result = Matrix(self.cols, self.rows, rand=False)
result.data = self.data.transpose()
return result
def __str__(self):
s = ''
for r in self.data:
s += str(r) + '\n'
return s
def __add__(self, b):
if self.rows == b.rows and self.cols == b.cols:
result = Matrix(self.rows, self.cols, rand=False)
result.data = np.add(self.data, b.data)
return result
else:
raise Exception('Matrixes A and B are not of the same size.')
def __sub__(self, b):
if self.rows == b.rows and self.cols == b.cols:
result = Matrix(self.rows, self.cols, rand=False)
result.data = np.subtract(self.data, b.data)
return result
else:
if b.rows == 1 and b.cols == 1:
result = Matrix(self.rows, self.cols, rand=False)
result.data = np.subtract(self.data, b.data)
return result
else:
raise Exception('Matrixes A and B are not of the same size.')
def __mul__(self, b):
if type(b) in supported_constants:
return self.__rmul__(b)
elif self.cols == b.rows:
return _multiply(self, b)
else:
raise Exception('Columns of matrix A does not match rows matrix B.')
def __rmul__(self, const):
if type(const) in supported_constants:
result = Matrix(self.rows, self.cols, rand=False)
result.data = self.data * const
return result
else:
raise Exception('Can not multiply int and matrix together.')
def __floordiv__(self, b):
if type(b) in supported_constants:
return self._div_(b)
else:
raise Exception('Operation not supported.')
def __truediv__(self, b):
if type(b) in supported_constants:
return self._div_(b)
else:
raise Exception('Operation not supported.')
def _div_(self, const):
if type(const) in supported_constants:
result = Matrix(self.rows, self.cols, rand=False)
result.data = self.data / const
return result
else:
raise Exception('Can not divide int and matrix.')
def __pow__(self, power):
result = Matrix(self.rows, self.cols, rand=False)
result.data = self.data
for i in range(1, power):
result = _multiply(result, self)
return result
def __getitem__(self, k):
return self.data[k]
def __setitem__(self, k, v):
self.data[k] = v
def __delitem__(self, k):
del self.data[k]
def _multiply(a, b):
result = Matrix(a.rows, b.cols, rand=False)
result.data = np.matmul(a.data, b.data)
return result
if __name__ == '__main__':
print('m1')
m1 = Matrix(2, 2)
print(m1)
print('m2')
m2 = Matrix(2, 1)
print(m2)
print('m1 * m2')
print(m1 * m2)
try:
print('m2 * m1')
print(m2 * m1)
except Exception as e:
print('Error: ', e, end='\n\n')
print('m1 + m1')
print(m1 + m1)
print(m1)
print(m1**3)
print(m1)
print(2.0 * m1)