@(@\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
.
AD Conditional Expressions
Syntax result = CondExpRel(left, right, if_true, if_false)
Purpose
Record,
as part of an AD of
Baseoperation sequence
,
the conditional result
if( leftCopright ) result = if_true
else result = if_false
The relational
Rel
and comparison operator
Cop
above have the following correspondence:
Rel Lt Le Eq Ge Gt Cop < <= == >= >
If
f
is the ADFun
object corresponding to the
AD operation sequence,
the assignment choice for
result
in an AD conditional expression is made each time
f.Forward
is used to evaluate the zero order Taylor
coefficients with new values for the
independent variables
.
This is in contrast to the AD comparison operators
which are boolean valued and not included in the AD operation sequence.
Rel
In the syntax above, the relation
Rel
represents one of the following
two characters: Lt, Le, Eq, Ge, Gt.
As in the table above,
Rel
determines which comparison operator
Cop
is used
when comparing
left
and
right
.
Type
These functions are defined in the CppAD namespace for arguments of
Type
is float , double, or any type of the form
AD<Base>
.
(Note that all four arguments must have the same type.)
left
The argument
left
has prototype
const Type& left
It specifies the value for the left side of the comparison operator.
right
The argument
right
has prototype
const Type& right
It specifies the value for the right side of the comparison operator.
if_true
The argument
if_true
has prototype
const Type& if_true
It specifies the return value if the result of the comparison is true.
if_false
The argument
if_false
has prototype
const Type& if_false
It specifies the return value if the result of the comparison is false.
Optimize
The optimize
method will optimize conditional expressions
in the following way:
During zero order forward mode
,
once the value of the
left
and
right
have been determined,
it is known if the true or false case is required.
From this point on, values corresponding to the case that is not required
are not computed.
This optimization is done for the rest of zero order forward mode
as well as forward and reverse derivatives calculations.
Deprecate 2005-08-07
Previous versions of CppAD used
CondExp(flag, if_true, if_false)
for the same meaning as
CondExpGt(flag, Type(0), if_true, if_false)
Use of CondExp is deprecated, but continues to be supported.
Test
The file
cond_exp.cpp
contains an example and test of this function.
Atan2
The following implementation of the
AD atan2
function is a more complex
example of using conditional expressions:
template <class Base>
AD<Base> atan2 (const AD<Base> &y, const AD<Base> &x)
{ //// zero, pi2, pi
AD<Base> zero(0.);
AD<Base> pi2(2. * atan(1.));
AD<Base> pi(2. * pi2);
//// abs_x, abs_y// Not using fabs because its derivative is zero at zero
AD<Base> abs_x = CondExpGe(x, zero, x, -x);
AD<Base> abs_y = CondExpGe(y, zero, y, -y);
//// first// This is the result for first quadrant: x >= 0 , y >= 0
AD<Base> alpha = atan(abs_y / abs_x);
AD<Base> beta = pi2 - atan(abs_x / abs_y);
AD<Base> first = CondExpGt(abs_x, abs_y, alpha, beta);
//// second// This is the result for second quadrant: x <= 0 , y >= 0
AD<Base> second = pi - first;
//// third// This is the result for third quadrant: x <= 0 , y <= 0
AD<Base> third = - pi + first;
//// fourth// This is the result for fourth quadrant: x >= 0 , y <= 0
AD<Base> fourth = - first;
//// alpha// This is the result for x >= 0
alpha = CondExpGe(y, zero, first, fourth);
//// beta// This is the result for x <= 0
beta = CondExpGe(y, zero, second, third);
////
AD<Base> result = CondExpGe(x, zero, alpha, beta);
return result;
}