<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>AD Conditional Expressions</title>
<meta name="description" id="description" content="AD Conditional Expressions"/>
<meta name="keywords" id="keywords" content=" conditional expression assign "/>
<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='_condexp_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="limits.cpp.xml" target="_top">Prev</a>
</td><td><a href="cond_exp.cpp.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>CondExp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>AD-&gt;</option>
<option>ad_ctor</option>
<option>ad_assign</option>
<option>Convert</option>
<option>ADValued</option>
<option>BoolValued</option>
<option>VecAD</option>
<option>base_require</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>std_math_ad</option>
<option>MathOther</option>
<option>CondExp</option>
<option>Discrete</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>CondExp-&gt;</option>
<option>cond_exp.cpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Syntax</option>
<option>Purpose</option>
<option>Rel</option>
<option>Type</option>
<option>left</option>
<option>right</option>
<option>if_true</option>
<option>if_false</option>
<option>result</option>
<option>Optimize</option>
<option>Deprecated</option>
<option>Operation Sequence</option>
<option>Example</option>
<option>Test</option>
<option>Atan2</option>
</select>
</td>
</tr></table><br/>







<center><b><big><big>AD Conditional Expressions</big></big></b></center>
<br/>
<b><big><a name="Syntax" id="Syntax">Syntax</a></big></b>

<br/>

