<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Old Matrix Multiply as a User Atomic Operation: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Old Matrix Multiply as a User Atomic Operation: Example and Test"/>
<meta name="keywords" id="keywords" content=" old matrix multiply user atomic operation: example test deprecated 2013-05-27 include file "/>
<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='_old_mat_mul.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="old_tan.cpp.xml" target="_top">Prev</a>
</td><td><a href="old_mat_mul.hpp.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>Appendix</option>
<option>deprecated</option>
<option>old_atomic</option>
<option>old_mat_mul.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>Appendix-&gt;</option>
<option>Faq</option>
<option>directory</option>
<option>Theory</option>
<option>glossary</option>
<option>Bib</option>
<option>wish_list</option>
<option>whats_new</option>
<option>deprecated</option>
<option>compare_c</option>
<option>numeric_ad</option>
<option>addon</option>
<option>License</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>deprecated-&gt;</option>
<option>include_deprecated</option>
<option>FunDeprecated</option>
<option>CompareChange</option>
<option>omp_max_thread</option>
<option>TrackNewDel</option>
<option>omp_alloc</option>
<option>memory_leak</option>
<option>epsilon</option>
<option>test_vector</option>
<option>cppad_ipopt_nlp</option>
<option>old_atomic</option>
<option>zdouble</option>
<option>autotools</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>old_atomic-&gt;</option>
<option>old_reciprocal.cpp</option>
<option>old_usead_1.cpp</option>
<option>old_usead_2.cpp</option>
<option>old_tan.cpp</option>
<option>old_mat_mul.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>old_mat_mul.cpp-&gt;</option>
<option>old_mat_mul.hpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Deprecated 2013-05-27</option>
<option>Include File</option>
</select>
</td>
</tr></table><br/>
<center><b><big><big>Old Matrix Multiply as a User Atomic Operation: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Deprecated 2013-05-27" id="Deprecated 2013-05-27">Deprecated 2013-05-27</a></big></b>
<br/>
This example has been deprecated;
use <a href="atomic_mat_mul.cpp.xml" target="_top"><span style='white-space: nowrap'>atomic_mat_mul.cpp</span></a>
 instead.


<br/>
<br/>
<b><big><a name="Include File" id="Include File">Include File</a></big></b>
<br/>
This routine uses the include file <a href="old_mat_mul.hpp.xml" target="_top"><span style='white-space: nowrap'>old_mat_mul.hpp</span></a>
.

