1 // (C) Copyright Nick Thompson 2021.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_MATH_TOOLS_QUARTIC_ROOTS_HPP
6 #define BOOST_MATH_TOOLS_QUARTIC_ROOTS_HPP
9 #include <boost/math/tools/cubic_roots.hpp>
11 namespace boost::math::tools {
15 // Make sure the nans are always at the back of the array:
16 template<typename Real>
17 bool comparator(Real r1, Real r2) {
19 if (isnan(r1)) { return false; }
20 if (isnan(r2)) { return true; }
24 template<typename Real>
25 std::array<Real, 4> polish_and_sort(Real a, Real b, Real c, Real d, Real e, std::array<Real, 4>& roots) {
26 // Polish the roots with a Halley iterate.
29 for (auto &r : roots) {
30 Real df = fma(4*a, r, 3*b);
33 Real d2f = fma(12*a, r, 6*b);
34 d2f = fma(d2f, r, 2*c);
35 Real f = fma(a, r, b);
39 Real denom = 2*df*df - f*d2f;
40 if (abs(denom) > (std::numeric_limits<Real>::min)())
45 std::sort(roots.begin(), roots.end(), detail::comparator<Real>);
50 // Solves ax^4 + bx^3 + cx^2 + dx + e = 0.
51 // Only returns the real roots, as these are the only roots of interest in ray intersection problems.
52 // Follows Graphics Gems V: https://github.com/erich666/GraphicsGems/blob/master/gems/Roots3And4.c
53 template<typename Real>
54 std::array<Real, 4> quartic_roots(Real a, Real b, Real c, Real d, Real e) {
57 auto nan = std::numeric_limits<Real>::quiet_NaN();
58 std::array<Real, 4> roots{nan, nan, nan, nan};
59 if (abs(a) <= (std::numeric_limits<Real>::min)()) {
60 auto cbrts = cubic_roots(b, c, d, e);
64 if (b == 0 && c == 0 && d == 0 && e == 0) {
67 return detail::polish_and_sort(a, b, c, d, e, roots);
69 if (abs(e) <= (std::numeric_limits<Real>::min)()) {
70 auto v = cubic_roots(a, b, c, d);
75 return detail::polish_and_sort(a, b, c, d, e, roots);
77 // Now solve x^4 + Ax^3 + Bx^2 + Cx + D = 0.
84 // Mathematica: Expand[(y - A/4)^4 + A*(y - A/4)^3 + B*(y - A/4)^2 + C*(y - A/4) + D]
85 // We now solve the depressed quartic y^4 + py^2 + qy + r = 0.
87 Real q = C - A*B/2 + Asq*A/8;
88 Real r = D - A*C/4 + Asq*B/16 - 3*Asq*Asq/256;
89 if (abs(r) <= (std::numeric_limits<Real>::min)()) {
90 auto [r1, r2, r3] = cubic_roots(Real(1), Real(0), p, q);
98 return detail::polish_and_sort(a, b, c, d, e, roots);
101 if (abs(q) <= (std::numeric_limits<Real>::min)()) {
102 auto [r1, r2] = quadratic_roots(Real(1), p, r);
105 roots[0] = rtr - A/4;
106 roots[1] = -rtr - A/4;
110 roots[2] = rtr - A/4;
111 roots[3] = -rtr - A/4;
113 return detail::polish_and_sort(a, b, c, d, e, roots);
116 // Now split the depressed quartic into two quadratics:
117 // y^4 + py^2 + qy + r = (y^2 + sy + u)(y^2 - sy + v) = y^4 + (v+u-s^2)y^2 + s(v - u)y + uv
118 // So p = v+u-s^2, q = s(v - u), r = uv.
119 // Then (v+u)^2 - (v-u)^2 = 4uv = 4r = (p+s^2)^2 - q^2/s^2.
120 // Multiply through by s^2 to get s^2(p+s^2)^2 - q^2 - 4rs^2 = 0, which is a cubic in s^2.
121 // Then we let z = s^2, to get
122 // z^3 + 2pz^2 + (p^2 - 4r)z - q^2 = 0.
123 auto z_roots = cubic_roots(Real(1), 2*p, p*p - 4*r, -q*q);
124 // z = s^2, so s = sqrt(z).
126 if (z_roots.back() <= 0) {
129 Real s = sqrt(z_roots.back());
131 // s is nonzero, because we took care of the biquadratic case.
132 Real v = (p + s*s + q/s)/2;
134 // Now solve y^2 + sy + u = 0:
135 auto [root0, root1] = quadratic_roots(Real(1), s, u);
137 // Now solve y^2 - sy + v = 0:
138 auto [root2, root3] = quadratic_roots(Real(1), -s, v);
144 for (auto& r : roots) {
147 return detail::polish_and_sort(a, b, c, d, e, roots);