-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodual.h
More file actions
283 lines (244 loc) · 11.6 KB
/
Codual.h
File metadata and controls
283 lines (244 loc) · 11.6 KB
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
#ifndef CODUAL_CODUAL_H
#define CODUAL_CODUAL_H
#include <cmath>
#include <functional>
namespace dual {
template<typename F>
struct Codual {
static_assert(std::is_arithmetic_v<F>, "k");
F x;
std::function<void(const F&)> derivative;
Codual() {
x = F(0);
derivative = [](const F &dx) {};
}
explicit Codual(F a) {
x = a;
derivative = [](const F &dx) -> void {};
}
Codual(F a, std::function<void(const F&)> b) {
x = a;
derivative = b;
}
//compound operators
inline __attribute__((always_inline)) Codual<F> operator+=(const Codual<F> &b) {
x += b.x;
std::function<void(const F&)> old_derivative = this->derivative;
derivative = [b, old_derivative](const F &dx) -> void {
old_derivative(dx);
b.derivative(dx);
};
return *this;
}
inline __attribute__((always_inline)) Codual<F> operator-=(const Codual<F> &b) {
x -= b.x;
std::function<void(const F&)> old_derivative = this->derivative;
derivative = [b, old_derivative](const F &dx) -> void {
old_derivative(dx);
b.derivative(-dx);
};
return *this;
}
inline __attribute__((always_inline)) Codual<F> operator*=(const Codual<F> &b) {
std::function<void(const F&)> old_derivative = this->derivative;
derivative = [this, b, old_derivative](const F &dx) -> void {
old_derivative(b.x * dx);
b.derivative(this->x * dx);
};
x *= b.x;
return *this;
}
inline __attribute__((always_inline)) Codual<F> operator/=(const Codual<F> &b) {
const F inv = F(1.0) / b.x;
x *= inv;
std::function<void(const F&)> old_derivative = this->derivative;
derivative = [this, b, inv, old_derivative](const F &dx) -> void {
old_derivative(dx * inv);
b.derivative(-(this->x) * dx * inv);
};
return *this;
}
inline __attribute__((always_inline)) Codual<F> &operator=(const F &b) {
x = b;
derivative = [](F &dx) -> void {};
}
};
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool isless(const Codual<F> &a, const Codual<F> &b) {
return std::isless(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool isgreater(const Codual<F> &a, const Codual<F> &b) {
return std::isgreater(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool islessequal(const Codual<F> &a, const Codual<F> &b) {
return std::islessequal(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool islessgreater(const Codual<F> &a, const Codual<F> &b) {
return std::islessgreater(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool isgreaterequal(const Codual<F> &a, const Codual<F> &b) {
return std::isgreaterequal(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool isunordered(const Codual<F> &a, const Codual<F> &b) {
return std::isunordered(a.x, b.x);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator<(const Codual<F> &a, const Codual<F> &b) {
return isless(a, b);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator>(const Codual<F> &a, const Codual<F> &b) {
return isgreater(a, b);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator<=(const Codual<F> &a, const Codual<F> &b) {
return islessequal(a, b);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator>=(const Codual<F> &a, const Codual<F> &b) {
return isgreaterequal(a, b);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator==(const Codual<F> &a, const Codual<F> &b) {
return (a.x == b.x && a.derivative == b.derivative);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) bool operator!=(const Codual<F> &a, const Codual<F> &b) {
return !(a.x == b.x && a.derivative == b.derivative);
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator+(const Codual<F> &a) {
return a;
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator-(const Codual<F> &a) {
return Codual<F>(-a.x, [a](const F &dx) -> void { a.derivative(-dx); });
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator+(const Codual<F> &a, const Codual<F> &b) {
return Codual<F>(a.x + b.x, [a, b](const F &dx) -> void {
a.derivative(dx);
b.derivative(dx);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator-(const Codual<F> &a, const Codual<F> &b) {
return Codual<F>(a.x - b.x, [a, b](const F &dx) -> void {
a.derivative(dx);
b.derivative(-dx);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator-(const Codual<F> &a, const F &b) {
return Codual<F>(a.x - b, [a](const F &dx) -> void {
a.derivative(dx);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator-(const F &a, const Codual<F> &b) {
return Codual<F>(a - b.x, [b](const F &dx) -> void {
b.derivative(-dx);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator*(const Codual<F> &a, const Codual<F> &b) {
return Codual<F>(a.x * b.x, [a, b](const F &dx) -> void {
a.derivative(dx * b.x);
b.derivative(dx * a.x);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator*(const Codual<F> &a, const F &b) {
return Codual<F>(a.x * b, [a, b](const F &dx) -> void {
a.derivative(dx * b);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator*(const F &a, const Codual<F> &b) {
return Codual<F>(a * b.x, [a, b](const F &dx) -> void {
b.derivative(dx * a);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> operator/(const Codual<F> &a, const Codual<F> &b) {
const F inv = F(1.0) / b.x;
return Codual<F>(a.x * inv, [a, b, inv](const F &dx) -> void {
const F dx_by_inv = dx * inv;
a.derivative(dx_by_inv);
b.derivative(-a.x * dx_by_inv);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> abs(const Codual<F> &a) {
return Codual<F>(std::abs(a.x), [a](const F &dx) -> void {
a.derivative(dx * copysign(F(1.0), a.x));
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> log(const Codual<F> &a) {
return Codual<F>(std::log(a.x), [a](const F &dx) -> void {
a.derivative(dx / a.x);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> pow(const Codual<F> &a, const Codual<F> &b) {
if (a.x == 0 && b.x >= 1) {
if (b.x > 1) {
return Codual<F>();
}
return a;
}
const F a_to_b = std::pow(a.x, b.x), bx_by_a_to_bm1 = b.x * a_to_b / a.x;
if (a.x < 0 && b.x == std::floor(b.x)) {
Codual<F> result(a_to_b, [a, b, bx_by_a_to_bm1, a_to_b](const F &dx) -> void {
a.derivative(dx * bx_by_a_to_bm1);
});
if (b.y != [](F &dx) {}) {
result.derivative = [a]() { a.derivative(F(std::numeric_limits<F>::quiet_NaN())); };
}
return result;
}
return Codual<F>(a_to_b, [a, b, bx_by_a_to_bm1, a_to_b](const F &dx) -> void {
a.derivative(dx * bx_by_a_to_bm1);
b.derivative(dx * a_to_b * std::log(a.x));
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> pow(const Codual<F> &a, const F &b) {
if (a.x == 0 && b >= 1) {
if (b > 1) {
return Codual<F>();
}
return a;
}
const F a_to_b = std::pow(a.x, b), bx_by_a_to_bm1 = b * a_to_b / a.x;
return Codual<F>(a_to_b, [a, bx_by_a_to_bm1](const F &dx) -> void {
a.derivative(dx * bx_by_a_to_bm1);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> exp(const Codual<F> &a) {
const F expx = std::exp(a.x);
return Codual<F>(expx, [a, expx](const F &dx) -> void {
a.derivative(dx * expx);
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> sin(const Codual<F> &a) {
return Codual<F>(std::sin(a.x), [a](const F &dx) -> void {
a.derivative(dx * std::cos(a.x));
});
}
template<typename F, typename std::enable_if<std::is_arithmetic_v<F>, bool>::type = true>
inline __attribute__((always_inline)) Codual<F> cos(const Codual<F> &a) {
return Codual<F>(std::cos(a.x), [a](const F &dx) -> void {
a.derivative(-dx * std::sin(a.x));
});
}
}
#endif //CODUAL_CODUAL_H