00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef O2SCL_CERN_MINIMIZE_H
00024 #define O2SCL_CERN_MINIMIZE_H
00025
00026 #include <o2scl/minimize.h>
00027
00028 #ifndef DOXYGENP
00029 namespace o2scl {
00030 #endif
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 template<class param_t, class func_t=funct<param_t> > class cern_minimize :
00061 public minimize<param_t,func_t>
00062 {
00063
00064 public:
00065
00066 cern_minimize() {
00067 delta_set=false;
00068 min_type=0;
00069 }
00070
00071 inline int nint(double x) {
00072 if (x<0.0) return ((int)(x-0.5));
00073 else return ((int)(x+0.5));
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 virtual int min_bkt(double &x, double a, double b, double &y,
00088 param_t &pa, func_t &func) {
00089
00090
00091 static const double w5=2.23606797749979;
00092 static const double hv=(3.0-w5)/2.0, hw=(w5-1.0)/2.0;
00093 double c, d, v=0.0, fv, w=0.0, fw, h;
00094
00095 if (!delta_set) delta=10.0*this->tolx;
00096
00097 int n=-1;
00098 if (this->tolx==0.0) {
00099 n=nint(2.0*log(fabs((a-b)/1.0e-8)));
00100 } else {
00101 if (a!=b) n=nint(2.0*log(fabs((a-b)/this->tolx)));
00102 }
00103 this->last_ntrial=n;
00104 c=a;
00105 d=b;
00106 if (a>b) {
00107 c=b;
00108 d=a;
00109 }
00110 bool llt=true, lge=true;
00111
00112
00113
00114 while (true) {
00115 h=d-c;
00116 if (n<0) {
00117 x=(c+d)/2.0;
00118 func(x,y,pa);
00119 if ((fabs(x-a)>delta && fabs(x-b)>delta)) min_type=0;
00120 min_type=1;
00121 return 0;
00122 }
00123 if (llt) {
00124 v=c+hv*h;
00125 func(v,fv,pa);
00126 }
00127 if (lge) {
00128 w=c+hw*h;
00129 func(w,fw,pa);
00130 }
00131 if (fv<fw) {
00132 llt=true;
00133 lge=false;
00134 d=w;
00135 w=v;
00136 fw=fv;
00137 } else {
00138 llt=false;
00139 lge=true;
00140 c=v;
00141 v=w;
00142 fv=fw;
00143 }
00144 n--;
00145 }
00146
00147 set_err("Failed sanity check in cern_minimize::min_bkt().",
00148 gsl_esanity);
00149 return gsl_esanity;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 int set_delta(double d) {
00159 if (d>0.0) {
00160 delta=d;
00161 delta_set=true;
00162 }
00163 return 0;
00164 }
00165
00166
00167 virtual const char *type() { return "cern_minimize"; }
00168
00169
00170 int min_type;
00171
00172 protected:
00173
00174 #ifndef DOXYGEN_INTERNAL
00175
00176
00177 double delta;
00178
00179
00180 bool delta_set;
00181
00182 #endif
00183
00184 };
00185
00186 #ifndef DOXYGENP
00187 }
00188 #endif
00189
00190 #endif