-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyAD.hpp
423 lines (398 loc) · 11.5 KB
/
easyAD.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#ifndef EASYAD_HPP
#define EASYAD_HPP
#ifdef __cplusplus
#include <cmath>
#include <ostream>
#include <istream>
#include <type_traits>
struct Forward {
double val;
double dot;
constexpr Forward(double _val, double _dot): val(_val), dot(_dot) {}
constexpr Forward(double _val): val(_val), dot(0.0) {}
constexpr Forward(): val(0.), dot(0.) {}; // non-trivial default constructor, thus Forward cannot be used in unions
template<typename T>
Forward& operator=(T const& other){
return *this = (Forward)other;
}
Forward& operator= (Forward const&) = default;
constexpr Forward(Forward const& forw) = default; // trivial copy constructor so Forward is trivially copyable
template<typename T>
explicit constexpr operator T() const {
return val;
}
};
using Gorward = Forward;
#include "traits.hpp"
// basic arithmetic with two operands
constexpr inline Forward operator +(Forward a, Forward b){
return {a.val+b.val, a.dot+b.dot};
}
constexpr inline Forward operator -(Forward a, Forward b){
return {a.val-b.val, a.dot-b.dot};
}
constexpr inline Forward operator *(Forward a, Forward b){
return {a.val*b.val, a.dot*b.val+a.val*b.dot};
}
constexpr inline Forward operator /(Forward a, Forward b){
return {a.val/b.val, a.dot/b.val-a.val*b.dot/(b.val*b.val)};
}
// overloads with one non-active operand
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator +(Forward a, T b){
return a + Forward(b);
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator +(T a, Forward b){
return Forward(a) + b;
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator -(Forward a, T b){
return a - Forward(b);
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator -(T a, Forward b){
return Forward(a) - b;
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator *(Forward a, T b){
return a * Forward(b);
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator *(T a, Forward b){
return Forward(a) * b;
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator /(Forward a, T b){
return a / Forward(b);
}
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value,bool>::type = true>
constexpr Forward operator /(T a, Forward b){
return Forward(a) / b;
}
// increment/decrement operators
inline Forward& operator++(Forward& a){
++a.val;
return a;
}
inline Forward& operator--(Forward& a){
++a.val;
return a;
}
inline Forward operator++(Forward& a, int){
return a.val++;
}
inline Forward operator--(Forward& a, int){
return a.val++;
}
// arithmetic assignment operators
template<typename T>
Forward& operator +=(Forward& a, T const& b){
a = a + b;
return a;
}
template<typename T>
Forward& operator -=(Forward& a, T const& b){
a = a - b;
return a;
}
template<typename T>
Forward& operator *=(Forward& a, T const& b){
a = a * b;
return a;
}
template<typename T>
Forward& operator /=(Forward& a, T const& b){
a = a / b;
return a;
}
// basic arithmetic with one operand
constexpr inline Forward operator -(Forward a){
return {-a.val,-a.dot};
}
constexpr inline Forward operator +(Forward a){
return a;
}
// comparisons
constexpr inline bool operator==(Forward const& a, Forward const& b){
return a.val==b.val;
}
constexpr inline bool operator!=(Forward const& a, Forward const& b){
return a.val!=b.val;
}
constexpr inline bool operator>(Forward const& a, Forward const& b){
return a.val>b.val;
}
constexpr inline bool operator<(Forward const& a, Forward const& b){
return a.val<b.val;
}
constexpr inline bool operator>=(Forward const& a, Forward const& b){
return a.val>=b.val;
}
constexpr inline bool operator<=(Forward const& a, Forward const& b){
return a.val<=b.val;
}
// stream I/O
inline std::ostream& operator<<(std::ostream& out, Forward a){
return out << a.val;
}
inline std::istream& operator>>(std::istream& in, Forward& a){
a.dot = 0.0;
return in >> a.val;
}
// math.h functions
namespace std {
inline Forward max(Forward a, Forward b){
if(a>b){
return a;
} else {
return b;
}
}
inline Forward min(Forward a, Forward b){
if(a<b){
return a;
} else {
return b;
}
}
constexpr inline Forward abs(Forward a){
return {abs(a.val), a.val >= 0. ? a.dot : -a.dot};
}
constexpr inline Forward acos(Forward a){
return {acos(a.val), -1./sqrt(1-a.val*a.val) * a.dot};
}
constexpr inline Forward acosh(Forward a){
return {acosh(a.val), 1./sqrt(a.val*a.val-1) * a.dot};
}
constexpr inline Forward asin(Forward a){
return {asin(a.val), 1./sqrt(1-a.val*a.val) * a.dot};
}
constexpr inline Forward asinh(Forward a){
return {asinh(a.val), 1./sqrt(a.val*a.val+1) * a.dot};
}
constexpr inline Forward atan(Forward a){
return {atan(a.val), 1./(1+a.val*a.val) * a.dot};
}
constexpr inline Forward cos(Forward a){
return {cos(a.val), -sin(a.val) * a.dot};
}
constexpr inline Forward cosh(Forward a){
return {cosh(a.val), sinh(a.val) * a.dot};
}
constexpr inline Forward copysign(Forward a, Forward b){
return {copysign(a.val,b.val), copysign(1.0, a.val*b.val) * a.dot};
}
constexpr inline Forward erf(Forward a){
return {erf(a.val), 2.0/sqrt(M_PI) * exp(-a.val*a.val) * a.dot};
}
constexpr inline Forward erfc(Forward a){
return {erfc(a.val), -2.0/sqrt(M_PI) * exp(-a.val*a.val) * a.dot};
}
constexpr inline Forward exp(Forward a){
return {exp(a.val), exp(a.val) * a.dot};
}
constexpr inline Forward exp2(Forward a){
return {exp2(a.val), exp2(a.val)*log(2.0) * a.dot};
}
constexpr inline Forward expm1(Forward a){
return {expm1(a.val), exp(a.val) * a.dot};
}
constexpr inline Forward fabs(Forward a){
return {fabs(a.val), (a.val>0?1.:-1.) * a.dot};
}
constexpr inline Forward floor(Forward a){
return {floor(a.val), 0.0};
}
constexpr inline Forward ceil(Forward a){
return {ceil(a.val), 0.0};
}
constexpr inline Forward trunc(Forward a){
return {trunc(a.val), 0.0};
}
constexpr inline Forward round(Forward a){
return {round(a.val), 0.0};
}
constexpr inline Forward nearbyint(Forward a){
return {nearbyint(a.val), 0.0};
}
constexpr inline Forward rint(Forward a){
return {rint(a.val), 0.0};
}
constexpr inline long lrint(Forward a){
return lrint(a.val);
}
constexpr inline long long llrint(Forward a){
return llrint(a.val);
}
constexpr inline Forward fmod(Forward a, Forward b){
return {fmod(a.val, b.val), 1.0 * a.dot - trunc(a.val/b.val) * b.dot};
}
inline Forward modf(Forward a, Forward* b){
b->dot = 0.;
return {modf(a.val, &b->val), a.dot};
}
constexpr inline Forward log(Forward a){
return {log(a.val), 1/a.val * a.dot};
}
constexpr inline Forward log2(Forward a){
return {log2(a.val), 1/(a.val*log(2.0)) * a.dot};
}
constexpr inline Forward log10(Forward a){
return {log10(a.val), 1/(a.val*log(10.0)) * a.dot};
}
constexpr inline Forward log1p(Forward a){
return {log1p(a.val), 1/(a.val+1.0) * a.dot};
}
constexpr inline Forward sin(Forward a){
return {sin(a.val), cos(a.val) * a.dot};
}
constexpr inline Forward sinh(Forward a){
return {sinh(a.val), cosh(a.val) * a.dot};
}
inline Forward sqrt(Forward a){
if(a.val==0) return {0.,0.};
return {sqrt(a.val), 0.5/sqrt(a.val) * a.dot};
}
constexpr inline Forward cbrt(Forward a){
return {cbrt(a.val), cbrt(a.val)/(3.0*a.val) * a.dot};
}
constexpr inline Forward hypot(Forward a, Forward b){
return {hypot(a.val,b.val), a.val/hypot(a.val,b.val) * a.dot + b.val/hypot(a.val,b.val) * b.dot};
}
constexpr inline Forward tan(Forward a){
return {tan(a.val), 1/(cos(a.val)*cos(a.val)) * a.dot};
}
constexpr inline Forward tanh(Forward a){
return {tanh(a.val), (1-tanh(a.val)*tanh(a.val)) * a.dot};
}
constexpr inline Forward atan2(Forward a, Forward b){
return {atan2(a.val,b.val), -b.val/(a.val*a.val + b.val*b.val) * a.dot + a.val/(a.val*a.val + b.val*b.val) * b.dot};
}
inline Forward pow(Forward a, Forward b){
double da = 0., db = 0.;
if(b.val!=0.0 && b.val!=-0.0){
da = b.val*pow(a.val,b.val-1.0);
}
if(a.val>=0.){
db = pow(a.val,b.val)*log(a.val);
}
return {pow(a.val,b.val), da * a.dot + db * b.dot};
}
inline Forward pow(Forward a, double b){
return pow(a, Forward(b));
}
inline Forward pow(double a, Forward b){
return pow(Forward(a), b);
}
inline Forward tgamma(Forward a){
double derivative = (tgamma(a.val*1.01)-tgamma(a.val*0.99))/(a.val*0.02); // TODO
return {tgamma(a.val), derivative * a.dot};
}
inline Forward lgamma(Forward a){
double derivative = (lgamma(a.val*1.01)-lgamma(a.val*0.99))/(a.val*0.02); // TODO
return {lgamma(a.val), derivative * a.dot};
}
inline bool isfinite(Forward a){
return isfinite(a.val);
}
inline bool isinf(Forward a){
return isinf(a.val);
}
inline bool isnan(Forward a){
return isnan(a.val);
}
}
using std::abs;
using std::acos;
using std::acosh;
using std::asin;
using std::asinh;
using std::atan;
using std::cos;
using std::cosh;
using std::copysign;
using std::erf;
using std::erfc;
using std::exp;
using std::exp2;
using std::expm1;
using std::fabs;
using std::floor;
using std::ceil;
using std::trunc;
using std::round;
using std::nearbyint;
using std::rint;
using std::lrint;
using std::llrint;
using std::fmod;
using std::modf;
using std::log;
using std::log2;
using std::log10;
using std::log1p;
using std::sin;
using std::sinh;
using std::sqrt;
using std::cbrt;
using std::hypot;
using std::tan;
using std::tanh;
using std::atan2;
using std::pow;
using std::lgamma;
using std::tgamma;
using std::isfinite;
using std::isinf;
using std::isnan;
#include <limits>
template<typename From, typename To>
struct forward_numeric_limits : std::numeric_limits<From> {
using Base = std::numeric_limits<From>;
using Base::is_specialized;
using Base::is_signed;
using Base::is_integer;
using Base::is_exact;
using Base::has_infinity;
using Base::has_quiet_NaN;
using Base::has_signaling_NaN;
using Base::has_denorm;
using Base::has_denorm_loss;
using Base::round_style;
using Base::is_iec559;
using Base::is_bounded;
using Base::is_modulo;
using Base::digits;
using Base::digits10;
using Base::radix;
using Base::min_exponent;
using Base::min_exponent10;
using Base::max_exponent;
using Base::max_exponent10;
using Base::traps;
using Base::tinyness_before;
static constexpr To min() { return Base::min(); }
static constexpr To lowest() { return Base::lowest(); }
static constexpr To max() { return Base::max(); }
static constexpr To epsilon() { return Base::epsilon(); }
static constexpr To round_error() { return Base::round_error(); }
static constexpr To infinity() { return Base::infinity(); }
static constexpr To quiet_NaN() { return Base::quiet_NaN(); }
static constexpr To signaling_NaN() { return Base::signaling_NaN(); }
static constexpr To denorm_min() { return Base::denorm_min(); }
};
template<> struct std::numeric_limits<Forward> : forward_numeric_limits<double,Forward> {};
template<> struct std::numeric_limits<Forward const> : forward_numeric_limits<double,Forward> {};
template<> struct std::numeric_limits<Forward volatile> : forward_numeric_limits<double,Forward> {};
template<> struct std::numeric_limits<Forward const volatile> : forward_numeric_limits<double,Forward> {};
#include <string>
namespace std {
inline std::string to_string(Forward a){
return to_string(a.val);
}
}
#else
#define ForwardIfPossible(x) (x)
#endif // __cplusplus
#endif // EASYAD_HPP