-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
64 lines (45 loc) · 1.35 KB
/
Utils.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
import sys
def print_matrix(Title, M):
print(Title)
for row in M:
print([round(x, 3) + 0 for x in row])
def print_matrices(Action, M1, M2, markov_terms, to_remove_idx=None):
print(Action)
# print(Title1, '\t' * int(len(M1) / 2) + "\t" * len(M1), Title2)
for i in range(len(M1)):
if i == to_remove_idx:
continue
row1 = " + ".join(['{0:-.3f}'.format(x[0]) + " * " + str(x[1]) for x in zip(M1[i], markov_terms)])
row2 = "".join(['{0:.3f}'.format(x) for x in M2[i]])
print(row1, '=', row2)
def zeros_matrix(rows, cols):
A = []
for i in range(rows):
A.append([])
for j in range(cols):
A[-1].append(0.0)
return A
def copy_matrix(M):
rows = len(M)
cols = len(M[0])
MC = zeros_matrix(rows, cols)
for i in range(rows):
for j in range(cols):
MC[i][j] = M[i][j]
return MC
def matrix_multiply(A, B):
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if colsA != rowsB:
print('Number of A columns must equal number of B rows.')
sys.exit()
C = zeros_matrix(rowsA, colsB)
for i in range(rowsA):
for j in range(colsB):
total = 0
for ii in range(colsA):
total += A[i][ii] * B[ii][j]
C[i][j] = total
return C