CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_AD_INCLUDED 00003 # define CPPAD_AD_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 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 // simple AD operations that must be defined for AD as well as base class 00017 # include <cppad/local/ordered.hpp> 00018 # include <cppad/local/identical.hpp> 00019 00020 // define the template classes that are used by the AD template class 00021 # include <cppad/local/op_code.hpp> 00022 # include <cppad/local/recorder.hpp> 00023 # include <cppad/local/player.hpp> 00024 # include <cppad/local/ad_tape.hpp> 00025 00026 namespace CppAD { // BEGIN_CPPAD_NAMESPACE 00027 00028 typedef enum { 00029 tape_manage_new, 00030 tape_manage_delete, 00031 tape_manage_clear 00032 } tape_manage_job; 00033 00034 template <class Base> 00035 class AD { 00036 // enable use of AD<Base> in parallel mode 00037 template <class Type> 00038 friend void parallel_ad(void); 00039 00040 // template friend functions where template parameter is not bound 00041 template <class VectorAD> 00042 friend void Independent(VectorAD &x); 00043 00044 // one argument functions 00045 friend bool Parameter <Base> 00046 (const AD<Base> &u); 00047 friend bool Parameter <Base> 00048 (const VecAD<Base> &u); 00049 friend bool Variable <Base> 00050 (const AD<Base> &u); 00051 friend bool Variable <Base> 00052 (const VecAD<Base> &u); 00053 friend int Integer <Base> 00054 (const AD<Base> &u); 00055 friend AD Var2Par <Base> 00056 (const AD<Base> &u); 00057 00058 // power function 00059 friend AD pow <Base> 00060 (const AD<Base> &x, const AD<Base> &y); 00061 00062 // order determining functions, see ordered.hpp 00063 friend bool GreaterThanZero <Base> (const AD<Base> &x); 00064 friend bool GreaterThanOrZero <Base> (const AD<Base> &x); 00065 friend bool LessThanZero <Base> (const AD<Base> &x); 00066 friend bool LessThanOrZero <Base> (const AD<Base> &x); 00067 friend bool abs_geq <Base> 00068 (const AD<Base>& x, const AD<Base>& y); 00069 00070 // The identical property functions, see identical.hpp 00071 friend bool IdenticalPar <Base> (const AD<Base> &x); 00072 friend bool IdenticalZero <Base> (const AD<Base> &x); 00073 friend bool IdenticalOne <Base> (const AD<Base> &x); 00074 friend bool IdenticalEqualPar <Base> 00075 (const AD<Base> &x, const AD<Base> &y); 00076 00077 // EqualOpSeq function 00078 friend bool EqualOpSeq <Base> 00079 (const AD<Base> &u, const AD<Base> &v); 00080 00081 // NearEqual function 00082 friend bool NearEqual <Base> ( 00083 const AD<Base> &x, const AD<Base> &y, const Base &r, const Base &a); 00084 00085 friend bool NearEqual <Base> ( 00086 const Base &x, const AD<Base> &y, const Base &r, const Base &a); 00087 00088 friend bool NearEqual <Base> ( 00089 const AD<Base> &x, const Base &y, const Base &r, const Base &a); 00090 00091 // CondExp function 00092 friend AD<Base> CondExpOp <Base> ( 00093 enum CompareOp cop , 00094 const AD<Base> &left , 00095 const AD<Base> &right , 00096 const AD<Base> &trueCase , 00097 const AD<Base> &falseCase 00098 ); 00099 00100 // classes 00101 friend class ADTape<Base>; 00102 friend class ADFun<Base>; 00103 friend class atomic_base<Base>; 00104 friend class discrete<Base>; 00105 friend class VecAD<Base>; 00106 friend class VecAD_reference<Base>; 00107 00108 // arithematic binary operators 00109 friend AD<Base> operator + <Base> 00110 (const AD<Base> &left, const AD<Base> &right); 00111 friend AD<Base> operator - <Base> 00112 (const AD<Base> &left, const AD<Base> &right); 00113 friend AD<Base> operator * <Base> 00114 (const AD<Base> &left, const AD<Base> &right); 00115 friend AD<Base> operator / <Base> 00116 (const AD<Base> &left, const AD<Base> &right); 00117 00118 // comparison operators 00119 friend bool operator < <Base> 00120 (const AD<Base> &left, const AD<Base> &right); 00121 friend bool operator <= <Base> 00122 (const AD<Base> &left, const AD<Base> &right); 00123 friend bool operator > <Base> 00124 (const AD<Base> &left, const AD<Base> &right); 00125 friend bool operator >= <Base> 00126 (const AD<Base> &left, const AD<Base> &right); 00127 friend bool operator == <Base> 00128 (const AD<Base> &left, const AD<Base> &right); 00129 friend bool operator != <Base> 00130 (const AD<Base> &left, const AD<Base> &right); 00131 00132 // input operator 00133 friend std::istream& operator >> <Base> 00134 (std::istream &is, AD<Base> &x); 00135 00136 // output operations 00137 friend std::ostream& operator << <Base> 00138 (std::ostream &os, const AD<Base> &x); 00139 friend void PrintFor <Base> ( 00140 const AD<Base>& flag , 00141 const char* before , 00142 const AD<Base>& var , 00143 const char* after 00144 ); 00145 public: 00146 // type of value 00147 typedef Base value_type; 00148 00149 // implicit default constructor 00150 inline AD(void); 00151 00152 // use default implicit copy constructor and assignment operator 00153 // inline AD(const AD &x); 00154 // inline AD& operator=(const AD &x); 00155 00156 // implicit construction and assingment from base type 00157 inline AD(const Base &b); 00158 inline AD& operator=(const Base &b); 00159 00160 // implicit contructor and assignment from VecAD<Base>::reference 00161 inline AD(const VecAD_reference<Base> &x); 00162 inline AD& operator=(const VecAD_reference<Base> &x); 00163 00164 # if CPPAD_IMPLICIT_CTOR_FROM_ANY_TYPE 00165 // implicit construction from some other type (depricated) 00166 template <class T> inline AD(const T &t); 00167 # else 00168 // explicit construction from some other type (depricated) 00169 template <class T> inline explicit AD(const T &t); 00170 # endif 00171 00172 // assignment from some other type 00173 template <class T> inline AD& operator=(const T &right); 00174 00175 // base type corresponding to an AD object 00176 friend Base Value <Base> (const AD<Base> &x); 00177 00178 // computed assignment operators 00179 inline AD& operator += (const AD &right); 00180 inline AD& operator -= (const AD &right); 00181 inline AD& operator *= (const AD &right); 00182 inline AD& operator /= (const AD &right); 00183 00184 // unary operators 00185 inline AD operator +(void) const; 00186 inline AD operator -(void) const; 00187 00188 // destructor 00189 ~AD(void) 00190 { } 00191 00192 // interface so these functions need not be friends 00193 inline AD Abs(void) const; 00194 inline AD acos(void) const; 00195 inline AD asin(void) const; 00196 inline AD atan(void) const; 00197 inline AD cos(void) const; 00198 inline AD cosh(void) const; 00199 inline AD exp(void) const; 00200 inline AD fabs(void) const; 00201 inline AD log(void) const; 00202 inline AD sin(void) const; 00203 inline AD Sign(void) const; 00204 inline AD sinh(void) const; 00205 inline AD sqrt(void) const; 00206 inline AD tan(void) const; 00207 inline AD tanh(void) const; 00208 00209 // ---------------------------------------------------------- 00210 // static public member functions 00211 00212 // abort current AD<Base> recording 00213 static void abort_recording(void); 00214 00215 // set the maximum number of OpenMP threads (deprecated) 00216 static void omp_max_thread(size_t number); 00217 00218 // These functions declared public so can be accessed by user through 00219 // a macro interface and are not intended for direct use. 00220 // The macro interface is documented in bool_fun.hpp. 00221 // Developer documentation for these fucntions is in bool_fun.hpp 00222 static inline bool UnaryBool( 00223 bool FunName(const Base &x), 00224 const AD<Base> &x 00225 ); 00226 static inline bool BinaryBool( 00227 bool FunName(const Base &x, const Base &y), 00228 const AD<Base> &x , const AD<Base> &y 00229 ); 00230 00231 private: 00232 // value_ corresponding to this object 00233 Base value_; 00234 00235 // Tape identifier corresponding to taddr 00236 tape_id_t tape_id_; 00237 00238 // taddr_ in tape for this variable 00239 addr_t taddr_; 00240 // 00241 // Make this variable a parameter 00242 // 00243 void make_parameter(void) 00244 { CPPAD_ASSERT_UNKNOWN( Variable(*this) ); // currently a var 00245 tape_id_ = 0; 00246 } 00247 // 00248 // Make this parameter a new variable 00249 // 00250 void make_variable(size_t id, size_t taddr) 00251 { CPPAD_ASSERT_UNKNOWN( Parameter(*this) ); // currently a par 00252 CPPAD_ASSERT_UNKNOWN( taddr > 0 ); // sure valid taddr 00253 00254 taddr_ = taddr; 00255 tape_id_ = id; 00256 } 00257 // --------------------------------------------------------------- 00258 // tape linking functions 00259 // 00260 // not static 00261 inline ADTape<Base>* tape_this(void) const; 00262 // 00263 // static 00264 inline static tape_id_t** tape_id_handle(size_t thread); 00265 inline static tape_id_t* tape_id_ptr(size_t thread); 00266 inline static ADTape<Base>** tape_handle(size_t thread); 00267 static ADTape<Base>* tape_manage(tape_manage_job job); 00268 inline static ADTape<Base>* tape_ptr(void); 00269 inline static ADTape<Base>* tape_ptr(tape_id_t tape_id); 00270 }; 00271 // --------------------------------------------------------------------------- 00272 00273 } // END_CPPAD_NAMESPACE 00274 00275 // tape linking private functions 00276 # include <cppad/local/tape_link.hpp> 00277 00278 // operations that expect the AD template class to be defined 00279 00280 00281 # endif