-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctaylor.h
2044 lines (2031 loc) · 52.6 KB
/
ctaylor.h
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// published under MIT license
/// Author: Peter Foelsche
/// October-1th 2024
/// Austin, TX, USA
/// email: peter_foelsche@outlook.com
/// A sparse, dual number implementation for calculating not just the 1th order of derivatives
/// Refer to ctaylor.cpp for a example usage
/// Compile time under Visual C++ 2022 tends to be much longer than using g++
/// Requires C++14
#pragma once
#include "initializer_list.h"
#include <iostream>
#include <type_traits>
#include <array>
#include <limits>
#include <functional>
#include <boost/mp11.hpp>
#include <boost/iterator/permutation_iterator.hpp>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include "merge_sorted_sets.h"
#include "taylor_series_expansions.h"
namespace taylor
{
namespace implementation
{
template<std::size_t POS, typename ...ARGS>
std::ostream& printTuple(std::ostream&_rS, const std::tuple<ARGS...>&_r, const std::integral_constant<std::size_t, POS>&);
template<typename ...ARGS>
std::ostream &operator<<(std::ostream&_rS, const std::tuple<ARGS...>&_r)
{ _rS << "(";
printTuple(_rS, _r, std::tuple_size<std::tuple<ARGS...> >());
return _rS << ")";
}
template<typename ...ARGS>
std::ostream& printTuple(std::ostream&_rS, const std::tuple<ARGS...>&, const std::integral_constant<std::size_t, 0>&)
{ return _rS;
}
template<std::size_t POSM1, typename ...ARGS>
std::ostream& printTuple(std::ostream&_rS, const std::tuple<ARGS...>&_r, const std::integral_constant<std::size_t, POSM1>&)
{ _rS << std::get<std::tuple_size<std::tuple<ARGS...> >::value - POSM1>(_r) << ",";
return printTuple(_rS, _r, std::integral_constant<std::size_t, POSM1 - 1>());
}
template<typename SIZE>
struct getTypeFromSize;
/// meta function for merging different result types
template<typename A, typename B>
struct common_type
{ typedef typename std::common_type<A, B>::type type;
};
/// function for merginig different result tyoes
template<typename T, typename F>
typename common_type<
typename std::decay<decltype(std::declval<T>()())>::type,
typename std::decay<decltype(std::declval<F>()())>::type
>::type if_(
const bool _b,
T &&_rT,
F&&_rF
);
using namespace boost::mp11;
/// to be passed to mp_for_each with a vector/set argument to ctaylor
/// for debgging purposes
struct output
{ std::ostream&m_r;
output(std::ostream&_r)
:m_r(_r)
{
}
template<std::size_t I>
void operator()(const mp_size_t<I>&) const
{ m_r << I << ",";
}
void operator()(const mp_list<>&) const
{ m_r << "mp_list(), ";
}
template<typename FIRST, typename SECOND>
void operator()(const pair<FIRST, SECOND>&) const
{ m_r << "pair(";
(*this)(FIRST());
(*this)(SECOND());
m_r << ")";
}
template<typename FIRST, typename ...REST, typename POSM1>
void operator()(const mp_list<FIRST, REST...>&, const POSM1) const
{ (*this)(mp_at<mp_list<FIRST, REST...>, mp_size_t<mp_size<mp_list<FIRST, REST...> >::value - POSM1::value> >());
(*this)(mp_list<FIRST, REST...>(), mp_size_t<POSM1::value - 1>());
}
template<typename FIRST, typename ...REST>
void operator()(const mp_list<FIRST, REST...>&, const mp_size_t<0>&) const
{
}
template<typename FIRST, typename ...REST>
void operator()(const mp_list<FIRST, REST...>&) const
{ m_r << "mp_list(";
(*this)(mp_list<FIRST, REST...>(), mp_size<mp_list<FIRST, REST...> >());
m_r << ")";
}
};
template<typename ...REST>
std::ostream&operator<<(std::ostream&_r, const mp_list<REST...>&)
{ _r << "(";
mp_for_each<
mp_list<REST...>
>(output(_r));
_r << ")";
return _r;
}
template<std::size_t I>
struct factorial
{ static constexpr double value = I*factorial<I - 1>::value;
};
template<>
struct factorial<0>
{ static constexpr double value = 1.0;
};
template<typename>
struct accumulatedFactorial;
template<>
struct accumulatedFactorial<mp_list<> >
{ static constexpr double value = 1.0;
};
template<std::size_t ENUM, std::size_t ORDER>
struct accumulatedFactorial<
mp_list<
pair<
mp_size_t<ENUM>,
mp_size_t<ORDER>
>
>
>
{ static constexpr double value = factorial<ORDER>::value;
};
template<typename FIRST, typename ...REST>
struct accumulatedFactorial<
mp_list<
FIRST,
REST...
>
>
{ static constexpr double value = accumulatedFactorial<mp_list<FIRST> >::value*accumulatedFactorial<mp_list<REST...> >::value;
};
template<typename T>
struct containsValue;
template<typename T>
struct containsValue2;
/// creates a type ready for passing to ctaylor
/// with the value as the first element
/// and the 1th order derivative as the second element
/// ENUM indicating the independent variable
template<std::size_t ENUM>
using makeIndependent = mp_list<
mp_list<>,
mp_list<
pair<
mp_size_t<ENUM>,
mp_size_t<1> /// the order
>
>
>;
/// for determining the order -- accumulating the second part of the pair
template<typename SUM, typename PAIR>
using add_second=std::integral_constant<
std::size_t,
SUM::value + PAIR::second_type::value
>;
/// for determining the order
template<typename LIST>
struct order
{ static constexpr const auto value = mp_fold<LIST, mp_size_t<0>, add_second>::value;
};
template<typename T0, typename T1>
struct lexicographical_compare;
template<typename T>
struct lexicographical_compare<T, T>
{ typedef mp_false type;
};
template<>
struct lexicographical_compare<mp_list<>, mp_list<> >
{ typedef mp_false type;
};
template<typename ...T>
struct lexicographical_compare<mp_list<T...>, mp_list<> >
{ typedef mp_false type;
};
template<typename ...T>
struct lexicographical_compare<mp_list<>, mp_list<T...> >
{ typedef mp_true type;
};
/// doing the comparison starting from the rear
/// makes certain, that multiplying a taylor polynomial
/// with an element of anothher polynomial yields an already sorted polynomial
template<typename ...R0, typename ...R1>
struct lexicographical_compare<mp_list<R0...>, mp_list<R1...> >
{ typedef typename std::conditional<
(mp_back<mp_list<R0...> >::first_type::value < mp_back<mp_list<R1...> >::first_type::value),
mp_identity<mp_true>,
typename std::conditional<
(mp_back<mp_list<R0...> >::first_type::value > mp_back<mp_list<R1...> >::first_type::value),
mp_identity<mp_false>,
typename std::conditional<
(mp_back<mp_list<R0...> >::second_type::value < mp_back<mp_list<R1...> >::second_type::value),
mp_identity<mp_true>,
typename std::conditional<
(mp_back<mp_list<R0...> >::second_type::value > mp_back<mp_list<R1...> >::second_type::value),
mp_identity<mp_false>,
lexicographical_compare<mp_pop_back<mp_list<R0...> >, mp_pop_back<mp_list<R1...> > >
>::type
>::type
>::type
>::type::type type;
};
template<typename T0, typename T1>
struct compareListOfPairs
{ typedef typename std::conditional<
(order<T0>::value < order<T1>::value),
mp_identity<mp_true>,
typename std::conditional<
(order<T0>::value > order<T1>::value),
mp_identity<mp_false>,
lexicographical_compare<T0, T1>
>::type
>::type::type type;
};
template<typename T>
struct compareListOfPairs<T, T>
{ typedef mp_false type;
};
template<typename T0, typename T1>
struct compareListOfPairs2
{ typedef typename compareListOfPairs<typename T0::first_type, typename T1::first_type>::type type;
};
#if 0
template<typename LIST>
struct listOfListsIsSorted;
template<>
struct listOfListsIsSorted<mp_list<> >
{ typedef mp_true type;
};
template<typename T>
struct listOfListsIsSorted<mp_list<T> >
{ typedef mp_true type;
};
template<typename T0, typename T1, typename ...REST>
struct listOfListsIsSorted<mp_list<T0, T1, REST...> >
{ typedef mp_and<
typename compareListOfPairs<T0, T1>::type,
typename listOfListsIsSorted<mp_pop_front<mp_list<T0, T1, REST...> > >::type
> type;
};
#endif
/// find positions of elements in SOURCE in TARET
template<typename TARGET, typename SOURCE, typename SIZE, bool CHECK=true>
struct findPositions
{
static_assert(!CHECK || mp_size<TARGET>::value >= mp_size<SOURCE>::value, "size of target must be larger than size of source!");
static_assert(mp_is_set<TARGET>::value, "TARGET must be a set!");
static_assert(mp_is_set<SOURCE>::value, "SOURCE must be a set!");
static_assert(!CHECK || std::is_same<TARGET, mp_set_union<TARGET, SOURCE> >::value, "TARGET must contain all elements in SOURCE");
typedef typename getTypeFromSize<SIZE>::type TYPE;
#if 0
template<typename STATE, typename SOURCE_ELEMENT>
using checkPosition = mp_list<
typename std::conditional<
(mp_first<STATE>::value == std::numeric_limits<TYPE>::max()),
std::conditional<
std::is_same<
SOURCE_ELEMENT,
mp_third<STATE>
>::value,
mp_second<STATE>,
mp_first<STATE>
>,
mp_identity<mp_first<STATE> >
>::type::type,
mp_size_t<mp_second<STATE>::value + 1>,
mp_third<STATE>
>;
template<typename STATE, typename TARGET_ELEMENT>
using findPosition = mp_push_back<
STATE,
mp_list<
mp_size<STATE>,
mp_first<
mp_fold<
SOURCE,
mp_list<
mp_size_t<std::numeric_limits<TYPE>::max()>, // the result
mp_size_t<0>, // the next position
TARGET_ELEMENT
>,
checkPosition
>
>
>
>;
typedef mp_fold<
TARGET,
mp_list<>,
findPosition
> type;
#else
template<typename RESULT, typename T>
using findElement = mp_push_back<
RESULT,
mp_list<
mp_size<RESULT>,
typename std::conditional<
(mp_find<SOURCE, T>::value == mp_size<SOURCE>::value),
mp_size_t<std::numeric_limits<TYPE>::max()>,
mp_find<SOURCE, T>
>::type
>
>;
typedef mp_fold<
TARGET,
mp_list<>,
findElement
> type;
#endif
};
template<typename L0, typename L1>
using combine = mp_list<
mp_first<L0>,
mp_second<L0>,
mp_second<L1>
>;
/// find positions of elements in SOURCE in TARET
template<typename TARGET, typename SOURCE0, typename SOURCE1>
struct findPositions2
{
#ifndef NDEBUG
static_assert((mp_size<TARGET>::value >= mp_size<SOURCE0>::value), "size of target must be larger than size of source!");
static_assert(mp_is_set<TARGET>::value, "TARGET must be a set!");
static_assert(mp_is_set<SOURCE0>::value, "SOURCE must be a set!");
static_assert(mp_is_set<SOURCE1>::value, "SOURCE must be a set!");
static_assert(std::is_same<TARGET, mp_set_union<TARGET, SOURCE0, SOURCE1> >::value, "TARGET must contain all elements in SOURCE");
#endif
typedef mp_plus<mp_max<mp_size<SOURCE0>, mp_size<SOURCE1> >, mp_size_t<1> > SIZE;
typedef mp_transform<
combine,
typename findPositions<TARGET, SOURCE0, SIZE>::type,
typename findPositions<TARGET, SOURCE1, SIZE>::type
> type;
};
template<typename A, typename B>
struct combineTwoPairs;
template<typename A, typename ...B, typename ...C>
struct combineTwoPairs<
pair<A, mp_list<B...> >,
pair<A, mp_list<C...> >
>
{ typedef pair<
A,
mp_append<
mp_list<B...>,
mp_list<C...>
>
> type;
};
/// new merge<>
/// COMPARE= compareListOfPairs2
/// MERGE=combineTwoPairs
/// CONTAINS_VALUE=containsValue2
/// merge two sets of list_of_list
template<
typename T0,
typename T1,
template<typename, typename> class COMPARE=compareListOfPairs,
template<typename, typename> class MERGE=combineTwo,
template<typename> class CONTAINS_VALUE=containsValue
>
struct merge
{
#ifndef NDEBUG
static_assert(mp_is_set<T0>::value, "must be a set!");
static_assert(mp_is_set<T1>::value, "must be a set!");
#endif
typedef typename merge_sorted_sets<
COMPARE,
T0,
T1,
MERGE
>::type type;
#ifndef NDEBUG
static_assert(
CONTAINS_VALUE<type>::type::value == mp_or<
typename CONTAINS_VALUE<T0>::type,
typename CONTAINS_VALUE<T1>::type
>::value, "value in merge result!");
#endif
};
template<
typename T,
template<typename, typename> class COMPARE,
template<typename, typename> class MERGE,
template<typename> class CONTAINS_VALUE
>
struct merge<T, mp_list<>, COMPARE, MERGE, CONTAINS_VALUE>
{ static_assert(mp_is_set<T>::value, "must be a set!");
typedef T type;
};
template<
typename T,
template<typename, typename> class COMPARE,
template<typename, typename> class MERGE,
template<typename> class CONTAINS_VALUE
>
struct merge<mp_list<>, T, COMPARE, MERGE, CONTAINS_VALUE>
{ static_assert(mp_is_set<T>::value, "must be a set!");
typedef T type;
};
template<
typename T,
template<typename, typename> class COMPARE,
template<typename, typename> class MERGE,
template<typename> class CONTAINS_VALUE
>
struct merge<T, T, COMPARE, MERGE, CONTAINS_VALUE>
{ typedef T type;
};
template<
template<typename, typename> class COMPARE,
template<typename, typename> class MERGE,
template<typename> class CONTAINS_VALUE
>
struct merge<mp_list<>, mp_list<>, COMPARE, MERGE, CONTAINS_VALUE>
{ typedef mp_list<> type;
};
template<typename SIZE>
struct getTypeFromSize
{ typedef typename std::conditional<
(SIZE::value <= std::numeric_limits<unsigned int>::max()),
typename std::conditional<
(SIZE::value <= std::numeric_limits<unsigned short>::max()),
typename std::conditional<
(SIZE::value <= std::numeric_limits<unsigned char>::max()),
unsigned char,
unsigned short
>::type,
unsigned int
>::type,
std::size_t
>::type type;
};
template<typename LIST_OF_PAIRS, typename TYPE>
struct createPair
{ typedef foelsche::init_list::convertToPair<
mp_transform<
mp_first,
LIST_OF_PAIRS
>,
mp_transform<
mp_second,
LIST_OF_PAIRS
>,
TYPE
> type;
};
template<typename LIST, typename SIZE>
struct convertToStdArray3;
template<typename ...ELEMENTS, typename SIZE>
struct convertToStdArray3<mp_list<ELEMENTS...>, SIZE>
{ typedef typename getTypeFromSize<SIZE>::type TYPE;
typedef std::initializer_list<TYPE> IL;
typedef std::pair<IL, IL> PAIR;
static constexpr const std::initializer_list<PAIR> value =
{ createPair<ELEMENTS, TYPE>::type::value...
};
};
template<typename ...ELEMENTS, typename SIZE>
constexpr const std::initializer_list<typename convertToStdArray3<mp_list<ELEMENTS...>, SIZE>::PAIR> convertToStdArray3<mp_list<ELEMENTS...>, SIZE>::value;
template<typename LIST, typename SIZE>
struct convertToStdArray
{ typedef typename getTypeFromSize<SIZE>::type TYPE;
typedef std::initializer_list<TYPE> IL;
typedef std::pair<IL, IL> PAIR;
static constexpr const PAIR value = foelsche::init_list::convertToPair<
mp_transform<
mp_second,
LIST
>,
mp_transform<
mp_third,
LIST
>,
TYPE
>::value;
};
template<typename LIST, typename SIZE>
constexpr const typename convertToStdArray<LIST, SIZE>::PAIR convertToStdArray<LIST, SIZE>::value;
template<typename LIST, typename SIZE>
struct convertToStdArray2
{ typedef typename foelsche::init_list::convertToStdInitializerList<
mp_transform<mp_second, LIST>,
typename getTypeFromSize<SIZE>::type
> type;
};
/// for creating the type result of multiplying one element of a ctaylor array with another
/// second and third arguments are an element of the first template argument of ctaylor
/// calls itself recursively
template<typename, typename, typename>
struct multiply_1_1_R;
template<typename RESULT>
struct multiply_1_1_R<RESULT, mp_list<>, mp_list<> >
{ typedef RESULT type;
};
template<typename RESULT, typename T, typename ...REST>
struct multiply_1_1_R<RESULT, mp_list<T, REST...>, mp_list<> >
{ typedef typename multiply_1_1_R<
mp_push_back<
RESULT,
T
>,
mp_list<REST...>,
mp_list<>
>::type type;
};
template<typename RESULT, typename T, typename ...REST>
struct multiply_1_1_R<RESULT, mp_list<>, mp_list<T, REST...> >
{ typedef typename multiply_1_1_R<
mp_push_back<RESULT, T>,
mp_list<>,
mp_list<REST...>
>::type type;
};
template<typename RESULT, typename T0, typename ...R0, typename T1, typename ...R1>
struct multiply_1_1_R<RESULT, mp_list<T0, R0...>, mp_list<T1, R1...> >
{ typedef typename std::conditional<
(T0::first_type::value < T1::first_type::value),
multiply_1_1_R<
mp_push_back<RESULT, T0>,
mp_list<R0...>,
mp_list<T1, R1...>
>,
typename std::conditional<
(T0::first_type::value > T1::first_type::value),
multiply_1_1_R<
mp_push_back<RESULT, T1>,
mp_list<T0, R0...>,
mp_list<R1...>
>,
multiply_1_1_R<
mp_push_back<
RESULT,
pair<
typename T0::first_type,
mp_size_t<T0::second_type::value + T1::second_type::value>
>
>,
mp_list<R0...>,
mp_list<R1...>
>
>::type
>::type::type type;
};
/// type is only accessed in case of it is needed
template<typename STATE, typename T0E>
struct multiply_1_1_E
{ typedef mp_push_back<
mp_first<STATE>,
pair<
typename multiply_1_1_R<
mp_list<>,
mp_first<T0E>,
mp_first<mp_second<STATE> >// T1E
>::type,
mp_list<
mp_list<
mp_second<T0E>,
mp_second<mp_second<STATE> >
>
>
>
> type;
};
/// invokes multiply_1_1_R
/// only if the resuling order would be smaller or equal MAX
template<typename STATE, typename T0E>
using multiply_1_1 = mp_list<
typename std::conditional<
(order<mp_first<T0E> >::value + order<mp_first<mp_second<STATE> > >::value <= mp_third<STATE>::value),
multiply_1_1_E<STATE, T0E>,
mp_identity<mp_first<STATE> >
>::type::type,
mp_second<STATE>,//T1E
mp_third<STATE>//MAX
>;
/// multiplies all elements of a template argument to ctaylor with one element
template<typename STATE, typename T1E>
using multiply_2_1 = mp_list<
mp_first<STATE>,//T0
typename merge<
mp_first<
mp_fold<
mp_first<STATE>, // T0
mp_list<
mp_list<>,
T1E,
mp_third<STATE>//MAX
>,
multiply_1_1
>
>,
mp_second<STATE>,
compareListOfPairs2,
combineTwoPairs,
containsValue2
>::type,
mp_third<STATE>//MAX
>;
template<typename A, typename B>
using make_pair = pair<A, B>;
// Transform function to pair each type with its index
template<typename L>
using add_index = mp_transform<
make_pair,
L,
mp_iota_c<mp_size<L>::value>
>;
/// multiplies two template arguments to ctaylor with each other
template<typename T0, typename T1, std::size_t MAX>
using multiply_2_2 = mp_second<
mp_fold<
add_index<T1>,
mp_list<
add_index<T0>,
mp_list<>,
mp_size_t<MAX>
>,
multiply_2_1
>
>;
template<typename T0, typename T1>
struct TypeDisplayer
{ static_assert(
std::is_same<
T0,
T1
>::value,
"types are not identical!"
);
};
// Define a metafunction to check if the first element of a sub-list is mp_size_t<ENUM>
template <typename ENUM, typename PAIR>
using is_first_equal_to_enum = std::is_same<typename PAIR::first_type, ENUM>;
// Define a metafunction to check if any sub-list meets the condition
template <typename ENUM, typename List>
using contains_pair_first = mp_any_of<
List,
mp_bind_front<
is_first_equal_to_enum,
ENUM
>::template fn
>;
/// check if a sublist of List contains a PAIR with first equal to ENUM
template <typename ENUM, typename List>
using contains_list_pair_first = mp_any_of<
List,
mp_bind_front<
contains_pair_first,
ENUM
>::template fn
>;
/// gets the maximum order for ENUM
template<typename ENUM, typename LIST>
using getMaxOrder = mp_max_element<
mp_push_front<
mp_transform<
second_of_pair,
mp_filter<
mp_bind_front<
is_first_equal_to_enum,
ENUM
>::template fn,
LIST
>
>,
mp_size_t<0>
>,
mp_less
>;
template<typename ENUM, typename T, typename T1, std::size_t MAX>
struct ChainRule2;
/// the class
/// first template argument is a vector of a vector of pairs of independent variable enum and order
/// all vectors must be sorted
/// usually first entry is mp_list<> indicating 0th derivative or value
/// MAX indicates maximum order of derivatives calculated
/// MAX should be minimally 1 for it to work
/// MAX should be minimally 2 for application of this class to make sense otherwise jacobian.h ought to be used
template<typename T, std::size_t MAX>
struct ctaylor
{ typedef T SET;
static constexpr std::size_t SIZE = mp_size<T>::value;
//static_assert(SIZE > 0, "size must be at least one!");
static_assert(mp_is_set<T>::value, "must be a set!");
typedef std::array<double, SIZE> ARRAY;
ARRAY m_s;
ctaylor(void) = default;
ctaylor(ctaylor&&) = default;
ctaylor(const ctaylor&) = default;
ctaylor&operator=(const ctaylor&) = default;
ctaylor&operator=(ctaylor&&) = default;
/// copying all elements from the source array to the destination array
/// the destination array is necessarily larger
/// and some elements will have to be initialized with zero
/// used by copy constructor and assignment operator
template<typename T1, bool CHECK = true>
static ARRAY convert(const typename ctaylor<T1, MAX>::ARRAY&_r, const mp_bool<CHECK>& = mp_bool<CHECK>())
{ typedef mp_plus<mp_size<T1>, mp_size_t<1> > SIZE;
typedef typename findPositions<T, T1, SIZE, CHECK>::type SOURCE_POSITIONS;
ARRAY s;
typedef typename getTypeFromSize<SIZE>::type TYPE;
auto &rT = convertToStdArray2<SOURCE_POSITIONS, SIZE>::type::value;
std::transform(
rT.begin(),
rT.end(),
s.begin(),
[&](const std::size_t _i)
{ if (_i == std::numeric_limits<TYPE>::max())
return 0.0;
else
return _r[_i];
}
);
return s;
}
template<
typename U=T,
typename std::enable_if<
(mp_size<mp_first<U> >::value == 0),
int
>::type = 0
>
ctaylor &operator=(const double _d)
{ m_s[0] = _d;
std::fill(m_s.begin() + 1, m_s.end(), 0.0);
return *this;
}
template<typename T1>
ctaylor &operator=(const ctaylor<T1, MAX>&_r)
{ static_assert(ctaylor<T1, MAX>::SIZE < SIZE, "RHS size must be smaller!");
m_s = convert<T1, true>(_r.m_s);
return *this;
}
template<
typename U=T,
typename std::enable_if<
(mp_size<mp_first<U> >::value == 0),
int
>::type = 0
>
ctaylor &operator+=(const double _d)
{ m_s[0] += _d;
return *this;
}
template<
typename U=T,
typename std::enable_if<
(mp_size<mp_first<U> >::value == 0),
int
>::type = 0
>
ctaylor &operator-=(const double _d)
{ m_s[0] -= _d;
return *this;
}
ctaylor &operator*=(const double _d)
{ for (std::size_t i = 0; i < SIZE; ++i)
m_s[i] *= _d;
return *this;
}
ctaylor &operator/=(const double _d)
{ const auto d1 = 1.0/_d;
for (std::size_t i = 0; i < SIZE; ++i)
m_s[i] *= d1;
return *this;
}
ctaylor &operator+=(const ctaylor&_r)
{ for (std::size_t i = 0; i < SIZE; ++i)
m_s[i] += _r.m_s[i];
return *this;
}
ctaylor &operator-=(const ctaylor&_r)
{ for (std::size_t i = 0; i < SIZE; ++i)
m_s[i] -= _r.m_s[i];
return *this;
}
template<typename T1>
ctaylor &operator+=(const ctaylor<T1, MAX>&_r)
{ typedef mp_size<T> SIZE;
typedef typename findPositions<T1, T, SIZE, false>::type SOURCE_POSITIONS;
typedef typename getTypeFromSize<SIZE>::type TYPE;
auto &rT = convertToStdArray2<SOURCE_POSITIONS, SIZE>::type::value;
std::transform(
_r.m_s.cbegin(),
_r.m_s.cend(),
boost::make_permutation_iterator(m_s.cbegin(), rT.begin()),
boost::make_permutation_iterator(m_s.begin(), rT.begin()),
[](const double _d0, const double _d1)
{ return _d1 + _d0;
}
);
return *this;
}
template<typename T1>
ctaylor &operator-=(const ctaylor<T1, MAX>&_r)
{ typedef mp_size<T> SIZE;
typedef typename findPositions<T1, T, SIZE, false>::type SOURCE_POSITIONS;
typedef typename getTypeFromSize<SIZE>::type TYPE;
auto &rT = convertToStdArray2<SOURCE_POSITIONS, SIZE>::type::value;
std::transform(
_r.m_s.cbegin(),
_r.m_s.cend(),
boost::make_permutation_iterator(m_s.cbegin(), rT.begin()),
boost::make_permutation_iterator(m_s.begin(), rT.begin()),
[](const double _d0, const double _d1)
{ return _d1 - _d0;
}
);
return *this;
}
template<
typename U=T,
typename std::enable_if<
(mp_size<U>::value == 2 //{{}, {{enum, order}}}
&& mp_size<mp_first<U> >::value == 0
&& mp_size<mp_second<U> >::value == 1
&& mp_size<mp_first<mp_second<U> > >::value == 2
&& mp_second<mp_first<mp_second<U> > >::value == 1
),
int
>::type = 0
>
ctaylor(const double _d, const bool)
:m_s({_d, 1.0})
{
}
ctaylor(const double _d)
:m_s({_d})
{
}
template<typename T1, bool CHECK = true>
ctaylor(const ctaylor<T1, MAX>&_r, const mp_bool<CHECK>& = mp_bool<CHECK>())
:m_s(convert<T1, CHECK>(_r.m_s))
{ static_assert(!CHECK || ctaylor<T1, MAX>::SIZE < SIZE, "RHS size must be smaller!");
}
/// create a new independent variable for chainrule to reduce the number of carried derivatives
template<std::size_t ENUM>
auto convert2Independent(const mp_size_t<ENUM>&) const
{ static_assert(
mp_not<
contains_list_pair_first<
mp_size_t<ENUM>,
T
>
>::value,
"List contains a sublist with first element matching ENUM"
);
typedef ctaylor<makeIndependent<ENUM>, MAX> TYPE;
return TYPE(value(*this), true);
}
/// T does not contain ENUM
template<typename T1, std::size_t ENUM>
auto chainRule(const ctaylor<T1, MAX>&_r, const mp_size_t<ENUM>&_rE, const mp_false&) const
{ return *this;
}
/// T does contain ENUM
template<typename T1, std::size_t ENUM>
auto chainRule(const ctaylor<T1, MAX>&_r, const mp_size_t<ENUM>&_rE, const mp_true&) const
{ return ChainRule2<
mp_size_t<ENUM>,
T,
mp_pop_front<T1>,
MAX
>(*this, dropValue(_r))();
}
/// substitutes one derivative by the ones passed in the first argument
/// might have to be called multiple times
/// the first argument must have been one on which convert2Independent() was called.
/// ENUM must be identical to the ENUM passed to convert2Independent()
template<typename T1, std::size_t ENUM>
auto chainRule(const ctaylor<T1, MAX>&_r, const mp_size_t<ENUM>&_rE) const
{ static_assert(
mp_not<
contains_list_pair_first<
mp_size_t<ENUM>,
T1
>
>::value,
"List contains a sublist with first element matching ENUM"
);
typedef contains_list_pair_first<
mp_size_t<ENUM>,
T
> CONTAINS;
return chainRule(_r, _rE, CONTAINS());
}
ctaylor operator+(const ctaylor&_r) const
{ ctaylor s;
for (std::size_t i = 0; i < SIZE; ++i)
s.m_s[i] = m_s[i] + _r.m_s[i];
return s;
}
ctaylor operator-(const ctaylor&_r) const
{ ctaylor s;
for (std::size_t i = 0; i < SIZE; ++i)
s.m_s[i] = m_s[i] - _r.m_s[i];
return s;
}
template<typename T1>
auto operator+(const ctaylor<T1, MAX>&_r) const
{ typedef typename merge<T, T1>::type TT;
typedef typename findPositions2<TT, T, T1>::type SOURCE_POSITIONS;
ctaylor<TT, MAX> s;
typedef mp_plus<
mp_max<
mp_size<T>,
mp_size<T1>
>,
mp_size_t<1>
> SIZE;
auto &rT = convertToStdArray<
SOURCE_POSITIONS,
SIZE
>::value;
typedef typename getTypeFromSize<SIZE>::type TYPE;
std::transform(
rT.first.begin(),
rT.first.end(),
rT.second.begin(),
s.m_s.begin(),
[&](const TYPE _i0, const TYPE _i1)
{ return _i0 != std::numeric_limits<TYPE>::max()
? (_i1 != std::numeric_limits<TYPE>::max()
? m_s[_i0] + _r.m_s[_i1]
: m_s[_i0]
)
: (_i1 != std::numeric_limits<TYPE>::max()
? _r.m_s[_i1]
: 0.0
);
}
);
return s;
}
template<typename T1>
auto operator-(const ctaylor<T1, MAX>&_r) const
{ typedef typename merge<T, T1>::type TT;
typedef typename findPositions2<TT, T, T1>::type SOURCE_POSITIONS;
ctaylor<TT, MAX> s;
typedef mp_plus<
mp_max<
mp_size<T>,
mp_size<T1>
>,
mp_size_t<1>
> SIZE;
auto &rT = convertToStdArray<SOURCE_POSITIONS, SIZE>::value;
typedef typename getTypeFromSize<SIZE>::type TYPE;
std::transform(
rT.first.begin(),
rT.first.end(),
rT.second.begin(),
s.m_s.begin(),
[&](const TYPE _i0, const TYPE _i1)
{ return _i0 != std::numeric_limits<TYPE>::max()
? (_i1 != std::numeric_limits<TYPE>::max()
? m_s[_i0] - _r.m_s[_i1]
: m_s[_i0]
)
: (_i1 != std::numeric_limits<TYPE>::max()
? -_r.m_s[_i1]
: 0.0
);
}
);
return s;
}
auto operator-(void) const
{ ctaylor s;
for (std::size_t i = 0; i < SIZE; ++i)
s.m_s[i] = -m_s[i];
return s;
}
friend auto operator-(const double _d, const ctaylor&_r)
{ ctaylor s;
for (std::size_t i = 0; i < SIZE; ++i)
s.m_s[i] = -_r.m_s[i];
s.m_s[0] += _d;
return s;
}
template<