/// <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); } }
/// <summary>Gets the real roots of a specified polynomial of degree up to 3. /// </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="realRoots">The real roots (output).</param> /// <param name="imaginaryZeroTolerance">The (positive) tolerance taken into account to indicate whether a complex number is interpreted as real number.</param> /// <returns>The number of real roots.</returns> /// <remarks>The real roots of the polynomial represented by the coefficients will be added to <paramref name="realRoots"/>. Roots are added with respect to their /// multiplicity, i.e. a root may add more than once in <paramref name="realRoots"/>. /// <para><paramref name="realRoots"/> will <c>not</c> be cleared before adding real roots. /// Implementations use the minimum of <paramref name="imaginaryZeroTolerance"/> and <see cref="MachineConsts.Epsilon"/> to indicate whether the imaginary part of a complex /// number is assumed to be <c>0.0</c> and a real number is given. /// </para> /// </remarks> /// <exception cref="NullReferenceException">Thrown, if <paramref name="realRoots"/> is <c>null</c>.</exception> public static int GetRealRoots(double absoluteCoefficient, double firstOrderCoefficient, double secondOrderCoefficient, double thirdOrderCoefficient, IList <double> realRoots, double imaginaryZeroTolerance = MachineConsts.Epsilon) { if (thirdOrderCoefficient != 0.0) // degree 3 { return(RealDegreeThreePolynomial.GetRealRoots(absoluteCoefficient, firstOrderCoefficient, secondOrderCoefficient, thirdOrderCoefficient, realRoots, imaginaryZeroTolerance)); } if (secondOrderCoefficient != 0.0) // degree 2 { return(RealDegreeTwoPolynomial.GetRealRoots(absoluteCoefficient, firstOrderCoefficient, secondOrderCoefficient, realRoots, imaginaryZeroTolerance)); } else if (firstOrderCoefficient != 0.0) // degree 1 { realRoots.Add(unchecked (-absoluteCoefficient / firstOrderCoefficient)); // the highest coefficient is always != 0.0, but maybe near 0.0 return(1); } else // degree 0 { return(0); } }