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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
//! Fourier transform module.
//! This is not a general purpose transform package but is designed to be
//! quick for arrays of length 2^n. It will work if the array length is of
//! the form 2^i * 3^j * 4^k * 5^l * 6^m (integer powers obviously).
//!
//! Minimal error-checking is performed by the code below. The only check is that
//! the initial factorisation can be performed.
//! Therefore if the transforms are called with an array of length <2, or a trig array
//! not matching the length of the array to be transformed the code will fail in a
//! spectacular way (eg. Seg. fault or nonsense returned).
//! It is up to the calling code to ensure everything is called sensibly.
//! The reason for stripping error checking is to speed up the backend by performing
//! less if() evaluations - as errors in practice seem to occur very rarely.
//! So the good news is this should be a fast library - the bad is that you may have to pick
//! around in it if there are failures.
//!
//! To initialise the routines call init(n,factors,trig,ierr).
//! This fills a factorisation array (factors), and a sin/cos array (trig).
//! These must be kept in memory by the calling program.
//! The init routine can be called multiple times with different arrays if more than
//! one length of array is to be transformed.
//! If a factorisation of the array length n cannot be found (as specified above)
//! then the init routine will exit immediately and the integer ierr will be set to 1.
//! If the init returns with ierr=0 then the call was successful.
//!
//! Top-level subroutines contained in this module are:
//! 1) initfft(n,factors,trig)        :
//!      Performs intialisation of the module, by working out the factors of n (the FFT length).
//!      This will fail if n is not factorised completely by 2,3,4,5,6.
//!      The trig array contains the necessary cosine and sine values.
//!      Both arrays passed to init **must** be kept between calls to routines in this module.
//! 2) forfft(m,n,x,trig,factors)  :
//!      This performs a FFT of an array x containing m vectors of length n.
//!      The transform length is n.
//!      This inverse of this transform is obtained by revfft.
//! 3) revfft(m,n,x,trig,factors)  :
//!      This performs an inverse FFT of an array x containing m vectors of length n.
//!      The transform length is n.
//!      This inverse of this transform is forfft.
//! 4) dct(m,n,x,trig,factors)     :
//!      This performs a discrete cosine transform of an array x containing m vectors of length n.
//!      The transform length is n.
//!      This routine calls forfft and performs pre- and post- processing to obtain the transform.
//!      This transform is it's own inverse.
//! 5) dst(m,n,x,trig,factors)     :
//!      This performs a discrete sine transform of an array x containing m vectors of length n.
//!      The transform length is n.
//!      This routine calls forfft and performs pre- and post- processing to obtain the transform.
//!      This transform is it's own inverse.
//!
//! The storage of the transformed array is in 'Hermitian form'. This means that, for the jth vector
//! the values x(j,1:nw) contain the cosine modes of the transform, while the values x(j,nw+1:n) contain
//! the sine modes (in reverse order ie. wave number increasing from n back to nw+1).
//! [Here, for even n, nw=n/2, and for odd n, nw=(n-1)/2].

use {crate::utils::*, core::f64::consts::PI, log::error, ndarray::ArrayViewMut2};

mod forward;
mod reverse;

pub use forward::*;
pub use reverse::*;

/// Subroutine performs initialisation work for all the transforms.
/// It calls routines to factorise the array length n and then sets up
/// a trig array full of sin/cos values used in the transform backend.
pub fn initfft(n: usize, factors: &mut [usize; 5], trig: &mut [f64]) {
    assert_eq!(2 * n, trig.len());

    let fac = [6, 4, 2, 3, 5];
    // First factorise n
    factorisen(n, factors);

    //Define constants needed in trig array definition
    let mut ftwopin = 2.0 * PI / (n as f64);
    let mut rem = n;
    let mut m = 1;
    for (i, element) in factors.iter().enumerate() {
        for _ in 1..=*element {
            rem /= fac[i];
            for k in 1..fac[i] {
                for l in 0..rem {
                    trig[m - 1] = ftwopin * ((k * l) as f64);
                    m += 1;
                }
            }
            ftwopin *= fac[i] as f64;
        }
    }

    for i in 1..n {
        trig[i + n - 1] = -(trig[i - 1].sin());
        trig[i - 1] = trig[i - 1].cos();
    }
}

