|
| 1 | +//! Fast Fourier Transform (FFT) implementation ripped from: <https://github.com/bminaiev/rust-contests/blob/main/algo_lib/src/math/fft.rs> |
| 2 | +use std::ops::{Add, Mul, MulAssign, Sub}; |
| 3 | + |
| 4 | +#[derive(Copy, Clone)] |
| 5 | +struct Complex { |
| 6 | + real: f64, |
| 7 | + imag: f64, |
| 8 | +} |
| 9 | + |
| 10 | +impl Complex { |
| 11 | + const ZERO: Self = Complex { |
| 12 | + real: 0.0, |
| 13 | + imag: 0.0, |
| 14 | + }; |
| 15 | + const ONE: Self = Complex { |
| 16 | + real: 1.0, |
| 17 | + imag: 0.0, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +impl Mul for Complex { |
| 22 | + type Output = Complex; |
| 23 | + |
| 24 | + fn mul(self, rhs: Self) -> Self::Output { |
| 25 | + Self { |
| 26 | + real: self.real * rhs.real - self.imag * rhs.imag, |
| 27 | + imag: self.real * rhs.imag + self.imag * rhs.real, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl MulAssign for Complex { |
| 33 | + fn mul_assign(&mut self, rhs: Self) { |
| 34 | + *self = *self * rhs; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl Add for Complex { |
| 39 | + type Output = Complex; |
| 40 | + |
| 41 | + fn add(self, rhs: Self) -> Self::Output { |
| 42 | + Self { |
| 43 | + real: self.real + rhs.real, |
| 44 | + imag: self.imag + rhs.imag, |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Sub for Complex { |
| 50 | + type Output = Complex; |
| 51 | + |
| 52 | + fn sub(self, rhs: Self) -> Self::Output { |
| 53 | + Self { |
| 54 | + real: self.real - rhs.real, |
| 55 | + imag: self.imag - rhs.imag, |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn fft(a: &mut [Complex], invert: bool) { |
| 61 | + let n = a.len(); |
| 62 | + assert!(n.is_power_of_two()); |
| 63 | + let shift = usize::BITS - n.trailing_zeros(); |
| 64 | + for i in 1..n { |
| 65 | + let j = (i << shift).reverse_bits(); |
| 66 | + assert!(j < n); |
| 67 | + if i < j { |
| 68 | + a.swap(i, j); |
| 69 | + } |
| 70 | + } |
| 71 | + for len in (1..).map(|x| 1 << x).take_while(|s| *s <= n) { |
| 72 | + let half = len / 2; |
| 73 | + let alpha = std::f64::consts::PI * 2.0 / (len as f64); |
| 74 | + let cos = f64::cos(alpha); |
| 75 | + let sin = f64::sin(alpha) * (if invert { -1.0 } else { 1.0 }); |
| 76 | + let complex_angle = Complex { |
| 77 | + real: cos, |
| 78 | + imag: sin, |
| 79 | + }; |
| 80 | + for start in (0..n).step_by(len) { |
| 81 | + let mut mult = Complex::ONE; |
| 82 | + for j in 0..half { |
| 83 | + let u = a[start + j]; |
| 84 | + let v = a[start + half + j] * mult; |
| 85 | + a[start + j] = u + v; |
| 86 | + a[start + j + half] = u - v; |
| 87 | + mult *= complex_angle; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + if invert { |
| 92 | + for elem in a.iter_mut().take(n) { |
| 93 | + let n = n as f64; |
| 94 | + elem.imag /= n; |
| 95 | + elem.real /= n; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn fft_multiply_raw(mut a: Vec<Complex>, mut b: Vec<Complex>) -> Vec<Complex> { |
| 101 | + assert!(a.len().is_power_of_two()); |
| 102 | + assert!(b.len().is_power_of_two()); |
| 103 | + assert_eq!(a.len(), b.len()); |
| 104 | + fft(&mut a, false); |
| 105 | + fft(&mut b, false); |
| 106 | + for (x, y) in a.iter_mut().zip(b.iter()) { |
| 107 | + *x *= *y; |
| 108 | + } |
| 109 | + fft(&mut a, true); |
| 110 | + a |
| 111 | +} |
| 112 | + |
| 113 | +fn fft_multiply_complex(mut a: Vec<Complex>, mut b: Vec<Complex>) -> Vec<Complex> { |
| 114 | + let expected_size = (a.len() + b.len() - 1).next_power_of_two(); |
| 115 | + a.resize(expected_size, Complex::ZERO); |
| 116 | + b.resize(expected_size, Complex::ZERO); |
| 117 | + fft_multiply_raw(a, b) |
| 118 | +} |
| 119 | + |
| 120 | +/// # Example |
| 121 | +/// ``` |
| 122 | +/// use programming_team_code_rust::numbers::fft::fft_multiply; |
| 123 | +/// |
| 124 | +/// let a = vec![1.0, 2.0, 3.0]; |
| 125 | +/// let b = vec![4.0, 5.0, 6.0]; |
| 126 | +/// let c = fft_multiply(a, b); |
| 127 | +/// let expected = vec![4.0, 13.0, 28.0, 27.0, 18.0]; |
| 128 | +/// for (x, y) in c.iter().zip(expected.iter()) { |
| 129 | +/// assert!((x - y).abs() < 1e-15); |
| 130 | +/// } |
| 131 | +/// ``` |
| 132 | +/// |
| 133 | +/// # Complexity (n = max(a.len(), b.len())) |
| 134 | +/// Given for practical use cases, although consider it with a relatively large constant factor |
| 135 | +/// - Time: O(n log n) |
| 136 | +/// - Space: O(n) |
| 137 | +pub fn fft_multiply(a: Vec<f64>, b: Vec<f64>) -> Vec<f64> { |
| 138 | + let a: Vec<_> = a.iter().map(|&x| Complex { real: x, imag: 0.0 }).collect(); |
| 139 | + let b: Vec<_> = b.iter().map(|&x| Complex { real: x, imag: 0.0 }).collect(); |
| 140 | + fft_multiply_complex(a, b).iter().map(|c| c.real).collect() |
| 141 | +} |
0 commit comments