feanor-math 3.5.18

A library for number theory, providing implementations for arithmetic in various rings and algorithms working on them.
Documentation
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
use std::alloc::Allocator;

use crate::algorithms::fft::complex_fft::*;
use crate::algorithms::fft::cooley_tuckey::CooleyTuckeyFFT;
use crate::algorithms::fft::radix3::CooleyTukeyRadix3FFT;
use crate::algorithms::fft::*;
use crate::algorithms::unity_root::is_prim_root_of_unity;
use crate::divisibility::DivisibilityRing;
use crate::homomorphism::*;
use crate::rings::float_complex::*;
use crate::seq::subvector::SubvectorView;

/// A generic variant of the Cooley-Tukey FFT algorithm that can be used to compute the Fourier
/// transform of an array of length `n1 * n2` given Fourier transforms for length `n1` resp. `n2`.
#[stability::unstable(feature = "enable")]
pub struct GeneralCooleyTukeyFFT<R_main, R_twiddle, H, T1, T2>
where
    R_main: ?Sized + RingBase,
    R_twiddle: ?Sized + RingBase,
    H: Homomorphism<R_twiddle, R_main>,
    T1: FFTAlgorithm<R_main>,
    T2: FFTAlgorithm<R_main>,
{
    twiddle_factors: Vec<R_twiddle::Element>,
    inv_twiddle_factors: Vec<R_twiddle::Element>,
    left_table: T1,
    right_table: T2,
    root_of_unity: R_main::Element,
    root_of_unity_twiddle: R_twiddle::Element,
    hom: H,
}

impl<R, T1, T2> GeneralCooleyTukeyFFT<R::Type, R::Type, Identity<R>, T1, T2>
where
    R: RingStore,
    T1: FFTAlgorithm<R::Type>,
    T2: FFTAlgorithm<R::Type>,
{
    /// Creates a new [`GeneralCooleyTukeyFFT`] over the given ring of length `n`, based on FFTs
    /// of length `n1` and `n2`, where `n = n1 * n2`.
    ///
    /// The closure `root_of_unity_pows` should, on input `i`, return `z^i` for the primitive `n`-th
    /// root of unity `z` satisfying `z^n1 == right_table.root_of_unity()` and `z^n2 -
    /// left_table.root_of_unity()`, where `n1` and `n2` are the lengths of `left_table` and
    /// `right_table`, respectively.
    #[stability::unstable(feature = "enable")]
    pub fn new_with_pows<F>(ring: R, root_of_unity_pows: F, left_table: T1, right_table: T2) -> Self
    where
        F: FnMut(i64) -> El<R>,
    {
        Self::new_with_pows_with_hom(ring.into_identity(), root_of_unity_pows, left_table, right_table)
    }

    /// Creates a new [`GeneralCooleyTukeyFFT`] over the given ring of length `n`, based on FFTs
    /// of length `n1` and `n2`, where `n = n1 * n2`.
    ///
    /// The given root of unity should be the primitive `n`-th root of unity satisfying
    /// `root_of_unity^n1 == right_table.root_of_unity()` and `root_of_unity^n2 -
    /// left_table.root_of_unity()`, where `n1` and `n2` are the lengths of `left_table` and
    /// `right_table`, respectively.
    ///
    /// Do not use this for approximate rings, as computing the powers of `root_of_unity`
    /// will incur avoidable precision loss.
    #[stability::unstable(feature = "enable")]
    pub fn new(ring: R, root_of_unity: El<R>, left_table: T1, right_table: T2) -> Self {
        Self::new_with_hom(ring.into_identity(), root_of_unity, left_table, right_table)
    }
}

impl<R_main, R_twiddle, H, A1, A2>
    GeneralCooleyTukeyFFT<
        R_main,
        R_twiddle,
        H,
        CooleyTukeyRadix3FFT<R_main, R_twiddle, H, A1>,
        CooleyTuckeyFFT<R_main, R_twiddle, H, A2>,
    >
