1 |
|
|
/* |
2 |
|
|
Teem: Tools to process and visualize scientific data and images . |
3 |
|
|
Copyright (C) 2013, 2012, 2011, 2010, 2009 University of Chicago |
4 |
|
|
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann |
5 |
|
|
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah |
6 |
|
|
|
7 |
|
|
This library is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU Lesser General Public License |
9 |
|
|
(LGPL) as published by the Free Software Foundation; either |
10 |
|
|
version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
The terms of redistributing and/or modifying this software also |
12 |
|
|
include exceptions to the LGPL that facilitate static linking. |
13 |
|
|
|
14 |
|
|
This library is distributed in the hope that it will be useful, |
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
|
|
Lesser General Public License for more details. |
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
along with this library; if not, write to Free Software Foundation, Inc., |
21 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
#include "ell.h" |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
/* |
30 |
|
|
******** ell_cubic(): |
31 |
|
|
** |
32 |
|
|
** finds real roots of x^3 + A*x^2 + B*x + C. |
33 |
|
|
** |
34 |
|
|
** records the found real roots in the given root array. |
35 |
|
|
** |
36 |
|
|
** returns information about the roots according to ellCubicRoot enum, |
37 |
|
|
** the set the following values in given root[] array: |
38 |
|
|
** ell_cubic_root_single: root[0], root[1] == root[2] == AIR_NAN |
39 |
|
|
** ell_cubic_root_triple: root[0] == root[1] == root[2] |
40 |
|
|
** ell_cubic_root_single_double: single root[0]; double root[1] == root[2] |
41 |
|
|
** or double root[0] == root[1], single root[2] |
42 |
|
|
** ell_cubic_root_three: root[0], root[1], root[2] |
43 |
|
|
** |
44 |
|
|
** The values stored in root[] are, in a change from the past, sorted |
45 |
|
|
** in descending order! No need to sort them any more! |
46 |
|
|
** |
47 |
|
|
** This does NOT use biff |
48 |
|
|
*/ |
49 |
|
|
int |
50 |
|
|
ell_cubic(double root[3], double A, double B, double C, int newton) { |
51 |
|
|
char me[]="ell_cubic"; |
52 |
|
|
double epsilon = 1.0E-11, AA, Q, R, QQQ, D, sqrt_D, der, |
53 |
|
|
u, v, x, theta, t, sub; |
54 |
|
|
|
55 |
|
|
/* |
56 |
|
|
printf("%s: A B C = %g %g %g\n", me, A, B, C); |
57 |
|
|
*/ |
58 |
|
|
sub = A/3.0; |
59 |
|
|
AA = A*A; |
60 |
|
|
Q = (AA/3.0 - B)/3.0; |
61 |
|
|
R = (-2.0*A*AA/27.0 + A*B/3.0 - C)/2.0; |
62 |
|
|
QQQ = Q*Q*Q; |
63 |
|
|
D = R*R - QQQ; |
64 |
|
|
/* |
65 |
|
|
printf(" R = %15.30f\n Q = %15.30f\n QQQ = %15.30f\n D = %15.30f\n", |
66 |
|
|
R, Q, QQQ, D); |
67 |
|
|
*/ |
68 |
|
|
if (D < -epsilon) { |
69 |
|
|
/* three distinct roots- this is the most common case, it has |
70 |
|
|
been tested the most, its code should go first */ |
71 |
|
|
theta = acos(R/sqrt(QQQ))/3.0; |
72 |
|
|
t = 2*sqrt(Q); |
73 |
|
|
/* yes, these are sorted, because the C definition of acos says |
74 |
|
|
that it returns values in in [0, pi] */ |
75 |
|
|
root[0] = t*cos(theta) - sub; |
76 |
|
|
root[1] = t*cos(theta - 2*AIR_PI/3.0) - sub; |
77 |
|
|
root[2] = t*cos(theta + 2*AIR_PI/3.0) - sub; |
78 |
|
|
/* |
79 |
|
|
if (!AIR_EXISTS(root[0])) { |
80 |
|
|
fprintf(stderr, "%s: %g %g %g --> nan!!!\n", me, A, B, C); |
81 |
|
|
} |
82 |
|
|
*/ |
83 |
|
|
return ell_cubic_root_three; |
84 |
|
|
} |
85 |
|
|
else if (D > epsilon) { |
86 |
|
|
double nr, fnr; |
87 |
|
|
/* one real solution, except maybe also a "rescued" double root */ |
88 |
|
|
sqrt_D = sqrt(D); |
89 |
|
|
u = airCbrt(sqrt_D+R); |
90 |
|
|
v = -airCbrt(sqrt_D-R); |
91 |
|
|
x = u+v - sub; |
92 |
|
|
if (!newton) { |
93 |
|
|
root[0] = x; |
94 |
|
|
root[1] = root[2] = AIR_NAN; |
95 |
|
|
return ell_cubic_root_single; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/* else refine x, the known root, with newton-raphson, so as to get the |
99 |
|
|
most accurate possible calculation for nr, the possible new root */ |
100 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
101 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
102 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
103 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
104 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
105 |
|
|
x -= (der = (3*x + 2*A)*x + B, ((x/der + A/der)*x + B/der)*x + C/der); |
106 |
|
|
nr = -(A + x)/2.0; |
107 |
|
|
fnr = ((nr + A)*nr + B)*nr + C; /* the polynomial evaluated at nr */ |
108 |
|
|
/* |
109 |
|
|
if (ell_debug) { |
110 |
|
|
fprintf(stderr, "%s: root = %g -> %g, nr=% 20.15f\n" |
111 |
|
|
" fnr=% 20.15f\n", me, |
112 |
|
|
x, (((x + A)*x + B)*x + C), nr, fnr); |
113 |
|
|
} |
114 |
|
|
*/ |
115 |
|
|
if (fnr < -epsilon || fnr > epsilon) { |
116 |
|
|
root[0] = x; |
117 |
|
|
root[1] = root[2] = AIR_NAN; |
118 |
|
|
return ell_cubic_root_single; |
119 |
|
|
} |
120 |
|
|
else { |
121 |
|
|
if (ell_debug) { |
122 |
|
|
fprintf(stderr, "%s: rescued double root:% 20.15f\n", me, nr); |
123 |
|
|
} |
124 |
|
|
if (x > nr) { |
125 |
|
|
root[0] = x; |
126 |
|
|
root[1] = nr; |
127 |
|
|
root[2] = nr; |
128 |
|
|
} else { |
129 |
|
|
root[0] = nr; |
130 |
|
|
root[1] = nr; |
131 |
|
|
root[2] = x; |
132 |
|
|
} |
133 |
|
|
return ell_cubic_root_single_double; |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
else { |
137 |
|
|
/* else D is in the interval [-epsilon, +epsilon] */ |
138 |
|
|
if (R < -epsilon || epsilon < R) { |
139 |
|
|
/* one double root and one single root */ |
140 |
|
|
u = airCbrt(R); |
141 |
|
|
if (u > 0) { |
142 |
|
|
root[0] = 2*u - sub; |
143 |
|
|
root[1] = -u - sub; |
144 |
|
|
root[2] = -u - sub; |
145 |
|
|
} else { |
146 |
|
|
root[0] = -u - sub; |
147 |
|
|
root[1] = -u - sub; |
148 |
|
|
root[2] = 2*u - sub; |
149 |
|
|
} |
150 |
|
|
return ell_cubic_root_single_double; |
151 |
|
|
} |
152 |
|
|
else { |
153 |
|
|
/* one triple root */ |
154 |
|
|
root[0] = root[1] = root[2] = -sub; |
155 |
|
|
return ell_cubic_root_triple; |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
/* shouldn't ever get here */ |
159 |
|
|
/* return ell_cubic_root_unknown; */ |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|