31 #include <o2scl/err_hnd.h> 32 #include <o2scl/funct.h> 33 #include <o2scl/misc.h> 35 #ifndef DOXYGEN_NO_O2NS 47 template<
class func_t=funct11,
class dfunc_t=func_t>
class root {
87 virtual const char *
type() {
return "root"; }
99 virtual int print_iter(
double x,
double y,
int iter,
double value=0.0,
100 double limit=0.0, std::string comment=
"") {
101 if (verbose<=0)
return success;
105 std::cout << comment <<
" Iteration: " << iter << std::endl;
106 if (x<0) std::cout << x <<
" ";
107 else std::cout <<
" " << x <<
" ";
108 if (y<0) std::cout << y <<
" ";
109 else std::cout <<
" " << y <<
" ";
110 if (value<0) std::cout << value <<
" ";
111 else std::cout <<
" " << value <<
" ";
112 if (limit<0) std::cout << limit << std::endl;
113 else std::cout <<
" " << limit << std::endl;
115 std::cout <<
"Press a key and type enter to continue. ";
124 virtual int solve(
double &x, func_t &func)=0;
130 return solve(x1,func);
136 virtual int solve_de(
double &x, func_t &func, dfunc_t &df) {
137 return solve(x,func);
145 template<
class func_t=funct11,
class dfunc_t=func_t>
class root_bkt :
146 public root<func_t,dfunc_t> {
175 virtual const char *
type() {
return "root_bkt"; }
180 virtual int solve_bkt(
double &
x1,
double x2, func_t &func)=0;
192 virtual int solve(
double &x, func_t &func) {
198 if (bracket_step<=0.0) {
199 bstep_int=fabs(x)*1.0e-4;
200 if (bstep_int<bracket_min) bstep_int=bracket_min;
201 if (bstep_int<=0.0) bstep_int=1.0e-4;
203 bstep_int=fabs(x)*bracket_step;
204 if (bstep_int<bracket_min) bstep_int=bracket_min;
205 if (bstep_int<=0.0) bstep_int=bracket_step;
208 double x2=0.0, df, fx, fx2;
213 while(done==
false && i<bracket_iters) {
220 if (!std::isfinite(fx)) {
221 O2SCL_ERR2(
"First function value not finite in ",
227 df=(fx2-fx)/bstep_int;
229 std::cout <<
"P1: x,x2,fx,fx2,df: " << x <<
" " << x2 <<
" " 230 << fx <<
" " << fx2 <<
" " << df << std::endl;
236 double step_phase1=bstep_int;
237 while ((!std::isfinite(fx2) || df==0.0) && j<bracket_iters*2) {
239 if (j%2==0) step_phase1*=-1.0;
240 else step_phase1/=2.0;
243 df=(fx2-fx)/step_phase1;
245 std::cout <<
"P1: x,x2,fx,fx2,df: " << x <<
" " << x2 <<
" " 246 << fx <<
" " << fx2 <<
" " << df << std::endl;
256 if (!std::isfinite(fx2)) {
258 "root_bkt::solve().",
264 std::cout <<
"root_bkt::solve(): Iteration " << i <<
" of " 265 << bracket_iters <<
"." << std::endl;
266 std::cout <<
"\tx1: " << x <<
" f(x1): " << fx << std::endl;
267 std::cout <<
"\tx2: " << x2 <<
" f(x2): " << fx2 << std::endl;
273 double step_phase2=2.0;
274 x2=x-step_phase2*fx/df;
277 std::cout <<
"P2: x,x2,fx,fx2: " << x <<
" " << x2 <<
" " 278 << fx <<
" " << fx2 << std::endl;
283 while (!std::isfinite(fx2) && k<bracket_iters) {
285 x2=x-step_phase2*fx/df;
289 std::cout <<
"P2: x,x2,fx,fx2: " << x <<
" " << x2 <<
" " 290 << fx <<
" " << fx2 << std::endl;
295 if (!std::isfinite(fx2)) {
297 "root_bkt::solve().",
303 std::cout <<
"\tx2: " << x2 <<
" f(x2): " << fx2 << std::endl;
305 std::cout <<
"Press a key and type enter to continue. ";
328 O2SCL_CONV_RET(
"Failed to bracket (iters>max) in root_bkt::solve().",
334 std::cout <<
"root_bkt::solve(): Going to solve_bkt()." 343 virtual int solve_de(
double &x, func_t &func, dfunc_t &df) {
345 double x2=0.0, dx, fx, fx2;
350 while(done==
false && i<10) {
368 O2SCL_CONV_RET(
"Failed to bracket function in root_bkt::solve_de().",
385 template<
class func_t=funct11,
class dfunc_t=func_t>
396 virtual const char *
type() {
return "root_de"; }
408 virtual int solve(
double &x, func_t &func) {
416 virtual int solve_de(
double &x, func_t &func, dfunc_t &df)=0;
420 #ifndef DOXYGEN_NO_O2NS virtual int solve(double &x, func_t &func)
Solve func using x as an initial guess.
int ntrial
Maximum number of iterations (default 100)
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.
virtual const char * type()
Return the type, "root_de".
double tol_abs
The minimum allowable stepsize (default )
virtual int solve(double &x, func_t &func)=0
Solve func using x as an initial guess.
exceeded max number of iterations
#define O2SCL_CONV2_RET(d, d2, n, b)
Set an error and return the error value, two-string version.
double bracket_min
The minimum stepsize for automatic bracketing (default zero)
One-dimensional with solver with derivatives [abstract base].
One-dimensional bracketing solver [abstract base].
virtual int solve_bkt(double &x1, double x2, func_t &func)
Solve func in region returning .
requested feature not (yet) implemented
double tol_rel
The maximum value of the functions for success (default )
bool err_nonconv
If true, call the error handler if the solver does not converge (default true)
virtual int solve_de(double &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
One-dimensional solver [abstract base].
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.
virtual const char * type()
Return the type, "root".
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
size_t bracket_iters
The number of iterations in attempt to bracket root (default 10)
virtual int solve(double &x, func_t &func)
Solve func using x as an initial guess.
virtual int solve_de(double &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
virtual int solve_bkt(double &x1, double x2, func_t &func)
Solve func in region returning .
problem with user-supplied function
int last_ntrial
The number of iterations used in the most recent solve.
double bracket_step
The relative stepsize for automatic bracketing (default value is zero)
virtual const char * type()
Return the type, "root_bkt".
static const double x2[5]
static const double x1[5]
int verbose
Output control (default 0)