27 #ifndef math_roots_hpp
28 #define math_roots_hpp
43 template<
typename realT>
61 template<
typename realT>
66 return -1*(4*p*p*p + 27*q*q);
80 template<
typename realT>
89 p = (3*a*
c-b*b)/(3*a*a);
90 q = (2*b*b*b-9*a*b*
c+27*a*a*d)/(27*a*a*a);
100 template<
typename realT>
105 realT D = q*q/4 + p*p*p/27;
107 if(D < 0)
throw std::runtime_error(
"cannot apply Cardano's formula, find roots using....");
114 return std::cbrt(u1) + std::cbrt(u2);
128 template<
typename realT>
137 std::complex<realT> p, q, S, Q, Delta0, Delta1;
139 p = (8.0*a*
c - 3.0*b*b)/(8.0*a*a);
141 q = (b*b*b - 4.0*a*b*
c + 8.0*a*a*d)/(8.0*a*a*a);
143 Delta0 =
c*
c - 3.0*b*d + 12.0*a*e;
144 Delta1 = 2.0*
c*
c*
c - 9.0*b*
c*d + 27.0*b*b*e + 27.0*a*d*d - 72.0*a*
c*e;
146 Q = pow(
static_cast<realT
>(0.5)* (Delta1 + sqrt( Delta1*Delta1 -
static_cast<realT
>(4)*Delta0*Delta0*Delta0)), 1./3.);
148 S =
static_cast<realT
>(0.5)*sqrt( -
static_cast<realT
>(2.0/3.0)*p +
static_cast<realT
>(1.0/(3.0*a))*(Q + Delta0/Q));
152 x[0] = -b/(4*a) - S +
static_cast<realT
>(0.5)*sqrt(
static_cast<realT
>(-4)*S*S -
static_cast<realT
>(2)*p + q/S);
153 x[1] = -b/(4*a) - S -
static_cast<realT
>(0.5)*sqrt(
static_cast<realT
>(-4)*S*S -
static_cast<realT
>(2)*p + q/S);
154 x[2] = -b/(4*a) + S +
static_cast<realT
>(0.5)*sqrt(
static_cast<realT
>(-4)*S*S -
static_cast<realT
>(2)*p - q/S);
155 x[3] = -b/(4*a) + S -
static_cast<realT
>(0.5)*sqrt(
static_cast<realT
>(-4)*S*S -
static_cast<realT
>(2)*p - q/S);
constexpr units::realT c()
The speed of light.
void quarticRoots(std::vector< std::complex< realT > > &x, realT a, realT b, realT c, realT d, realT e)
Find the roots of the general quartic equation.
realT cubicRealRoot(const realT &p, const realT &q)
Calculate the real root for a depressed cubic with negative descriminant.
realT cubicDescriminant(const realT &a, const realT &b, const realT &c, const realT &d)
Calculate the descriminant for the general cubic.
void cubicDepressed(realT &p, realT &q, const realT &a, const realT &b, const realT &c, const realT &d)
Convert a general cubic equation to depressed form.