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