CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
define.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_DEFINE_HPP
2 # define CPPAD_CORE_DEFINE_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 /*!
16 \file define.hpp
17 Define processor symbols and macros that are used by CppAD.
18 */
19 
20 // ----------------------------------------------------------------------------
21 /*!
22 \def CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION
23 A version of the inline command that works with MC compiler.
24 
25 Microsoft Visual C++ version 9.0 generates a warning if a template
26 function is declared as a friend
27 (this was not a problem for version 7.0).
28 The warning identifier is
29 \verbatim
30  warning C4396
31 \endverbatim
32 and it contains the text
33 \verbatim
34  the inline specifier cannot be used when a friend declaration refers
35  to a specialization of a function template
36 \endverbatim
37 This happens even if the function is not a specialization.
38 This macro is defined as empty for Microsoft compilers.
39 */
40 # ifdef _MSC_VER
41 # define CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION
42 # else
43 # define CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION inline
44 # endif
45 
46 // ----------------------------------------------------------------------------
47 /*!
48 \def CPPAD_LIB_EXPORT
49 Special macro for exporting windows DLL symbols; see
50 https://cmake.org/Wiki/BuildingWinDLL
51 */
52 # ifdef _MSC_VER
53 # ifdef cppad_lib_EXPORTS
54 # define CPPAD_LIB_EXPORT __declspec(dllexport)
55 # else
56 # define CPPAD_LIB_EXPORT __declspec(dllimport)
57 # endif // cppad_lib_EXPORTS
58 # else // _MSC_VER
59 # define CPPAD_LIB_EXPORT
60 # endif
61 
62 
63 // ============================================================================
64 /*!
65 \def CPPAD_FOLD_ASSIGNMENT_OPERATOR(Op)
66 Declares automatic coercion for certain AD assignment operations.
67 
68 This macro assumes that the operator
69 \verbatim
70  left Op right
71 \endverbatim
72 is defined for the case where left and right have type AD<Base>.
73 It uses this case to define the cases where
74 left has type AD<Base> and right has type
75 VecAD_reference<Base>,
76 Base, or
77 double.
78 The argument right is const and call by reference.
79 This macro converts the operands to AD<Base> and then
80 uses the definition of the same operation for that case.
81 */
82 
83 # define CPPAD_FOLD_ASSIGNMENT_OPERATOR(Op) \
84 /* ----------------------------------------------------------------*/ \
85 template <class Base> \
86 inline AD<Base>& operator Op \
87 (AD<Base> &left, double right) \
88 { return left Op AD<Base>(right); } \
89  \
90 template <class Base> \
91 inline AD<Base>& operator Op \
92 (AD<Base> &left, const Base &right) \
93 { return left Op AD<Base>(right); } \
94  \
95 inline AD<double>& operator Op \
96 (AD<double> &left, const double &right) \
97 { return left Op AD<double>(right); } \
98  \
99 template <class Base> \
100 inline AD<Base>& operator Op \
101 (AD<Base> &left, const VecAD_reference<Base> &right) \
102 { return left Op right.ADBase(); }
103 
104 // =====================================================================
105 /*!
106 \def CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(Op)
107 Declares automatic coercion for certain binary operations with AD result.
108 
109 This macro assumes that the operator
110 \verbatim
111  left Op right
112 \endverbatim
113 is defined for the case where left and right
114 and the result of the operation all
115 have type AD<Base>.
116 It uses this case to define the cases either left
117 or right has type VecAD_reference<Base> or AD<Base>
118 and the type of the other operand is one of the following:
119 VecAD_reference<Base>, AD<Base>, Base, double.
120 All of the arguments are const and call by reference.
121 This macro converts the operands to AD<Base> and then
122 uses the definition of the same operation for that case.
123 */
124 # define CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(Op) \
125 /* ----------------------------------------------------------------*/ \
126 /* Operations with VecAD_reference<Base> and AD<Base> only*/ \
127  \
128 template <class Base> \
129 inline AD<Base> operator Op \
130 (const AD<Base> &left, const VecAD_reference<Base> &right) \
131 { return left Op right.ADBase(); } \
132  \
133 template <class Base> \
134 inline AD<Base> operator Op \
135 (const VecAD_reference<Base> &left, const VecAD_reference<Base> &right)\
136 { return left.ADBase() Op right.ADBase(); } \
137  \
138 template <class Base> \
139 inline AD<Base> operator Op \
140  (const VecAD_reference<Base> &left, const AD<Base> &right) \
141 { return left.ADBase() Op right; } \
142 /* ----------------------------------------------------------------*/ \
143 /* Operations Base */ \
144  \
145 template <class Base> \
146 inline AD<Base> operator Op \
147  (const Base &left, const AD<Base> &right) \
148 { return AD<Base>(left) Op right; } \
149  \
150 template <class Base> \
151 inline AD<Base> operator Op \
152  (const Base &left, const VecAD_reference<Base> &right) \
153 { return AD<Base>(left) Op right.ADBase(); } \
154  \
155 template <class Base> \
156 inline AD<Base> operator Op \
157  (const AD<Base> &left, const Base &right) \
158 { return left Op AD<Base>(right); } \
159  \
160 template <class Base> \
161 inline AD<Base> operator Op \
162  (const VecAD_reference<Base> &left, const Base &right) \
163 { return left.ADBase() Op AD<Base>(right); } \
164  \
165 /* ----------------------------------------------------------------*/ \
166 /* Operations double */ \
167  \
168 template <class Base> \
169 inline AD<Base> operator Op \
170  (const double &left, const AD<Base> &right) \
171 { return AD<Base>(left) Op right; } \
172  \
173 template <class Base> \
174 inline AD<Base> operator Op \
175  (const double &left, const VecAD_reference<Base> &right) \
176 { return AD<Base>(left) Op right.ADBase(); } \
177  \
178 template <class Base> \
179 inline AD<Base> operator Op \
180  (const AD<Base> &left, const double &right) \
181 { return left Op AD<Base>(right); } \
182  \
183 template <class Base> \
184 inline AD<Base> operator Op \
185  (const VecAD_reference<Base> &left, const double &right) \
186 { return left.ADBase() Op AD<Base>(right); } \
187 /* ----------------------------------------------------------------*/ \
188 /* Special case to avoid ambuigity when Base is double */ \
189  \
190 inline AD<double> operator Op \
191  (const double &left, const AD<double> &right) \
192 { return AD<double>(left) Op right; } \
193  \
194 inline AD<double> operator Op \
195  (const double &left, const VecAD_reference<double> &right) \
196 { return AD<double>(left) Op right.ADBase(); } \
197  \
198 inline AD<double> operator Op \
199  (const AD<double> &left, const double &right) \
200 { return left Op AD<double>(right); } \
201  \
202 inline AD<double> operator Op \
203  (const VecAD_reference<double> &left, const double &right) \
204 { return left.ADBase() Op AD<double>(right); }
205 
206 // =======================================================================
207 
208 /*!
209 \def CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(Op)
210 Declares automatic coercion for certain binary operations with bool result.
211 
212 This macro assumes that the operator
213 \verbatim
214  left Op right
215 \endverbatim
216 is defined for the case where left and right
217 have type AD<Base> and the result has type bool.
218 It uses this case to define the cases either left
219 or right has type
220 VecAD_reference<Base> or AD<Base>
221 and the type of the other operand is one of the following:
222 VecAD_reference<Base>, AD<Base>, Base, double.
223 All of the arguments are const and call by reference.
224 This macro converts the operands to AD<Base> and then
225 uses the definition of the same operation for that case.
226 */
227 # define CPPAD_FOLD_BOOL_VALUED_BINARY_OPERATOR(Op) \
228 /* ----------------------------------------------------------------*/ \
229 /* Operations with VecAD_reference<Base> and AD<Base> only*/ \
230  \
231 template <class Base> \
232 inline bool operator Op \
233 (const AD<Base> &left, const VecAD_reference<Base> &right) \
234 { return left Op right.ADBase(); } \
235  \
236 template <class Base> \
237 inline bool operator Op \
238 (const VecAD_reference<Base> &left, const VecAD_reference<Base> &right)\
239 { return left.ADBase() Op right.ADBase(); } \
240  \
241 template <class Base> \
242 inline bool operator Op \
243  (const VecAD_reference<Base> &left, const AD<Base> &right) \
244 { return left.ADBase() Op right; } \
245 /* ----------------------------------------------------------------*/ \
246 /* Operations Base */ \
247  \
248 template <class Base> \
249 inline bool operator Op \
250  (const Base &left, const AD<Base> &right) \
251 { return AD<Base>(left) Op right; } \
252  \
253 template <class Base> \
254 inline bool operator Op \
255  (const Base &left, const VecAD_reference<Base> &right) \
256 { return AD<Base>(left) Op right.ADBase(); } \
257  \
258 template <class Base> \
259 inline bool operator Op \
260  (const AD<Base> &left, const Base &right) \
261 { return left Op AD<Base>(right); } \
262  \
263 template <class Base> \
264 inline bool operator Op \
265  (const VecAD_reference<Base> &left, const Base &right) \
266 { return left.ADBase() Op AD<Base>(right); } \
267  \
268 /* ----------------------------------------------------------------*/ \
269 /* Operations double */ \
270  \
271 template <class Base> \
272 inline bool operator Op \
273  (const double &left, const AD<Base> &right) \
274 { return AD<Base>(left) Op right; } \
275  \
276 template <class Base> \
277 inline bool operator Op \
278  (const double &left, const VecAD_reference<Base> &right) \
279 { return AD<Base>(left) Op right.ADBase(); } \
280  \
281 template <class Base> \
282 inline bool operator Op \
283  (const AD<Base> &left, const double &right) \
284 { return left Op AD<Base>(right); } \
285  \
286 template <class Base> \
287 inline bool operator Op \
288  (const VecAD_reference<Base> &left, const double &right) \
289 { return left.ADBase() Op AD<Base>(right); } \
290 /* ----------------------------------------------------------------*/ \
291 /* Special case to avoid ambuigity when Base is double */ \
292  \
293 inline bool operator Op \
294  (const double &left, const AD<double> &right) \
295 { return AD<double>(left) Op right; } \
296  \
297 inline bool operator Op \
298  (const double &left, const VecAD_reference<double> &right) \
299 { return AD<double>(left) Op right.ADBase(); } \
300  \
301 inline bool operator Op \
302  (const AD<double> &left, const double &right) \
303 { return left Op AD<double>(right); } \
304  \
305 inline bool operator Op \
306  (const VecAD_reference<double> &left, const double &right) \
307 { return left.ADBase() Op AD<double>(right); }
308 
309 # endif