Theoretica
A C++ numerical and automatic mathematical library
deriv.h
Go to the documentation of this file.
1 
5 
6 #ifndef DERIVATION_THEORETICA_H
7 #define DERIVATION_THEORETICA_H
8 
9 #include "../core/function.h"
10 #include "../polynomial/polynomial.h"
11 
12 
13 namespace theoretica {
14 
15 
20  template<typename Field = real>
22 
23  if (p.coeff.size() == 0) {
24  TH_MATH_ERROR("deriv", p.coeff.size(), INVALID_ARGUMENT);
25  return polynomial<Field>({ static_cast<Field>(nan()) });
26  }
27 
28  if (p.coeff.size() == 1)
29  return polynomial<Field>({static_cast<Field>(0.0)});
30 
32  Dp.coeff.resize(p.coeff.size() - 1);
33 
34  for (unsigned int i = 1; i < p.size(); ++i)
35  Dp.coeff[i - 1] = p[i] * i;
36 
37  return Dp;
38  }
39 
40 
48  template <
49  typename RealFunction = std::function<real(real)>,
50  enable_real_func<RealFunction> = true
51  >
52  inline real deriv_central(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
53  return (f(x + h) - f(x - h)) / (2.0 * h);
54  }
55 
56 
64  template <
65  typename RealFunction = std::function<real(real)>,
66  enable_real_func<RealFunction> = true
67  >
68  inline real deriv_forward(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
69  return (f(x + h) - f(x)) / h;
70  }
71 
72 
80  template <
81  typename RealFunction = std::function<real(real)>,
82  enable_real_func<RealFunction> = true
83  >
84  inline real deriv_backward(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
85  return (f(x) - f(x - h)) / h;
86  }
87 
88 
96  template <
97  typename RealFunction = std::function<real(real)>,
98  enable_real_func<RealFunction> = true
99  >
100  inline real deriv_ridders2(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
101  return (4.0 * deriv_central(f, x, h / 2.0) - deriv_central(f, x, h)) / 3.0;
102  }
103 
104 
113  template <
114  typename RealFunction = std::function<real(real)>,
115  enable_real_func<RealFunction> = true
116  >
117  inline real deriv_ridders(RealFunction f, real x, real h = 0.01, unsigned int degree = 3) {
118 
119  real A[degree][degree];
120 
121  for (unsigned int i = 0; i < degree; ++i) {
122 
123  for (unsigned int n = 0; n <= i; ++n) {
124 
125  unsigned int m = i - n;
126 
127  if(n == 0) {
128  A[n][m] = deriv_central(f, x, h / (1 << m));
129  } else {
130  real coeff = square(1 << n);
131  A[n][m] = (coeff * A[n - 1][m + 1] - A[n - 1][m]) / (coeff - 1);
132  }
133  }
134 
135  }
136 
137  return A[degree - 1][0];
138  }
139 
140 
148  template <
149  typename RealFunction = std::function<real(real)>,
150  enable_real_func<RealFunction> = true
151  >
152  inline real deriv(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
153  return deriv_ridders2(f, x, h);
154  }
155 
156 
164  template <
165  typename RealFunction = std::function<real(real)>,
166  enable_real_func<RealFunction> = true
167  >
168  inline real deriv2(RealFunction f, real x, real h = CALCULUS_DERIV_STEP) {
169  return (f(x + h) - (2 * f(x)) + f(x - h)) / (h * h);
170  }
171 
172 }
173 
174 
175 #endif
A polynomial of arbitrary order.
Definition: polynomial.h:25
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real deriv_forward(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the forward method.
Definition: deriv.h:68
constexpr real CALCULUS_DERIV_STEP
Default variation for derivative approximation.
Definition: constants.h:302
real deriv_ridders(RealFunction f, real x, real h=0.01, unsigned int degree=3)
Approximate the first derivative of a real function using Ridder's method of arbitrary degree.
Definition: deriv.h:117
real deriv_ridders2(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using Ridder's method of second degree.
Definition: deriv.h:100
real deriv_central(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the central method.
Definition: deriv.h:52
real deriv2(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the second derivative of a real function using the best available algorithm.
Definition: deriv.h:168
real deriv_backward(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the backward method.
Definition: deriv.h:84
polynomial< Field > deriv(const polynomial< Field > &p)
Compute the exact derivative of a polynomial function.
Definition: deriv.h:21
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54