# Class Cubic

- Defined in File PolynomialRoots.hh

## Class Documentation

class PolynomialRoots::Cubic

  Cubic polynomial class

  Constructor

      double a = 1;
      double b = 2;
      double c = 3;
      double d = 3;
      Cubic p(a,b,c,d); // build an solve ``a x^3 + b x^2 + c x + d = 0``

      Cubic p;
      p.setup(a,b,c,d); // build an solve ``a x^3 + b x^2 + c x + d = 0``

  Get kind of solution

      int  nroots = p.numRoots();
      bool has_complex_roots = p.complexRoots();
      bool has_a_double_root = p.doubleRoot();
      bool has_a_triple_root = p.tripleRoot();

  Get real roots

      double r_min = 0;
      double r_max = 2;
      double r[3];
      int nroots;
      nroots = p.getRealRoots( r );
      nroots = p.getPositiveRoots( r );
      nroots = p.getNegativeRoots( r );
      nroots = p.getRootsInRange( r_min, r_max, r );
      nroots = p.getRootsInOpenRange( r_min, r_max, r );

  Get roots

      double r0 = p.real_root0();
      double r1 = p.real_root1();
      double r2 = p.real_root2();
      complexType r0 = p.root0();
      complexType r1 = p.root1();
      complexType r2 = p.root2();

      complexType r;
      double re, im;
      p.getRoot0( re, im );
      p.getRoot0( r );
      p.getRoot1( re, im );
      p.getRoot1( r );
      p.getRoot2( re, im );
      p.getRoot2( r );

  Evaluate polynomial

      {double or complex} v, x;
      v = p.eval( x );

      p.eval( x, p, dp );

  Information

      p.info( cout );
      bool ok = p.check( cout );

  Public Functions

  inline Cubic()
  inline Cubic(valueType _a, valueType _b, valueType _c, valueType _d)
  inline void setup(valueType a, valueType b, valueType c, valueType d)

    Compute the roots of cubic polynomial \( a x^3 + b x^2 + c x + d \)

    Parameters

      - a – [in] coefficient of \( x^3 \)

      - b – [in] coefficient of \( x^2 \)

      - c – [in] coefficient of \( x \)

      - d – [in] coefficient of \( x^0 \)

  inline indexType numRoots() const

    Number of found roots.

  inline bool complexRoots() const

    Has complex roots?

  inline bool doubleRoot() const

    Has a double root?

  inline bool tripleRoot() const

    Has a triple root?

  indexType getRealRoots(valueType r[]) const

    Get the real roots.

    Parameters

      r – [out] vector that will be filled with the real roots

    Returns

      the total number of real roots, 0, 1, 2 or 3

  indexType getPositiveRoots(valueType r[]) const

    Get positive real roots.

    Parameters

      r – [out] vector that will be filled with the real roots

    Returns

      the total number of positive real roots, 0, 1, 2 or 3

  indexType getNegativeRoots(valueType r[]) const

    Get negative real roots.

    Parameters

      r – [out] vector that will be filled with the real roots

    Returns

      the total number of negative real roots, 0, 1, 2 or 3

  indexType getRootsInRange(valueType a, valueType b, valueType r[]) const

    Get real roots in a closed range.

    Parameters

      - a – [in] left side of the range

      - b – [in] right side of the range

      - r – [out] vector that will be filled with the real roots

    Returns

      the total number of real roots in the range [a,b]

  indexType getRootsInOpenRange(valueType a, valueType b, valueType r[]) const

    Get real roots in an open range.

    Parameters

      - a – [in] left side of the range

      - b – [in] right side of the range

      - r – [out] vector that will be filled with the real roots

    Returns

      the total number of real roots in the open range (a,b)

  inline valueType real_root0() const

    First real root.

  inline valueType real_root1() const

    Second real root.

  inline valueType real_root2() const

    Third real root.

  inline complexType root0() const

    First complex or real root.

  inline complexType root1() const

    Second complex or real root.

  inline complexType root2() const

    Third complex or real root.

  inline void getRoot0(valueType &re, valueType &im) const

    First complex or real root.

  inline void getRoot0(complexType &r) const

    First complex or real root.

  inline void getRoot1(valueType &re, valueType &im) const

    Second complex or real root.

  inline void getRoot1(complexType &r) const

    Second complex or real root.

  inline void getRoot2(valueType &re, valueType &im) const

    Third complex or real root.

  inline void getRoot2(complexType &r) const

    Third complex or real root.

  inline valueType eval(valueType x) const

    Evalute the cubic polynomial.

    Parameters

      x – value where compute \( p(x) \)

    Returns

      the value \( p(x) \)

  inline complexType eval(complexType const &x) const

    Evalute the cubic polynomial.

    Parameters

      x – value where compute \( p(x) \), x complex

    Returns

      the value \( p(x) \)

  void eval(valueType x, valueType &p, valueType &dp) const

    Evalute the polynomial with its derivative.

    Parameters

      - x – value where compute \( p(x) \)

      - p – value \( p(x) \)

      - dp – value \( p'(x) \)

  void info(std::ostream &s) const

    Print info of the roots of the polynomial.

  bool check(std::ostream &s) const

    Check tolerenace and quality of the computed roots.