32 int solve_deg2(
double a,
double b,
double c,
double & x1,
double & x2)
34 double delta = b * b - 4 * a * c;
36 if (delta < 0)
return 0;
38 double inv_2a = 0.5 / a;
47 double sqrt_delta = sqrt(delta);
48 x1 = (-b + sqrt_delta) * inv_2a;
49 x2 = (-b - sqrt_delta) * inv_2a;
58 double & x0,
double & x1,
double & x2)
78 double inv_a = 1. / a;
79 double b_a = inv_a * b, b_a2 = b_a * b_a;
80 double c_a = inv_a * c;
81 double d_a = inv_a * d;
84 double Q = (3 * c_a - b_a2) / 9;
85 double R = (9 * b_a * c_a - 27 * d_a - 2 * b_a * b_a2) / 54;
86 double Q3 = Q * Q * Q;
87 double D = Q3 + R * R;
88 double b_a_3 = (1. / 3.) * b_a;
92 x0 = x1 = x2 = - b_a_3;
97 x0 = pow(2 * R, 1 / 3.0) - b_a_3;
105 double theta = acos(R / sqrt(-Q3));
106 double sqrt_Q = sqrt(-Q);
107 x0 = 2 * sqrt_Q * cos(theta / 3.0) - b_a_3;
108 x1 = 2 * sqrt_Q * cos((theta + 2 *
M_PI)/ 3.0) - b_a_3;
109 x2 = 2 * sqrt_Q * cos((theta + 4 *
M_PI)/ 3.0) - b_a_3;
115 double AD = pow(fabs(R) + sqrt(D), 1.0 / 3.0) * (R > 0 ? 1 : (R < 0 ? -1 : 0));
116 double BD = (AD == 0) ? 0 : -Q / AD;
119 x0 = AD + BD - b_a_3;
127 int solve_deg4(
double a,
double b,
double c,
double d,
double e,
128 double & x0,
double & x1,
double & x2,
double & x3)
137 double inv_a = 1. / a;
138 b *= inv_a; c *= inv_a; d *= inv_a; e *= inv_a;
139 double b2 = b * b, bc = b * c, b3 = b2 * b;
143 int n =
solve_deg3(1, -c, d * b - 4 * e, 4 * c * e - d * d - b2 * e, r0, r1, r2);
144 if (n == 0)
return 0;
147 double R2 = 0.25 * b2 - c + r0, R;
152 double inv_R = 1. / R;
154 int nb_real_roots = 0;
160 double temp = r0 * r0 - 4 * e;
165 double sqrt_temp = sqrt(temp);
166 D2 = 0.75 * b2 - 2 * c + 2 * sqrt_temp;
167 E2 = D2 - 4 * sqrt_temp;
172 double u = 0.75 * b2 - 2 * c - R2, v = 0.25 * inv_R * (4 * bc - 8 * d - b3);
177 double b_4 = 0.25 * b, R_2 = 0.5 * R;
181 double D_2 = 0.5 * D;
182 x0 = R_2 + D_2 - b_4;
189 double E_2 = 0.5 *
E;
190 if (nb_real_roots == 0)
192 x0 = - R_2 + E_2 - b_4;
198 x2 = - R_2 + E_2 - b_4;
204 return nb_real_roots;