16 #define CV_PI 3.14159265358979323846 18 int solve_deg2(
double a,
double b,
double c,
double& x1,
double& x2)
20 double delta = b * b - 4 * a * c;
22 if (delta < 0)
return 0;
24 double inv_2a = 0.5 / a;
33 double sqrt_delta = sqrt(delta);
34 x1 = (-b + sqrt_delta) * inv_2a;
35 x2 = (-b - sqrt_delta) * inv_2a;
44 double a,
double b,
double c,
double d,
double& x0,
double& x1,
double& x2)
63 double inv_a = 1. / a;
64 double b_a = inv_a * b, b_a2 = b_a * b_a;
65 double c_a = inv_a * c;
66 double d_a = inv_a * d;
69 double Q = (3 * c_a - b_a2) / 9;
70 double R = (9 * b_a * c_a - 27 * d_a - 2 * b_a * b_a2) / 54;
71 double Q3 = Q * Q * Q;
72 double D = Q3 +
R *
R;
73 double b_a_3 = (1. / 3.) * b_a;
79 x0 = x1 = x2 = -b_a_3;
84 x0 = pow(2 *
R, 1 / 3.0) - b_a_3;
92 double theta = acos(
R / sqrt(-Q3));
93 double sqrt_Q = sqrt(-Q);
94 x0 = 2 * sqrt_Q * cos(theta / 3.0) - b_a_3;
95 x1 = 2 * sqrt_Q * cos((theta + 2 *
CV_PI) / 3.0) - b_a_3;
96 x2 = 2 * sqrt_Q * cos((theta + 4 *
CV_PI) / 3.0) - b_a_3;
103 pow(fabs(
R) + sqrt(D), 1.0 / 3.0) * (
R > 0 ? 1 : (
R < 0 ? -1 : 0));
104 double BD = (AD == 0) ? 0 : -Q / AD;
107 x0 = AD + BD - b_a_3;
117 double a,
double b,
double c,
double d,
double e,
double& x0,
double& x1,
118 double& x2,
double& x3)
127 double inv_a = 1. / a;
132 double b2 = b * b, bc = b * c,
b3 =
b2 * b;
137 1, -c, d * b - 4 * e, 4 * c * e - d * d -
b2 * e, r0, r1, r2);
138 if (n == 0)
return 0;
141 double R2 = 0.25 *
b2 - c + r0,
R;
142 if (R2 < 0)
return 0;
145 double inv_R = 1. /
R;
147 int nb_real_roots = 0;
153 double temp = r0 * r0 - 4 * e;
158 double sqrt_temp = sqrt(temp);
159 D2 = 0.75 *
b2 - 2 * c + 2 * sqrt_temp;
160 E2 = D2 - 4 * sqrt_temp;
165 double u = 0.75 *
b2 - 2 * c - R2,
166 v = 0.25 * inv_R * (4 * bc - 8 * d -
b3);
171 double b_4 = 0.25 * b, R_2 = 0.5 *
R;
176 double D_2 = 0.5 * D;
177 x0 = R_2 + D_2 - b_4;
185 double E_2 = 0.5 * E;
186 if (nb_real_roots == 0)
188 x0 = -R_2 + E_2 - b_4;
194 x2 = -R_2 + E_2 - b_4;
200 return nb_real_roots;
int solve_deg4(double a, double b, double c, double d, double e, double &x0, double &x1, double &x2, double &x3)
Reference : Eric W.
int solve_deg2(double a, double b, double c, double &x1, double &x2)
int solve_deg3(double a, double b, double c, double d, double &x0, double &x1, double &x2)
Reference : Eric W.