Class PolynomialRoots

java.lang.Object
org.djutils.polynomialroots.PolynomialRoots

public final class PolynomialRoots extends Object
PolynomialRoots.java. Polynomial234RootSolvers - final all roots of linear, quadratic, cubic and quartic polynomials. Derived from https://dl.acm.org/doi/10.1145/2699468. Manual translation from Fortran90 to java by Peter Knoppers.

Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers
  • Method Details

    • linearRoots

      public static Complex[] linearRoots(double q1, double q0)
      LINEAR POLYNOMIAL ROOT SOLVER.

      Calculates the root of the linear polynomial:
      q1 * x + q0
      Unlike the quadratic, cubic and quartic code, this is NOT derived from that Fortran90 code; it was added for completenes.

      Parameters:
      q1 - double; coefficient of the x term
      q0 - double; independent coefficient
      Returns:
      Complex[]; the roots of the equation
    • linearRoots

      public static Complex[] linearRoots(double q0)
      LINEAR POLYNOMIAL ROOT SOLVER.

      Calculates the root of the linear polynomial:
      x + q0
      Unlike the quadratic, cubic and quartic code, this is NOT derived from that Fortran90 code; it was added for completenes.

      Parameters:
      q0 - double; independent coefficient
      Returns:
      Complex[]; the roots of the equation
    • quadraticRoots

      public static Complex[] quadraticRoots(double q2, double q1, double q0)
      QUADRATIC POLYNOMIAL ROOT SOLVER

      Calculates all real + complex roots of the quadratic polynomial:
      q2 * x^2 + q1 * x + q0
      The code checks internally if rescaling of the coefficients is needed to avoid overflow.

      The order of the roots is as follows:
      1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last).
      2) Since there can be only one complex conjugate pair root, no order is necessary.
      q1 : coefficient of x term q0 : independent coefficient

      Parameters:
      q2 - double; coefficient of the quadratic term
      q1 - double; coefficient of the x term
      q0 - double; independent coefficient
      Returns:
      Complex[]; the roots of the equation
    • quadraticRoots

      public static Complex[] quadraticRoots(double q1, double q0)
      QUADRATIC POLYNOMIAL ROOT SOLVER

      Calculates all real + complex roots of the quadratic polynomial:
      x^2 + q1 * x + q0
      The code checks internally if rescaling of the coefficients is needed to avoid overflow.

      The order of the roots is as follows:
      1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last).
      2) Since there can be only one complex conjugate pair root, no order is necessary.
      q1 : coefficient of x term q0 : independent coefficient

      Parameters:
      q1 - double; coefficient of the x term
      q0 - double; independent coefficient
      Returns:
      Complex[]; the roots of the equation
    • cubicRoots

      public static Complex[] cubicRoots(double c3, double c2, double c1, double c0)
      CUBIC POLYNOMIAL ROOT SOLVER.

      Calculates all (real and complex) roots of the cubic polynomial:
      c3 * x^3 + c2 * x^2 + c1 * x + c0
      The first real root (which always exists) is obtained using an optimized Newton-Raphson scheme. The other remaining roots are obtained through composite deflation into a quadratic.

      The cubic root solver can handle any size of cubic coefficients and there is no danger of overflow due to proper rescaling of the cubic polynomial. The order of the roots is as follows: 1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last). 2) Since there can be only one complex conjugate pair root, no order is necessary. 3) All real roots precede the complex ones.

      Parameters:
      c3 - double; coefficient of the cubic term
      c2 - double; coefficient of the quadratic term
      c1 - double; coefficient of the linear term
      c0 - double; coefficient of the independent term
      Returns:
      Complex[]; array of Complex with all the roots
    • cubicRoots

      public static Complex[] cubicRoots(double c2, double c1, double c0)
      CUBIC POLYNOMIAL ROOT SOLVER.

      Calculates all (real and complex) roots of the cubic polynomial:
      x^3 + c2 * x^2 + c1 * x + c0
      The first real root (which always exists) is obtained using an optimized Newton-Raphson scheme. The other remaining roots are obtained through composite deflation into a quadratic.

      The cubic root solver can handle any size of cubic coefficients and there is no danger of overflow due to proper rescaling of the cubic polynomial. The order of the roots is as follows: 1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last). 2) Since there can be only one complex conjugate pair root, no order is necessary. 3) All real roots precede the complex ones.

      Parameters:
      c2 - double; coefficient of the quadratic term
      c1 - double; coefficient of the linear term
      c0 - double; coefficient of the independent term
      Returns:
      Complex[]; array of Complex with all the roots
    • cubicRoots

      public static Complex[] cubicRoots(double c2, double c1, double c0, boolean verbose)
      CUBIC POLYNOMIAL ROOT SOLVER.

      Calculates all (real and complex) roots of the cubic polynomial:
      x^3 + c2 * x^2 + c1 * x + c0
      The first real root (which always exists) is obtained using an optimized Newton-Raphson scheme. The other remaining roots are obtained through composite deflation into a quadratic. An option for printing detailed info about the intermediate stages in solving the cubic is available.

      The cubic root solver can handle any size of cubic coefficients and there is no danger of overflow due to proper rescaling of the cubic polynomial. The order of the roots is as follows: 1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last). 2) Since there can be only one complex conjugate pair root, no order is necessary. 3) All real roots precede the complex ones.

      Parameters:
      c2 - double; coefficient of the quadratic term
      c1 - double; coefficient of the linear term
      c0 - double; coefficient of the independent term
      verbose - boolean; if true; produce debugging output; if false; do not produce debugging output
      Returns:
      Complex[]; array of Complex with all the roots
    • quarticRoots

      public static Complex[] quarticRoots(double q4, double q3, double q2, double q1, double q0)
      QUARTIC POLYNOMIAL ROOT SOLVER

      Calculates all real + complex roots of the quartic polynomial:
      q4 * x^4 + q3 * x^3 + q2 * x^2 + q1 * x + q0

      The quartic root solver can handle any size of quartic coefficients and there is no danger of overflow, due to proper rescaling of the quartic polynomial.

      The order of the roots is as follows:
      1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last).
      2) For complex conjugate pair roots, the order is according to the algebraic value of their real parts (largest positive first). If the real parts are equal, the order is according to the algebraic value of their imaginary parts (largest first).
      3) All real roots precede the complex ones.

      Parameters:
      q4 - double; coefficient of the quartic term
      q3 - double; coefficient of the cubic term
      q2 - double; coefficient of the quadratic term
      q1 - double; coefficient of the linear term
      q0 - double; independent coefficient
      Returns:
      Complex[]; array of Complex with all the roots
    • quarticRoots

      public static Complex[] quarticRoots(double q3, double q2, double q1, double q0)
      QUARTIC POLYNOMIAL ROOT SOLVER

      Calculates all real + complex roots of the quartic polynomial:
      x^4 + q3 * x^3 + q2 * x^2 + q1 * x + q0

      The quartic root solver can handle any size of quartic coefficients and there is no danger of overflow, due to proper rescaling of the quartic polynomial.

      The order of the roots is as follows:
      1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last).
      2) For complex conjugate pair roots, the order is according to the algebraic value of their real parts (largest positive first). If the real parts are equal, the order is according to the algebraic value of their imaginary parts (largest first).
      3) All real roots precede the complex ones.

      Parameters:
      q3 - double; coefficient of the cubic term
      q2 - double; coefficient of the quadratic term
      q1 - double; coefficient of the linear term
      q0 - double; independent coefficient
      Returns:
      Complex[]; array of Complex with all the roots
    • quarticRoots

      public static Complex[] quarticRoots(double q3, double q2, double q1, double q0, boolean verbose)
      QUARTIC POLYNOMIAL ROOT SOLVER

      Calculates all real + complex roots of the quartic polynomial:
      x^4 + q3 * x^3 + q2 * x^2 + q1 * x + q0

      An option for printing detailed info about the intermediate stages in solving the quartic is available. This enables a detailed check in case something went wrong and the roots obtained are not proper.
      The quartic root solver can handle any size of quartic coefficients and there is no danger of overflow, due to proper rescaling of the quartic polynomial.

      The order of the roots is as follows:
      1) For real roots, the order is according to their algebraic value on the number scale (largest positive first, largest negative last).
      2) For complex conjugate pair roots, the order is according to the algebraic value of their real parts (largest positive first). If the real parts are equal, the order is according to the algebraic value of their imaginary parts (largest first).
      3) All real roots precede the complex ones.

      Parameters:
      q3 - double; coefficient of the cubic term
      q2 - double; coefficient of the quadratic term
      q1 - double; coefficient of the linear term
      q0 - double; independent coefficient
      verbose - boolean; if true; produce debugging output; if false; do not produce debugging output
      Returns:
      Complex[]; array of Complex with all the roots