CppAD: A C++ Algorithmic Differentiation Package 20110419
ad.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_AD_INCLUDED
00003 # define CPPAD_AD_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Common 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 template <class Base>
00029 class AD {
00030 
00031         // template friend functions where template parameter is not bound
00032         template <class VectorAD>
00033         friend void Independent(VectorAD &x);
00034 
00035         // one argument functions
00036         friend bool Parameter          <Base> 
00037                 (const AD<Base>    &u);
00038         friend bool Parameter          <Base>
00039                 (const VecAD<Base> &u);
00040         friend bool Variable           <Base> 
00041                 (const AD<Base>    &u);
00042         friend bool Variable           <Base> 
00043                 (const VecAD<Base> &u);
00044         friend int  Integer            <Base> 
00045                 (const AD<Base>    &u);
00046         friend AD   Var2Par            <Base>
00047                 (const AD<Base>    &u);
00048 
00049         // power function
00050         friend AD pow <Base>
00051                 (const AD<Base> &x, const AD<Base> &y);
00052 
00053         // order determining functions, see ordered.hpp
00054         friend bool GreaterThanZero   <Base> (const AD<Base> &x);
00055         friend bool GreaterThanOrZero <Base> (const AD<Base> &x);
00056         friend bool LessThanZero      <Base> (const AD<Base> &x);
00057         friend bool LessThanOrZero    <Base> (const AD<Base> &x);
00058 
00059         // The identical property functions, see identical.hpp
00060         friend bool IdenticalPar      <Base> (const AD<Base> &x);
00061         friend bool IdenticalZero     <Base> (const AD<Base> &x);
00062         friend bool IdenticalOne      <Base> (const AD<Base> &x);
00063         friend bool IdenticalEqualPar <Base> 
00064                 (const AD<Base> &x, const AD<Base> &y);
00065 
00066         // EqualOpSeq function
00067         friend bool EqualOpSeq <Base> 
00068                 (const AD<Base> &u, const AD<Base> &v);
00069 
00070         // NearEqual function
00071         friend bool NearEqual <Base> (
00072         const AD<Base> &x, const AD<Base> &y, const Base &r, const Base &a);
00073 
00074         friend bool NearEqual <Base> (
00075         const Base &x, const AD<Base> &y, const Base &r, const Base &a);
00076 
00077         friend bool NearEqual <Base> (
00078         const AD<Base> &x, const Base &y, const Base &r, const Base &a);
00079 
00080         // CondExp function
00081         friend AD<Base> CondExpOp  <Base> (
00082                 enum CompareOp  cop       ,
00083                 const AD<Base> &left      , 
00084                 const AD<Base> &right     , 
00085                 const AD<Base> &trueCase  , 
00086                 const AD<Base> &falseCase 
00087         );
00088 
00089         // classes
00090         friend class ADTape<Base>;
00091         friend class ADFun<Base>;
00092         friend class discrete<Base>;
00093         friend class user_atomic<Base>;
00094         friend class VecAD<Base>;
00095         friend class VecAD_reference<Base>;
00096 
00097         // arithematic binary operators
00098         friend AD<Base> operator + <Base>
00099                 (const AD<Base> &left, const AD<Base> &right);
00100         friend AD<Base> operator - <Base>
00101                 (const AD<Base> &left, const AD<Base> &right);
00102         friend AD<Base> operator * <Base>
00103                 (const AD<Base> &left, const AD<Base> &right);
00104         friend AD<Base> operator / <Base>
00105                 (const AD<Base> &left, const AD<Base> &right);
00106 
00107         // comparison operators
00108         friend bool operator < <Base>
00109                 (const AD<Base> &left, const AD<Base> &right);
00110         friend bool operator <= <Base>
00111                 (const AD<Base> &left, const AD<Base> &right);
00112         friend bool operator > <Base>
00113                 (const AD<Base> &left, const AD<Base> &right);
00114         friend bool operator >= <Base>
00115                 (const AD<Base> &left, const AD<Base> &right);
00116         friend bool operator == <Base>
00117                 (const AD<Base> &left, const AD<Base> &right);
00118         friend bool operator != <Base>
00119                 (const AD<Base> &left, const AD<Base> &right);
00120 
00121         // output operations
00122         friend std::ostream& operator << <Base>
00123                 (std::ostream &os, const AD<Base> &x);
00124         friend void PrintFor <Base>
00125                 (const char *text, const AD<Base> &x);
00126 
00127 public:
00128         // type of value
00129         typedef Base value_type;
00130 
00131         // default constructor
00132         inline AD(void);
00133 
00134         // use default copy constructor and assignment operator
00135         // inline AD(const AD &x);
00136         // inline AD& operator=(const AD &x);
00137 
00138         // construction and assingment from base type
00139         inline AD(const Base &b);
00140         inline AD& operator=(const Base &b); 
00141 
00142         // contructor and assignment from VecAD<Base>::reference
00143         inline AD(const VecAD_reference<Base> &x);
00144         inline AD& operator=(const VecAD_reference<Base> &x);
00145 
00146         // construction and assignment from some other type
00147         template <class T> inline AD(const T &t);
00148         template <class T> inline AD& operator=(const T &right);
00149 
00150         // base type corresponding to an AD object
00151         friend Base Value <Base> (const AD<Base> &x);
00152 
00153         // computed assignment operators
00154         inline AD& operator += (const AD &right);
00155         inline AD& operator -= (const AD &right);
00156         inline AD& operator *= (const AD &right);
00157         inline AD& operator /= (const AD &right);
00158 
00159         // unary operators
00160         inline AD operator +(void) const;
00161         inline AD operator -(void) const;
00162 
00163         // destructor
00164         ~AD(void)
00165         { }
00166 
00167         // interface so these functions need not be friends
00168         inline AD Abs(void) const;
00169         inline AD acos(void) const;
00170         inline AD asin(void) const;
00171         inline AD atan(void) const;
00172         inline AD cos(void) const;
00173         inline AD cosh(void) const;
00174         inline AD exp(void) const;
00175         inline AD log(void) const;
00176         inline AD sin(void) const;
00177         inline AD sinh(void) const;
00178         inline AD sqrt(void) const;
00179 
00180         // ----------------------------------------------------------
00181         // static public member functions
00182         
00183         // abort current AD<Base> recording
00184         static void        abort_recording(void);   
00185 
00186         // set the maximum number of OpenMP threads that will be active
00187         static size_t      omp_max_thread(size_t number);
00188 
00189         // These functions declared public so can be accessed by user through
00190         // a macro interface and are not intended for direct use.
00191         // The macro interface is documented in bool_fun.hpp.
00192         // Developer documentation for these fucntions is in  bool_fun.hpp
00193         static inline bool UnaryBool(
00194                 bool FunName(const Base &x),
00195                 const AD<Base> &x
00196         );
00197         static inline bool BinaryBool(
00198                 bool FunName(const Base &x, const Base &y),
00199                 const AD<Base> &x , const AD<Base> &y
00200         );
00201 
00202 private:
00203         // value_ corresponding to this object
00204         Base value_;
00205 
00206         // Tape identifier corresponding to taddr
00207         // This is a variable if and only if id_ == *id_handle()
00208         // For parameters id_ is CPPAD_MAX_NUM_THREADS, so that following hold
00209         // id_ != 0 , id_ % CPPAD_MAX_NUM_THREADS == 0, id_ != *id_handle().
00210         size_t id_;
00211 
00212         // taddr_ in tape for this variable 
00213         size_t taddr_;
00214         //
00215         // Make this variable a parameter
00216         //
00217         void make_parameter(void)
00218         {       CPPAD_ASSERT_UNKNOWN( Variable(*this) );  // currently a var
00219                 id_ = CPPAD_MAX_NUM_THREADS;
00220         }
00221         //
00222         // Make this parameter a new variable 
00223         //
00224         void make_variable(size_t id,  size_t taddr)
00225         {       CPPAD_ASSERT_UNKNOWN( Parameter(*this) ); // currently a par
00226                 CPPAD_ASSERT_UNKNOWN( taddr > 0 );        // sure valid taddr
00227 
00228                 taddr_ = taddr;
00229                 id_    = id;
00230         }
00231         // ---------------------------------------------------------------
00232         // tape linking functions
00233         // 
00234         // not static
00235         inline ADTape<Base> *tape_this(void) const;
00236         //
00237         // static 
00238         inline static size_t        *id_handle (size_t thread);
00239         inline static ADTape<Base> **tape_handle(size_t thread);
00240         static size_t                tape_new(void);
00241         static void                  tape_delete(size_t id);
00242         inline static ADTape<Base>  *tape_ptr(void);
00243         inline static ADTape<Base>  *tape_ptr(size_t id);
00244 }; 
00245 // ---------------------------------------------------------------------------
00246 
00247 CPPAD_END_NAMESPACE
00248 
00249 // tape linking private functions
00250 # include <cppad/local/tape_link.hpp>
00251 
00252 // operations that expect the AD template class to be defined
00253 
00254 
00255 # endif