<?xml version='1.0'?>
<?xml-stylesheet type='text/xsl' href='pmathml.xsl'?>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Enable use of AD&lt;Base&gt; where Base is std::complex&lt;double&gt;</title>
<meta name="description" id="description" content="Enable use of AD&lt;Base&gt; where Base is std::complex&lt;double&gt;"/>
<meta name="keywords" id="keywords" content=" complex double Base "/>
<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='_base_complex.hpp_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="base_double.hpp.xml" target="_top">Prev</a>
</td><td><a href="complexpoly.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>base_require</option>
<option>base_complex.hpp</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>multi_thread</option>
<option>library</option>
<option>cppad_ipopt_nlp</option>
<option>Example</option>
<option>preprocessor</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>AD-&gt;</option>
<option>Default</option>
<option>ad_copy</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>base_require-&gt;</option>
<option>base_cond_exp</option>
<option>base_identical</option>
<option>base_ordered</option>
<option>base_std_math</option>
<option>base_float.hpp</option>
<option>base_double.hpp</option>
<option>base_complex.hpp</option>
<option>base_adolc.hpp</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>base_complex.hpp-&gt;</option>
<option>ComplexPoly.cpp</option>
<option>not_complex_ad.cpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Example</option>
<option>See Also</option>
<option>Include Order</option>
<option>CondExpOp</option>
<option>CondExpRel</option>
<option>EqualOpSeq</option>
<option>Identical</option>
<option>Ordered</option>
<option>Integer</option>
<option>epsilon</option>
<option>isnan</option>
<option>Valid Unary Math</option>
<option>Invalid Unary Math</option>
<option>pow</option>
</select>
</td>
</tr></table><br/>







<center><b><big><big>Enable use of AD&lt;Base&gt; where Base is std::complex&lt;double&gt;</big></big></b></center>
<br/>
<b><big><a name="Example" id="Example">Example</a></big></b>
<br/>
The file <a href="complexpoly.cpp.xml" target="_top"><span style='white-space: nowrap'>ComplexPoly.cpp</span></a>
 contains an example use of
<code><font color="blue">std::complex&lt;double&gt;</font></code> type for a CppAD <i>Base</i> type.
It returns true if it succeeds and false otherwise.

<br/>
<br/>
<b><big><a name="See Also" id="See Also">See Also</a></big></b>
<br/>
The file <a href="not_complex_ad.cpp.xml" target="_top"><span style='white-space: nowrap'>not_complex_ad.cpp</span></a>
 contains an example using
complex arithmetic where the function is not complex differentiable.

<br/>
<br/>
<b><big><a name="Include Order" id="Include Order">Include Order</a></big></b>
<br/>
This file is included before <code><font color="blue">&lt;cppad/cppad.hpp&gt;</font></code>
so it is necessary to define the error handler
in addition to including
<a href="base_require.xml#Include Order" target="_top"><span style='white-space: nowrap'>base_require.hpp</span></a>

<code><font color='blue'><pre style='display:inline'> 
# include &lt;limits&gt;
# include &lt;complex&gt;
# include &lt;cppad/base_require.hpp&gt;
# include &lt;cppad/local/cppad_assert.hpp&gt;

</pre></font></code>


