OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Private Member Functions | Private Attributes | List of all members
OneDimSolve Class Reference

#include <solution.h>

Public Member Functions

 OneDimSolve (R1_R1 &f, Real AccY=0.0001, Real AccX=0.0)
 
Real Solve (Real Y, Real X, Real Dev, int Lim=100)
 

Private Member Functions

void LookAt (int)
 
void VFlip ()
 
void HFlip ()
 
void Flip ()
 
void State (int I, int J, int K)
 
void Linear (int, int, int)
 
void Quadratic (int, int, int)
 

Private Attributes

R1_R1function
 
Real accX
 
Real accY
 
int lim
 
Real x [3]
 
Real y [3]
 
int L
 
int C
 
int U
 
int Last
 
int vpol
 
int hpol
 
Real YY
 
int i
 
bool Finish
 
bool Captured
 

Detailed Description

Definition at line 53 of file solution.h.

Constructor & Destructor Documentation

◆ OneDimSolve()

OneDimSolve::OneDimSolve ( R1_R1 f,
Real  AccY = 0.0001,
Real  AccX = 0.0 
)
inline

Definition at line 61 of file solution.h.

62  : function(f), accX(AccX), accY(AccY) {}
Real accX
Definition: solution.h:56
Real accY
Definition: solution.h:57

Member Function Documentation

◆ Flip()

void OneDimSolve::Flip ( )
private

Definition at line 65 of file solution.cpp.

References C, hpol, L, State(), U, vpol, and y.

Referenced by Solve().

66 {
67  hpol=-hpol; vpol=-vpol; State(U,C,L);
68  y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2];
69 }
int hpol
Definition: solution.h:74
Real y[3]
Definition: solution.h:72
int vpol
Definition: solution.h:74
void State(int I, int J, int K)
Definition: solution.cpp:71

◆ HFlip()

void OneDimSolve::HFlip ( )
private

Definition at line 60 of file solution.cpp.

References C, hpol, L, State(), and U.

Referenced by Solve().

60 { hpol=-hpol; State(U,C,L); }
int hpol
Definition: solution.h:74
void State(int I, int J, int K)
Definition: solution.cpp:71

◆ Linear()

void OneDimSolve::Linear ( int  I,
int  J,
int  K 
)
private

Definition at line 73 of file solution.cpp.

References x, and y.

Referenced by Solve().

74 {
75  x[J] = (x[I]*y[K] - x[K]*y[I])/(y[K] - y[I]);
76  // cout << "Linear\n";
77 }
Real y[3]
Definition: solution.h:72
Real x[3]
Definition: solution.h:72

◆ LookAt()

void OneDimSolve::LookAt ( int  V)
private

Definition at line 49 of file solution.cpp.

References accX, accY, Captured, Finish, L, Last, lim, U, vpol, x, y, and YY.

Referenced by Solve().

50 {
51  lim--;
52  if (!lim) Throw(SolutionException("Does not converge"));
53  Last = V;
54  Real yy = function(x[V]) - YY;
55  Finish = (std::fabs(yy) <= accY) ||
56  (Captured && std::fabs(x[L]-x[U]) <= accX );
57  y[V] = vpol*yy;
58 }
double Real
Definition: include.h:57
Real accX
Definition: solution.h:56
int Last
Definition: solution.h:73
Real y[3]
Definition: solution.h:72
bool Finish
Definition: solution.h:78
Real x[3]
Definition: solution.h:72
int vpol
Definition: solution.h:74
Real YY
Definition: solution.h:75
bool Captured
Definition: solution.h:79
Real accY
Definition: solution.h:57

◆ Quadratic()

void OneDimSolve::Quadratic ( int  I,
int  J,
int  K 
)
private

Definition at line 79 of file solution.cpp.

References square(), x, and y.

Referenced by Solve().

80 {
81  // result to overwrite I
82  Real YJK, YIK, YIJ, XKI, XKJ;
83  YJK = y[J] - y[K]; YIK = y[I] - y[K]; YIJ = y[I] - y[J];
84  XKI = (x[K] - x[I]);
85  XKJ = (x[K]*y[J] - x[J]*y[K])/YJK;
86  if ( square(YJK/YIK)>(x[K] - x[J])/XKI ||
87  square(YIJ/YIK)>(x[J] - x[I])/XKI )
88  {
89  x[I] = XKJ;
90  // cout << "Quadratic - exceptional\n";
91  }
92  else
93  {
94  XKI = (x[K]*y[I] - x[I]*y[K])/YIK;
95  x[I] = (XKJ*y[I] - XKI*y[J])/YIJ;
96  // cout << "Quadratic - normal\n";
97  }
98 }
double Real
Definition: include.h:57
Real y[3]
Definition: solution.h:72
Real x[3]
Definition: solution.h:72
Real square(Real x)
Definition: solution.cpp:47

