solution.h

00001 //$$ solution.h                      // solve routines
00002 
00003 #include "myexcept.h"
00004 
00005 #ifdef use_namespace
00006 namespace RBD_COMMON {
00007 #endif
00008 
00009 
00010 // Solve the equation f(x)=y for x where f is a monotone continuous
00011 // function of x
00012 // Essentially Brent s method
00013 
00014 // You need to derive a class from R1_R1 and override "operator()"
00015 // with the function you want to solve.
00016 // Use an object from this class in OneDimSolve
00017 
00018 class R1_R1
00019 {
00020    // the prototype for a Real function of a Real variable
00021    // you need to derive your function from this one and put in your
00022    // function for operator() at least. You probably also want to set up a
00023    // constructor to put in additional parameter values (e.g. that will not
00024    // vary during a solve)
00025 
00026 protected:
00027    Real x;                             // Current x value
00028    bool xSet;                          // true if a value assigned to x
00029 
00030 public:
00031    Real minX, maxX;                    // range of value x
00032    bool minXinf, maxXinf;              // true if these are infinite
00033    R1_R1() : xSet(false), minXinf(true), maxXinf(true) {}
00034    virtual Real operator()() = 0;      // function value at current x
00035                                        // set current x
00036    virtual void Set(Real X);           // set x, check OK
00037    Real operator()(Real X) { Set(X); return operator()(); }
00038                                        // set x, return value
00039    virtual bool IsValid(Real X);
00040    operator Real();                    // implicit conversion
00041 };
00042 
00043 class SolutionException : public BaseException
00044 {
00045 public:
00046    static unsigned long Select;
00047    SolutionException(const char* a_what = 0);
00048 };
00049 
00050 class OneDimSolve
00051 {
00052    R1_R1& function;                     // reference to the function
00053    Real accX;                           // accuracy in X direction
00054    Real accY;                           // accuracy in Y direction
00055    int lim;                             // maximum number of iterations
00056 
00057 public:
00058    OneDimSolve(R1_R1& f, Real AccY = 0.0001, Real AccX = 0.0)
00059       : function(f), accX(AccX), accY(AccY) {}
00060                        // f is an R1_R1 function
00061    Real Solve(Real Y, Real X, Real Dev, int Lim=100);
00062                        // Solve for x in Y=f(x)
00063                        // X is the initial trial value of x
00064                        // X+Dev is the second trial value
00065                        // program returns a value of x such that
00066                        // |Y-f(x)| <= accY or |f.inv(Y)-x| <= accX
00067 
00068 private:
00069    Real x[3], y[3];                         // Trial values of X and Y
00070    int L,C,U,Last;                          // Locations of trial values
00071    int vpol, hpol;                          // polarities
00072    Real YY;                                 // target value
00073    int i;
00074    void LookAt(int);                        // get new value of function
00075    bool Finish;                             // true if LookAt finds conv.
00076    bool Captured;                           // true when target surrounded
00077    void VFlip();
00078    void HFlip();
00079    void Flip();
00080    void State(int I, int J, int K);
00081    void Linear(int, int, int);
00082    void Quadratic(int, int, int);
00083 };
00084 
00085 
00086 #ifdef use_namespace
00087 }
00088 #endif
00089 
00090 // body file: solution.cpp
00091 
00092 
00093 
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3