File: dtest.py

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (260 lines) | stat: -rw-r--r-- 6,954 bytes parent folder | download | duplicates (3)
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
"""
>>> case(fft.fft( (0,1)*4 ),
...      Numeric.array([ 4.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
...                     -4.+0.j, 0.+0.j,  0.+0.j, 0.+0.j]))
OK

>>> case(fft.inverse_fft( fft.fft((0,1)*4)),
...      Numeric.array([ 0.+0.j,  1.+0.j,  0.+0.j,  1.+0.j,
...                      0.+0.j,  1.+0.j,  0.+0.j,  1.+0.j]))
OK

>>> case(fft.fft( (0,1)*4, n=16),
...      Numeric.array([
...     4.00000000e+00+0.j        ,  -1.11022302e-15-2.61312593j,
...     0.00000000e+00+0.j        ,  -1.55431223e-15-1.0823922j ,
...     0.00000000e+00+0.j        ,  -4.44089210e-16-1.0823922j ,
...     0.00000000e+00+0.j        ,  -3.55271368e-15-2.61312593j,
...     -4.00000000e+00+0.j        ,   1.11022302e-15+2.61312593j,
...     0.00000000e+00+0.j        ,   1.55431223e-15+1.0823922j ,
...     0.00000000e+00+0.j        ,   4.44089210e-16+1.0823922j ,
...     0.00000000e+00+0.j        ,   3.55271368e-15+2.61312593j]))
OK
    
>>> case(fft.fft( (0,1)*4, n=4 ),
...      Numeric.array([ 2.+0.j,  0.+0.j, -2.+0.j,  0.+0.j]))
OK
    
>>> case(fft.fft2d( [(0,1),(1,0)] ),
...      Numeric.array([[ 2.+0.j,  0.+0.j],
...                     [ 0.+0.j, -2.+0.j]]))
OK

>>> case(fft.inverse_fft2d (fft.fft2d( [(0, 1), (1, 0)] ) ),
...      Numeric.array([[ 0.+0.j,  1.+0.j],
...                     [ 1.+0.j,  0.+0.j]]))
OK
    
>>> case(fft.real_fft2d([(0,1),(1,0)]),
...      Numeric.array([[ 2.+0.j,  0.+0.j],
...                     [ 0.+0.j, -2.+0.j]]))
OK

>>> case(fft.real_fft2d([(1,1),(1,1)] ),
...      Numeric.array([[ 4.+0.j,  0.+0.j],
...                     [ 0.+0.j,  0.+0.j]]))
OK

    Tests for correctness.
    Somewhat limited since
    p (and thus q also) is real and hermite, and the dimension we're
    testing is a power of 2.  If someone can cook up more general data
    in their head or with another program/library, splice it in!

>>> toler = 1.e-10

>>> oosq2 = 1.0/Numeric.sqrt(2.0)

>>> p = Numeric.array(((1, 1, 1, 1, 1, 1, 1, 1),
...                    (1, oosq2, 0, -oosq2, -1, -oosq2, 0, oosq2),
...                    (1, 0, -1, 0, 1, 0, -1, 0),
...                    (1, -oosq2, 0, oosq2, -1, oosq2, 0, -oosq2),
...                    (1, -1, 1, -1, 1, -1, 1, -1),
...                    (1, 0, 0, 0, 0, 0, 0, 0),
...                    (0, 0, 0, 0, 0, 0, 0, 0)))
>>> q = Numeric.array(((8,0,0,0,0,0,0,0),
...                    (0,4,0,0,0,0,0,4),
...                    (0,0,4,0,0,0,4,0),
...                    (0,0,0,4,0,4,0,0),
...                    (0,0,0,0,8,0,0,0),
...                    (1,1,1,1,1,1,1,1),
...                    (0,0,0,0,0,0,0,0)))

    Correctness testing dimension -1.

>>> P = fft.fft(p)
>>> cndns(P-q) / cndns(q) > toler
0

>>> RP = fft.real_fft(p)
>>> npt = p.getshape()[-1]; rpt = npt/2 + 1
>>> qr = q[:,:rpt];  cndns(RP-qr) / cndns(qr) > toler
0

>>> I = fft.inverse_real_fft(q, npt)
>>> cndns(I-p) / cndns(p) > toler
0

Consistency testing dimension 0, length 7.

>>> dim = 0

FFT

>>> P = fft.fft(p, None, dim)
>>> Q = fft.inverse_fft(P, None, dim)
>>> cndns(Q-p) / cndns(p) > toler
0

Real FFT

>>> RP = fft.real_fft(p, None, dim)
>>> npt = p.getshape()[dim]; rpt = npt/2 + 1
>>> P = Numeric.take(P, range(rpt), axis=dim)
>>> cndns(RP-P) / cndns(RP) > toler
0

Inverse FFT

>>> P = fft.fft(p, None, dim)
>>> Q = fft.inverse_fft(P, None, dim)
>>> cndns(Q-p) / cndns(p) > toler
0

Inverse Hermite FFT

>>> hp = fft.inverse_hermite_fft(q, npt, dim)
>>> Q = fft.inverse_fft(q, None, dim)
>>> Q = Numeric.take(Q, range(rpt), axis=dim)
>>> cndns(hp-Q) / cndns(hp) > toler
0


Consistency testing dimension 1, length 8.

>>> dim = 1

FFT

>>> P = fft.fft(p, None, dim)
>>> Q = fft.inverse_fft(P, None, dim)
>>> cndns(Q-p) / cndns(p) > toler
0

Real FFT

>>> RP = fft.real_fft(p, None, dim)
>>> npt = p.getshape()[dim]; rpt = npt/2 + 1
>>> P = Numeric.take(P, range(rpt), axis=dim)
>>> cndns(RP-P) / cndns(RP) > toler
0

Inverse FFT

>>> P = fft.fft(p, None, dim)
>>> Q = fft.inverse_fft(P, None, dim)
>>> cndns(Q-p) / cndns(p) > toler
0

Inverse Hermite FFT

>>> hp = fft.inverse_hermite_fft(q, npt, dim)
>>> Q = fft.inverse_fft(q, None, dim)
>>> Q = Numeric.take(Q, range(rpt), axis=dim)
>>> cndns(hp-Q) / cndns(hp) > toler
0

Multi-dimensional tests

>>> tee = Numeric.array(((2.0, 0, 2, 0),
...                      (0, 2, 0, 2),
...                      (2, 0, 2, 0),
...                      (0, 2, 0, 2)))
>>> eff = Numeric.array(((16.0, 0, 0, 0),
...                      (0, 0, 0, 0),
...                      (0, 0, 16, 0),
...                      (0, 0, 0, 0)))

FFT N-dimensional

>>> ftest = fft.fftnd(tee)
>>> cndns(ftest - eff) / cndns(eff) > toler
0

Inverse FFT N-Dimensional

>>> cndns(fft.inverse_fftnd(ftest) - tee) / cndns(tee) > toler
0

Real FFT N-dimensional

>>> fred = fft.real_fftnd(p)
>>> npts = p.getshape()[-1]; rpts = npts/2 + 1
>>> actual = fft.fftnd(p);  ract = actual[..., :rpts]
>>> cndns(fred-ract) / cndns(ract) > toler
0

Inverse Real FFT N-dimensional

>>> ethel = fft.inverse_real_fftnd(fred)
>>> cndns(p-ethel) / cndns(p) > toler
0

FFT 2D shape test:

>>> axes = (0,1); shape = (7,4); barney = Numeric.zeros(shape,'d')
>>> betty = fft.fft2d(barney); betty.shape == barney.shape
1
>>> betty = fft.fft2d(barney, None, axes); betty.shape == barney.shape
1
>>> betty = fft.fft2d(barney, shape); betty.shape == barney.shape
1
>>> betty = fft.fft2d(barney, shape, axes); betty.shape == barney.shape
1
>>> betty = fft.real_fft2d(barney); wilma = fft.inverse_real_fft2d(betty)
>>> wilma.shape == barney.shape
1
>>> wilma = fft.inverse_real_fft2d(betty, shape); wilma.shape == barney.shape
1
>>> wilma = fft.inverse_real_fft2d(betty, None, axes); wilma.shape == barney.shape
1
>>> wilma = fft.inverse_real_fft2d(betty, shape, axes); wilma.shape == barney.shape
1

Codelet order test:

>>> for size in range (1,25): 
...     for i in range(3): 
...         a = random_array.random(size) 
...         b = fft.real_fft(a) 
...         c = fft.inverse_real_fft(b,size) 
...         if cndns(c-a) / cndns(a) > toler:
...             print "real transforms failed for size %d" % size
...         a = a + random_array.random(size) * 1j
...         b = fft.fft(a) 
...         c = fft.inverse_fft(b,size) 
...         if cndns(c-a) / cndns(a) > toler:
...             print "complex transforms failed for size %d" % size

>>> x = Numeric.cos(Numeric.arange(30.0)/30.0*2*Numeric.pi) 
>>> y = fft.real_fft(x) 
>>> z = fft.inverse_real_fft(y,30)
>>> cndns(x-z)/cndns(x) > toler
0
"""

import numarray.fft as fft
import numarray.numeric as Numeric
import numarray.random_array as random_array
import sys

class DoctestError(Exception):
    pass

def cndns(m):
    return Numeric.maximum.reduce(Numeric.abs(m).getflat())

def case(expr, ans, eps=1e-9):
    if cndns(ans-expr) < eps:
        print "OK"
    else:
        raise DoctestError, "failed"
    
def test():
    import dtest
    import doctest
    return doctest.testmod(dtest)

if __name__ == '__main__':
    test()