<code><i><font color="black"><span style='white-space: nowrap'>result</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;CondExp</span></font><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i><font color="blue"><span style='white-space: nowrap'>(</span></font><i><font color="black"><span style='white-space: nowrap'>left</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>right</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'>)</span></font></code>



<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
Record, 
as part of an AD of 
<code><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i></code>

<a href="glossary.xml#Operation.Sequence" target="_top"><span style='white-space: nowrap'>operation&#xA0;sequence</span></a>
,
the conditional result 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;if(&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>left</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Cop</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>right</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;)<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>result</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;else&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>result</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
The relational 
<code><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i></code>
 and comparison operator 
<code><i><font color="black"><span style='white-space: nowrap'>Cop</span></font></i></code>
 
above have the following correspondence: 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&#xA0;&#xA0;Lt&#xA0;&#xA0;&#xA0;Le&#xA0;&#xA0;&#xA0;Eq&#xA0;&#xA0;&#xA0;Ge&#xA0;&#xA0;&#xA0;Gt<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Cop</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&#xA0;&#xA0;&#xA0;&lt;&#xA0;&#xA0;&#xA0;&lt;=&#xA0;&#xA0;&#xA0;==&#xA0;&#xA0;&#xA0;&gt;=&#xA0;&#xA0;&#xA0;&gt;<br/>
</span></font></code>
If 
<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
 is the <a href="adfun.xml" target="_top"><span style='white-space: nowrap'>ADFun</span></a>
 object corresponding to the
AD operation sequence,
the assignment choice for 
<code><i><font color="black"><span style='white-space: nowrap'>result</span></font></i></code>

in an AD conditional expression is made each time
<a href="forward.xml" target="_top"><span style='white-space: nowrap'>f.Forward</span></a>
 is used to evaluate the zero order Taylor
coefficients with new values for the 
<a href="glossary.xml#Tape.Independent Variable" target="_top"><span style='white-space: nowrap'>independent&#xA0;variables</span></a>
.
This is in contrast to the <a href="compare.xml" target="_top"><span style='white-space: nowrap'>AD&#xA0;comparison&#xA0;operators</span></a>

which are boolean valued and not included in the AD operation sequence. 

<br/>
<br/>
<b><big><a name="Rel" id="Rel">Rel</a></big></b>
<br/>
In the syntax above, the relation 
<code><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i></code>
 represents one of the following
two characters: <code><font color="blue">Lt</font></code>, <code><font color="blue">Le</font></code>, <code><font color="blue">Eq</font></code>, <code><font color="blue">Ge</font></code>, <code><font color="blue">Gt</font></code>. 
As in the table above,

<code><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i></code>
 determines which comparison operator 
<code><i><font color="black"><span style='white-space: nowrap'>Cop</span></font></i></code>
 is used
when comparing 
<code><i><font color="black"><span style='white-space: nowrap'>left</span></font></i></code>
 and 
<code><i><font color="black"><span style='white-space: nowrap'>right</span></font></i></code>
.

<br/>
<br/>
<b><big><a name="Type" id="Type">Type</a></big></b>
<br/>
These functions are defined in the CppAD namespace for arguments of

<code><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i></code>
 is <code><font color="blue">float</font></code> , <code><font color="blue">double</font></code>, or any type of the form

<code><font color="blue"><span style='white-space: nowrap'>AD&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;</span></font></code>
.
(Note that all four arguments must have the same type.)
 
<br/>
<br/>
<b><big><a name="left" id="left">left</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>left</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>left</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the value for the left side of the comparison operator.
 
<br/>
<br/>
<b><big><a name="right" id="right">right</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>right</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>right</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the value for the right side of the comparison operator.

<br/>
<br/>
<b><big><a name="if_true" id="if_true">if_true</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the return value if the result of the comparison is true.

<br/>
<br/>
<b><big><a name="if_false" id="if_false">if_false</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It specifies the return value if the result of the comparison is false.

<br/>
<br/>
<b><big><a name="result" id="result">result</a></big></b>
<br/>
The 
<code><i><font color="black"><span style='white-space: nowrap'>result</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&amp;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
<br/>
<b><big><a name="Optimize" id="Optimize">Optimize</a></big></b>
<br/>
The <a href="optimize.xml" target="_top"><span style='white-space: nowrap'>optimize</span></a>
 method will optimize conditional expressions
in the following way:
During <a href="forward_zero.xml" target="_top"><span style='white-space: nowrap'>zero&#xA0;order&#xA0;forward&#xA0;mode</span></a>
,
once the value of the 
<code><i><font color="black"><span style='white-space: nowrap'>left</span></font></i></code>
 and 
<code><i><font color="black"><span style='white-space: nowrap'>right</span></font></i></code>
 have been determined, 
it is known if the true or false case is required.
From this point on, values corresponding to the case that is not required
are not computed. 
This optimization is done for the rest of zero order forward mode
as well as forward and reverse derivatives calculations.
There is one exception to this optimization; see
<a href="wishlist.xml#Conditional Expressions" target="_top"><span style='white-space: nowrap'>wish&#xA0;list</span></a>
.

<br/>
<br/>
<b><big><a name="Deprecated" id="Deprecated">Deprecated</a></big></b>
<br/>
Previous versions of CppAD used 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;CondExp(</span></font><i><font color="black"><span style='white-space: nowrap'>flag</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
for the same meaning as 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;CondExpGt(</span></font><i><font color="black"><span style='white-space: nowrap'>flag</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>(0),&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_true</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>if_false</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
Use of <code><font color="blue">CondExp</font></code> is deprecated, but continues to be supported.

<br/>
<br/>
<b><big><a name="Operation Sequence" id="Operation Sequence">Operation Sequence</a></big></b>
<br/>
This is an AD of 
<code><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i></code>

<a href="glossary.xml#Operation.Atomic" target="_top"><span style='white-space: nowrap'>atomic&#xA0;operation</span></a>

and hence is part of the current
AD of 
<code><i><font color="black"><span style='white-space: nowrap'>Base</span></font></i></code>

<a href="glossary.xml#Operation.Sequence" target="_top"><span style='white-space: nowrap'>operation&#xA0;sequence</span></a>
.


<br/>
<br/>
<b><big><a name="Example" id="Example">Example</a></big></b>


<br/>
<br/>
<b><big><a name="Test" id="Test">Test</a></big></b>

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

contains an example and test of this function.   
It returns true if it succeeds and false otherwise.

<br/>
<br/>
<b><big><a name="Atan2" id="Atan2">Atan2</a></big></b>
<br/>
The following implementation of the
AD <a href="atan2.xml" target="_top"><span style='white-space: nowrap'>atan2</span></a>
 function is a more complex
example of using conditional expressions:
<code><font color="blue">
<pre style='display:inline'> 
template &lt;class Base&gt;
<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; atan2 (const <a href="ad.xml" target="_top">AD</a>&lt;Base&gt; &amp;y, const <a href="ad.xml" target="_top">AD</a>&lt;Base&gt; &amp;x)
{	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; alpha;
	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; beta;
	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; theta;

	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; zero(0.);
	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; pi2(2. * atan(1.));
	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; pi(2. * pi2);

	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; ax = abs(x);
	<a href="ad.xml" target="_top">AD</a>&lt;Base&gt; ay = abs(y);

	// if( ax &gt; ay )
	// 	theta = atan(ay / ax);
	// else	theta = pi2 - atan(ax / ay);
	alpha = atan(ay / ax);
	beta  = pi2 - atan(ax / ay);
	theta = CondExpGt(ax, ay, alpha, beta);         // use of CondExp

	// if( x &lt;= 0 )
	// 	theta = pi - theta;
	theta = CondExpLe(x, zero, pi - theta, theta);  // use of CondExp
	
	// if( y &lt;= 0 )
	// 	theta = - theta;
	theta = CondExpLe(y, zero, -theta, theta);      // use of CondExp

	return theta;
}
</pre>

</font></code>



<hr/>Input File: cppad/local/cond_exp.hpp

</body>
</html>