pub fn factorisen(n: usize, factors: &mut [usize; 5]) {
    let mut rem = n;

    for elem in factors.iter_mut() {
        *elem = 0;
    }

    //Find factors of 6:
    while rem % 6 == 0 {
        factors[0] += 1;
        rem /= 6;
        if rem == 1 {
            return;
        }
    }

    //Find factors of 4:
    while rem % 4 == 0 {
        factors[1] += 1;
        rem /= 4;
        if rem == 1 {
            return;
        }
    }

    //Find factors of 2:
    while rem % 2 == 0 {
        factors[2] += 1;
        rem /= 2;
        if rem == 1 {
            return;
        }
    }
    //Find factors of 3:
    while rem % 3 == 0 {
        factors[3] += 1;
        rem /= 3;
        if rem == 1 {
            return;
        }
    }

    //Find factors of 5:
    while rem % 5 == 0 {
        factors[4] += 1;
        rem /= 5;
        if rem == 1 {
            return;
        }
    }

    error!("Factorization failed");
    quit::with_code(1);
}

/// Main physical to spectral (forward) FFT routine.
/// Performs m transforms of length n in the array x which is dimensioned x(m,n).
/// The arrays trig and factors are filled by the init routine and
/// should be kept from call to call.
/// Backend consists of mixed-radix routines, with 'decimation in time'.
/// Transform is stored in Hermitian form.
pub fn forfft(m: usize, n: usize, mut xs: ArrayViewMut2<f64>, trig: &[f64], factors: &[usize; 5]) {
    let xs = xs.as_slice_memory_order_mut().unwrap();

    assert_eq!(m * n, xs.len());
    assert_eq!(2 * n, trig.len());

    let normfac: f64;
    let mut wk = vec![0.0; m * n];

    let mut rem = n;
    let mut cum = 1;
    let mut iloc: usize;

    let mut orig = true;

    //Use factors of 5:
    for _ in 0..factors[4] {
        rem /= 5;
        iloc = (rem - 1) * 5 * cum;

        let cosine = view2d(&trig[iloc..], cum, 4);
        let sine = view2d(&trig[n + iloc..], cum, 4);

        if orig {
            let a = view3d(xs, m * rem, 5, cum);
            let b = viewmut3d(&mut wk, m * rem, cum, 5);
            forrdx5(a, b, m * rem, cum, cosine, sine);
        } else {
            let a = view3d(&wk, m * rem, 5, cum);
            let b = viewmut3d(xs, m * rem, cum, 5);
            forrdx5(a, b, m * rem, cum, cosine, sine);
        }

        orig = !orig;
        cum *= 5;
    }

    //Use factors of 3:
    for _ in 0..factors[3] {
        rem /= 3;
        iloc = (rem - 1) * 3 * cum;

        let cosine = view2d(&trig[iloc..], cum, 2);
        let sine = view2d(&trig[n + iloc..], cum, 2);

        if orig {
            let a = view3d(xs, m * rem, 3, cum);
            let b = viewmut3d(&mut wk, m * rem, cum, 3);
            forrdx3(a, b, m * rem, cum, cosine, sine);
        } else {
            let a = view3d(&wk, m * rem, 3, cum);
            let b = viewmut3d(xs, m * rem, cum, 3);
            forrdx3(a, b, m * rem, cum, cosine, sine);
        }

        orig = !orig;
        cum *= 3;
    }

    //Use factors of 2:
    for _ in 0..factors[2] {
        rem /= 2;
        iloc = (rem - 1) * 2 * cum;

        let cosine = view2d(&trig[iloc..], cum, 1);
        let sine = view2d(&trig[n + iloc..], cum, 1);

        if orig {
            let a = view3d(xs, m * rem, 2, cum);
            let b = viewmut3d(&mut wk, m * rem, cum, 2);
            forrdx2(a, b, m * rem, cum, cosine, sine);
        } else {
            let a = view3d(&wk, m * rem, 2, cum);
            let b = viewmut3d(xs, m * rem, cum, 2);
            forrdx2(a, b, m * rem, cum, cosine, sine);
        }

        orig = !orig;
        cum *= 2;
    }

    //Use factors of 4:
    for _ in 0..factors[1] {
        rem /= 4;
        iloc = (rem - 1) * 4 * cum;

        let cosine = view2d(&trig[iloc..], cum, 3);
        let sine = view2d(&trig[n + iloc..], cum, 3);

        if orig {
            let a = view3d(xs, m * rem, 4, cum);
            let b = viewmut3d(&mut wk, m * rem, cum, 4);
            forrdx4(a, b, m * rem, cum, cosine, sine);
        } else {
            let a = view3d(&wk, m * rem, 4, cum);
            let b = viewmut3d(xs, m * rem, cum, 4);
            forrdx4(a, b, m * rem, cum, cosine, sine);
        }

        orig = !orig;
        cum *= 4;
    }

    //Use factors of 6:
    for _ in 0..factors[0] {
        rem /= 6;
        iloc = (rem - 1) * 6 * cum;

        let cosine = view2d(&trig[iloc..], cum, 5);
        let sine = view2d(&trig[n + iloc..], cum, 5);

        if orig {
            let a = view3d(xs, m * rem, 6, cum);
            let b = viewmut3d(&mut wk, m * rem, cum, 6);
            forrdx6(a, b, m * rem, cum, cosine, sine);
        } else {
            let a = view3d(&wk, m * rem, 6, cum);
            let b = viewmut3d(xs, m * rem, cum, 6);
            forrdx6(a, b, m * rem, cum, cosine, sine);
        }

        orig = !orig;
        cum *= 6;
    }

    //Multiply by the normalisation constant and put
    //transformed array in the right location:
    normfac = 1.0 / (n as f64).sqrt();
    for (i, x) in xs.iter_mut().enumerate() {
        if orig {
            *x *= normfac;
        } else {
            *x = wk[i] * normfac;
        }
    }
}

