forked from TUD-RST/symbtools
/
mathe_smith_form.py
325 lines (233 loc) · 7.5 KB
/
mathe_smith_form.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# coding: utf-8
# In[2]:
import sympy as sp
import symbtools as st
from ipydex import IPS
# Hier versuche ich den Wikipedia-Algorithmus zur Bestimmung der Smith-Normalform zu implementieren.
# http://en.wikipedia.org/wiki/Smith_normal_form
#
"""
Aktueller Stand: Schritte 1-3 sind umgesetzt. Schritt 4 (Normierung) fehlt noch
Weiteres Vorgehen:
* Normierung,
* Umgang mit Rechteckigen Matrizen,
* Erweiterung auf andere algebraische Strukturen als Polynome
"""
# Beispliel von Prof. Röbenack:
m0, m1, l, g, s= sp.symbols('m0, m1, l, g, s')
M = sp.Matrix([(m0+m1)*s**2,-l*m1*s**2,-1,0,-l*m1*s**2,l*m1*(l*s**2-g),0,-1]).reshape(2,4)
M
# In[11]:
def first_nonzero_element(seq):
for i, elt in enumerate(seq):
if not elt == 0:
return i, elt
raise ValueError("sequence must not vanish identically")
# In[23]:
def coeff_list_to_poly(coeffs, var):
res = sp.sympify(0)
for i, c in enumerate(coeffs):
res += c*var**i
return res
def solve_bezout_eq(p1, p2, var):
"""
solving the bezout equation
c1*p1 + c2*p2 = 1
for monovariate polynomials p1, p2
by ansatz-polynomials and equating
coefficients
"""
g1 = st.poly_degree(p1, var)
g2 = st.poly_degree(p2, var)
if (not sp.gcd(p1, p2) == 1) and (not p1*p2==0):
# pass
errmsg = "p1, p2 need to be coprime "\
"(condition for Bezout identity to be solveble).\n"\
"p1 = {p1}\n"\
"p2 = {p2}"
raise ValueError(errmsg.format(p1=p1, p2=p2))
if p1 == p2 == 0:
raise ValueError("invalid: p1==p2==0")
if p1 == 0 and g2 > 0:
raise ValueError("invalid: p1==0 and not p2==const ")
if p2 == 0 and g1 > 0:
raise ValueError("invalid: p2==0 and not p1==const ")
# Note: degree(0) = -sp.oo = -inf
k1 = g2 - 1
k2 = g1 - 1
if k1<0 and k2 <0:
if p1 == 0:
k2 = 0
else:
k1 = 0
if k1 == -sp.oo:
k1 = -1
if k2 == -sp.oo:
k2 = -1
cc1 = sp.symbols("c1_0:%i" % (k1 + 1))
cc2 = sp.symbols("c2_0:%i" % (k2 + 1))
c1 = coeff_list_to_poly(cc1, var)
c2 = coeff_list_to_poly(cc2, var)
# Bezout equation:
eq = c1*p1 + c2*p2 - 1
# solve it w.r.t. the unknown coeffs
sol = sp.solve(eq, cc1+cc2, dict=True)
if len(sol) == 0:
errmsg = "No solution found.\n"\
"p1 = {p1}\n"\
"p2 = {p2}"
raise ValueError(errmsg.format(p1=p1, p2=p2))
sol = sol[0]
sol_symbols = st.atoms(sp.Matrix(sol.values()), sp.Symbol)
# there might be some superflous coeffs
free_c_symbols = set(cc1+cc2).intersection(sol_symbols)
if free_c_symbols:
# set them to zero
fcs0 = st.zip0(free_c_symbols)
keys, values = zip(*sol.items())
new_values = [v.subs(fcs0) for v in values]
sol = dict(zip(keys, new_values)+fcs0)
return c1.subs(sol), c2.subs(sol)
def smith_column_step(col, t, var):
nr = len(col)
L0 = sp.eye(nr)
col = col.expand()
at = col[t]
for k, ak in enumerate(col):
if k == t or ak == 0:
continue
GCD = sp.gcd(at, ak)
alpha_t = sp.simplify(at/GCD)
gamma_k = sp.simplify(ak/GCD)
sigma, tau = solve_bezout_eq(alpha_t, gamma_k, var)
L0[t, t] = sigma
L0[t, k] = tau
L0[k, t] = -gamma_k
L0[k, k] = alpha_t
new_col = sp.expand(L0*col)
# Linksmultiplikation der Spalte mit L0 liefert eine neue Spalte
# mit Einträgen beta bei t und 0 bei k
break
return new_col, L0
def row_swap(n, i1, i2):
row_op = sp.eye(n)
tmp1 = row_op[i1, :]
tmp2 = row_op[i2, :]
row_op[i2, :] = tmp1
row_op[i1, :] = tmp2
return row_op
def row_op(n, i1, i2, c1, c2):
"""
new <row i1> is <old i1>*c1 + <old i2>*c2
"""
assert not c1 == 0
row_op = sp.eye(n)
row_op[i1, i1] = c1
row_op[i1, i2] = c2
return row_op
def smith_step(A, t, var):
# erste Spalte (Index: j), die nicht komplett 0 ist
# j soll größer als Schrittzähler sein
nr, nc = A.shape
row_op_list = []
cols = st.col_split(A)
# erste nicht verschwindende Spalte finden
for j, c in enumerate(cols):
if j < t:
continue
if not c == c*0:
break
# Eintrag mit Index t soll ungleich 0 sein, ggf. Zeilen tauschen
if c[t] == 0:
i, elt = first_nonzero_element(c)
ro = row_swap(nr, t, i)
c = ro*c
row_op_list.append(ro)
col = c.expand()
while True:
new_col, L0 = smith_column_step(col, t, var)
if L0 == sp.eye(nr):
# nothing has changed
break
row_op_list.append(L0)
col = new_col
# jetzt teilt col[t] alle Einträge in col
# Probe in der nächsten Schleife
col.simplify()
col = col.expand()
for i,a in enumerate(col):
if i == t:
continue
if not sp.simplify(sp.gcd(a, col[t]) - col[t]) == 0:
IPS()
raise ValueError, "col[t] should divide all entries in col"
quotient = sp.simplify(a/col[t])
if a == 0:
continue
# eliminiere a
ro = row_op(nr, i, t, -1, quotient)
row_op_list.append(ro)
return row_op_list
def smith_form(A, var):
nr, nc = A.shape
row_op_list = []
total_ro = sp.eye(nr)
col_op_list = []
total_co = sp.eye(nc)
A_tmp = A*1
for t in xrange(nr):
while True:
print "t=", t
step_ro = sp.eye(nr)
step_co = sp.eye(nc)
new_ro_list = smith_step(A_tmp, t, var)
for ro in new_ro_list:
A_tmp = ro*A_tmp
A_tmp.simplify()
total_ro = ro*total_ro
step_ro = ro*step_ro
row_op_list.extend(new_ro_list)
# after processing column t, now process row t
new_co_list = smith_step(A_tmp.T, t, var)
# Transpose the column operations
new_co_list = [co.T for co in new_co_list]
for co in new_co_list:
A_tmp = A_tmp*co
A_tmp.simplify()
total_co = total_co*co
step_co = step_co*co
if step_ro == sp.eye(nr) and step_co == sp.eye(nc):
break
print "fertig mit t=", t
total_ro.simplify()
total_co.simplify()
return total_ro.expand(), total_co.expand()
if __name__ == "__main__":
#solve_bezout_eq(0, 3, s)
#smith_step(M, 0, s)
if 0:
M1 = sp.Matrix([[s-2, 0, 0, 0],
[1, s-1, 0, 0],
[0, 1, s , 1],
[-1, -1, -1, s-2]])
ro = smith_form(M1, s)
M1_1 = sp.simplify(ro*M1)
IPS()
# - - - -
M1_1 = sp.Matrix([
[s - 2, 0, 0, 0],
[ 1, s - 1, 0, 0],
[ 0, 0, s*(s - 2) + 1, 0],
[ 0, 1, s, 1]])
ro2, co = smith_form(M1_1.T, s)
M1_2 = sp.simplify(ro2*M1_1).T
ro3 = smith_form(M1_2, s)
# M = sp.Matrix([
# [s - 2, -(s - 2)*(s - 1) + 1, s*((s - 2)*(s - 1) - 1), s**4 - 5*s**3 + 8*s**2 - 5*s + 1],
# [ 0, s - 1, s*(-s + 1), -(s - 1)*(s**2 - 2*s + 1) + 1],
# [ 0, 0, s*(s - 2) + 1, s + (s - 2)*(s*(s - 2) + 1)],
# [ 0, 0, 0, 1]])
#
# M.simplify()
# ro, co = smith_form(M, s)
IPS()