<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>User Atomic Matrix Multiply: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="User Atomic Matrix Multiply: Example and Test"/>
<meta name="keywords" id="keywords" content=" user atomic matrix multiply: example test see also class definition use function constructor recording forward reverse option for_sparse_jac rev_sparse_jac rev_sparse_hes "/>
<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='_atomic_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="atomic_eigen_cholesky.hpp.xml" target="_top">Prev</a>
</td><td><a href="atomic_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>AD</option>
<option>ADValued</option>
<option>atomic</option>
<option>atomic_base</option>
<option>atomic_mat_mul.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>unary_standard_math</option>
<option>binary_math</option>
<option>CondExp</option>
<option>Discrete</option>
<option>numeric_limits</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>atomic-&gt;</option>
<option>checkpoint</option>
<option>atomic_base</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>atomic_base-&gt;</option>
<option>atomic_ctor</option>
<option>atomic_option</option>
<option>atomic_afun</option>
<option>atomic_forward</option>
<option>atomic_reverse</option>
<option>atomic_for_sparse_jac</option>
<option>atomic_rev_sparse_jac</option>
<option>atomic_for_sparse_hes</option>
<option>atomic_rev_sparse_hes</option>
<option>atomic_base_clear</option>
<option>atomic_get_started.cpp</option>
<option>atomic_norm_sq.cpp</option>
<option>atomic_reciprocal.cpp</option>
<option>atomic_set_sparsity.cpp</option>
<option>atomic_tangent.cpp</option>
<option>atomic_eigen_mat_mul.cpp</option>
<option>atomic_eigen_mat_inv.cpp</option>
<option>atomic_eigen_cholesky.cpp</option>
<option>atomic_mat_mul.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>atomic_mat_mul.cpp-&gt;</option>
<option>atomic_mat_mul.hpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>See Also</option>
<option>Class Definition</option>
<option>Use Atomic Function</option>
<option>---..Constructor</option>
<option>---..Recording</option>
<option>---..forward</option>
<option>---..reverse</option>
<option>---..option</option>
<option>---..for_sparse_jac</option>
<option>---..rev_sparse_jac</option>
<option>---..rev_sparse_hes</option>
</select>
</td>
</tr></table><br/>
<center><b><big><big>User Atomic Matrix Multiply: Example and Test</big></big></b></center>
<br/>
<b><big><a name="See Also" id="See Also">See Also</a></big></b>

<br/>
<a href="atomic_eigen_mat_mul.cpp.xml" target="_top"><span style='white-space: nowrap'>atomic_eigen_mat_mul.cpp</span></a>




<br/>
<br/>
<b><big><a name="Class Definition" id="Class Definition">Class Definition</a></big></b>
<br/>
This example uses the file <a href="atomic_mat_mul.hpp.xml" target="_top"><span style='white-space: nowrap'>atomic_mat_mul.hpp</span></a>

which defines matrix multiply as a <a href="atomic_base.xml" target="_top"><span style='white-space: nowrap'>atomic_base</span></a>
 operation.



<br/>
<br/>
<b><big><a name="Use Atomic Function" id="Use Atomic Function">Use Atomic Function</a></big></b>

<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">&lt;cppad/example/mat_mul.hpp&gt;</span>

<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">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-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>vector<span style="color: #990000">;</span>
     <span style="color: #008080">size_t</span> i<span style="color: #990000">,</span> j<span style="color: #990000">;</span></tt></pre>
<br/>
<b><a name="Use Atomic Function.Constructor" id="Use Atomic Function.Constructor">Constructor</a></b>

<pre style='display:inline'><tt>
     <span style="font-style: italic"><span style="color: #9A1900">// -------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// object that multiplies  2 x 2  matrices</span></span>
     <span style="color: #008080">atomic_mat_mul</span> afun<span style="color: #990000">;</span>
</tt></pre>
<br/>
<b><a name="Use Atomic Function.Recording" id="Use Atomic Function.Recording">Recording</a></b>

