16 #define CV_PI 3.14159265358979323846 20 double delta =
b *
b - 4 *
a *
c;
22 if (delta < 0)
return 0;
24 double inv_2a = 0.5 /
a;
32 double sqrt_delta = sqrt(delta);
33 x1 = (-
b + sqrt_delta) * inv_2a;
34 x2 = (-
b - sqrt_delta) * inv_2a;
43 double & x0,
double & x1,
double & x2)
61 double inv_a = 1. /
a;
62 double b_a = inv_a *
b, b_a2 = b_a * b_a;
63 double c_a = inv_a *
c;
64 double d_a = inv_a * d;
67 double Q = (3 * c_a - b_a2) / 9;
68 double R = (9 * b_a * c_a - 27 * d_a - 2 * b_a * b_a2) / 54;
69 double Q3 = Q * Q * Q;
70 double D = Q3 +
R *
R;
71 double b_a_3 = (1. / 3.) * b_a;
75 x0 = x1 = x2 = - b_a_3;
79 x0 = pow(2 *
R, 1 / 3.0) - b_a_3;
86 double theta = acos(
R / sqrt(-Q3));
87 double sqrt_Q = sqrt(-Q);
88 x0 = 2 * sqrt_Q * cos(theta / 3.0) - b_a_3;
89 x1 = 2 * sqrt_Q * cos((theta + 2 *
CV_PI)/ 3.0) - b_a_3;
90 x2 = 2 * sqrt_Q * cos((theta + 4 *
CV_PI)/ 3.0) - b_a_3;
96 double AD = pow(fabs(
R) + sqrt(D), 1.0 / 3.0) * (
R > 0 ? 1 : (
R < 0 ? -1 : 0));
97 double BD = (AD == 0) ? 0 : -Q / AD;
100 x0 = AD + BD - b_a_3;
109 double & x0,
double & x1,
double & x2,
double & x3)
117 double inv_a = 1. /
a;
118 b *= inv_a;
c *= inv_a; d *= inv_a; e *= inv_a;
123 int n =
solve_deg3(1, -
c, d *
b - 4 * e, 4 *
c * e - d * d -
b2 * e, r0, r1, r2);
124 if (
n == 0)
return 0;
127 double R2 = 0.25 *
b2 -
c + r0,
R;
132 double inv_R = 1. /
R;
134 int nb_real_roots = 0;
139 double temp = r0 * r0 - 4 * e;
143 double sqrt_temp = sqrt(temp);
144 D2 = 0.75 *
b2 - 2 *
c + 2 * sqrt_temp;
145 E2 = D2 - 4 * sqrt_temp;
149 double u = 0.75 *
b2 - 2 *
c - R2,
150 v = 0.25 * inv_R * (4 * bc - 8 * d -
b3);
155 double b_4 = 0.25 *
b, R_2 = 0.5 *
R;
159 double D_2 = 0.5 * D;
160 x0 = R_2 + D_2 - b_4;
167 double E_2 = 0.5 * E;
168 if (nb_real_roots == 0) {
169 x0 = - R_2 + E_2 - b_4;
174 x2 = - R_2 + E_2 - b_4;
180 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.
GLubyte GLubyte GLubyte a