/// <summary>Gets the (complex) roots of a specified polynomial of degree up to 4. /// </summary> /// <param name="absoluteCoefficient">The absolute coefficient.</param> /// <param name="firstOrderCoefficient">The first order coefficient.</param> /// <param name="secondOrderCoefficient">The second order coefficient.</param> /// <param name="thirdOrderCoefficient">The third order coefficient.</param> /// <param name="fourthOrderCoefficient">The fourth order coefficient.</param> /// <param name="firstRoot">The first root (output).</param> /// <param name="secondRoot">The second root (output).</param> /// <param name="thirdRoot">The third root (output).</param> /// <param name="fourthRoot">The fourth root (output).</param> /// <returns>The order of the polynomial represented by the coefficients, i.e. the number of roots.</returns> public static int GetRoots(double absoluteCoefficient, double firstOrderCoefficient, double secondOrderCoefficient, double thirdOrderCoefficient, double fourthOrderCoefficient, out System.Numerics.Complex firstRoot, out System.Numerics.Complex secondRoot, out System.Numerics.Complex thirdRoot, out System.Numerics.Complex fourthRoot) { if (fourthOrderCoefficient != 0.0) // degree 4 { RealDegreeFourPolynomial.GetRoots(absoluteCoefficient, firstOrderCoefficient, secondOrderCoefficient, thirdOrderCoefficient, fourthOrderCoefficient, out firstRoot, out secondRoot, out thirdRoot, out fourthRoot); return(4); } if (thirdOrderCoefficient != 0.0) // degree 3 { RealDegreeThreePolynomial.GetRoots(absoluteCoefficient, firstOrderCoefficient, secondOrderCoefficient, thirdOrderCoefficient, out firstRoot, out secondRoot, out thirdRoot); fourthRoot = new System.Numerics.Complex(Double.NaN, Double.NaN); return(3); } if (secondOrderCoefficient != 0.0) // degree 2 { RealDegreeTwoPolynomial.GetRoots(absoluteCoefficient, firstOrderCoefficient, secondOrderCoefficient, out firstRoot, out secondRoot); thirdRoot = fourthRoot = new System.Numerics.Complex(Double.NaN, Double.NaN); return(2); } else if (firstOrderCoefficient != 0.0) // degree 1 { firstRoot = unchecked (-absoluteCoefficient / firstOrderCoefficient); // the highest coefficient is always != 0.0, but maybe near 0.0 secondRoot = thirdRoot = fourthRoot = new System.Numerics.Complex(Double.NaN, Double.NaN); return(1); } else // degree 0 { firstRoot = secondRoot = thirdRoot = fourthRoot = new System.Numerics.Complex(Double.NaN, Double.NaN); return(0); } }