-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstage3 - 1.py
More file actions
240 lines (206 loc) · 6.09 KB
/
stage3 - 1.py
File metadata and controls
240 lines (206 loc) · 6.09 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import fractions
# m = [[0, 2, 1, 0, 0], [0, 0, 0, 3, 4], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
m = [[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
# m = [[1]]
# m = [[1, 2, 3, 0, 0, 0], [4, 5, 6, 0, 0, 0], [7, 8, 9, 1, 0, 0], [0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
# m = [
# [0, 7, 0, 17, 0, 1, 0, 5, 0, 2],
# [0, 0, 29, 0, 28, 0, 3, 0, 16, 0],
# [0, 3, 0, 0, 0, 1, 0, 0, 0, 0],
# [48, 0, 3, 0, 0, 0, 17, 0, 0, 0],
# [0, 6, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# ]
def answer(m):
if len(m) == 1:
return [1, 1]
R = []
Q = []
I = []
O = []
F = []
#identify absorbing states and the order that the states
#will be in when translated into standard form
absorbing = []
transition = []
absorbingStates = []
transitionStates = []
for stateNum, state in enumerate(m):
if sum(state) == 0:
absorbing.append(state)
absorbingStates.append(stateNum)
else:
transition.append(state)
transitionStates.append(stateNum)
order = absorbingStates + transitionStates
#reorder each transition state's values according to the
#state order and change numbers to probabilities
for stateNum, state in enumerate(transition):
tmpState = state[:]
for valNum, val in enumerate(state):
tmpState[valNum] = float(state[order[valNum]]) / float(sum(state))
transition[stateNum] = tmpState[:]
#get matrix R
RIndex = []
for i in range(0, len(absorbing)):
RIndex.append(transition[stateNum][i])
R.append(RIndex)
#get matrix Q
QIndex = []
for i in range(len(absorbing), len(m)):
QIndex.append(transition[stateNum][i])
Q.append(QIndex)
#add a 1 to each absorbing state to make it an identity
#matrix
for stateNum, state in enumerate(absorbing):
state[stateNum] = 1
#concatenate the lists so that the absorbing states
#come before the transition states
standard = absorbing + transition
#find F matrix
ICount = 0;
for rowIdx, row in enumerate(Q):
IIndex = []
for valIdx, val in enumerate(row):
if rowIdx == ICount and valIdx == ICount:
IIndex.append(1)
else:
IIndex.append(0)
I.append(IIndex)
ICount += 1
F = getMatrixInverse(subtractMatrices(I, Q))
FR = multiplyMatrices(F, R)
# generate numerators
numerators = []
for i in FR[0]:
numerator = fractions.Fraction(i).limit_denominator().numerator
numerators.append(numerator)
# generate denominators
denominators = []
for i in FR[0]:
denominator = fractions.Fraction(i).limit_denominator().denominator
denominators.append(denominator)
lcd = 1
for val in denominators:
lcd = getLcm(lcd, val)
# print lcd
for idx, num in enumerate(numerators):
numerators[idx] = num * (lcd / denominators[idx])
# numerators[:] = (value for value in numerators if value != 0)
final = []
for idx, val in enumerate(absorbing):
final.append(numerators[idx])
final.append(lcd)
# print reducefract(Fraction(FR[0][1]).numerator, Fraction(FR[0][1]).denominator)
# for row in FR:
# for val in row:
# print val.as_integer_ratio()
# print numerators
# print denominators
# for row in FR:
# print row
return final
def reducefract(n, d):
'''Reduces fractions. n is the numerator and d the denominator.'''
def gcd(n, d):
while d != 0:
t = d
d = n%d
n = t
return n
assert d!=0, "integer division by zero"
assert isinstance(d, int), "must be int"
assert isinstance(n, int), "must be int"
greatest=gcd(n,d)
n/=greatest
d/=greatest
return n, d
def multiplyMatrices(a, b):
if len(a[0]) > len(b[0]):
rowNum = len(a[0])
else:
rowNum = len(b[0])
if len(a) > len(b):
colNum = len(a)
else:
colNum = len(b)
output = [[0 for k in xrange(rowNum)] for j in xrange(colNum)]
# iterate through rows of a
for i in range(len(a)):
# iterate through columns of b
for j in range(len(b[0])):
# iterate through rows of b
for k in range(len(b)):
output[i][j] += a[i][k] * b[k][j]
return output
def subtractMatrices(a, b):
output = []
for rowIdx, row in enumerate(a):
outputRow = []
for valIdx, val in enumerate(row):
outputRow.append(val - b[rowIdx][valIdx])
output.append(outputRow)
return output
def transposeMatrix(m):
t = []
for r in range(len(m)):
tRow = []
for c in range(len(m[r])):
if c == r:
tRow.append(m[r][c])
else:
tRow.append(m[c][r])
t.append(tRow)
return t
def getMatrixMinor(m,i,j):
return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]
def getMatrixDeternminant(m):
#base case for 2x2 matrix
if len(m) == 2:
return m[0][0]*m[1][1]-m[0][1]*m[1][0]
determinant = 0
for c in range(len(m)):
determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c))
return determinant
def getMatrixInverse(m):
determinant = getMatrixDeternminant(m)
#special case for 2x2 matrix:
if len(m) == 2:
return [[m[1][1]/determinant, -1*m[0][1]/determinant],
[-1*m[1][0]/determinant, m[0][0]/determinant]]
#find matrix of cofactors
cofactors = []
for r in range(len(m)):
cofactorRow = []
for c in range(len(m)):
minor = getMatrixMinor(m,r,c)
cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor))
cofactors.append(cofactorRow)
cofactors = transposeMatrix(cofactors)
for r in range(len(cofactors)):
for c in range(len(cofactors)):
cofactors[r][c] = cofactors[r][c]/determinant
return cofactors
def getLcm(x, y):
"""This function takes two
integers and returns the L.C.M."""
if x == 0:
return y
if y == 0:
return x
# choose the greater number
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
print answer(m)