1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//$$ solution.cpp // solve routines
// Copyright (C) 1994: R B Davies
#define WANT_STREAM // include.h will get stream fns
#define WANT_MATH // include.h will get math fns
#include "include.h"
#include "config.h"
#include "boolean.h"
#include "myexcept.h"
#include "solution.h"
#ifdef use_namespace
namespace RBD_COMMON {
#endif
void R1_R1::Set(Real X)
{
if ((!minXinf && X <= minX) || (!maxXinf && X >= maxX))
Throw(SolutionException("X value out of range"));
x = X; xSet = true;
}
R1_R1::operator Real()
{
if (!xSet) Throw(SolutionException("Value of X not set"));
Real y = operator()();
return y;
}
unsigned long SolutionException::Select;
SolutionException::SolutionException(const char* a_what) : BaseException()
{
Select = BaseException::Select;
AddMessage("Error detected by solution package\n");
AddMessage(a_what); AddMessage("\n");
if (a_what) Tracer::AddTrace();
};
inline Real square(Real x) { return x*x; }
void OneDimSolve::LookAt(int V)
{
lim--;
if (!lim) Throw(SolutionException("Does not converge"));
Last = V;
Real yy = function(x[V]) - YY;
Finish = (fabs(yy) <= accY) || (Captured && fabs(x[L]-x[U]) <= accX );
y[V] = vpol*yy;
}
void OneDimSolve::HFlip() { hpol=-hpol; State(U,C,L); }
void OneDimSolve::VFlip()
{ vpol = -vpol; y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2]; }
void OneDimSolve::Flip()
{
hpol=-hpol; vpol=-vpol; State(U,C,L);
y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2];
}
void OneDimSolve::State(int I, int J, int K) { L=I; C=J; U=K; }
void OneDimSolve::Linear(int I, int J, int K)
{
x[J] = (x[I]*y[K] - x[K]*y[I])/(y[K] - y[I]);
// cout << "Linear\n";
}
void OneDimSolve::Quadratic(int I, int J, int K)
{
// result to overwrite I
Real YJK, YIK, YIJ, XKI, XKJ;
YJK = y[J] - y[K]; YIK = y[I] - y[K]; YIJ = y[I] - y[J];
XKI = (x[K] - x[I]);
XKJ = (x[K]*y[J] - x[J]*y[K])/YJK;
if ( square(YJK/YIK)>(x[K] - x[J])/XKI ||
square(YIJ/YIK)>(x[J] - x[I])/XKI )
{
x[I] = XKJ;
// cout << "Quadratic - exceptional\n";
}
else
{
XKI = (x[K]*y[I] - x[I]*y[K])/YIK;
x[I] = (XKJ*y[I] - XKI*y[J])/YIJ;
// cout << "Quadratic - normal\n";
}
}
Real OneDimSolve::Solve(Real Y, Real X, Real Dev, int Lim)
{
enum Loop { start, captured1, captured2, binary, finish };
Tracer et("OneDimSolve::Solve");
lim=Lim; Captured = false;
if (Dev==0.0) Throw(SolutionException("Dev is zero"));
L=0; C=1; U=2; vpol=1; hpol=1; y[C]=0.0; y[U]=0.0;
if (Dev<0.0) { hpol=-1; Dev = -Dev; }
YY=Y; // target value
x[L] = X; // initial trial value
if (!function.IsValid(X))
Throw(SolutionException("Starting value is invalid"));
Loop TheLoop = start;
for (;;)
{
switch (TheLoop)
{
case start:
LookAt(L); if (Finish) { TheLoop = finish; break; }
if (y[L]>0.0) VFlip(); // so Y[L] < 0
x[U] = X + Dev * hpol;
if (!function.maxXinf && x[U] > function.maxX)
x[U] = (function.maxX + X) / 2.0;
if (!function.minXinf && x[U] < function.minX)
x[U] = (function.minX + X) / 2.0;
LookAt(U); if (Finish) { TheLoop = finish; break; }
if (y[U] > 0.0) { TheLoop = captured1; Captured = true; break; }
if (y[U] == y[L])
Throw(SolutionException("Function is flat"));
if (y[U] < y[L]) HFlip(); // Change direction
State(L,U,C);
for (i=0; i<20; i++)
{
// cout << "Searching for crossing point\n";
// Have L C then crossing point, Y[L]<Y[C]<0
x[U] = x[C] + Dev * hpol;
if (!function.maxXinf && x[U] > function.maxX)
x[U] = (function.maxX + x[C]) / 2.0;
if (!function.minXinf && x[U] < function.minX)
x[U] = (function.minX + x[C]) / 2.0;
LookAt(U); if (Finish) { TheLoop = finish; break; }
if (y[U] > 0) { TheLoop = captured2; Captured = true; break; }
if (y[U] < y[C])
Throw(SolutionException("Function is not monotone"));
Dev *= 2.0;
State(C,U,L);
}
if (TheLoop != start ) break;
Throw(SolutionException("Cannot locate a crossing point"));
case captured1:
// cout << "Captured - 1\n";
// We have 2 points L and U with crossing between them
Linear(L,C,U); // linear interpolation
// - result to C
LookAt(C); if (Finish) { TheLoop = finish; break; }
if (y[C] > 0.0) Flip(); // Want y[C] < 0
if (y[C] < 0.5*y[L]) { State(C,L,U); TheLoop = binary; break; }
case captured2:
// cout << "Captured - 2\n";
// We have L,C before crossing, U after crossing
Quadratic(L,C,U); // quad interpolation
// - result to L
State(C,L,U);
if ((x[C] - x[L])*hpol <= 0.0 || (x[C] - x[U])*hpol >= 0.0)
{ TheLoop = captured1; break; }
LookAt(C); if (Finish) { TheLoop = finish; break; }
// cout << "Through first stage\n";
if (y[C] > 0.0) Flip();
if (y[C] > 0.5*y[L]) { TheLoop = captured2; break; }
else { State(C,L,U); TheLoop = captured1; break; }
case binary:
// We have L, U around crossing - do binary search
// cout << "Binary\n";
for (i=3; i; i--)
{
x[C] = 0.5*(x[L]+x[U]);
LookAt(C); if (Finish) { TheLoop = finish; break; }
if (y[C]>0.0) State(L,U,C); else State(C,L,U);
}
if (TheLoop != binary) break;
TheLoop = captured1; break;
case finish:
return x[Last];
}
}
}
bool R1_R1::IsValid(Real X)
{
Set(X);
return (minXinf || x > minX) && (maxXinf || x < maxX);
}
#ifdef use_namespace
}
#endif
|