/home/coin/SVN-release/CoinAll-1.1.0/cppad/cppad/local/compare.hpp

Go to the documentation of this file.
00001 # ifndef CPPAD_COMPARE_INCLUDED
00002 # define CPPAD_COMPARE_INCLUDED
00003 
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Common Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 
00015 /*
00016 -------------------------------------------------------------------------------
00017 $begin Compare$$
00018 $spell
00019         cos
00020         Op
00021         bool
00022         const
00023         inline
00024 $$
00025 
00026 $index binary, AD compare operator$$
00027 $index AD, binary compare operator$$
00028 $index compare, AD binary operator$$
00029 $index operator, AD binary compare$$
00030 
00031 $index <, AD operator$$
00032 $index <=, AD operator$$
00033 $index >, AD operator$$
00034 $index >=, AD operator$$
00035 $index ==, AD operator$$
00036 $index !=, AD operator$$
00037 
00038 $section AD Binary Comparison Operators$$
00039 
00040 
00041 $head Syntax$$
00042 
00043 $syntax%%b% = %x% %Op% %y%$$
00044 
00045 
00046 $head Purpose$$
00047 Compares two operands where one of the operands is an
00048 $syntax%AD<%Base%>%$$ object.
00049 The comparison has the same interpretation as for 
00050 the $italic Base$$ type.
00051 
00052 
00053 $head Op$$
00054 The operator $italic Op$$ is one of the following:
00055 $table
00056 $bold Op$$ $pre $$  $cnext $bold Meaning$$                           $rnext
00057 $code <$$   $cnext is $italic x$$ less than $italic y$$              $rnext
00058 $code <=$$  $cnext is $italic x$$ less than or equal $italic y$$     $rnext
00059 $code >$$   $cnext is $italic x$$ greater than $italic y$$           $rnext
00060 $code >=$$  $cnext is $italic x$$ greater than or equal $italic y$$  $rnext
00061 $code ==$$  $cnext is $italic x$$ equal to $italic y$$               $rnext
00062 $code !=$$  $cnext is $italic x$$ not equal to $italic y$$
00063 $tend
00064  
00065 $head x$$
00066 The operand $italic x$$ has prototype
00067 $syntax%
00068         const %Type% &%x%
00069 %$$
00070 where $italic Type$$ is $syntax%AD<%Base%>%$$, $italic Base$$, or $code int$$.
00071 
00072 $head y$$
00073 The operand $italic y$$ has prototype
00074 $syntax%
00075         const %Type% &%y%
00076 %$$
00077 where $italic Type$$ is $syntax%AD<%Base%>%$$, $italic Base$$, or $code int$$.
00078 
00079 $head b$$
00080 The result $italic b$$ has type
00081 $syntax%
00082         bool %b%
00083 %$$
00084 
00085 $head Operation Sequence$$
00086 The result of this operation is a $code bool$$ value
00087 (not an $xref/glossary/AD of Base/AD of Base/$$ object).
00088 Thus it will not be recorded as part of an
00089 AD of $italic Base$$
00090 $xref/glossary/Operation/Sequence/operation sequence/1/$$.
00091 $pre
00092 
00093 $$
00094 For example, suppose 
00095 $italic x$$ and $italic y$$ are $syntax%AD<%Base%>%$$ objects,
00096 the tape corresponding to $syntax%AD<%Base%>%$$ is recording,
00097 $italic b$$ is true,
00098 and the subsequent code is
00099 $syntax%
00100         if( %b% )
00101                 %y% = cos(%x%);
00102         else    %y% = sin(%x%); 
00103 %$$
00104 only the assignment $syntax%%y% = cos(%x%)%$$ is recorded on the tape
00105 (if $italic x$$ is a $cref/parameter/glossary/Parameter/$$, 
00106 nothing is recorded).
00107 The $xref/CompareChange/$$ function can yield
00108 some information about changes in comparison operation results.
00109 You can use $xref/CondExp/$$ to obtain comparison operations
00110 that depends on the 
00111 $cref/independent variable/glossary/Tape/Independent Variable/$$ 
00112 values with out re-taping the AD sequence of operations.
00113 
00114 $head Assumptions$$
00115 If one of the $italic Op$$ operators listed above
00116 is used with an $syntax%AD<%Base%>%$$ object,
00117 it is assumed that the same operator is supported by the base type 
00118 $italic Base$$.
00119 
00120 $head Example$$
00121 $children%
00122         example/compare.cpp
00123 %$$
00124 The file
00125 $xref/Compare.cpp/$$
00126 contains an example and test of these operations.
00127 It returns true if it succeeds and false otherwise.
00128 
00129 $end
00130 -------------------------------------------------------------------------------
00131 */
00132 //  BEGIN CppAD namespace
00133 namespace CppAD {
00134 
00135 template <class Base>
00136 
00137 // -------------- RecordCompare(cop, result, left, right) --------------------
00138 void ADTape<Base>::RecordCompare(
00139         enum CompareOp  cop   ,
00140         bool           result ,
00141         const AD<Base> &left  ,
00142         const AD<Base> &right )
00143 {       size_t ind0, ind1, ind2, ind3;
00144 
00145         // ind[1] = base 2 representation of [result, Var(left), Var(right])
00146         ind1 = 0;
00147 
00148         // ind[2] = left address
00149         if( Parameter(left) )
00150                 ind2 = Rec.PutPar(left.value_);
00151         else
00152         {       ind1 += 2;
00153                 ind2 =  left.taddr_;
00154         }
00155 
00156         // ind[3] = right address
00157         if( Parameter(right) )
00158                 ind3 = Rec.PutPar(right.value_);
00159         else
00160         {       ind1 += 4;
00161                 ind3 =  right.taddr_;
00162         }
00163 
00164         // If both left and right are parameters, do not need to record
00165         if( ind1 == 0 )
00166                 return;
00167 
00168         // ind[1] & 1 = result
00169         if( result )
00170                 ind1+= 1;
00171 
00172         // ind[0] = cop 
00173         ind0 = size_t (cop);
00174 
00175         CPPAD_ASSERT_UNKNOWN( ind1 > 1 );
00176         CPPAD_ASSERT_UNKNOWN( NumInd(ComOp) == 4 );
00177 
00178         // put the operator in the tape
00179         Rec.PutOp(ComOp);
00180         Rec.PutInd(ind0, ind1, ind2, ind3);
00181 }
00182 
00183 // -------------------------------- < -------------------------
00184 # ifdef NDEBUG
00185 
00186 template <class Base>
00187 inline bool AD<Base>::operator < (const AD<Base> &right) const
00188 {       bool result =  (value_ < right.value_); 
00189         return result;
00190 }
00191 
00192 # else
00193 template <class Base>
00194 inline bool AD<Base>::operator < (const AD<Base> &right) const
00195 {       bool result =  (value_ < right.value_); 
00196 
00197         ADTape<Base> *tape = CPPAD_NULL;
00198         if( Variable(*this) )
00199                 tape = tape_this();
00200         else if ( Variable(right) )
00201                 tape = right.tape_this();
00202 
00203         if( tape != CPPAD_NULL )
00204                 tape->RecordCompare(CompareLt, result, *this, right);
00205 
00206         return result;
00207 }
00208 # endif
00209 
00210 // convert other cases into the case above
00211 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(<)
00212 
00213 // -------------------------------- <= -------------------------
00214 # ifdef NDEBUG
00215 
00216 template <class Base>
00217 inline bool AD<Base>::operator <= (const AD<Base> &right) const
00218 {       bool result =  (value_ <= right.value_); 
00219         return result;
00220 }
00221 
00222 # else
00223 template <class Base>
00224 inline bool AD<Base>::operator <= (const AD<Base> &right) const
00225 {       bool result =  (value_ <= right.value_); 
00226 
00227         ADTape<Base> *tape = CPPAD_NULL;
00228         if( Variable(*this) )
00229                 tape = tape_this();
00230         else if ( Variable(right) )
00231                 tape = right.tape_this();
00232 
00233         if( tape != CPPAD_NULL )
00234                 tape->RecordCompare(CompareLe, result, *this, right);
00235 
00236         return result;
00237 }
00238 # endif
00239 
00240 // convert other cases into the case above
00241 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(<=)
00242 
00243 
00244 // -------------------------------- > -------------------------
00245 # ifdef NDEBUG
00246 
00247 template <class Base>
00248 inline bool AD<Base>::operator > (const AD<Base> &right) const
00249 {       bool result =  (value_ > right.value_); 
00250         return result;
00251 }
00252 
00253 # else
00254 template <class Base>
00255 inline bool AD<Base>::operator > (const AD<Base> &right) const
00256 {       bool result =  (value_ > right.value_); 
00257 
00258         ADTape<Base> *tape = CPPAD_NULL;
00259         if( Variable(*this) )
00260                 tape = tape_this();
00261         else if ( Variable(right) )
00262                 tape = right.tape_this();
00263 
00264         if( tape != CPPAD_NULL )
00265                 tape->RecordCompare(CompareGt, result, *this, right);
00266 
00267 
00268         return result;
00269 }
00270 # endif
00271 
00272 // convert other cases into the case above
00273 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(>)
00274 
00275 // -------------------------------- >= -------------------------
00276 # ifdef NDEBUG
00277 
00278 template <class Base>
00279 inline bool AD<Base>::operator >= (const AD<Base> &right) const
00280 {       bool result =  (value_ >= right.value_); 
00281         return result;
00282 }
00283 
00284 # else
00285 template <class Base>
00286 inline bool AD<Base>::operator >= (const AD<Base> &right) const
00287 {       bool result =  (value_ >= right.value_); 
00288 
00289         ADTape<Base> *tape = CPPAD_NULL;
00290         if( Variable(*this) )
00291                 tape = tape_this();
00292         else if ( Variable(right) )
00293                 tape = right.tape_this();
00294 
00295         if( tape != CPPAD_NULL )
00296                 tape->RecordCompare(CompareGe, result, *this, right);
00297 
00298         return result;
00299 }
00300 # endif
00301 
00302 // convert other cases into the case above
00303 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(>=)
00304 
00305 
00306 // -------------------------------- == -------------------------
00307 # ifdef NDEBUG
00308 
00309 template <class Base>
00310 inline bool AD<Base>::operator == (const AD<Base> &right) const
00311 {       bool result =  (value_ == right.value_); 
00312         return result;
00313 }
00314 
00315 # else 
00316 template <class Base>
00317 inline bool AD<Base>::operator == (const AD<Base> &right) const
00318 {       bool result =  (value_ == right.value_); 
00319 
00320         ADTape<Base> *tape = CPPAD_NULL;
00321         if( Variable(*this) )
00322                 tape = tape_this();
00323         else if ( Variable(right) )
00324                 tape = right.tape_this();
00325 
00326         if( tape != CPPAD_NULL )
00327                 tape->RecordCompare(CompareEq, result, *this, right);
00328 
00329         return result;
00330 }
00331 # endif
00332 
00333 // convert other cases into the case above
00334 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(==)
00335 
00336 // -------------------------------- != -------------------------
00337 # ifdef NDEBUG
00338 
00339 template <class Base>
00340 inline bool AD<Base>::operator != (const AD<Base> &right) const
00341 {       bool result =  (value_ != right.value_);
00342         return result;
00343 }
00344 
00345 # else
00346 template <class Base>
00347 inline bool AD<Base>::operator != (const AD<Base> &right) const
00348 {       bool result =  (value_ != right.value_);
00349 
00350         ADTape<Base> *tape = CPPAD_NULL;
00351         if( Variable(*this) )
00352                 tape = tape_this();
00353         else if ( Variable(right) )
00354                 tape = right.tape_this();
00355 
00356         if( tape != CPPAD_NULL )
00357                 tape->RecordCompare(CompareNe, result, *this, right);
00358 
00359         return result;
00360 }
00361 # endif
00362 
00363 // convert other cases into the case above
00364 CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(!=)
00365 
00366 } // END CppAD namespace
00367 
00368 # endif

Generated on Sun Nov 14 14:06:33 2010 for Coin-All by  doxygen 1.4.7