CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_AD_INCLUDED 00003 # define CPPAD_AD_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 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 CPPAD_BEGIN_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 discrete<Base>; 00104 friend class user_atomic<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 // default constructor 00150 inline AD(void); 00151 00152 // use default copy constructor and assignment operator 00153 // inline AD(const AD &x); 00154 // inline AD& operator=(const AD &x); 00155 00156 // construction and assingment from base type 00157 inline AD(const Base &b); 00158 inline AD& operator=(const Base &b); 00159 00160 // 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 // construction and assignment from some other type 00165 template <class T> inline AD(const T &t); 00166 template <class T> inline AD& operator=(const T &right); 00167 00168 // base type corresponding to an AD object 00169 friend Base Value <Base> (const AD<Base> &x); 00170 00171 // computed assignment operators 00172 inline AD& operator += (const AD &right); 00173 inline AD& operator -= (const AD &right); 00174 inline AD& operator *= (const AD &right); 00175 inline AD& operator /= (const AD &right); 00176 00177 // unary operators 00178 inline AD operator +(void) const; 00179 inline AD operator -(void) const; 00180 00181 // destructor 00182 ~AD(void) 00183 { } 00184 00185 // interface so these functions need not be friends 00186 inline AD Abs(void) const; 00187 inline AD acos(void) const; 00188 inline AD asin(void) const; 00189 inline AD atan(void) const; 00190 inline AD cos(void) const; 00191 inline AD cosh(void) const; 00192 inline AD exp(void) const; 00193 inline AD fabs(void) const; 00194 inline AD log(void) const; 00195 inline AD sin(void) const; 00196 inline AD Sign(void) const; 00197 inline AD sinh(void) const; 00198 inline AD sqrt(void) const; 00199 inline AD tan(void) const; 00200 inline AD tanh(void) const; 00201 00202 // ---------------------------------------------------------- 00203 // static public member functions 00204 00205 // abort current AD<Base> recording 00206 static void abort_recording(void); 00207 00208 // set the maximum number of OpenMP threads (deprecated) 00209 static void omp_max_thread(size_t number); 00210 00211 // These functions declared public so can be accessed by user through 00212 // a macro interface and are not intended for direct use. 00213 // The macro interface is documented in bool_fun.hpp. 00214 // Developer documentation for these fucntions is in bool_fun.hpp 00215 static inline bool UnaryBool( 00216 bool FunName(const Base &x), 00217 const AD<Base> &x 00218 ); 00219 static inline bool BinaryBool( 00220 bool FunName(const Base &x, const Base &y), 00221 const AD<Base> &x , const AD<Base> &y 00222 ); 00223 00224 private: 00225 // value_ corresponding to this object 00226 Base value_; 00227 00228 // Tape identifier corresponding to taddr 00229 tape_id_t tape_id_; 00230 00231 // taddr_ in tape for this variable 00232 addr_t taddr_; 00233 // 00234 // Make this variable a parameter 00235 // 00236 void make_parameter(void) 00237 { CPPAD_ASSERT_UNKNOWN( Variable(*this) ); // currently a var 00238 tape_id_ = 0; 00239 } 00240 // 00241 // Make this parameter a new variable 00242 // 00243 void make_variable(size_t id, size_t taddr) 00244 { CPPAD_ASSERT_UNKNOWN( Parameter(*this) ); // currently a par 00245 CPPAD_ASSERT_UNKNOWN( taddr > 0 ); // sure valid taddr 00246 00247 taddr_ = taddr; 00248 tape_id_ = id; 00249 } 00250 // --------------------------------------------------------------- 00251 // tape linking functions 00252 // 00253 // not static 00254 inline ADTape<Base>* tape_this(void) const; 00255 // 00256 // static 00257 inline static tape_id_t** tape_id_handle(size_t thread); 00258 inline static tape_id_t* tape_id_ptr(size_t thread); 00259 inline static ADTape<Base>** tape_handle(size_t thread); 00260 static ADTape<Base>* tape_manage(tape_manage_job job); 00261 inline static ADTape<Base>* tape_ptr(void); 00262 inline static ADTape<Base>* tape_ptr(tape_id_t tape_id); 00263 }; 00264 // --------------------------------------------------------------------------- 00265 00266 CPPAD_END_NAMESPACE 00267 00268 // tape linking private functions 00269 # include <cppad/local/tape_link.hpp> 00270 00271 // operations that expect the AD template class to be defined 00272 00273 00274 # endif