derivativeRootFinder.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_DERIVATIVEROOTFINDER_HPP
2 #define JULIAN_DERIVATIVEROOTFINDER_HPP
3 
4 #include <gsl/gsl_errno.h>
5 #include <gsl/gsl_math.h>
6 #include <gsl/gsl_roots.h>
9 #include <gsl/gsl_deriv.h>
10 
11 namespace julian {
12 
49  SECANT,
50  STEFFENSEN
51  };
52 
70  template<typename T, typename U>
71  double derivativeRootFinder(T f, U df, double x_0, DerivativeRootFinder type,double precision = 1e-12, int max_iter = 100) {
73  int status;
74  int iter = 0;
75  double x = x_0;
76  const gsl_root_fdfsolver_type *Type = gsl_root_fdfsolver_newton;
77  gsl_root_fdfsolver *s;
78 
79  switch (type) {
80  case DerivativeRootFinder::NEWTON_RAPHSON: Type = gsl_root_fdfsolver_newton; break;
81  case DerivativeRootFinder::SECANT: Type = gsl_root_fdfsolver_secant; break;
82  case DerivativeRootFinder::STEFFENSEN: Type = gsl_root_fdfsolver_steffenson; break;
83  }
84 
85  s = gsl_root_fdfsolver_alloc (Type);
86  gsl_root_fdfsolver_set (s, &Fdf, x_0);
87 
88  do {
89  iter++;
90  status = gsl_root_fdfsolver_iterate (s);
91  x_0 = x;
92  x = gsl_root_fdfsolver_root (s);
93  status = gsl_root_test_delta (x, x_0, 0, precision);
94  } while (status == GSL_CONTINUE && iter < max_iter);
95 
96  gsl_root_fdfsolver_free (s);
97  return x;
98  }
99 
121  template<typename T>
122  double derivativeRootFinder(T f, double x_0, DerivativeRootFinder type,double precision = 1e-12, int max_iter = 100, double h = 1e-6) {
124  gsl_function *gsl_f = static_cast<gsl_function*>(&pF);
125 
126  auto df = [&](double x)->double{
127  double result, abserr;
128  gsl_deriv_forward (gsl_f, x, h, &result, &abserr);
129  return result;
130  };
131 
133 
134  int status;
135  int iter = 0;
136  double x = x_0;
137  const gsl_root_fdfsolver_type *Type;
138  gsl_root_fdfsolver *s;
139 
140  switch (type) {
141  case DerivativeRootFinder::NEWTON_RAPHSON: Type = gsl_root_fdfsolver_newton; break;
142  case DerivativeRootFinder::SECANT : Type = gsl_root_fdfsolver_secant; break;
143  case DerivativeRootFinder::STEFFENSEN : Type = gsl_root_fdfsolver_steffenson; break;
144  }
145 
146  s = gsl_root_fdfsolver_alloc (Type);
147  gsl_root_fdfsolver_set (s, &Fdf, x_0);
148 
149  do {
150  iter++;
151  status = gsl_root_fdfsolver_iterate (s);
152  x_0 = x;
153  x = gsl_root_fdfsolver_root (s);
154  status = gsl_root_test_delta (x, x_0, 0, precision);
155  } while (status == GSL_CONTINUE && iter < max_iter);
156 
157  gsl_root_fdfsolver_free (s);
158  return x;
159  }
160 } // namespace julian
161 #endif
Class implements adapter for gsl_function_fdf.
Definition: GslFunctionFdfAdapter.hpp:23
File contains adapter of GSL Function.
Definition: cadHoliday.cpp:3
Class implements adapter for gsl_function.
Definition: GslFunctionAdapter.hpp:25
DerivativeRootFinder
Types of root finding algorithms using derivatives.
Definition: derivativeRootFinder.hpp:48
double derivativeRootFinder(T f, double x_0, DerivativeRootFinder type, double precision=1e-12, int max_iter=100, double h=1e-6)
Function finds roots basing on derivative.
Definition: derivativeRootFinder.hpp:122
File contains adapter of GSL Function.