<br/>
<br/>
<b><big><a name="CondExpOp" id="CondExpOp">CondExpOp</a></big></b>
<br/>
The type <code><font color="blue">std::complex&lt;double&gt;</font></code> does not supports the
<code><font color="blue">&lt;</font></code>, <code><font color="blue">&lt;=</font></code>, <code><font color="blue">==</font></code>, <code><font color="blue">&gt;=</font></code>, and <code><font color="blue">&gt;</font></code> operators; see
<a href="base_cond_exp.xml#CondExpTemplate.Not Ordered" target="_top"><span style='white-space: nowrap'>not&#xA0;ordered</span></a>
.
Hence its <code><font color="blue">CondExpOp</font></code> function is defined by
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline std::complex&lt;double&gt; CondExpOp(
		enum CppAD::CompareOp      cop        ,
		const std::complex&lt;double&gt; &amp;left      ,
		const std::complex&lt;double&gt; &amp;right     ,
		const std::complex&lt;double&gt; &amp;trueCase  ,
		const std::complex&lt;double&gt; &amp;falseCase )
	{	CppAD::ErrorHandler::Call(
			true     , __LINE__ , __FILE__ ,
			&quot;std::complex&lt;float&gt; CondExpOp(...)&quot;,
			&quot;Error: cannot use CondExp with a complex type&quot;
		);
		return std::complex&lt;double&gt;(0);
	}
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="CondExpRel" id="CondExpRel">CondExpRel</a></big></b>
<br/>
The <a href="base_cond_exp.xml#CondExpRel" target="_top"><span style='white-space: nowrap'>CPPAD_COND_EXP_REL</span></a>
 macro invocation
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	CPPAD_COND_EXP_REL( std::complex&lt;double&gt; )
}
</pre></font></code>

used <code><font color="blue">CondExpOp</font></code> above to
define 
<code><font color="blue"><span style='white-space: nowrap'>CondExp</span></font><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i></code>
 for <code><font color="blue">std::complex&lt;double&gt;</font></code> arguments
and 
<code><i><font color="black"><span style='white-space: nowrap'>Rel</span></font></i></code>
 equal to
<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>, and <code><font color="blue">Gt</font></code>.

