fn main() {
let one= Complex64::new(1.0, 0.0);
let zz = Complex64::new(0.0, 0.0);
let mut data = vec![one,one,one,one, zz,zz,zz,zz,];
let test = vec![one,one,one,one, zz,zz,zz,zz,];
fft (&mut data); // ifft (&mut data, DC);
let mut E = 0_u32; for (k,s) in data.iter().enumerate() { if (s -test[k]).norm() > 1.0e-8 { E += 1; } }
println!(" abs(ifft vs test data) Errors {}", E);
}
const PI: f64 = 3.14159265358979323846264338327950288_f64;
fn fft (x: &mut Vec<Complex64> )
{ let N = x.len() as usize; if N <= 1 { return ; }
let mut even = vec![Complex64::default(); N/2];
let mut odd = vec![Complex64::default(); N/2];
let mut k = 0_usize;
while k< N/2 { even.push(x[2*k]); odd.push(x[2*k+1]); k += 1; }
///////////
assert!( even[0]==x[0] && (odd[0]==x[1])) ;
///////////
fft( &mut even); fft( &mut odd);
// for k in 0..N/2
k= 0; let mut Ninv = 0.0_f64;
while k< N/2
{ let t= odd[k]* (Complex64::from_polar(1.0, -2.0*PI*Ninv) ).exp(); // (k as f64)/(N as f64)) ).exp();
x[k] = even[k] +t;
x[k +N/2]= even[k] -t;
Ninv += 1.0/(N as f64);
k += 1;
}
}
The code you have posted is incomplete (e.g. doesn't include import/definition of Complex64), and you have not included the error message, so I am not able to see the details of the problem.
Looks like you are comparing floating point numbers for equality. One should never do this because the same values calculated by different means using floating point are very likely to not be equal. I would expect your asserts to fail.
This perhaps counter intuitive result is due to the inherent inaccuracies of floating point, accumulation of rounding errors and so on.
Rather that copying odds and evens into separate arrays wouldn't it be easier, faster, to iterate over the elements with a stride: itertools::Stride itertools - Rust ?
I presume that code is supposed to be an implementation of the. recursive Cooley-Tukey as shown in the pseudo code here: Cooley–Tukey FFT algorithm - Wikipedia Where the function takes an array, a length and a stride.
Looks like it should be a nice exercise to implement that in Rust using the itertools::stride.
That's not true; reread my longer post. even[N/2] is a copy of x[0] and odd[N/2] is a copy of x[1]. I'm pretty sure you want Vec::with_capacity(N/2) and not vec![Complex64::default(); N/2] after running it on the playground.
Vec::with_capacity(N/2)
gets me passed the assert ...for reasons I dont understand...I don`t see how the assert passes ::with_cap.. and not the original stmt
...Also the final while { } has bugs...and I not sure about the re-cursive calls