Prev Next

AD Conditional Expressions

Syntax
result = CondExpOp(leftrighttrueCasefalseCase)

Purpose
Record, as part of an AD of Base operation sequence , a the conditional result of the form
     if( 
left op right )
          
result = trueCase
     else 
result = falseCase
The notation Op and op above have the following correspondence:
Op    Lt    Le    Eq    Ge    Gt
op < <= == >= >
If f is the ADFun object corresponding to the AD operation sequence, the choice 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.

Type
We use Type for the type of left, right, trueCase, and falseCase (which must all have the same type). This type must be float, double, or in the AD levels above above float or double.

Op
In the syntax above, Op represents one of the following two characters: Lt, Le, Eq, Ge, Gt. As in the table above, Op determines the comparison operator op.

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.

trueCase
The argument trueCase has prototype
     const 
Type &trueCase
It specifies the return value if the result of the comparison is true.

falseCase
The argument falseCase has prototype
     const 
Type &falseCase
It specifies the return value if the result of the comparison is false.

result
The result has prototype
     
Type &falseCase

CondExp
Previous versions of CppAD used
     CondExp(
flagtrueCasefalseCase)
for the same meaning as
     CondExpGt(
flagType(0), trueCasefalseCase)
Use of CondExp is deprecated, but continues to be supported.

Operation Sequence
This is an AD of Base atomic operation and hence is part of the current AD of Base operation sequence .

Example

Test
The file CondExp.cpp contains an example and test of this function. It returns true if it succeeds and false otherwise.

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)
{    AD<Base> alpha;
     AD<Base> beta;
     AD<Base> theta;

     AD<Base> zero = 0;
     AD<Base> pi2  = 2. * atan(1.);
     AD<Base> pi   = 2. * pi2;

     AD<Base> ax = abs(x);
     AD<Base> ay = abs(y);

     // if( ax > ay )
     //   theta = atan(ay / ax);
     // else   theta = pi2 - atan(ax / ay);
     alpha = atan(ay / ax);
     beta  = pi2 - atan(ax / ay);
     theta = CondExpGt(ax, ay, alpha, beta);         // use of CondExp

     // if( x <= 0 )
     //   theta = pi - theta;
     theta = CondExpLe(x, zero, pi - theta, theta);  // use of CondExp
     
     // if( y <= 0 )
     //   theta = - theta;
     theta = CondExpLe(y, zero, -theta, theta);      // use of CondExp

     return theta;
}

Input File: cppad/local/cond_exp.hpp