where
    R_main: ?Sized + RingBase,
    R_twiddle: ?Sized + RingBase + DivisibilityRing,
    H: Homomorphism<R_twiddle, R_main>,
    A1: Allocator + Clone,
    A2: Allocator + Clone,
{
    /// Replaces the ring that this object can compute FFTs over, assuming that the current
    /// twiddle factors can be mapped into the new ring with the given homomorphism.
    ///
    /// In particular, this function does not recompute twiddles, but uses a different
    /// homomorphism to map the current twiddles into a new ring. Hence, it is extremely
    /// cheap.
    #[stability::unstable(feature = "enable")]
    pub fn change_ring<R_new: ?Sized + RingBase, H_new: Clone + Homomorphism<R_twiddle, R_new>>(
        self,
        new_hom: H_new,
    ) -> (
        GeneralCooleyTukeyFFT<
            R_new,
            R_twiddle,
            H_new,
            CooleyTukeyRadix3FFT<R_new, R_twiddle, H_new, A1>,
            CooleyTuckeyFFT<R_new, R_twiddle, H_new, A2>,
        >,
        H,
    ) {
        let ring = new_hom.codomain();
        let root_of_unity = new_hom.map_ref(&self.root_of_unity_twiddle);
        assert!(ring.is_commutative());
        assert!(ring.get_ring().is_approximate() || is_prim_root_of_unity(&ring, &root_of_unity, self.len()));

        return (
            GeneralCooleyTukeyFFT {
                twiddle_factors: self.twiddle_factors,
                left_table: self.left_table.change_ring(new_hom.clone()).0,
                right_table: self.right_table.change_ring(new_hom.clone()).0,
                inv_twiddle_factors: self.inv_twiddle_factors,
                root_of_unity,
                root_of_unity_twiddle: self.root_of_unity_twiddle,
                hom: new_hom,
            },
            self.hom,
        );
    }
}

