std::sqrt(std::valarray) - cppreference.com (original) (raw)

Finds all three roots (two of which can be complex conjugates) of several Cubic equations at once.

#include #include #include #include #include #include 聽 using CD = std::complex; using VA = std::valarray; 聽 // return all n complex roots out of a given complex number x VA root(CD x, unsigned n) { const double mag = std::pow(std::abs(x), 1.0 / n); const double step = 2.0 * std:馃敘:pi / n; double phase = std::arg(x) / n; VA v(n); for (std::size_t i{}; i != n; ++i, phase += step) v[i] = std::polar(mag, phase); return v; } 聽 // return n complex roots of each element in v; in the output valarray first // goes the sequence of all n roots of v[0], then all n roots of v[1], etc. VA root(VA v, unsigned n) { VA o(v.size() * n); VA t(n); for (std::size_t i = 0; i != v.size(); ++i) { t = root(v[i], n); for (unsigned j = 0; j != n; ++j) o[n * i + j] = t[j]; } return o; } 聽 // floating-point numbers comparator that tolerates given rounding error inline bool is_equ(CD x, CD y, double tolerance = 0.000'000'001) { return std::abs(std::abs(x) - std::abs(y)) < tolerance; } 聽 int main() { // input coefficients for polynomial x鲁 + p路x + q const VA p{1, 2, 3, 4, 5, 6, 7, 8}; const VA q{1, 2, 3, 4, 5, 6, 7, 8}; 聽 // the solver const VA d = std::sqrt(std::pow(q / 2, 2) + std::pow(p / 3, 3)); const VA u = root(-q / 2 + d, 3); const VA n = root(-q / 2 - d, 3); 聽 // allocate memory for roots: 3 * number of input cubic polynomials VA x[3]; for (std::size_t t = 0; t != 3; ++t) x[t].resize(p.size()); 聽 auto is_proper_root = [](CD a, CD b, CD p) { return is_equ(a * b + p / 3.0, 0.0); }; 聽 // sieve out 6 out of 9 generated roots, leaving only 3 proper roots (per polynomial) for (std::size_t i = 0; i != p.size(); ++i) for (std::size_t j = 0, r = 0; j != 3; ++j) for (std::size_t k = 0; k != 3; ++k) if (is_proper_root(u[3 * i + j], n[3 * i + k], p[i])) x[r++][i] = u[3 * i + j] + n[3 * i + k]; 聽 std::cout << "Depressed cubic equation: Root 1: \t\t Root 2: \t\t Root 3:\n"; for (std::size_t i = 0; i != p.size(); ++i) { std::cout << "x鲁 + " << p[i] << "路x + " << q[i] << " = 0 " << std::fixed << x[0][i] << " " << x[1][i] << " " << x[2][i] << std::defaultfloat << '\n'; 聽 assert(is_equ(std::pow(x[0][i], 3) + x[0][i] * p[i] + q[i], 0.0)); assert(is_equ(std::pow(x[1][i], 3) + x[1][i] * p[i] + q[i], 0.0)); assert(is_equ(std::pow(x[2][i], 3) + x[2][i] * p[i] + q[i], 0.0)); } }

Depressed cubic equation: Root 1: Root 2: Root 3: x鲁 + (1,0)路x + (1,0) = 0 (-0.682328,0.000000) (0.341164,1.161541) (0.341164,-1.161541) x鲁 + (2,0)路x + (2,0) = 0 (-0.770917,0.000000) (0.385458,1.563885) (0.385458,-1.563885) x鲁 + (3,0)路x + (3,0) = 0 (-0.817732,0.000000) (0.408866,1.871233) (0.408866,-1.871233) x鲁 + (4,0)路x + (4,0) = 0 (-0.847708,0.000000) (0.423854,2.130483) (0.423854,-2.130483) x鲁 + (5,0)路x + (5,0) = 0 (-0.868830,0.000000) (0.434415,2.359269) (0.434415,-2.359269) x鲁 + (6,0)路x + (6,0) = 0 (-0.884622,0.000000) (0.442311,2.566499) (0.442311,-2.566499) x鲁 + (7,0)路x + (7,0) = 0 (-0.896922,0.000000) (0.448461,2.757418) (0.448461,-2.757418) x鲁 + (8,0)路x + (8,0) = 0 (-0.906795,0.000000) (0.453398,2.935423) (0.453398,-2.935423)