Debian Codegraph @

t/fftw.t

raw symbols SCIP
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use PDL::LiteF;
7 use PDL::Types;
8 use PDL::FFTW3;
9
10 # Please be careful about rearranging these tests, since they depend on the
11 # global FFTW plan cache, and thus order can matter.
12 #
13 # Here I use GNU octave to compute the reference results. To transform 'format
14 # long' octave output to the PDL input that appears here I use this:
15 #
16 # perl -ane 'if(/Column/) { print "],\n[" } if( @F == 3 ) { $F[2] =~ s/i//; print "[" . "$F[0],$F[1]$F[2]" . "],"; } END {print "],\n"} '
17
18 # I turn on the pthreading to stress things out a bit more, and to make sure it
19 # works
20 set_autopthread_targ(2);
21 set_autopthread_size(0);
22
23 # With potentially-unaligned input I can no longer predict when a new plan will
24 # be created, so I disable plan-creation checks. When PDL itself produces only
25 # aligned data buffers this should be re-enabled.
26 my $do_check_plan_creations = undef;
27
28 use constant approx_eps_double => 1e-8;
29 use constant approx_eps_single => 1e-3;
30
31 my $Nplans = 0;
32
33 # 1D basic test
34 {
35 my $x = pdl '0 1+1i 2+4i 3+9i 4+16i 5+25i 6+36i 7+49i 8+64i 9+81i';
36
37 # from octave: conj( fft( (0:9) + i* ((0:9).**2) )' )
38 my $Xref = pdl('45.0+285i
39 -158.8841768587627+17.7490974608742i
40 -73.8190960235587-28.6459544426446i
41 -41.3271264002680-38.7279671349711i
42 -21.2459848116453-42.8475374738350i
43 -5.0-45i
44 11.2459848116453-46.0967344361641i
45 31.3271264002680-45.9933924150247i
46 63.8190960235587-42.4097736473563i
47 148.8841768587627-13.0277379108784i');
48
49 ok_should_make_plan( all( approx( fft1($x), $Xref, approx_eps_double) ),
50 "Basic 1D complex FFT - double precision" );
51
52 ok_should_reuse_plan( all( approx( $x->fft1, $Xref, approx_eps_double) ),
53 "Basic 1D complex FFT as a method - double precision" );
54
55 ok_should_make_plan( all( approx( fft1(cfloat $x), cfloat($Xref), approx_eps_single) ),
56 "Basic 1D complex FFT - single precision" );
57
58 ok_should_make_plan( all( approx( ifft1(fft1($x)), $x, approx_eps_double) ),
59 "Basic 1D complex FFT - inverse(forward) should be the same (normalized)" );
60 }
61
62 # 2D basic test
63 {
64 my $x = pdl '
65 0 1+0.01i 2+0.04i 3+0.09i 4+0.16i;
66 5+0.25i 6+0.36i 7+0.49i 8+0.64i 9+0.81i;
67 10+i 11+1.21i 12+1.44i 13+1.69i 14+1.96i;
68 15+2.25i 16+2.56i 17+2.89i 18+3.24i 19+3.61i;
69 20+4i 21+4.41i 22+4.84i 23+5.29i 24+5.76i;
70 25+6.25i 26+6.76i 27+7.29i 28+7.84i 29+8.41i;
71 30+9i 31+9.61i 32+10.24i 33+10.89i 34+11.56i
72 ';
73
74 # from octave: fft2( reshape(0:34,5,7)' + i* (1e-2*reshape(0:34,5,7)' .** 2) )
75 my $Xref = pdl '
76 595+136.85i -25.9303392628859+18.4682083666705i -19.4901331394265-0.24543007434912i -15.5098668605735-11.6176194425008i -9.06966073711407-29.7051588498206i;
77 -158.361292658031+170.810364558179i 3.02129040241307-1.62582569424951i 2.10126095620458+0.284635136279005i 1.532651487797+1.46536486372099i 0.612622041588519+3.37582569424951i;
78 -114.713779395612+42.8112631783535i 1.90212339568438-0.0854244602002882i 0.982093949475899+0.648274540139187i 0.413484481068306+1.10172545986082i -0.506544965140175+1.83542446020029i;
79 -95.2888085635639-9.5507800001045i 1.40404722050367+0.60011858233589i 0.484017774295172+0.810109299679749i -0.0845916941124162+0.939890700320246i -1.00462114032089+1.14988141766411i;
80 -79.7111914364361-49.4933880183808i 1.0046211403209+1.14988141766412i 0.0845916941124141+0.939890700320234i -0.484017774295178+0.810109299679762i -1.40404722050366+0.600118582335895i;
81 -60.286220604388-96.7465798760672i 0.50654496514017+1.83542446020029i -0.413484481068314+1.10172545986081i -0.982093949475905+0.648274540139193i -1.90212339568438-0.085424460200287i;
82 -16.638707341969-192.58087984198i -0.612622041588522+3.3758256942495i -1.53265148779701+1.46536486372098i -2.10126095620459+0.28463513627902i -3.02129040241307-1.6258256942495i
83 ';
84
85 ok_should_make_plan( all( approx( fft2($x), $Xref, approx_eps_double) ),
86 "Basic 2D native complex FFT - double precision" );
87
88 ok_should_make_plan( all( approx( fft2(cfloat $x), cfloat($Xref), approx_eps_single) ),
89 "Basic 2D complex FFT - single precision" );
90
91 ok_should_make_plan( all( approx( ifft2(fft2($x)), $x, approx_eps_double) ),
92 "Basic 2D complex FFT - inverse(forward) should be the same (normalized)" );
93 }
94
95 # lots of 1D ffts broadcasted in a 2d array
96 {
97 my $x = pdl '
98 4.24264068711928i 1+4.12310562561766i 2+4i 3+3.87298334620742i 4+3.74165738677394i 5+3.60555127546399i;
99 6+3.46410161513775i 7+3.3166247903554i 8+3.16227766016838i 9+3i 10+2.82842712474619i 11+2.64575131106459i;
100 12+2.44948974278318i 13+2.23606797749979i 14+2i 15+1.73205080756888i 16+1.4142135623731i 17+i;
101 18 19+i 20+1.4142135623731i 21+1.73205080756888i 22+2i 23+2.23606797749979i;
102 24+2.44948974278318i 25+2.64575131106459i 26+2.82842712474619i 27+3i 28+3.16227766016838i 29+3.3166247903554i
103 ';
104
105 # octave result: fft( reshape(0:29,6,5) + i*sqrt(abs(reshape(18:-1:-11,6,5))) )
106 my $Xref = pdl '
107 15+23.5859383211823i -2.32805351899395+5.55930952077235i -2.7755160508616+2.11251769696778i -3+0.38265782660416i -3.2244839491384-1.35158391816997i -3.67194648100605-4.83299532464091i;
108 51+18.4171825014723i -2.12988347946625+5.6460896960971i -2.70812956895156+2.21961197953935i -3+0.49243029863233i -3.29187043104844-1.2444896355984i -3.87011652053375-4.74621514931617i;
109 87+10.8318220902249i -1.42222779450344+5.82451856548428i -2.43683966685802+2.58845058798449i -3+0.89558452008761i -3.56316033314198-0.87565102715326i -4.57777220549656-4.56778627992898i;
110 123+8.38233234744176i -4.57777220549656+3.3750288227011i -3.56316033314198+0.13896084520131i -3-1.55390522269557i -2.43683966685802-3.32514076993644i -1.42222779450344-7.01727602271216i;
111 159+17.4025706291177i -3.87011652053375+4.63147782374252i -3.29187043104844+1.20500010718478i -3-0.52218157372224i -2.70812956895156-2.25910150795298i -2.12988347946625-5.76082702167074i
112 ';
113
114 ok_should_make_plan( all( approx( fft1($x), $Xref, approx_eps_double) ),
115 "1D FFTs broadcasted inside a 2D ndarray" );
116
117 ok_should_make_plan( all( approx( ifft1(fft1($x)), $x , approx_eps_double) ),
118 "1D FFTs broadcasted inside a 2D ndarray - inverse(forward) should be the same" );
119 }
120
121 # lots of 1D ffts broadcasted in a 3d array
122 {
123 my $x = PDL::czip(sequence(6,5,4)**1.1, (abs(sequence(6,5,4) - 10))**0.8);
124
125 # octave reference code (3d array collapsed to 2d)
126 # re = reshape(0:119,6,4*5) .** 1.1
127 # im = (abs(reshape(0:119,6,4*5) - 10)) .** 0.8
128 # fft(re + i*im)
129 my $Xref = PDL::czip(reshape( pdl( [[1.69598045826025e+01,+2.99472886475101e+01],[-4.57128799578198e-01,+7.88558765388318e+00],[-2.51287898808965e+00,+3.70301251723728e+00],[-3.48312389248108e+00,+1.61384695353585e+00],[-4.40181702820775e+00,-4.91751648931500e-01],[-6.10485587424586e+00,-4.80054345442333e+00]],
130 [[6.33118711499336e+01,+9.18075894489374e+00],[-1.28375178011374e+00,+9.98129736230945e+00],[-4.36076655499263e+00,+3.82708231830186e+00],[-4.08027791431578e+00,+3.64309574332352e-01],[-3.78420764599829e+00,-9.13541864133209e-01],[-6.73854409586578e+00,-4.25130753757941e+00]],
131 [[1.13759697869505e+02,+1.97408963697151e+01],[-7.29388611445114e+00,+5.57273237248353e+00],[-5.32061252013794e+00,+6.73706431548140e-01],[-4.30915950819925e+00,-1.80990242523002e+00],[-3.28801256905250e+00,-4.31960446632102e+00],[-1.23762967061300e+00,-9.41122152264220e+00]],
132 [[1.66435716068732e+02,+3.92801435749690e+01],[-7.00620607474377e+00,+6.20403184755403e+00],[-5.31853826280121e+00,+1.07388595520872e+00],[-4.46232098197304e+00,-1.50418919374508e+00],[-3.59897150704107e+00,-4.09148521833613e+00],[-1.85441208781864e+00,-9.29419710710112e+00]],
133 [[2.20709491043481e+02,+5.64630711694275e+01],[-6.91312575495935e+00,+6.55086880329684e+00],[-5.36308316775101e+00,+1.27431097299403e+00],[-4.57890644153963e+00,-1.37165712546438e+00],[-3.78905073734364e+00,-4.02301895420251e+00],[-2.19365625629182e+00,-9.34243162811467e+00]],
134 [[2.76241513888165e+02,+7.23930056605892e+01],[-6.87318460924704e+00,+6.80271191149501e+00],[-5.41174690593730e+00,+1.41218875469242e+00],[-4.67359735938111e+00,-1.28842511961541e+00],[-3.93071118249889e+00,-3.99277491384701e+00],[-2.43142506404472e+00,-9.41307369494718e+00]],
135 [[3.32816672889238e+02,+8.74786737952202e+01],[-6.85538501192059e+00,+7.00327114642510e+00],[-5.45840640198368e+00,+1.51804628653698e+00],[-4.75361170451194e+00,-1.22862468678152e+00],[-4.04474354399512e+00,-3.97811872251649e+00],[-2.61520470809143e+00,-9.48587494925505e+00]],
136 [[3.90285534816727e+02,+1.01934524303480e+02],[-6.84870160277348e+00,+7.17094139230458e+00],[-5.50194078556953e+00,+1.60420335500904e+00],[-4.82306119868588e+00,-1.18240994885097e+00],[-4.14060148105651e+00,-3.97127361702190e+00],[-2.76521775751010e+00,-9.55598548492096e+00]],
137 [[4.48537844122192e+02,+1.15891995850952e+02],[-6.84822636641758e+00,+7.31547544486356e+00],[-5.54234829173794e+00,+1.67694646900063e+00],[-4.88452106175259e+00,-1.14500594148577e+00],[-4.22349575457002e+00,-3.96881837596082e+00],[-2.89204567772393e+00,-9.62221002325373e+00]],
138 [[5.07488694175294e+02,+1.29439468309100e+02],[-6.85145842410931e+00,+7.44274887104558e+00],[-5.57990116405240e+00,+1.73994109240401e+00],[-4.93971551455435e+00,-1.11374817715806e+00],[-4.29663683461546e+00,-3.96901565750555e+00],[-3.00194446700983e+00,-9.68443780878244e+00]],
139 [[5.67070568283683e+02,+1.42640190784627e+02],[-6.85700195115374e+00,+7.55660524048573e+00],[-5.61491735058256e+00,+1.79551966393514e+00],[-4.98985787485722e+00,-1.08700470396320e+00],[-4.36215484055982e+00,-3.97089501060820e+00],[-3.09892860515759e+00,-9.74290039627863e+00]],
140 [[6.27228420472482e+02,+1.55541513973010e+02],[-6.86402517554169e+00,+7.65970907217783e+00],[-5.64769495207659e+00,+1.84526159054989e+00],[-5.03583553656148e+00,-1.06370689880151e+00],[-4.42154056842631e+00,-3.97387614329543e+00],[-3.18573117267543e+00,-9.79792420309997e+00]],
141 [[6.87916464776208e+02,+1.68180119564361e+02],[-6.87200754524014e+00,+7.75398935566485e+00],[-5.67849584768631e+00,+1.89028747465175e+00],[-5.07831785783708e+00,-1.04311878827984e+00],[-4.47588053315932e+00,-3.97759390422351e+00],[-3.26429827802791e+00,-9.84984260267090e+00]],
142 [[7.49095987297811e+02,+1.80585198360161e+02],[-6.88061078534553e+00,+7.84089054155888e+00],[-5.70754494005455e+00,+1.93142189375022e+00],[-5.11782264695134e+00,-1.02471305032201e+00],[-4.52599231404356e+00,-3.98180928875154e+00],[-3.33606544685574e+00,-9.89896527117856e+00]],
143 [[8.10733802053247e+02,+1.92780489751580e+02],[-6.88960853349083e+00,+7.92152398109467e+00],[-5.73503408146089e+00,+1.96928962600128e+00],[-5.15475900259173e+00,-1.00809962679505e+00],[-4.57250726514005e+00,-3.98636092807034e+00],[-3.40212222387915e+00,-9.94556934146171e+00]],
144 [[8.72801129019100e+02,+2.04785648596696e+02],[-6.89884580787876e+00,+7.99676402590117e+00],[-5.76112695592524e+00,+2.00437559511875e+00],[-5.18945595634560e+00,-9.92982320347998e-01],[-4.61592350811448e+00,-3.99113714088274e+00],[-3.46331527512791e+00,-9.98989898003569e+00]],
145 [[9.35272758698146e+02,+2.16617194443934e+02],[-6.90821460405405e+00,+8.06731155157295e+00],[-5.78596367678119e+00,+2.03706383976585e+00],[-5.22218222758994e+00,-9.79131204378469e-01],[-4.65664122235683e+00,-3.99605908789967e+00],[-3.52031568871528e+00,-1.00321680493426e+01]],
146 [[9.98126416838528e+02,+2.28289190542651e+02],[-6.91763865128097e+00,+8.13373739118195e+00],[-5.80966474975681e+00,+2.06766375929783e+00],[-5.25316021545925e+00,-9.66364422864565e-01],[-4.69498691572975e+00,-4.00107022982894e+00],[-3.57366444372853e+00,-1.00725636708949e+01]],
147 [[1.06134227249634e+03,+2.39813741913757e+02],[-6.92706358972381e+00,+8.19651289056715e+00],[-5.83233436936525e+00,+2.09642832609833e+00],[-5.28257613941830e+00,-9.54535801950115e-01],[-4.73123057917205e+00,-4.00612951750744e+00],[-3.62380403823212e+00,-1.01112498044903e+01]],
148 [[1.12490255099778e+03,+2.51201368661019e+02],[-6.93645047026591e+00,+8.25603193408341e+00],[-5.85406312069293e+00,+2.12356705077917e+00],[-5.31058753759640e+00,-9.43526187589065e-01],[-4.76559809982867e+00,-4.01120687194793e+00],[-3.67110104753344e+00,-1.01483705104741e+01]] ),
149 2,6,5,4 )->using(0,1));
150
151 my $f = fft1($x);
152
153 is( get_autopthread_actual(), 2, "1D FFTs broadcasted inside a 3D ndarray - CPU threading should work" );
154
155 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_double) ),
156 "1D FFTs broadcasted inside a 3D ndarray" );
157 }
158
159 # try out some different ways of calling the module, make sure the argument
160 # verification works
161 {
162 eval { fft1( ) };
163 ok( $@, "Calling fft1 with no arguments should fail" );
164
165 eval { fft1(sequence(2,5), sequence(2,5), sequence(2,5) ) };
166 ok( $@, "Calling fft1 with too many arguments should fail" );
167
168 eval { fft1(null) };
169 ok( $@, "Calling fft1(null) should fail.");
170
171 eval { fft1( {} ) };
172 ok( $@, "Calling fft1( {} ) should fail (want ndarray input).");
173
174 eval { fft1(sequence(2,5), sequence(3,5) ) };
175 ok( $@, "Calling fft1 with mismatched arguments should fail" );
176
177 # should be able to ask for output in the arglist
178 my $x = random(10)->r2C;
179 my $f1 = fft1( $x );
180 my $f2;
181 eval { fft1( $x, $f2 ) };
182 ok( $@, "Calling fft1 with undef argument should fail" );
183
184 $f2 = null;
185 eval { fft1( $x, $f2 ) };
186 ok_should_reuse_plan( !$@ && all( approx( $f1, $f2 ), approx_eps_double),
187 "Should be able to ask for output in the arglist" );
188
189 eval { fft4( sequence(5,3,4,4)->r2C ); };
190 ok_should_make_plan( ! $@, "dimensionality baseline" );
191
192 eval { fft4( sequence(5,4,4)->r2C ); };
193 ok( $@, "too few dimensions should fail" );
194 }
195
196 # inplace checks
197 {
198 my $xorig = PDL::czip(sequence(10), sequence(10)**2);
199
200 # from octave: conj( fft( (0:9) + i* ((0:9).**2) )' )
201 my $Xref = PDL::czip(pdl( [45.0000000000000,+285.0000000000000],
202 [-158.8841768587627,+17.7490974608742],
203 [-73.8190960235587,-28.6459544426446],
204 [-41.3271264002680,-38.7279671349711],
205 [-21.2459848116453,-42.8475374738350],
206 [-5.0000000000000,-45.0000000000000],
207 [11.2459848116453,-46.0967344361641],
208 [31.3271264002680,-45.9933924150247],
209 [63.8190960235587,-42.4097736473563],
210 [148.8841768587627,-13.0277379108784] )->using(0,1));
211
212 my $x = $xorig->copy;
213 fft1($x, $x);
214 ok_should_make_plan( all( approx( $x, $Xref, approx_eps_double) ),
215 'In-place test: fft1($x,$x)' );
216
217 $x = $xorig->copy;
218 fft1( $x->inplace );
219 ok_should_reuse_plan( all( approx( $x, $Xref, approx_eps_double) ),
220 'In-place test: fft1( $x->inplace )' );
221
222 ok( !$x->is_inplace, "After computation the in-place flag should be cleared" );
223 }
224
225 # lots of 2D ffts broadcasted in a 3d array
226 {
227 my $x = PDL::czip( sequence(6,5,4)**1.1,
228 (abs(sequence(6,5,4) - 10))**0.8 );
229
230 # in octave I compute 4 slices separately
231 # re = reshape(0:119,6,4*5) .** 1.1
232 # im = (abs(reshape(0:119,6,4*5) - 10)) .** 0.8
233 # slice1 = fft2(re(:, 1:5 ) + i*im(:, 1:5 ))
234 # slice2 = fft2(re(:, 6:10) + i*im(:, 6:10))
235 # slice3 = fft2(re(:,11:15) + i*im(:,11:15))
236 # slice4 = fft2(re(:,16:20) + i*im(:,16:20))
237 my $Xref = PDL::czip(pdl( [
238 [[581.176580714253419,+154.612158706515430],[-22.954098523846195,+36.194518039527026],[-22.875879493772423,+10.551998195290025],[-20.913788738508782,-2.707592216571274],[-18.862059487643258,-13.839402151924361],[-18.129097984835099,-37.099701249860743]],
239 [[-178.408651850856160,+183.139542083867042],[11.470377256968625,-1.718053156591536],[5.282150186239193,+2.913557271028725],[2.408626479527148,+3.419465916129992],[1.652807905147736,+4.600098529135501],[-1.590381734378646,+10.091671271587607]],
240 [[-135.442076986364611,+37.496948236259065],[4.372069715046538,-5.432458423609452],[3.947276983624399,-0.475185262726981],[2.122893057072843,+1.257276333468205],[1.641541660378284,+1.195733119702670],[3.269737048112678,+3.674743286679739]],
241 [[-117.024242102743898,-47.339310480754413],[-0.861443876013559,+1.732467533240662],[0.185127597800303,+0.707054055400392],[-0.499359313992656,+1.552118887461319],[-2.447776485966577,+0.609947466490766],[-2.937831933834375,-2.841282678209592]],
242 [[-65.502586861276143,-178.172895308336507],[5.687451429953602,+8.651464276849186],[0.896929785660302,+4.817638327194231],[-0.533990946503966,+4.547965847191024],[-3.993598732954924,+4.974864791937925],[-11.136704766293862,+2.171852097686326]] ],
243
244 [
245 [[1.95537025989162e+03,+5.07137667919341e+02],[-3.42769560144680e+01,+3.57351487661338e+01],[-2.74943435492808e+01,+7.95132595764308e+00],[-2.40745068388859e+01,-5.95821387389173e+00],[-2.06361887967360e+01,-1.98800012868518e+01],[-1.37058376743800e+01,-4.77615819611594e+01]],
246 [[-1.90823299051261e+02,+1.63561853708853e+02],[-5.30707810726989e-01,-4.48966067048194e-01],[-1.41545809310629e-01,-3.74843067838806e-01],[5.33592439251135e-02,-3.42459575841451e-01],[2.48256646407656e-01,-3.13193921558001e-01],[6.37372950089715e-01,-2.63898986015401e-01]],
247 [[-1.55758721083882e+02,+1.14830386090883e+01],[-1.37557316201933e-01,-4.05958779660870e-01],[4.43148065741885e-02,-2.42630352946638e-01],[1.36711806861231e-01,-1.63554000296091e-01],[2.29943537575820e-01,-8.62535289493658e-02],[4.18482577171305e-01,+6.29473735169093e-02]],
248 [[-1.32979537236871e+02,-8.30537608826256e+01],[1.04159629258482e-01,-4.09670807209363e-01],[1.66802170317476e-01,-1.76664338294510e-01],[2.00610593363786e-01,-6.16794569397552e-02],[2.35974686283565e-01,+5.21905158295986e-02],[3.11024493644906e-01,+2.76346187116616e-01]],
249 [[-9.46011330787756e+01,-2.37163771051710e+02],[4.75138465903229e-01,-4.56993554740357e-01],[3.66037852013303e-01,-9.62444251010284e-02],[3.15838397830188e-01,+8.37813088919830e-02],[2.68458013974529e-01,+2.63383652294485e-01],[1.81832333250489e-01,+6.20818911805349e-01]]],
250
251 [
252 [[3.44204524288343e+03,+8.39727512433739e+02],[-3.43632539907719e+01,+3.87327181909819e+01],[-2.83836871718609e+01,+9.43178024888828e+00],[-2.53765929187988e+01,-5.22664306816161e+00],[-2.23580755213291e+01,-1.98905352749490e+01],[-1.62871457265958e+01,-4.92352018146898e+01]],
253 [[-1.93850055487056e+02,+1.78604950208849e+02],[-2.81086565720529e-01,-2.74439891768760e-01],[-6.30094446091169e-02,-2.17686087051074e-01],[4.62218954441780e-02,-1.90640300235732e-01],[1.55551122009561e-01,-1.64483923137678e-01],[3.74411612383404e-01,-1.14838044041313e-01]],
254 [[-1.62294627938734e+02,+1.82927539954391e+01],[-5.11143306149971e-02,-2.36649532393143e-01],[4.16430850746093e-02,-1.33253277381126e-01],[8.85453597042875e-02,-8.22033677083361e-02],[1.35780317473130e-01,-3.15956544938805e-02],[2.31197389980492e-01,+6.82673228827312e-02]],
255 [[-1.42113448440296e+02,-8.10603929075218e+01],[9.13716757316451e-02,-2.22938826506865e-01],[1.09204470637664e-01,-8.58346301593518e-02],[1.18905834471482e-01,-1.75430229641282e-02],[1.29121710583797e-01,+5.05580698311880e-02],[1.51066988141103e-01,+1.86140992994433e-01]],
256 [[-1.08434269598928e+02,-2.42363869807370e+02],[3.19073455607117e-01,-2.15663737884522e-01],[2.21262307844932e-01,-1.74079346210403e-02],[1.73630454892792e-01,+8.20062392537970e-02],[1.26848168463476e-01,+1.81581729708371e-01],[3.58267103028470e-02,+3.81129561460766e-01]]],
257
258 [
259 [[4.99244512804990e+03,+1.14070714415806e+03],[-3.45882131232035e+01,+4.06503577933066e+01],[-2.90431528725214e+01,+1.03290985710599e+01],[-2.62579620764095e+01,-4.83653993713021e+00],[-2.34643803252018e+01,-2.00056028480667e+01],[-1.78522004933373e+01,-5.03542510152376e+01]],
260 [[-1.96380323533266e+02,+1.88152304723188e+02],[-1.92865464527335e-01,-2.02889176717224e-01],[-3.86947461251954e-02,-1.56831303578437e-01],[3.85138755642588e-02,-1.34443894895275e-01],[1.15794735723374e-01,-1.12485367180954e-01],[2.70543304787344e-01,-6.98569500483150e-02]],
261 [[-1.66872451417648e+02,+2.23039858589711e+01],[-2.77487657486899e-02,-1.70406376180963e-01],[3.47883788886858e-02,-9.33380414940140e-02],[6.63337828406928e-02,-5.50977611340899e-02],[9.80588055564104e-02,-1.70571735903083e-02],[1.62032394212931e-01,+5.84144732089472e-02]],
262 [[-1.48137315640352e+02,-8.03755018459729e+01],[7.46992540366458e-02,-1.55140422934376e-01],[8.17655449197855e-02,-5.64025227538078e-02],[8.56906163946222e-02,-7.12349856999575e-03],[8.98748544483531e-02,+4.20897732548463e-02],[9.90136072387319e-02,+1.40302117404749e-01]],
263 [[-1.17049392363129e+02,-2.46859689910763e+02],[2.39899060049070e-01,-1.38101687968226e-01],[1.59658915211960e-01,-6.48727639912472e-04],[1.20144019881925e-01,+6.82934899895806e-02],[8.10343889012709e-02,+1.37369911169460e-01],[4.03481145874271e-03,+2.75896474493798e-01]]] )->using(0,1));
264
265 ok_should_make_plan( all( approx( fft2($x), $Xref, approx_eps_double) ),
266 "2D FFTs broadcasted inside a 3D ndarray" );
267
268 ok_should_make_plan( all( approx( fft2(cfloat $x), cfloat($Xref), approx_eps_single) ),
269 "2D FFTs broadcasted inside a 3D ndarray - single precision" );
270
271
272 # Now I take a slice of the input matrix, and fft that. This makes sure the
273 # nembed logic works correctly. It turns out that $P() in PP physicalizes the
274 # data, so no nembed logic currently exists, and stuff works anyway
275
276 my $x_slice1_connected = $x->slice('0:4');
277 my $x_slice1_severed = $x_slice1_connected->copy;
278
279 # octave ref; x is the same, but I slice it differently
280 # slice1 = fft2(re(1:5, 1:5 ) + i*im(1:5, 1:5 ))
281 # slice2 = fft2(re(1:5, 6:10) + i*im(1:5, 6:10))
282 # slice3 = fft2(re(1:5,11:15) + i*im(1:5,11:15))
283 # slice4 = fft2(re(1:5,16:20) + i*im(1:5,16:20))
284 my $X_slice1_ref = PDL::czip(pdl( [
285 [[466.673919234444952,+126.917907968545364],[-17.870670056332415,+22.924773314027945],[-17.419713146828549,+4.981405529320815],[-17.185596880754357,-6.395001262195541],[-16.329892079156576,-25.335769447218507]],
286 [[-146.569840424967879,+155.523541145286003],[7.951065332965691,-1.600154624557469],[4.722562521481950,+2.138095446385981],[2.501374281282990,+4.563007814082358],[-1.259388175222825,+8.080745148100860]],
287 [[-111.129976688165755,+32.155437078172781],[2.298297449575815,-3.696832692971159],[2.653255527169226,-1.211338065207176],[2.961744281698524,+0.603384484335323],[3.142880661280621,+3.580230270481282]],
288 [[-98.149556168903700,-38.308859759070558],[-1.167503315289326,+2.243222101185434],[-1.415433176872478,+0.465280753105545],[-1.265331912942163,-0.563640202617083],[-0.990113854951290,-1.818507239579064]],
289 [[-55.390996616595444,-144.671074787325381],[4.429337544491914,+7.214235144120515],[-0.507891051226907,+5.073209513809378],[-3.336844872242201,+3.567780589580991],[-7.345688413939850,+1.308257900249694]]],
290 [
291 [[1.60938166615551e+03,+4.17670759558360e+02],[-2.68270274976814e+01,+2.26046920161292e+01],[-2.16496476842982e+01,+1.53675022177060e+00],[-1.84235786296893e+01,-1.15007886011593e+01],[-1.31627231622858e+01,-3.26244585940880e+01]],
292 [[-1.58974908440522e+02,+1.36021676093101e+02],[-3.47634511617340e-01,-3.58080303406544e-01],[-4.81671641422727e-02,-3.04113508354926e-01],[1.37085201618827e-01,-2.75199098858623e-01],[4.36488388932893e-01,-2.35539773688715e-01]],
293 [[-1.29686452688029e+02,+9.43606648956182e+00],[-7.01487711558949e-02,-2.99496693116574e-01],[7.08541090235068e-02,-1.75726626102805e-01],[1.59295212504510e-01,-1.01725734277373e-01],[3.04097411071888e-01,+1.38736645617663e-02]],
294 [[-1.10651829493696e+02,-6.92608523091722e+01],[1.02487962542310e-01,-2.85298738134981e-01],[1.52660684703708e-01,-1.07199757251788e-01],[1.85962069038061e-01,+1.35561877936201e-03],[2.43247390743176e-01,+1.74296200677137e-01]],
295 [[-7.85760340725247e+01,-1.97565816638999e+02],[3.69940012121665e-01,-2.93442198214098e-01],[2.89796229462924e-01,-1.61887501790537e-02],[2.44337576650966e-01,+1.54687612811214e-01],[1.77103574452566e-01,+4.29905675781014e-01]]],
296 [
297 [[2.84720460273767e+03,+6.95426680279720e+02],[-2.71001945282257e+01,+2.47316707441501e+01],[-2.25465424823261e+01,+2.50812248540162e+00],[-1.97162495543114e+01,-1.12342767103843e+01],[-1.51112098777464e+01,-3.34825045357138e+01]],
298 [[-1.61503358924060e+02,+1.48680117938248e+02],[-1.79689739185806e-01,-2.15121551579987e-01],[-1.29520991006925e-02,-1.72606756571795e-01],[9.02589918624682e-02,-1.47576720159049e-01],[2.57436047526738e-01,-1.09094607758747e-01]],
299 [[-1.35172303389293e+02,+1.51762198152973e+01],[-1.92334762886415e-02,-1.71645475592242e-01],[5.19956985158810e-02,-9.30825631541810e-02],[9.64968448637267e-02,-4.51405104074254e-02],[1.69230891271143e-01,+3.14149724776526e-02]],
300 [[-1.18329666591565e+02,-6.75646155694635e+01],[8.08712125581526e-02,-1.51645886509917e-01],[9.50510458629830e-02,-4.70696243011893e-02],[1.04542849160088e-01,+1.73074187214384e-02],[1.21053350041535e-01,+1.21015057947666e-01]],
301 [[-9.02186573536845e+01,-2.01901820815281e+02],[2.41850811057668e-01,-1.30135033446000e-01],[1.68066917394274e-01,+2.15612297762638e-02],[1.23657088647714e-01,+1.15562097610201e-01],[5.37254517374679e-02,+2.67979230130139e-01]]],
302 [
303 [[4.13847519887461e+03,+9.46564617413697e+02],[-2.73984844843996e+01,+2.60748739526840e+01],[-2.31796005168213e+01,+3.07463500922328e+00],[-2.05604995190013e+01,-1.11449862512929e+01],[-1.63039317216984e+01,-3.41606345226538e+01]],
304 [[-1.63618296453467e+02,+1.56682279811098e+02],[-1.21816276616064e-01,-1.57768013235654e-01],[-4.20394797933782e-03,-1.23055796103499e-01],[6.85931647323148e-02,-1.02200070987194e-01],[1.86530323460939e-01,-6.94242384647662e-02]],
305 [[-1.39005402614916e+02,+1.85410801291960e+01],[-7.24932070775660e-03,-1.22713508583900e-01],[4.06094617668798e-02,-6.41494708720088e-02],[7.04423276692341e-02,-2.82307037179682e-02],[1.19111127939139e-01,+2.94310371284693e-02]],
306 [[-1.23376794373435e+02,-6.69854082776712e+01],[6.41877749756504e-02,-1.04443436094924e-01],[6.98384223826521e-02,-2.92264510229911e-02],[7.36940494064069e-02,+1.71728447983243e-02],[8.05143239080554e-02,+9.20916536812421e-02]],
307 [[-9.74417295843736e+01,-2.05659722932842e+02],[1.79920562610709e-01,-8.03744824636155e-02],[1.19169692096844e-01,+2.45697427068684e-02],[8.21878607839186e-02,+8.96232824108548e-02],[2.32671625274674e-02,+1.95170681256241e-01]]] )->using(0,1));
308
309 ok_should_make_plan( all( approx( fft2($x_slice1_severed), $X_slice1_ref, approx_eps_double) ),
310 "2D FFTs broadcasted inside a 3D ndarray - slice1 - severed" );
311
312 ok_should_reuse_plan( all( approx( fft2($x_slice1_connected), $X_slice1_ref, approx_eps_double) ),
313 "2D FFTs broadcasted inside a 3D ndarray - slice1 - connected" );
314
315 # now go again with single precision. If $P() didn't physicalize its input,
316 # this would no longer be aligned like before, since the input has shifted by
317 # 4 bytes
318 ok_should_make_plan( all( approx( fft2(cfloat $x_slice1_connected), cfloat($X_slice1_ref), approx_eps_single) ),
319 "2D FFTs broadcasted inside a 3D ndarray - slice1 - connected - single precision" );
320
321
322
323
324 # Similar, but slicing from 1
325 my $x_slice2_connected = $x->slice('1:5');
326 my $x_slice2_severed = $x_slice2_connected->copy;
327
328 # octave ref; x is the same, but I slice it differently
329 # slice1 = fft2(re(2:6, 1:5 ) + i*im(2:6, 1:5 ))
330 # slice2 = fft2(re(2:6, 6:10) + i*im(2:6, 6:10))
331 # slice3 = fft2(re(2:6,11:15) + i*im(2:6,11:15))
332 # slice4 = fft2(re(2:6,16:20) + i*im(2:6,16:20))
333 my $X_slice2_ref = PDL::czip(pdl( [
334 [[501.602971299978947,+129.993495486019384],[-19.456482657442123,+24.258038724117910],[-18.901134436232283,+3.773486895387808],[-16.305859228821138,-7.743879151484332],[-15.423234561310013,-24.567626816829364]],
335 [[-151.877806557964107,+149.398495098007515],[8.163865009246718,+0.126518424791354],[2.817072235106799,+2.881419182368629],[1.352437467320572,+2.913388776160406],[-0.169443715571585,+6.850269367688697]],
336 [[-115.427317232676344,+31.210772021296826],[3.796588391538631,-3.335504645848345],[2.597359558283342,+0.777874238982427],[0.879920386805304,+1.061145569333210],[2.149823747278043,+1.912391035534666]],
337 [[-96.426654416952132,-39.742809611359242],[-0.598036090192128,+0.778294180312916],[0.320622108376032,+0.964395257139944],[-1.544335750271782,+1.347131498949573],[-2.866826597745860,-1.701987219366836]],
338 [[-53.072170179373629,-152.671376980423531],[2.832891540353520,+6.174977250984833],[0.255424405994367,+3.432508834397477],[-1.781776614496669,+4.184361527846315],[-7.917898111232422,+2.712874425873987]]],
339 [
340 [[1.64950652205531e+03,+4.27600276999139e+02],[-2.68326169419681e+01,+2.27006099751444e+01],[-2.16864604518378e+01,+1.58304952104838e+00],[-1.84802721265835e+01,-1.14845056637592e+01],[-1.32524144503064e+01,-3.26556345359360e+01]],
341 [[-1.59063871746115e+02,+1.36592105027094e+02],[-3.36871694070668e-01,-3.49609863474421e-01],[-4.55953991858456e-02,-2.96056972955683e-01],[1.34600311442187e-01,-2.67131294666781e-01],[4.25889674867783e-01,-2.27029553320656e-01]],
342 [[-1.29914250138566e+02,+9.70844038896290e+00],[-6.62751362095687e-02,-2.91745099839182e-01],[7.01619885190578e-02,-1.70438268145247e-01],[1.55723949511221e-01,-9.77926603796963e-02],[2.95815595456073e-01,+1.58897872028383e-02]],
343 [[-1.10986042959537e+02,-6.91582210856051e+01],[1.02120011495870e-01,-2.76831416225680e-01],[1.49564602040562e-01,-1.03018025874890e-01],[1.81059831817083e-01,+3.01286202283804e-03],[2.35263568251515e-01,+1.72089158783488e-01]],
344 [[-7.91021617428083e+01,-1.97705600191950e+02],[3.63757532989153e-01,-2.82417409570807e-01],[2.82874371475266e-01,-1.27278916830977e-02],[2.36733361180215e-01,+1.53574423567642e-01],[1.68068158380308e-01,+4.21594307082665e-01]]],
345 [
346 [[2.88949916162442e+03,+7.04137573981104e+02],[-2.71104520320376e+01,+2.47850199832838e+01],[-2.25708531361947e+01,+2.53121804927141e+00],[-1.97494432727707e+01,-1.12297375097047e+01],[-1.51590850953107e+01,-3.35077499355859e+01]],
347 [[-1.61580394342465e+02,+1.48997806548413e+02],[-1.76879506003449e-01,-2.12465169857628e-01],[-1.24755429736926e-02,-1.70267020812346e-01],[8.92911645313911e-02,-1.45396031420977e-01],[2.54131545443049e-01,-1.07112102202037e-01]],
348 [[-1.35319865252548e+02,+1.53132007477149e+01],[-1.85746548309280e-02,-1.69343727504296e-01],[5.15118373088666e-02,-9.16869090114727e-02],[9.52949133188648e-02,-4.42851974580498e-02],[1.66850120576454e-01,+3.14287260996196e-02]],
349 [[-1.18527818813508e+02,-6.75353911868008e+01],[8.01879439510876e-02,-1.49371193378786e-01],[9.38997734474098e-02,-4.61642470396344e-02],[1.03082280030402e-01,+1.73771906993944e-02],[1.19061337332500e-01,+1.19751924894898e-01]],
350 [[-9.05079981819587e+01,-2.02038499149128e+02],[2.39079369215351e-01,-1.27615555152443e-01],[1.65776790935290e-01,+2.18486772332205e-02],[1.21629148805613e-01,+1.14469852669599e-01],[5.20654175199115e-02,+2.64657747522687e-01]]],
351 [
352 [[4.18223859152336e+03,+9.54625443037726e+02],[-2.74079447865556e+01,+2.61124638323619e+01],[-2.31978971811230e+01,+3.09007255890941e+00],[-2.05843589930420e+01,-1.11431800313476e+01],[-1.63369532442610e+01,-3.41807837482412e+01]],
353 [[-1.63682484895292e+02,+1.56906338384727e+02],[-1.20524688463036e-01,-1.56416089806520e-01],[-4.03733743270347e-03,-1.21913001592987e-01],[6.80631350314347e-02,-1.01174630711859e-01],[1.84871712650214e-01,-6.85694062663622e-02]],
354 [[-1.39115953613999e+02,+1.86329023623412e+01],[-7.02295195511970e-03,-1.21580690846917e-01],[4.03182507468609e-02,-6.34983391169523e-02],[6.98267599630536e-02,-2.78713424589018e-02],[1.17963938106273e-01,+2.93281548087439e-02]],
355 [[-1.23519603679800e+02,-6.69735391127112e+01],[6.37479744806300e-02,-1.03380542771702e-01],[6.92370420039889e-02,-2.88488700507459e-02],[7.29858824833397e-02,+1.71294493033826e-02],[7.96224057648701e-02,+9.13717087078728e-02]],
356 [[-9.76419555018576e+01,-2.05773543168977e+02],[1.78415452819879e-01,-7.93210083326257e-02],[1.18035987634609e-01,+2.45615120500224e-02],[8.12721813043989e-02,+8.89567757119387e-02],[2.26856083517065e-02,+1.93436447059624e-01]]] )->using(0,1));
357
358 ok_should_reuse_plan( all( approx( fft2($x_slice2_severed), $X_slice2_ref, approx_eps_double) ),
359 "2D FFTs broadcasted inside a 3D ndarray - slice2 - severed" );
360
361 ok_should_reuse_plan( all( approx( fft2($x_slice2_connected), $X_slice2_ref, approx_eps_double) ),
362 "2D FFTs broadcasted inside a 3D ndarray - slice2 - connected" );
363
364 # now go again with single precision. If $P() didn't physicalize its input,
365 # this would no longer be aligned like before, since the input has shifted by
366 # 4 bytes
367 ok_should_reuse_plan( all( approx( fft2(cfloat $x_slice2_connected), cfloat($X_slice2_ref), approx_eps_single) ),
368 "2D FFTs broadcasted inside a 3D ndarray - slice2 - connected - single precision" );
369
370
371 # I now try to fft INTO a slice. The out-of-slice pieces shouldn't be touched
372 my $x_orig = $x->copy;
373 fft2( $x_slice2_severed, $x_slice2_connected );
374 is( get_autopthread_actual(), 2, "1D FFTs broadcasted inside a 3D ndarray - slice2 - connected - in-slice correct - CPU threading should work" );
375 ok_should_reuse_plan( all( approx( $x_slice2_connected, $X_slice2_ref, approx_eps_double) ),
376 "2D FFTs broadcasted inside a 3D ndarray - slice2 - connected - in-slice correct" );
377
378 my $X_partialfft_ref = $x_orig->copy;
379 $X_partialfft_ref->slice('1:5') .= $x_slice2_connected;
380
381 ok( all( approx( $x, $X_partialfft_ref, approx_eps_double) ),
382 "2D FFTs broadcasted inside a 3D ndarray - slice2 - connected - out-of-slice correct" );
383 }
384
385 # check the type logic
386 {
387 my $x = PDL::czip(sequence(10), sequence(10)**2);
388
389 # from octave: conj( fft( (0:9) + i* ((0:9).**2) )' )
390 my $Xref = PDL::czip(pdl( [45.0000000000000,+285.0000000000000],
391 [-158.8841768587627,+17.7490974608742],
392 [-73.8190960235587,-28.6459544426446],
393 [-41.3271264002680,-38.7279671349711],
394 [-21.2459848116453,-42.8475374738350],
395 [-5.0000000000000,-45.0000000000000],
396 [11.2459848116453,-46.0967344361641],
397 [31.3271264002680,-45.9933924150247],
398 [63.8190960235587,-42.4097736473563],
399 [148.8841768587627,-13.0277379108784] )->using(0,1));
400
401 my $f = null;
402 my $F;
403
404 fft1($x, $f);
405 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_double) ),
406 "Type-checking baseline" );
407 ok( $PDL::FFTW3::_last_do_double_precision,
408 "Unspecified FFTs should be double-precision" );
409
410 $f = null;
411 $F = fft1($x->cfloat, $f);
412 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_single) ),
413 "Float input/Unspecified output baseline" );
414 ok( ! $PDL::FFTW3::_last_do_double_precision,
415 "Float input/Unspecified output should do a float FFT" );
416 is( $F->type, cfloat,
417 "CFloat input/Unspecified output should return a cfloat" );
418
419 $f = $x->zeros;
420 fft1($x, $f);
421 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_double) ),
422 "double input/double output baseline" );
423 ok( $PDL::FFTW3::_last_do_double_precision,
424 "double input/double output should do a double FFT" );
425
426 $f = $x->zeros->cfloat;
427 fft1($x, $f);
428 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_single) ),
429 "double input/float output baseline" );
430 ok( ! $PDL::FFTW3::_last_do_double_precision,
431 "double input/cfloat output should do a float FFT" );
432
433 $f = $x->zeros->byte;
434 eval { fft1($x, $f) };
435 ok( $@, "Output to 'byte' should fail");
436
437 $f = $x->zeros;
438 fft1($x->cfloat, $f->cdouble );
439 ok_should_reuse_plan( all( approx( $f, $Xref, approx_eps_double) ),
440 "float input/double output baseline" );
441 ok( $PDL::FFTW3::_last_do_double_precision,
442 "float input/double output should do a double FFT" );
443 }
444
445 # real fft basic checks
446 {
447 my $x6 = sequence(6)**2;
448 my $x7 = sequence(7)**2;
449
450 # octave refs parse with this:
451 # perl -ane '@F[2] =~ s/i//g; print "[$F[0],$F[1]$F[2]],\n";'
452
453 # octave reference: conj(fft( (0:5).**2 )') and conj(fft( (0:6).**2 )')
454 my $fx6_ref = PDL::czip(pdl( [55.00000000000000,+0.00000000000000],
455 [-6.00000000000000,+31.17691453623979],
456 [-14.00000000000000,+10.39230484541326],
457 [-15.00000000000000,+0.00000000000000],
458 [-14.00000000000000,-10.39230484541326],
459 [-6.00000000000000,-31.17691453623979] )->using(0,1));
460
461 my $fx7_ref = PDL::czip(pdl( [91.00000000000000,+0.00000000000000],
462 [-5.90820611352046,+50.87477421602225],
463 [-18.77412667908545,+19.53809802761889],
464 [-20.81766720739410,+5.59196512255867],
465 [-20.81766720739410,-5.59196512255867],
466 [-18.77412667908545,-19.53809802761889],
467 [-5.90820611352046,-50.87477421602225] )->using(0,1));
468
469 my $fx6 = rNfft1($x6);
470 my $fx6_ref_input = $fx6_ref->slice('0:3');
471 ok_should_make_plan( all( approx( $fx6, $fx6_ref_input, approx_eps_double) ),
472 "rfft basic test - native forward - 6long" );
473
474 $fx6 = rNfft1($x6->float);
475 ok_should_make_plan( all( approx( $fx6, $fx6_ref_input->cfloat, approx_eps_single) ),
476 "rNfft - native float forward - 6long" )
477 or diag "Got: $fx6\nExpected: ", $fx6_ref_input->cfloat;
478
479 my $fx7 = rNfft1($x7);
480 ok_should_make_plan( all( approx( $fx7, $fx7_ref->slice('0:3'), approx_eps_double) ),
481 "rfft basic test - forward - 7long" );
482
483 my $x6_back = irfft1($fx6_ref_input, zeros(6) );
484 ok_should_make_plan( all( approx( $x6_back, $x6, approx_eps_double) ),
485 "rfft basic test - native backward - 6long - output in arglist" )
486 or diag "Got: ($x6_back)\nExpected ($x6)";
487
488 $x6_back = irfft1($fx6_ref_input);
489 ok_should_reuse_plan( all( approx( $x6_back, $x6, approx_eps_double) ),
490 "rfft basic test - native backward - 6long - output returned" )
491 or diag "Got: ($x6_back)\nExpected ($x6)";
492
493 my $x7_back = irfft1($fx7_ref->slice('0:3'), zeros(7) );
494 ok_should_make_plan( all( approx( $x7, $x7_back, approx_eps_double) ),
495 "rfft basic test - backward - 7long" );
496
497 # Currently a single plan is made for ALL the thread slices. These tests are
498 # meant to exercise cases where this is a bad assumption. I.e. where some
499 # slices have different alignment than others. For some reason I'm not seeing
500 # these tests trigger any failures, so I'm not adding any plan-per-slice
501 # logic. I'm leaving these tests here so that if error DO show up, we'll know
502 # about them.
503 #
504 # I have two 1d broadcasted real ffts with odd size. I do this both with
505 # single-precision and with double-precision
506 #
507 # octave code:
508 # conj(fft((0:4).**2)') and conj(fft((5+(0:4)).**2)')
509 my $x5_double = (sequence(10)**2)->reshape(5,2);
510 my $x5_single = $x5_double->float;
511
512 my $fx5_ref = PDL::czip(pdl( [ [30.00000000000000, + 0.00000000000000],
513 [-5.26393202250021, +17.20477400588967],
514 [-9.73606797749979, + 4.06149620291133],
515 [-9.73606797749979, - 4.06149620291133],
516 [-5.26393202250021, -17.20477400588967]],
517 [ [255.0000000000000, + 0.0000000000000],
518 [-30.2639320225002, +51.6143220176690],
519 [-34.7360679774998, +12.1844886087340],
520 [-34.7360679774998, -12.1844886087340],
521 [-30.2639320225002, -51.6143220176690]])->using(0,1));
522
523 my $fx5_double = rfft1($x5_double);
524 ok_should_make_plan( all( approx( $fx5_double, $fx5_ref->slice('0:2'), approx_eps_double) ),
525 "rfft broadcasted double precision, odd number. May need 2 plans" );
526
527 my $fx5_single = rfft1($x5_single);
528 ok_should_make_plan( all( approx( $fx5_single, $fx5_ref->slice('0:2'), approx_eps_single) ),
529 "rfft broadcasted single precision, odd number. May need 2 plans" ) or diag "got=$fx5_single\nexpected=", $fx5_ref->slice('0:2');
530 }
531
532 # real fft 2D checks
533 {
534 my $x64 = sequence(6,4)**1.1;
535 my $x65 = sequence(6,5)**1.1;
536 my $x74 = sequence(7,4)**1.1;
537 my $x75 = sequence(7,5)**1.1;
538
539 my $x64_ref = PDL::czip(pdl( [[360.467089670772225,+0.000000000000000],[-96.799893286902019,+103.123844918798000],[-99.028084766558067,+0.000000000000000],[-96.799893286902019,-103.123844918798000]],
540 [[-15.988207248715064,+28.700459428988125],[0.351953528236895,-1.568072536701052],[0.894706789826859,-1.030374425555896],[1.617577583003193,-0.729750250118165]],
541 [[-16.292902538160519,+9.547035210008918],[0.634591040891635,-0.785541150275863],[0.769581432672688,-0.358960145970985],[1.059338032001399,-0.013005581424496]],
542 [[-16.334882296969155,+0.000000000000000],[0.826035615718165,-0.382043067657264],[0.750315495608504,+0.000000000000000],[0.826035615718165,+0.382043067657264]],
543 [[-16.292902538160519,-9.547035210008918],[1.059338032001399,+0.013005581424496],[0.769581432672688,+0.358960145970985],[0.634591040891635,+0.785541150275863]],
544 [[-15.988207248715064,-28.700459428988125],[1.617577583003193,+0.729750250118165],[0.894706789826859,+1.030374425555896],[0.351953528236895,+1.568072536701052]] )->using(0,1))->t;
545
546 my $x65_ref = PDL::czip(pdl( [[581.176580714253532,+0.000000000000000],[-121.955619356066137,+180.656218696101774],[-126.233159544554283,+42.418129358506746],[-126.233159544554283,-42.418129358506746],[-121.955619356066137,-180.656218696101774]],
547 [[-20.541598254340641,+36.647109644693884],[0.166836245337382,-1.944952627138933],[0.717118890606079,-1.295587872699933],[1.204146586049559,-0.971137876719541],[2.048534847787472,-0.720103497369214]],
548 [[-20.868969490707826,+12.195700173607197],[0.644275726642125,-1.030653760454608],[0.749750248828911,-0.542566364608868],[0.913334629089297,-0.244339532151135],[1.274868845404011,+0.108769899029373]],
549 [[-20.913788738508799,+0.000000000000000],[0.937317766511587,-0.564249965530514],[0.811766871540105,-0.147421276996557],[0.811766871540105,+0.147421276996557],[0.937317766511587,+0.564249965530514]],
550 [[-20.868969490707826,-12.195700173607197],[1.274868845404011,-0.108769899029373],[0.913334629089297,+0.244339532151135],[0.749750248828911,+0.542566364608868],[0.644275726642125,+1.030653760454608]],
551 [[-20.541598254340641,-36.647109644693884],[2.048534847787472,+0.720103497369214],[1.204146586049559,+0.971137876719541],[0.717118890606079,+1.295587872699933],[0.166836245337382,+1.944952627138933]] )->using(0,1))->t;
552
553 my $x74_ref = PDL::czip(pdl( [[501.493394074383787,+0.000000000000000],[-133.959755823278442,+142.617956550047921],[-137.025115324985222,+0.000000000000000],[-133.959755823278442,-142.617956550047921]],
554 [[-18.865669184773111,+40.763000085858437],[0.303558154663907,-2.139893880507619],[1.092794692703986,-1.466640717037794],[2.101733986260737,-1.130389132503664]],
555 [[-19.293372490503472,+15.621007669166248],[0.660940909046470,-1.114601274898615],[0.916525999732684,-0.589874650055651],[1.356292422245748,-0.197237286031954]],
556 [[-19.369109179404475,+4.468560279899923],[0.870724656176023,-0.640911587295642],[0.881225310656959,-0.170789696788245],[1.069971171773709,+0.261394699800439]],
557 [[-19.369109179404475,-4.468560279899923],[1.069971171773709,-0.261394699800439],[0.881225310656959,+0.170789696788245],[0.870724656176023,+0.640911587295642]],
558 [[-19.293372490503472,-15.621007669166248],[1.356292422245748,+0.197237286031954],[0.916525999732684,+0.589874650055651],[0.660940909046470,+1.114601274898615]],
559 [[-18.865669184773111,-40.763000085858437],[2.101733986260737,+1.130389132503664],[1.092794692703986,+1.466640717037794],[0.303558154663907,+2.139893880507619]] )->using(0,1))->t;
560
561 my $x75_ref = PDL::czip(pdl( [[807.475069006401100,+0.000000000000000],[-168.753063158798682,+249.823189325119387],[-174.641491905125804,+58.661407474458414],[-174.641491905125804,-58.661407474458414],[-168.753063158798682,-249.823189325119387]],
562 [[-24.254907872104781,+52.050559234805071],[0.014162346363465,-2.632174401873477],[0.825892556600018,-1.808402549148552],[1.517875094286842,-1.417505033220904],[2.687499935923872,-1.159881805800431]],
563 [[-24.714356335554129,+19.955552964153426],[0.626228017088200,-1.433208444983354],[0.856153542379253,-0.823419842535374],[1.123969929564233,-0.469253829892776],[1.658488397174277,-0.080552774016870]],
564 [[-24.795144499563577,+5.709126404576390],[0.953872055406139,-0.885288945105194],[0.915476598274172,-0.361029355303055],[0.992228878787710,-0.012896599792635],[1.249581916098234,+0.447905615145083]],
565 [[-24.795144499563577,-5.709126404576390],[1.249581916098234,-0.447905615145083],[0.992228878787710,+0.012896599792635],[0.915476598274172,+0.361029355303055],[0.953872055406139,+0.885288945105194]],
566 [[-24.714356335554129,-19.955552964153426],[1.658488397174277,+0.080552774016870],[1.123969929564233,+0.469253829892776],[0.856153542379253,+0.823419842535374],[0.626228017088200,+1.433208444983354]],
567 [[-24.254907872104781,-52.050559234805071],[2.687499935923872,+1.159881805800431],[1.517875094286842,+1.417505033220904],[0.825892556600018,+1.808402549148552],[0.014162346363465,+2.632174401873477]] )->using(0,1))->t;
568
569
570 # forward
571 my $fx64 = rfft2($x64);
572 ok_should_make_plan( all( approx( $fx64, $x64_ref->slice('0:3'), approx_eps_double) ),
573 "rfft 2d test - forward - 6,4" );
574
575 my $fx65 = rfft2($x65);
576 ok_should_make_plan( all( approx( $fx65, $x65_ref->slice('0:3'), approx_eps_double) ),
577 "rfft 2d test - forward - 6,5" );
578
579 my $fx74 = rfft2($x74);
580 ok_should_make_plan( all( approx( $fx74, $x74_ref->slice('0:3'), approx_eps_double) ),
581 "rfft 2d test - forward - 7,4" );
582
583 my $fx75 = rfft2($x75);
584 ok_should_make_plan( all( approx( $fx75, $x75_ref->slice('0:3'), approx_eps_double) ),
585 "rfft 2d test - forward - 7,5" );
586
587 # backward
588 my $x64_back = irfft2($x64_ref->slice('0:3') );
589 ok_should_make_plan( all( approx( $x64, $x64_back, approx_eps_double) ),
590 "rfft 2d test - backward - 6,4 - output returned" );
591
592 $x64_back = zeros(6,4);
593 irfft2($x64_ref->slice('0:3'), $x64_back );
594 ok_should_reuse_plan( all( approx( $x64_back, $x64, approx_eps_double) ),
595 "rfft 2d test - backward - 6,4 - output in arglist" );
596
597 my $x65_back = irfft2($x65_ref->slice('0:3') );
598 ok_should_make_plan( all( approx( $x65_back, $x65, approx_eps_double) ),
599 "rfft 2d test - backward - 6,5 - output returned" );
600
601 $x65_back = zeros(6,5);
602 irfft2($x65_ref->slice('0:3'), $x65_back );
603 ok_should_reuse_plan( all( approx( $x65_back, $x65, approx_eps_double) ),
604 "rfft 2d test - backward - 6,5 - output in arglist" );
605
606 my $x74_back = irfft2($x74_ref->slice('0:3'), zeros(7,4) );
607 ok_should_make_plan( all( approx( $x74_back, $x74, approx_eps_double) ),
608 "rfft 2d test - backward - 7,4" );
609
610 my $x75_back = irfft2($x75_ref->slice('0:3'), zeros(7,5) );
611 ok_should_make_plan( all( approx( $x75_back, $x75, approx_eps_double) ),
612 "rfft 2d test - backward - 7,5" );
613 }
614
615 # real fft sanity checks
616 {
617 {
618 eval { rfft1( sequence(6) ) };
619 ok_should_reuse_plan( ! $@, "real ffts shouldn't work inplace - baseline forward" );
620 eval { rfft1( inplace sequence(6) ) };
621 ok( $@, "real ffts shouldn't work inplace - forward" );
622 eval { rNfft1( sequence(6) ) };
623 ok_should_reuse_plan( ! $@, "real->N ffts shouldn't work inplace - baseline forward" );
624 eval { rNfft1( inplace sequence(6) ) };
625 like $@, qr/in-place real FFTs are not supported/, "real->N ffts shouldn't work inplace - forward";
626 }
627
628 {
629 eval { irfft1( sequence(cdouble,4) ) };
630 ok_should_reuse_plan( ! $@, "real ffts shouldn't work inplace - baseline backward" );
631 eval { irfft1( inplace sequence(cdouble,4) ) };
632 like $@, qr/in-place real FFTs are not supported/, "real ffts shouldn't work inplace - backward";
633 }
634
635 {
636 eval { rfft1( sequence(7), sequence(cdouble,4) ) };
637 ok_should_reuse_plan( ! $@, "real fft dims - baseline");
638 eval { rfft1( sequence(7), sequence(cdouble,5) ) };
639 like $@, qr/mismatched first dimension/, "real fft dimensionality 1";
640 eval { rfft1( sequence(7), sequence(cdouble,3) ) };
641 like $@, qr/mismatched first dimension/, "real fft dimensionality 2";
642
643 eval { rNfft1( sequence(7), sequence(4) ) };
644 like $@, qr/produces complex output/, "real->N fft type";
645 eval { rNfft1( sequence(7), sequence(cdouble,4) ) };
646 ok_should_reuse_plan( ! $@, "real fft dims - baseline");
647 eval { rNfft1( sequence(7), sequence(cdouble,5) ) };
648 like $@, qr/mismatched first dimension/, "real->N fft dimensionality 1";
649 eval { rNfft1( sequence(7), sequence(cdouble,3) ) };
650 like $@, qr/mismatched first dimension/, "real->N fft dimensionality 2";
651 }
652
653 {
654 eval { rfft4( sequence(5,3,4,4) ) };
655 ok_should_make_plan( ! $@, "real dimensionality baseline" );
656
657 eval { rfft4( sequence(5,4,4) ) };
658 ok( $@, "real dimensionality: too few dimensions should fail" );
659
660 eval { irfft4( sequence(cdouble,5,3,4,4) ) };
661 ok_should_make_plan( ! $@, "real-backward dimensionality baseline" );
662
663 eval { irfft4( sequence(5,4,4) ) };
664 ok( $@, "real-backward dimensionality: too few dimensions should fail" );
665 }
666
667 {
668 eval { rfft4( sequence(5,3,4,4), sequence(cdouble,3,3,4,4) ) };
669 ok_should_reuse_plan( ! $@, "real dimensionality baseline - more explicit" );
670
671 eval { rfft4( sequence(5,3,4,4), sequence(cdouble,3,3,4,4,3) ) };
672 ok_should_reuse_plan( ! $@, "real dimensionality baseline - extra dims should be ok" );
673
674 eval { rfft4( sequence(5,3,4,4), sequence(cdouble,3,3,3,4,3) ) };
675 ok( $@, "real dimensionality - output should look like complex numbers" );
676
677 eval { rfft4( sequence(5,3,4,4), sequence(cdouble,3,3,4,3) ) };
678 ok( $@, "real dimensionality - different dims should break 1" );
679
680 eval { rfft4( sequence(5,3,4,4), sequence(cdouble,3,3,4,5) ) };
681 ok( $@, "real dimensionality - different dims should break 2" );
682
683 eval { rfft4( sequence(4,3,4,4), sequence(cdouble,3,3,4,4) ) };
684 ok_should_make_plan( !$@, "real dimensionality - slightly different complex dims still ok" );
685
686 eval { rfft4( sequence(6,3,4,4), sequence(cdouble,3,3,4,4) ) };
687 ok( $@, "real dimensionality - too different complex dims should break" );
688 }
689
690 {
691 eval { irfft4( sequence(cdouble,3,3,4,4) ) };
692 ok_should_make_plan( ! $@, "real-backward dimensionality baseline 1" );
693
694 eval { irfft4( sequence(cdouble,3,3,4,4), sequence(5,3,4,4) ) };
695 ok_should_make_plan( ! $@, "real-backward dimensionality baseline 2" );
696
697 eval { irfft4( sequence(cdouble,3,3,4,4), sequence(5,3,4,4,3) ) };
698 ok_should_reuse_plan( ! $@, "real-backward dimensionality baseline - extra dims should be ok" );
699
700 eval { irfft4( sequence(3,3,3,4,4,3), sequence(5,3,4,4) ) };
701 ok( $@, "real-backward dimensionality - input should look like complex numbers" );
702
703 eval { irfft4( sequence(cdouble,3,3,4,3), sequence(5,3,4,4) ) };
704 ok( $@, "real-backward dimensionality - different dims should break 1" );
705
706 eval { irfft4( sequence(cdouble,3,3,4,5), sequence(5,3,4,4) ) };
707 ok( $@, "real-backward dimensionality - different dims should break 2" );
708
709 eval { irfft4( sequence(cdouble,3,3,4,4), sequence(4,3,4,4) ) };
710 ok_should_reuse_plan( !$@, "real-backward dimensionality - slightly different complex dims still ok" );
711
712 eval { irfft4( sequence(cdouble,3,3,4,4), sequence(6,3,4,4) ) };
713 ok( $@, "real-backward dimensionality - too different complex dims should break" );
714 }
715 }
716
717 # make sure the routines do not damage their input
718 {
719 # I use all-new size to make sure a planning phase takes place. This checks
720 # the planning phase for preserving its input also
721 my $xorig = sequence(6,7,8);
722 my $Xorig = PDL::czip(sequence(2,6,7,8)->using(0,1));
723
724 my $X = $Xorig->copy;
725 fft2($X);
726 ok_should_make_plan( all( approx( $X, $Xorig, approx_eps_double) ),
727 "2D forward FFT preserves its input" );
728
729 $X = $Xorig->copy;
730 ifft2($X);
731 ok_should_make_plan( all( approx( $X, $Xorig, approx_eps_double) ),
732 "2D backward FFT preserves its input" );
733
734 my $x = $xorig->copy;
735 rfft3($x);
736 ok_should_make_plan( all( approx( $x, $xorig, approx_eps_double) ),
737 "3D real forward FFT preserves its input" );
738
739 $X = $Xorig->copy;
740 irfft3($X);
741 ok_should_make_plan( all( approx( $X, $Xorig, approx_eps_double) ),
742 "3D real backward FFT preserves its input" );
743 }
744
745 # Check parameterized operation
746 {
747 my $a = PDL::czip((sequence(2,4,4,4)==0)->using(0,1));
748 my $b = fftn($a,1);
749 my $btemplate = zeroes($a);
750 $btemplate->re->slice(':,(0),(0)') .= 1;
751 ok_should_make_plan( all( approx( $b, $btemplate, approx_eps_double ) ),
752 "parameterized forward complex FFT works (1d on a 1+3d var)" );
753
754 ok_should_reuse_plan( all( approx( $a->fftn(1), $btemplate, approx_eps_double ) ),
755 "parameterized forward complex FFT works (1d on a 1+3d var) - method" );
756
757 $b = fftn($a,2);
758 $btemplate .= 0;
759 $btemplate->re->slice(':,:,(0)') .= 1;
760 ok_should_make_plan( all( approx( $b, $btemplate, approx_eps_double ) ),
761 "parameterized forward complex FFT works (2d on a 1+3d var)" );
762
763 $b = fftn($a,3);
764 $btemplate .= 1;
765 ok_should_make_plan( all( approx( $b, $btemplate, approx_eps_double ) ),
766 "parameterized forward complex FFT works (3d on a 1+3d var)" );
767
768 # inverse on 2d -- should leave the 3rd dimension alone
769 my $c = ifftn($b,2);
770 my $ctemplate= zeroes($c);
771 $ctemplate->re->slice('(0),(0)') .= 1;
772 ok_should_make_plan( all( approx( $c, $ctemplate, approx_eps_double ) ),
773 "parameterized reverse complex FFT works (2d on a 1+3d var)" );
774
775 $a = sequence(4,4,4)==0;
776 $b = rfftn($a,1);
777 $btemplate = zeroes($b);
778 $btemplate->slice(':,(0),(0)') .= 1;
779 ok_should_make_plan( all( approx( $b, $btemplate, approx_eps_double ) ),
780 "parameterized forward real FFT works (1d on a 3d var)" );
781
782 # inverse on 2d -- should be a normalized forward on the non-transformed direction
783 $c = irfftn($b,2);
784 $ctemplate = zeroes($a);
785 $ctemplate->slice('(0),:,(0)') .= 0.25;
786 ok_should_make_plan( all( approx( $c, $ctemplate, approx_eps_double) ),
787 "parameterized reverse real FFT works (2d on a 3d var)" );
788 }
789
790 # Alignment checks. Here I try to fft purposely-unaligned ndarrays to make sure
791 # that PDL::FFTW3 both makes a new plan and computes the data correctly. I only
792 # create an input offset
793 #
794 # This test isn't written perfectly. It assumes that $x->copy is aligned at 16
795 # bytes, which is true on my amd64 box, but is not true in general.
796 # Additionally, FFTW crashes if alignment is particularly bad, so I disable
797 # these tests.
798 if(0)
799 {
800 my $x = PDL::czip(sequence(10), sequence(10)**2);
801
802 # from octave: conj( fft( (0:9) + i* ((0:9).**2) )' )
803 my $Xref = PDL::czip(pdl( [45.0000000000000,+285.0000000000000],
804 [-158.8841768587627,+17.7490974608742],
805 [-73.8190960235587,-28.6459544426446],
806 [-41.3271264002680,-38.7279671349711],
807 [-21.2459848116453,-42.8475374738350],
808 [-5.0000000000000,-45.0000000000000],
809 [11.2459848116453,-46.0967344361641],
810 [31.3271264002680,-45.9933924150247],
811 [63.8190960235587,-42.4097736473563],
812 [148.8841768587627,-13.0277379108784] )->using(0,1));
813
814 my $length = length ${$x->get_dataref};
815 for my $offset (4,8)
816 {
817 # this assumes a 16-aligned ndarray is created. This is true on my amd64 box
818 my $x_offset = $x->copy;
819
820 # create an offset in the data inside the ndarray
821 ${$x_offset->get_dataref} .= ' ' x $offset;
822 substr(${$x_offset->get_dataref}, 0, $offset) = "";
823 substr(${$x_offset->get_dataref}, 0) = ${$x->get_dataref};
824 $x_offset->upd_data();
825
826 ok_should_make_plan( all( approx( fft1($x_offset), $Xref, approx_eps_double) ),
827 "Basic 1D complex FFT - unaligned $offset bytes" );
828 }
829 }
830
831 done_testing;
832
833 sub ok_should_make_plan
834 {
835 my ($value, $planname) = @_;
836 local $Test::Builder::Level = $Test::Builder::Level + 1;
837 my $ret = ok( $value, $planname );
838 check_new_plan( $planname );
839 $ret;
840 }
841
842 sub ok_should_reuse_plan
843 {
844 my ($value, $planname) = @_;
845 local $Test::Builder::Level = $Test::Builder::Level + 1;
846 my $ret = ok( $value, $planname );
847 check_reused_plan( $planname );
848 $ret;
849 }
850
851 sub check_new_plan
852 {
853 my $planname = shift;
854
855 SKIP: {
856 skip "Plan creation checks disabled because pdl memory may be unaligned", 1 unless $do_check_plan_creations;
857 local $Test::Builder::Level = $Test::Builder::Level + 1;
858 ok( $PDL::FFTW3::_Nplans == $Nplans+1,
859 "$planname: should make a new plan" );
860 }
861
862 $Nplans = $PDL::FFTW3::_Nplans;
863 }
864
865 sub check_reused_plan
866 {
867 my $planname = shift;
868
869 SKIP: {
870 skip "Plan creation checks disabled because pdl memory may be unaligned", 1 unless $do_check_plan_creations;
871 local $Test::Builder::Level = $Test::Builder::Level + 1;
872 ok( $PDL::FFTW3::_Nplans == $Nplans,
873 "$planname: should reuse an existing plan" );
874 }
875 $Nplans = $PDL::FFTW3::_Nplans;
876 }