-
Notifications
You must be signed in to change notification settings - Fork 2
/
doomsday_fuel.py
124 lines (114 loc) · 3.7 KB
/
doomsday_fuel.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
from fractions import Fraction
from fractions import gcd
def simplify(x, y):
g = gcd(x, y)
return Fraction(long(x/g), long(y/g))
def lcm(x, y):
return long(x*y/gcd(x,y))
def transform(mat):
sum_list = list(map(sum, mat))
bool_indices = list(map(lambda x: x == 0, sum_list))
indices = set([i for i, x in enumerate(bool_indices) if x])
new_mat = []
for i in range(len(mat)):
new_mat.append(list(map(lambda x: Fraction(0, 1) if(sum_list[i] == 0) else simplify(x, sum_list[i]), mat[i])))
transform_mat = []
zeros_mat = []
for i in range(len(new_mat)):
if i not in indices:
transform_mat.append(new_mat[i])
else:
zeros_mat.append(new_mat[i])
transform_mat.extend(zeros_mat)
tmat = []
for i in range(len(transform_mat)):
tmat.append([])
extend_mat = []
for j in range(len(transform_mat)):
if j not in indices:
tmat[i].append(transform_mat[i][j])
else:
extend_mat.append(transform_mat[i][j])
tmat[i].extend(extend_mat)
return [tmat, len(zeros_mat)]
def copy_mat(mat):
cmat = []
for i in range(len(mat)):
cmat.append([])
for j in range(len(mat[i])):
cmat[i].append(Fraction(mat[i][j].numerator, mat[i][j].denominator))
return cmat
def gauss_elmination(m, values):
mat = copy_mat(m)
for i in range(len(mat)):
index = -1
for j in range(i, len(mat)):
if mat[j][i].numerator != 0:
index = j
break
if index == -1:
raise ValueError('Gauss elimination failed!')
mat[i], mat[index] = mat[index], mat[j]
values[i], values[index] = values[index], values[i]
for j in range(i+1, len(mat)):
if mat[j][i].numerator == 0:
continue
ratio = -mat[j][i]/mat[i][i]
for k in range(i, len(mat)):
mat[j][k] += ratio * mat[i][k]
values[j] += ratio * values[i]
res = [0 for i in range(len(mat))]
for i in range(len(mat)):
index = len(mat) -1 -i
end = len(mat) - 1
while end > index:
values[index] -= mat[index][end] * res[end]
end -= 1
res[index] = values[index]/mat[index][index]
return res
def transpose(mat):
tmat = []
for i in range(len(mat)):
for j in range(len(mat)):
if i == 0:
tmat.append([])
tmat[j].append(mat[i][j])
return tmat
def inverse(mat):
tmat = transpose(mat)
mat_inv = []
for i in range(len(tmat)):
values = [Fraction(int(i==j), 1) for j in range(len(mat))]
mat_inv.append(gauss_elmination(tmat, values))
return mat_inv
def mat_mult(mat1, mat2):
res = []
for i in range(len(mat1)):
res.append([])
for j in range(len(mat2[0])):
res[i].append(Fraction(0, 1))
for k in range(len(mat1[0])):
res[i][j] += mat1[i][k] * mat2[k][j]
return res
def splitQR(mat, lengthR):
lengthQ = len(mat) - lengthR
Q = []
R = []
for i in range(lengthQ):
Q.append([int(i==j)-mat[i][j] for j in range(lengthQ)])
R.append(mat[i][lengthQ:])
return [Q, R]
def solution(mat):
res = transform(mat)
if res[1] == len(mat):
return [1, 1]
Q, R = splitQR(*res)
inv = inverse(Q)
res = mat_mult(inv, R)
row = res[0]
l = 1
for item in row:
l = lcm(l, item.denominator)
res = list(map(lambda x: long(x.numerator*l/x.denominator), row))
res.append(l)
return res