48 static QList <double>
realLinearRoots(
double coeffLinearTerm,
double coeffConstTerm) {
49 double m=coeffLinearTerm, b=coeffConstTerm;
72 double coeffConstTerm) {
73 double A=coeffQuadTerm, B=coeffLinearTerm, C=coeffConstTerm;
82 if (disc < 0)
return roots;
83 q = -0.5*(B + (B < 0 ? -1.0 : 1.0)*sqrt(disc));
85 if (q == 0) roots << q/A;
90 if (!roots.contains(temp)) roots << temp;
106 static QList <double>
realCubicRoots(
double coeffCubicTerm,
double coeffQuadTerm,
107 double coeffLinearTerm,
double coeffConstTerm) {
108 double a=coeffQuadTerm, b=coeffLinearTerm, c=coeffConstTerm;
112 if (coeffCubicTerm == 0.0)
117 if ( coeffCubicTerm != 1.0) {
125 Q = (a*a - 3.0*b) / 9.0;
126 R = (2*a*a*a - 9.0*a*b + 27.0*c) / 54.0;
129 if ( a == 0.0 && b == 0.0 ) {
130 roots << (c < 0 ? 1.0 : -1.0)*pow(fabs(c),1.0/3.0);
132 else if (R*R <= Q*Q*Q) {
133 double theta = acos(R/sqrt(Q*Q*Q)),temp;
136 roots << Q*cos(theta/3.0) - a /3.0;
138 temp = Q*cos((theta +
TWOPI)/3.0) - a / 3.0;
139 if (!roots.contains(temp)) roots << temp;
140 temp = Q*cos((theta -
TWOPI)/3.0) - a / 3.0;
141 if (!roots.contains(temp)) roots << temp;
145 A = (R < 0 ? 1.0 : -1.0 ) * pow(fabs(R) + sqrt(R*R - Q*Q*Q),1.0/3.0);
146 B = A == 0.0 ? 0.0 : Q / A;
148 roots << (A + B) - a / 3.0;
175 template <
typename Functor>
176 static bool brentsRootFinder(Functor &func,
const QList<double> pt1,
const QList<double> pt2,
177 double tol,
int maxIter,
double &root) {
178 double a = pt1[0], b = pt2[0], c, d = 0;
179 double fa = pt1[1], fb = pt2[1], fc;
180 double p, q, r, s, t, tol1, bnew, fbnew, temp, deltaI, deltaF;
183 double offset = (a + b) / 2.0;
188 if ( (fa > 0) - (fa < 0) == (fb > 0) - (fb < 0) ) {
189 QString msg =
"The function evaluations of two bounding points passed to Brents Method "
190 "have the same sign. Therefore, they don't necessary bound a root. No "
191 "root finding will be attempted.\n";
198 if (fabs(fa) < fabs(fb)) {
212 for (
int iter = 0; iter < maxIter; iter++) {
213 tol1 = DBL_EPSILON * 2.0 * fabs(b);
214 if (a != c && b != c) {
219 p = s * (t * (r - t) * (c - b) - (1 - r) * (b - a));
220 q = (t - 1) * (r - 1) * (s - 1);
225 bnew = b - fb * (b - a) / (fb - fa);
232 temp = (3 * a + b) / 4.0;
233 p = temp < b ? temp : b;
234 q = b > temp ? b : temp;
235 deltaI = fabs(b - bnew);
237 (bnew < p || bnew > q ) ||
241 (mflag && deltaI >= fabs(b - c) / 2.0) ||
246 (!mflag && deltaI >= fabs(c - d) / 2.0) ||
251 (mflag && fabs(b - c) < tol1) ||
256 (!mflag && fabs(c - d) < tol1) ) {
259 bnew = (a + b) / 2.0;
266 fbnew = func(bnew + offset);
268 QString msg =
"Function evaluation failed at:" +
toString(bnew) +
269 ". Function must be continuous and defined for the entire interval "
270 "inorder to gaurentee brentsRootFinder will work.";
277 if ( (fa > 0) - (fa < 0) == (fbnew > 0) - (fbnew < 0) ) {
279 deltaF = fabs(a - bnew);
285 deltaF = fabs(b - bnew);
290 if (fabs(fa) < fabs(fb)) {
299 if (fabs(fb) < tol) {
304 else if ( deltaF < tol1 && fabs(b) < 100.0 * tol) {
314 else if (deltaF < tol1) {
#define _FILEINFO_
Macro for the filename and line number.
Definition IException.h:24
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
const double TWOPI
Two * PI, a complete revolution.
Definition Constants.h:42