-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
svd.py
363 lines (343 loc) · 10.7 KB
/
svd.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# Almost exact translation of the ALGOL SVD algorithm published in
# Numer. Math. 14, 403-420 (1970) by G. H. Golub and C. Reinsch
#
# Copyright (c) 2005 by Thomas R. Metcalf, helicity314-stitch <at> yahoo <dot> com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Pure Python SVD algorithm.
# Input: 2-D list (m by n) with m >= n
# Output: U,W V so that A = U*W*VT
# Note this program returns V not VT (=transpose(V))
# On error, a ValueError is raised.
#
# Here is the test case (first example) from Golub and Reinsch
#
# a = [[22.,10., 2., 3., 7.],
# [14., 7.,10., 0., 8.],
# [-1.,13.,-1.,-11., 3.],
# [-3.,-2.,13., -2., 4.],
# [ 9., 8., 1., -2., 4.],
# [ 9., 1.,-7., 5.,-1.],
# [ 2.,-6., 6., 5., 1.],
# [ 4., 5., 0., -2., 2.]]
#
# import svd
# import math
# u,w,vt = svd.svd(a)
# print w
#
# [35.327043465311384, 1.2982256062667619e-15,
# 19.999999999999996, 19.595917942265423, 0.0]
#
# the correct answer is (the order may vary)
#
# print (math.sqrt(1248.),20.,math.sqrt(384.),0.,0.)
#
# (35.327043465311391, 20.0, 19.595917942265423, 0.0, 0.0)
#
# transpose and matrix multiplication functions are also included
# to facilitate the solution of linear systems.
#
# Version 1.0 2005 May 01
import copy
import math
def svd(a):
'''Compute the singular value decomposition of array.'''
# Golub and Reinsch state that eps should not be smaller than the
# machine precision, ie the smallest number
# for which 1+e>1. tol should be beta/e where beta is the smallest
# positive number representable in the computer.
eps = 1.e-15 # assumes double precision
tol = 1.e-64/eps
assert 1.0+eps > 1.0 # if this fails, make eps bigger
assert tol > 0.0 # if this fails, make tol bigger
itmax = 50
u = copy.deepcopy(a)
m = len(a)
n = len(a[0])
#if __debug__: print 'a is ',m,' by ',n
if m < n:
if __debug__: print 'Error: m is less than n'
raise ValueError,'SVD Error: m is less than n.'
e = [0.0]*n # allocate arrays
q = [0.0]*n
v = []
for k in range(n): v.append([0.0]*n)
# Householder's reduction to bidiagonal form
g = 0.0
x = 0.0
for i in range(n):
e[i] = g
s = 0.0
l = i+1
for j in range(i,m): s += (u[j][i]*u[j][i])
print s
if s <= tol:
g = 0.0
else:
f = u[i][i]
if f < 0.0:
g = math.sqrt(s)
else:
g = -math.sqrt(s)
h = f*g-s
u[i][i] = f-g
for j in range(l,n):
s = 0.0
for k in range(i,m): s += u[k][i]*u[k][j]
f = s/h
for k in range(i,m): u[k][j] = u[k][j] + f*u[k][i]
q[i] = g
s = 0.0
for j in range(l,n): s = s + u[i][j]*u[i][j]
if s <= tol:
g = 0.0
else:
f = u[i][i+1]
if f < 0.0:
g = math.sqrt(s)
else:
g = -math.sqrt(s)
h = f*g - s
u[i][i+1] = f-g
for j in range(l,n): e[j] = u[i][j]/h
for j in range(l,m):
s=0.0
for k in range(l,n): s = s+(u[j][k]*u[i][k])
for k in range(l,n): u[j][k] = u[j][k]+(s*e[k])
y = abs(q[i])+abs(e[i])
if y>x: x=y
print e
print q
print u
print v
print "==="
# accumulation of right hand gtransformations
for i in range(n-1,-1,-1):
if g != 0.0:
h = g*u[i][i+1]
for j in range(l,n): v[j][i] = u[i][j]/h
for j in range(l,n):
s=0.0
for k in range(l,n): s += (u[i][k]*v[k][j])
for k in range(l,n): v[k][j] += (s*v[k][i])
for j in range(l,n):
v[i][j] = 0.0
v[j][i] = 0.0
v[i][i] = 1.0
g = e[i]
l = i
#accumulation of left hand transformations
for i in range(n-1,-1,-1):
l = i+1
g = q[i]
for j in range(l,n): u[i][j] = 0.0
if g != 0.0:
h = u[i][i]*g
for j in range(l,n):
s=0.0
for k in range(l,m): s += (u[k][i]*u[k][j])
f = s/h
for k in range(i,m): u[k][j] += (f*u[k][i])
for j in range(i,m): u[j][i] = u[j][i]/g
else:
for j in range(i,m): u[j][i] = 0.0
u[i][i] += 1.0
#diagonalization of the bidiagonal form
eps = eps*x
for k in range(n-1,-1,-1):
for iteration in range(itmax):
# test f splitting
for l in range(k,-1,-1):
goto_test_f_convergence = False
if abs(e[l]) <= eps:
# goto test f convergence
goto_test_f_convergence = True
break # break out of l loop
if abs(q[l-1]) <= eps:
# goto cancellation
break # break out of l loop
if not goto_test_f_convergence:
#cancellation of e[l] if l>0
c = 0.0
s = 1.0
l1 = l-1
for i in range(l,k+1):
f = s*e[i]
e[i] = c*e[i]
if abs(f) <= eps:
#goto test f convergence
break
g = q[i]
h = pythag(f,g)
q[i] = h
c = g/h
s = -f/h
for j in range(m):
y = u[j][l1]
z = u[j][i]
u[j][l1] = y*c+z*s
u[j][i] = -y*s+z*c
# test f convergence
z = q[k]
if l == k:
# convergence
if z<0.0:
#q[k] is made non-negative
q[k] = -z
for j in range(n):
v[j][k] = -v[j][k]
break # break out of iteration loop and move on to next k value
if iteration >= itmax-1:
if __debug__: print 'Error: no convergence.'
# should this move on the the next k or exit with error??
#raise ValueError,'SVD Error: No convergence.' # exit the program with error
break # break out of iteration loop and move on to next k
# shift from bottom 2x2 minor
x = q[l]
y = q[k-1]
g = e[k-1]
h = e[k]
f = ((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y)
g = pythag(f,1.0)
if f < 0:
f = ((x-z)*(x+z)+h*(y/(f-g)-h))/x
else:
f = ((x-z)*(x+z)+h*(y/(f+g)-h))/x
# next QR transformation
c = 1.0
s = 1.0
for i in range(l+1,k+1):
g = e[i]
y = q[i]
h = s*g
g = c*g
z = pythag(f,h)
e[i-1] = z
c = f/z
s = h/z
f = x*c+g*s
g = -x*s+g*c
h = y*s
y = y*c
for j in range(n):
x = v[j][i-1]
z = v[j][i]
v[j][i-1] = x*c+z*s
v[j][i] = -x*s+z*c
z = pythag(f,h)
q[i-1] = z
c = f/z
s = h/z
f = c*g+s*y
x = -s*g+c*y
for j in range(m):
y = u[j][i-1]
z = u[j][i]
u[j][i-1] = y*c+z*s
u[j][i] = -y*s+z*c
e[l] = 0.0
e[k] = f
q[k] = x
# goto test f splitting
#vt = transpose(v)
#return (u,q,vt)
return (u,q,v)
def pythag(a,b):
absa = abs(a)
absb = abs(b)
if absa > absb: return absa*math.sqrt(1.0+(absb/absa)**2)
else:
if absb == 0.0: return 0.0
else: return absb*math.sqrt(1.0+(absa/absb)**2)
def transpose(a):
'''Compute the transpose of a matrix.'''
m = len(a)
n = len(a[0])
at = []
for i in range(n): at.append([0.0]*m)
for i in range(m):
for j in range(n):
at[j][i]=a[i][j]
return at
def matrixmultiply(a,b):
'''Multiply two matrices.
a must be two dimensional
b can be one or two dimensional.'''
am = len(a)
bm = len(b)
an = len(a[0])
try:
bn = len(b[0])
except TypeError:
bn = 1
if an != bm:
raise ValueError, 'matrixmultiply error: array sizes do not match.'
cm = am
cn = bn
if bn == 1:
c = [0.0]*cm
else:
c = []
for k in range(cm): c.append([0.0]*cn)
for i in range(cm):
for j in range(cn):
for k in range(an):
if bn == 1:
c[i] += a[i][k]*b[k]
else:
c[i][j] += a[i][k]*b[k][j]
return c
#a = [[22.,10., 2., 3., 7.],
# [14., 7.,10., 0., 8.],
# [-1.,13.,-1.,-11., 3.],
# [-3.,-2.,13., -2., 4.],
# [ 9., 8., 1., -2., 4.],
# [ 9., 1.,-7., 5.,-1.],
# [ 2.,-6., 6., 5., 1.],
# [ 4., 5., 0., -2., 2.]]
a = [[3.,0.],
[4.,5.]]
#
# import svd
# import math
u,w,vt = svd(a)
print(w)
#
# [35.327043465311384, 1.2982256062667619e-15,
# 19.999999999999996, 19.595917942265423, 0.0]
#
# the correct answer is (the order may vary)
#
#print (math.sqrt(1248.),20.,math.sqrt(384.),0.,0.)
#
# (35.327043465311391, 20.0, 19.595917942265423, 0.0, 0.0)
print u
print vt
# C:\Program Files (x86)\Phix>py27 svd.py
# [2.23606797749979, 6.708203932499368]
# [[0.9486832980505138, 0.316227766016838], [-0.3162277660168379, 0.9486832980505138]]
# [[0.7071067811865475, 0.7071067811865475], [-0.7071067811865475, 0.7071067811865475]]
# Sample Output
#
# 0.31622776601683794 -0.9486832980505138
# 0.9486832980505138 0.31622776601683794
#
# 6.708203932499369 0
# 0 2.23606797749979
#
# 0.7071067811865475 -0.7071067811865475
# 0.7071067811865475 0.7071067811865475