23 #ifndef O2SCL_MINIMIZE_H 24 #define O2SCL_MINIMIZE_H 29 #include <o2scl/err_hnd.h> 30 #include <o2scl/funct.h> 36 #ifndef DOXYGEN_NO_O2NS 42 template<
class func_t=funct11,
class dfunc_t=func_t>
class min_base {
90 virtual int print_iter(
double x,
double y,
int iter,
double value=0.0,
91 double limit=0.0, std::string comment=
"") {
97 std::cout << comment <<
" Iteration: " << iter << std::endl;
98 if (x<0) std::cout << x <<
" ";
99 else std::cout <<
" " << x <<
" ";
100 if (y<0) std::cout << y <<
" ";
101 else std::cout <<
" " << y <<
" ";
102 if (value<0) std::cout << value <<
" ";
103 else std::cout <<
" " << value <<
" ";
104 if (limit<0) std::cout << limit << std::endl;
105 else std::cout <<
" " << limit << std::endl;
107 std::cout <<
"Press a key and type enter to continue. ";
120 virtual int min(
double &x,
double &fmin, func_t &func)=0;
127 virtual int min_bkt(
double &
x2,
double x1,
double x3,
double &fmin,
137 virtual int min_de(
double &x,
double &fmin, func_t &func,
157 virtual int bracket(
double &ax,
double &bx,
double &cx,
double &fa,
158 double &fb,
double &fc, func_t &func) {
160 double x=ax, x2=bx, x3=(ax+bx)/2.0;
165 while(done==
false && i<bracket_iter) {
171 std::cout <<
"Function min::bracket(), Iteration: " 173 std::cout <<
" " << x <<
" " << x3 <<
" " << x2 << std::endl;
174 std::cout <<
" " << fx <<
" " << fx3 <<
" " << fx2 << std::endl;
177 std::cout <<
"Press a key and type enter to continue. ";
182 if (fx3>=fx2 && fx3>=fx) {
192 }
else if (fx<=fx3 && fx3<=fx2) {
196 }
else if (fx3<fx2 && fx3<fx) {
214 O2SCL_ERR(
"Too many iterations in min::bracket().",
222 virtual const char *
type() {
return "min"; }
228 template<
class func_t,
class dfunc_t=func_t>
250 virtual int min(
double &x,
double &fmin, func_t &func) {
251 double xl, xr, f, fl, fr;
254 if (this->
bracket(xl,x,xr,fl,f,fr,func)!=0) {
258 return min_bkt(x,xl,xr,fmin,func);
266 virtual int min_bkt(
double &
x2,
double x1,
double x3,
double &fmin,
276 virtual int min_de(
double &x,
double &fmin, func_t &func,
278 double xl, xr, f, fl, fr;
281 if (this->
bracket(xl,x,xr,fl,f,fr,func)!=0) {
285 return min_bkt(x,xl,xr,fmin,func);
289 virtual const char *
type() {
return "min_bkt"; }
301 template<
class func_t,
class dfunc_t=func_t>
317 virtual int min(
double &x,
double &fmin, func_t &func)=0;
324 virtual int min_bkt(
double &
x2,
double x1,
double x3,
double &fmin,
334 virtual int min_de(
double &x,
double &fmin, func_t &func,
338 virtual const char *
type() {
return "min_de"; }
358 inline double constraint(
double x,
double center,
double width,
361 if (x>center+width) {
362 ret=height*(1.0+fabs(x-center-width)/width);
363 }
else if (x<center-width) {
364 ret=height*(1.0+fabs(x-center+width)/width);
400 double height,
double tightness=40.0,
401 double exp_arg_limit=50.0) {
402 double ret, wid2=width*width;
403 double arg=tightness/wid2*(x-center+width)*(center+width-x);
404 if (arg<-exp_arg_limit) {
405 ret=(x-center)*(x-center)/wid2;
407 ret=(x-center)*(x-center)/wid2/(1.0+exp(arg));
429 if (x<center) ret=height*(1.0+fabs(x-center)/width);
461 double height,
double tightness=40.0,
462 double exp_arg_limit=50.0) {
463 double ret, arg=tightness*(x-center)/width;
464 if (arg>exp_arg_limit) {
466 }
else if (arg<-exp_arg_limit) {
467 ret=height*(center-x+width)/width;
469 ret=height*(center-x+width)/width/(1.0+exp(arg));
474 #ifndef DOXYGEN_NO_O2NS int bracket_iter
The number of iterations for automatically bracketing a minimum (default 20)
One-dimensional bracketing minimization [abstract base].
double cont_lower_bound(double x, double center, double width, double height, double tightness=40.0, double exp_arg_limit=50.0)
Constrain x to be greater than the value given by center.
virtual const char * type()
Return string denoting type ("min_de")
double tol_rel
The tolerance for the minimum function value.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
#define O2SCL_CONV_RET(d, n, b)
Set a "convergence" error and return the error value.
One-dimensional minimization using derivatives [abstract base].
virtual int min_de(double &x, double &fmin, func_t &func, dfunc_t &df)=0
Calculate the minimum min of func with derivative dfunc w.r.t 'x'.
int verbose
Output control.
int ntrial
Maximum number of iterations.
double tol_abs
The tolerance for the location of the minimum.
double cont_constraint(double x, double center, double width, double height, double tightness=40.0, double exp_arg_limit=50.0)
Constrain x to be within width of the value given by center.
exceeded max number of iterations
double constraint(double x, double center, double width, double height)
Constrain x to be within width of the value given by center.
bool err_nonconv
If true, call the error handler if the routine does not "converge".
virtual int bracket(double &ax, double &bx, double &cx, double &fa, double &fb, double &fc, func_t &func)
Given interval (ax,bx), attempt to bracket a minimum for function func.
virtual int min(double &x, double &fmin, func_t &func)
Calculate the minimum min of func w.r.t 'x'.
int last_ntrial
The number of iterations used in the most recent minimization.
virtual int min_bkt(double &x2, double x1, double x3, double &fmin, func_t &func)=0
Calculate the minimum min of func with x2 bracketed between x1 and x3.
virtual int print_iter(double x, double y, int iter, double value=0.0, double limit=0.0, std::string comment="")
Print out iteration information.
int bracket_iter
The number of iterations for automatically bracketing a minimum (default 20)
static const double x3[11]
virtual int min(double &x, double &fmin, func_t &func)=0
Calculate the minimum min of func w.r.t 'x'.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
One-dimensional minimization [abstract base].
virtual int min_de(double &x, double &fmin, func_t &func, dfunc_t &df)
Calculate the minimum min of func with derivative dfunc w.r.t 'x'.
double lower_bound(double x, double center, double width, double height)
Constrain x to be greater than the value given by center.
static const double x2[5]
static const double x1[5]
virtual const char * type()
Return string denoting type ("min")
virtual const char * type()
Return string denoting type ("min_bkt")