#ifndef BRENT_HPP #define BRENT_HPP #include #include "bas_util.hpp" //#include "optimizer.hpp" namespace opt_utilities { template T brent(T ax,T bx,T cx,func_obj& f,T tol,T& xmin) { const int ITMAX=100; const T CGOLD=0.3819660; const T ZEPS=std::numeric_limits::epsilon()*1.e-3; int iter; T a=0,b=0,d(0),etemp=0,fu=0,fv=0,fw=0,fx=0,p=0,q=0 ,r=0,tol1=0,tol2=0,u=0,v=0,w=0,x=0,xm=0; T e=0.; a=(axcx?ax:cx); x=w=v=bx; fw=fv=fx=f.eval(x); for(iter=0;itertol1) { r=(x-w)*(fx-fv); q=(x-v)*(fx-fw); p=(x-v)*q-(x-w)*r; q=2.*(q-r); if(q>0.) { p=-p; } q=tabs(q); etemp=e; e=d; if(tabs(p)>=tabs(T(T(.5)*p*etemp))||p<=q*(a-x)||p>=q*(b-x)) { d=CGOLD*(e=(x>=xm?a-x:b-x)); } else { d=p/q; u=x+d; if(u-a=xm?a-x:b-x)); } u=(tabs(d)>=tol1?x+d:x+sign(tol1,d)); fu=f.eval(u); if(fu<=fx) { if(u>=x) { a=x; } else { b=x; } shft3(v,w,x,u); shft3(fv,fw,fx,fu); } else { if(u