\(\newcommand{\W}[1]{ \; #1 \; }\) \(\newcommand{\R}[1]{ {\rm #1} }\) \(\newcommand{\B}[1]{ {\bf #1} }\) \(\newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} }\) \(\newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} }\) \(\newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} }\) \(\newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }\)
exp_eps¶
View page sourceAn Epsilon Accurate Exponential Approximation¶
Syntax¶
include
"exp_eps.hpp"
exp_eps
( x , epsilon )Purpose¶
This is a an example algorithm that is used to demonstrate how Algorithmic Differentiation works with loops and boolean decision variables (see exp_2 for a simpler example).
Mathematical Function¶
The exponential function can be defined by
We define \(k ( x, \varepsilon )\) as the smallest non-negative integer such that \(\varepsilon \geq x^k / k !\); i.e.,
The mathematical form for our approximation of the exponential function is
include¶
The include command in the syntax is relative to
cppad-
yyyymmdd /introduction/exp_apx
where cppad-
yyyymmdd is the distribution directory
created during the beginning steps of the
installation of CppAD.
x¶
The argument x has prototype
const
Type & x
(see Type below). It specifies the point at which to evaluate the approximation for the exponential function.
epsilon¶
The argument epsilon has prototype
const
Type & epsilon
It specifies the accuracy with which to approximate the exponential function value; i.e., it is the value of \(\varepsilon\) in the exponential function approximation defined above.
y¶
The result y has prototype
Type y
It is the value of the exponential function approximation defined above.
Type¶
If u and v are Type objects and i
is an int
:
Operation |
Result Type |
Description |
Type ( i ) |
Type |
object with value equal to i |
Type u = v |
Type |
construct u with value equal to v |
u > v |
|
true, if u greater than v , an false otherwise |
u = v |
Type |
new u (and result) is value of v |
u * v |
Type |
result is value of \(u * v\) |
u / v |
Type |
result is value of \(u / v\) |
u + v |
Type |
result is value of \(u + v\) |
|
Type |
result is value of \(- u\) |
Implementation¶
The file exp_eps.hpp contains a C++ implementation of this function.
Test¶
The file exp_eps.cpp contains a test of this implementation.
Exercises¶
Using the definition of \(k( x, \varepsilon )\) above, what is the value of \(k(.5, 1)\), \(k(.5, .1)\), and \(k(.5, .01)\) ?
Suppose that we make the following call to
exp_eps
:double x = 1.; double epsilon = .01; double y = exp_eps(x, epsilon);
What is the value assigned to
k
,temp
,term
, andsum
the first time through thewhile
loop in exp_eps.hpp ?Continuing the previous exercise, what is the value assigned to
k
,temp
,term
, andsum
the second time through thewhile
loop in exp_eps.hpp ?