21 #define TwoPi 6.28318530717958648 22 const double eps = 1e-14;
32 double q = (
a2 - 3 * b) / 9;
33 double r = (a * (2 *
a2 - 9 * b) + 27 * c) / 54;
35 double q3 = q * q * q;
39 double t = r / sqrt(q3);
45 x[0] = q * cos(t / 3) - a;
46 x[1] = q * cos((t +
TwoPi) / 3) - a;
47 x[2] = q * cos((t -
TwoPi) / 3) - a;
52 A = -pow(std::abs(r) + sqrt(r2 - q3), 1. / 3);
54 B =
A == 0 ? 0 : q /
A;
58 x[1] = -0.5 * (
A + B) - a;
59 x[2] = 0.5 * sqrt(3.) * (
A - B);
60 if (std::abs(x[2]) <
eps)
71 double x,
double y,
double& a,
double& b)
73 double r = sqrt(x * x + y * y);
90 a = sqrt(0.5 * (x + r));
96 double* x,
double b,
double d)
98 double D = b * b - 4 * d;
102 double x1 = (-b + sD) / 2;
103 double x2 = (-b - sD) / 2;
106 double sx1 = sqrt(x1);
107 double sx2 = sqrt(x2);
116 double sx1 = sqrt(-x1);
117 double sx2 = sqrt(-x2);
125 double sx1 = sqrt(x1);
126 double sx2 = sqrt(-x2);
135 double sD2 = 0.5 * sqrt(-D);
136 CSqrt(-0.5 * b, sD2, x[0], x[1]);
137 CSqrt(-0.5 * b, -sD2, x[2], x[3]);
142 static void dblSort3(
double& a,
double& b,
double& c)
144 if (a > b) std::swap(a, b);
148 if (a > b) std::swap(a, b);
153 double* x,
double b,
double c,
157 if (std::abs(c) < 1e-14 * (std::abs(b) + std::abs(d)))
161 x, 2 * b, b * b - 4 * d, -c * c);
169 double sz1 = sqrt(x[0]);
170 double sz2 = sqrt(x[1]);
171 double sz3 = sqrt(x[2]);
175 x[0] = (-sz1 - sz2 - sz3) / 2;
176 x[1] = (-sz1 + sz2 + sz3) / 2;
177 x[2] = (+sz1 - sz2 + sz3) / 2;
178 x[3] = (+sz1 + sz2 - sz3) / 2;
182 x[0] = (-sz1 - sz2 + sz3) / 2;
183 x[1] = (-sz1 + sz2 - sz3) / 2;
184 x[2] = (+sz1 - sz2 - sz3) / 2;
185 x[3] = (+sz1 + sz2 + sz3) / 2;
190 double sz1 = sqrt(-x[0]);
191 double sz2 = sqrt(-x[1]);
192 double sz3 = sqrt(x[2]);
197 x[1] = (sz1 - sz2) / 2;
199 x[3] = (-sz1 - sz2) / 2;
204 x[1] = (-sz1 + sz2) / 2;
206 x[3] = (sz1 + sz2) / 2;
212 double sz1 = sqrt(x[0]);
214 CSqrt(x[1], x[2], szr, szi);
217 x[0] = -sz1 / 2 - szr;
218 x[1] = -sz1 / 2 + szr;
224 x[0] = sz1 / 2 - szr;
225 x[1] = sz1 / 2 + szr;
234 double x,
double a,
double b,
double c,
237 double fxs = ((4 * x + 3 * a) * x + 2 * b) * x + c;
238 if (fxs == 0)
return 1e99;
239 double fx = (((x + a) * x + b) * x + c) * x + d;
248 double* x,
double a,
double b,
double c,
double d) noexcept
251 double d1 = d + 0.25 * a * (0.25 * b * a - 3. / 64 * a * a * a - c);
252 double c1 = c + 0.5 * a * (0.25 * a * a - b);
253 double b1 = b - 0.375 * a * a;
276 x[0] =
N4Step(x[0], a, b, c, d);
277 x[1] =
N4Step(x[1], a, b, c, d);
281 x[2] =
N4Step(x[2], a, b, c, d);
282 x[3] =
N4Step(x[3], a, b, c, d);
287 #define F5(t) ((((((t) + a) * (t) + b) * (t) + c) * (t) + d) * (t) + e) 290 double a,
double b,
double c,
double d,
294 if (std::abs(e) <
eps)
return 0;
296 double brd = std::abs(a);
297 if (std::abs(b) > brd) brd = std::abs(b);
298 if (std::abs(c) > brd) brd = std::abs(c);
299 if (std::abs(d) > brd) brd = std::abs(d);
300 if (std::abs(e) > brd) brd = std::abs(e);
325 if (std::abs(f0) <
eps)
return x0;
326 if (std::abs(f1) <
eps)
return x1;
330 for (cnt = 0; cnt < 5; cnt++)
334 if (std::abs(f2) <
eps)
return x2;
354 if (x2 <= x0 || x2 >= x1) x2 = (x0 + x1) / 2;
356 if (std::abs(f2) <
eps)
return x2;
368 (((5 * x2 + 4 * a) * x2 + 3 * b) * x2 + 2 * c) * x2 + d;
369 if (std::abs(f2s) <
eps)
376 }
while (std::abs(dx) >
eps);
382 double* x,
double a,
double b,
double c,
double d,
386 double r = x[0] =
SolveP5_1(a, b, c, d, e);
387 double a1 = a + r,
b1 = b + r *
a1, c1 = c + r *
b1, d1 = d + r * c1;
395 double a,
double b,
double c,
double& r1,
double& r2) noexcept
397 if (std::abs(a) <
eps)
400 if (std::abs(b) <
eps)
return 0;
407 double Di = b * b - 4 * a * c;
414 r1 = (-b + Di) / (2 * a);
415 r2 = (-b - Di) / (2 * a);
417 if (r2 < r1) std::swap(r1, r2);
static double SolveP5_1(double a, double b, double c, double d, double e)
int solve_poly5(double *x, double a, double b, double c, double d, double e) noexcept
Solves equation x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.
int SolveP4Bi(double *x, double b, double d)
double N4Step(double x, double a, double b, double c, double d)
static void dblSort3(double &a, double &b, double &c)
int solve_poly4(double *x, double a, double b, double c, double d) noexcept
Solves quartic equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method. ...
void CSqrt(double x, double y, double &a, double &b)
int SolveP4De(double *x, double b, double c, double d)
int solve_poly3(double *x, double a, double b, double c) noexcept
Solves cubic equation x^3 + a*x^2 + b*x + c = 0.
int solve_poly2(double a, double b, double c, double &r1, double &r2) noexcept
Solves equation a*x^2 + b*x + c = 0.