24 #define cbrt(x) ((x < 0) ? (-1*pow(-x, 1.0/3.0)) : pow (x, 1.0/3.0))
27 #define M_PI 3.14159265358979323846
31 #define AEQ0(x) (((x) < EPS) && ((x) > -EPS))
33 int solve3(
double *coeff,
double *roots)
37 double p, q, disc, b_over_3a, c_over_a, d_over_a;
38 double r, theta, temp,
alpha, beta;
40 a = coeff[3], b = coeff[2], c = coeff[1], d = coeff[0];
42 return solve2(coeff, roots);
43 b_over_3a = b / (3 * a);
47 p = b_over_3a * b_over_3a;
48 q = 2 * b_over_3a * p - b_over_3a * c_over_a + d_over_a;
50 disc = q * q + 4 * p * p * p;
53 r = .5 * sqrt(-disc + q * q);
54 theta = atan2(sqrt(-disc), -q);
56 roots[0] = temp * cos(theta / 3);
57 roots[1] = temp * cos((theta +
M_PI +
M_PI) / 3);
58 roots[2] = temp * cos((theta -
M_PI -
M_PI) / 3);
61 alpha = .5 * (sqrt(disc) - q);
67 roots[1] = roots[2] = -.5 * roots[0], rootn = 3;
70 for (i = 0; i < rootn; i++)
71 roots[i] -= b_over_3a;
76 int solve2(
double *coeff,
double *roots)
79 double disc, b_over_2a, c_over_a;
81 a = coeff[2], b = coeff[1], c = coeff[0];
83 return solve1(coeff, roots);
84 b_over_2a = b / (2 * a);
87 disc = b_over_2a * b_over_2a - c_over_a;
91 roots[0] = -b_over_2a;
94 roots[0] = -b_over_2a + sqrt(disc);
95 roots[1] = -2 * b_over_2a - roots[0];
104 a = coeff[1], b = coeff[0];
int solve2(double *coeff, double *roots)
int solve3(double *coeff, double *roots)
int solve1(double *coeff, double *roots)