public static Polinomials operator *(Polinomials a, Polinomials b) { int n = 1; while (n < Math.Max(a.Length, b.Length)) { n <<= 1; } n <<= 1; double[] res = new double[n]; Complex[] p1 = a.Numbers.Select(x => new Complex(x, 0)).ToArray(); Array.Resize <Complex>(ref p1, n); Complex[] p2 = b.Numbers.Select(x => new Complex(x, 0)).ToArray(); Array.Resize <Complex>(ref p2, n); Fourier.FFT(ref p1, false); Fourier.FFT(ref p2, false); for (int i = 0; i < n; i++) { p1[i] *= p2[i]; } Fourier.FFT(ref p1, true); for (int i = 0; i < n; i++) { res[i] = Math.Round((p1[i].Real), 2); } return(new Polinomials(res)); }
public static void FFT(ref Complex[] polinomial, bool invert) { int l = polinomial.Length; if (l == 1) { return; } Complex[] p1 = new Complex[l / 2]; Complex[] p2 = new Complex[l / 2]; for (int i = 0; i < l / 2; i++) { p1[i] = polinomial[2 * i]; p2[i] = polinomial[2 * i + 1]; } Fourier.FFT(ref p1, invert); Fourier.FFT(ref p2, invert); double angle = 2 * Math.PI / l * (invert ? -1 : 1); Complex w = new Complex(1, 0); Complex wn = new Complex(Math.Cos(angle), Math.Sin(angle)); for (int i = 0; i < l / 2; i++) { polinomial[i] = p1[i] + w * p2[i]; polinomial[i + l / 2] = p1[i] - w * p2[i]; if (invert) { polinomial[i] /= 2; polinomial[i + l / 2] /= 2; } w *= wn; } }