impl<R_main, R_twiddle, H, T1, T2> GeneralCooleyTukeyFFT<R_main, R_twiddle, H, T1, T2>
where
    R_main: ?Sized + RingBase,
    R_twiddle: ?Sized + RingBase,
    H: Homomorphism<R_twiddle, R_main>,
    T1: FFTAlgorithm<R_main>,
    T2: FFTAlgorithm<R_main>,
{
    /// Creates a new [`GeneralCooleyTukeyFFT`] over the given ring of length `n`, based on FFTs
    /// of length `n1` and `n2`, where `n = n1 * n2`.
    ///
    /// The closure `root_of_unity_pows` should, on input `i`, return `z^i` for the primitive `n`-th
    /// root of unity `z` satisfying `z^n1 == right_table.root_of_unity()` and `z^n2 -
    /// left_table.root_of_unity()`, where `n1` and `n2` are the lengths of `left_table` and
    /// `right_table`, respectively.
    ///
    /// Instead of a ring, this function takes a homomorphism `R -> S`. Twiddle factors that are
    /// precomputed will be stored as elements of `R`, while the main FFT computations will be
    /// performed in `S`. This allows both implicit ring conversions, and using patterns like
    /// [`zn_64::ZnFastmul`] to precompute some data for better performance.
    #[stability::unstable(feature = "enable")]
    pub fn new_with_pows_with_hom<F>(hom: H, root_of_unity_pows: F, left_table: T1, right_table: T2) -> Self
    where
        F: FnMut(i64) -> R_twiddle::Element,
    {
        Self::create(hom, root_of_unity_pows, left_table, right_table)
    }

    /// Most general way to create a [`GeneralCooleyTukeyFFT`]. Currently equivalent to
    /// [`GeneralCooleyTukeyFFT::new_with_pows_with_hom()`].
    #[stability::unstable(feature = "enable")]
    pub fn create<F>(hom: H, mut root_of_unity_pows: F, left_table: T1, right_table: T2) -> Self
    where
        F: FnMut(i64) -> R_twiddle::Element,
    {
        let ring = hom.codomain();

        assert!(ring.is_commutative());
        assert!(
            ring.get_ring().is_approximate()
                || is_prim_root_of_unity(
                    ring,
                    &hom.map(root_of_unity_pows(1)),
                    left_table.len() * right_table.len()
                )
        );
        assert!(
            ring.get_ring().is_approximate()
                || ring.eq_el(
                    &hom.map(root_of_unity_pows(right_table.len().try_into().unwrap())),
                    left_table.root_of_unity(ring)
                )
        );
        assert!(
            ring.get_ring().is_approximate()
                || ring.eq_el(
                    &hom.map(root_of_unity_pows(left_table.len().try_into().unwrap())),
                    right_table.root_of_unity(ring)
                )
        );

        let root_of_unity = root_of_unity_pows(1);
        let inv_twiddle_factors = Self::create_twiddle_factors(|i| root_of_unity_pows(-i), &left_table, &right_table);
        let twiddle_factors = Self::create_twiddle_factors(root_of_unity_pows, &left_table, &right_table);

        GeneralCooleyTukeyFFT {
            twiddle_factors,
            inv_twiddle_factors,
            left_table,
            right_table,
            root_of_unity: hom.map_ref(&root_of_unity),
            root_of_unity_twiddle: root_of_unity,
            hom,
        }
    }

    /// Returns the length-`n1` FFT used by this object to compute length-`n` FFTs.
    #[stability::unstable(feature = "enable")]
    pub fn left_fft_table(&self) -> &T1 { &self.left_table }

    /// Returns the length-`n2` FFT used by this object to compute length-`n` FFTs.
    #[stability::unstable(feature = "enable")]
    pub fn right_fft_table(&self) -> &T2 { &self.right_table }

    /// Returns the homomorphism used to map twiddle factors into the main
    /// ring during the computation of FFTs.
    #[stability::unstable(feature = "enable")]
    pub fn hom<'a>(&'a self) -> &'a H { &self.hom }

    /// Creates a new [`GeneralCooleyTukeyFFT`] over the given ring of length `n`, based on FFTs
    /// of length `n1` and `n2`, where `n = n1 * n2`.
    ///
    /// The given root of unity should be the primitive `n`-th root of unity satisfying
    /// `root_of_unity^n1 == right_table.root_of_unity()` and `root_of_unity^n2 -
    /// left_table.root_of_unity()`, where `n1` and `n2` are the lengths of `left_table` and
    /// `right_table`, respectively.
    ///
    /// Instead of a ring, this function takes a homomorphism `R -> S`. Twiddle factors that are
    /// precomputed will be stored as elements of `R`, while the main FFT computations will be
    /// performed in `S`. This allows both implicit ring conversions, and using patterns like
    /// [`zn_64::ZnFastmul`] to precompute some data for better performance.
    ///
    /// Do not use this for approximate rings, as computing the powers of `root_of_unity`
    /// will incur avoidable precision loss.
    #[stability::unstable(feature = "enable")]
    pub fn new_with_hom(hom: H, root_of_unity: R_twiddle::Element, left_table: T1, right_table: T2) -> Self {
        let len = left_table.len() * right_table.len();
        let root_of_unity_pows = |i: i64| {
            if i >= 0 {
                hom.domain()
                    .pow(hom.domain().clone_el(&root_of_unity), i.try_into().unwrap())
            } else {
                let len_i64: i64 = len.try_into().unwrap();
                hom.domain().pow(
                    hom.domain().clone_el(&root_of_unity),
                    (len_i64 + (i % len_i64)).try_into().unwrap(),
                )
            }
        };
        let result = GeneralCooleyTukeyFFT::create(&hom, root_of_unity_pows, left_table, right_table);
        GeneralCooleyTukeyFFT {
            twiddle_factors: result.twiddle_factors,
            inv_twiddle_factors: result.inv_twiddle_factors,
            left_table: result.left_table,
            right_table: result.right_table,
            root_of_unity: result.root_of_unity,
            root_of_unity_twiddle: result.root_of_unity_twiddle,
            hom,
        }
    }

    fn create_twiddle_factors<F>(
        mut root_of_unity_pows: F,
        left_table: &T1,
        right_table: &T2,
    ) -> Vec<R_twiddle::Element>
    where
        F: FnMut(i64) -> R_twiddle::Element,
    {
        (0..(left_table.len() * right_table.len()))
            .map(|i| {
                let ri: i64 = (i % right_table.len()).try_into().unwrap();
                let li = i / right_table.len();
                return root_of_unity_pows(
                    TryInto::<i64>::try_into(left_table.unordered_fft_permutation(li)).unwrap() * ri,
                );
            })
            .collect::<Vec<_>>()
    }

    /// Returns the ring over which this object can compute FFTs.
    #[stability::unstable(feature = "enable")]
    pub fn ring<'a>(&'a self) -> &'a <H as Homomorphism<R_twiddle, R_main>>::CodomainStore { self.hom.codomain() }
}