<br/>
<br/>
<b><big><a name="EqualOpSeq" id="EqualOpSeq">EqualOpSeq</a></big></b>
<br/>
Complex numbers do not carry operation sequence information. 
Thus they are equal in this sense if and only if there values are equal.  
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline bool EqualOpSeq(
		const std::complex&lt;double&gt; &amp;x , 
		const std::complex&lt;double&gt; &amp;y )
	{	return x == y; 
	}
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Identical" id="Identical">Identical</a></big></b>
<br/>
Complex numbers do not carry operation sequence information. 
Thus they are all parameters so the identical functions just check values.
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline bool IdenticalPar(const std::complex&lt;double&gt; &amp;x)
	{	return true; }
	inline bool IdenticalZero(const std::complex&lt;double&gt; &amp;x)
	{	return (x == std::complex&lt;double&gt;(0., 0.) ); }
	inline bool IdenticalOne(const std::complex&lt;double&gt; &amp;x)
	{	return (x == std::complex&lt;double&gt;(1., 0.) ); }
	inline bool IdenticalEqualPar(
		const std::complex&lt;double&gt; &amp;x, const std::complex&lt;double&gt; &amp;y)
	{	return (x == y); }
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Ordered" id="Ordered">Ordered</a></big></b>
<br/>
Complex types do not support comparison operators, 
<code><font color='blue'><pre style='display:inline'> 
# undef  CPPAD_USER_MACRO
# define CPPAD_USER_MACRO(Fun)                                     \
inline bool Fun(const std::complex&lt;double&gt;&amp; x)                     \
{      CppAD::ErrorHandler::Call(                                  \
               true     , __LINE__ , __FILE__ ,                    \
               #Fun&quot;(x)&quot;,                                          \
               &quot;Error: cannot use &quot; #Fun &quot; with x complex&lt;double&gt; &quot; \
       );                                                          \
       return false;                                               \
}
namespace CppAD {
	CPPAD_USER_MACRO(LessThanZero)
	CPPAD_USER_MACRO(LessThanOrZero)
	CPPAD_USER_MACRO(GreaterThanOrZero)
	CPPAD_USER_MACRO(GreaterThanZero)
	inline bool abs_geq(
		const std::complex&lt;double&gt;&amp; x , 
		const std::complex&lt;double&gt;&amp; y )
	{	return std::abs(x) &gt;= std::abs(y); }
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Integer" id="Integer">Integer</a></big></b>
<br/>
The implementation of this function must agree
with the CppAD user specifications for complex arguments to the
<a href="integer.xml#x.Complex Types" target="_top"><span style='white-space: nowrap'>Integer</span></a>
 function:
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline int Integer(const std::complex&lt;double&gt; &amp;x)
	{	return static_cast&lt;int&gt;( x.real() ); }
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="epsilon" id="epsilon">epsilon</a></big></b>
<br/>
The standard double complex machine epsilon in a double,
hence we must convert it to a complex to meet the prototype
for the CppAD machine epsilon function.
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	template &lt;&gt;
	inline std::complex&lt;double&gt; epsilon&lt; std::complex&lt;double&gt; &gt;(void)
	{	double eps = std::numeric_limits&lt;double&gt;::epsilon();
		return std::complex&lt;double&gt;(eps, 0.);
	}
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="isnan" id="isnan">isnan</a></big></b>
<br/>
The gcc 4.1.1 complier defines the function

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;int&#xA0;std::complex&lt;double&gt;::isnan(&#xA0;std::complex&lt;double&gt;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>z</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;)<br/>
</span></font></code>
(which is not specified in the C++ 1998 standard ISO/IEC 14882).
This causes an ambiguity between the function above and the CppAD
<a href="nan.xml" target="_top"><span style='white-space: nowrap'>isnan</span></a>
 template function.
We avoid this ambiguity by defining a non-template version of
this function in the CppAD namespace.
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline bool isnan(const std::complex&lt;double&gt;&amp; z)
	{	CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL;	
		static const double nan = std::numeric_limits&lt;double&gt;::quiet_NaN();
		return (z != z) | (z.real() == nan) | (z.imag() == nan);
	}
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Valid Unary Math" id="Valid Unary Math">Valid Unary Math</a></big></b>
<br/>
The following macro invocations define the standard unary 
math functions that are valid with complex arguments and are
required to use <code><font color="blue">AD&lt; std::complex&lt;double&gt; &gt;</font></code>.
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, cos)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, cosh)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, exp)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, log)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, sin)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, sinh)
	CPPAD_STANDARD_MATH_UNARY(std::complex&lt;double&gt;, sqrt)
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Invalid Unary Math" id="Invalid Unary Math">Invalid Unary Math</a></big></b>
<br/>
The following macro definition and invocations define the standard unary 
math functions that are invalid with complex arguments and are
required to use <code><font color="blue">AD&lt; std::complex&lt;double&gt; &gt;</font></code>.
<code><font color='blue'><pre style='display:inline'> 
# undef  CPPAD_USER_MACRO
# define CPPAD_USER_MACRO(Fun)                                     \
inline std::complex&lt;double&gt; Fun(const std::complex&lt;double&gt;&amp; x)     \
{      CppAD::ErrorHandler::Call(                                  \
               true     , __LINE__ , __FILE__ ,                    \
               #Fun&quot;(x)&quot;,                                          \
               &quot;Error: cannot use &quot; #Fun &quot; with x complex&lt;double&gt; &quot; \
       );                                                          \
       return std::complex&lt;double&gt;(0);                             \
}
namespace CppAD {
	CPPAD_USER_MACRO(abs)
	CPPAD_USER_MACRO(acos)
	CPPAD_USER_MACRO(asin)
	CPPAD_USER_MACRO(atan)
	CPPAD_USER_MACRO(sign)
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="pow" id="pow">pow</a></big></b>
<br/>
The following defines a <code><font color="blue">CppAD::pow</font></code> function that
is required to use <code><font color="blue">AD&lt; std::complex&lt;double&gt; &gt;</font></code>:
<code><font color='blue'><pre style='display:inline'> 
namespace CppAD {
	inline std::complex&lt;double&gt; pow(
		const std::complex&lt;double&gt; &amp;x , 
		const std::complex&lt;double&gt; &amp;y )
	{	return std::pow(x, y); }
}
</pre></font></code>


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

</body>
</html>