/// Main spectral to physical (reverse) FFT routine.
/// Performs m reverse transforms of length n in the array x which is dimensioned x(m,n).
/// The arrays trig and factors are filled by the init routine and
/// should be kept from call to call.
/// Backend consists of mixed-radix routines, with 'decimation in frequency'.
/// Reverse transform starts in Hermitian form.
pub fn revfft(m: usize, n: usize, mut xs: ArrayViewMut2<f64>, trig: &[f64], factors: &[usize; 5]) {
    let xs = xs.as_slice_memory_order_mut().unwrap();

    assert_eq!(m * n, xs.len());
    assert_eq!(2 * n, trig.len());

    let normfac: f64;
    let mut wk = vec![0.0; m * n];

    let mut rem = n;
    let mut cum = 1;
    let mut iloc: usize;

    let mut orig = true;

    for elem in xs.iter_mut().skip((n / 2 + 1) * m) {
        *elem = -*elem;
    }

    //Scale 0 and Nyquist frequencies:
    for elem in xs.iter_mut().take(m) {
        *elem *= 0.5;
    }

    if n % 2 == 0 {
        let k = m * n / 2;
        for i in 0..m {
            xs[k + i] *= 0.5;
        }
    }

    //Use factors of 6:
    for _ in 0..factors[0] {
        rem /= 6;
        iloc = (cum - 1) * 6 * rem;

        let cosine = view2d(&trig[iloc..], rem, 5);
        let sine = view2d(&trig[n + iloc..], rem, 5);

        if orig {
            let a = view3d(xs, m * cum, rem, 6);
            let b = viewmut3d(&mut wk, m * cum, 6, rem);
            revrdx6(a, b, m * cum, rem, cosine, sine);
        } else {
            let a = view3d(&wk, m * cum, rem, 6);
            let b = viewmut3d(xs, m * cum, 6, rem);
            revrdx6(a, b, m * cum, rem, cosine, sine);
        }

        orig = !orig;
        cum *= 6;
    }

    //Use factors of 4:
    for _ in 0..factors[1] {
        rem /= 4;
        iloc = (cum - 1) * 4 * rem;

        let cosine = view2d(&trig[iloc..], rem, 3);
        let sine = view2d(&trig[n + iloc..], rem, 3);

        if orig {
            let a = view3d(xs, m * cum, rem, 4);
            let b = viewmut3d(&mut wk, m * cum, 4, rem);
            revrdx4(a, b, m * cum, rem, cosine, sine);
        } else {
            let a = view3d(&wk, m * cum, rem, 4);
            let b = viewmut3d(xs, m * cum, 4, rem);
            revrdx4(a, b, m * cum, rem, cosine, sine);
        }

        orig = !orig;
        cum *= 4;
    }

    //Use factors of 2:
    for _ in 0..factors[2] {
        rem /= 2;
        iloc = (cum - 1) * 2 * rem;

        let cosine = view2d(&trig[iloc..], rem, 1);
        let sine = view2d(&trig[n + iloc..], rem, 1);

        if orig {
            let a = view3d(xs, m * cum, rem, 2);
            let b = viewmut3d(&mut wk, m * cum, 2, rem);
            revrdx2(a, b, m * cum, rem, cosine, sine);
        } else {
            let a = view3d(&wk, m * cum, rem, 2);
            let b = viewmut3d(xs, m * cum, 2, rem);
            revrdx2(a, b, m * cum, rem, cosine, sine);
        }

        orig = !orig;
        cum *= 2;
    }

    //Use factors of 3:
    for _ in 0..factors[3] {
        rem /= 3;
        iloc = (cum - 1) * 3 * rem;

        let cosine = view2d(&trig[iloc..], rem, 2);
        let sine = view2d(&trig[n + iloc..], rem, 2);

        if orig {
            let a = view3d(xs, m * cum, rem, 3);
            let b = viewmut3d(&mut wk, m * cum, 3, rem);
            revrdx3(a, b, m * cum, rem, cosine, sine);
        } else {
            let a = view3d(&wk, m * cum, rem, 3);
            let b = viewmut3d(xs, m * cum, 3, rem);
            revrdx3(a, b, m * cum, rem, cosine, sine);
        }

        orig = !orig;
        cum *= 3;
    }

    //Use factors of 5:
    for _ in 0..factors[4] {
        rem /= 5;
        iloc = (cum - 1) * 5 * rem;

        let cosine = view2d(&trig[iloc..], rem, 4);
        let sine = view2d(&trig[n + iloc..], rem, 4);

        if orig {
            let a = view3d(xs, m * cum, rem, 5);
            let b = viewmut3d(&mut wk, m * cum, 5, rem);
            revrdx5(a, b, m * cum, rem, cosine, sine);
        } else {
            let a = view3d(&wk, m * cum, rem, 5);
            let b = viewmut3d(xs, m * cum, 5, rem);
            revrdx5(a, b, m * cum, rem, cosine, sine);
        }

        orig = !orig;
        cum *= 5;
    }

    //Multiply by the normalisation constant and put
    //transformed array in the right location:
    normfac = 2.0 / (n as f64).sqrt();
    for (i, x) in xs.iter_mut().enumerate() {
        if orig {
            *x *= normfac;
        } else {
            *x = wk[i] * normfac;
        }
    }
}

