All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
root.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file root.h
24  \brief File for one-dimensional solver base class \ref o2scl::root
25 */
26 #ifndef O2SCL_ROOT_H
27 #define O2SCL_ROOT_H
28 
29 #include <iostream>
30 #include <cmath>
31 #include <o2scl/err_hnd.h>
32 #include <o2scl/funct.h>
33 
34 #ifndef DOXYGEN_NO_O2NS
35 namespace o2scl {
36 #endif
37 
38  /** \brief One-dimensional solver [abstract base]
39 
40  See the \ref onedsolve_subsect section of the User's guide for
41  general information about \o2 solvers.
42 
43  \future Maybe consider allowing the user to specify
44  the stream to which 'verbose' information is sent.
45  */
46  template<class func_t=funct11, class dfunc_t=func_t> class root {
47 
48  public:
49 
50  root() {
51  ntrial=100;
52  tol_rel=1.0e-8;
53  verbose=0;
54  tol_abs=1.0e-12;
55  last_ntrial=0;
56  err_nonconv=true;
57  }
58 
59  virtual ~root() {}
60 
61  /** \brief The maximum value of the functions for success
62  (default \f$ 10^{-8} \f$ )
63  */
64  double tol_rel;
65 
66  /** \brief The minimum allowable stepsize
67  (default \f$ 10^{-12} \f$ )
68  */
69  double tol_abs;
70 
71  /// Output control (default 0)
72  int verbose;
73 
74  /// Maximum number of iterations (default 100)
75  int ntrial;
76 
77  /** \brief If true, call the error handler if the solver does
78  not converge (default true)
79  */
81 
82  /// The number of iterations used in the most recent solve
84 
85  /// Return the type, \c "root".
86  virtual const char *type() { return "root"; }
87 
88  /** \brief Print out iteration information.
89 
90  Depending on the value of the variable verbose, this prints
91  out the iteration information. If verbose=0, then no
92  information is printed, while if verbose>1, then after each
93  iteration, the present values of \c x and \c y are
94  output to std::cout along with the iteration number. If
95  verbose>=2 then each iteration waits for a character before
96  continuing.
97  */
98  virtual int print_iter(double x, double y, int iter, double value=0.0,
99  double limit=0.0, std::string comment="") {
100  if (verbose<=0) return success;
101 
102  char ch;
103 
104  std::cout << comment << " Iteration: " << iter << std::endl;
105  if (x<0) std::cout << x << " ";
106  else std::cout << " " << x << " ";
107  if (y<0) std::cout << y << " ";
108  else std::cout << " " << y << " ";
109  if (value<0) std::cout << value << " ";
110  else std::cout << " " << value << " ";
111  if (limit<0) std::cout << limit << std::endl;
112  else std::cout << " " << limit << std::endl;
113  if (verbose>1) {
114  std::cout << "Press a key and type enter to continue. ";
115  std::cin >> ch;
116  }
117 
118  return success;
119  }
120 
121  /** \brief Solve \c func using \c x as an initial guess
122  */
123  virtual int solve(double &x, func_t &func)=0;
124 
125  /** \brief Solve \c func in region \f$ x_1<x<x_2 \f$
126  returning \f$ x_1 \f$ .
127  */
128  virtual int solve_bkt(double &x1, double x2, func_t &func) {
129  return solve(x1,func);
130  }
131 
132  /** \brief Solve \c func using \c x as an initial
133  guess using derivatives \c df.
134  */
135  virtual int solve_de(double &x, func_t &func, dfunc_t &df) {
136  return solve(x,func);
137  }
138 
139  };
140 
141  /** \brief One-dimensional bracketing solver [abstract base]
142 
143  */
144  template<class func_t=funct11, class dfunc_t=func_t> class root_bkt :
145  public root<func_t,dfunc_t> {
146 
147  public:
148 
149  root_bkt() {
150  bracket_step=1.0e-4;
151  bracket_iters=10;
152  }
153 
154  virtual ~root_bkt() {}
155 
156  /** \brief The stepsize for automatic bracketing
157  (default \f$ 10^{-4} \f$)
158 
159  If this is exactly zero, it will be reset to
160  \f$ 10^{-4} \f$ by solve().
161  */
162  double bracket_step;
163 
164  /// The number of iterations in attempt to bracket root (default 10)
166 
167  /// Return the type, \c "root_bkt".
168  virtual const char *type() { return "root_bkt"; }
169 
170  /** \brief Solve \c func in region \f$ x_1<x<x_2 \f$
171  returning \f$ x_1 \f$ .
172  */
173  virtual int solve_bkt(double &x1, double x2, func_t &func)=0;
174 
175  /** \brief Solve \c func using \c x as an initial guess
176  */
177  virtual int solve(double &x, func_t &func) {
178 
179  if (bracket_step==0.0) bracket_step=1.0e-4;
180 
181  double x2=0.0, dx, fx, fx2;
182  size_t i=0;
183  bool done=false;
184  // Use function to try to bracket a root
185  while(done==false && i<bracket_iters) {
186 
187  fx=func(x);
188  fx2=func(x*(1.0+bracket_step));
189 
190  dx=(fx2-fx)/(bracket_step*x);
191  x2=x-2.0*fx/dx;
192 
193  fx2=func(x2);
194 
195  if (fx*fx2<0.0) {
196  done=true;
197  } else {
198  x=(x2+x)/2.0;
199  }
200 
201  i++;
202  }
203  if (done==false) {
204  O2SCL_ERR("Failed to bracket function in root_bkt::solve().",
206  }
207  return solve_bkt(x,x2,func);
208 
209  return 0;
210  }
211 
212  /** \brief Solve \c func using \c x as an initial
213  guess using derivatives \c df.
214  */
215  virtual int solve_de(double &x, func_t &func, dfunc_t &df) {
216 
217  double x2=0.0, dx, fx, fx2;
218  int i=0;
219  bool done=false;
220 
221  // Use derivative information to try to bracket a root
222  while(done==false && i<10) {
223 
224  fx=func(x);
225  dx=df(x);
226 
227  x2=x-2.0*fx/dx;
228 
229  fx2=func(x2);
230 
231  if (fx*fx2<0.0) {
232  done=true;
233  } else {
234  x=(x2+x)/2.0;
235  }
236  i++;
237  }
238 
239  if (done==false) {
240  O2SCL_ERR("Failed to bracket function in root_bkt::solve_de().",
242  }
243 
244  return solve_bkt(x,x2,func);
245 
246  }
247 
248  };
249 
250  /** \brief One-dimensional with solver with derivatives [abstract base]
251 
252  \todo At the moment, the functions solve() and solve_bkt()
253  are not implemented for derivative solvers.
254  */
255  template<class func_t=funct11, class dfunc_t=func_t>
256  class root_de : public root<func_t> {
257 
258  public:
259 
260  root_de() {
261  }
262 
263  virtual ~root_de() {}
264 
265  /// Return the type, \c "root_de".
266  virtual const char *type() { return "root_de"; }
267 
268  /** \brief Solve \c func in region \f$ x_1<x<x_2 \f$
269  returning \f$ x_1 \f$ .
270  */
271  virtual int solve_bkt(double &x1, double x2, func_t &func) {
272  O2SCL_ERR("Function solve_bkt() not implemented.",exc_eunimpl);
273  return 0;
274  }
275 
276  /** \brief Solve \c func using \c x as an initial guess
277  */
278  virtual int solve(double &x, func_t &func) {
279  O2SCL_ERR("Function solve() not implemented.",exc_eunimpl);
280  return 0;
281  }
282 
283  /** \brief Solve \c func using \c x as an initial
284  guess using derivatives \c df.
285  */
286  virtual int solve_de(double &x, func_t &func, dfunc_t &df)=0;
287 
288  };
289 
290 #ifndef DOXYGEN_NO_O2NS
291 }
292 #endif
293 
294 #endif
295 
virtual int solve(double &x, func_t &func)
Solve func using x as an initial guess.
Definition: root.h:278
int ntrial
Maximum number of iterations (default 100)
Definition: root.h:75
virtual int solve_bkt(double &x1, double x2, func_t &func)=0
Solve func in region returning .
virtual const char * type()
Return the type, "root_de".
Definition: root.h:266
double tol_abs
The minimum allowable stepsize (default )
Definition: root.h:69
virtual int solve(double &x, func_t &func)=0
Solve func using x as an initial guess.
exceeded max number of iterations
Definition: err_hnd.h:73
One-dimensional with solver with derivatives [abstract base].
Definition: root.h:256
One-dimensional bracketing solver [abstract base].
Definition: root.h:144
generic failure
Definition: err_hnd.h:61
virtual int solve_bkt(double &x1, double x2, func_t &func)
Solve func in region returning .
Definition: root.h:128
requested feature not (yet) implemented
Definition: err_hnd.h:99
double tol_rel
The maximum value of the functions for success (default )
Definition: root.h:64
bool err_nonconv
If true, call the error handler if the solver does not converge (default true)
Definition: root.h:80
virtual int solve_de(double &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
Definition: root.h:215
One-dimensional solver [abstract base].
Definition: root.h:46
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.
Definition: root.h:98
virtual int solve_de(double &x, func_t &func, dfunc_t &df)=0
Solve func using x as an initial guess using derivatives df.
virtual const char * type()
Return the type, "root".
Definition: root.h:86
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
size_t bracket_iters
The number of iterations in attempt to bracket root (default 10)
Definition: root.h:165
virtual int solve(double &x, func_t &func)
Solve func using x as an initial guess.
Definition: root.h:177
virtual int solve_de(double &x, func_t &func, dfunc_t &df)
Solve func using x as an initial guess using derivatives df.
Definition: root.h:135
virtual int solve_bkt(double &x1, double x2, func_t &func)
Solve func in region returning .
Definition: root.h:271
int last_ntrial
The number of iterations used in the most recent solve.
Definition: root.h:83
double bracket_step
The stepsize for automatic bracketing (default )
Definition: root.h:162
virtual const char * type()
Return the type, "root_bkt".
Definition: root.h:168
static const double x2[5]
Definition: inte_qng_gsl.h:66
static const double x1[5]
Definition: inte_qng_gsl.h:48
int verbose
Output control (default 0)
Definition: root.h:72
Success.
Definition: err_hnd.h:47

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..