File: test_arpack.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (355 lines) | stat: -rw-r--r-- 11,152 bytes parent folder | download
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
#!/usr/bin/env python
__usage__ = """
First ensure that scipy core modules are installed.

Build interface to arpack 
  python setup.py build
Run tests locally:
  python tests/test_arpack.py [-l<int>] [-v<int>]

"""

import sys
from numpy.testing import *

set_package_path()
from arpack import *
del sys.path[0]

import numpy 
from scipy.linalg import eig,eigh,norm

class test_eigen_nonsymmetric(ScipyTestCase):

    def get_a1(self,typ):
        mat=numpy.array([[-2., -8.,  1.,  2., -5.],
                         [ 6.,  6.,  0.,  2.,  1.],
                         [ 0.,  4., -2., 11.,  0.],
                         [ 1.,  6.,  1.,  0., -4.],
                         [ 2., -6.,  4.,  9., -3]],typ)

        w=numpy.array([-2.21691+8.59661*1j,-2.21691-8.59661*1j,\
                       4.45961+3.80078*1j, 4.45961-3.80078*1j,\
                       -5.48541+0j],typ.upper())
        return mat,w


    def large_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.abs(aw)
        num=numpy.abs(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num[-k:],exact[-k:],decimal=5)

    def small_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.abs(aw)
        num=numpy.abs(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num[:k],exact[:k],decimal=5)


    def large_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.real(aw)
        num=numpy.real(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num[-k:],exact[-k:],decimal=5)

    def small_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.real(aw)
        num=numpy.real(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num[:k],exact[:k],decimal=5)


    def large_imag(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LI')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        print w
        print aw
        exact=numpy.imag(aw)
        num=numpy.imag(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num[-k:],exact[-k:],decimal=5)

    def small_imag(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SI')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.imag(aw)
        num=numpy.imag(w)
        exact.sort()
        num.sort()
        print num
        assert_array_almost_equal(num[:k],exact[:k],decimal=5)


    def check_type(self):
        k=2
        for typ in 'fd':
            self.large_magnitude(typ,k)
            self.small_magnitude(typ,k)
            self.large_real(typ,k)
            self.small_real(typ,k)
# Maybe my understanding of small imaginary and large imaginary
# isn't too keen.  I don't understand why these return
# different answers than in the complex case (the latter seems correct)
#            self.large_imag(typ,k)
#            self.small_imag(typ,k)



class test_eigen_complex_nonsymmetric(ScipyTestCase):

    def get_a1(self,typ):
        mat=numpy.array([[-2., -8.,  1.,  2., -5.],
                         [ 6.,  6.,  0.,  2.,  1.],
                         [ 0.,  4., -2., 11.,  0.],
                         [ 1.,  6.,  1.,  0., -4.],
                         [ 2., -6.,  4.,  9., -3]],typ)

        w=numpy.array([-2.21691+8.59661*1j,-2.21691-8.59661*1j,\
                       4.45961+3.80078*1j, 4.45961-3.80078*1j,\
                       -5.48541+0j],typ.upper())
        return mat,w


    def large_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.abs(aw)
        num=numpy.abs(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[-k:],decimal=5)

    def small_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.abs(aw)
        num=numpy.abs(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[:k],decimal=5)


    def large_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.real(aw)
        num=numpy.real(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[-k:],decimal=5)

    def small_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.real(aw)
        num=numpy.real(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[:k],decimal=5)


    def large_imag(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LI')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.imag(aw)
        num=numpy.imag(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[-k:],decimal=5)

    def small_imag(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SI')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        exact=numpy.imag(aw)
        num=numpy.imag(w)
        exact.sort()
        num.sort()
        assert_array_almost_equal(num,exact[:k],decimal=5)


    def check_type(self):
        k=2
        for typ in 'FD':
            self.large_magnitude(typ,k)
            self.small_magnitude(typ,k)
            self.large_real(typ,k)
            self.small_real(typ,k)
            self.large_imag(typ,k)
            self.small_imag(typ,k)



    
class test_eigen_symmetric(ScipyTestCase):

    def get_a1(self,typ):
        mat_a1=numpy.array([[ 2.,  0.,  0., -1.,  0., -1.],
                            [ 0.,  2.,  0., -1.,  0., -1.],
                            [ 0.,  0.,  2., -1.,  0., -1.],
                            [-1., -1., -1.,  4.,  0., -1.],
                            [ 0.,  0.,  0.,  0.,  1., -1.],
                            [-1., -1., -1., -1., -1.,  5.]],
                           typ)
        w = [0,1,2,2,5,6] # eigenvalues of a1
        return mat_a1,w

    def large_eigenvalues(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='LM',tol=1e-7)
        assert_array_almost_equal(w,aw[-k:])

    def small_eigenvalues(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='SM')
        assert_array_almost_equal(w,aw[:k])

    def end_eigenvalues(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='BE')
        exact=[aw[0],aw[-1]]
        assert_array_almost_equal(w,exact)

    def large_eigenvectors(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='LM')
        ew,ev = eigh(a)
        ind=ew.argsort()
        assert_array_almost_equal(w,numpy.take(ew,ind[-k:]))
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])

    def small_eigenvectors(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='SM',tol=1e-7)
        ew,ev = eigh(a)
        ind=ew.argsort()
        assert_array_almost_equal(w,numpy.take(ew,ind[:k]))
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])

    def end_eigenvectors(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen_symmetric(a,k,which='BE')
        ew,ev = eigh(a)
        ind=ew.argsort()
        exact=numpy.concatenate(([ind[:k/2],ind[-k/2:]]))
        assert_array_almost_equal(w,numpy.take(ew,exact))
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])

    def check_eigenvectors(self):
        k=2
        for typ in 'fd':
            self.large_eigenvectors(typ,k)
            self.small_eigenvectors(typ,k)
            self.end_eigenvectors(typ,k)

    def check_type(self):
        k=2
        for typ in 'fd':
            self.large_eigenvalues(typ,k)
            self.small_eigenvalues(typ,k)
            self.end_eigenvalues(typ,k)
    

