-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkul.hpp
2467 lines (2183 loc) · 76 KB
/
kul.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
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
#pragma once
/* The Kokkos-ified Units Library
*
* This is a header-only C++17 library based on Kokkos:
* https://github.com/kokkos/kokkos
* This library provides C++ classes to represent physical units
* and physical quantities (floating-point quantities which have
* physical units).
* What distinguishes KUL from similar projects is that it is
* based on Kokkos so the compile-time-unit quantities can be used
* on all hardware Kokkos supports including NVIDIA and AMD GPUs
* inside CUDA and HIP kernels.
*/
#include <string>
#include <memory>
#include <vector>
#include <type_traits>
#include <Kokkos_Core.hpp>
#include <Kokkos_SIMD.hpp>
namespace kul {
// Section [rational]: constexpr-compatible "runtime" rational number type
KOKKOS_INLINE_FUNCTION constexpr std::int64_t abs(std::int64_t a)
{
return (a < 0) ? -a : a;
}
KOKKOS_INLINE_FUNCTION constexpr std::int64_t gcd(std::int64_t a, std::int64_t b)
{
while (b != 0) {
auto const t = b;
b = a % b;
a = t;
}
return a;
}
class rational {
std::int64_t m_numerator{0};
std::int64_t m_denominator{1};
public:
KOKKOS_INLINE_FUNCTION constexpr rational(std::int64_t numerator_arg, std::int64_t denominator_arg)
{
auto const abs_num_arg = kul::abs(numerator_arg);
auto const abs_den_arg = kul::abs(denominator_arg);
auto const common = kul::gcd(abs_num_arg, abs_den_arg);
auto const abs_num = abs_num_arg / common;
auto const abs_den = abs_den_arg / common;
auto const is_negative = (!(numerator_arg < 0)) != (!(denominator_arg < 0));
m_numerator = is_negative ? -abs_num : abs_num;
m_denominator = abs_den;
}
KOKKOS_INLINE_FUNCTION constexpr rational(std::int64_t numerator_arg)
:rational(numerator_arg, 1)
{
}
constexpr rational() = default;
KOKKOS_INLINE_FUNCTION constexpr std::int64_t numerator() const
{
return m_numerator;
}
KOKKOS_INLINE_FUNCTION constexpr std::int64_t denominator() const
{
return m_denominator;
}
template <class T>
KOKKOS_INLINE_FUNCTION constexpr T convert_to() const
{
return T(m_numerator) / T(m_denominator);
}
};
KOKKOS_INLINE_FUNCTION constexpr rational inverse(rational const& a)
{
return rational(a.denominator(), a.numerator());
}
KOKKOS_INLINE_FUNCTION constexpr rational operator*(rational const& a, rational const& b)
{
return rational(a.numerator() * b.numerator(), a.denominator() * b.denominator());
}
KOKKOS_INLINE_FUNCTION constexpr rational operator/(rational const& a, rational const& b)
{
return a * inverse(b);
}
KOKKOS_INLINE_FUNCTION constexpr bool operator==(rational const& a, rational const& b)
{
return (a.numerator() == b.numerator()) && (a.denominator() == b.denominator());
}
KOKKOS_INLINE_FUNCTION constexpr bool operator!=(rational const& a, rational const& b)
{
return !operator==(a, b);
}
KOKKOS_INLINE_FUNCTION constexpr rational pow(rational const& b, int e)
{
rational result{1};
for (int i = 0; i < e; ++i) {
result = result * b;
}
for (int i = 0; i < -e; ++i) {
result = result / b;
}
return result;
}
// Section [dimension]: constexpr-compatible "runtime" SI dimension type
class dimension {
int m_time_exponent;
int m_length_exponent;
int m_mass_exponent;
int m_electric_current_exponent;
int m_temperature_exponent;
int m_amount_of_substance_exponent;
int m_luminous_intensity_exponent;
public:
KOKKOS_INLINE_FUNCTION constexpr dimension(
int time_exponent_arg,
int length_exponent_arg,
int mass_exponent_arg,
int electric_current_exponent_arg = 0,
int temperature_exponent_arg = 0,
int amount_of_substance_exponent_arg = 0,
int luminous_intensity_arg = 0)
:m_time_exponent(time_exponent_arg)
,m_length_exponent(length_exponent_arg)
,m_mass_exponent(mass_exponent_arg)
,m_electric_current_exponent(electric_current_exponent_arg)
,m_temperature_exponent(temperature_exponent_arg)
,m_amount_of_substance_exponent(amount_of_substance_exponent_arg)
,m_luminous_intensity_exponent(luminous_intensity_arg)
{
}
KOKKOS_INLINE_FUNCTION constexpr int time_exponent() const
{
return m_time_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int length_exponent() const
{
return m_length_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int mass_exponent() const
{
return m_mass_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int electric_current_exponent() const
{
return m_electric_current_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int temperature_exponent() const
{
return m_temperature_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int amount_of_substance_exponent() const
{
return m_amount_of_substance_exponent;
}
KOKKOS_INLINE_FUNCTION constexpr int luminous_intensity_exponent() const
{
return m_luminous_intensity_exponent;
}
};
KOKKOS_INLINE_FUNCTION constexpr dimension dimension_one()
{
return dimension(0, 0, 0);
}
KOKKOS_INLINE_FUNCTION constexpr dimension operator*(dimension const& a, dimension const& b)
{
return dimension(
a.time_exponent() + b.time_exponent(),
a.length_exponent() + b.length_exponent(),
a.mass_exponent() + b.mass_exponent(),
a.electric_current_exponent() + b.electric_current_exponent(),
a.temperature_exponent() + b.temperature_exponent(),
a.amount_of_substance_exponent() + b.amount_of_substance_exponent(),
a.luminous_intensity_exponent() + b.luminous_intensity_exponent());
}
KOKKOS_INLINE_FUNCTION constexpr dimension operator/(dimension const& a, dimension const& b)
{
return dimension(
a.time_exponent() - b.time_exponent(),
a.length_exponent() - b.length_exponent(),
a.mass_exponent() - b.mass_exponent(),
a.electric_current_exponent() - b.electric_current_exponent(),
a.temperature_exponent() - b.temperature_exponent(),
a.amount_of_substance_exponent() - b.amount_of_substance_exponent(),
a.luminous_intensity_exponent() - b.luminous_intensity_exponent());
}
KOKKOS_INLINE_FUNCTION constexpr dimension pow(dimension const& d, int e)
{
return dimension(
d.time_exponent() * e,
d.length_exponent() * e,
d.mass_exponent() * e,
d.electric_current_exponent() * e,
d.temperature_exponent() * e,
d.amount_of_substance_exponent() * e,
d.luminous_intensity_exponent() * e);
}
KOKKOS_INLINE_FUNCTION constexpr bool operator==(dimension const& a, dimension const& b)
{
return (a.time_exponent() == b.time_exponent()) &&
(a.length_exponent() == b.length_exponent()) &&
(a.mass_exponent() == b.mass_exponent()) &&
(a.electric_current_exponent() == b.electric_current_exponent()) &&
(a.temperature_exponent() == b.temperature_exponent()) &&
(a.amount_of_substance_exponent() == b.amount_of_substance_exponent()) &&
(a.luminous_intensity_exponent() == b.luminous_intensity_exponent());
}
KOKKOS_INLINE_FUNCTION constexpr bool operator!=(dimension const& a, dimension const& b)
{
return !operator==(a, b);
}
// Section [named dimension]: commonly referred-to dimensions
KOKKOS_INLINE_FUNCTION constexpr dimension time()
{
return dimension(1, 0, 0);
}
KOKKOS_INLINE_FUNCTION constexpr dimension length()
{
return dimension(0, 1, 0);
}
KOKKOS_INLINE_FUNCTION constexpr dimension mass()
{
return dimension(0, 0, 1);
}
KOKKOS_INLINE_FUNCTION constexpr dimension electric_current()
{
return dimension(0, 0, 0, 1);
}
KOKKOS_INLINE_FUNCTION constexpr dimension temperature()
{
return dimension(0, 0, 0, 0, 1);
}
KOKKOS_INLINE_FUNCTION constexpr dimension amount_of_substance()
{
return dimension(0, 0, 0, 0, 0, 1);
}
KOKKOS_INLINE_FUNCTION constexpr dimension luminous_intensity()
{
return dimension(0, 0, 0, 0, 0, 0, 1);
}
KOKKOS_INLINE_FUNCTION constexpr dimension area()
{
return length() * length();
}
KOKKOS_INLINE_FUNCTION constexpr dimension volume()
{
return area() * length();
}
KOKKOS_INLINE_FUNCTION constexpr dimension speed()
{
return length() / time();
}
KOKKOS_INLINE_FUNCTION constexpr dimension acceleration()
{
return speed() / time();
}
KOKKOS_INLINE_FUNCTION constexpr dimension force()
{
return mass() * acceleration();
}
KOKKOS_INLINE_FUNCTION constexpr dimension momentum()
{
return mass() * speed();
}
KOKKOS_INLINE_FUNCTION constexpr dimension energy()
{
return force() * length();
}
KOKKOS_INLINE_FUNCTION constexpr dimension power()
{
return energy() / time();
}
KOKKOS_INLINE_FUNCTION constexpr dimension pressure()
{
return force() / area();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electric_charge()
{
return electric_current() * time();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electric_potential()
{
return energy() / electric_charge();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electrical_resistance()
{
return electric_potential() / electric_current();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electrical_conductance()
{
return dimension_one() / electrical_resistance();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electrical_resistivity()
{
return electrical_resistance() * length();
}
KOKKOS_INLINE_FUNCTION constexpr dimension electrical_conductivity()
{
return dimension_one() / electrical_resistivity();
}
KOKKOS_INLINE_FUNCTION constexpr dimension capacitance()
{
return electric_charge() / electric_potential();
}
KOKKOS_INLINE_FUNCTION constexpr dimension inductance()
{
return electric_potential() / (electric_current() / time());
}
// Section [optiona]: constexpr-compatible and Kokkosified version of std::optional<T>
class nullopt_t {};
inline constexpr nullopt_t nullopt = {};
// This class always has the value alive because things like placement new don't work
// in a constexpr context, so it is limited in usefulness to trivial types
template <class T>
class optional
{
bool m_has_value{false};
T m_value;
public:
KOKKOS_INLINE_FUNCTION constexpr optional(nullopt_t)
{
}
KOKKOS_INLINE_FUNCTION constexpr optional(T const& value)
{
m_value = T(value);
m_has_value = true;
}
KOKKOS_INLINE_FUNCTION constexpr bool has_value() const
{
return m_has_value;
}
KOKKOS_INLINE_FUNCTION constexpr T& value()
{
return m_value;
}
KOKKOS_INLINE_FUNCTION constexpr T const& value() const
{
return m_value;
}
};
template <class T>
KOKKOS_INLINE_FUNCTION constexpr bool operator==(optional<T> const& a, optional<T> const& b)
{
if ((!a.has_value()) && (!b.has_value())) {
return true;
}
if (a.has_value() && b.has_value()) {
return a.value() == b.value();
}
return false;
}
// Section [unit]: virtual base, derived Curiously Recurring Template Pattern classes for physical unit types,
class unit {
public:
virtual ~unit() = default;
virtual std::string name() const = 0;
virtual kul::dimension dimension() const = 0;
virtual rational magnitude() const = 0;
virtual optional<rational> origin() const = 0;
virtual std::unique_ptr<unit> copy() const = 0;
virtual std::unique_ptr<unit> simplify() const = 0;
};
inline bool operator==(unit const& a, unit const& b)
{
return a.dimension() == b.dimension() &&
a.magnitude() == b.magnitude() &&
a.origin() == b.origin();
}
inline bool operator!=(unit const& a, unit const& b)
{
return !operator==(a, b);
}
class named : public unit {
public:
};
template <class Unit>
class crtp : public named {
public:
std::string name() const override
{
return Unit::static_name();
}
kul::dimension dimension() const override
{
return Unit::static_dimension();
}
rational magnitude() const override
{
return Unit::static_magnitude();
}
optional<rational> origin() const override
{
return Unit::static_origin();
}
std::unique_ptr<unit> copy() const override
{
return std::make_unique<Unit>();
}
std::unique_ptr<unit> simplify() const override
{
return this->copy();
}
};
class unit_one : public crtp<unit_one> {
public:
static std::string static_name() { return "1"; }
KOKKOS_INLINE_FUNCTION static constexpr kul::dimension static_dimension() { return kul::dimension_one(); }
KOKKOS_INLINE_FUNCTION static constexpr rational static_magnitude() { return rational(1); }
KOKKOS_INLINE_FUNCTION static constexpr optional<rational> static_origin() { return nullopt; }
};
// Section [dynamic]: classes for runtime representation of derived units
class dynamic_unit : public unit {
std::unique_ptr<unit> m_pointer;
public:
dynamic_unit() = default;
dynamic_unit(unit const& u)
:m_pointer(u.copy())
{
}
dynamic_unit(std::unique_ptr<unit>&& ptr)
:m_pointer(std::move(ptr))
{
}
dynamic_unit(std::unique_ptr<unit> const& ptr)
:m_pointer(ptr->copy())
{
}
dynamic_unit(dynamic_unit&&) = default;
dynamic_unit& operator=(dynamic_unit&&) = default;
dynamic_unit(dynamic_unit const& other)
:m_pointer(other ? other.m_pointer->copy() : std::unique_ptr<unit>())
{
}
dynamic_unit& operator=(dynamic_unit const& other)
{
m_pointer = other.m_pointer->copy();
return *this;
}
std::string name() const override
{
return m_pointer->name();
}
kul::dimension dimension() const override
{
return m_pointer->dimension();
}
rational magnitude() const override
{
return m_pointer->magnitude();
}
optional<rational> origin() const override
{
return m_pointer->origin();
}
std::unique_ptr<unit> copy() const override
{
return m_pointer->copy();
}
std::unique_ptr<unit> simplify() const override
{
return m_pointer->simplify();
}
unit const* pointer() const
{
return m_pointer.get();
}
unit* pointer()
{
return m_pointer.get();
}
bool is_unitless() const
{
return dynamic_cast<unit_one const*>(m_pointer.get()) != nullptr;
}
explicit operator bool() const
{
return static_cast<bool>(m_pointer);
}
};
class dynamic_exp : public unit {
dynamic_unit m_base;
int m_exponent;
public:
dynamic_exp(dynamic_unit base_arg, int exponent_arg)
:m_base(base_arg)
,m_exponent(exponent_arg)
{
}
std::string name() const override
{
return m_base.name() + "^" + std::to_string(m_exponent);
}
kul::dimension dimension() const override
{
return kul::pow(m_base.dimension(), m_exponent);
}
rational magnitude() const override
{
return kul::pow(m_base.magnitude(), m_exponent);
}
optional<rational> origin() const override
{
return nullopt;
}
std::unique_ptr<unit> copy() const override
{
return std::make_unique<dynamic_exp>(*this);
}
std::unique_ptr<unit> simplify() const override
{
if (m_exponent == 0) return unit_one().copy();
if (m_exponent == 1) return m_base.copy();
return copy();
}
dynamic_unit const& base() const
{
return m_base;
}
int exponent() const
{
return m_exponent;
}
};
class dynamic_product : public unit {
std::vector<dynamic_unit> m_terms;
public:
void push_back(dynamic_unit const& term)
{
m_terms.push_back(term);
}
void push_back_unless_unitless(dynamic_unit const& term)
{
if (dynamic_cast<unit_one const*>(term.pointer()) == nullptr) {
push_back(term);
}
}
void multiply_with(dynamic_exp const& new_exp)
{
for (auto& existing_any : m_terms) {
auto& existing_exp = dynamic_cast<dynamic_exp&>(*(existing_any.pointer()));
if (existing_exp.base() == new_exp.base()) {
existing_exp = dynamic_exp(new_exp.base(),
existing_exp.exponent() + new_exp.exponent());
return;
}
}
push_back(new_exp);
}
void divide_by(dynamic_exp const& new_exp)
{
multiply_with(dynamic_exp(new_exp.base(), -new_exp.exponent()));
}
void multiply_with(named const& new_named)
{
multiply_with(dynamic_exp(new_named, 1));
}
void divide_by(named const& new_named)
{
divide_by(dynamic_exp(new_named, 1));
}
void multiply_with(dynamic_product const& other_product)
{
for (auto& term : other_product.m_terms) {
multiply_with(term);
}
}
void divide_by(dynamic_product const& other_product)
{
for (auto& term : other_product.m_terms) {
divide_by(term);
}
}
void multiply_with(dynamic_unit const& new_unit)
{
auto ptr = new_unit.pointer();
auto product_ptr = dynamic_cast<dynamic_product const*>(ptr);
if (product_ptr) {
multiply_with(*product_ptr);
return;
}
auto exp_ptr = dynamic_cast<dynamic_exp const*>(ptr);
if (exp_ptr) {
multiply_with(*exp_ptr);
return;
}
auto named_ptr = dynamic_cast<named const*>(ptr);
if (named_ptr) {
multiply_with(*named_ptr);
return;
}
}
void divide_by(dynamic_unit const& new_unit)
{
auto ptr = new_unit.pointer();
auto product_ptr = dynamic_cast<dynamic_product const*>(ptr);
if (product_ptr) {
divide_by(*product_ptr);
return;
}
auto exp_ptr = dynamic_cast<dynamic_exp const*>(ptr);
if (exp_ptr) {
divide_by(*exp_ptr);
return;
}
auto named_ptr = dynamic_cast<named const*>(ptr);
if (named_ptr) {
divide_by(*named_ptr);
return;
}
}
std::string name() const override
{
auto it = m_terms.begin();
auto result = it->name();
++it;
while (it != m_terms.end()) {
result += " * ";
result += it->name();
++it;
}
return result;
}
kul::dimension dimension() const override
{
auto it = m_terms.begin();
auto result = it->dimension();
++it;
while (it != m_terms.end()) {
result = result * it->dimension();
++it;
}
return result;
}
rational magnitude() const override
{
auto it = m_terms.begin();
auto result = it->magnitude();
++it;
while (it != m_terms.end()) {
result = result * it->magnitude();
++it;
}
return result;
}
optional<rational> origin() const override
{
return nullopt;
}
std::unique_ptr<unit> copy() const override
{
return std::make_unique<dynamic_product>(*this);
}
std::unique_ptr<unit> simplify() const override
{
auto result = dynamic_product();
for (auto& u : m_terms) {
result.push_back_unless_unitless(u.simplify());
}
if (result.m_terms.empty()) return unit_one().copy();
if (result.m_terms.size() == 1) return result.m_terms.front().copy();
return result.copy();
}
std::vector<dynamic_unit> const& terms() const
{
return m_terms;
}
};
inline dynamic_unit operator*(dynamic_unit const& a, dynamic_unit const& b)
{
dynamic_product p;
p.multiply_with(a);
p.multiply_with(b);
return p.simplify();
}
inline dynamic_unit operator/(dynamic_unit const& a, dynamic_unit const& b)
{
dynamic_product p;
p.multiply_with(a);
p.divide_by(b);
return p.simplify();
}
inline dynamic_unit& operator*=(dynamic_unit& a, dynamic_unit const& b)
{
return a = a * b;
}
inline dynamic_unit& operator/=(dynamic_unit& a, dynamic_unit const& b)
{
return a = a / b;
}
inline dynamic_unit root(dynamic_unit const& base, int exponent)
{
auto ptr = base.pointer();
auto named_ptr = dynamic_cast<named const*>(ptr);
if (named_ptr) {
throw std::runtime_error("cannot take " + std::to_string(exponent) + "th root of named unit");
}
auto exp_ptr = dynamic_cast<dynamic_exp const*>(ptr);
if (exp_ptr) {
if (exp_ptr->exponent() % exponent != 0) {
throw std::runtime_error("taking " + std::to_string(exponent) + "th root of non-divisible "
+ std::to_string(exp_ptr->exponent()) + "th power of " + exp_ptr->base().name());
}
return dynamic_exp(exp_ptr->base(), exp_ptr->exponent() / exponent).simplify();
}
auto product_ptr = dynamic_cast<dynamic_product const*>(ptr);
if (product_ptr) {
auto result = dynamic_product();
for (auto& term : product_ptr->terms()) {
result.multiply_with(root(term, exponent));
}
return result.simplify();
}
throw std::logic_error("unexpected type");
}
inline dynamic_unit sqrt(dynamic_unit const& base)
{
return root(base, 2);
}
inline dynamic_unit cbrt(dynamic_unit const& base)
{
return root(base, 3);
}
// Section [static]: Compile-time implementations of derived unit operations
template <class Base, int Exponent>
class static_pow : public crtp<static_pow<Base, Exponent>> {
public:
static std::string static_name() { return Base::static_name() + "^" + std::to_string(Exponent); }
KOKKOS_INLINE_FUNCTION static constexpr kul::dimension static_dimension() { return kul::pow(Base::static_dimension(), Exponent); }
KOKKOS_INLINE_FUNCTION static constexpr rational static_magnitude() { return kul::pow(Base::static_magnitude(), Exponent); }
KOKKOS_INLINE_FUNCTION static constexpr optional<rational> static_origin() { return nullopt; }
std::unique_ptr<unit> copy() const override
{
return dynamic_exp(Base(), Exponent).simplify();
}
};
template <class... Units>
class static_product;
template <>
class static_product<> : public crtp<static_product<>> {
public:
static std::string static_name() { return "1"; }
KOKKOS_INLINE_FUNCTION static constexpr kul::dimension static_dimension() { return kul::dimension_one(); }
KOKKOS_INLINE_FUNCTION static constexpr rational static_magnitude() { return kul::rational(1); }
KOKKOS_INLINE_FUNCTION static constexpr optional<rational> static_origin() { return nullopt; }
std::unique_ptr<unit> copy() const override
{
return dynamic_product().simplify();
}
};
template <class Unit>
class static_product<Unit> : public crtp<static_product<Unit>> {
public:
static std::string static_name() { return Unit::static_name(); }
KOKKOS_INLINE_FUNCTION static constexpr kul::dimension static_dimension() { return Unit::static_dimension(); }
KOKKOS_INLINE_FUNCTION static constexpr rational static_magnitude() { return Unit::static_magnitude(); }
KOKKOS_INLINE_FUNCTION static constexpr optional<rational> static_origin() { return nullopt; }
std::unique_ptr<unit> copy() const override
{
auto p = dynamic_product();
p.multiply_with(Unit());
return p.simplify();
}
};
template <class FirstUnit, class... OtherUnits>
class static_product<FirstUnit, OtherUnits...> : public crtp<static_product<FirstUnit, OtherUnits...>> {
public:
using tail_type = static_product<OtherUnits...>;
static std::string static_name()
{
return FirstUnit::static_name() + " * " + tail_type::static_name();
}
KOKKOS_INLINE_FUNCTION static constexpr kul::dimension static_dimension()
{
return FirstUnit::static_dimension() * tail_type::static_dimension();
}
KOKKOS_INLINE_FUNCTION static constexpr rational static_magnitude()
{
return FirstUnit::static_magnitude() * tail_type::static_magnitude();
}
KOKKOS_INLINE_FUNCTION static constexpr optional<rational> static_origin() { return nullopt; }
std::unique_ptr<unit> copy() const override
{
auto p = dynamic_product();
p.multiply_with(FirstUnit());
p.multiply_with(tail_type());
return p.simplify();
}
};
template <class A, class B>
class push_back;
template <class... Units, class LastUnit>
class push_back<static_product<Units...>, LastUnit> {
public:
using type = static_product<Units..., LastUnit>;
};
template <class A, class B>
using push_back_t = typename push_back<A, B>::type;
template <class A, class B>
class prepend;
template <class FirstUnit, class... Units>
class prepend<FirstUnit, static_product<Units...>> {
public:
using type = static_product<FirstUnit, Units...>;
};
template <class A, class B>
using prepend_t = typename prepend<A, B>::type;
template <class A, class B>
class push_back_unless_unitless {
public:
using type = push_back_t<A, B>;
};
template <class A>
class push_back_unless_unitless<A, unit_one> {
public:
using type = A;
};
template <class A, class B>
using push_back_unless_unitless_t = typename push_back_unless_unitless<A, B>::type;
template <class A, class B>
class prepend_unless_unitless {
public:
using type = prepend_t<A, B>;
};
template <class B>
class prepend_unless_unitless<unit_one, B> {
public:
using type = B;
};
template <class A, class B>
using prepend_unless_unitless_t = typename prepend_unless_unitless<A, B>::type;
template <class A, class B>
class multiply_with {
public:
using type = typename multiply_with<A, static_pow<B, 1>>::type;
};
template <class Base, int Exponent>
class multiply_with<static_product<>, static_pow<Base, Exponent>> {
public:
using type = static_product<static_pow<Base, Exponent>>;
};
template <class FirstUnit, class... NextUnits, class Base, int Exponent>
class multiply_with<static_product<FirstUnit, NextUnits...>, static_pow<Base, Exponent>> {
public:
using type = prepend_t<FirstUnit, typename multiply_with<static_product<NextUnits...>, static_pow<Base, Exponent>>::type>;
};
template <class... NextUnits, class Base, int Exponent1, int Exponent2>
class multiply_with<static_product<static_pow<Base, Exponent1>, NextUnits...>, static_pow<Base, Exponent2>> {
public:
using type = prepend_t<static_pow<Base, Exponent1 + Exponent2>, static_product<NextUnits...>>;
};
template <class LHS>
class multiply_with<LHS, static_product<>> {
public:
using type = LHS;
};
template <class LHS, class FirstUnit, class... NextUnits>
class multiply_with<LHS, static_product<FirstUnit, NextUnits...>> {
using product_with_first = typename multiply_with<LHS, FirstUnit>::type;
public:
using type = typename multiply_with<product_with_first, static_product<NextUnits...>>::type;
};
template <class A, class B>
using multiply_with_t = typename multiply_with<A, B>::type;
template <class A, class B>
class divide_by {
public:
using type = typename divide_by<A, static_pow<B, 1>>::type;
};
template <class Base, int Exponent>
class divide_by<static_product<>, static_pow<Base, Exponent>> {
public:
using type = static_product<static_pow<Base, -Exponent>>;
};
template <class FirstUnit, class... NextUnits, class Base, int Exponent>
class divide_by<static_product<FirstUnit, NextUnits...>, static_pow<Base, Exponent>> {
public:
using type = prepend_t<FirstUnit, typename divide_by<static_product<NextUnits...>, static_pow<Base, Exponent>>::type>;
};
template <class... NextUnits, class Base, int Exponent1, int Exponent2>
class divide_by<static_product<static_pow<Base, Exponent1>, NextUnits...>, static_pow<Base, Exponent2>> {
public:
using type = prepend_t<static_pow<Base, Exponent1 - Exponent2>, static_product<NextUnits...>>;
};
template <class LHS>
class divide_by<LHS, static_product<>> {
public:
using type = LHS;
};
template <class LHS, class FirstUnit, class... NextUnits>
class divide_by<LHS, static_product<FirstUnit, NextUnits...>> {
using product_with_first = typename divide_by<LHS, FirstUnit>::type;
public:
using type = typename divide_by<product_with_first, static_product<NextUnits...>>::type;
};
template <class A, class B>
using divide_by_t = typename divide_by<A, B>::type;
template <class T>
class simplify {
public:
using type = T;
};