CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ad.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_AD_HPP
2 # define CPPAD_CORE_AD_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 // simple AD operations that must be defined for AD as well as base class
16 # include <cppad/core/ordered.hpp>
17 # include <cppad/core/identical.hpp>
18 
19 // define the template classes that are used by the AD template class
20 # include <cppad/local/op_code.hpp>
21 # include <cppad/local/recorder.hpp>
22 # include <cppad/local/player.hpp>
23 # include <cppad/local/ad_tape.hpp>
24 
25 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
26 
27 typedef enum {
32 
33 template <class Base>
34 class AD {
35 private :
36  // -----------------------------------------------------------------------
37  // value_ corresponding to this object
38  Base value_;
39 
40  // Tape identifier corresponding to taddr
42 
43  // taddr_ in tape for this variable
45  // -----------------------------------------------------------------------
46 
47  // enable use of AD<Base> in parallel mode
48  template <class Type>
49  friend void parallel_ad(void);
50 
51  // template friend functions where template parameter is not bound
52  template <class VectorAD>
53  friend void Independent(VectorAD &x, size_t abort_op_index);
54 
55  // one argument functions
56  friend bool Parameter <Base>
57  (const AD<Base> &u);
58  friend bool Parameter <Base>
59  (const VecAD<Base> &u);
60  friend bool Variable <Base>
61  (const AD<Base> &u);
62  friend bool Variable <Base>
63  (const VecAD<Base> &u);
64  friend int Integer <Base>
65  (const AD<Base> &u);
66  friend AD Var2Par <Base>
67  (const AD<Base> &u);
68 
69  // power function
70  friend AD pow <Base>
71  (const AD<Base> &x, const AD<Base> &y);
72 
73  // azmul function
74  friend AD azmul <Base>
75  (const AD<Base> &x, const AD<Base> &y);
76 
77  // order determining functions, see ordered.hpp
78  friend bool GreaterThanZero <Base> (const AD<Base> &x);
79  friend bool GreaterThanOrZero <Base> (const AD<Base> &x);
80  friend bool LessThanZero <Base> (const AD<Base> &x);
81  friend bool LessThanOrZero <Base> (const AD<Base> &x);
82  friend bool abs_geq <Base>
83  (const AD<Base>& x, const AD<Base>& y);
84 
85  // The identical property functions, see identical.hpp
86  friend bool IdenticalPar <Base> (const AD<Base> &x);
87  friend bool IdenticalZero <Base> (const AD<Base> &x);
88  friend bool IdenticalOne <Base> (const AD<Base> &x);
89  friend bool IdenticalEqualPar <Base>
90  (const AD<Base> &x, const AD<Base> &y);
91 
92  // EqualOpSeq function
93  friend bool EqualOpSeq <Base>
94  (const AD<Base> &u, const AD<Base> &v);
95 
96  // NearEqual function
97  friend bool NearEqual <Base> (
98  const AD<Base> &x, const AD<Base> &y, const Base &r, const Base &a);
99 
100  friend bool NearEqual <Base> (
101  const Base &x, const AD<Base> &y, const Base &r, const Base &a);
102 
103  friend bool NearEqual <Base> (
104  const AD<Base> &x, const Base &y, const Base &r, const Base &a);
105 
106  // CondExp function
107  friend AD<Base> CondExpOp <Base> (
108  enum CompareOp cop ,
109  const AD<Base> &left ,
110  const AD<Base> &right ,
111  const AD<Base> &trueCase ,
112  const AD<Base> &falseCase
113  );
114 
115  // classes
116  friend class local::ADTape<Base>;
117  friend class ADFun<Base>;
118  friend class atomic_base<Base>;
119  friend class discrete<Base>;
120  friend class VecAD<Base>;
121  friend class VecAD_reference<Base>;
122 
123  // arithematic binary operators
124  friend AD<Base> operator + <Base>
125  (const AD<Base> &left, const AD<Base> &right);
126  friend AD<Base> operator - <Base>
127  (const AD<Base> &left, const AD<Base> &right);
128  friend AD<Base> operator * <Base>
129  (const AD<Base> &left, const AD<Base> &right);
130  friend AD<Base> operator / <Base>
131  (const AD<Base> &left, const AD<Base> &right);
132 
133  // comparison operators
134  friend bool operator < <Base>
135  (const AD<Base> &left, const AD<Base> &right);
136  friend bool operator <= <Base>
137  (const AD<Base> &left, const AD<Base> &right);
138  friend bool operator > <Base>
139  (const AD<Base> &left, const AD<Base> &right);
140  friend bool operator >= <Base>
141  (const AD<Base> &left, const AD<Base> &right);
142  friend bool operator == <Base>
143  (const AD<Base> &left, const AD<Base> &right);
144  friend bool operator != <Base>
145  (const AD<Base> &left, const AD<Base> &right);
146 
147  // input operator
148  friend std::istream& operator >> <Base>
149  (std::istream &is, AD<Base> &x);
150 
151  // output operations
152  friend std::ostream& operator << <Base>
153  (std::ostream &os, const AD<Base> &x);
154  friend void PrintFor <Base> (
155  const AD<Base>& flag ,
156  const char* before ,
157  const AD<Base>& var ,
158  const char* after
159  );
160 public:
161  // type of value
162  typedef Base value_type;
163 
164  // implicit default constructor
165  inline AD(void);
166 
167  // destructor
168  ~AD(void) { }
169 
170  // use default implicit copy constructor
171  // inline AD(const AD &x);
172 
173 # ifdef CPPAD_FOR_TMB
174  // TMB would rather have implicit construction from double,
175  // CppAD uses default constructor and assignment to double instead.
176  inline AD(const double &d);
177 # else
178  // implicit construction from base type
179  inline AD(const Base &b);
180 # endif
181 
182  // implicit contructor from VecAD<Base>::reference
183  inline AD(const VecAD_reference<Base> &x);
184 
185  // explicit construction from some other type (depricated)
186  template <class T> inline explicit AD(const T &t);
187 
188  // conversion from AD to Base type
189  friend Base Value <Base> (const AD<Base> &x);
190 
191  // use default assignment operator
192  // inline AD& operator=(const AD &x);
193 
194  // assingment from base type
195  inline AD& operator=(const Base &b);
196 
197  // assignment from VecAD<Base>::reference
198  inline AD& operator=(const VecAD_reference<Base> &x);
199 
200  // assignment from some other type
201  template <class T> inline AD& operator=(const T &right);
202 
203  // compound assignment operators
204  inline AD& operator += (const AD &right);
205  inline AD& operator -= (const AD &right);
206  inline AD& operator *= (const AD &right);
207  inline AD& operator /= (const AD &right);
208 
209  // unary operators
210  inline AD operator +(void) const;
211  inline AD operator -(void) const;
212 
213  // interface so these functions need not be friends
214  inline AD abs_me(void) const;
215  inline AD acos_me(void) const;
216  inline AD asin_me(void) const;
217  inline AD atan_me(void) const;
218  inline AD cos_me(void) const;
219  inline AD cosh_me(void) const;
220  inline AD exp_me(void) const;
221  inline AD fabs_me(void) const;
222  inline AD log_me(void) const;
223  inline AD sin_me(void) const;
224  inline AD sign_me(void) const;
225  inline AD sinh_me(void) const;
226  inline AD sqrt_me(void) const;
227  inline AD tan_me(void) const;
228  inline AD tanh_me(void) const;
229 # if CPPAD_USE_CPLUSPLUS_2011
230  inline AD erf_me(void) const;
231  inline AD asinh_me(void) const;
232  inline AD acosh_me(void) const;
233  inline AD atanh_me(void) const;
234  inline AD expm1_me(void) const;
235  inline AD log1p_me(void) const;
236 # endif
237 
238  // ----------------------------------------------------------
239  // static public member functions
240 
241  // abort current AD<Base> recording
242  static void abort_recording(void);
243 
244  // set the maximum number of OpenMP threads (deprecated)
245  static void omp_max_thread(size_t number);
246 
247  // These functions declared public so can be accessed by user through
248  // a macro interface and are not intended for direct use.
249  // The macro interface is documented in bool_fun.hpp.
250  // Developer documentation for these fucntions is in bool_fun.hpp
251  static inline bool UnaryBool(
252  bool FunName(const Base &x),
253  const AD<Base> &x
254  );
255  static inline bool BinaryBool(
256  bool FunName(const Base &x, const Base &y),
257  const AD<Base> &x , const AD<Base> &y
258  );
259 
260 private:
261  //
262  // Make this variable a parameter
263  //
264  void make_parameter(void)
265  { CPPAD_ASSERT_UNKNOWN( Variable(*this) ); // currently a var
266  tape_id_ = 0;
267  }
268  //
269  // Make this parameter a new variable
270  //
272  { CPPAD_ASSERT_UNKNOWN( Parameter(*this) ); // currently a par
273  CPPAD_ASSERT_UNKNOWN( taddr > 0 ); // sure valid taddr
274 
275  taddr_ = taddr;
276  tape_id_ = id;
277  }
278  // ---------------------------------------------------------------
279  // tape linking functions
280  //
281  // not static
282  inline local::ADTape<Base>* tape_this(void) const;
283  //
284  // static
285  inline static tape_id_t** tape_id_handle(size_t thread);
286  inline static tape_id_t* tape_id_ptr(size_t thread);
287  inline static local::ADTape<Base>** tape_handle(size_t thread);
289  inline static local::ADTape<Base>* tape_ptr(void);
290  inline static local::ADTape<Base>* tape_ptr(tape_id_t tape_id);
291 };
292 // ---------------------------------------------------------------------------
293 
294 } // END_CPPAD_NAMESPACE
295 
296 // tape linking private functions
297 # include <cppad/core/tape_link.hpp>
298 
299 // operations that expect the AD template class to be defined
300 
301 
302 # endif
~AD(void)
Definition: ad.hpp:168
AD asinh_me(void) const
AD sinh_me(void) const
AD & operator-=(const AD &right)
Definition: sub_eq.hpp:20
AD operator-(void) const
Definition: unary_minus.hpp:84
friend bool Parameter(const AD< Base > &u)
Definition: par_var.hpp:80
static tape_id_t * tape_id_ptr(size_t thread)
Pointer to the tape identifier for this AD&lt;Base&gt; class and the specific thread.
Definition: tape_link.hpp:81
Class used to hold function objects.
Definition: ad_fun.hpp:69
Base value_
Definition: ad.hpp:38
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
AD tanh_me(void) const
AD sin_me(void) const
Definition: ad.hpp:34
AD & operator*=(const AD &right)
Definition: mul_eq.hpp:20
AD acosh_me(void) const
static bool UnaryBool(bool FunName(const Base &x), const AD< Base > &x)
Link a function name, and AD value pair to function call with base argument and bool retrun value...
Definition: bool_fun.hpp:189
AD sign_me(void) const
Definition: sign.hpp:71
AD acos_me(void) const
AD fabs_me(void) const
friend bool Variable(const AD< Base > &u)
Definition: par_var.hpp:99
Defines the OpCode enum type and functions related to it.
friend void Independent(VectorAD &x, size_t abort_op_index)
Declaration of independent variables.
friend void parallel_ad(void)
Enable parallel execution mode with AD&lt;Base&gt; by initializing static variables that my be used...
Definition: parallel_ad.hpp:79
AD tan_me(void) const
File used to define the recorder class.
AD & operator/=(const AD &right)
Definition: div_eq.hpp:20
static local::ADTape< Base > * tape_manage(tape_manage_job job)
Create and delete tapes that record AD&lt;Base&gt; operations for current thread.
Definition: tape_link.hpp:210
AD & operator=(const Base &b)
Assignment to Base type value.
Definition: ad_assign.hpp:99
AD sqrt_me(void) const
static local::ADTape< Base > ** tape_handle(size_t thread)
Handle for the tape for this AD&lt;Base&gt; class and the specific thread.
Definition: tape_link.hpp:105
AD abs_me(void) const
Definition: abs.hpp:82
Check and AD values ordering properties relative to zero.
AD(void)
Default Constructor.
Definition: ad_ctor.hpp:107
AD erf_me(void) const
void make_parameter(void)
Definition: ad.hpp:264
AD cos_me(void) const
AD asin_me(void) const
static local::ADTape< Base > * tape_ptr(void)
Pointer for the tape for this AD&lt;Base&gt; class and the current thread.
Definition: tape_link.hpp:130
AD cosh_me(void) const
AD exp_me(void) const
AD & operator+=(const AD &right)
Definition: add_eq.hpp:20
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
Check if certain properties is true for any possible AD tape play back.
File used to define the player class.
static void abort_recording(void)
tape_id_t tape_id_
Definition: ad.hpp:41
static tape_id_t ** tape_id_handle(size_t thread)
Handle to the tape identifier for this AD&lt;Base&gt; class and the specific thread.
Definition: tape_link.hpp:48
Class used to hold a reference to an element of a VecAD object.
Definition: vec_ad.hpp:352
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
Vector of AD objects that tracks indexing operations on the tape.
Definition: vec_ad.hpp:462
AD log_me(void) const
AD operator+(void) const
Definition: unary_plus.hpp:83
tape_manage_job
Definition: ad.hpp:27
addr_t taddr_
Definition: ad.hpp:44
void make_variable(tape_id_t id, addr_t taddr)
Definition: ad.hpp:271
AD atanh_me(void) const
AD atan_me(void) const
Base value_type
Definition: ad.hpp:162
AD expm1_me(void) const
local::ADTape< Base > * tape_this(void) const
Get a pointer to tape that records AD&lt;Base&gt; operations for the current thread.
Definition: tape_link.hpp:335
static bool BinaryBool(bool FunName(const Base &x, const Base &y), const AD< Base > &x, const AD< Base > &y)
Link a function name, and two AD values to function call with base arguments and bool retrun value...
Definition: bool_fun.hpp:234
AD log1p_me(void) const
static void omp_max_thread(size_t number)
CPPAD_TAPE_ID_TYPE tape_id_t
Definition: declare_ad.hpp:45