/// <summary>
        /// Gets roots of real polynom
        /// </summary>
        /// <param name="p">Coefficients of real polynom</param>
        /// <returns>Complex roots</returns>
        public static Complex[] GetRoots(double[] p)
        {
            ComplexPolynom pol = new ComplexPolynom(p);

            Complex[] c = pol.Roots(1e-15);
            return(c);
        }
Exemple #2
0
        /// <summary>
        /// Finds all roots of polynom
        /// </summary>
        /// <param name="accuracy">Accuracy</param>
        /// <returns>Array of roots</returns>
        public Complex[] Roots(double accuracy)
        {
            if (Deg == 1)
            {
                return(new Complex[] { (-coeff[0] / coeff[1]) });
            }
            if (Deg == 2)
            {
                return(SquareRoots);
            }
            ComplexPolynom polynom = this;
            ComplexPolynom p       = this;
            ComplexPolynom deri    = Derivation;
            IDivision      div     = this;
            ComplexPolynom comd    = StaticExtensionClassicalAlgebra.GreatestCommonDivisor(this, deri) as ComplexPolynom;

            if (comd.Deg > 0)
            {
                List <Complex> r         = new List <Complex>();
                ComplexPolynom divcomdeg = Divide(comd)[0];
                Complex[]      r1        = comd.Roots(accuracy);
                Complex[]      r2        = divcomdeg.Roots(accuracy);
                r.AddRange(r1);
                r.AddRange(r2);
                return(r.ToArray());
            }
            Complex[] roots = new Complex[p.Deg];
            for (int i = 0; i < roots.Length; i++)
            {
                if (p.Deg == 1)
                {
                    roots[i] = (-p.coeff[0]) / p.coeff[1];
                    break;
                }
                Complex x = p.Root(accuracy);
                roots[i] = x;
                ComplexPolynom pol = new ComplexPolynom(new Complex[] { -x, 1 });
                p = p.Divide(pol)[0];
            }
            return(roots);
        }