summaryrefslogtreecommitdiff
path: root/src/math/math_BracketMinimum.cxx
blob: 9da0c22f5df4da3363eb208a3bfa8a93b01903f8 (plain)
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
//static const char* sccsid = "@(#)math_BracketMinimum.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
#include <math_BracketMinimum.ixx>

#include <StdFail_NotDone.hxx>   // waiting for NotDone Exception
#include <math_Function.hxx>

#define GOLD           1.618034
#define CGOLD          0.3819660
#define GLIMIT         100.0
#define TINY           1.0e-20
#ifdef MAX
#undef MAX
#endif
#define MAX(a,b)       ((a) > (b) ? (a) : (b))
#define SIGN(a,b)      ((b) > 0.0 ? fabs(a) : -fabs(a))
#define SHFT(a,b,c,d)  (a)=(b);(b)=(c);(c)=(d)


    void math_BracketMinimum::Perform(math_Function& F, 
                                      const Standard_Real A, 
                                      const Standard_Real B) {

     Standard_Boolean OK;
     Standard_Real ulim, u, r, q, f, fu, dum;

     Done = Standard_False; 
     Ax = A;
     Bx = B;
     Standard_Real Lambda = GOLD;
     if (!myFA) {
       OK = F.Value(Ax, FAx);
       if(!OK) return;
     }
     if (!myFB) {
       OK = F.Value(Bx, FBx);
       if(!OK) return;
     }
     if(FBx > FAx) {
       SHFT(dum, Ax, Bx, dum);
       SHFT(dum, FBx, FAx, dum);
     }
     Cx = Bx + Lambda * (Bx - Ax);
     OK = F.Value(Cx, FCx);
     if(!OK) return;
     while(FBx > FCx) {
       r = (Bx - Ax) * (FBx -FCx);
       q = (Bx - Cx) * (FBx -FAx);
       u = Bx - ((Bx - Cx) * q - (Bx - Ax) * r) / 
           (2.0 * SIGN(MAX(fabs(q - r), TINY), q - r));
       ulim = Bx + GLIMIT * (Cx - Bx);
       if((Bx - u) * (u - Cx) > 0.0) {
         OK = F.Value(u, fu);
         if(!OK) return;
         if(fu < FCx) {
           Ax = Bx;
           Bx = u;
           FAx = FBx;
           FBx = fu;
           Done = Standard_True;
           return;
         }
         else if(fu > FBx) {
           Cx = u;
           FCx = fu;
           Done = Standard_True;
           return;
         }
         u = Cx + Lambda * (Cx - Bx);
         OK = F.Value(u, fu);
         if(!OK) return;
       }
       else if((Cx - u) * (u - ulim) > 0.0) {
         OK = F.Value(u, fu);
         if(!OK) return;
         if(fu < FCx) {
           SHFT(Bx, Cx, u, Cx + GOLD * (Cx - Bx));
           OK = F.Value(u, f);
           if(!OK) return;
           SHFT(FBx, FCx, fu, f);
         }
       }
       else if ((u - ulim) * (ulim - Cx) >= 0.0) {
         u = ulim;
         OK = F.Value(u, fu);
         if(!OK) return;
       }
       else {
         u = Cx + GOLD * (Cx - Bx);
         OK = F.Value(u, fu);
         if(!OK) return;
       }
       SHFT(Ax, Bx, Cx, u);
       SHFT(FAx, FBx, FCx, fu);
     }
     Done = Standard_True;
   }




    math_BracketMinimum::math_BracketMinimum(math_Function& F, 
                                             const Standard_Real A, 
                                             const Standard_Real B) {

      myFA = Standard_False;
      myFB = Standard_False;
      Perform(F, A, B);
    }

    math_BracketMinimum::math_BracketMinimum(math_Function& F, 
                                             const Standard_Real A, 
                                             const Standard_Real B,
					     const Standard_Real FA) {
      FAx = FA;
      myFA = Standard_True;
      myFB = Standard_False;
      Perform(F, A, B);
    }

    math_BracketMinimum::math_BracketMinimum(math_Function& F, 
                                             const Standard_Real A, 
                                             const Standard_Real B,
					     const Standard_Real FA,
					     const Standard_Real FB) {
      FAx = FA;
      FBx = FB;
      myFA = Standard_True;
      myFB = Standard_True;
      Perform(F, A, B);
    }


    void math_BracketMinimum::Values(Standard_Real& A, Standard_Real& B, Standard_Real& C) const{

      StdFail_NotDone_Raise_if(!Done, " ");
      A = Ax;
      B = Bx;
      C = Cx;
    }

    void math_BracketMinimum::FunctionValues(Standard_Real& FA, Standard_Real& FB, Standard_Real& FC) const{

      StdFail_NotDone_Raise_if(!Done, " ");
      FA = FAx;
      FB = FBx;
      FC = FCx;
    }

    void math_BracketMinimum::Dump(Standard_OStream& o) const {

       o << "math_BracketMinimum ";
       if(Done) {
         o << " Status = Done \n";
	 o << " The bracketed triplet is: " << endl;
	 o << Ax << ", " << Bx << ", " << Cx << endl;
	 o << " The corresponding function values are: "<< endl;
	 o << FAx << ", " << FBx << ", " << FCx << endl;
       }
       else {
         o << " Status = not Done \n";
       }
}