• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Automatic Differentiation</title>
5<link rel="stylesheet" href="../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../quadrature.html" title="Chapter 13. Quadrature and Differentiation">
9<link rel="prev" href="diff.html" title="Numerical Differentiation">
10<link rel="next" href="diff0.html" title="Lanczos Smoothing Derivatives">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="diff.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quadrature.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="diff0.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="math_toolkit.autodiff"></a><a class="link" href="autodiff.html" title="Automatic Differentiation">Automatic Differentiation</a>
28</h2></div></div></div>
29<h2>
30<a name="math_toolkit.autodiff.h0"></a>
31      <span class="phrase"><a name="math_toolkit.autodiff.synopsis"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.synopsis">Synopsis</a>
32    </h2>
33<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">differentiation</span><span class="special">/</span><span class="identifier">autodiff</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
34
35<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
36<span class="keyword">namespace</span> <span class="identifier">math</span> <span class="special">{</span>
37<span class="keyword">namespace</span> <span class="identifier">differentiation</span> <span class="special">{</span>
38
39<span class="comment">// Function returning a single variable of differentiation. Recommended: Use auto for type.</span>
40<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">Order</span><span class="special">,</span> <span class="identifier">size_t</span><span class="special">...</span> <span class="identifier">Orders</span><span class="special">&gt;</span>
41<span class="identifier">autodiff_fvar</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">,</span> <span class="identifier">Orders</span><span class="special">...&gt;</span> <span class="identifier">make_fvar</span><span class="special">(</span><span class="identifier">RealType</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">ca</span><span class="special">);</span>
42
43<span class="comment">// Function returning multiple independent variables of differentiation in a std::tuple.</span>
44<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">size_t</span><span class="special">...</span> <span class="identifier">Orders</span><span class="special">,</span> <span class="keyword">typename</span><span class="special">...</span> <span class="identifier">RealTypes</span><span class="special">&gt;</span>
45<span class="keyword">auto</span> <span class="identifier">make_ftuple</span><span class="special">(</span><span class="identifier">RealTypes</span> <span class="keyword">const</span><span class="special">&amp;...</span> <span class="identifier">ca</span><span class="special">);</span>
46
47<span class="comment">// Type of combined autodiff types. Recommended: Use auto for return type (C++14).</span>
48<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">typename</span><span class="special">...</span> <span class="identifier">RealTypes</span><span class="special">&gt;</span>
49<span class="keyword">using</span> <span class="identifier">promote</span> <span class="special">=</span> <span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">promote_args_n</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">RealTypes</span><span class="special">...&gt;::</span><span class="identifier">type</span><span class="special">;</span>
50
51<span class="keyword">namespace</span> <span class="identifier">detail</span> <span class="special">{</span>
52
53<span class="comment">// Single autodiff variable. Use make_fvar() or make_ftuple() to instantiate.</span>
54<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">Order</span><span class="special">&gt;</span>
55<span class="keyword">class</span> <span class="identifier">fvar</span> <span class="special">{</span>
56 <span class="keyword">public</span><span class="special">:</span>
57  <span class="comment">// Query return value of function to get the derivatives.</span>
58  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span><span class="special">...</span> <span class="identifier">Orders</span><span class="special">&gt;</span>
59  <span class="identifier">get_type_at</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">sizeof</span><span class="special">...(</span><span class="identifier">Orders</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">&gt;</span> <span class="identifier">derivative</span><span class="special">(</span><span class="identifier">Orders</span><span class="special">...</span> <span class="identifier">orders</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
60
61  <span class="comment">// All of the arithmetic and comparison operators are overloaded.</span>
62  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType2</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">Order2</span><span class="special">&gt;</span>
63  <span class="identifier">fvar</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="identifier">fvar</span><span class="special">&lt;</span><span class="identifier">RealType2</span><span class="special">,</span> <span class="identifier">Order2</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
64
65  <span class="identifier">fvar</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="identifier">root_type</span> <span class="keyword">const</span><span class="special">&amp;);</span>
66
67  <span class="comment">// ...</span>
68<span class="special">};</span>
69
70<span class="comment">// Standard math functions are overloaded and called via argument-dependent lookup (ADL).</span>
71<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">Order</span><span class="special">&gt;</span>
72<span class="identifier">fvar</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;</span> <span class="identifier">floor</span><span class="special">(</span><span class="identifier">fvar</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
73
74<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">size_t</span> <span class="identifier">Order</span><span class="special">&gt;</span>
75<span class="identifier">fvar</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;</span> <span class="identifier">exp</span><span class="special">(</span><span class="identifier">fvar</span><span class="special">&lt;</span><span class="identifier">RealType</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;</span> <span class="keyword">const</span><span class="special">&amp;);</span>
76
77<span class="comment">// ...</span>
78
79<span class="special">}</span>  <span class="comment">// namespace detail</span>
80
81<span class="special">}</span>  <span class="comment">// namespace differentiation</span>
82<span class="special">}</span>  <span class="comment">// namespace math</span>
83<span class="special">}</span>  <span class="comment">// namespace boost</span>
84</pre>
85<h2>
86<a name="math_toolkit.autodiff.h1"></a>
87      <span class="phrase"><a name="math_toolkit.autodiff.description"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.description">Description</a>
88    </h2>
89<p>
90      Autodiff is a header-only C++ library that facilitates the <a href="https://en.wikipedia.org/wiki/Automatic_differentiation" target="_top">automatic
91      differentiation</a> (forward mode) of mathematical functions of single
92      and multiple variables.
93    </p>
94<p>
95      This implementation is based upon the <a href="https://en.wikipedia.org/wiki/Taylor_series" target="_top">Taylor
96      series</a> expansion of an analytic function <span class="emphasis"><em>f</em></span> at
97      the point <span class="emphasis"><em>x<sub>0</sub></em></span>:
98    </p>
99<div class="blockquote"><blockquote class="blockquote"><div class="blockquote"><blockquote class="blockquote"><p>
100          <span class="inlinemediaobject"><img src="../../equations/autodiff/taylor_series.svg"></span>
101        </p></blockquote></div></blockquote></div>
102<p>
103      The essential idea of autodiff is the substitution of numbers with polynomials
104      in the evaluation of <span class="emphasis"><em>f(x<sub>0</sub>)</em></span>. By substituting the number
105      <span class="emphasis"><em>x<sub>0</sub></em></span> with the first-order polynomial <span class="emphasis"><em>x<sub>0</sub>+ε</em></span>,
106      and using the same algorithm to compute <span class="emphasis"><em>f(x<sub>0</sub>+ε)</em></span>,
107      the resulting polynomial in <span class="emphasis"><em>ε</em></span> contains the function's
108      derivatives <span class="emphasis"><em>f'(x<sub>0</sub>)</em></span>, <span class="emphasis"><em>f''(x<sub>0</sub>)</em></span>, <span class="emphasis"><em>f'''(x<sub>0</sub>)</em></span>,
109      ... within the coefficients. Each coefficient is equal to the derivative of
110      its respective order, divided by the factorial of the order.
111    </p>
112<p>
113      In greater detail, assume one is interested in calculating the first <span class="emphasis"><em>N</em></span>
114      derivatives of <span class="emphasis"><em>f</em></span> at <span class="emphasis"><em>x<sub>0</sub></em></span>. Without loss
115      of precision to the calculation of the derivatives, all terms <span class="emphasis"><em>O(ε<sup>N+1</sup>)</em></span>
116      that include powers of <span class="emphasis"><em>ε</em></span> greater than <span class="emphasis"><em>N</em></span>
117      can be discarded. (This is due to the fact that each term in a polynomial depends
118      only upon equal and lower-order terms under arithmetic operations.) Under these
119      truncation rules, <span class="emphasis"><em>f</em></span> provides a polynomial-to-polynomial
120      transformation:
121    </p>
122<div class="blockquote"><blockquote class="blockquote"><div class="blockquote"><blockquote class="blockquote"><p>
123          <span class="inlinemediaobject"><img src="../../equations/autodiff/polynomial_transform.svg"></span>
124        </p></blockquote></div></blockquote></div>
125<p>
126      C++'s ability to overload operators and functions allows for the creation of
127      a class <code class="computeroutput"><span class="identifier">fvar</span></code> (<span class="underline">f</span>orward-mode
128      autodiff <span class="underline">var</span>iable) that represents polynomials
129      in <span class="emphasis"><em>ε</em></span>. Thus the same algorithm <span class="emphasis"><em>f</em></span>
130      that calculates the numeric value of <span class="emphasis"><em>y<sub>0</sub>=f(x<sub>0</sub>)</em></span>, when written
131      to accept and return variables of a generic (template) type, is also used to
132      calculate the polynomial <span class="emphasis"><em>Σ<sub>n</sub>y<sub>n</sub>ε<sup>n</sup>=f(x<sub>0</sub>+ε)</em></span>.
133      The derivatives <span class="emphasis"><em>f<sup>(n)</sup>(x<sub>0</sub>)</em></span> are then found from the product
134      of the respective factorial <span class="emphasis"><em>n!</em></span> and coefficient <span class="emphasis"><em>y<sub>n</sub></em></span>:
135    </p>
136<div class="blockquote"><blockquote class="blockquote"><div class="blockquote"><blockquote class="blockquote"><p>
137          <span class="inlinemediaobject"><img src="../../equations/autodiff/derivative_formula.svg"></span>
138        </p></blockquote></div></blockquote></div>
139<h2>
140<a name="math_toolkit.autodiff.h2"></a>
141      <span class="phrase"><a name="math_toolkit.autodiff.examples"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.examples">Examples</a>
142    </h2>
143<h3>
144<a name="math_toolkit.autodiff.h3"></a>
145      <span class="phrase"><a name="math_toolkit.autodiff.example-single-variable"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.example-single-variable">Example
146      1: Single-variable derivatives</a>
147    </h3>
148<h4>
149<a name="math_toolkit.autodiff.h4"></a>
150      <span class="phrase"><a name="math_toolkit.autodiff.calculate_derivatives_of_f_x_x_s"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.calculate_derivatives_of_f_x_x_s">Calculate
151      derivatives of <span class="emphasis"><em>f(x)=x<sup>4</sup></em></span> at <span class="emphasis"><em>x</em></span>=2.</a>
152    </h4>
153<p>
154      In this example, <code class="computeroutput"><span class="identifier">make_fvar</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span>
155      <span class="identifier">Order</span><span class="special">&gt;(</span><span class="number">2.0</span><span class="special">)</span></code> instantiates
156      the polynomial 2+<span class="emphasis"><em>ε</em></span>. The <code class="computeroutput"><span class="identifier">Order</span><span class="special">=</span><span class="number">5</span></code> means that
157      enough space is allocated (on the stack) to hold a polynomial of up to degree
158      5 during the proceeding computation.
159    </p>
160<p>
161      Internally, this is modeled by a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span><span class="number">6</span><span class="special">&gt;</span></code> whose elements <code class="computeroutput"><span class="special">{</span><span class="number">2</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span>
162      <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">}</span></code>
163      correspond to the 6 coefficients of the polynomial upon initialization. Its
164      fourth power, at the end of the computation, is a polynomial with coefficients
165      <code class="computeroutput"><span class="identifier">y</span> <span class="special">=</span>
166      <span class="special">{</span><span class="number">16</span><span class="special">,</span>
167      <span class="number">32</span><span class="special">,</span> <span class="number">24</span><span class="special">,</span> <span class="number">8</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span>
168      <span class="number">0</span><span class="special">}</span></code>. The
169      derivatives are obtained using the formula <span class="emphasis"><em>f<sup>(n)</sup>(2)=n!*y[n]</em></span>.
170    </p>
171<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">differentiation</span><span class="special">/</span><span class="identifier">autodiff</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
172<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
173
174<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
175<span class="identifier">T</span> <span class="identifier">fourth_power</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
176  <span class="identifier">T</span> <span class="identifier">x4</span> <span class="special">=</span> <span class="identifier">x</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">;</span>  <span class="comment">// retval in operator*() uses x4's memory via NRVO.</span>
177  <span class="identifier">x4</span> <span class="special">*=</span> <span class="identifier">x4</span><span class="special">;</span>      <span class="comment">// No copies of x4 are made within operator*=() even when squaring.</span>
178  <span class="keyword">return</span> <span class="identifier">x4</span><span class="special">;</span>     <span class="comment">// x4 uses y's memory in main() via NRVO.</span>
179<span class="special">}</span>
180
181<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
182  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">differentiation</span><span class="special">;</span>
183
184  <span class="keyword">constexpr</span> <span class="keyword">unsigned</span> <span class="identifier">Order</span> <span class="special">=</span> <span class="number">5</span><span class="special">;</span>                  <span class="comment">// Highest order derivative to be calculated.</span>
185  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">make_fvar</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">Order</span><span class="special">&gt;(</span><span class="number">2.0</span><span class="special">);</span>  <span class="comment">// Find derivatives at x=2.</span>
186  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">fourth_power</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
187  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;=</span> <span class="identifier">Order</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
188    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"y.derivative("</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">") = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
189  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
190<span class="special">}</span>
191<span class="comment">/*
192Output:
193y.derivative(0) = 16
194y.derivative(1) = 32
195y.derivative(2) = 48
196y.derivative(3) = 48
197y.derivative(4) = 24
198y.derivative(5) = 0
199*/</span>
200</pre>
201<p>
202      The above calculates
203    </p>
204<div class="blockquote"><blockquote class="blockquote"><div class="blockquote"><blockquote class="blockquote"><p>
205          <span class="inlinemediaobject"><img src="../../equations/autodiff/example1.svg"></span>
206        </p></blockquote></div></blockquote></div>
207<h3>
208<a name="math_toolkit.autodiff.h5"></a>
209      <span class="phrase"><a name="math_toolkit.autodiff.example-multiprecision"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.example-multiprecision">Example
210      2: Multi-variable mixed partial derivatives with multi-precision data type</a>
211    </h3>
212<h4>
213<a name="math_toolkit.autodiff.h6"></a>
214      <span class="phrase"><a name="math_toolkit.autodiff.calculate_autodiff_equation_mixe"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.calculate_autodiff_equation_mixe">Calculate
215      <span class="inlinemediaobject"><img src="../../equations/autodiff/mixed12.svg"></span> with a precision of about 50 decimal digits, where <span class="inlinemediaobject"><img src="../../equations/autodiff/example2f.svg"></span>.</a>
216    </h4>
217<p>
218      In this example, <code class="computeroutput"><span class="identifier">make_ftuple</span><span class="special">&lt;</span><span class="identifier">float50</span><span class="special">,</span> <span class="identifier">Nw</span><span class="special">,</span>
219      <span class="identifier">Nx</span><span class="special">,</span> <span class="identifier">Ny</span><span class="special">,</span> <span class="identifier">Nz</span><span class="special">&gt;(</span><span class="number">11</span><span class="special">,</span>
220      <span class="number">12</span><span class="special">,</span> <span class="number">13</span><span class="special">,</span> <span class="number">14</span><span class="special">)</span></code> returns a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span></code> of
221      4 independent <code class="computeroutput"><span class="identifier">fvar</span></code> variables,
222      with values of 11, 12, 13, and 14, for which the maximum order derivative to
223      be calculated for each are 3, 2, 4, 3, respectively. The order of the variables
224      is important, as it is the same order used when calling <code class="computeroutput"><span class="identifier">v</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="identifier">Nw</span><span class="special">,</span>
225      <span class="identifier">Nx</span><span class="special">,</span> <span class="identifier">Ny</span><span class="special">,</span> <span class="identifier">Nz</span><span class="special">)</span></code> in the example below.
226    </p>
227<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">differentiation</span><span class="special">/</span><span class="identifier">autodiff</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
228<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
229<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
230
231<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">differentiation</span><span class="special">;</span>
232
233<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">W</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">X</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Y</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Z</span><span class="special">&gt;</span>
234<span class="identifier">promote</span><span class="special">&lt;</span><span class="identifier">W</span><span class="special">,</span> <span class="identifier">X</span><span class="special">,</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">Z</span><span class="special">&gt;</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">W</span><span class="special">&amp;</span> <span class="identifier">w</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">X</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Y</span><span class="special">&amp;</span> <span class="identifier">y</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Z</span><span class="special">&amp;</span> <span class="identifier">z</span><span class="special">)</span> <span class="special">{</span>
235  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
236  <span class="keyword">return</span> <span class="identifier">exp</span><span class="special">(</span><span class="identifier">w</span> <span class="special">*</span> <span class="identifier">sin</span><span class="special">(</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span> <span class="special">/</span> <span class="identifier">z</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">w</span> <span class="special">*</span> <span class="identifier">z</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">y</span><span class="special">)))</span> <span class="special">+</span> <span class="identifier">w</span> <span class="special">*</span> <span class="identifier">w</span> <span class="special">/</span> <span class="identifier">tan</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span>
237<span class="special">}</span>
238
239<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
240  <span class="keyword">using</span> <span class="identifier">float50</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_float_50</span><span class="special">;</span>
241
242  <span class="keyword">constexpr</span> <span class="keyword">unsigned</span> <span class="identifier">Nw</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span>  <span class="comment">// Max order of derivative to calculate for w</span>
243  <span class="keyword">constexpr</span> <span class="keyword">unsigned</span> <span class="identifier">Nx</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>  <span class="comment">// Max order of derivative to calculate for x</span>
244  <span class="keyword">constexpr</span> <span class="keyword">unsigned</span> <span class="identifier">Ny</span> <span class="special">=</span> <span class="number">4</span><span class="special">;</span>  <span class="comment">// Max order of derivative to calculate for y</span>
245  <span class="keyword">constexpr</span> <span class="keyword">unsigned</span> <span class="identifier">Nz</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span>  <span class="comment">// Max order of derivative to calculate for z</span>
246  <span class="comment">// Declare 4 independent variables together into a std::tuple.</span>
247  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">variables</span> <span class="special">=</span> <span class="identifier">make_ftuple</span><span class="special">&lt;</span><span class="identifier">float50</span><span class="special">,</span> <span class="identifier">Nw</span><span class="special">,</span> <span class="identifier">Nx</span><span class="special">,</span> <span class="identifier">Ny</span><span class="special">,</span> <span class="identifier">Nz</span><span class="special">&gt;(</span><span class="number">11</span><span class="special">,</span> <span class="number">12</span><span class="special">,</span> <span class="number">13</span><span class="special">,</span> <span class="number">14</span><span class="special">);</span>
248  <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">w</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;(</span><span class="identifier">variables</span><span class="special">);</span>  <span class="comment">// Up to Nw derivatives at w=11</span>
249  <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;(</span><span class="identifier">variables</span><span class="special">);</span>  <span class="comment">// Up to Nx derivatives at x=12</span>
250  <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;(</span><span class="identifier">variables</span><span class="special">);</span>  <span class="comment">// Up to Ny derivatives at y=13</span>
251  <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">3</span><span class="special">&gt;(</span><span class="identifier">variables</span><span class="special">);</span>  <span class="comment">// Up to Nz derivatives at z=14</span>
252  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">w</span><span class="special">,</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">);</span>
253  <span class="comment">// Calculated from Mathematica symbolic differentiation.</span>
254  <span class="identifier">float50</span> <span class="keyword">const</span> <span class="identifier">answer</span><span class="special">(</span><span class="string">"1976.319600747797717779881875290418720908121189218755"</span><span class="special">);</span>
255  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">float50</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
256            <span class="special">&lt;&lt;</span> <span class="string">"mathematica   : "</span> <span class="special">&lt;&lt;</span> <span class="identifier">answer</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
257            <span class="special">&lt;&lt;</span> <span class="string">"autodiff      : "</span> <span class="special">&lt;&lt;</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="identifier">Nw</span><span class="special">,</span> <span class="identifier">Nx</span><span class="special">,</span> <span class="identifier">Ny</span><span class="special">,</span> <span class="identifier">Nz</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
258            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>
259            <span class="special">&lt;&lt;</span> <span class="string">"relative error: "</span> <span class="special">&lt;&lt;</span> <span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="identifier">Nw</span><span class="special">,</span> <span class="identifier">Nx</span><span class="special">,</span> <span class="identifier">Ny</span><span class="special">,</span> <span class="identifier">Nz</span><span class="special">)</span> <span class="special">/</span> <span class="identifier">answer</span> <span class="special">-</span> <span class="number">1</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
260  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
261<span class="special">}</span>
262<span class="comment">/*
263Output:
264mathematica   : 1976.3196007477977177798818752904187209081211892188
265autodiff      : 1976.3196007477977177798818752904187209081211892188
266relative error: 2.67e-50
267*/</span>
268</pre>
269<h3>
270<a name="math_toolkit.autodiff.h7"></a>
271      <span class="phrase"><a name="math_toolkit.autodiff.example-black_scholes"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.example-black_scholes">Example
272      3: Black-Scholes Option Pricing with Greeks Automatically Calculated</a>
273    </h3>
274<h4>
275<a name="math_toolkit.autodiff.h8"></a>
276      <span class="phrase"><a name="math_toolkit.autodiff.calculate_greeks_directly_from_t"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.calculate_greeks_directly_from_t">Calculate
277      greeks directly from the Black-Scholes pricing function.</a>
278    </h4>
279<p>
280      Below is the standard Black-Scholes pricing function written as a function
281      template, where the price, volatility (sigma), time to expiration (tau) and
282      interest rate are template parameters. This means that any greek based on these
283      4 variables can be calculated using autodiff. The below example calculates
284      delta and gamma where the variable of differentiation is only the price. For
285      examples of more exotic greeks, see <code class="computeroutput"><span class="identifier">example</span><span class="special">/</span><span class="identifier">black_scholes</span><span class="special">.</span><span class="identifier">cpp</span></code>.
286    </p>
287<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">differentiation</span><span class="special">/</span><span class="identifier">autodiff</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
288<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
289
290<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">;</span>
291<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">differentiation</span><span class="special">;</span>
292
293<span class="comment">// Equations and function/variable names are from</span>
294<span class="comment">// https://en.wikipedia.org/wiki/Greeks_(finance)#Formulas_for_European_option_Greeks</span>
295
296<span class="comment">// Standard normal cumulative distribution function</span>
297<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">X</span><span class="special">&gt;</span>
298<span class="identifier">X</span> <span class="identifier">Phi</span><span class="special">(</span><span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
299  <span class="keyword">return</span> <span class="number">0.5</span> <span class="special">*</span> <span class="identifier">erfc</span><span class="special">(-</span><span class="identifier">one_div_root_two</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;()</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">);</span>
300<span class="special">}</span>
301
302<span class="keyword">enum</span> <span class="keyword">class</span> <span class="identifier">CP</span> <span class="special">{</span> <span class="identifier">call</span><span class="special">,</span> <span class="identifier">put</span> <span class="special">};</span>
303
304<span class="comment">// Assume zero annual dividend yield (q=0).</span>
305<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Price</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Sigma</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Tau</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Rate</span><span class="special">&gt;</span>
306<span class="identifier">promote</span><span class="special">&lt;</span><span class="identifier">Price</span><span class="special">,</span> <span class="identifier">Sigma</span><span class="special">,</span> <span class="identifier">Tau</span><span class="special">,</span> <span class="identifier">Rate</span><span class="special">&gt;</span> <span class="identifier">black_scholes_option_price</span><span class="special">(</span><span class="identifier">CP</span> <span class="identifier">cp</span><span class="special">,</span>
307                                                            <span class="keyword">double</span> <span class="identifier">K</span><span class="special">,</span>
308                                                            <span class="identifier">Price</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">S</span><span class="special">,</span>
309                                                            <span class="identifier">Sigma</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">sigma</span><span class="special">,</span>
310                                                            <span class="identifier">Tau</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">tau</span><span class="special">,</span>
311                                                            <span class="identifier">Rate</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">r</span><span class="special">)</span> <span class="special">{</span>
312  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
313  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">d1</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">log</span><span class="special">(</span><span class="identifier">S</span> <span class="special">/</span> <span class="identifier">K</span><span class="special">)</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">r</span> <span class="special">+</span> <span class="identifier">sigma</span> <span class="special">*</span> <span class="identifier">sigma</span> <span class="special">/</span> <span class="number">2</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">tau</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">sigma</span> <span class="special">*</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">tau</span><span class="special">));</span>
314  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">d2</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">log</span><span class="special">(</span><span class="identifier">S</span> <span class="special">/</span> <span class="identifier">K</span><span class="special">)</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">r</span> <span class="special">-</span> <span class="identifier">sigma</span> <span class="special">*</span> <span class="identifier">sigma</span> <span class="special">/</span> <span class="number">2</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">tau</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">sigma</span> <span class="special">*</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">tau</span><span class="special">));</span>
315  <span class="keyword">switch</span> <span class="special">(</span><span class="identifier">cp</span><span class="special">)</span> <span class="special">{</span>
316    <span class="keyword">case</span> <span class="identifier">CP</span><span class="special">::</span><span class="identifier">call</span><span class="special">:</span>
317      <span class="keyword">return</span> <span class="identifier">S</span> <span class="special">*</span> <span class="identifier">Phi</span><span class="special">(</span><span class="identifier">d1</span><span class="special">)</span> <span class="special">-</span> <span class="identifier">exp</span><span class="special">(-</span><span class="identifier">r</span> <span class="special">*</span> <span class="identifier">tau</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">K</span> <span class="special">*</span> <span class="identifier">Phi</span><span class="special">(</span><span class="identifier">d2</span><span class="special">);</span>
318    <span class="keyword">case</span> <span class="identifier">CP</span><span class="special">::</span><span class="identifier">put</span><span class="special">:</span>
319      <span class="keyword">return</span> <span class="identifier">exp</span><span class="special">(-</span><span class="identifier">r</span> <span class="special">*</span> <span class="identifier">tau</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">K</span> <span class="special">*</span> <span class="identifier">Phi</span><span class="special">(-</span><span class="identifier">d2</span><span class="special">)</span> <span class="special">-</span> <span class="identifier">S</span> <span class="special">*</span> <span class="identifier">Phi</span><span class="special">(-</span><span class="identifier">d1</span><span class="special">);</span>
320  <span class="special">}</span>
321<span class="special">}</span>
322
323<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
324  <span class="keyword">double</span> <span class="keyword">const</span> <span class="identifier">K</span> <span class="special">=</span> <span class="number">100.0</span><span class="special">;</span>                    <span class="comment">// Strike price.</span>
325  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">S</span> <span class="special">=</span> <span class="identifier">make_fvar</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;(</span><span class="number">105</span><span class="special">);</span>  <span class="comment">// Stock price.</span>
326  <span class="keyword">double</span> <span class="keyword">const</span> <span class="identifier">sigma</span> <span class="special">=</span> <span class="number">5</span><span class="special">;</span>                    <span class="comment">// Volatility.</span>
327  <span class="keyword">double</span> <span class="keyword">const</span> <span class="identifier">tau</span> <span class="special">=</span> <span class="number">30.0</span> <span class="special">/</span> <span class="number">365</span><span class="special">;</span>             <span class="comment">// Time to expiration in years. (30 days).</span>
328  <span class="keyword">double</span> <span class="keyword">const</span> <span class="identifier">r</span> <span class="special">=</span> <span class="number">1.25</span> <span class="special">/</span> <span class="number">100</span><span class="special">;</span>               <span class="comment">// Interest rate.</span>
329  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">call_price</span> <span class="special">=</span> <span class="identifier">black_scholes_option_price</span><span class="special">(</span><span class="identifier">CP</span><span class="special">::</span><span class="identifier">call</span><span class="special">,</span> <span class="identifier">K</span><span class="special">,</span> <span class="identifier">S</span><span class="special">,</span> <span class="identifier">sigma</span><span class="special">,</span> <span class="identifier">tau</span><span class="special">,</span> <span class="identifier">r</span><span class="special">);</span>
330  <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">put_price</span> <span class="special">=</span> <span class="identifier">black_scholes_option_price</span><span class="special">(</span><span class="identifier">CP</span><span class="special">::</span><span class="identifier">put</span><span class="special">,</span> <span class="identifier">K</span><span class="special">,</span> <span class="identifier">S</span><span class="special">,</span> <span class="identifier">sigma</span><span class="special">,</span> <span class="identifier">tau</span><span class="special">,</span> <span class="identifier">r</span><span class="special">);</span>
331
332  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"black-scholes call price = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">call_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
333            <span class="special">&lt;&lt;</span> <span class="string">"black-scholes put  price = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">put_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
334            <span class="special">&lt;&lt;</span> <span class="string">"call delta = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">call_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
335            <span class="special">&lt;&lt;</span> <span class="string">"put  delta = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">put_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
336            <span class="special">&lt;&lt;</span> <span class="string">"call gamma = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">call_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">2</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span>
337            <span class="special">&lt;&lt;</span> <span class="string">"put  gamma = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">put_price</span><span class="special">.</span><span class="identifier">derivative</span><span class="special">(</span><span class="number">2</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
338  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
339<span class="special">}</span>
340<span class="comment">/*
341Output:
342black-scholes call price = 56.5136
343black-scholes put  price = 51.4109
344call delta = 0.773818
345put  delta = -0.226182
346call gamma = 0.00199852
347put  gamma = 0.00199852
348*/</span>
349</pre>
350<h2>
351<a name="math_toolkit.autodiff.h9"></a>
352      <span class="phrase"><a name="math_toolkit.autodiff.advantages_of_automatic_differen"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.advantages_of_automatic_differen">Advantages
353      of Automatic Differentiation</a>
354    </h2>
355<p>
356      The above examples illustrate some of the advantages of using autodiff:
357    </p>
358<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
359<li class="listitem">
360          Elimination of code redundancy. The existence of <span class="emphasis"><em>N</em></span>
361          separate functions to calculate derivatives is a form of code redundancy,
362          with all the liabilities that come with it:
363          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
364<li class="listitem">
365                Changes to one function require <span class="emphasis"><em>N</em></span> additional
366                changes to other functions. In the 3rd example above, consider how
367                much larger and inter-dependent the above code base would be if a
368                separate function were written for <a href="https://en.wikipedia.org/wiki/Greeks_(finance)#Formulas_for_European_option_Greeks" target="_top">each
369                Greek</a> value.
370              </li>
371<li class="listitem">
372                Dependencies upon a derivative function for a different purpose will
373                break when changes are made to the original function. What doesn't
374                need to exist cannot break.
375              </li>
376<li class="listitem">
377                Code bloat, reducing conceptual integrity. Control over the evolution
378                of code is easier/safer when the code base is smaller and able to
379                be intuitively grasped.
380              </li>
381</ul></div>
382        </li>
383<li class="listitem">
384          Accuracy of derivatives over finite difference methods. Single-iteration
385          finite difference methods always include a <span class="emphasis"><em>Δx</em></span>
386          free variable that must be carefully chosen for each application. If <span class="emphasis"><em>Δx</em></span>
387          is too small, then numerical errors become large. If <span class="emphasis"><em>Δx</em></span>
388          is too large, then mathematical errors become large. With autodiff, there
389          are no free variables to set and the accuracy of the answer is generally
390          superior to finite difference methods even with the best choice of <span class="emphasis"><em>Δx</em></span>.
391        </li>
392</ul></div>
393<h2>
394<a name="math_toolkit.autodiff.h10"></a>
395      <span class="phrase"><a name="math_toolkit.autodiff.manual"></a></span><a class="link" href="autodiff.html#math_toolkit.autodiff.manual">Manual</a>
396    </h2>
397<p>
398      Additional details are in the <a href="../../differentiation/autodiff.pdf" target="_top">autodiff
399      manual</a>.
400    </p>
401</div>
402<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
403<td align="left"></td>
404<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
405      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
406      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
407      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
408      Daryle Walker and Xiaogang Zhang<p>
409        Distributed under the Boost Software License, Version 1.0. (See accompanying
410        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
411      </p>
412</div></td>
413</tr></table>
414<hr>
415<div class="spirit-nav">
416<a accesskey="p" href="diff.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quadrature.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="diff0.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
417</div>
418</body>
419</html>
420