CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_DEFINE_INCLUDED 00003 # define CPPAD_DEFINE_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 00016 /*! 00017 \file define.hpp 00018 Define processor symbols and macros that are used by CppAD. 00019 */ 00020 00021 /*! 00022 \def CPPAD_OP_CODE_TYPE 00023 Is the type used to store enum OpCode values. If not the same as OpCode, then 00024 <code>sizeof(CPPAD_OP_CODE_TYPE) <= sizeof( enum OpCode )</code> 00025 to conserve memory. 00026 This type must support \c std::numeric_limits, 00027 the \c <= operator, 00028 and conversion to \c size_t. 00029 Make sure that the type chosen returns true for is_pod<CPPAD_OP_CODE_TYPE> 00030 in pod_vector.hpp. 00031 */ 00032 # define CPPAD_OP_CODE_TYPE unsigned char 00033 00034 00035 /*! 00036 \def CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION 00037 A version of the inline command that works with MC compiler. 00038 00039 Microsoft Visual C++ version 9.0 generates a warning if a template 00040 function is declared as a friend 00041 (this was not a problem for version 7.0). 00042 The warning identifier is 00043 \verbatim 00044 warning C4396 00045 \endverbatim 00046 and it contains the text 00047 \verbatim 00048 the inline specifier cannot be used when a friend declaration refers 00049 to a specialization of a function template 00050 \endverbatim 00051 This happens even if the function is not a specialization. 00052 This macro is defined as empty for Microsoft compilers. 00053 */ 00054 # ifdef _MSC_VER 00055 # define CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION 00056 # else 00057 # define CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION inline 00058 # endif 00059 00060 /*! 00061 \def CPPAD_NULL 00062 This preprocessor symbol is used for a null pointer. 00063 00064 If it is not yet defined, 00065 it is defined when cppad/local/define.hpp is included. 00066 */ 00067 # ifndef CPPAD_NULL 00068 # if CPPAD_HAS_NULLPTR 00069 # define CPPAD_NULL nullptr 00070 # else 00071 # define CPPAD_NULL 0 00072 # endif 00073 # endif 00074 00075 /*! 00076 \def CPPAD_FOLD_ASSIGNMENT_OPERATOR(Op) 00077 Declares automatic coercion for certain AD assignment operations. 00078 00079 This macro assumes that the operator 00080 \verbatim 00081 left Op right 00082 \endverbatim 00083 is defined for the case where left and right have type AD<Base>. 00084 It uses this case to define the cases where 00085 left has type AD<Base> and right has type 00086 VecAD_reference<Base>, 00087 Base, or 00088 double. 00089 The argument right is const and call by reference. 00090 This macro converts the operands to AD<Base> and then 00091 uses the definition of the same operation for that case. 00092 */ 00093 00094 # define CPPAD_FOLD_ASSIGNMENT_OPERATOR(Op) \ 00095 /* ----------------------------------------------------------------*/ \ 00096 template <class Base> \ 00097 inline AD<Base>& operator Op \ 00098 (AD<Base> &left, double right) \ 00099 { return left Op AD<Base>(right); } \ 00100 \ 00101 template <class Base> \ 00102 inline AD<Base>& operator Op \ 00103 (AD<Base> &left, const Base &right) \ 00104 { return left Op AD<Base>(right); } \ 00105 \ 00106 inline AD<double>& operator Op \ 00107 (AD<double> &left, const double &right) \ 00108 { return left Op AD<double>(right); } \ 00109 \ 00110 template <class Base> \ 00111 inline AD<Base>& operator Op \ 00112 (AD<Base> &left, const VecAD_reference<Base> &right) \ 00113 { return left Op right.ADBase(); } 00114 00115 // ===================================================================== 00116 /*! 00117 \def CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(Op) 00118 Declares automatic coercion for certain binary operations with AD result. 00119 00120 This macro assumes that the operator 00121 \verbatim 00122 left Op right 00123 \endverbatim 00124 is defined for the case where left and right 00125 and the result of the operation all 00126 have type AD<Base>. 00127 It uses this case to define the cases either left 00128 or right has type VecAD_reference<Base> or AD<Base> 00129 and the type of the other operand is one of the following: 00130 VecAD_reference<Base>, AD<Base>, Base, double. 00131 All of the arguments are const and call by reference. 00132 This macro converts the operands to AD<Base> and then 00133 uses the definition of the same operation for that case. 00134 */ 00135 # define CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(Op) \ 00136 /* ----------------------------------------------------------------*/ \ 00137 /* Operations with VecAD_reference<Base> and AD<Base> only*/ \ 00138 \ 00139 template <class Base> \ 00140 inline AD<Base> operator Op \ 00141 (const AD<Base> &left, const VecAD_reference<Base> &right) \ 00142 { return left Op right.ADBase(); } \ 00143 \ 00144 template <class Base> \ 00145 inline AD<Base> operator Op \ 00146 (const VecAD_reference<Base> &left, const VecAD_reference<Base> &right)\ 00147 { return left.ADBase() Op right.ADBase(); } \ 00148 \ 00149 template <class Base> \ 00150 inline AD<Base> operator Op \ 00151 (const VecAD_reference<Base> &left, const AD<Base> &right) \ 00152 { return left.ADBase() Op right; } \ 00153 /* ----------------------------------------------------------------*/ \ 00154 /* Operations Base */ \ 00155 \ 00156 template <class Base> \ 00157 inline AD<Base> operator Op \ 00158 (const Base &left, const AD<Base> &right) \ 00159 { return AD<Base>(left) Op right; } \ 00160 \ 00161 template <class Base> \ 00162 inline AD<Base> operator Op \ 00163 (const Base &left, const VecAD_reference<Base> &right) \ 00164 { return AD<Base>(left) Op right.ADBase(); } \ 00165 \ 00166 template <class Base> \ 00167 inline AD<Base> operator Op \ 00168 (const AD<Base> &left, const Base &right) \ 00169 { return left Op AD<Base>(right); } \ 00170 \ 00171 template <class Base> \ 00172 inline AD<Base> operator Op \ 00173 (const VecAD_reference<Base> &left, const Base &right) \ 00174 { return left.ADBase() Op AD<Base>(right); } \ 00175 \ 00176 /* ----------------------------------------------------------------*/ \ 00177 /* Operations double */ \ 00178 \ 00179 template <class Base> \ 00180 inline AD<Base> operator Op \ 00181 (const double &left, const AD<Base> &right) \ 00182 { return AD<Base>(left) Op right; } \ 00183 \ 00184 template <class Base> \ 00185 inline AD<Base> operator Op \ 00186 (const double &left, const VecAD_reference<Base> &right) \ 00187 { return AD<Base>(left) Op right.ADBase(); } \ 00188 \ 00189 template <class Base> \ 00190 inline AD<Base> operator Op \ 00191 (const AD<Base> &left, const double &right) \ 00192 { return left Op AD<Base>(right); } \ 00193 \ 00194 template <class Base> \ 00195 inline AD<Base> operator Op \ 00196 (const VecAD_reference<Base> &left, const double &right) \ 00197 { return left.ADBase() Op AD<Base>(right); } \ 00198 /* ----------------------------------------------------------------*/ \ 00199 /* Special case to avoid ambuigity when Base is double */ \ 00200 \ 00201 inline AD<double> operator Op \ 00202 (const double &left, const AD<double> &right) \ 00203 { return AD<double>(left) Op right; } \ 00204 \ 00205 inline AD<double> operator Op \ 00206 (const double &left, const VecAD_reference<double> &right) \ 00207 { return AD<double>(left) Op right.ADBase(); } \ 00208 \ 00209 inline AD<double> operator Op \ 00210 (const AD<double> &left, const double &right) \ 00211 { return left Op AD<double>(right); } \ 00212 \ 00213 inline AD<double> operator Op \ 00214 (const VecAD_reference<double> &left, const double &right) \ 00215 { return left.ADBase() Op AD<double>(right); } 00216 00217 // ======================================================================= 00218 00219 /*! 00220 \def CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(Op) 00221 Declares automatic coercion for certain binary operations with bool result. 00222 00223 This macro assumes that the operator 00224 \verbatim 00225 left Op right 00226 \endverbatim 00227 is defined for the case where left and right 00228 have type AD<Base> and the result has type bool. 00229 It uses this case to define the cases either left 00230 or right has type 00231 VecAD_reference<Base> or AD<Base> 00232 and the type of the other operand is one of the following: 00233 VecAD_reference<Base>, AD<Base>, Base, double. 00234 All of the arguments are const and call by reference. 00235 This macro converts the operands to AD<Base> and then 00236 uses the definition of the same operation for that case. 00237 */ 00238 # define CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(Op) \ 00239 /* ----------------------------------------------------------------*/ \ 00240 /* Operations with VecAD_reference<Base> and AD<Base> only*/ \ 00241 \ 00242 template <class Base> \ 00243 inline bool operator Op \ 00244 (const AD<Base> &left, const VecAD_reference<Base> &right) \ 00245 { return left Op right.ADBase(); } \ 00246 \ 00247 template <class Base> \ 00248 inline bool operator Op \ 00249 (const VecAD_reference<Base> &left, const VecAD_reference<Base> &right)\ 00250 { return left.ADBase() Op right.ADBase(); } \ 00251 \ 00252 template <class Base> \ 00253 inline bool operator Op \ 00254 (const VecAD_reference<Base> &left, const AD<Base> &right) \ 00255 { return left.ADBase() Op right; } \ 00256 /* ----------------------------------------------------------------*/ \ 00257 /* Operations Base */ \ 00258 \ 00259 template <class Base> \ 00260 inline bool operator Op \ 00261 (const Base &left, const AD<Base> &right) \ 00262 { return AD<Base>(left) Op right; } \ 00263 \ 00264 template <class Base> \ 00265 inline bool operator Op \ 00266 (const Base &left, const VecAD_reference<Base> &right) \ 00267 { return AD<Base>(left) Op right.ADBase(); } \ 00268 \ 00269 template <class Base> \ 00270 inline bool operator Op \ 00271 (const AD<Base> &left, const Base &right) \ 00272 { return left Op AD<Base>(right); } \ 00273 \ 00274 template <class Base> \ 00275 inline bool operator Op \ 00276 (const VecAD_reference<Base> &left, const Base &right) \ 00277 { return left.ADBase() Op AD<Base>(right); } \ 00278 \ 00279 /* ----------------------------------------------------------------*/ \ 00280 /* Operations double */ \ 00281 \ 00282 template <class Base> \ 00283 inline bool operator Op \ 00284 (const double &left, const AD<Base> &right) \ 00285 { return AD<Base>(left) Op right; } \ 00286 \ 00287 template <class Base> \ 00288 inline bool operator Op \ 00289 (const double &left, const VecAD_reference<Base> &right) \ 00290 { return AD<Base>(left) Op right.ADBase(); } \ 00291 \ 00292 template <class Base> \ 00293 inline bool operator Op \ 00294 (const AD<Base> &left, const double &right) \ 00295 { return left Op AD<Base>(right); } \ 00296 \ 00297 template <class Base> \ 00298 inline bool operator Op \ 00299 (const VecAD_reference<Base> &left, const double &right) \ 00300 { return left.ADBase() Op AD<Base>(right); } \ 00301 /* ----------------------------------------------------------------*/ \ 00302 /* Special case to avoid ambuigity when Base is double */ \ 00303 \ 00304 inline bool operator Op \ 00305 (const double &left, const AD<double> &right) \ 00306 { return AD<double>(left) Op right; } \ 00307 \ 00308 inline bool operator Op \ 00309 (const double &left, const VecAD_reference<double> &right) \ 00310 { return AD<double>(left) Op right.ADBase(); } \ 00311 \ 00312 inline bool operator Op \ 00313 (const AD<double> &left, const double &right) \ 00314 { return left Op AD<double>(right); } \ 00315 \ 00316 inline bool operator Op \ 00317 (const VecAD_reference<double> &left, const double &right) \ 00318 { return left.ADBase() Op AD<double>(right); } 00319 00320 # endif