-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathxmath.hpp
More file actions
3334 lines (3049 loc) · 122 KB
/
Copy pathxmath.hpp
File metadata and controls
3334 lines (3049 loc) · 122 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
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
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
/**
* @brief standard mathematical functions for xexpressions
*/
#ifndef XTENSOR_MATH_HPP
#define XTENSOR_MATH_HPP
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <type_traits>
#include <xtl/xcomplex.hpp>
#include <xtl/xsequence.hpp>
#include <xtl/xtype_traits.hpp>
#include "../core/xeval.hpp"
#include "../core/xoperation.hpp"
#include "../core/xtensor_config.hpp"
#include "../misc/xmanipulation.hpp"
#include "../reducers/xaccumulator.hpp"
#include "../reducers/xreducer.hpp"
#include "../views/xslice.hpp"
#include "../views/xstrided_view.hpp"
namespace xt
{
template <class T = double>
struct numeric_constants
{
static constexpr T PI = static_cast<T>(3.141592653589793238463);
static constexpr T PI_2 = static_cast<T>(1.57079632679489661923);
static constexpr T PI_4 = static_cast<T>(0.785398163397448309616);
static constexpr T D_1_PI = static_cast<T>(0.318309886183790671538);
static constexpr T D_2_PI = static_cast<T>(0.636619772367581343076);
static constexpr T D_2_SQRTPI = static_cast<T>(1.12837916709551257390);
static constexpr T SQRT2 = static_cast<T>(1.41421356237309504880);
static constexpr T SQRT1_2 = static_cast<T>(0.707106781186547524401);
static constexpr T E = static_cast<T>(2.71828182845904523536);
static constexpr T LOG2E = static_cast<T>(1.44269504088896340736);
static constexpr T LOG10E = static_cast<T>(0.434294481903251827651);
static constexpr T LN2 = static_cast<T>(0.693147180559945309417);
};
/***********
* Helpers *
***********/
#define XTENSOR_UNSIGNED_ABS_FUNC(T) \
constexpr inline T abs(const T& x) \
{ \
return x; \
}
#define XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, T) \
constexpr inline bool FUNC_NAME(const T& /*x*/) noexcept \
{ \
return RETURN_VAL; \
}
#define XTENSOR_INT_SPECIALIZATION(FUNC_NAME, RETURN_VAL) \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, char); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, short); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, int); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, long); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, long long); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned char); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned short); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned int); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned long); \
XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned long long);
#define XTENSOR_UNARY_MATH_FUNCTOR(NAME) \
struct NAME##_fun \
{ \
template <class T> \
constexpr auto operator()(const T& arg) const \
{ \
using math::NAME; \
return NAME(arg); \
} \
template <class B> \
constexpr auto simd_apply(const B& arg) const \
{ \
using math::NAME; \
return NAME(arg); \
} \
}
#define XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(NAME) \
struct NAME##_fun \
{ \
template <class T> \
constexpr auto operator()(const T& arg) const \
{ \
using math::NAME; \
return NAME(arg); \
} \
template <class B> \
constexpr auto simd_apply(const B& arg) const \
{ \
using math::NAME; \
return NAME(arg); \
} \
}
#define XTENSOR_BINARY_MATH_FUNCTOR(NAME) \
struct NAME##_fun \
{ \
template <class T1, class T2> \
constexpr auto operator()(const T1& arg1, const T2& arg2) const \
{ \
using math::NAME; \
return NAME(arg1, arg2); \
} \
template <class B> \
constexpr auto simd_apply(const B& arg1, const B& arg2) const \
{ \
using math::NAME; \
return NAME(arg1, arg2); \
} \
}
#define XTENSOR_TERNARY_MATH_FUNCTOR(NAME) \
struct NAME##_fun \
{ \
template <class T1, class T2, class T3> \
constexpr auto operator()(const T1& arg1, const T2& arg2, const T3& arg3) const \
{ \
using math::NAME; \
return NAME(arg1, arg2, arg3); \
} \
template <class B> \
auto simd_apply(const B& arg1, const B& arg2, const B& arg3) const \
{ \
using math::NAME; \
return NAME(arg1, arg2, arg3); \
} \
}
namespace math
{
using std::abs;
using std::fabs;
using std::acos;
using std::asin;
using std::atan;
using std::cos;
using std::sin;
using std::tan;
using std::acosh;
using std::asinh;
using std::atanh;
using std::cosh;
using std::sinh;
using std::tanh;
using std::cbrt;
using std::sqrt;
using std::exp;
using std::exp2;
using std::expm1;
using std::ilogb;
using std::log;
using std::log10;
using std::log1p;
using std::log2;
using std::logb;
using std::ceil;
using std::floor;
using std::llround;
using std::lround;
using std::nearbyint;
using std::remainder;
using std::rint;
using std::round;
using std::trunc;
using std::erf;
using std::erfc;
using std::lgamma;
using std::tgamma;
using std::arg;
using std::conj;
using std::imag;
using std::real;
using std::atan2;
// copysign is not in the std namespace for MSVC
#if !defined(_MSC_VER)
using std::copysign;
#endif
using std::fdim;
using std::fmax;
using std::fmin;
using std::fmod;
using std::hypot;
using std::pow;
using std::fma;
using std::fpclassify;
// Overload isinf, isnan and isfinite because glibc implementation
// might return int instead of bool and the SIMD detection requires
// bool return type.
template <class T>
inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isinf(const T& t)
{
return bool(std::isinf(t));
}
template <class T>
inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isnan(const T& t)
{
return bool(std::isnan(t));
}
template <class T>
inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isfinite(const T& t)
{
return bool(std::isfinite(t));
}
// Overload isinf, isnan and isfinite for complex datatypes,
// following the Python specification:
template <class T>
inline bool isinf(const std::complex<T>& c)
{
return std::isinf(std::real(c)) || std::isinf(std::imag(c));
}
template <class T>
inline bool isnan(const std::complex<T>& c)
{
return std::isnan(std::real(c)) || std::isnan(std::imag(c));
}
template <class T>
inline bool isfinite(const std::complex<T>& c)
{
return !isinf(c) && !isnan(c);
}
// VS2015 STL defines isnan, isinf and isfinite as template
// functions, breaking ADL.
#if defined(_WIN32) && defined(XTENSOR_USE_XSIMD)
/*template <class T, class A>
inline xsimd::batch_bool<T, A> isinf(const xsimd::batch<T, A>& b)
{
return xsimd::isinf(b);
}
template <class T, class A>
inline xsimd::batch_bool<T, A> isnan(const xsimd::batch<T, A>& b)
{
return xsimd::isnan(b);
}
template <class T, class A>
inline xsimd::batch_bool<T, A> isfinite(const xsimd::batch<T, A>& b)
{
return xsimd::isfinite(b);
}*/
#endif
// The following specializations are needed to avoid 'ambiguous overload' errors,
// whereas 'unsigned char' and 'unsigned short' are automatically converted to 'int'.
// we're still adding those functions to silence warnings
XTENSOR_UNSIGNED_ABS_FUNC(unsigned char)
XTENSOR_UNSIGNED_ABS_FUNC(unsigned short)
XTENSOR_UNSIGNED_ABS_FUNC(unsigned int)
XTENSOR_UNSIGNED_ABS_FUNC(unsigned long)
XTENSOR_UNSIGNED_ABS_FUNC(unsigned long long)
#ifdef _WIN32
XTENSOR_INT_SPECIALIZATION(isinf, false);
XTENSOR_INT_SPECIALIZATION(isnan, false);
XTENSOR_INT_SPECIALIZATION(isfinite, true);
#endif
XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(abs);
XTENSOR_UNARY_MATH_FUNCTOR(fabs);
XTENSOR_BINARY_MATH_FUNCTOR(fmod);
XTENSOR_BINARY_MATH_FUNCTOR(remainder);
XTENSOR_TERNARY_MATH_FUNCTOR(fma);
XTENSOR_BINARY_MATH_FUNCTOR(fmax);
XTENSOR_BINARY_MATH_FUNCTOR(fmin);
XTENSOR_BINARY_MATH_FUNCTOR(fdim);
XTENSOR_UNARY_MATH_FUNCTOR(exp);
XTENSOR_UNARY_MATH_FUNCTOR(exp2);
XTENSOR_UNARY_MATH_FUNCTOR(expm1);
XTENSOR_UNARY_MATH_FUNCTOR(log);
XTENSOR_UNARY_MATH_FUNCTOR(log10);
XTENSOR_UNARY_MATH_FUNCTOR(log2);
XTENSOR_UNARY_MATH_FUNCTOR(log1p);
XTENSOR_BINARY_MATH_FUNCTOR(pow);
XTENSOR_UNARY_MATH_FUNCTOR(sqrt);
XTENSOR_UNARY_MATH_FUNCTOR(cbrt);
XTENSOR_BINARY_MATH_FUNCTOR(hypot);
XTENSOR_UNARY_MATH_FUNCTOR(sin);
XTENSOR_UNARY_MATH_FUNCTOR(cos);
XTENSOR_UNARY_MATH_FUNCTOR(tan);
XTENSOR_UNARY_MATH_FUNCTOR(asin);
XTENSOR_UNARY_MATH_FUNCTOR(acos);
XTENSOR_UNARY_MATH_FUNCTOR(atan);
XTENSOR_BINARY_MATH_FUNCTOR(atan2);
XTENSOR_UNARY_MATH_FUNCTOR(sinh);
XTENSOR_UNARY_MATH_FUNCTOR(cosh);
XTENSOR_UNARY_MATH_FUNCTOR(tanh);
XTENSOR_UNARY_MATH_FUNCTOR(asinh);
XTENSOR_UNARY_MATH_FUNCTOR(acosh);
XTENSOR_UNARY_MATH_FUNCTOR(atanh);
XTENSOR_UNARY_MATH_FUNCTOR(erf);
XTENSOR_UNARY_MATH_FUNCTOR(erfc);
XTENSOR_UNARY_MATH_FUNCTOR(tgamma);
XTENSOR_UNARY_MATH_FUNCTOR(lgamma);
XTENSOR_UNARY_MATH_FUNCTOR(ceil);
XTENSOR_UNARY_MATH_FUNCTOR(floor);
XTENSOR_UNARY_MATH_FUNCTOR(trunc);
XTENSOR_UNARY_MATH_FUNCTOR(round);
XTENSOR_UNARY_MATH_FUNCTOR(nearbyint);
XTENSOR_UNARY_MATH_FUNCTOR(rint);
XTENSOR_UNARY_MATH_FUNCTOR(isfinite);
XTENSOR_UNARY_MATH_FUNCTOR(isinf);
XTENSOR_UNARY_MATH_FUNCTOR(isnan);
XTENSOR_UNARY_MATH_FUNCTOR(conj);
}
#undef XTENSOR_UNARY_MATH_FUNCTOR
#undef XTENSOR_BINARY_MATH_FUNCTOR
#undef XTENSOR_TERNARY_MATH_FUNCTOR
#undef XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING
#undef XTENSOR_UNSIGNED_ABS_FUNC
namespace detail
{
template <class R, class T>
std::enable_if_t<!has_iterator_interface<R>::value, R> fill_init(T init)
{
return R(init);
}
template <class R, class T>
std::enable_if_t<has_iterator_interface<R>::value, R> fill_init(T init)
{
R result;
std::fill(std::begin(result), std::end(result), init);
return result;
}
}
#define XTENSOR_REDUCER_FUNCTION(NAME, FUNCTOR, INIT_VALUE_TYPE, INIT) \
template < \
class T = void, \
class E, \
class X, \
class EVS = DEFAULT_STRATEGY_REDUCERS, \
XTL_REQUIRES(std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<std::decay_t<X>>>)> \
inline auto NAME(E&& e, X&& axes, EVS es = EVS()) \
{ \
using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
using functor_type = FUNCTOR; \
using init_value_fct = xt::const_value<init_value_type>; \
return xt::reduce( \
make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
std::forward<E>(e), \
std::forward<X>(axes), \
es \
); \
} \
\
template < \
class T = void, \
class E, \
class X, \
class EVS = DEFAULT_STRATEGY_REDUCERS, \
XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<std::decay_t<X>>)> \
inline auto NAME(E&& e, X axis, EVS es = EVS()) \
{ \
return NAME(std::forward<E>(e), {axis}, es); \
} \
\
template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)> \
inline auto NAME(E&& e, EVS es = EVS()) \
{ \
using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
using functor_type = FUNCTOR; \
using init_value_fct = xt::const_value<init_value_type>; \
return xt::reduce( \
make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
std::forward<E>(e), \
es \
); \
} \
\
template <class T = void, class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS> \
inline auto NAME(E&& e, const I(&axes)[N], EVS es = EVS()) \
{ \
using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
using functor_type = FUNCTOR; \
using init_value_fct = xt::const_value<init_value_type>; \
return xt::reduce( \
make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
std::forward<E>(e), \
axes, \
es \
); \
}
/*******************
* basic functions *
*******************/
/**
* @defgroup basic_functions Basic functions
*/
/**
* @ingroup basic_functions
* @brief Absolute value function.
*
* Returns an \ref xfunction for the element-wise absolute value
* of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto abs(E&& e) noexcept -> detail::xfunction_type_t<math::abs_fun, E>
{
return detail::make_xfunction<math::abs_fun>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Absolute value function.
*
* Returns an \ref xfunction for the element-wise absolute value
* of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto fabs(E&& e) noexcept -> detail::xfunction_type_t<math::fabs_fun, E>
{
return detail::make_xfunction<math::fabs_fun>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Remainder of the floating point division operation.
*
* Returns an \ref xfunction for the element-wise remainder of
* the floating point division operation <em>e1 / e2</em>.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
* @note e1 and e2 can't be both scalars.
*/
template <class E1, class E2>
inline auto fmod(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmod_fun, E1, E2>
{
return detail::make_xfunction<math::fmod_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Signed remainder of the division operation.
*
* Returns an \ref xfunction for the element-wise signed remainder
* of the floating point division operation <em>e1 / e2</em>.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
* @note e1 and e2 can't be both scalars.
*/
template <class E1, class E2>
inline auto remainder(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::remainder_fun, E1, E2>
{
return detail::make_xfunction<math::remainder_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Fused multiply-add operation.
*
* Returns an \ref xfunction for <em>e1 * e2 + e3</em> as if
* to infinite precision and rounded only once to fit the result type.
* @param e1 an \ref xfunction or a scalar
* @param e2 an \ref xfunction or a scalar
* @param e3 an \ref xfunction or a scalar
* @return an \ref xfunction
* @note e1, e2 and e3 can't be scalars every three.
*/
template <class E1, class E2, class E3>
inline auto fma(E1&& e1, E2&& e2, E3&& e3) noexcept -> detail::xfunction_type_t<math::fma_fun, E1, E2, E3>
{
return detail::make_xfunction<math::fma_fun>(
std::forward<E1>(e1),
std::forward<E2>(e2),
std::forward<E3>(e3)
);
}
/**
* @ingroup basic_functions
* @brief Maximum function.
*
* Returns an \ref xfunction for the element-wise maximum
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
* @note e1 and e2 can't be both scalars.
*/
template <class E1, class E2>
inline auto fmax(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmax_fun, E1, E2>
{
return detail::make_xfunction<math::fmax_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Minimum function.
*
* Returns an \ref xfunction for the element-wise minimum
* of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
* @note e1 and e2 can't be both scalars.
*/
template <class E1, class E2>
inline auto fmin(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmin_fun, E1, E2>
{
return detail::make_xfunction<math::fmin_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Positive difference function.
*
* Returns an \ref xfunction for the element-wise positive
* difference of \a e1 and \a e2.
* @param e1 an \ref xexpression or a scalar
* @param e2 an \ref xexpression or a scalar
* @return an \ref xfunction
* @note e1 and e2 can't be both scalars.
*/
template <class E1, class E2>
inline auto fdim(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fdim_fun, E1, E2>
{
return detail::make_xfunction<math::fdim_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
}
namespace math
{
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
#endif
template <class T = void>
struct minimum
{
template <class A1, class A2>
constexpr auto operator()(const A1& t1, const A2& t2) const noexcept
{
return xtl::select(t1 < t2, t1, t2);
}
template <class A1, class A2>
constexpr auto simd_apply(const A1& t1, const A2& t2) const noexcept
{
return xt_simd::select(t1 < t2, t1, t2);
}
};
template <class T = void>
struct maximum
{
template <class A1, class A2>
constexpr auto operator()(const A1& t1, const A2& t2) const noexcept
{
return xtl::select(t1 > t2, t1, t2);
}
template <class A1, class A2>
constexpr auto simd_apply(const A1& t1, const A2& t2) const noexcept
{
return xt_simd::select(t1 > t2, t1, t2);
}
};
struct clamp_fun
{
template <class A1, class A2, class A3>
constexpr auto operator()(const A1& v, const A2& lo, const A3& hi) const
{
return xtl::select(v < lo, lo, xtl::select(hi < v, hi, v));
}
template <class A1, class A2, class A3>
constexpr auto simd_apply(const A1& v, const A2& lo, const A3& hi) const
{
return xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v));
}
};
struct deg2rad
{
template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
constexpr double operator()(const A& a) const noexcept
{
return a * xt::numeric_constants<double>::PI / 180.0;
}
template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
constexpr auto operator()(const A& a) const noexcept
{
return a * xt::numeric_constants<A>::PI / A(180.0);
}
template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
constexpr double simd_apply(const A& a) const noexcept
{
return a * xt::numeric_constants<double>::PI / 180.0;
}
template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
constexpr auto simd_apply(const A& a) const noexcept
{
return a * xt::numeric_constants<A>::PI / A(180.0);
}
};
struct rad2deg
{
template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
constexpr double operator()(const A& a) const noexcept
{
return a * 180.0 / xt::numeric_constants<double>::PI;
}
template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
constexpr auto operator()(const A& a) const noexcept
{
return a * A(180.0) / xt::numeric_constants<A>::PI;
}
template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
constexpr double simd_apply(const A& a) const noexcept
{
return a * 180.0 / xt::numeric_constants<double>::PI;
}
template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
constexpr auto simd_apply(const A& a) const noexcept
{
return a * A(180.0) / xt::numeric_constants<A>::PI;
}
};
}
/**
* @ingroup basic_functions
* @brief Convert angles from degrees to radians.
*
* Returns an \ref xfunction for the element-wise corresponding
* angle in radians of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto deg2rad(E&& e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>
{
return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Convert angles from degrees to radians.
*
* Returns an \ref xfunction for the element-wise corresponding
* angle in radians of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto radians(E&& e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>
{
return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Convert angles from radians to degrees.
*
* Returns an \ref xfunction for the element-wise corresponding
* angle in degrees of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto rad2deg(E&& e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>
{
return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Convert angles from radians to degrees.
*
* Returns an \ref xfunction for the element-wise corresponding
* angle in degrees of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto degrees(E&& e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>
{
return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
}
/**
* @ingroup basic_functions
* @brief Elementwise maximum
*
* Returns an \ref xfunction for the element-wise
* maximum between e1 and e2.
* @param e1 an \ref xexpression
* @param e2 an \ref xexpression
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto maximum(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::maximum<void>, E1, E2>
{
return detail::make_xfunction<math::maximum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Elementwise minimum
*
* Returns an \ref xfunction for the element-wise
* minimum between e1 and e2.
* @param e1 an \ref xexpression
* @param e2 an \ref xexpression
* @return an \ref xfunction
*/
template <class E1, class E2>
inline auto minimum(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::minimum<void>, E1, E2>
{
return detail::make_xfunction<math::minimum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
}
/**
* @ingroup basic_functions
* @brief Maximum element along given axis.
*
* Returns an \ref xreducer for the maximum of elements over given
* \em axes.
* @param e an \ref xexpression
* @param axes the axes along which the maximum is found (optional)
* @param es evaluation strategy of the reducer
* @return an \ref xreducer
*/
XTENSOR_REDUCER_FUNCTION(
amax,
math::maximum<void>,
typename std::decay_t<E>::value_type,
std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::lowest()
)
/**
* @ingroup basic_functions
* @brief Minimum element along given axis.
*
* Returns an \ref xreducer for the minimum of elements over given
* \em axes.
* @param e an \ref xexpression
* @param axes the axes along which the minimum is found (optional)
* @param es evaluation strategy of the reducer
* @return an \ref xreducer
*/
XTENSOR_REDUCER_FUNCTION(
amin,
math::minimum<void>,
typename std::decay_t<E>::value_type,
std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::max()
)
/**
* @ingroup basic_functions
* @brief Clip values between hi and lo
*
* Returns an \ref xfunction for the element-wise clipped
* values between lo and hi
* @param e1 an \ref xexpression or a scalar
* @param lo a scalar
* @param hi a scalar
*
* @return a \ref xfunction
*/
template <class E1, class E2, class E3>
inline auto clip(E1&& e1, E2&& lo, E3&& hi) noexcept
-> detail::xfunction_type_t<math::clamp_fun, E1, E2, E3>
{
return detail::make_xfunction<math::clamp_fun>(
std::forward<E1>(e1),
std::forward<E2>(lo),
std::forward<E3>(hi)
);
}
namespace math
{
template <class T>
struct sign_impl
{
template <class XT = T>
static constexpr std::enable_if_t<xtl::is_signed<XT>::value, T> run(T x)
{
return std::isnan(x) ? std::numeric_limits<T>::quiet_NaN()
: x == 0 ? T(copysign(T(0), x))
: T(copysign(T(1), x));
}
template <class XT = T>
static constexpr std::enable_if_t<xtl::is_complex<XT>::value, T> run(T x)
{
return T(
sign_impl<typename T::value_type>::run(
(x.real() != typename T::value_type(0)) ? x.real() : x.imag()
),
0
);
}
template <class XT = T>
static constexpr std::enable_if_t<std::is_unsigned<XT>::value, T> run(T x)
{
return T(x > T(0));
}
};
struct sign_fun
{
template <class T>
constexpr auto operator()(const T& x) const
{
return sign_impl<T>::run(x);
}
};
}
/**
* @ingroup basic_functions
* @brief Returns an element-wise indication of the sign of a number
*
* If the number is positive, returns +1. If negative, -1. If the number
* is zero, returns 0.
*
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto sign(E&& e) noexcept -> detail::xfunction_type_t<math::sign_fun, E>
{
return detail::make_xfunction<math::sign_fun>(std::forward<E>(e));
}
/*************************
* exponential functions *
*************************/
/**
* @defgroup exp_functions Exponential functions
*/
/**
* @ingroup exp_functions
* @brief Natural exponential function.
*
* Returns an \ref xfunction for the element-wise natural
* exponential of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto exp(E&& e) noexcept -> detail::xfunction_type_t<math::exp_fun, E>
{
return detail::make_xfunction<math::exp_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Base 2 exponential function.
*
* Returns an \ref xfunction for the element-wise base 2
* exponential of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto exp2(E&& e) noexcept -> detail::xfunction_type_t<math::exp2_fun, E>
{
return detail::make_xfunction<math::exp2_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Natural exponential minus one function.
*
* Returns an \ref xfunction for the element-wise natural
* exponential of \em e, minus 1.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto expm1(E&& e) noexcept -> detail::xfunction_type_t<math::expm1_fun, E>
{
return detail::make_xfunction<math::expm1_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Natural logarithm function.
*
* Returns an \ref xfunction for the element-wise natural
* logarithm of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto log(E&& e) noexcept -> detail::xfunction_type_t<math::log_fun, E>
{
return detail::make_xfunction<math::log_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Base 10 logarithm function.
*
* Returns an \ref xfunction for the element-wise base 10
* logarithm of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto log10(E&& e) noexcept -> detail::xfunction_type_t<math::log10_fun, E>
{
return detail::make_xfunction<math::log10_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Base 2 logarithm function.
*
* Returns an \ref xfunction for the element-wise base 2
* logarithm of \em e.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto log2(E&& e) noexcept -> detail::xfunction_type_t<math::log2_fun, E>
{
return detail::make_xfunction<math::log2_fun>(std::forward<E>(e));
}
/**
* @ingroup exp_functions
* @brief Natural logarithm of one plus function.
*
* Returns an \ref xfunction for the element-wise natural
* logarithm of \em e, plus 1.
* @param e an \ref xexpression
* @return an \ref xfunction
*/
template <class E>
inline auto log1p(E&& e) noexcept -> detail::xfunction_type_t<math::log1p_fun, E>
{
return detail::make_xfunction<math::log1p_fun>(std::forward<E>(e));
}
/*******************
* power functions *