impl<R_main, R_twiddle, H, T1, T2> PartialEq for GeneralCooleyTukeyFFT<R_main, R_twiddle, H, T1, T2>
where
    R_main: ?Sized + RingBase,
    R_twiddle: ?Sized + RingBase,
    H: Homomorphism<R_twiddle, R_main>,
    T1: FFTAlgorithm<R_main> + PartialEq,
    T2: FFTAlgorithm<R_main> + PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.ring().get_ring() == other.ring().get_ring()
            && self.left_table == other.left_table
            && self.right_table == other.right_table
            && self
                .ring()
                .eq_el(self.root_of_unity(self.ring()), other.root_of_unity(self.ring()))
    }
}

impl<R_main, R_twiddle, H, T1, T2> FFTAlgorithm<R_main> for GeneralCooleyTukeyFFT<R_main, R_twiddle, H, T1, T2>
where
    R_main: ?Sized + RingBase,
    R_twiddle: ?Sized + RingBase,
    H: Homomorphism<R_twiddle, R_main>,
    T1: FFTAlgorithm<R_main>,
    T2: FFTAlgorithm<R_main>,
{
    fn len(&self) -> usize { self.left_table.len() * self.right_table.len() }

    fn root_of_unity<S: RingStore<Type = R_main> + Copy>(&self, ring: S) -> &R_main::Element {
        assert!(self.ring().get_ring() == ring.get_ring(), "unsupported ring");
        &self.root_of_unity
    }

    fn unordered_fft<V, S>(&self, mut values: V, ring: S)
    where
        V: SwappableVectorViewMut<<R_main as RingBase>::Element>,
        S: RingStore<Type = R_main> + Copy,
    {
        assert!(self.ring().get_ring() == ring.get_ring(), "unsupported ring");
        if self.left_table.len() > 1 {
            for i in 0..self.right_table.len() {
                let mut v = SubvectorView::new(&mut values)
                    .restrict(i..)
                    .step_by_view(self.right_table.len());
                self.left_table.unordered_fft(&mut v, ring);
            }
            for i in 0..self.len() {
                self.hom
                    .mul_assign_ref_map(values.at_mut(i), self.inv_twiddle_factors.at(i));
            }
        }
        for i in 0..self.left_table.len() {
            let mut v = SubvectorView::new(&mut values)
                .restrict((i * self.right_table.len())..((i + 1) * self.right_table.len()));
            self.right_table.unordered_fft(&mut v, ring);
        }
    }

    fn unordered_inv_fft<V, S>(&self, mut values: V, ring: S)
    where
        V: SwappableVectorViewMut<<R_main as RingBase>::Element>,
        S: RingStore<Type = R_main> + Copy,
    {
        assert!(self.ring().get_ring() == ring.get_ring(), "unsupported ring");
        for i in 0..self.left_table.len() {
            let mut v = SubvectorView::new(&mut values)
                .restrict((i * self.right_table.len())..((i + 1) * self.right_table.len()));
            self.right_table.unordered_inv_fft(&mut v, ring);
        }
        if self.left_table.len() > 1 {
            for i in 0..self.len() {
                self.hom
                    .mul_assign_ref_map(values.at_mut(i), self.twiddle_factors.at(i));
                debug_assert!(
                    self.ring().get_ring().is_approximate()
                        || self.hom.domain().is_one(
                            &self
                                .hom
                                .domain()
                                .mul_ref(self.twiddle_factors.at(i), self.inv_twiddle_factors.at(i))
                        )
                );
            }
            for i in 0..self.right_table.len() {
                let mut v = SubvectorView::new(&mut values)
                    .restrict(i..)
                    .step_by_view(self.right_table.len());
                self.left_table.unordered_inv_fft(&mut v, ring);
            }
        }
    }

    fn unordered_fft_permutation(&self, i: usize) -> usize {
        assert!(i < self.len());
        self.left_table.unordered_fft_permutation(i / self.right_table.len())
            + self.left_table.len() * self.right_table.unordered_fft_permutation(i % self.right_table.len())
    }

    fn unordered_fft_permutation_inv(&self, i: usize) -> usize {
        assert!(i < self.len());
        self.left_table.unordered_fft_permutation_inv(i % self.left_table.len()) * self.right_table.len()
            + self
                .right_table
                .unordered_fft_permutation_inv(i / self.left_table.len())
    }
}