◆ Solve()

Real OneDimSolve::Solve ( Real  Y,
Real  X,
Real  Dev,
int  Lim = 100 
)

Definition at line 100 of file solution.cpp.

References C, Captured, Finish, Flip(), HFlip(), hpol, i, L, Last, lim, Linear(), LookAt(), Quadratic(), State(), U, VFlip(), vpol, x, y, and YY.

101 {
102  enum Loop { start, captured1, captured2, binary, finish };
103  Tracer et("OneDimSolve::Solve");
104  lim=Lim; Captured = false;
105  if (Dev==0.0) Throw(SolutionException("Dev is zero"));
106  L=0; C=1; U=2; vpol=1; hpol=1; y[C]=0.0; y[U]=0.0;
107  if (Dev<0.0) { hpol=-1; Dev = -Dev; }
108  YY=Y; // target value
109  x[L] = X; // initial trial value
110  if (!function.IsValid(X))
111  Throw(SolutionException("Starting value is invalid"));
112  Loop TheLoop = start;
113  for (;;)
114  {
115  switch (TheLoop)
116  {
117  case start:
118  LookAt(L); if (Finish) { TheLoop = finish; break; }
119  if (y[L]>0.0) VFlip(); // so Y[L] < 0
120 
121  x[U] = X + Dev * hpol;
122  if (!function.maxXinf && x[U] > function.maxX)
123  x[U] = (function.maxX + X) / 2.0;
124  if (!function.minXinf && x[U] < function.minX)
125  x[U] = (function.minX + X) / 2.0;
126 
127  LookAt(U); if (Finish) { TheLoop = finish; break; }
128  if (y[U] > 0.0) { TheLoop = captured1; Captured = true; break; }
129  if (y[U] == y[L])
130  Throw(SolutionException("Function is flat"));
131  if (y[U] < y[L]) HFlip(); // Change direction
132  State(L,U,C);
133  for (i=0; i<20; i++)
134  {
135  // cout << "Searching for crossing point\n";
136  // Have L C then crossing point, Y[L]<Y[C]<0
137  x[U] = x[C] + Dev * hpol;
138  if (!function.maxXinf && x[U] > function.maxX)
139  x[U] = (function.maxX + x[C]) / 2.0;
140  if (!function.minXinf && x[U] < function.minX)
141  x[U] = (function.minX + x[C]) / 2.0;
142 
143  LookAt(U); if (Finish) { TheLoop = finish; break; }
144  if (y[U] > 0) { TheLoop = captured2; Captured = true; break; }
145  if (y[U] < y[C])
146  Throw(SolutionException("Function is not monotone"));
147  Dev *= 2.0;
148  State(C,U,L);
149  }
150  if (TheLoop != start ) break;
151  Throw(SolutionException("Cannot locate a crossing point"));
152 
153  case captured1:
154  // cout << "Captured - 1\n";
155  // We have 2 points L and U with crossing between them
156  Linear(L,C,U); // linear interpolation
157  // - result to C
158  LookAt(C); if (Finish) { TheLoop = finish; break; }
159  if (y[C] > 0.0) Flip(); // Want y[C] < 0
160  if (y[C] < 0.5*y[L]) { State(C,L,U); TheLoop = binary; break; }
161 
162  case captured2:
163  // cout << "Captured - 2\n";
164  // We have L,C before crossing, U after crossing
165  Quadratic(L,C,U); // quad interpolation
166  // - result to L
167  State(C,L,U);
168  if ((x[C] - x[L])*hpol <= 0.0 || (x[C] - x[U])*hpol >= 0.0)
169  { TheLoop = captured1; break; }
170  LookAt(C); if (Finish) { TheLoop = finish; break; }
171  // cout << "Through first stage\n";
172  if (y[C] > 0.0) Flip();
173  if (y[C] > 0.5*y[L]) { TheLoop = captured2; break; }
174  else { State(C,L,U); TheLoop = captured1; break; }
175 
176  case binary:
177  // We have L, U around crossing - do binary search
178  // cout << "Binary\n";
179  for (i=3; i; i--)
180  {
181  x[C] = 0.5*(x[L]+x[U]);
182  LookAt(C); if (Finish) { TheLoop = finish; break; }
183  if (y[C]>0.0) State(L,U,C); else State(C,L,U);
184  }
185  if (TheLoop != binary) break;
186  TheLoop = captured1; break;
187 
188  case finish:
189  return x[Last];
190 
191  }
192  }
193 }
int hpol
Definition: solution.h:74
int Last
Definition: solution.h:73
Real y[3]
Definition: solution.h:72
bool Finish
Definition: solution.h:78
Real x[3]
Definition: solution.h:72
int vpol
Definition: solution.h:74
void State(int I, int J, int K)
Definition: solution.cpp:71
void VFlip()
Definition: solution.cpp:62
void Linear(int, int, int)
Definition: solution.cpp:73
Real YY
Definition: solution.h:75
void LookAt(int)
Definition: solution.cpp:49
bool Captured
Definition: solution.h:79
void HFlip()
Definition: solution.cpp:60
void Quadratic(int, int, int)
Definition: solution.cpp:79
void Flip()
Definition: solution.cpp:65