<code><font color="blue">
<pre><tt><span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;cppad/cppad.hpp&gt;</span>
<span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">"old_mat_mul.hpp"</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">old_mat_mul</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-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>AD<span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// matrix sizes for this test</span></span>
     <span style="color: #008080">size_t</span> nr_result <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
     <span style="color: #008080">size_t</span> n_middle  <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>
     <span style="color: #008080">size_t</span> nc_result <span style="color: #990000">=</span> <span style="color: #993399">2</span><span style="color: #990000">;</span>

     <span style="font-style: italic"><span style="color: #9A1900">// declare the AD&lt;double&gt; vectors ax and ay and X</span></span>
     <span style="color: #008080">size_t</span> n <span style="color: #990000">=</span> nr_result <span style="color: #990000">*</span> n_middle <span style="color: #990000">+</span> n_middle <span style="color: #990000">*</span> nc_result<span style="color: #990000">;</span>
     <span style="color: #008080">size_t</span> m <span style="color: #990000">=</span> nr_result <span style="color: #990000">*</span> nc_result<span style="color: #990000">;</span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt; AD&lt;double&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">X</span></span><span style="color: #990000">(</span><span style="color: #993399">4</span><span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">ax</span></span><span style="color: #990000">(</span>n<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">ay</span></span><span style="color: #990000">(</span>m<span style="color: #990000">);</span>
     <span style="color: #008080">size_t</span> i<span style="color: #990000">,</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> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> j<span style="color: #990000">++)</span>
          X<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">(</span>j <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// X is the vector of independent variables</span></span>
     CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">Independent</span></span><span style="color: #990000">(</span>X<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">// left matrix</span></span>
     ax<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span>  <span style="color: #990000">=</span> X<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>  <span style="font-style: italic"><span style="color: #9A1900">// left[0,0]   = x[0] = 1</span></span>
     ax<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span>  <span style="color: #990000">=</span> X<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>  <span style="font-style: italic"><span style="color: #9A1900">// left[0,1]   = x[1] = 2</span></span>
     ax<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">5</span><span style="color: #990000">.;</span>    <span style="font-style: italic"><span style="color: #9A1900">// left[1,0]   = 5</span></span>
     ax<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">6</span><span style="color: #990000">.;</span>    <span style="font-style: italic"><span style="color: #9A1900">// left[1,1]   = 6</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// right matrix</span></span>
     ax<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span>  <span style="color: #990000">=</span> X<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">];</span>  <span style="font-style: italic"><span style="color: #9A1900">// right[0,0]  = x[2] = 3</span></span>
     ax<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">7</span><span style="color: #990000">.;</span>    <span style="font-style: italic"><span style="color: #9A1900">// right[0,1]  = 7</span></span>
     ax<span style="color: #990000">[</span><span style="color: #993399">6</span><span style="color: #990000">]</span>  <span style="color: #990000">=</span> X<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">];</span>  <span style="font-style: italic"><span style="color: #9A1900">// right[1,0]  = x[3] = 4</span></span>
     ax<span style="color: #990000">[</span><span style="color: #993399">7</span><span style="color: #990000">]</span>  <span style="color: #990000">=</span> <span style="color: #993399">8</span><span style="color: #990000">.;</span>    <span style="font-style: italic"><span style="color: #9A1900">// right[1,1]  = 8</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ x0 , x1 ] * [ x2 , 7 ] = [ x0*x2 + x1*x3 , x0*7 + x1*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ 5  , 6 ]    [ x3 , 8 ]   [ 5*x2  + 6*x3  , 5*7 + 6*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     */</span></span>

     <span style="font-style: italic"><span style="color: #9A1900">// The call back routines need to know the dimensions of the matrices.</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Store information about the matrix multiply for this call to mat_mul.</span></span>
     <span style="color: #008080">call_info</span> info<span style="color: #990000">;</span>
     info<span style="color: #990000">.</span>nr_result <span style="color: #990000">=</span> nr_result<span style="color: #990000">;</span>
     info<span style="color: #990000">.</span>n_middle  <span style="color: #990000">=</span> n_middle<span style="color: #990000">;</span>
     info<span style="color: #990000">.</span>nc_result <span style="color: #990000">=</span> nc_result<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">// info.vx gets set by forward during call to mat_mul below</span></span>
     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> info<span style="color: #990000">.</span>vx<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></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: #008080">size_t</span> id      <span style="color: #990000">=</span> info_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
     info_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span>info<span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// user defined AD&lt;double&gt; version of matrix multiply</span></span>
     <span style="font-weight: bold"><span style="color: #000000">mat_mul</span></span><span style="color: #990000">(</span>id<span style="color: #990000">,</span> ax<span style="color: #990000">,</span> ay<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// check AD&lt;double&gt;  results</span></span>
     ok <span style="color: #990000">&amp;=</span> ay<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">(</span><span style="color: #993399">1</span><span style="color: #990000">*</span><span style="color: #993399">3</span> <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">*</span><span style="color: #993399">4</span><span style="color: #990000">);</span> ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span> ay<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> ay<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">(</span><span style="color: #993399">1</span><span style="color: #990000">*</span><span style="color: #993399">7</span> <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">*</span><span style="color: #993399">8</span><span style="color: #990000">);</span> ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span> ay<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> ay<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">(</span><span style="color: #993399">5</span><span style="color: #990000">*</span><span style="color: #993399">3</span> <span style="color: #990000">+</span> <span style="color: #993399">6</span><span style="color: #990000">*</span><span style="color: #993399">4</span><span style="color: #990000">);</span> ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span> ay<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> ay<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #990000">(</span><span style="color: #993399">5</span><span style="color: #990000">*</span><span style="color: #993399">7</span> <span style="color: #990000">+</span> <span style="color: #993399">6</span><span style="color: #990000">*</span><span style="color: #993399">8</span><span style="color: #990000">);</span> ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">Parameter</span></span><span style="color: #990000">(</span> ay<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// use mat_mul to define a function g : X -&gt; ay</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">ADFun&lt;double&gt;</span> G<span style="color: #990000">;</span>
     G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Dependent</span></span><span style="color: #990000">(</span>X<span style="color: #990000">,</span> ay<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">// g(x) = [ x0*x2 + x1*x3 , x0*7 + x1*8 , 5*x2  + 6*x3  , 5*7 + 6*8 ]^T</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test zero order forward mode evaluation of g(x)</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">y</span></span><span style="color: #990000">(</span>m<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
          x<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>j <span style="color: #990000">+</span> <span style="color: #993399">2</span><span style="color: #990000">);</span>
     y <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Forward</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> y<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> x<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">+</span> x<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> y<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> <span style="color: #993399">7</span><span style="color: #990000">.</span>   <span style="color: #990000">+</span> x<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">8</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> y<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">5</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> x<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">6</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> y<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">5</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> <span style="color: #993399">7</span><span style="color: #990000">.</span>     <span style="color: #990000">+</span> <span style="color: #993399">6</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> <span style="color: #993399">8</span><span style="color: #990000">.;</span>

     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test first order forward mode evaluation of g'(x) * [1, 2, 3, 4]^T</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// g'(x) = [ x2, x3, x0, x1 ]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//         [ 7 ,  8,  0, 0  ]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//         [ 0 ,  0,  5, 6  ]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//         [ 0 ,  0,  0, 0  ]</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">dx</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">dy</span></span><span style="color: #990000">(</span>m<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
          dx<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>j <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
     dy <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Forward</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> dx<span style="color: #990000">);</span>
     ok <span style="color: #990000">&amp;=</span> dy<span style="color: #990000">[</span><span style="color: #993399">0</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> x<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">2</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> x<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">3</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">4</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> x<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> dy<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: #993399">7</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> <span style="color: #993399">8</span><span style="color: #990000">.</span>   <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: #993399">4</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> <span style="color: #993399">0</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> dy<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: #993399">0</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> <span style="color: #993399">0</span><span style="color: #990000">.</span>   <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">5</span><span style="color: #990000">.</span>   <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">6</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> dy<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">1</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: #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: #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: #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="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test second order forward mode</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// g_0^2 (x) = [ 0, 0, 1, 0 ], g_0^2 (x) * [1] = [3]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//             [ 0, 0, 0, 1 ]              [2]   [4]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//             [ 1, 0, 0, 0 ]              [3]   [1]</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//             [ 0, 1, 0, 0 ]              [4]   [2]</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">ddx</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">ddy</span></span><span style="color: #990000">(</span>m<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
          ddx<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>
     ddy <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Forward</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span><span style="color: #990000">,</span> ddx<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">// [1, 2, 3, 4] * g_0^2 (x) * [1, 2, 3, 4]^T = 1*3 + 2*4 + 3*1 + 4*2</span></span>
     ok <span style="color: #990000">&amp;=</span> <span style="color: #993399">2</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> ddy<span style="color: #990000">[</span><span style="color: #993399">0</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: #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> <span style="color: #993399">4</span><span style="color: #990000">.</span> <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">1</span><span style="color: #990000">.</span> <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>
     <span style="font-style: italic"><span style="color: #9A1900">// for i &gt; 0, [1, 2, 3, 4] * g_i^2 (x) * [1, 2, 3, 4]^T = 0</span></span>
     ok <span style="color: #990000">&amp;=</span> ddy<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</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> ddy<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>
     ok <span style="color: #990000">&amp;=</span> ddy<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="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test second order reverse mode</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">w</span></span><span style="color: #990000">(</span>m<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">dw</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span> <span style="color: #990000">*</span>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
          w<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">.;</span>
     w<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>
     dw <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Reverse</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span><span style="color: #990000">,</span> w<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">// g_0'(x) = [ x2, x3, x0, x1 ]</span></span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">*</span><span style="color: #993399">2</span> <span style="color: #990000">+</span> <span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">*</span><span style="color: #993399">2</span> <span style="color: #990000">+</span> <span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">*</span><span style="color: #993399">2</span> <span style="color: #990000">+</span> <span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">*</span><span style="color: #993399">2</span> <span style="color: #990000">+</span> <span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> x<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
     <span style="font-style: italic"><span style="color: #9A1900">// g_0'(x)   * [1, 2, 3, 4]  = 1 * x2 + 2 * x3 + 3 * x0 + 4 * x1</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// g_0^2 (x) * [1, 2, 3, 4]  = [3, 4, 1, 2]</span></span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">*</span><span style="color: #993399">2</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: #993399">3</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">*</span><span style="color: #993399">2</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: #993399">4</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">*</span><span style="color: #993399">2</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: #993399">1</span><span style="color: #990000">.;</span>
     ok <span style="color: #990000">&amp;=</span> dw<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">*</span><span style="color: #993399">2</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: #993399">2</span><span style="color: #990000">.;</span>

     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test forward and reverse Jacobian sparsity pattern</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ x0 , x1 ] * [ x2 , 7 ] = [ x0*x2 + x1*x3 , x0*7 + x1*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ 5  , 6 ]    [ x3 , 8 ]   [ 5*x2  + 6*x3  , 5*7 + 6*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     so the sparsity pattern should be</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     s[0] = {0, 1, 2, 3}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     s[1] = {0, 1}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     s[2] = {2, 3}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     s[3] = {}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     */</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt; std::set&lt;size_t&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">r</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">s</span></span><span style="color: #990000">(</span>m<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> r<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">()</span> <span style="color: #990000">);</span>
          r<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>j<span style="color: #990000">);</span>
     <span style="color: #FF0000">}</span>
     s <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">ForSparseJac</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">,</span> r<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// s[0] = {0, 1, 2, 3}</span></span>
          ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> s<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-style: italic"><span style="color: #9A1900">// s[1] = {0, 1}</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">if</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">==</span> <span style="color: #993399">1</span> <span style="color: #990000">)</span>
               ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> s<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">==</span> s<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-style: italic"><span style="color: #9A1900">// s[2] = {2, 3}</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> j <span style="color: #990000">==</span> <span style="color: #993399">2</span> <span style="color: #990000">||</span> j <span style="color: #990000">==</span> <span style="color: #993399">3</span> <span style="color: #990000">)</span>
               ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> s<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">==</span> s<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// s[3] == {}</span></span>
     ok <span style="color: #990000">&amp;=</span> s<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">();</span>

     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test reverse Jacobian sparsity pattern</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ x0 , x1 ] * [ x2 , 7 ] = [ x0*x2 + x1*x3 , x0*7 + x1*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     [ 5  , 6 ]    [ x3 , 8 ]   [ 5*x2  + 6*x3  , 5*7 + 6*8 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     so the sparsity pattern should be</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     r[0] = {0, 1, 2, 3}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     r[1] = {0, 1}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     r[2] = {2, 3}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     r[3] = {}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     */</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span>  m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     s<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span>
          s<span style="color: #990000">[</span>i<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>i<span style="color: #990000">);</span>
     <span style="color: #FF0000">}</span>
     r <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">RevSparseJac</span></span><span style="color: #990000">(</span>m<span style="color: #990000">,</span> s<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// r[0] = {0, 1, 2, 3}</span></span>
          ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> r<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-style: italic"><span style="color: #9A1900">// r[1] = {0, 1}</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">if</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">==</span> <span style="color: #993399">1</span> <span style="color: #990000">)</span>
               ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> r<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">==</span> r<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-style: italic"><span style="color: #9A1900">// r[2] = {2, 3}</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> j <span style="color: #990000">==</span> <span style="color: #993399">2</span> <span style="color: #990000">||</span> j <span style="color: #990000">==</span> <span style="color: #993399">3</span> <span style="color: #990000">)</span>
               ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">!=</span> r<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>j<span style="color: #990000">)</span> <span style="color: #990000">==</span> r<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// r[3] == {}</span></span>
     ok <span style="color: #990000">&amp;=</span> r<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">empty</span></span><span style="color: #990000">();</span>

     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">/* Test reverse Hessian sparsity pattern</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     g_0^2 (x) = [ 0, 0, 1, 0 ] and for i &gt; 0, g_i^2 = 0</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                 [ 0, 0, 0, 1 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                 [ 1, 0, 0, 0 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">                 [ 0, 1, 0, 0 ]</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     so for the sparsity pattern for the first component of g is</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     h[0] = {2}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     h[1] = {3}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     h[2] = {0}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     h[3] = {1}</span></span>
<span style="font-style: italic"><span style="color: #9A1900">     */</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">vector&lt; std::set&lt;size_t&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">t</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
     t<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span>
     t<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">);</span>
     h <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">RevSparseHes</span></span><span style="color: #990000">(</span>X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">,</span> t<span style="color: #990000">);</span>
     <span style="color: #008080">size_t</span> check<span style="color: #990000">[]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">{</span><span style="color: #993399">2</span><span style="color: #990000">,</span> <span style="color: #993399">3</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #FF0000">}</span><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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// h[j] = { check[j] }</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
          <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> i <span style="color: #990000">==</span> check<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">)</span>
                    ok <span style="color: #990000">&amp;=</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>i<span style="color: #990000">)</span> <span style="color: #990000">!=</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
               <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     ok <span style="color: #990000">&amp;=</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>i<span style="color: #990000">)</span> <span style="color: #990000">==</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
          <span style="color: #FF0000">}</span>
     <span style="color: #FF0000">}</span>
     t<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><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">1</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> j<span style="color: #990000">++)</span>
               t<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>j<span style="color: #990000">);</span>
     h <span style="color: #990000">=</span> G<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">RevSparseHes</span></span><span style="color: #990000">(</span>X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">,</span> t<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>  X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// h[j] = { }</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> X<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span> i<span style="color: #990000">++)</span>
               ok <span style="color: #990000">&amp;=</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">find</span></span><span style="color: #990000">(</span>i<span style="color: #990000">)</span> <span style="color: #990000">==</span> h<span style="color: #990000">[</span>j<span style="color: #990000">].</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// --------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Free temporary work space. (If there are future calls to</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// old_mat_mul they would create new temporary work space.)</span></span>
     CppAD<span style="color: #990000">::</span>user_atomic<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</span>
     info_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">clear</span></span><span style="color: #990000">();</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>
</font></code>
<hr/>Input File: example/deprecated/old_mat_mul.cpp

</body>
</html>
