Esempio n. 1
0
        public static bool AreAlmostEquival(Polynom p, Polynom q, bool output = false)
        {
            p = p.Clone();
            q = q.Clone();
            p.Trim();
            q.Trim();

            if (p.Count != q.Count)
            {
                if (output)
                {
                    Console.WriteLine("p.Count: {0},\nq.Count: {1}\n", p.Count, q.Count);
                }
                return(false);
            }
            for (int i = 0; i < p.Count; i++)
            {
                if (!ComplexUtils.IsAlmostEqual(p[i], q[i]))
                {
                    if (output)
                    {
                        Console.WriteLine("p[i]: {0},\nq[i]: {1},\ni: {2}\n", p[i], q[i], i);
                    }
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Make recursive FFT
        /// </summary>
        /// <param name="a">Polynom. Number of items must be pow of two</param>
        /// <param name="omega">Proper sqrt of 1</param>
        /// <returns></returns>
        public static Polynom RecursiveFFT(Polynom a, Complex omega)
        {
            if (ComplexUtils.IsAlmostEqual(omega, 1))
            {
                return(a);
            }
            Polynom a_s = new Polynom();
            Polynom a_l = new Polynom();

            for (int i = 0; i < a.Count / 2; i++)
            {
                a_s.Add(a[2 * i]);
                a_l.Add(a[2 * i + 1]);
            }

            Polynom s = RecursiveFFT(a_s, omega * omega);
            Polynom l = RecursiveFFT(a_l, omega * omega);

            Complex x   = 1;
            Polynom ret = new Polynom(a.Count);

            for (int i = 0; i < a.Count / 2; i++)
            {
                ret[i] = s[i] + x * l[i];
                ret[i + a.Count / 2] = s[i] - x * l[i];
                x *= omega;
            }
            return(ret);
        }
Esempio n. 3
0
 // Remove null members at the end
 public void Trim()
 {
     while (this.Count > 0 && ComplexUtils.IsAlmostEqual(this[this.Count - 1], 0))
     {
         this.RemoveAt(this.Count - 1);
     }
 }