1 | /// \ingroup newmat
|
---|
2 | ///@{
|
---|
3 |
|
---|
4 | /// \file solution.h
|
---|
5 | /// One dimensional solve routine.
|
---|
6 |
|
---|
7 | #include "myexcept.h"
|
---|
8 |
|
---|
9 | #ifdef use_namespace
|
---|
10 | namespace 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 |
|
---|
22 | class 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 |
|
---|
30 | protected:
|
---|
31 | Real x; // Current x value
|
---|
32 | bool xSet; // true if a value assigned to x
|
---|
33 |
|
---|
34 | public:
|
---|
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 |
|
---|
48 | class SolutionException : public BaseException
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | static unsigned long Select;
|
---|
52 | SolutionException(const char* a_what = 0);
|
---|
53 | };
|
---|
54 |
|
---|
55 | class 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 |
|
---|
62 | public:
|
---|
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 |
|
---|
73 | private:
|
---|
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);
|
---|
87 | void Quadratic(int, int, int);
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 | #ifdef use_namespace
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | // body file: solution.cpp
|
---|
96 |
|
---|
97 |
|
---|
98 | ///@}
|
---|
99 |
|
---|