@(@\newcommand{\W}[1]{ \; #1 \; }
\newcommand{\R}[1]{ {\rm #1} }
\newcommand{\B}[1]{ {\bf #1} }
\newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} }
\newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} }
\newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} }
\newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its
current documentation
.
Example AD<Base> Where Base Constructor Allocates Memory
Purpose
Demonstrate use of
AD<Base>
where memory is allocated for each element of the type
Base
.
In addition, this is a complete example where all the
required Base
type
operations are defined (as apposed to other examples where
some of the operations for the Base type are already defined).
Boolean Operator Macro
This macro can be used for the
base_alloc binary operators that have a
bool result; to be specific,
used with
op
equal to
==,
!=,
<,
<=,
>=, and
>,
Class Definition
The following example class
defines the necessary base_member
functions.
It is made more complicated by storing a pointer to a double
instead of the double value itself.
class base_alloc {
public:
double* ptrdbl_;
base_alloc(void)
{ size_t cap;
void* v = CppAD::thread_alloc::get_memory(sizeof(double), cap);
ptrdbl_ = static_cast<double*>(v);
}
base_alloc(double dbl)
{ size_t cap;
void *v = CppAD::thread_alloc::get_memory(sizeof(double), cap);
ptrdbl_ = static_cast<double*>(v);
*ptrdbl_ = dbl;
}
base_alloc(const base_alloc& x)
{ size_t cap;
void *v = CppAD::thread_alloc::get_memory(sizeof(double), cap);
ptrdbl_ = static_cast<double*>(v);
*ptrdbl_ = *x.ptrdbl_;
}
~base_alloc(void)
{ void* v = static_cast<void*>(ptrdbl_);
CppAD::thread_alloc::return_memory(v);
}
base_alloc operator-(void) const
{ base_alloc result;
*result.ptrdbl_ = - *ptrdbl_;
return result;
}
base_alloc operator+(void) const
{ return *this; }
void operator=(const base_alloc& x)
{ *ptrdbl_ = *x.ptrdbl_; }
BASE_ALLOC_ASSIGN_OPERATOR(+=)
BASE_ALLOC_ASSIGN_OPERATOR(-=)
BASE_ALLOC_ASSIGN_OPERATOR(*=)
BASE_ALLOC_ASSIGN_OPERATOR(/=)
BASE_ALLOC_BINARY_OPERATOR(+)
BASE_ALLOC_BINARY_OPERATOR(-)
BASE_ALLOC_BINARY_OPERATOR(*)
BASE_ALLOC_BINARY_OPERATOR(/)
BASE_ALLOC_BOOL_OPERATOR(==)
BASE_ALLOC_BOOL_OPERATOR(!=)
// The <= operator is not necessary for the base type requirements// (needed so we can use NearEqual with base_alloc arguments).BASE_ALLOC_BOOL_OPERATOR(<=)
};
CondExpOp
The type base_alloc does not use CondExp
operations.
Hence its CondExpOp function is defined by
namespace CppAD {
inline base_alloc CondExpOp(
enum CompareOp cop ,
const base_alloc& left ,
const base_alloc& right ,
const base_alloc& exp_if_true ,
const base_alloc& exp_if_false )
{ // not usedassert(false);
// to void compiler errorreturnbase_alloc();
}
}