-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
85 lines (72 loc) · 2.82 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
import random
class Matrix:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.data = [[0.0 for _ in range(cols)] for _ in range(rows)]
@classmethod
def zeroes(cls, rows, cols):
return cls(rows, cols)
@classmethod
def random(cls, rows, cols):
res = cls.zeroes(rows, cols)
for i in range(rows):
for j in range(cols):
res.data[i][j] = random.uniform(-1.0, 1.0)
return res
@classmethod
def from_list(cls, data):
rows = len(data)
cols = len(data[0])
matrix = cls(rows, cols)
matrix.data = data
return matrix
def multiply(self, other):
if self.cols != other.rows:
raise ValueError("Attempted to multiply two matrices with incorrect dimensions")
res = Matrix.zeroes(self.rows, other.cols)
for i in range(self.rows):
for j in range(other.cols):
sum = 0.0
for k in range(self.cols):
sum += self.data[i][k] * other.data[k][j]
res.data[i][j] = sum
return res
def add(self, other):
if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Attempted to add matrices with incorrect dimensions")
res = Matrix.zeroes(self.rows, self.cols)
for i in range(self.rows):
for j in range(self.cols):
res.data[i][j] = self.data[i][j] + other.data[i][j]
return res
def dot_multiply(self, other):
if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Attempted to dot multiply matrices with incorrect dimensions")
res = Matrix.zeroes(self.rows, self.cols)
for i in range(self.rows):
for j in range(self.cols):
res.data[i][j] = self.data[i][j] * other.data[i][j]
return res
def subtract(self, other):
if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Attempted to subtract matrices with incorrect dimensions")
res = Matrix.zeroes(self.rows, self.cols)
for i in range(self.rows):
for j in range(self.cols):
res.data[i][j] = self.data[i][j] - other.data[i][j]
return res
def map(self, function):
return Matrix.from_list([[function(value) for value in row] for row in self.data])
def transpose(self):
res = Matrix.zeroes(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
res.data[j][i] = self.data[i][j]
return res
def __str__(self):
return "Matrix {{\n{}\n}}".format(
"\n".join([" " + " ".join(map(str, row)) for row in self.data])
)
def __repr__(self):
return self.__str__()