#[cfg(test)]
mod test {
    use {
        super::*,
        crate::array2_from_file,
        byteorder::{ByteOrder, NetworkEndian},
        insta::assert_debug_snapshot,
        ndarray::{Array2, ShapeBuilder},
    };

    #[test]
    fn factorisen_snapshot_1() {
        let n = 16;
        let mut factors = [0; 5];

        factorisen(n, &mut factors);

        assert_debug_snapshot!(factors);
    }

    #[test]
    fn factorisen_snapshot_2() {
        let n = 8640;
        let mut factors = [0; 5];

        factorisen(n, &mut factors);

        assert_debug_snapshot!(factors);
    }

    #[test]
    fn initfft_snapshot_1() {
        let n = 30;
        let mut factors = [0; 5];
        let mut trig = [0.0; 60];
        let trig2 = include_bytes!("testdata/initfft/30_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        initfft(n, &mut factors, &mut trig);

        assert_approx_eq_slice(&trig2, &trig);
    }

    #[test]
    fn initfft_snapshot_2() {
        let n = 32;
        let mut factors = [0; 5];
        let mut trig = [0.0; 64];
        let trig2 = include_bytes!("testdata/initfft/32_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        initfft(n, &mut factors, &mut trig);

        assert_approx_eq_slice(&trig2, &trig);
    }

    #[test]
    fn initfft_snapshot_3() {
        let n = 18;
        let mut factors = [0; 5];
        let mut trig = [0.0; 36];
        let trig2 = include_bytes!("testdata/initfft/18_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        initfft(n, &mut factors, &mut trig);

        assert_approx_eq_slice(&trig2, &trig);
    }

    #[test]
    fn forfft_ng12_1() {
        let m = 12;
        let n = 12;
        let mut x = array2_from_file!(12, 12, "testdata/forfft/forfft_ng12_1_x.bin");
        let x2 = array2_from_file!(12, 12, "testdata/forfft/forfft_ng12_1_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng12_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 1, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng12_2() {
        let m = 12;
        let n = 12;
        let mut x = array2_from_file!(12, 12, "testdata/forfft/forfft_ng12_2_x.bin");
        let x2 = array2_from_file!(12, 12, "testdata/forfft/forfft_ng12_2_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng12_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 1, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng15_1() {
        let m = 15;
        let n = 15;
        let mut x = array2_from_file!(15, 15, "testdata/forfft/forfft_ng15_1_x.bin");
        let x2 = array2_from_file!(15, 15, "testdata/forfft/forfft_ng15_1_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng15_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [0, 0, 0, 1, 1];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng15_2() {
        let m = 15;
        let n = 15;
        let mut x = array2_from_file!(15, 15, "testdata/forfft/forfft_ng15_2_x.bin");
        let x2 = array2_from_file!(15, 15, "testdata/forfft/forfft_ng15_2_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng15_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [0, 0, 0, 1, 1];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng16_1() {
        let m = 16;
        let n = 16;
        let mut x = array2_from_file!(16, 16, "testdata/forfft/forfft_ng16_1_x.bin");
        let x2 = array2_from_file!(16, 16, "testdata/forfft/forfft_ng16_1_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng16_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [0, 2, 0, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng16_2() {
        let m = 16;
        let n = 16;
        let mut x = array2_from_file!(16, 16, "testdata/forfft/forfft_ng16_2_x.bin");
        let x2 = array2_from_file!(16, 16, "testdata/forfft/forfft_ng16_2_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng16_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [0, 2, 0, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng18_1() {
        let m = 18;
        let n = 18;
        let mut x = array2_from_file!(18, 18, "testdata/forfft/forfft_ng18_1_x.bin");
        let x2 = array2_from_file!(18, 18, "testdata/forfft/forfft_ng18_1_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng18_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 0, 1, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng18_2() {
        let m = 18;
        let n = 18;
        let mut x = array2_from_file!(18, 18, "testdata/forfft/forfft_ng18_2_x.bin");
        let x2 = array2_from_file!(18, 18, "testdata/forfft/forfft_ng18_2_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng18_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 0, 1, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng24_1() {
        let m = 24;
        let n = 24;
        let mut x = array2_from_file!(24, 24, "testdata/forfft/forfft_ng24_1_x.bin");
        let x2 = array2_from_file!(24, 24, "testdata/forfft/forfft_ng24_1_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng24_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 1, 0, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn forfft_ng24_2() {
        let m = 24;
        let n = 24;
        let mut x = array2_from_file!(24, 24, "testdata/forfft/forfft_ng24_2_x.bin");
        let x2 = array2_from_file!(24, 24, "testdata/forfft/forfft_ng24_2_x2.bin");
        let trig = include_bytes!("testdata/forfft/forfft_ng24_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 1, 0, 0, 0];

        forfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng12_1() {
        let m = 12;
        let n = 12;
        let mut x = array2_from_file!(12, 12, "testdata/revfft/revfft_ng12_1_x.bin");
        let x2 = array2_from_file!(12, 12, "testdata/revfft/revfft_ng12_1_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng12_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 1, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng12_2() {
        let m = 12;
        let n = 12;
        let mut x = array2_from_file!(12, 12, "testdata/revfft/revfft_ng12_2_x.bin");
        let x2 = array2_from_file!(12, 12, "testdata/revfft/revfft_ng12_2_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng12_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();
        let factors = [1, 0, 1, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng15_1() {
        let m = 15;
        let n = 15;
        let mut x = array2_from_file!(15, 15, "testdata/revfft/revfft_ng15_1_x.bin");
        let x2 = array2_from_file!(15, 15, "testdata/revfft/revfft_ng15_1_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng15_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [0, 0, 0, 1, 1];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng15_2() {
        let m = 15;
        let n = 15;
        let mut x = array2_from_file!(15, 15, "testdata/revfft/revfft_ng15_2_x.bin");
        let x2 = array2_from_file!(15, 15, "testdata/revfft/revfft_ng15_2_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng15_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [0, 0, 0, 1, 1];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng16_1() {
        let m = 16;
        let n = 16;
        let mut x = array2_from_file!(16, 16, "testdata/revfft/revfft_ng16_1_x.bin");
        let x2 = array2_from_file!(16, 16, "testdata/revfft/revfft_ng16_1_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng16_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [0, 2, 0, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng16_2() {
        let m = 16;
        let n = 16;
        let mut x = array2_from_file!(16, 16, "testdata/revfft/revfft_ng16_2_x.bin");
        let x2 = array2_from_file!(16, 16, "testdata/revfft/revfft_ng16_2_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng16_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [0, 2, 0, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng18_1() {
        let m = 18;
        let n = 18;
        let mut x = array2_from_file!(18, 18, "testdata/revfft/revfft_ng18_1_x.bin");
        let x2 = array2_from_file!(18, 18, "testdata/revfft/revfft_ng18_1_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng18_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [1, 0, 0, 1, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng18_2() {
        let m = 18;
        let n = 18;
        let mut x = array2_from_file!(18, 18, "testdata/revfft/revfft_ng18_2_x.bin");
        let x2 = array2_from_file!(18, 18, "testdata/revfft/revfft_ng18_2_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng18_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [1, 0, 0, 1, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng24_1() {
        let m = 24;
        let n = 24;
        let mut x = array2_from_file!(24, 24, "testdata/revfft/revfft_ng24_1_x.bin");
        let x2 = array2_from_file!(24, 24, "testdata/revfft/revfft_ng24_1_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng24_1_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [1, 1, 0, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }

    #[test]
    fn revfft_ng24_2() {
        let m = 24;
        let n = 24;
        let mut x = array2_from_file!(24, 24, "testdata/revfft/revfft_ng24_2_x.bin");
        let x2 = array2_from_file!(24, 24, "testdata/revfft/revfft_ng24_2_x2.bin");
        let trig = include_bytes!("testdata/revfft/revfft_ng24_2_trig.bin")
            .chunks(8)
            .map(NetworkEndian::read_f64)
            .collect::<Vec<f64>>();

        let factors = [1, 1, 0, 0, 0];

        revfft(m, n, x.view_mut(), &trig, &factors);

        assert_eq!(x2, x);
    }
}