<pre><tt>     <span style="font-style: italic"><span style="color: #9A1900">// start recording with four independent varables</span></span>
     <span style="color: #008080">size_t</span> n <span style="color: #990000">=</span> <span style="color: #993399">4</span><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>n<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">ax</span></span><span style="color: #990000">(</span>n<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> n<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
          ax<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <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">1</span><span style="color: #990000">);</span>
     CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">Independent</span></span><span style="color: #990000">(</span>ax<span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// ------------------------------------------------------------------</span></span>
     <span style="color: #008080">size_t</span> nr_left <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_right <span style="color: #990000">=</span> <span style="color: #993399">2</span><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">atom_x</span></span><span style="color: #990000">(</span><span style="color: #993399">3</span> <span style="color: #990000">+</span> <span style="color: #990000">(</span>nr_left <span style="color: #990000">+</span> nc_right<span style="color: #990000">)</span> <span style="color: #990000">*</span> n_middle <span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// matrix dimensions</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> AD<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;(</span> nr_left <span style="color: #990000">);</span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> AD<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;(</span> n_middle <span style="color: #990000">);</span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> AD<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;(</span> nc_right <span style="color: #990000">);</span>

     <span style="font-style: italic"><span style="color: #9A1900">// left matrix</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> ax<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] = x0</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">4</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> ax<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] = x1</span></span>
     atom_x<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">5</span><span style="color: #990000">.;</span>     <span style="font-style: italic"><span style="color: #9A1900">// left[1, 0] = 5</span></span>
     atom_x<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">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 matix</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">7</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> ax<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] = x2</span></span>
     atom_x<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">7</span><span style="color: #990000">.;</span>     <span style="font-style: italic"><span style="color: #9A1900">// right[0, 1] = 7</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">9</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> ax<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] = x3</span></span>
     atom_x<span style="color: #990000">[</span><span style="color: #993399">10</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">/*</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="color: #008080">vector&lt; AD&lt;double&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">atom_y</span></span><span style="color: #990000">(</span>nr_left <span style="color: #990000">*</span> nc_right<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #000000">afun</span></span><span style="color: #990000">(</span>atom_x<span style="color: #990000">,</span> atom_y<span style="color: #990000">);</span>

     ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>atom_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>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>x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">])</span> <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span>atom_y<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]);</span>
     ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>atom_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: #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: #993399">8</span><span style="color: #990000">.</span>  <span style="color: #990000">)</span> <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span>atom_y<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]);</span>
     ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>atom_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>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>x<span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">])</span> <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">Variable</span></span><span style="color: #990000">(</span>atom_y<span style="color: #990000">[</span><span style="color: #993399">2</span><span style="color: #990000">]);</span>
     ok <span style="color: #990000">&amp;=</span> <span style="color: #990000">(</span>atom_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: #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: #993399">8</span><span style="color: #990000">.</span>  <span style="color: #990000">)</span> <span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">Parameter</span></span><span style="color: #990000">(</span>atom_y<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">// ------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// define the function g : x -&gt; atom_y</span></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>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">ADFun&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">g</span></span><span style="color: #990000">(</span>ax<span style="color: #990000">,</span> atom_y<span style="color: #990000">);</span></tt></pre>
<br/>
<b><a name="Use Atomic Function.forward" id="Use Atomic Function.forward">forward</a></b>

<pre><tt>     <span style="font-style: italic"><span style="color: #9A1900">// Test zero order forward mode evaluation of g(x)</span></span>
     <span style="color: #008080">size_t</span> m <span style="color: #990000">=</span> atom_y<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: #008080">vector&lt;double&gt;</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>  n<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>n<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>  n<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>n<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>  n<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></tt></pre>
<br/>
<b><a name="Use Atomic Function.reverse" id="Use Atomic Function.reverse">reverse</a></b>

<pre><tt>     <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> n<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></tt></pre>
<br/>
<b><a name="Use Atomic Function.option" id="Use Atomic Function.option">option</a></b>

<pre><tt>     <span style="font-style: italic"><span style="color: #9A1900">//----------------------------------------------------------------------</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Test both the boolean and set sparsity at the atomic level</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> sparse_index <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> sparse_index <span style="color: #990000">&lt;</span> <span style="color: #993399">2</span><span style="color: #990000">;</span> sparse_index<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> sparse_index <span style="color: #990000">==</span> <span style="color: #993399">0</span> <span style="color: #990000">)</span>
               afun<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">option</span></span><span style="color: #990000">(</span> CppAD<span style="color: #990000">::</span>atomic_base<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span>bool_sparsity_enum <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>     afun<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">option</span></span><span style="color: #990000">(</span> CppAD<span style="color: #990000">::</span>atomic_base<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span>set_sparsity_enum <span style="color: #990000">);</span></tt></pre>
<br/>
<b><a name="Use Atomic Function.for_sparse_jac" id="Use Atomic Function.for_sparse_jac">for_sparse_jac</a></b>

<pre><tt>     <span style="font-style: italic"><span style="color: #9A1900">// Test forward 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">     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">     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>n<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>  n<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>n<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>  n<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></tt></pre>
<br/>
<b><a name="Use Atomic Function.rev_sparse_jac" id="Use Atomic Function.rev_sparse_jac">rev_sparse_jac</a></b>

<pre><tt>     <span style="font-style: italic"><span style="color: #9A1900">// Test reverse Jacobian sparsity pattern</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>  n <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></tt></pre>
<br/>
<b><a name="Use Atomic Function.rev_sparse_hes" id="Use Atomic Function.rev_sparse_hes">rev_sparse_hes</a></b>

<pre><tt>     <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>n<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>n<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>  n<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> n<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>n<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>  n<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> n<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="color: #FF0000">}</span> <span style="font-style: italic"><span style="color: #9A1900">// end for(size_t sparse_index  ...</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">//-----------------------------------------------------------------</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: example/atomic/mat_mul.cpp

</body>
</html>
