-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathcpp2taylor.h
More file actions
521 lines (412 loc) · 15.4 KB
/
cpp2taylor.h
File metadata and controls
521 lines (412 loc) · 15.4 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#ifndef CPP2TAYLOR_H_CPP2
#define CPP2TAYLOR_H_CPP2
//=== Cpp2 type declarations ====================================================
#include "cpp2util.h"
#line 1 "cpp2taylor.h2"
#line 4 "cpp2taylor.h2"
namespace cpp2 {
template<typename R, int dim> class taylor;
#line 257 "cpp2taylor.h2"
}
//=== Cpp2 type definitions and function declarations ===========================
#line 1 "cpp2taylor.h2"
#ifndef CPP2_CPP2TAYLOR_H
#define CPP2_CPP2TAYLOR_H
#line 4 "cpp2taylor.h2"
namespace cpp2 {
template<typename R, int dim> class taylor {
public: std::array<R,dim> v {};
public: explicit taylor();
public: taylor(R const& d1);
#line 10 "cpp2taylor.h2"
public: auto operator=(R const& d1) -> taylor& ;
#line 14 "cpp2taylor.h2"
public: taylor(taylor const& that);
#line 14 "cpp2taylor.h2"
public: auto operator=(taylor const& that) -> taylor& ;
#line 14 "cpp2taylor.h2"
public: taylor(taylor&& that) noexcept;
#line 14 "cpp2taylor.h2"
public: auto operator=(taylor&& that) noexcept -> taylor& ;
public: taylor(std::initializer_list<R> const& l);
#line 16 "cpp2taylor.h2"
public: auto operator=(std::initializer_list<R> const& l) -> taylor& ;
#line 23 "cpp2taylor.h2"
// C++ interface
public: [[nodiscard]] auto operator[](cpp2::impl::in<int> k) const& -> R;
#line 35 "cpp2taylor.h2"
public: auto set(cpp2::impl::in<int> k, cpp2::impl::in<R> value) & -> void;
#line 45 "cpp2taylor.h2"
// C++2 interface / AD interface
public: [[nodiscard]] auto get(cpp2::impl::in<int> i, cpp2::impl::in<R> v0) const& -> R;
#line 58 "cpp2taylor.h2"
// Overload for reverse AD.
public: auto operator+=(cpp2::impl::in<taylor> o) & -> auto&&;
#line 64 "cpp2taylor.h2"
// Overload for reverse AD.
public: auto operator-=(cpp2::impl::in<taylor> o) & -> auto&&;
#line 70 "cpp2taylor.h2"
// Overload for simple handling of connected adds.
public: [[nodiscard]] auto operator+(cpp2::impl::in<taylor> o) const& -> taylor;
#line 75 "cpp2taylor.h2"
// Overload for simple handling of connected minuses.
public: [[nodiscard]] auto operator-(cpp2::impl::in<taylor> o) const& -> taylor;
#line 80 "cpp2taylor.h2"
// Overload for simple handling of prefix +.
public: [[nodiscard]] auto operator+() const& -> taylor;
#line 85 "cpp2taylor.h2"
// Overload for simple handling of prefix -.
public: [[nodiscard]] auto operator-() const& -> taylor;
#line 97 "cpp2taylor.h2"
public: [[nodiscard]] auto add(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor;
#line 108 "cpp2taylor.h2"
public: [[nodiscard]] auto sub(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor;
#line 119 "cpp2taylor.h2"
public: template<int dim_o> [[nodiscard]] auto mul(taylor<R,dim_o> const& o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor<R,std::max(dim, dim_o)>;
#line 133 "cpp2taylor.h2"
public: [[nodiscard]] auto div(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor;
#line 151 "cpp2taylor.h2"
public: [[nodiscard]] auto sqrt(cpp2::impl::in<R> v0) const& -> taylor;
#line 170 "cpp2taylor.h2"
public: [[nodiscard]] auto log(cpp2::impl::in<R> v0) const& -> taylor;
#line 189 "cpp2taylor.h2"
public: [[nodiscard]] auto exp(cpp2::impl::in<R> v0) const& -> taylor;
#line 208 "cpp2taylor.h2"
public: static auto comp_sin_cos(taylor& s, taylor& c, cpp2::impl::in<taylor> u, cpp2::impl::in<R> u0) -> void;
#line 225 "cpp2taylor.h2"
public: [[nodiscard]] auto sin(cpp2::impl::in<R> v0) const& -> taylor;
#line 235 "cpp2taylor.h2"
public: [[nodiscard]] auto cos(cpp2::impl::in<R> v0) const& -> taylor;
#line 244 "cpp2taylor.h2"
};
template<typename R, int order> [[nodiscard]] auto to_string(taylor<R,order> const& o) -> std::string;
#line 257 "cpp2taylor.h2"
} // cpp2 namespace
#endif // CPP2_CPP2TAYLOR_H
//=== Cpp2 function definitions =================================================
#line 1 "cpp2taylor.h2"
#line 4 "cpp2taylor.h2"
namespace cpp2 {
#line 9 "cpp2taylor.h2"
template <typename R, int dim> taylor<R,dim>::taylor(){}
#line 10 "cpp2taylor.h2"
template <typename R, int dim> taylor<R,dim>::taylor(R const& d1)
: v{ }{
#line 12 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS_LITERAL(v, 0) = d1;
}
#line 10 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator=(R const& d1) -> taylor& {
v = {};
#line 12 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS_LITERAL(v, 0) = d1;
return *this;
#line 13 "cpp2taylor.h2"
}
#line 14 "cpp2taylor.h2"
template <typename R, int dim> taylor<R,dim>::taylor(taylor const& that)
: v{ that.v }{}
#line 14 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator=(taylor const& that) -> taylor& {
v = that.v;
return *this; }
#line 14 "cpp2taylor.h2"
template <typename R, int dim> taylor<R,dim>::taylor(taylor&& that) noexcept
: v{ std::move(that).v }{}
#line 14 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator=(taylor&& that) noexcept -> taylor& {
v = std::move(that).v;
return *this; }
#line 16 "cpp2taylor.h2"
template <typename R, int dim> taylor<R,dim>::taylor(std::initializer_list<R> const& l){
{
auto i{1};
#line 18 "cpp2taylor.h2"
for ( auto const& cur : l ) {
set(i, cur);
}
}
#line 21 "cpp2taylor.h2"
}
#line 16 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator=(std::initializer_list<R> const& l) -> taylor& {
v = {};
{
auto i{1};
#line 18 "cpp2taylor.h2"
for ( auto const& cur : l ) {
set(i, cur);
}
}
return *this;
#line 21 "cpp2taylor.h2"
}
#line 25 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::operator[](cpp2::impl::in<int> k) const& -> R{
if (cpp2::cpp2_default.is_active() && !([_0 = 1, _1 = k, _2 = dim]{ return cpp2::impl::cmp_less_eq(_0,_1) && cpp2::impl::cmp_less_eq(_1,_2); }()) ) { cpp2::cpp2_default.report_violation(""); }
R r {CPP2_ASSERT_IN_BOUNDS(v, k - 1)};
{
auto i{2};
#line 29 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(i,k); i += 1 ) {
r *= i;
}
}
#line 32 "cpp2taylor.h2"
return r;
}
#line 35 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::set(cpp2::impl::in<int> k, cpp2::impl::in<R> value) & -> void{
if (cpp2::cpp2_default.is_active() && !([_0 = 1, _1 = k, _2 = dim]{ return cpp2::impl::cmp_less_eq(_0,_1) && cpp2::impl::cmp_less_eq(_1,_2); }()) ) { cpp2::cpp2_default.report_violation(""); }
CPP2_ASSERT_IN_BOUNDS(v, k - 1) = value;
{
auto i{2};
#line 40 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(i,k); i += 1 ) {
CPP2_ASSERT_IN_BOUNDS(v, k - 1) /= CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(CPP2_ASSERT_IN_BOUNDS(v, k - 1)),i);
}
}
#line 43 "cpp2taylor.h2"
}
#line 47 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::get(cpp2::impl::in<int> i, cpp2::impl::in<R> v0) const& -> R{
if (i == 0) {
return v0;
}
else {if (cpp2::impl::cmp_greater(i,dim)) {
return 0.0;
}}
return CPP2_ASSERT_IN_BOUNDS(v, i - 1);
}
#line 59 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator+=(cpp2::impl::in<taylor> o) & -> auto&&{
(*this) = (*this) + o;
return (*this);
}
#line 65 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::operator-=(cpp2::impl::in<taylor> o) & -> auto&&{
(*this) = (*this) - o;
return (*this);
}
#line 71 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::operator+(cpp2::impl::in<taylor> o) const& -> taylor{
return add(o, 0.0, 0.0); // Primal values are not required.
}
#line 76 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::operator-(cpp2::impl::in<taylor> o) const& -> taylor{
return sub(o, 0.0, 0.0); // Primal values are not required.
}
#line 81 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::operator+() const& -> taylor{
return (*this);
}
#line 86 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::operator-() const& -> taylor{
taylor r {};
{
auto k{1};
#line 90 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = -CPP2_ASSERT_IN_BOUNDS(v, k - 1);
}
}
#line 94 "cpp2taylor.h2"
return r;
}
#line 97 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::add(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor{
taylor r {};
{
auto k{1};
#line 101 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = get(k, v0) + CPP2_UFCS(get)(o, k, o0);
}
}
#line 105 "cpp2taylor.h2"
return r;
}
#line 108 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::sub(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor{
taylor r {};
{
auto k{1};
#line 112 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = get(k, v0) - CPP2_UFCS(get)(o, k, o0);
}
}
#line 116 "cpp2taylor.h2"
return r;
}
#line 119 "cpp2taylor.h2"
template <typename R, int dim> template<int dim_o> [[nodiscard]] auto taylor<R,dim>::mul(taylor<R,dim_o> const& o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor<R,std::max(dim, dim_o)>{
int constexpr dim_r{ std::max(dim, dim_o) };
taylor<R,dim_r> r {};
{
auto k{1};
#line 124 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim_r); k += 1 ) {
{
auto j{0};
#line 126 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) += get(j, v0) * o.get(k - j, o0);
}
}
#line 129 "cpp2taylor.h2"
}
}
#line 130 "cpp2taylor.h2"
return r;
}
#line 133 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::div(cpp2::impl::in<taylor> o, cpp2::impl::in<R> v0, cpp2::impl::in<R> o0) const& -> taylor{
taylor r {};
R r0 {v0 / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(v0),o0)};
R factor {1.0 / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(1.0),o0)};
{
auto k{1};
#line 140 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = get(k, v0);
{
auto j{0};
#line 143 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) -= CPP2_UFCS(get)(r, j, r0) * o.get(k - j, o0);
}
}
#line 146 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) *= factor;
}
}
#line 148 "cpp2taylor.h2"
return r;
}
#line 151 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::sqrt(cpp2::impl::in<R> v0) const& -> taylor{
taylor r {};
R r0 {std::sqrt(v0)};
R factor {0.5 / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(0.5),r0)};
{
auto k{1};
#line 158 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = get(k, v0);
{
auto j{1};
#line 161 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) -= r.get(j, r0) * r.get(k - j, r0);
}
}
#line 164 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) *= factor;
}
}
#line 167 "cpp2taylor.h2"
return r;
}
#line 170 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::log(cpp2::impl::in<R> v0) const& -> taylor{
taylor r {};
R r0 {std::log(v0)};
R factor {1.0 / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(1.0),v0)};
{
auto k{1};
#line 177 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) = k * get(k, v0);
{
auto j{1};
#line 180 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) -= j * get(k - j, v0) * r.get(j, r0);
}
}
#line 183 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) *= factor / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(factor),k);
}
}
#line 186 "cpp2taylor.h2"
return r;
}
#line 189 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::exp(cpp2::impl::in<R> v0) const& -> taylor{
taylor r {};
R r0 {std::exp(v0)};
R factor {1.0 / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(1.0),v0)};
{
auto k{1};
#line 196 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
{
auto j{1};
#line 198 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) += j * r.get(k - j, r0) * get(j, v0);
}
}
#line 201 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS(r.v, k - 1) /= CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(CPP2_ASSERT_IN_BOUNDS(r.v, k - 1)),k);
}
}
#line 204 "cpp2taylor.h2"
return r;
}
#line 208 "cpp2taylor.h2"
template <typename R, int dim> auto taylor<R,dim>::comp_sin_cos(taylor& s, taylor& c, cpp2::impl::in<taylor> u, cpp2::impl::in<R> u0) -> void{
R s0 {std::sin(u0)};
R c0 {std::cos(u0)};
{
auto k{1};
#line 213 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(k,dim); k += 1 ) {
{
auto j{1};
#line 215 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(j,k); j += 1 ) {
CPP2_ASSERT_IN_BOUNDS(s.v, k - 1) += j * u.get(j, u0) * c.get(k - j, c0);
CPP2_ASSERT_IN_BOUNDS(c.v, k - 1) -= j * u.get(j, u0) * s.get(k - j, s0);
}
}
#line 219 "cpp2taylor.h2"
CPP2_ASSERT_IN_BOUNDS(s.v, k - 1) /= CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(CPP2_ASSERT_IN_BOUNDS(s.v, k - 1)),k);
CPP2_ASSERT_IN_BOUNDS(c.v, k - 1) /= CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(CPP2_ASSERT_IN_BOUNDS(c.v, k - 1)),k);
}
}
#line 222 "cpp2taylor.h2"
}
#line 225 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::sin(cpp2::impl::in<R> v0) const& -> taylor{
taylor t {};
taylor r {};
comp_sin_cos(r, t, (*this), v0);
static_cast<void>(cpp2::move(t));
return r;
}
#line 235 "cpp2taylor.h2"
template <typename R, int dim> [[nodiscard]] auto taylor<R,dim>::cos(cpp2::impl::in<R> v0) const& -> taylor{
taylor t {};
taylor r {};
comp_sin_cos(t, r, (*this), v0);
static_cast<void>(cpp2::move(t));
return r;
}
#line 246 "cpp2taylor.h2"
template<typename R, int order> [[nodiscard]] auto to_string(taylor<R,order> const& o) -> std::string{
std::string r {"("};
{
auto i{1};
#line 249 "cpp2taylor.h2"
for( ; cpp2::impl::cmp_less_eq(i,order); i += 1 ) {
r += " " + cpp2::to_string(CPP2_ASSERT_IN_BOUNDS(o, i)) + "";
}
}
#line 252 "cpp2taylor.h2"
r += " )";
return r;
}
}
#endif