# source:nanovis/trunk/newmat11/solution.h@4794

Last change on this file since 4794 was 2096, checked in by ldelgass, 9 years ago

Normalize line endings, set eol-style to native on *.cpp, *.h files

• Property svn:eol-style set to `native`
File size: 3.2 KB
Line
1/// \ingroup newmat
2///@{
3
4/// \file solution.h
5/// One dimensional solve routine.
6
7#include "myexcept.h"
8
9#ifdef use_namespace
10namespace RBD_COMMON {
11#endif
12
13
14// Solve the equation f(x)=y for x where f is a monotone continuous
15// function of x
16// Essentially Brent s method
17
18// You need to derive a class from R1_R1 and override "operator()"
19// with the function you want to solve.
20// Use an object from this class in OneDimSolve
21
22class R1_R1
23{
24   // the prototype for a Real function of a Real variable
25   // you need to derive your function from this one and put in your
26   // function for operator() at least. You probably also want to set up a
27   // constructor to put in additional parameter values (e.g. that will not
28   // vary during a solve)
29
30protected:
31   Real x;                             // Current x value
32   bool xSet;                          // true if a value assigned to x
33
34public:
35   Real minX, maxX;                    // range of value x
36   bool minXinf, maxXinf;              // true if these are infinite
37   R1_R1() : xSet(false), minXinf(true), maxXinf(true) {}
38   virtual Real operator()() = 0;      // function value at current x
39                                       // set current x
40   virtual void Set(Real X);           // set x, check OK
41   Real operator()(Real X) { Set(X); return operator()(); }
42                                       // set x, return value
43   virtual bool IsValid(Real X);
44   operator Real();                    // implicit conversion
45   virtual ~R1_R1() {}                 // keep gnu happy
46};
47
48class SolutionException : public BaseException
49{
50public:
51   static unsigned long Select;
52   SolutionException(const char* a_what = 0);
53};
54
55class OneDimSolve
56{
57   R1_R1& function;                     // reference to the function
58   Real accX;                           // accuracy in X direction
59   Real accY;                           // accuracy in Y direction
60   int lim;                             // maximum number of iterations
61
62public:
63   OneDimSolve(R1_R1& f, Real AccY = 0.0001, Real AccX = 0.0)
64      : function(f), accX(AccX), accY(AccY) {}
65                       // f is an R1_R1 function
66   Real Solve(Real Y, Real X, Real Dev, int Lim=100);
67                       // Solve for x in Y=f(x)
68                       // X is the initial trial value of x
69                       // X+Dev is the second trial value
70                       // program returns a value of x such that
71                       // |Y-f(x)| <= accY or |f.inv(Y)-x| <= accX
72
73private:
74   Real x[3], y[3];                         // Trial values of X and Y
75   int L,C,U,Last;                          // Locations of trial values
76   int vpol, hpol;                          // polarities
77   Real YY;                                 // target value
78   int i;
79   void LookAt(int);                        // get new value of function
80   bool Finish;                             // true if LookAt finds conv.
81   bool Captured;                           // true when target surrounded
82   void VFlip();
83   void HFlip();
84   void Flip();
85   void State(int I, int J, int K);
86   void Linear(int, int, int);