1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
@begin atomic_two_get_started.cpp@@
$section Getting Started with Atomic Operations: Example and Test$$
$head Purpose$$
This example demonstrates the minimal amount of information
necessary for a $cref atomic_two$$ operation.
$head Start Class Definition$$
$srccode%cpp% */
# include <cppad/cppad.hpp>
namespace { // isolate items below to this file
using CppAD::vector; // abbreviate as vector
class atomic_get_started : public CppAD::atomic_base<double> {
/* %$$
$head Constructor$$
$srccode%cpp% */
public:
// constructor (could use const char* for name)
atomic_get_started(const std::string& name) :
// this example does not use any sparsity patterns
CppAD::atomic_base<double>(name)
{ }
private:
/* %$$
$head forward$$
$srccode%cpp% */
// forward mode routine called by CppAD
virtual bool forward(
size_t p ,
size_t q ,
const vector<bool>& vx ,
vector<bool>& vy ,
const vector<double>& tx ,
vector<double>& ty
)
{
# ifndef NDEBUG
size_t n = tx.size() / (q + 1);
size_t m = ty.size() / (q + 1);
# endif
assert( n == 1 );
assert( m == 1 );
// return flag
bool ok = q == 0;
if( ! ok )
return ok;
// check for defining variable information
// This case must always be implemented
if( vx.size() > 0 )
vy[0] = vx[0];
// Order zero forward mode.
// This case must always be implemented
// y^0 = f( x^0 ) = 1 / x^0
double f = 1. / tx[0];
if( p <= 0 )
ty[0] = f;
return ok;
}
/* %$$
$head End Class Definition$$
$srccode%cpp% */
}; // End of atomic_get_started class
} // End empty namespace
/* %$$
$head Use Atomic Function$$
$srccode%cpp% */
bool get_started(void)
{ bool ok = true;
using CppAD::AD;
using CppAD::NearEqual;
double eps = 10. * CppAD::numeric_limits<double>::epsilon();
/* %$$
$subhead Constructor$$
$srccode%cpp% */
// Create the atomic get_started object
atomic_get_started afun("atomic_get_started");
/* %$$
$subhead Recording$$
$srccode%cpp% */
// Create the function f(x)
//
// domain space vector
size_t n = 1;
double x0 = 0.5;
vector< AD<double> > ax(n);
ax[0] = x0;
// declare independent variables and start tape recording
CppAD::Independent(ax);
// range space vector
size_t m = 1;
vector< AD<double> > ay(m);
// call atomic function and store get_started(x) in au[0]
vector< AD<double> > au(m);
afun(ax, au); // u = 1 / x
// now use AD division to invert to invert the operation
ay[0] = 1.0 / au[0]; // y = 1 / u = x
// create f: x -> y and stop tape recording
CppAD::ADFun<double> f;
f.Dependent (ax, ay); // f(x) = x
/* %$$
$subhead forward$$
$srccode%cpp% */
// check function value
double check = x0;
ok &= NearEqual( Value(ay[0]) , check, eps, eps);
// check zero order forward mode
size_t q;
vector<double> x_q(n), y_q(m);
q = 0;
x_q[0] = x0;
y_q = f.Forward(q, x_q);
ok &= NearEqual(y_q[0] , check, eps, eps);
return ok;
}
/* %$$
$end
*/
|