class test_eigen_complex_symmetric(ScipyTestCase):

    def get_a1(self,typ):
        mat_a1=numpy.array([[ 2.,  0.,  0., -1.,  0., -1.],
                            [ 0.,  2.,  0., -1.,  0., -1.],
                            [ 0.,  0.,  2., -1.,  0., -1.],
                            [-1., -1., -1.,  4.,  0., -1.],
                            [ 0.,  0.,  0.,  0.,  1., -1.],
                            [-1., -1., -1., -1., -1.,  5.]],
                           typ)
        w = numpy.array([0+0j,1+0j,2+0j,2+0j,5+0j,6+0j]) # eigenvalues of a1
        return mat_a1,w

    def large_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])
        aw.real.sort()
        w.real.sort()
        assert_array_almost_equal(w,aw[-k:])


    def small_magnitude(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SM')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])
        aw.real.sort()
        w.real.sort()
        assert_array_almost_equal(w,aw[:k])

    def large_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='LR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i],decimal=5)
        aw.real.sort()
        w.real.sort()
        assert_array_almost_equal(w,aw[-k:],decimal=5)


    def small_real(self,typ,k):
        a,aw = self.get_a1(typ)
        w,v = eigen(a,k,which='SR')
        for i in range(k):
            assert_array_almost_equal(sb.dot(a,v[:,i]),w[i]*v[:,i])
        aw.real.sort()
        w.real.sort()
        assert_array_almost_equal(w,aw[:k])

    def check_complex_symmetric(self):
        k=2
        for typ in 'FD':
            self.large_magnitude(typ,k)
            self.small_magnitude(typ,k)
            self.large_real(typ,k)
            self.small_real(typ,k)



if __name__ == "__main__":
    ScipyTest().run()