<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>exp_2: Verify Second Order Reverse Sweep</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="exp_2: Verify Second Order Reverse Sweep"/>
<meta name="keywords" id="keywords" content=" exp_2: verify second order reverse sweep exp_2 mode "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_exp_2_rev2.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="exp_2_rev2.xml" target="_top">Prev</a>
</td><td><a href="exp_2_cppad.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>Introduction</option>
<option>exp_2</option>
<option>exp_2_rev2</option>
<option>exp_2_rev2.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>Introduction-&gt;</option>
<option>exp_2</option>
<option>exp_eps</option>
<option>exp_apx.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>exp_2-&gt;</option>
<option>exp_2.hpp</option>
<option>exp_2.cpp</option>
<option>exp_2_for0</option>
<option>exp_2_for1</option>
<option>exp_2_rev1</option>
<option>exp_2_for2</option>
<option>exp_2_rev2</option>
<option>exp_2_cppad</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>exp_2_rev2-&gt;</option>
<option>exp_2_rev2.cpp</option>
</select>
</td>
<td>exp_2_rev2.cpp</td>
<td>Headings</td>
</tr></table><br/>
<center><b><big><big>exp_2: Verify Second Order Reverse Sweep</big></big></b></center>
<pre><tt><span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;cstddef&gt;</span>                 <span style="font-style: italic"><span style="color: #9A1900">// define size_t</span></span>
<span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;cmath&gt;</span>                   <span style="font-style: italic"><span style="color: #9A1900">// prototype for fabs</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">extern</span></span> <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">exp_2_for0</span></span><span style="color: #990000">(</span><span style="color: #009900">double</span> <span style="color: #990000">*</span>v0<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// computes zero order forward sweep</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">extern</span></span> <span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">exp_2_for1</span></span><span style="color: #990000">(</span><span style="color: #009900">double</span> <span style="color: #990000">*</span>v1<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// computes first order forward sweep</span></span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">exp_2_rev2</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// set the value of v0[j], v1[j] for j = 1 , ... , 5</span></span>
     <span style="color: #009900">double</span> v0<span style="color: #990000">[</span><span style="color: #993399">6</span><span style="color: #990000">],</span> v1<span style="color: #990000">[</span><span style="color: #993399">6</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">exp_2_for0</span></span><span style="color: #990000">(</span>v0<span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">exp_2_for1</span></span><span style="color: #990000">(</span>v1<span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// initial all partial derivatives as zero</span></span>
     <span style="color: #009900">double</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">6</span><span style="color: #990000">],</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">6</span><span style="color: #990000">];</span>
     <span style="color: #008080">size_t</span> j<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> <span style="color: #993399">6</span><span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     f_v0<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">.;</span>
          f_v1<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">.;</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// set partial derivative for f_5</span></span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_5 w.r.t v_5^1</span></span>

     <span style="font-style: italic"><span style="color: #9A1900">// f_4 = f_5( v_1^0 , ... , v_4^1 , v_2^0 + v_4^0 , v_2^1 + v_4^1 )</span></span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">0</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_4 w.r.t. v_2^0</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">0</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_4 w.r.t. v_4^0</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_4 w.r.t. v_2^1</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_4 w.r.t. v_4^1</span></span>

     <span style="font-style: italic"><span style="color: #9A1900">// f_3 = f_4( v_1^0 , ... , v_3^1, v_3^0 / 2 , v_3^1 / 2 )</span></span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">.;</span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">/</span> <span style="color: #993399">2</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">0</span><span style="color: #990000">.</span>  <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_3 w.r.t. v_3^0</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">0.5</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_3 w.r.t. v_3^1</span></span>

     <span style="font-style: italic"><span style="color: #9A1900">// f_2 = f_3(  v_1^0 , ... , v_2^1, v_1^0 * v_1^0 , 2 * v_1^0 * v_1^1 )</span></span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> v1<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">2</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>  <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_2 w.r.t. v_1^0</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">0.5</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_2 w.r.t. v_1^1</span></span>

     <span style="font-style: italic"><span style="color: #9A1900">// f_1 = f_2( v_1^0 , v_1^1 , 1 + v_1^0 , v_1^1 )</span></span>
     f_v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     f_v1<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">+=</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v0<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span> <span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_1 w.r.t. v_1^0</span></span>
     ok <span style="color: #990000">&amp;=</span> std<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">fabs</span></span><span style="color: #990000">(</span> f_v1<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">-</span> <span style="color: #993399">1.5</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;=</span> <span style="color: #993399">1e-10</span><span style="color: #990000">;</span> <span style="font-style: italic"><span style="color: #9A1900">// partial f_1 w.r.t. v_1^1</span></span>

     <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ok<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre>

<hr/>Input File: introduction/exp_2_rev2.cpp

</body>
</html>
