7#include "../utility/utility.hpp"
8#include "../container/container.hpp"
9#include "../macros.hpp"
13hi_export_module(hikogui.numeric.polynomial);
15hi_export
namespace hi::inline
v1 {
30hi_force_inline
constexpr lean_vector<T>
solvePolynomial(T
const& a, T
const& b)
noexcept
33 return {
static_cast<T
>(-(b / a))};
57hi_force_inline
constexpr lean_vector<T>
solvePolynomial(T
const& a, T
const& b, T
const& c)
noexcept
62 auto const D = b * b - T{4} * a * c;
66 return {
static_cast<T
>(-b / (T{2} * a))};
68 auto const Dsqrt = sqrt(D);
69 return {
static_cast<T
>((-b - Dsqrt) / (T{2} * a)),
static_cast<T
>((-b + Dsqrt) / (T{2} * a))};
79 constexpr T oneThird = T{1} / T{3};
80 constexpr T pi2_3 = (T{2} / T{3}) * std::numbers::pi_v<T>;
81 constexpr T pi4_3 = (T{4} / T{3}) * std::numbers::pi_v<T>;
83 auto const U = oneThird * acos(((T{3} * q) / (T{2} * p)) * sqrt(T{-3} / p));
84 auto const V = T{2} * sqrt(-oneThird * p);
86 auto const t0 = V * cos(U);
87 auto const t1 = V * cos(U - pi2_3);
88 auto const t2 = V * cos(U - pi4_3);
89 return {
static_cast<T
>(t0),
static_cast<T
>(t1),
static_cast<T
>(t2)};
93hi_force_inline lean_vector<T> solveDepressedCubicCardano(T
const& p, T
const& q, T
const& D)
noexcept
95 auto const sqrtD = sqrt(D);
96 auto const minusHalfQ = T{-0.5} * q;
97 auto const v =
cbrt(minusHalfQ + sqrtD);
98 auto const w =
cbrt(minusHalfQ - sqrtD);
99 return {
static_cast<T
>(v + w)};
121 constexpr T oneForth = T{1} / T{4};
122 constexpr T oneTwentySeventh = T{1} / T{27};
124 if (p == 0.0 && q == 0.0) {
128 auto const D = oneForth * q * q + oneTwentySeventh * p * p * p;
130 if (D < 0 && p != 0.0) {
134 }
else if (D == 0 && p != 0.0) {
136 auto const t0 = (T{3} * q) / p;
137 auto const t1 = (T{-3} * q) / (T{2} * p);
138 return {
static_cast<T
>(t0),
static_cast<T
>(t1),
static_cast<T
>(t1)};
142 return solveDepressedCubicCardano(p, q, D);
156hi_force_inline
constexpr lean_vector<T>
solvePolynomial(T
const& a, T
const& b, T
const& c, T
const& d)
noexcept
162 auto const p = (T{3} * a * c - b * b) / (T{3} * a * a);
163 auto const q = (T{2} * b * b * b - T{9} * a * b * c + T{27} * a * a * d) /
166 auto const b_3a = b / (T{3} * a);
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_force_inline constexpr lean_vector< T > solvePolynomial(T const &a, T const &b) noexcept
Definition polynomial.hpp:30
hi_force_inline lean_vector< T > solveDepressedCubicTrig(T const &p, T const &q) noexcept
Definition polynomial.hpp:77
hi_force_inline lean_vector< T > solveDepressedCubic(T const &p, T const &q) noexcept
Definition polynomial.hpp:119