-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrices.py
66 lines (55 loc) · 1.62 KB
/
matrices.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
import copy as c
def multiply(a, b):
if a.cols != b.rows:
print "A Columns does not equal B Columns"
return None
result = Matrix(a.rows, b.cols)
for r in range(result.rows):
for c in range(result.cols):
sum = 0.0
for n in range(a.cols):
sum += a.values[r][n]*b.values[n][c]
result.values[r][c] = sum
return result
def fromVector(vector):
r = len(vector)
c = 1
m = Matrix(r, c)
for i in range(r):
m.values[i] = [vector[i]]
return m
def add(a, b):
output = Matrix(a.rows, a.cols)
for r in range(output.rows):
for c in range(output.cols):
output.values[r][c] = a.values[r][c] + b.values[r][c]
return output
class Matrix:
def __init__(self, rows, cols, rand = False):
self.rows = rows
self.cols = cols
self.values = []
for r in range(self.rows):
col = []
for c in range(self.cols):
col.append(0)
self.values.append(col)
if rand:
self.randomize()
def printOut(self):
for r in self.values:
print r
def randomize(self):
for r in range(self.rows):
for c in range(self.cols):
self.values[r][c] = random(1)*2-1
def mapOver(self, f):
for r in range(self.rows):
for c in range(self.cols):
self.values[r][c] = f(self.values[r][c])
def determinant(self):
pass
def toVector(self):
return [x[0] for x in self.values]
def createCopy(self):
return c.deepcopy(self)