...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/tools/quartic_roots.hpp> namespace boost::math::tools { // Solves ax⁴ + bx³ + cx² + dx + e = 0. std::array<Real,3> quartic_roots(Real a, Real b, Real c, Real d, Real e); }
The quartic_roots
function
extracts all real roots of a quartic polynomial ax⁴+ bx³ + cx² + dx + e.
The result is a std::array<Real, 4>
, which has length four, irrespective of
the number of real roots the polynomial possesses. (This is to prevent the
performance overhead of allocating a vector, which often exceeds the time to
extract the roots.) The roots are returned in nondecreasing order. If a root
is complex, then it is placed at the back of the array and set to a nan.
The algorithm uses the classical method of Ferrari, and follows Graphics Gems V, with an additional Halley iterate for root polishing. A typical use of a quartic real-root solver is to raytrace a torus.
On a consumer laptop, we observe extraction of the roots taking ~90ns. The
file reporting/performance/quartic_roots_performance.cpp
allows determination of the speed on
your system.