|
Prev | Next |
AD<Base> can be used.
In the case where Base is
float,
double,
std::complex<float>,
std::complex<double>,
or AD<Other>,
these requirements are provided by including he file
cppad/cppad.hpp.
enum type
CompareOp (and possible other things).
This should be done with the following include command:
# include <cppad/local/declare.hpp>
y = abs(x)
which computes the absolute value of it's argument.
The argument x has prototype
const Base &x
and the return value y has prototype
Base y
The CppAD abs derivative calculation assumes that
the type Base is ordered; i.e.,
\[
{\rm abs} (x) = \left\{ \begin{array}{ll}
x & {\rm if} \; x \geq 0
\\
- x & {\rm otherwise}
\end{array} \right.
\]
abs function can be defined by
namespace CppAD {
inline Base abs(const Base &x)
{ if( x < Base(0) )
return - x;
return x;
}
}
abs function will not be computed correctly.
In this case one might (but need not) define abs as follows:
namespace CppAD {
inline Base abs(const Base &x)
{ // attempt to use abs with a Base argument
assert(0);
return x;
}
}
result = CondExpOp(cop, left, right, trueCase, falseCase)
which computes the result for the corresponding CondExp
function.
The argument cop has prototype
enum CppAD::CompareOp cop
The possible values for this enum type are
CompareLt,
CompareLe,
CompareEq,
CompareGe, and
CompareGt.
The other arguments have the prototype
const Base &left ,
const Base &right ,
const Base &trueCase ,
const Base &falseCase )
The result has prototype
Base &result
<, <=, ==, >=, and > operators
its CondExpOp function can be defined by
namespace CppAD {
inline Base CondExpOp(
enum CppAD::CompareOp cop ,
const Base &left ,
const Base &right ,
const Base &trueCase ,
const Base &falseCase )
{ return CppAD::CondExpTemplate(
cop, left, right, trueCase, falseCase);
}
}
CondExpOp function does not make sense.
In this case one might (but need not) define CondExpOp as follows:
namespace CppAD {
inline Base CondExpOp(
enum CompareOp cop ,
const Base &left ,
const Base &right ,
const Base &trueCase ,
const Base &falseCase )
{ // attempt to use CondExp with a Base argument
assert(0);
return Base(0);
}
}
AD<Base>,
the type Base must support the syntax
b = EqualOpSeq(x, y)
which returns true if and only if x is equal to y
(this is used by the EqualOpSeq
function).
The arguments x and y have prototype
const Base &x
const Base &y
The return value b has prototype
bool b
EqualOpSeq function can be defined by
namespace CppAD {
inline Base EqualOpSeq(const Base &x, const Base &y)
{ return x == y; }
}
y = erf(x)
which computes the erf
function.
The argument x has prototype
const Base &x
and the return value y has prototype
Base y
erf function, you could make its use
by AD<Base> an error as follows:
namespace CppAD {
inline Base erf(const Base &x)
{ assert (0);
return Base(0);
}
}
AD<Base>,
CppAD must know if the Base value corresponding to an operation
will be the same.
For example, suppose the current operation is between two
AD<Base> objects where Base is AD<double>;
some optimizations depend on one of the objects being a
parameter
as well as its
corresponding Base value also being a parameter.
In general, the type Base must support the following functions:
| Syntax | Result |
b = IdenticalPar(x)
| the Base value will always be the same |
b = IdenticalZero(x)
|
x equals zero and IdenticalPar(x)
|
b = IdenticalOne(x)
|
x equals one and IdenticalPar(x)
|
b = IdenticalEqualPar(x, y)
|
x equals y,
IdenticalPar(x) and
IdenticalPar(y)
|
const Base x
If it is present, the argument y has prototype
const Base y
The result b has prototype
bool b
false is a slow but safer option for all of these functions.
If Base is a relatively simple type
(does not record operations for future calculations),
the IdenticalPar function can be defined by
namespace CppAD {
inline bool IdenticalPar(const Base &x)
{ return true; }
}
and the IdenticalZero function can be defined by
namespace CppAD {
inline bool IdenticalZero(const Base &x)
{ return x == Base(0); }
}
The other functions could be defined in a similar manner.
If the Base type records operations and may change
the value of x or y during some future calculation,
these functions should return false.
If you are not sure what should be returned,
false is a safer value (but makes some calculations slower).
i = Integer(x)
which converts x to an int.
The argument x has prototype
const Base &x
and the return value i has prototype
int i
Integer function
might be defined by
namespace CppAD {
inline int Integer(const Base &x)
{ return static_cast<int>(x); }
}
>, >=, <, or <=,
Base must support the following functions:
| Syntax | Result |
b = GreaterThanZero(x)
|
x > 0
|
b = GreaterThanOrZero(x)
|
x \geq 0
|
b = LessThanZero(x)
|
x < 0
|
b = LessThanOrZero(x)
|
x \leq 0
|
const Base &x
and the result b has prototype
bool b
namespace CppAD {
inline bool GreaterThanZero(const Base &x)
{ return (x > 0);
}
}
The other functions would replace > by the corresponding operator.
GreaterThanZero as follows:
namespace CppAD {
inline bool GreaterThanZero(const Base &x)
{ // attempt to use GreaterThanZero with a Base argument
assert(0);
return x;
}
}
The other functions would have the corresponding definition.
z = pow(x, y)
which computes
z = x^y
.
The arguments x and y have prototypes
const Base &x
const Base &y
The return value z has prototype
Base z
| Syntax | Result |
y = acos(x) | inverse cosine |
y = asin(x) | inverse sine |
y = atan(x) | inverse tangent |
y = cos(x) | cosine |
y = cosh(x) | hyperbolic cosine |
y = exp(x) | exponential |
y = log(x) | natural logarithm |
y = sin(x) | sine |
y = sinh(x) | hyperbolic sine |
y = sqrt(x) | square root |
y = tan(x) | tangent |
const Base &x
and the result y has prototype
Base y