impl<H, T1, T2> FFTErrorEstimate for GeneralCooleyTukeyFFT<Complex64Base, Complex64Base, H, T1, T2>
where
    H: Homomorphism<Complex64Base, Complex64Base>,
    T1: FFTAlgorithm<Complex64Base> + FFTErrorEstimate,
    T2: FFTAlgorithm<Complex64Base> + FFTErrorEstimate,
{
    fn expected_absolute_error(&self, input_bound: f64, input_error: f64) -> f64 {
        let error_after_first_fft = self.left_table.expected_absolute_error(input_bound, input_error);
        let new_input_bound = self.left_table.len() as f64 * input_bound;
        let error_after_twiddling = error_after_first_fft + new_input_bound * (root_of_unity_error() + f64::EPSILON);
        return self
            .right_table
            .expected_absolute_error(new_input_bound, error_after_twiddling);
    }
}

#[cfg(test)]
use std::alloc::Global;

#[cfg(test)]
use crate::algorithms::fft::bluestein::BluesteinFFT;
#[cfg(test)]
use crate::algorithms::unity_root::*;
#[cfg(test)]
use crate::rings::zn::zn_64;
#[cfg(test)]
use crate::rings::zn::zn_static::{Fp, Zn};

#[test]
fn test_fft_basic() {
    let ring = Zn::<97>::RING;
    let z = ring.int_hom().map(39);
    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 16),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 48), 1),
        BluesteinFFT::new(ring, ring.pow(z, 16), ring.pow(z, 12), 3, 3, Global),
    );
    let mut values = [1, 0, 0, 1, 0, 1];
    let expected = [3, 62, 63, 96, 37, 36];
    let mut permuted_expected = [0; 6];
    for i in 0..6 {
        permuted_expected[i] = expected[fft.unordered_fft_permutation(i)];
    }

    fft.unordered_fft(&mut values, ring);
    assert_eq!(values, permuted_expected);
}

#[test]
fn test_fft_long() {
    let ring = Fp::<97>::RING;
    let z = ring.int_hom().map(39);
    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 4),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 12), 3),
        BluesteinFFT::new(ring, ring.pow(z, 16), ring.pow(z, 12), 3, 3, Global),
    );
    let mut values = [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 2, 2, 0, 2, 0, 1, 2, 3, 4];
    let expected = [
        26, 0, 75, 47, 41, 31, 28, 62, 39, 93, 53, 27, 0, 54, 74, 61, 65, 81, 63, 38, 53, 94, 89, 91,
    ];
    let mut permuted_expected = [0; 24];
    for i in 0..24 {
        permuted_expected[i] = expected[fft.unordered_fft_permutation(i)];
    }

    fft.unordered_fft(&mut values, ring);
    assert_eq!(values, permuted_expected);
}

#[test]
fn test_fft_unordered() {
    let ring = Fp::<1409>::RING;
    let z = get_prim_root_of_unity(ring, 64 * 11).unwrap();
    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 4),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 44), 4),
        BluesteinFFT::new(ring, ring.pow(z, 32), ring.pow(z, 22), 11, 5, Global),
    );
    const LEN: usize = 16 * 11;
    let mut values = [0; LEN];
    for i in 0..LEN {
        values[i] = ring.int_hom().map(i as i32);
    }
    let original = values;

    fft.unordered_fft(&mut values, ring);

    let mut ordered_fft = [0; LEN];
    for i in 0..LEN {
        ordered_fft[fft.unordered_fft_permutation(i)] = values[i];
    }

    fft.unordered_inv_fft(&mut values, ring);
    assert_eq!(values, original);

    fft.inv_fft(&mut ordered_fft, ring);
    assert_eq!(ordered_fft, original);
}

