10 #define WANT_STREAM // include.h will get stream fns 11 #define WANT_MATH // include.h will get math fns 19 namespace RBD_COMMON {
25 if ((!minXinf && X <= minX) || (!maxXinf && X >= maxX))
33 Real y = operator()();
42 AddMessage(
"Error detected by solution package\n");
54 Real yy =
function(x[V]) - YY;
55 Finish = (fabs(yy) <= accY) || (Captured && fabs(x[L]-x[U]) <= accX );
62 { vpol = -vpol; y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2]; }
66 hpol=-hpol; vpol=-vpol; State(U,C,L);
67 y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2];
74 x[J] = (x[I]*y[
K] - x[
K]*y[I])/(y[K] - y[I]);
81 Real YJK, YIK, YIJ, XKI, XKJ;
82 YJK = y[J] - y[
K]; YIK = y[I] - y[
K]; YIJ = y[I] - y[J];
84 XKJ = (x[
K]*y[J] - x[J]*y[
K])/YJK;
85 if (
square(YJK/YIK)>(x[
K] - x[J])/XKI ||
86 square(YIJ/YIK)>(x[J] - x[I])/XKI )
93 XKI = (x[
K]*y[I] - x[I]*y[
K])/YIK;
94 x[I] = (XKJ*y[I] - XKI*y[J])/YIJ;
101 enum Loop { start, captured1, captured2, binary, finish };
102 Tracer et(
"OneDimSolve::Solve");
103 lim=Lim; Captured =
false;
105 L=0; C=1; U=2; vpol=1; hpol=1; y[C]=0.0; y[U]=0.0;
106 if (Dev<0.0) { hpol=-1; Dev = -Dev; }
109 if (!
function.IsValid(X))
111 Loop TheLoop = start;
117 LookAt(L);
if (Finish) { TheLoop = finish;
break; }
118 if (y[L]>0.0) VFlip();
120 x[U] = X + Dev * hpol;
121 if (!
function.maxXinf && x[U] >
function.maxX)
122 x[U] = (
function.maxX + X) / 2.0;
123 if (!
function.minXinf && x[U] <
function.minX)
124 x[U] = (
function.minX + X) / 2.0;
126 LookAt(U);
if (Finish) { TheLoop = finish;
break; }
127 if (y[U] > 0.0) { TheLoop = captured1; Captured =
true;
break; }
130 if (y[U] < y[L]) HFlip();
136 x[U] = x[C] + Dev * hpol;
137 if (!
function.maxXinf && x[U] >
function.maxX)
138 x[U] = (
function.maxX + x[C]) / 2.0;
139 if (!
function.minXinf && x[U] <
function.minX)
140 x[U] = (
function.minX + x[C]) / 2.0;
142 LookAt(U);
if (Finish) { TheLoop = finish;
break; }
143 if (y[U] > 0) { TheLoop = captured2; Captured =
true;
break; }
149 if (TheLoop != start )
break;
157 LookAt(C);
if (Finish) { TheLoop = finish;
break; }
158 if (y[C] > 0.0) Flip();
159 if (y[C] < 0.5*y[L]) { State(C,L,U); TheLoop = binary;
break; }
167 if ((x[C] - x[L])*hpol <= 0.0 || (x[C] - x[U])*hpol >= 0.0)
168 { TheLoop = captured1;
break; }
169 LookAt(C);
if (Finish) { TheLoop = finish;
break; }
171 if (y[C] > 0.0) Flip();
172 if (y[C] > 0.5*y[L]) { TheLoop = captured2;
break; }
173 else { State(C,L,U); TheLoop = captured1;
break; }
180 x[C] = 0.5*(x[L]+x[U]);
181 LookAt(C);
if (Finish) { TheLoop = finish;
break; }
182 if (y[C]>0.0) State(L,U,C);
else State(C,L,U);
184 if (TheLoop != binary)
break;
185 TheLoop = captured1;
break;
197 return (minXinf || x > minX) && (maxXinf || x < maxX);
static void AddMessage(const char *a_what)
SolutionException(const char *a_what=0)
void State(int I, int J, int K)
void Linear(int, int, int)
virtual bool IsValid(Real X)
static unsigned long Select
static unsigned long Select
void Quadratic(int, int, int)
Real Solve(Real Y, Real X, Real Dev, int Lim=100)