◆ State()

void OneDimSolve::State ( int  I,
int  J,
int  K 
)
private

Definition at line 71 of file solution.cpp.

References C, L, and U.

Referenced by Flip(), HFlip(), and Solve().

71 { L=I; C=J; U=K; }

◆ VFlip()

void OneDimSolve::VFlip ( )
private

Definition at line 62 of file solution.cpp.

References vpol, and y.

Referenced by Solve().

63  { vpol = -vpol; y[0] = -y[0]; y[1] = -y[1]; y[2] = -y[2]; }
Real y[3]
Definition: solution.h:72
int vpol
Definition: solution.h:74

Member Data Documentation

◆ accX

Real OneDimSolve::accX
private

Definition at line 56 of file solution.h.

Referenced by LookAt().

◆ accY

Real OneDimSolve::accY
private

Definition at line 57 of file solution.h.

Referenced by LookAt().

◆ C

int OneDimSolve::C
private

Definition at line 73 of file solution.h.

Referenced by Flip(), HFlip(), Solve(), and State().

◆ Captured

bool OneDimSolve::Captured
private

Definition at line 79 of file solution.h.

Referenced by LookAt(), and Solve().

◆ Finish

bool OneDimSolve::Finish
private

Definition at line 78 of file solution.h.

Referenced by LookAt(), and Solve().

◆ function

R1_R1& OneDimSolve::function
private

Definition at line 55 of file solution.h.

◆ hpol

int OneDimSolve::hpol
private

Definition at line 74 of file solution.h.

Referenced by Flip(), HFlip(), and Solve().

◆ i

int OneDimSolve::i
private

Definition at line 76 of file solution.h.

Referenced by Solve().

◆ L

int OneDimSolve::L
private

Definition at line 73 of file solution.h.

Referenced by Flip(), HFlip(), LookAt(), Solve(), and State().

◆ Last

int OneDimSolve::Last
private

Definition at line 73 of file solution.h.

Referenced by LookAt(), and Solve().

◆ lim

int OneDimSolve::lim
private

Definition at line 58 of file solution.h.

Referenced by LookAt(), and Solve().

◆ U

int OneDimSolve::U
private

Definition at line 73 of file solution.h.

Referenced by Flip(), HFlip(), LookAt(), Solve(), and State().

◆ vpol

int OneDimSolve::vpol
private

Definition at line 74 of file solution.h.

Referenced by Flip(), LookAt(), Solve(), and VFlip().

◆ x

Real OneDimSolve::x[3]
private

Definition at line 72 of file solution.h.

Referenced by Linear(), LookAt(), Quadratic(), and Solve().

◆ y

Real OneDimSolve::y[3]
private

Definition at line 72 of file solution.h.

Referenced by Flip(), Linear(), LookAt(), Quadratic(), Solve(), and VFlip().

◆ YY

Real OneDimSolve::YY
private

Definition at line 75 of file solution.h.

Referenced by LookAt(), and Solve().


The documentation for this class was generated from the following files: