Theoretica
A C++ numerical and automatic mathematical library
taylor.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_TAYLOR_H
7 #define THEORETICA_TAYLOR_H
8 
9 #include "../polynomial/polynomial.h"
10 #include "../autodiff/dual.h"
11 #include "../autodiff/dual2.h"
12 
13 
14 namespace theoretica {
15 
17  namespace taylor {
18 
19 
29  template<typename DualFunction>
30  inline polynomial<real> expand_linear(DualFunction f, real x0 = 0) {
31 
32  dual d = f(dual(x0, 1));
33  real fx = d.Re();
34  real dfx = d.Dual();
35 
36  polynomial<real> P = {fx};
37  P += polynomial<>({-x0, 1}) * dfx;
38 
39  return P;
40  }
41 
42 
52  template<typename Dual2Function>
53  inline polynomial<real> expand_quadratic(Dual2Function f, real x0 = 0) {
54 
55  dual2 d = f(dual2(x0, 1, 0));
56  real fx = d.Re();
57  real dfx = d.Dual1();
58  real d2fx = d.Dual2();
59 
60  polynomial<real> P = {fx};
61  P += polynomial<>({-x0, 1}) * dfx;
62  P += polynomial<>({square(x0), -2 * x0, 1}) * (d2fx / 2.0);
63 
64  return P;
65  }
66 
67  }
68 
69 }
70 
71 
72 #endif
Second order dual number class.
Definition: dual2.h:29
real Dual2() const
Return second order dual part.
Definition: dual2.h:95
real Re() const
Return real part.
Definition: dual2.h:85
real Dual1() const
Return first order dual part.
Definition: dual2.h:90
Dual number class.
Definition: dual.h:28
const real & Re() const
Return real part.
Definition: dual.h:75
const real & Dual() const
Return dual part.
Definition: dual.h:96
A polynomial of arbitrary order.
Definition: polynomial.h:25
polynomial< real > expand_quadratic(Dual2Function f, real x0=0)
Computes the second order Taylor expansion of a generic function around x0, computed using dual numbe...
Definition: taylor.h:53
polynomial< real > expand_linear(DualFunction f, real x0=0)
Computes the first order Taylor expansion of a generic function around x0, computed using dual number...
Definition: taylor.h:30
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
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23