<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Tan and Tanh as User Atomic Operations: Example and Test</title>
<meta name="description" id="description" content="Tan and Tanh as User Atomic Operations: Example and Test"/>
<meta name="keywords" id="keywords" content=" tan user_atomic user atomic test example "/>
<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='_user_tan.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="user_atomic.xml" target="_top">Prev</a>
</td><td><a href="mat_mul.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>user_atomic</option>
<option>user_tan.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(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_down2(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>std_math_ad</option>
<option>MathOther</option>
<option>CondExp</option>
<option>Discrete</option>
<option>user_atomic</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>user_atomic-&gt;</option>
<option>user_tan.cpp</option>
<option>mat_mul.cpp</option>
</select>
</td>
<td>user_tan.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Theory</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Tan and Tanh as User Atomic Operations: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Theory" id="Theory">Theory</a></big></b>
<br/>
The code below uses the <a href="tan_forward.xml" target="_top"><span style='white-space: nowrap'>tan_forward</span></a>
 and <a href="tan_reverse.xml" target="_top"><span style='white-space: nowrap'>tan_reverse</span></a>

to implement the tangent (
<code><i><font color="black"><span style='white-space: nowrap'>id</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;==&#xA0;0</span></font></code>
) and hyperbolic tangent
(
<code><i><font color="black"><span style='white-space: nowrap'>id</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;==&#xA0;1</span></font></code>
) functions as user atomic operations.

<code><font color="blue">
<pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;

namespace { // Begin empty namespace 
	using CppAD::vector;

	// a utility to compute the union of two sets.
	void my_union(
		std::set&lt;size_t&gt;&amp;         result  ,
		const std::set&lt;size_t&gt;&amp;   left    ,
		const std::set&lt;size_t&gt;&amp;   right   )
	{	std::set&lt;size_t&gt; temp;
		std::set_union(
			left.begin()              ,
			left.end()                ,
			right.begin()             ,
			right.end()               ,
			std::inserter(temp, temp.begin())
		);
		result.swap(temp);
	}

	// ----------------------------------------------------------------------
	// forward mode routine called by CppAD
	bool user_tan_forward(
		size_t                   id ,
		size_t                order ,
		size_t                    n ,
		size_t                    m ,
		const vector&lt;bool&gt;&amp;      vx ,
		vector&lt;bool&gt;&amp;           vzy ,
		const vector&lt;float&gt;&amp;     tx ,
		vector&lt;float&gt;&amp;          tzy
	)
	{
		assert( id == 0 || id == 1 );
		assert( n == 1 );
		assert( m == 2 );
		assert( tx.size() &gt;= (order+1) * n );
		assert( tzy.size() &gt;= (order+1) * m );

		size_t n_order = order + 1;
		size_t j = order;
		size_t k;

		// check if this is during the call to user_tan(id, ax, ay)
		if( vx.size() &gt; 0 )
		{	assert( vx.size() &gt;= n );
			assert( vzy.size() &gt;= m );
			
			// now setvzy
			vzy[0] = vx[0];
			vzy[1] = vx[0];
		}

		if( j == 0 )
		{	// z^{(0)} = tan( x^{(0)} ) or tanh( x^{(0)} )
			if( id == 0 )
				tzy[0] = tan( tx[0] );
			else	tzy[0] = tanh( tx[0] );

			// y^{(0)} = z^{(0)} * z^{(0)}
			tzy[n_order + 0] = tzy[0] * tzy[0];
		}
		else
		{	float j_inv = 1.f / float(j);
			if( id == 1 )
				j_inv = - j_inv;

			// z^{(j)} = x^{(j)} +- sum_{k=1}^j k x^{(k)} y^{(j-k)} / j
			tzy[j] = tx[j];  
			for(k = 1; k &lt;= j; k++)
				tzy[j] += tx[k] * tzy[n_order + j-k] * k * j_inv;

			// y^{(j)} = sum_{k=0}^j z^{(k)} z^{(j-k)}
			tzy[n_order + j] = 0.;
			for(k = 0; k &lt;= j; k++)
				tzy[n_order + j] += tzy[k] * tzy[j-k];
		}
			
		// All orders are implemented and there are no possible errors
		return true;
	}
	// ----------------------------------------------------------------------
	// reverse mode routine called by CppAD
	bool user_tan_reverse(
		size_t                   id ,
		size_t                order ,
		size_t                    n ,
		size_t                    m ,
		const vector&lt;float&gt;&amp;     tx ,
		const vector&lt;float&gt;&amp;    tzy ,
		vector&lt;float&gt;&amp;           px ,
		const vector&lt;float&gt;&amp;    pzy
	)
	{
		assert( id == 0 || id == 1 );
		assert( n == 1 );
		assert( m == 2 );
		assert( tx.size() &gt;= (order+1) * n );
		assert( tzy.size() &gt;= (order+1) * m );
		assert( px.size() &gt;= (order+1) * n );
		assert( pzy.size() &gt;= (order+1) * m );

		size_t n_order = order + 1;
		size_t j, k;

		// copy because partials w.r.t. y and z need to change
		vector&lt;float&gt; qzy = pzy;

		// initialize accumultion of reverse mode partials
		for(k = 0; k &lt; n_order; k++)
			px[k] = 0.;

		// eliminate positive orders
		for(j = order; j &gt; 0; j--)
		{	float j_inv = 1.f / float(j);
			if( id == 1 )
				j_inv = - j_inv;

			// H_{x^{(k)}} += delta(j-k) +- H_{z^{(j)} y^{(j-k)} * k / j
			px[j] += qzy[j];
			for(k = 1; k &lt;= j; k++)
				px[k] += qzy[j] * tzy[n_order + j-k] * k * j_inv;  

			// H_{y^{j-k)} += +- H_{z^{(j)} x^{(k)} * k / j
			for(k = 1; k &lt;= j; k++)
				qzy[n_order + j-k] += qzy[j] * tx[k] * k * j_inv;  

			// H_{z^{(k)}} += H_{y^{(j-1)}} * z^{(j-k-1)} * 2. 
			for(k = 0; k &lt; j; k++)
				qzy[k] += qzy[n_order + j-1] * tzy[j-k-1] * 2.f; 
		}

		// eliminate order zero
		if( id == 0 )
			px[0] += qzy[0] * (1.f + tzy[n_order + 0]);
		else
			px[0] += qzy[0] * (1.f - tzy[n_order + 0]);

		return true; 
	}
	// ----------------------------------------------------------------------
	// forward Jacobian sparsity routine called by CppAD
	bool user_tan_for_jac_sparse(
		size_t                               id ,             
		size_t                                n ,
		size_t                                m ,
		size_t                                q ,
		const vector&lt; std::set&lt;size_t&gt; &gt;&amp;     r ,
		vector&lt; std::set&lt;size_t&gt; &gt;&amp;           s )
	{
		assert( n == 1 );
		assert( m == 2 );
		assert( id == 0 || id == 1 );
		assert( r.size() &gt;= n );
		assert( s.size() &gt;= m );

		// sparsity for z and y are the same as for x
		s[0] = r[0];
		s[1] = r[0];

		return true;
	}
	// ----------------------------------------------------------------------
	// reverse Jacobian sparsity routine called by CppAD
	bool user_tan_rev_jac_sparse(
		size_t                               id ,             
		size_t                                n ,
		size_t                                m ,
		size_t                                q ,
		vector&lt; std::set&lt;size_t&gt; &gt;&amp;           r ,
		const vector&lt; std::set&lt;size_t&gt; &gt;&amp;     s )
	{
		assert( n == 1 );
		assert( m == 2 );
		assert( id == 0 || id == 1 );
		assert( r.size() &gt;= n );
		assert( s.size() &gt;= m );

		// note that, if the users code only uses z, and not y,
		// we could just set r[0] = s[0]	
		my_union(r[0], s[0], s[1]);
		return true; 
	}
	// ----------------------------------------------------------------------
	// reverse Hessian sparsity routine called by CppAD
	bool user_tan_rev_hes_sparse(
		size_t                               id ,             
		size_t                                n ,
		size_t                                m ,
		size_t                                q ,
		const vector&lt; std::set&lt;size_t&gt; &gt;&amp;     r ,
		const vector&lt;bool&gt;&amp;                   s ,
		vector&lt;bool&gt;&amp;                         t ,
		const vector&lt; std::set&lt;size_t&gt; &gt;&amp;     u ,
		vector&lt; std::set&lt;size_t&gt; &gt;&amp;           v )
	{
		assert( n == 1 );
		assert( m == 2 );
		assert( id == 0 || id == 1 );
		assert( r.size() &gt;= n );
		assert( s.size() &gt;= m );
		assert( t.size() &gt;= n );
		assert( u.size() &gt;= m );
		assert( v.size() &gt;= n );

		// back propogate Jacobian sparsity. If users code only uses z,
		// we could just set t[0] = s[0];
		t[0] =  s[0] | s[1];

		// back propogate Hessian sparsity, ...
		my_union(v[0], u[0], u[1]);

		// convert forward Jacobian sparsity to Hessian sparsity
		// because tan and tanh are nonlinear
		if( t[0] )
			my_union(v[0], v[0], r[0]);

		return true;
	}
	// ---------------------------------------------------------------------
	// Declare the <a href="ad.xml" target="_top">AD</a>&lt;float&gt; routine user_tan(id, ax, ay)
	CPPAD_USER_ATOMIC(
		user_tan                 , 
		CPPAD_TEST_VECTOR        ,
		float                    , 
		user_tan_forward         , 
		user_tan_reverse         ,
		user_tan_for_jac_sparse  ,
		user_tan_rev_jac_sparse  ,
		user_tan_rev_hes_sparse  
	)
} // End empty namespace

bool user_tan(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;
	float eps = 10.f * CppAD::epsilon&lt;float&gt;();

	// domain space vector
	size_t n  = 1;
	float  x0 = 0.5;
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;float&gt; &gt; ax(n);
	ax[0]     = x0;

	// declare independent variables and start tape recording
	CppAD::<a href="independent.xml" target="_top">Independent</a>(ax);

	// range space vector 
	size_t m = 3;
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;float&gt; &gt; af(m);

	// temporary vector for user_tan computations
	// (user_tan computes tan or tanh and its square)
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;float&gt; &gt; az(2);

	// call user tan function and store tan(x) in f[0] (ignore tan(x)^2)
	size_t id = 0;
	user_tan(id, ax, az);
	af[0] = az[0];

	// call user tanh function and store tanh(x) in f[1] (ignore tanh(x)^2)
	id = 1;
	user_tan(id, ax, az);
	af[1] = az[0];

	// put a constant in f[2] = tanh(1.) (for sparsity pattern testing)
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;float&gt; &gt; one(1);
	one[0] = 1.;
	user_tan(id, one, az);
	af[2] = az[0]; 

	// create f: x -&gt; f and stop tape recording
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;float&gt; F(ax, af); 

	// check value 
	float tan = std::tan(x0);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(af[0] , tan,  eps, eps);
	float tanh = std::tanh(x0);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(af[1] , tanh,  eps, eps);

	// compute first partial of f w.r.t. x[0] using forward mode
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;float&gt; dx(n), df(m);
	dx[0] = 1.;
	df    = F.<a href="forward.xml" target="_top">Forward</a>(1, dx);

	// compute derivative of tan - tanh using reverse mode
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;float&gt; w(m), dw(n);
	w[0]  = 1.;
	w[1]  = 1.;
	dw    = F.<a href="reverse.xml" target="_top">Reverse</a>(1, w);

	// tan'(x)   = 1 + tan(x)  * tan(x) 
	// tanh'(x)  = 1 - tanh(x) * tanh(x) 
	float tanp  = 1.f + tan * tan; 
	float tanhp = 1.f - tanh * tanh; 
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(df[0], tanp, eps, eps);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(df[1], tanhp, eps, eps);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], w[0]*tanp + w[1]*tanhp, eps, eps);

	// compute second partial of f w.r.t. x[0] using forward mode
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;float&gt; ddx(n), ddf(m);
	ddx[0] = 0.;
	ddf    = F.<a href="forward.xml" target="_top">Forward</a>(2, ddx);

	// compute second derivative of tan - tanh using reverse mode
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;float&gt; ddw(2);
	ddw   = F.<a href="reverse.xml" target="_top">Reverse</a>(2, w);

	// tan''(x)   = 2 *  tan(x) * tan'(x) 
	// tanh''(x)  = - 2 * tanh(x) * tanh'(x) 
	// Note that second order Taylor coefficient for u half the
	// corresponding second derivative.
	float two    = 2;
	float tanpp  =   two * tan * tanp;
	float tanhpp = - two * tanh * tanhp;
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(two * ddf[0], tanpp, eps, eps);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(two * ddf[1], tanhpp, eps, eps);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(ddw[0], w[0]*tanp  + w[1]*tanhp , eps, eps);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(ddw[1], w[0]*tanpp + w[1]*tanhpp, eps, eps);

	// Forward mode computation of sparsity pattern for F.
	size_t q = n;
	// user vectorBool because m and n are small
	CppAD::vectorBool r1(q), s1(m * q);
	r1[0] = true;            // propogate sparsity for x[0]
	s1    = F.ForSparseJac(q, r1);
	ok  &amp;= (s1[0] == true);  // f[0] depends on x[0]
	ok  &amp;= (s1[1] == true);  // f[1] depends on x[0]
	ok  &amp;= (s1[2] == false); // f[2] does not depend on x[0]

	// Reverse mode computation of sparsity pattern for F.
	size_t p = m;
	CppAD::vectorBool s2(p * m), r2(p * n);
	// Sparsity pattern for identity matrix
	size_t i, j;
	for(i = 0; i &lt; p; i++)
	{	for(j = 0; j &lt; m; j++)
			s2[i * p + j] = (i == j);
	}
	r2   = F.RevSparseJac(p, s2);
	ok  &amp;= (r2[0] == true);  // f[0] depends on x[0]
	ok  &amp;= (r2[1] == true);  // f[1] depends on x[0]
	ok  &amp;= (r2[2] == false); // f[2] does not depend on x[0]

	// Hessian sparsity for f[0]
	CppAD::vectorBool s3(m), h(q * n);
	s3[0] = true;
	s3[1] = false;
	s3[2] = false;
	h    = F.RevSparseHes(q, s3);
	ok  &amp;= (h[0] == true);  // Hessian is non-zero

	// Hessian sparsity for f[2]
	s3[0] = false;
	s3[2] = true;
	h    = F.RevSparseHes(q, s3);
	ok  &amp;= (h[0] == false);  // Hessian is zero

	// check tanh results for a large value of x
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;float&gt; x(n), f(m);
	x[0]  = std::numeric_limits&lt;float&gt;::max() / two;
	f     = F.<a href="forward.xml" target="_top">Forward</a>(0, x);
	tanh  = 1.;
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(f[1], tanh, eps, eps);
	df    = F.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	tanhp = 0.;
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(df[1], tanhp, eps, eps);
 
	// --------------------------------------------------------------------
	// Free all temporary work space associated with user_atomic objects. 
	// (If there are future calls to user atomic functions, they will 
	// create new temporary work space.)
	CppAD::user_atomic&lt;float&gt;::clear();

	return ok;
}
</pre>

</font></code>


<hr/>Input File: example/user_tan.cpp

</body>
</html>

