-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimbpolic.h
More file actions
919 lines (804 loc) · 32.4 KB
/
simbpolic.h
File metadata and controls
919 lines (804 loc) · 32.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
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
#ifndef SIMBPOLIC
#define SIMBPOLIC
/*******************************
* SIMBPOLIC *
* Simple Symbolic Calculus *
* at compile-time *
* *
* (Currently WIP, largely *
* incomplete, messy and *
* underdocumented, but *
* entirely functional for *
* the purposes of the *
* current project... ) *
* *
* By: Nuno Fernandes *
* *
*******************************/
#include <limits>
#include <utility>
#include <type_traits>
#include <numeric>
#if __CUDA_ARCH__
#define SIMBPOLIC_CUDA_AVAILABLE 1
#elif __CUDA__
#define SIMBPOLIC_CUDA_AVAILABLE 1
#else
#define SIMBPOLIC_CUDA_AVAILABLE 0
#endif
//For maximum interoperatability between CUDA and normal code.
#if SIMBPOLIC_CUDA_AVAILABLE
#define SIMBPOLIC_CUDA_HOS_DEV __host__ __device__
#else
#define SIMBPOLIC_CUDA_HOS_DEV
#endif
#if SIMBPOLIC_CUDA_AVAILABLE
#define SIMBPOLIC_CUDA_ONLY_HOS __host__
#else
#define SIMBPOLIC_CUDA_ONLY_HOS
#endif
#if SIMBPOLIC_CUDA_AVAILABLE
#define SIMBPOLIC_CUDA_ONLY_DEV __device__
#else
#define SIMBPOLIC_CUDA_ONLY_DEV
#endif
namespace Simbpolic
{
/*!
\brief Allows customization of the library's behaviour by declaring member typedefs/type aliases.
\detail Supported aliases:
- \c ResultType, the type of the numeric results.
Should be able to hold real results (so, floating point).
- \c IntegerType, the type that holds integer quantities.
Must be signed!
\remark The user can specify only some of the aliases, with the default
values being taken if the alias is absent.
The full default configuration would be equivalent to using:
~~~~~~{cpp}
class Simbpolic::Configuration
{
public:
using ResultType = double;
using IntegerType = int;
};
~~~~~~
\remark Obviously, this will be equivalent to `class Configuration {};`,
since the default values are taken when the members are not aliased.
*/
class Configuration;
namespace internals
{
#define TYPEDEF_CHECKER(MEMBERNAME, INVALID_TYPE) \
template<class T> inline static constexpr \
auto MEMBERNAME ## _t_checker(T*) -> decltype(typename T:: MEMBERNAME{}); \
template<class T> inline static constexpr \
INVALID_TYPE MEMBERNAME ## _t_checker(...); \
template <class T> \
using MEMBERNAME ## _t_type = decltype( MEMBERNAME ## _t_checker<T>(nullptr) ); \
template <class T> inline static constexpr \
bool MEMBERNAME ## _t_exists = !std::is_same_v<MEMBERNAME ## _t_type<T>, INVALID_TYPE>; \
class config_handler
{
private:
TYPEDEF_CHECKER(ResultType, double);
TYPEDEF_CHECKER(IntegerType, int);
public:
using Type = ResultType_t_type<Configuration>;
using indexer = IntegerType_t_type<Configuration>;
};
#undef TYPEDEF_CHECKER
}
using Type = internals::config_handler::Type;
using indexer = internals::config_handler::indexer;
namespace internals
{
template <class base_T, class exp_T>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline static base_T fastpow_in(const base_T &base, exp_T exp)
{
base_T r{1}, v{base};
while (exp)
{
if (exp & 1)
{
r = r*v;
}
v = (v*v);
exp = (exp >> 1);
}
return r;
}
}
/*!
\brief Expectably efficient implementation of exponentiation by integers.
\pre \p exp_T must be an integer type with bitwise operations.
*/
template <class base_T, class exp_T>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline static base_T fastpow(const base_T &base, exp_T exp)
{
if (exp < 0)
{
return base_T(1)/internals::fastpow_in(base, -exp);
}
else
{
return internals::fastpow_in(base, exp);
}
}
/*
SIMBPOLIC_CUDA_HOS_DEV constexpr inline static indexer gcd(const indexer &a, const indexer &b)
//The Binary Euclidean Algorithm.
//May or may not have been implemented
//with direct inspiration from Wikipedia...
{
if (a < 0 && b < 0)
{
return gcd(-a, -b);
}
else if (a < 0)
{
return gcd(-a, b);
}
else if (b < 0)
{
return gcd(a, -b);
}
if (a == b)
{
return a;
}
else if (a == 0)
{
return b;
}
else if (b == 0)
{
return a;
}
indexer u(a), v(b), factor(1);
while (u % 2 == 0 && v % 2 == 0)
{
u /= 2;
v /= 2;
factor *= 2;
}
while (1)
//Will terminate eventually.
{
if (u > v)
{
const indexer temp = u;
u = v;
v = temp;
}
v -= u;
if (v == 0)
{
return u * factor;
}
else
{
while (v % 2 == 0)
{
v /= 2;
}
}
}
return 0;
}
*/
/*!
\brief Base class, to mark every symbolic function implemented here.
*/
struct SymBase
{
};
/*!
\brief Base class for things that represent exact numbers
(like rationals and the currently unsupported radicals or mathematical constants)
*/
struct SymExactNumber
{
};
struct SymBranched
{
};
struct SymExactBranched: public SymBranched
{
};
struct SymHoldsValues
{
};
struct SymOpFunc
{
};
template <class T>
inline static constexpr bool holds_values = std::is_base_of_v<SymHoldsValues, std::decay_t<T>>;
template <class A, class B> struct func_add;
template <class A, class B> struct func_sub;
template <class A, class B> struct func_mul;
template <class A, class B> struct func_div;
struct Constant;
struct Zero;
struct One;
template <indexer num_, indexer denom_> struct Rational;
template <indexer val> using Intg = Rational<val, 1>;
template <indexer store_idx> struct Stored;
template <indexer order, indexer dim = 0> struct Monomial;
template <class A, class B, indexer dim, class Cut> struct branch_function;
template <class A, class B, class C, indexer dim, class LowerCut, class UpperCut> struct interval_function;
template <class T>
inline static constexpr bool is_symbolic = std::is_base_of_v<SymBase, std::decay_t<T>>;
template <class T>
inline static constexpr bool is_numeric = std::is_base_of_v<SymExactNumber, std::decay_t<T>> ||
std::is_convertible_v<std::decay_t<T>, Type> ||
std::is_same_v<std::decay_t<T>, Constant>;
template <class A, class B>
inline static constexpr bool is_numeric<func_add<A, B>> = is_numeric<A> && is_numeric<B>;
template <class A, class B>
inline static constexpr bool is_numeric<func_sub<A, B>> = is_numeric<A> && is_numeric<B>;
template <class A, class B>
inline static constexpr bool is_numeric<func_mul<A, B>> = is_numeric<A> && is_numeric<B>;
template <class A, class B>
inline static constexpr bool is_numeric<func_div<A, B>> = is_numeric<A> && is_numeric<B>;
template <class A>
inline static constexpr bool is_stored = false;
template <indexer idx>
inline static constexpr bool is_stored<Stored<idx>> = true;
template <class T>
inline static constexpr bool is_exact = std::is_base_of_v<SymExactNumber, std::decay_t<T>>;
template <class T>
inline static constexpr bool is_exceptional = std::is_same_v<T, Zero> || std::is_same_v<T, One>;
template <class T>
inline static constexpr bool is_branched = std::is_base_of_v<SymBranched, std::decay_t<T>>;
template <class T>
inline static constexpr bool is_exact_branched = std::is_base_of_v<SymExactBranched, std::decay_t<T>>;
template <class T>
inline static constexpr bool is_op_func = std::is_base_of_v<SymOpFunc, std::decay_t<T>>;
/*!
\brief A variable along a dimension.
\remark Dimensions are 1-indexed!
*/
template <indexer dim>
struct Var
{
static_assert(dim > 0, "Dimensions are 1-indexed!");
SIMBPOLIC_CUDA_HOS_DEV constexpr Var()
{
}
SIMBPOLIC_CUDA_HOS_DEV constexpr Var(const Monomial<1, dim> &m)
{
}
SIMBPOLIC_CUDA_HOS_DEV constexpr inline operator Monomial<1, dim> () const
{
return Monomial<1, dim>{};
}
};
namespace internals
{
template <class Func1, class Func2, indexer dim>
SIMBPOLIC_CUDA_HOS_DEV inline static constexpr bool share_dimensions_impl()
{
if constexpr (Func1::template has_dimension<dim>() && Func2::template has_dimension<dim>())
{
return true;
}
else
{
if constexpr (dim > Func1::max_dimension || dim > Func2::max_dimension)
{
return false;
}
else
{
return share_dimensions_impl<Func1, Func2, dim + 1>();
}
}
}
}
/*!
\brief Returns `true` if the functions share at least one dimension.
*/
template <class Func1, class Func2>
SIMBPOLIC_CUDA_HOS_DEV inline static constexpr bool share_dimensions()
{
if constexpr (Func1::min_dimension > Func2::max_dimension ||
Func1::max_dimension < Func2::max_dimension )
{
return false;
}
else
{
if constexpr (Func1::min_dimension < Func2::min_dimension)
{
return internals::share_dimensions_impl<Func1, Func2, Func2::min_dimension>();
}
else
{
return internals::share_dimensions_impl<Func1, Func2, Func1::min_dimension>();
}
}
}
/*!
\remark To actually get stuff from the store, it must have a function with signature
`template <indexer i> SIMBPOLIC_CUDA_HOS_DEV inline constexpr Type get() const`
that returns the value that is to be attributed to the `i`-th constant.
*/
struct Store {};
template <class C>
inline static constexpr bool is_store = std::is_base_of_v<Store, C>;
namespace internals
{
class NotFoundType{};
#define SIMBPOLIC_HOLD_ARGS(...) __VA_ARGS__
#define SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(FUNCNAME, TEMPARGS, TEMPARGNAMES, ARGINIT) \
private: \
template <class T, TEMPARGS> inline static constexpr \
auto FUNCNAME ## _checker(T*) -> decltype(std::declval<T>().template FUNCNAME<TEMPARGNAMES>(ARGINIT)); \
template <class T, TEMPARGS> inline static constexpr \
auto FUNCNAME ## _checker(...) -> NotFoundType; \
public: \
template <TEMPARGS> static constexpr bool \
has_ ##FUNCNAME = !std::is_same_v<NotFoundType, decltype(FUNCNAME ## _checker <C, TEMPARGNAMES> (nullptr))>
template <class C>
class MemberFunctionChecker
{
SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(change_dim,
SIMBPOLIC_HOLD_ARGS(indexer from, indexer to),
SIMBPOLIC_HOLD_ARGS(from, to),
SIMBPOLIC_HOLD_ARGS(Var<from>{}, Var<to>{})
);
SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(offset,
SIMBPOLIC_HOLD_ARGS(indexer dim, class Off),
SIMBPOLIC_HOLD_ARGS(dim, Off),
SIMBPOLIC_HOLD_ARGS(Var<dim>{}, Off{})
);
SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(reverse,
SIMBPOLIC_HOLD_ARGS(indexer dim),
SIMBPOLIC_HOLD_ARGS(dim),
SIMBPOLIC_HOLD_ARGS(Var<dim>{})
);
SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(deform,
SIMBPOLIC_HOLD_ARGS(indexer dim, class Fact),
SIMBPOLIC_HOLD_ARGS(dim, Fact),
SIMBPOLIC_HOLD_ARGS(Var<dim>{}, Fact{})
);
SIMBPOLIC_MEMBER_FUNCTION_CHECKER_HELPER(distribute,
SIMBPOLIC_HOLD_ARGS(indexer recurse_count),
SIMBPOLIC_HOLD_ARGS(recurse_count),
);
};
}
/*!
\brief Changes any functions that depend on the \p from variable to \p to.
*/
template <indexer from, indexer to, class Func>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto change_dim(const Var<from> &x, const Var<to> &y, const Func &f)
{
if constexpr (internals::MemberFunctionChecker<Func>::template has_change_dim<from, to>)
{
return f.change_dim(x, y);
}
else
{
return f;
}
}
/*!
\brief Returns `f(x + off)` in a consistent way.
(For branched functions mainly, since we don't properly handle conditions yet...)
*/
template <indexer dim, class Func, class Off>
SIMBPOLIC_CUDA_HOS_DEV constexpr static auto offset(const Var<dim> &var, const Off &off, const Func& f)
{
if constexpr (internals::MemberFunctionChecker<Func>::template has_offset<dim, Off>)
{
return f.offset(var, off);
}
else
{
return f;
}
}
/*!
\brief Returns `f(-x)` in a consistent way.
(For branched functions mainly, since we don't properly handle conditions yet...)
*/
template <indexer dim, class Func>
SIMBPOLIC_CUDA_HOS_DEV constexpr static auto reverse(const Var<dim> &var, const Func& f)
{
if constexpr (internals::MemberFunctionChecker<Func>::template has_reverse<dim>)
{
return f.reverse(var);
}
else
{
return f;
}
}
/*!
\brief Returns `f(fact x)` in a consistent way.
(For branched functions mainly, since we don't properly handle conditions yet...)
\warning The factor must be positive! If not, the conditions will not be properly updated!
In case a negative factor is desired, see `reverse`.
*/
template <indexer dim, class Func, class Fact>
SIMBPOLIC_CUDA_HOS_DEV constexpr static auto deform(const Var<dim> &var, const Fact &fact, const Func& f)
{
if constexpr (internals::MemberFunctionChecker<Func>::template has_deform<dim, Fact>)
{
return f.deform(var, fact);
}
else
{
return f;
}
}
template <indexer recurse_count, class Func>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline static auto distribute(const Func& f)
{
if constexpr (recurse_count > 0 && internals::MemberFunctionChecker<Func>::template has_distribute<recurse_count>)
{
return f.template distribute<recurse_count>();
}
else
{
return f;
}
}
}
#include "simbpolic/constants.h"
#include "simbpolic/constant_comparison.h"
#include "simbpolic/stored.h"
#include "simbpolic/func_holders.h"
#include "simbpolic/monomial.h"
#include "simbpolic/op_funcs.h"
#include "simbpolic/branch.h"
#include "simbpolic/interval.h"
#include "simbpolic/branching_helper.h"
#include "simbpolic/integrate.h"
namespace Simbpolic
{
#define SIMBPOLIC_OPS_BINARY_OPER(OP, NAME) \
template <class T1, class T2, \
typename std::enable_if_t<(is_symbolic<T1> || is_numeric<T1>) && \
(is_symbolic<T2> || is_numeric<T2>) && \
!(is_branched<T1> && !is_branched<T2>) && \
!(!is_branched<T1> && is_branched<T2>)>* = nullptr> \
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator OP (const T1& a, const T2& b) \
{ \
if constexpr (is_symbolic<T1>&& is_symbolic<T2>) \
{ \
return NAME <std::decay_t<T1>,std::decay_t<T2>>{a, b}; \
} \
else if constexpr (is_symbolic<T1>) \
{ \
return NAME <std::decay_t<T1>,Constant>{a, Constant{Type(b)}}; \
} \
else /*if constexpr (is_symbolic<T2>)*/ \
{ \
return NAME <Constant,std::decay_t<T2>>{Constant{Type(b)},a}; \
} \
} \
SIMBPOLIC_OPS_BINARY_OPER(+, func_add);
SIMBPOLIC_OPS_BINARY_OPER(-, func_sub);
SIMBPOLIC_OPS_BINARY_OPER(*, func_mul);
SIMBPOLIC_OPS_BINARY_OPER(/, func_div);
template <class T, typename std::enable_if_t<is_symbolic<T>>* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator+ (const T& t)
{
return t;
}
template <class T, typename std::enable_if_t<is_symbolic<T>>* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator- (const T& t)
{
return t * Intg<-1>{};
}
#define SIMBPOLIC_OPS_BINARY_OPER_SIMPLIFIER(OP, NAME) \
template <class T1, class A, class B, \
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> && \
(is_numeric<A> || is_numeric<B>) >* = nullptr> \
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator OP (const T1& val, const NAME<A, B>& func) \
{ \
if constexpr (is_numeric<A> && !is_stored<A>) \
{ \
return (val OP func.f1()) OP func.f2(); \
} \
else /*if constexpr (is_numeric<B> && !is_stored<B>) */ \
{ \
return func.f1() OP (val OP func.f2()); \
} \
} \
template <class T1, class A, class B, \
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> && \
(is_numeric<A> || is_numeric<B>) >* = nullptr> \
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator OP (const NAME<A, B>& func, const T1& val) \
{ \
if constexpr (is_numeric<A> && !is_stored<A>) \
{ \
return (func.f1() OP val) OP func.f2(); \
} \
else /*if constexpr (is_numeric<B> && !is_stored<B>) */ \
{ \
return func.f1() OP (func.f2() OP val); \
} \
} \
SIMBPOLIC_OPS_BINARY_OPER_SIMPLIFIER(+, func_add);
SIMBPOLIC_OPS_BINARY_OPER_SIMPLIFIER(*, func_mul);
template <class A, class B,
typename std::enable_if_t< is_numeric<A> || is_numeric<B> >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator + (const One& val, const func_add<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val + func.f1()) + func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val + func.f2()) + func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator - (const T1& val, const func_add<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val - func.f1()) - func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val - func.f2()) - func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator + (const T1& val, const func_sub<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val + func.f1()) - func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val - func.f2()) + func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator - (const T1& val, const func_sub<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val - func.f1()) + func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val + func.f2()) - func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const T1& val, const func_mul<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val / func.f1()) / func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val / func.f2()) / func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const T1& val, const func_div<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val / func.f1()) * func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val * func.f2()) / func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator * (const T1& val, const func_div<A, B>& func)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (val * func.f1()) / func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (val / func.f2()) * func.f1() ;
}
}
template <class A, class B,
typename std::enable_if_t< is_numeric<A> || is_numeric<B> >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator + (const func_add<A, B>& func, const One& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() + val) + func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return (func.f2() + val) + func.f1() ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator - (const func_add<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() - val) + func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() + (func.f2() - val);
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator + (const func_sub<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() + val) - func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() - (func.f2() - val);
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !std::is_same_v<T1, Zero> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator - (const func_sub<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() - val) - func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() - (func.f2() + val);
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const func_mul<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() / val) * func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() * (func.f2() / val) ;
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const func_div<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1()/val) / func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() / (func.f2() * val);
}
}
template <indexer n, indexer d, class A, class B,
typename std::enable_if_t< is_numeric<A> || is_numeric<B> >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const func_mul<A, B>& func, const Rational<n,d>& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() / val) * func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() * (func.f2() / val) ;
}
}
template <indexer n, indexer d, class A, class B,
typename std::enable_if_t< is_numeric<A> || is_numeric<B> >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const func_div<A, B>& func, const Rational<n,d>& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1()/val) / func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() / (func.f2() * val);
}
}
template <class T1, class A, class B,
typename std::enable_if_t<is_numeric<T1> && !is_exceptional<T1> && !is_op_func<T1> &&
(is_numeric<A> || is_numeric<B>) >* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator * (const func_div<A, B>& func, const T1& val)
{
if constexpr (is_numeric<A> && !is_stored<A>)
{
return (func.f1() * val) / func.f2();
}
else /*if constexpr (is_numeric<B> && !is_stored<B>) */
{
return func.f1() * (val / func.f2());
}
}
template <class T, typename std::enable_if_t<is_symbolic<T> && !holds_values<T>>* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator / (const T& t1, const T& t2)
{
return One{};
}
template <class T, typename std::enable_if_t<is_symbolic<T> && !holds_values<T>>* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator - (const T& t1, const T& t2)
{
return Zero{};
}
template <class T, indexer val, typename std::enable_if_t<!is_exceptional<T>>* = nullptr>
SIMBPOLIC_CUDA_HOS_DEV constexpr inline auto operator ^ (const T& base, const Intg<val>& exp)
{
if constexpr (val < 0)
{
return Intg<1>{} / (base ^ Intg<-val>{});
}
else if constexpr (val == 0)
{
return One{};
}
else if constexpr (val == 1)
{
return base;
}
else
{
if constexpr (val % 2 == 0)
{
const auto temp = base ^ Intg<val/2>{};
return temp * temp;
}
else
{
const auto temp = base ^ Intg<val - 1>{};
return base * temp;
}
}
}
//NOTE: operator ^ has lower precedence than the arithmetic operators.
//If using it to signify exponentiation,
//wrap the thing in parenthesis.
//(But it's still shorter than using pow<num>(thing)...)
//And now some hacks follow for us to be able to complete the project in time.
//A more complete treatment of piecewise functions would make them unnecessary,
//but that would mean we need to take into account integration constants
//and solve for the equality of the integrated function at the (possible) discontinuities.
//Worse, we'd need to support essentially arbitrary conditions (or, at the very least, linear ones),
//which would be much more complicated...
}
//#undef SIMBPOLIC_CUDA_AVAILABLE
//#undef SIMBPOLIC_CUDA_HOS_DEV
//#undef SIMBPOLIC_CUDA_ONLY_DEV
//#undef SIMBPOLIC_CUDA_ONLY_HOS
#undef SIMBPOLIC_OPS_BINARY_OPER
#undef SIMBPOLIC_OPS_BINARY_OPER_SIMPLIFIER
#undef SIMBPOLIC_OPS_OPERATORS
#endif