#[test]
fn test_unordered_fft_permutation_inv() {
    let ring = Fp::<1409>::RING;
    let z = get_prim_root_of_unity(ring, 64 * 11).unwrap();
    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 4),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 44), 4),
        BluesteinFFT::new(ring, ring.pow(z, 32), ring.pow(z, 22), 11, 5, Global),
    );
    for i in 0..(16 * 11) {
        assert_eq!(fft.unordered_fft_permutation_inv(fft.unordered_fft_permutation(i)), i);
        assert_eq!(fft.unordered_fft_permutation(fft.unordered_fft_permutation_inv(i)), i);
    }

    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 4),
        BluesteinFFT::new(ring, ring.pow(z, 32), ring.pow(z, 22), 11, 5, Global),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 44), 4),
    );
    for i in 0..(16 * 11) {
        assert_eq!(fft.unordered_fft_permutation_inv(fft.unordered_fft_permutation(i)), i);
        assert_eq!(fft.unordered_fft_permutation(fft.unordered_fft_permutation_inv(i)), i);
    }
}

#[test]
fn test_inv_fft() {
    let ring = Fp::<97>::RING;
    let z = ring.int_hom().map(39);
    let fft = GeneralCooleyTukeyFFT::new(
        ring,
        ring.pow(z, 16),
        CooleyTuckeyFFT::new(ring, ring.pow(z, 16 * 3), 1),
        BluesteinFFT::new(ring, ring.pow(z, 16), ring.pow(z, 12), 3, 3, Global),
    );
    let mut values = [3, 62, 63, 96, 37, 36];
    let expected = [1, 0, 0, 1, 0, 1];

    fft.inv_fft(&mut values, ring);
    assert_eq!(values, expected);
}

#[test]
fn test_approximate_fft() {
    let CC = Complex64::RING;
    for (p, log2_n) in [(5, 3), (53, 5), (101, 8), (503, 10)] {
        let fft = GeneralCooleyTukeyFFT::new_with_pows(
            CC,
            |i| CC.root_of_unity(i, TryInto::<i64>::try_into(p).unwrap() << log2_n),
            BluesteinFFT::for_complex(CC, p, Global),
            CooleyTuckeyFFT::for_complex(CC, log2_n),
        );
        let mut array = (0..(p << log2_n))
            .map(|i| CC.root_of_unity(i.try_into().unwrap(), TryInto::<i64>::try_into(p).unwrap() << log2_n))
            .collect::<Vec<_>>();
        fft.fft(&mut array, CC);
        let err = fft.expected_absolute_error(1.0, 0.0);
        assert!(CC.is_absolute_approx_eq(array[0], CC.zero(), err));
        assert!(CC.is_absolute_approx_eq(array[1], CC.from_f64(fft.len() as f64), err));
        for i in 2..fft.len() {
            assert!(CC.is_absolute_approx_eq(array[i], CC.zero(), err));
        }
    }
}

#[cfg(test)]
const BENCH_N1: usize = 31;
#[cfg(test)]
const BENCH_N2: usize = 601;

#[bench]
fn bench_factor_fft(bencher: &mut test::Bencher) {
    let ring = zn_64::Zn::new(1602564097);
    let fastmul_ring = zn_64::ZnFastmul::new(ring).unwrap();
    let embedding = ring.can_hom(&fastmul_ring).unwrap();
    let root_of_unity = fastmul_ring.coerce(&ring, get_prim_root_of_unity_zn(&ring, 2 * 31 * 601).unwrap());
    let fft = GeneralCooleyTukeyFFT::new_with_hom(
        embedding.clone(),
        fastmul_ring.pow(root_of_unity, 2),
        BluesteinFFT::new_with_hom(
            embedding.clone(),
            fastmul_ring.pow(root_of_unity, BENCH_N1),
            get_prim_root_of_unity_zn(&fastmul_ring, 1 << 11).unwrap(),
            BENCH_N2,
            11,
            Global,
        ),
        BluesteinFFT::new_with_hom(
            embedding,
            fastmul_ring.pow(root_of_unity, BENCH_N2),
            get_prim_root_of_unity_zn(&fastmul_ring, 1 << 6).unwrap(),
            BENCH_N1,
            6,
            Global,
        ),
    );
    let data = (0..(BENCH_N1 * BENCH_N2))
        .map(|i| ring.int_hom().map(i as i32))
        .collect::<Vec<_>>();
    let mut copy = Vec::with_capacity(BENCH_N1 * BENCH_N2);
    bencher.iter(|| {
        copy.clear();
        copy.extend(data.iter().map(|x| ring.clone_el(x)));
        fft.unordered_fft(&mut copy[..], &ring);
        fft.unordered_inv_fft(&mut copy[..], &ring);
        assert_el_eq!(ring, copy[0], data[0]);
    });
}