Debian Codegraph @

cvd_src/quartic.cpp

raw symbols SCIP
1 #include <algorithm>
2 #include <cassert>
3 #include <cmath>
4 using namespace std;
5
6 namespace CVD
7 {
8
9 #ifdef WIN32
10
11 // Visual Studio 2005 does not yet define the C99 cbrt function
12 static inline double cbrt(double x)
13 {
14 return pow(fabs(x), 1.0 / 3.0) * (x >= 0 ? 1 : -1);
15 }
16
17 #endif
18
19 static int depressed_cubic_real_roots(double P, double Q, double r[])
20 {
21 static const double third = 1.0 / 3.0;
22 double third_P = third * P;
23 double disc = 4 * (third_P * third_P * third_P) + Q * Q;
24 if(disc >= 0)
25 {
26 double root_disc = sqrt(disc);
27 double cube = Q < 0 ? -0.5 * (Q - root_disc) : -0.5 * (Q + root_disc);
28 double u = cbrt(cube);
29 double x = u - third_P / u;
30 r[0] = x;
31 return 1;
32 }
33 else
34 {
35 double y3_re = -0.5 * Q;
36 double y3_im = 0.5 * sqrt(-disc);
37 // y = cube root (y3)
38 double theta = atan2(y3_im, y3_re) * third;
39 double radius = sqrt(-third_P);
40 double y_re = radius * cos(theta);
41 double x = 2 * y_re;
42 double root_disc = sqrt(-3 * x * x - 4 * P);
43 if(x > 0)
44 {
45 r[0] = -0.5 * (x + root_disc);
46 r[1] = -0.5 * (x - root_disc);
47 r[2] = x;
48 }
49 else
50 {
51 r[0] = x;
52 r[1] = -0.5 * (x + root_disc);
53 r[2] = -0.5 * (x - root_disc);
54 }
55 return 3;
56 }
57 }
58
59 int find_quartic_real_roots(double B, double C, double D, double E, double r[])
60 {
61 static const double third = 1.0 / 3.0;
62 double alpha = C - 0.375 * B * B;
63 double half_B = 0.5 * B;
64 double beta = half_B * (half_B * half_B - C) + D;
65 double q_B = 0.25 * B;
66 double four_gamma = B * (q_B * (C - 3 * q_B * q_B) - D) + 4 * E;
67
68 if(beta * beta < 1e-18)
69 {
70 double disc = alpha * alpha - four_gamma;
71 if(disc < 0)
72 return 0;
73 double root_disc = sqrt(disc);
74 {
75 double disc2 = -alpha + root_disc;
76 if(disc2 < 0)
77 return 0;
78 double root_disc2 = sqrt(0.5 * disc2);
79 r[0] = -q_B - root_disc2;
80 r[1] = -q_B + root_disc2;
81 }
82 {
83 double disc2 = -alpha - root_disc;
84 if(disc2 < 0)
85 return 2;
86 double root_disc2 = sqrt(0.5 * disc2);
87 r[2] = -q_B - root_disc2;
88 r[3] = -q_B + root_disc2;
89 }
90 return 4;
91 }
92
93 double third_alpha = alpha * third;
94 double P = -0.25 * (alpha * third_alpha + four_gamma);
95 double Q = third_alpha * (0.25 * (four_gamma - third_alpha * third_alpha)) - beta * beta * 0.125;
96
97 double dcr[3];
98 int ndcr = depressed_cubic_real_roots(P, Q, dcr);
99 double disc2 = 2 * (dcr[ndcr - 1] - third_alpha);
100 double W = sqrt(disc2);
101
102 double disc_base = 2 * alpha + disc2; //3*alpha + 2*y;
103 double disc_add = 2 * beta / W;
104
105 int count = 0;
106 if(disc_base + disc_add <= 0)
107 {
108 double sr = sqrt(-disc_base - disc_add);
109 r[count++] = -q_B + 0.5 * (W - sr);
110 r[count++] = -q_B + 0.5 * (W + sr);
111 }
112 if(disc_base - disc_add <= 0)
113 {
114 double sr = sqrt(-disc_base + disc_add);
115 r[count++] = -q_B - 0.5 * (W + sr);
116 r[count++] = -q_B - 0.5 * (W - sr);
117 }
118 return count;
119 }
120
121 }