Go to the documentation of this file.
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=funct,
class dfunc_t=func_t,
class fp_t=
double>
88 virtual const char *
type() {
return "root"; }
100 virtual int print_iter(fp_t x, fp_t y,
int iter, fp_t value=0.0,
101 fp_t limit=0.0, std::string comment=
"") {
106 std::cout << comment <<
" Iteration: " << iter << std::endl;
107 if (x<0) std::cout << x <<
" ";
108 else std::cout <<
" " << x <<
" ";
109 if (y<0) std::cout << y <<
" ";
110 else std::cout <<
" " << y <<
" ";
111 if (value<0) std::cout << value <<
" ";
112 else std::cout <<
" " << value <<
" ";
113 if (limit<0) std::cout << limit << std::endl;
114 else std::cout <<
" " << limit << std::endl;
116 std::cout <<
"Press a key and type enter to continue. ";
125 virtual int solve(fp_t &x, func_t &func)=0;
137 virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df) {
138 return solve(x,func);
150 template<
class func_t=funct,
class dfunc_t=func_t,
class fp_t=
double>
180 virtual const char *
type() {
return "root_bkt"; }
197 virtual int solve(fp_t &x, func_t &func) {
205 if (bstep_int<0.0) bstep_int=-bstep_int;
207 if (bstep_int<=0.0) bstep_int=1.0e-4;
210 if (bstep_int<0.0) bstep_int=-bstep_int;
215 fp_t
x2=0.0, df, fx, fx2;
227 if (!std::isfinite(fx)) {
228 O2SCL_ERR2(
"First function value not finite in ",
234 df=(fx2-fx)/bstep_int;
236 std::cout <<
"P1: x,x2,fx,fx2,df: " << x <<
" " <<
x2 <<
" "
237 << fx <<
" " << fx2 <<
" " << df << std::endl;
244 fp_t step_phase1=bstep_int;
245 while ((!std::isfinite(fx2) || df==0.0) && j<
bracket_iters*2) {
247 if (j%2==0) step_phase1=-step_phase1;
248 else step_phase1/=two;
251 df=(fx2-fx)/step_phase1;
253 std::cout <<
"P1: x,x2,fx,fx2,df: " << x <<
" " <<
x2 <<
" "
254 << fx <<
" " << fx2 <<
" " << df << std::endl;
264 if (!std::isfinite(fx2)) {
266 "root_bkt::solve().",
272 std::cout <<
"root_bkt::solve(): Iteration " << i <<
" of "
274 std::cout <<
"\tx1: " << x <<
" f(x1): " << fx << std::endl;
275 std::cout <<
"\tx2: " <<
x2 <<
" f(x2): " << fx2 << std::endl;
281 fp_t step_phase2=two;
282 x2=x-step_phase2*fx/df;
285 std::cout <<
"P2: x,x2,fx,fx2: " << x <<
" " <<
x2 <<
" "
286 << fx <<
" " << fx2 << std::endl;
293 x2=x-step_phase2*fx/df;
297 std::cout <<
"P2: x,x2,fx,fx2: " << x <<
" " <<
x2 <<
" "
298 << fx <<
" " << fx2 << std::endl;
303 if (!std::isfinite(fx2)) {
305 "root_bkt::solve().",
311 std::cout <<
"\tx2: " <<
x2 <<
" f(x2): " << fx2 << std::endl;
313 std::cout <<
"Press a key and type enter to continue. ";
336 O2SCL_CONV_RET(
"Failed to bracket (iters>max) in root_bkt::solve().",
342 std::cout <<
"root_bkt::solve(): Going to solve_bkt()."
351 virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df) {
353 fp_t
x2=0, dx, fx, fx2, two=2;
358 while(done==
false && i<10) {
376 O2SCL_CONV_RET(
"Failed to bracket function in root_bkt::solve_de().",
393 template<
class func_t=funct,
class dfunc_t=func_t,
class fp_t=
double>
404 virtual const char *
type() {
return "root_de"; }
416 virtual int solve(fp_t &x, func_t &func) {
424 virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df)=0;
428 #ifndef DOXYGEN_NO_O2NS
fp_t tol_rel
The maximum value of the functions for success (default )
virtual int solve_bkt(fp_t &x1, fp_t x2, func_t &func)=0
Solve func in region returning .
virtual int solve_bkt(fp_t &x1, fp_t x2, func_t &func)
Solve func in region returning .
size_t bracket_iters
The number of iterations in attempt to bracket root (default 10)
virtual int solve(fp_t &x, func_t &func)=0
Solve func using x as an initial guess.
#define O2SCL_CONV2_RET(d, d2, n, b)
Set an error and return the error value, two-string version.
int last_ntrial
The number of iterations used in the most recent solve.
fp_t tol_abs
The minimum allowable stepsize (default )
One-dimensional bracketing solver [abstract base].
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
@ exc_emaxiter
exceeded max number of iterations
@ exc_eunimpl
requested feature not (yet) implemented
virtual int print_iter(fp_t x, fp_t y, int iter, fp_t value=0.0, fp_t limit=0.0, std::string comment="")
Print out iteration information.
int verbose
Output control (default 0)
One-dimensional solver [abstract base].
static const double x2[5]
virtual int solve(fp_t &x, func_t &func)
Solve func using x as an initial guess.
virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df)=0
Solve func using x as an initial guess using derivatives df.
static const double x1[5]
fp_t bracket_step
The relative stepsize for automatic bracketing (default value is zero)
#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".
One-dimensional with solver with derivatives [abstract base].
virtual const char * type()
Return the type, "root".
virtual const char * type()
Return the type, "root_bkt".
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
virtual int solve(fp_t &x, func_t &func)
Solve func using x as an initial guess.
bool err_nonconv
If true, call the error handler if the solver does not converge (default true)
fp_t bracket_min
The minimum stepsize for automatic bracketing (default zero)
virtual int solve_bkt(fp_t &x1, fp_t x2, func_t &func)
Solve func in region returning .
int ntrial
Maximum number of iterations (default 100)
@ exc_ebadfunc
problem with user-supplied function
virtual int solve_de(fp_t &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).