root_toms748.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2013-2017, 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_toms748.h
24  \brief File defining \ref o2scl::root_toms748
25 */
26 #ifndef O2SCL_ROOT_TOMS748_H
27 #define O2SCL_ROOT_TOMS748_H
28 
29 #include <string>
30 
31 #include <boost/math/tools/roots.hpp>
32 
33 #include <o2scl/root.h>
34 
35 namespace o2scl {
36 
37  /** \brief Convergence test similar to
38  <tt>gsl_root_test_interval()</tt> for \ref root_toms748
39  */
40  template <class T> class gsl_tolerance {
41 
42  public:
43 
44  gsl_tolerance(double tol_abs, double tol_rel) {
45  epsabs=tol_abs;
46  epsrel=tol_rel;
47  }
48 
49  bool operator()(const T &a, const T &b) {
50  double abs_a=fabs(a);
51  double abs_b=fabs(b);
52  double min_abs;
53  if ((a > 0.0 && b > 0.0) || (a < 0.0 && b < 0.0)) {
54  if (abs_a<abs_b) min_abs=abs_a;
55  else min_abs=abs_b;
56  } else {
57  min_abs=0.0;
58  }
59 
60  double tolerance = epsabs+epsrel*min_abs;
61 
62  if (fabs(b-a) < tolerance) {
63  return true;
64  }
65  return false;
66  }
67 
68  private:
69 
70  double epsabs;
71  double epsrel;
72 
73  };
74 
75  /** \brief Bracketing solver based the Boost implementation of TOMS
76  748
77 
78  This class currently uses \ref o2scl::gsl_tolerance as a test,
79  since this works even when the root is zero.
80  */
81  template<class func_t=funct11> class root_toms748 :
82  public root_bkt<func_t> {
83 
84  public:
85 
86  root_toms748() {
87  }
88 
89  virtual ~root_toms748() {}
90 
91  /// Return the type, \c "root_toms748".
92  virtual const char *type() { return "root_toms748"; }
93 
94  /// Solve \c func using \c x as an initial guess, returning \c x.
95  virtual int solve_bkt(double &x1, double x2, func_t &func) {
96  std::pair<double,double> res;
97  size_t digits;
98  if (this->tol_rel>1.0) digits=1;
99  else if (this->tol_rel<=0.0) digits=18;
100  else digits=((size_t)(-log10(this->tol_rel)));
101  gsl_tolerance<double> tol(this->tol_abs,this->tol_rel);
102  //boost::math::tools::eps_tolerance<double> tol(digits);
103  size_t niter=((size_t)this->ntrial);
104  res=boost::math::tools::toms748_solve(func,x1,x2,tol,niter);
105  this->last_ntrial=niter;
106  x1=res.first;
107  return 0;
108  }
109 
110  };
111 
112 }
113 
114 #endif
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
One-dimensional bracketing solver [abstract base].
Definition: root.h:145
Convergence test similar to gsl_root_test_interval() for root_toms748.
Definition: root_toms748.h:40
virtual int solve_bkt(double &x1, double x2, func_t &func)
Solve func using x as an initial guess, returning x.
Definition: root_toms748.h:95
virtual const char * type()
Return the type, "root_toms748".
Definition: root_toms748.h:92
Bracketing solver based the Boost implementation of TOMS 748.
Definition: root_toms748.h:81
static const double x2[5]
Definition: inte_qng_gsl.h:66
static const double x1[5]
Definition: inte_qng_gsl.h:48

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).