xtensor
Loading...
Searching...
No Matches
xmath.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
13
14#ifndef XTENSOR_MATH_HPP
15#define XTENSOR_MATH_HPP
16
17#include <algorithm>
18#include <array>
19#include <cmath>
20#include <complex>
21#include <numbers>
22#include <type_traits>
23
24#include <xtl/xcomplex.hpp>
25#include <xtl/xsequence.hpp>
26#include <xtl/xtype_traits.hpp>
27
28#include "../core/xeval.hpp"
29#include "../core/xoperation.hpp"
30#include "../core/xtensor_config.hpp"
31#include "../misc/xmanipulation.hpp"
32#include "../reducers/xaccumulator.hpp"
33#include "../reducers/xreducer.hpp"
34#include "../views/xslice.hpp"
35#include "../views/xstrided_view.hpp"
36
37namespace xt
38{
39 template <class T = double>
41 {
42 static constexpr T PI = std::numbers::pi_v<T>;
43 static constexpr T PI_2 = std::numbers::pi_v<T> / 2;
44 static constexpr T PI_4 = std::numbers::pi_v<T> / 4;
45 static constexpr T D_1_PI = std::numbers::inv_pi_v<T>;
46 static constexpr T D_2_PI = 2 * std::numbers::inv_pi_v<T>;
47 static constexpr T D_2_SQRTPI = 2 * std::numbers::inv_sqrtpi_v<T>;
48 static constexpr T SQRT2 = std::numbers::sqrt2_v<T>;
49 static constexpr T SQRT1_2 = std::numbers::sqrt2_v<T> / 2;
50 static constexpr T E = std::numbers::e_v<T>;
51 static constexpr T LOG2E = std::numbers::log2e_v<T>;
52 static constexpr T LOG10E = std::numbers::log10e_v<T>;
53 static constexpr T LN2 = std::numbers::ln2_v<T>;
54 };
55
56 /***********
57 * Helpers *
58 ***********/
59
60#define XTENSOR_UNSIGNED_ABS_FUNC(T) \
61 constexpr inline T abs(const T& x) \
62 { \
63 return x; \
64 }
65
66#define XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, T) \
67 constexpr inline bool FUNC_NAME(const T& /*x*/) noexcept \
68 { \
69 return RETURN_VAL; \
70 }
71
72#define XTENSOR_INT_SPECIALIZATION(FUNC_NAME, RETURN_VAL) \
73 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, char); \
74 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, short); \
75 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, int); \
76 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, long); \
77 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, long long); \
78 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned char); \
79 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned short); \
80 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned int); \
81 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned long); \
82 XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, unsigned long long);
83
84
85#define XTENSOR_UNARY_MATH_FUNCTOR(NAME) \
86 struct NAME##_fun \
87 { \
88 template <class T> \
89 constexpr auto operator()(const T& arg) const \
90 { \
91 using math::NAME; \
92 return NAME(arg); \
93 } \
94 template <class B> \
95 constexpr auto simd_apply(const B& arg) const \
96 { \
97 using math::NAME; \
98 return NAME(arg); \
99 } \
100 }
101
102#define XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(NAME) \
103 struct NAME##_fun \
104 { \
105 template <class T> \
106 constexpr auto operator()(const T& arg) const \
107 { \
108 using math::NAME; \
109 return NAME(arg); \
110 } \
111 template <class B> \
112 constexpr auto simd_apply(const B& arg) const \
113 { \
114 using math::NAME; \
115 return NAME(arg); \
116 } \
117 }
118
119#define XTENSOR_BINARY_MATH_FUNCTOR(NAME) \
120 struct NAME##_fun \
121 { \
122 template <class T1, class T2> \
123 constexpr auto operator()(const T1& arg1, const T2& arg2) const \
124 { \
125 using math::NAME; \
126 return NAME(arg1, arg2); \
127 } \
128 template <class B> \
129 constexpr auto simd_apply(const B& arg1, const B& arg2) const \
130 { \
131 using math::NAME; \
132 return NAME(arg1, arg2); \
133 } \
134 }
135
136#define XTENSOR_TERNARY_MATH_FUNCTOR(NAME) \
137 struct NAME##_fun \
138 { \
139 template <class T1, class T2, class T3> \
140 constexpr auto operator()(const T1& arg1, const T2& arg2, const T3& arg3) const \
141 { \
142 using math::NAME; \
143 return NAME(arg1, arg2, arg3); \
144 } \
145 template <class B> \
146 auto simd_apply(const B& arg1, const B& arg2, const B& arg3) const \
147 { \
148 using math::NAME; \
149 return NAME(arg1, arg2, arg3); \
150 } \
151 }
152
153 namespace math
154 {
155 using std::abs;
156 using std::fabs;
157
158 using std::acos;
159 using std::asin;
160 using std::atan;
161 using std::cos;
162 using std::sin;
163 using std::tan;
164
165 using std::acosh;
166 using std::asinh;
167 using std::atanh;
168 using std::cosh;
169 using std::sinh;
170 using std::tanh;
171
172 using std::cbrt;
173 using std::sqrt;
174
175 using std::exp;
176 using std::exp2;
177 using std::expm1;
178 using std::ilogb;
179 using std::log;
180 using std::log10;
181 using std::log1p;
182 using std::log2;
183 using std::logb;
184
185 using std::ceil;
186 using std::floor;
187 using std::llround;
188 using std::lround;
189 using std::nearbyint;
190 using std::remainder;
191 using std::rint;
192 using std::round;
193 using std::trunc;
194
195 using std::erf;
196 using std::erfc;
197 using std::lgamma;
198 using std::tgamma;
199
200 using std::arg;
201 using std::conj;
202 using std::imag;
203 using std::real;
204
205 using std::atan2;
206
207// copysign is not in the std namespace for MSVC
208#if !defined(_MSC_VER)
209 using std::copysign;
210#endif
211 using std::fdim;
212 using std::fmax;
213 using std::fmin;
214 using std::fmod;
215 using std::hypot;
216 using std::pow;
217
218 using std::fma;
219 using std::fpclassify;
220
221 // Overload isinf, isnan and isfinite because glibc implementation
222 // might return int instead of bool and the SIMD detection requires
223 // bool return type.
224 template <class T>
225 inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isinf(const T& t)
226 {
227 return bool(std::isinf(t));
228 }
229
230 template <class T>
231 inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isnan(const T& t)
232 {
233 return bool(std::isnan(t));
234 }
235
236 template <class T>
237 inline std::enable_if_t<xtl::is_arithmetic<T>::value, bool> isfinite(const T& t)
238 {
239 return bool(std::isfinite(t));
240 }
241
242 // Overload isinf, isnan and isfinite for complex datatypes,
243 // following the Python specification:
244 template <class T>
245 inline bool isinf(const std::complex<T>& c)
246 {
247 return std::isinf(std::real(c)) || std::isinf(std::imag(c));
248 }
249
250 template <class T>
251 inline bool isnan(const std::complex<T>& c)
252 {
253 return std::isnan(std::real(c)) || std::isnan(std::imag(c));
254 }
255
256 template <class T>
257 inline bool isfinite(const std::complex<T>& c)
258 {
259 return !isinf(c) && !isnan(c);
260 }
261
262 // VS2015 STL defines isnan, isinf and isfinite as template
263 // functions, breaking ADL.
264#if defined(_WIN32) && defined(XTENSOR_USE_XSIMD)
265 /*template <class T, class A>
266 inline xsimd::batch_bool<T, A> isinf(const xsimd::batch<T, A>& b)
267 {
268 return xsimd::isinf(b);
269 }
270 template <class T, class A>
271 inline xsimd::batch_bool<T, A> isnan(const xsimd::batch<T, A>& b)
272 {
273 return xsimd::isnan(b);
274 }
275 template <class T, class A>
276 inline xsimd::batch_bool<T, A> isfinite(const xsimd::batch<T, A>& b)
277 {
278 return xsimd::isfinite(b);
279 }*/
280#endif
281 // The following specializations are needed to avoid 'ambiguous overload' errors,
282 // whereas 'unsigned char' and 'unsigned short' are automatically converted to 'int'.
283 // we're still adding those functions to silence warnings
284 XTENSOR_UNSIGNED_ABS_FUNC(unsigned char)
285 XTENSOR_UNSIGNED_ABS_FUNC(unsigned short)
286 XTENSOR_UNSIGNED_ABS_FUNC(unsigned int)
287 XTENSOR_UNSIGNED_ABS_FUNC(unsigned long)
288 XTENSOR_UNSIGNED_ABS_FUNC(unsigned long long)
289
290#ifdef _WIN32
291 XTENSOR_INT_SPECIALIZATION(isinf, false);
292 XTENSOR_INT_SPECIALIZATION(isnan, false);
293 XTENSOR_INT_SPECIALIZATION(isfinite, true);
294#endif
295
296 XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(abs);
297
298 XTENSOR_UNARY_MATH_FUNCTOR(fabs);
299 XTENSOR_BINARY_MATH_FUNCTOR(fmod);
300 XTENSOR_BINARY_MATH_FUNCTOR(remainder);
301 XTENSOR_TERNARY_MATH_FUNCTOR(fma);
302 XTENSOR_BINARY_MATH_FUNCTOR(fmax);
303 XTENSOR_BINARY_MATH_FUNCTOR(fmin);
304 XTENSOR_BINARY_MATH_FUNCTOR(fdim);
305 XTENSOR_UNARY_MATH_FUNCTOR(exp);
306 XTENSOR_UNARY_MATH_FUNCTOR(exp2);
307 XTENSOR_UNARY_MATH_FUNCTOR(expm1);
308 XTENSOR_UNARY_MATH_FUNCTOR(log);
309 XTENSOR_UNARY_MATH_FUNCTOR(log10);
310 XTENSOR_UNARY_MATH_FUNCTOR(log2);
311 XTENSOR_UNARY_MATH_FUNCTOR(log1p);
312 XTENSOR_BINARY_MATH_FUNCTOR(pow);
313 XTENSOR_UNARY_MATH_FUNCTOR(sqrt);
314 XTENSOR_UNARY_MATH_FUNCTOR(cbrt);
315 XTENSOR_BINARY_MATH_FUNCTOR(hypot);
316 XTENSOR_UNARY_MATH_FUNCTOR(sin);
317 XTENSOR_UNARY_MATH_FUNCTOR(cos);
318 XTENSOR_UNARY_MATH_FUNCTOR(tan);
319 XTENSOR_UNARY_MATH_FUNCTOR(asin);
320 XTENSOR_UNARY_MATH_FUNCTOR(acos);
321 XTENSOR_UNARY_MATH_FUNCTOR(atan);
322 XTENSOR_BINARY_MATH_FUNCTOR(atan2);
323 XTENSOR_UNARY_MATH_FUNCTOR(sinh);
324 XTENSOR_UNARY_MATH_FUNCTOR(cosh);
325 XTENSOR_UNARY_MATH_FUNCTOR(tanh);
326 XTENSOR_UNARY_MATH_FUNCTOR(asinh);
327 XTENSOR_UNARY_MATH_FUNCTOR(acosh);
328 XTENSOR_UNARY_MATH_FUNCTOR(atanh);
329 XTENSOR_UNARY_MATH_FUNCTOR(erf);
330 XTENSOR_UNARY_MATH_FUNCTOR(erfc);
331 XTENSOR_UNARY_MATH_FUNCTOR(tgamma);
332 XTENSOR_UNARY_MATH_FUNCTOR(lgamma);
333 XTENSOR_UNARY_MATH_FUNCTOR(ceil);
334 XTENSOR_UNARY_MATH_FUNCTOR(floor);
335 XTENSOR_UNARY_MATH_FUNCTOR(trunc);
336 XTENSOR_UNARY_MATH_FUNCTOR(round);
337 XTENSOR_UNARY_MATH_FUNCTOR(nearbyint);
338 XTENSOR_UNARY_MATH_FUNCTOR(rint);
339 XTENSOR_UNARY_MATH_FUNCTOR(isfinite);
340 XTENSOR_UNARY_MATH_FUNCTOR(isinf);
341 XTENSOR_UNARY_MATH_FUNCTOR(isnan);
342 XTENSOR_UNARY_MATH_FUNCTOR(conj);
343 }
344
345#undef XTENSOR_UNARY_MATH_FUNCTOR
346#undef XTENSOR_BINARY_MATH_FUNCTOR
347#undef XTENSOR_TERNARY_MATH_FUNCTOR
348#undef XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING
349#undef XTENSOR_UNSIGNED_ABS_FUNC
350
351 namespace detail
352 {
353 template <class R, class T>
354 std::enable_if_t<!iterable_expression<R>, R> fill_init(T init)
355 {
356 return R(init);
357 }
358
359 template <class R, class T>
360 std::enable_if_t<iterable_expression<R>, R> fill_init(T init)
361 {
362 R result;
363 std::fill(std::begin(result), std::end(result), init);
364 return result;
365 }
366 }
367
368#define XTENSOR_REDUCER_FUNCTION(NAME, FUNCTOR, INIT_VALUE_TYPE, INIT) \
369 template < \
370 class T = void, \
371 class E, \
372 class X, \
373 class EVS = DEFAULT_STRATEGY_REDUCERS, \
374 XTL_REQUIRES(std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<std::decay_t<X>>>)> \
375 inline auto NAME(E&& e, X&& axes, EVS es = EVS()) \
376 { \
377 using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
378 using functor_type = FUNCTOR; \
379 using init_value_fct = xt::const_value<init_value_type>; \
380 return xt::reduce( \
381 make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
382 std::forward<E>(e), \
383 std::forward<X>(axes), \
384 es \
385 ); \
386 } \
387 \
388 template < \
389 class T = void, \
390 class E, \
391 class X, \
392 class EVS = DEFAULT_STRATEGY_REDUCERS, \
393 XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<std::decay_t<X>>)> \
394 inline auto NAME(E&& e, X axis, EVS es = EVS()) \
395 { \
396 return NAME(std::forward<E>(e), {axis}, es); \
397 } \
398 \
399 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)> \
400 inline auto NAME(E&& e, EVS es = EVS()) \
401 { \
402 using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
403 using functor_type = FUNCTOR; \
404 using init_value_fct = xt::const_value<init_value_type>; \
405 return xt::reduce( \
406 make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
407 std::forward<E>(e), \
408 es \
409 ); \
410 } \
411 \
412 template <class T = void, class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS> \
413 inline auto NAME(E&& e, const I(&axes)[N], EVS es = EVS()) \
414 { \
415 using init_value_type = std::conditional_t<std::is_same<T, void>::value, INIT_VALUE_TYPE, T>; \
416 using functor_type = FUNCTOR; \
417 using init_value_fct = xt::const_value<init_value_type>; \
418 return xt::reduce( \
419 make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
420 std::forward<E>(e), \
421 axes, \
422 es \
423 ); \
424 }
425
426 /*******************
427 * basic functions *
428 *******************/
429
433
443 template <class E>
444 inline auto abs(E&& e) noexcept -> detail::xfunction_type_t<math::abs_fun, E>
445 {
446 return detail::make_xfunction<math::abs_fun>(std::forward<E>(e));
447 }
448
458 template <class E>
459 inline auto fabs(E&& e) noexcept -> detail::xfunction_type_t<math::fabs_fun, E>
460 {
461 return detail::make_xfunction<math::fabs_fun>(std::forward<E>(e));
462 }
463
475 template <class E1, class E2>
476 inline auto fmod(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmod_fun, E1, E2>
477 {
478 return detail::make_xfunction<math::fmod_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
479 }
480
492 template <class E1, class E2>
493 inline auto remainder(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::remainder_fun, E1, E2>
494 {
495 return detail::make_xfunction<math::remainder_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
496 }
497
510 template <class E1, class E2, class E3>
511 inline auto fma(E1&& e1, E2&& e2, E3&& e3) noexcept -> detail::xfunction_type_t<math::fma_fun, E1, E2, E3>
512 {
513 return detail::make_xfunction<math::fma_fun>(
514 std::forward<E1>(e1),
515 std::forward<E2>(e2),
516 std::forward<E3>(e3)
517 );
518 }
519
531 template <class E1, class E2>
532 inline auto fmax(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmax_fun, E1, E2>
533 {
534 return detail::make_xfunction<math::fmax_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
535 }
536
548 template <class E1, class E2>
549 inline auto fmin(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fmin_fun, E1, E2>
550 {
551 return detail::make_xfunction<math::fmin_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
552 }
553
565 template <class E1, class E2>
566 inline auto fdim(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::fdim_fun, E1, E2>
567 {
568 return detail::make_xfunction<math::fdim_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
569 }
570
571 namespace math
572 {
573 template <class T = void>
574 struct minimum
575 {
576 template <class A1, class A2>
577 constexpr auto operator()(const A1& t1, const A2& t2) const noexcept
578 {
579 return xtl::select(t1 < t2, t1, t2);
580 }
581
582 template <class A1, class A2>
583 constexpr auto simd_apply(const A1& t1, const A2& t2) const noexcept
584 {
585 return xt_simd::select(t1 < t2, t1, t2);
586 }
587 };
588
589 template <class T = void>
590 struct maximum
591 {
592 template <class A1, class A2>
593 constexpr auto operator()(const A1& t1, const A2& t2) const noexcept
594 {
595 return xtl::select(t1 > t2, t1, t2);
596 }
597
598 template <class A1, class A2>
599 constexpr auto simd_apply(const A1& t1, const A2& t2) const noexcept
600 {
601 return xt_simd::select(t1 > t2, t1, t2);
602 }
603 };
604
606 {
607 template <class A1, class A2, class A3>
608 constexpr auto operator()(const A1& v, const A2& lo, const A3& hi) const
609 {
610 return xtl::select(lo < hi, xtl::select(v < lo, lo, xtl::select(hi < v, hi, v)), hi);
611 }
612
613 template <class A1, class A2, class A3>
614 constexpr auto simd_apply(const A1& v, const A2& lo, const A3& hi) const
615 {
616 return xt_simd::select(lo < hi, xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v)), hi);
617 }
618 };
619
620 struct deg2rad
621 {
622 template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
623 constexpr double operator()(const A& a) const noexcept
624 {
625 return a * xt::numeric_constants<double>::PI / 180.0;
626 }
627
628 template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
629 constexpr auto operator()(const A& a) const noexcept
630 {
631 return a * xt::numeric_constants<A>::PI / A(180.0);
632 }
633
634 template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
635 constexpr double simd_apply(const A& a) const noexcept
636 {
637 return a * xt::numeric_constants<double>::PI / 180.0;
638 }
639
640 template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
641 constexpr auto simd_apply(const A& a) const noexcept
642 {
643 return a * xt::numeric_constants<A>::PI / A(180.0);
644 }
645 };
646
647 struct rad2deg
648 {
649 template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
650 constexpr double operator()(const A& a) const noexcept
651 {
652 return a * 180.0 / xt::numeric_constants<double>::PI;
653 }
654
655 template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
656 constexpr auto operator()(const A& a) const noexcept
657 {
658 return a * A(180.0) / xt::numeric_constants<A>::PI;
659 }
660
661 template <class A, std::enable_if_t<xtl::is_integral<A>::value, int> = 0>
662 constexpr double simd_apply(const A& a) const noexcept
663 {
664 return a * 180.0 / xt::numeric_constants<double>::PI;
665 }
666
667 template <class A, std::enable_if_t<std::is_floating_point<A>::value, int> = 0>
668 constexpr auto simd_apply(const A& a) const noexcept
669 {
670 return a * A(180.0) / xt::numeric_constants<A>::PI;
671 }
672 };
673 }
674
684 template <class E>
685 inline auto deg2rad(E&& e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>
686 {
687 return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
688 }
689
699 template <class E>
700 inline auto radians(E&& e) noexcept -> detail::xfunction_type_t<math::deg2rad, E>
701 {
702 return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
703 }
704
714 template <class E>
715 inline auto rad2deg(E&& e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>
716 {
717 return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
718 }
719
729 template <class E>
730 inline auto degrees(E&& e) noexcept -> detail::xfunction_type_t<math::rad2deg, E>
731 {
732 return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
733 }
734
745 template <class E1, class E2>
746 inline auto maximum(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::maximum<void>, E1, E2>
747 {
748 return detail::make_xfunction<math::maximum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
749 }
750
761 template <class E1, class E2>
762 inline auto minimum(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::minimum<void>, E1, E2>
763 {
764 return detail::make_xfunction<math::minimum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
765 }
766
778 XTENSOR_REDUCER_FUNCTION(
779 amax,
780 math::maximum<void>,
781 typename std::decay_t<E>::value_type,
782 std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::lowest()
784
785
796 XTENSOR_REDUCER_FUNCTION(
797 amin,
798 math::minimum<void>,
799 typename std::decay_t<E>::value_type,
800 std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::max()
802
815 template <class E1, class E2, class E3>
816 inline auto clip(E1&& e1, E2&& lo, E3&& hi) noexcept
817 -> detail::xfunction_type_t<math::clamp_fun, E1, E2, E3>
818 {
819 return detail::make_xfunction<math::clamp_fun>(
820 std::forward<E1>(e1),
821 std::forward<E2>(lo),
822 std::forward<E3>(hi)
823 );
824 }
825
826 namespace math
827 {
828 template <class T>
830 {
831 template <class XT = T>
832 static constexpr std::enable_if_t<xtl::is_signed<XT>::value, T> run(T x)
833 {
834 return std::isnan(x) ? std::numeric_limits<T>::quiet_NaN()
835 : x == 0 ? T(copysign(T(0), x))
836 : T(copysign(T(1), x));
837 }
838
839 template <class XT = T>
840 static constexpr std::enable_if_t<xtl::is_complex<XT>::value, T> run(T x)
841 {
842 return T(
843 sign_impl<typename T::value_type>::run(
844 (x.real() != typename T::value_type(0)) ? x.real() : x.imag()
845 ),
846 0
847 );
848 }
849
850 template <class XT = T>
851 static constexpr std::enable_if_t<std::is_unsigned<XT>::value, T> run(T x)
852 {
853 return T(x > T(0));
854 }
855 };
856
857 struct sign_fun
858 {
859 template <class T>
860 constexpr auto operator()(const T& x) const
861 {
862 return sign_impl<T>::run(x);
863 }
864 };
865 }
866
877 template <class E>
878 inline auto sign(E&& e) noexcept -> detail::xfunction_type_t<math::sign_fun, E>
879 {
880 return detail::make_xfunction<math::sign_fun>(std::forward<E>(e));
881 }
882
883 /*************************
884 * exponential functions *
885 *************************/
886
890
900 template <class E>
901 inline auto exp(E&& e) noexcept -> detail::xfunction_type_t<math::exp_fun, E>
902 {
903 return detail::make_xfunction<math::exp_fun>(std::forward<E>(e));
904 }
905
915 template <class E>
916 inline auto exp2(E&& e) noexcept -> detail::xfunction_type_t<math::exp2_fun, E>
917 {
918 return detail::make_xfunction<math::exp2_fun>(std::forward<E>(e));
919 }
920
930 template <class E>
931 inline auto expm1(E&& e) noexcept -> detail::xfunction_type_t<math::expm1_fun, E>
932 {
933 return detail::make_xfunction<math::expm1_fun>(std::forward<E>(e));
934 }
935
945 template <class E>
946 inline auto log(E&& e) noexcept -> detail::xfunction_type_t<math::log_fun, E>
947 {
948 return detail::make_xfunction<math::log_fun>(std::forward<E>(e));
949 }
950
960 template <class E>
961 inline auto log10(E&& e) noexcept -> detail::xfunction_type_t<math::log10_fun, E>
962 {
963 return detail::make_xfunction<math::log10_fun>(std::forward<E>(e));
964 }
965
975 template <class E>
976 inline auto log2(E&& e) noexcept -> detail::xfunction_type_t<math::log2_fun, E>
977 {
978 return detail::make_xfunction<math::log2_fun>(std::forward<E>(e));
979 }
980
990 template <class E>
991 inline auto log1p(E&& e) noexcept -> detail::xfunction_type_t<math::log1p_fun, E>
992 {
993 return detail::make_xfunction<math::log1p_fun>(std::forward<E>(e));
994 }
995
996 /*******************
997 * power functions *
998 *******************/
999
1003
1015 template <class E1, class E2>
1016 inline auto pow(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::pow_fun, E1, E2>
1017 {
1018 return detail::make_xfunction<math::pow_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
1019 }
1020
1021 namespace detail
1022 {
1023 template <class F, class... T, typename = decltype(std::declval<F>()(std::declval<T>()...))>
1024 std::true_type supports_test(const F&, const T&...);
1025 std::false_type supports_test(...);
1026
1027 template <class... T>
1028 struct supports;
1029
1030 template <class F, class... T>
1031 struct supports<F(T...)> : decltype(supports_test(std::declval<F>(), std::declval<T>()...))
1032 {
1033 };
1034
1035 template <class F>
1036 struct lambda_adapt
1037 {
1038 explicit lambda_adapt(F&& lmbd)
1039 : m_lambda(std::move(lmbd))
1040 {
1041 }
1042
1043 template <class... T>
1044 auto operator()(T... args) const
1045 {
1046 return m_lambda(args...);
1047 }
1048
1049 template <class... T, XTL_REQUIRES(detail::supports<F(T...)>)>
1050 auto simd_apply(T... args) const
1051 {
1052 return m_lambda(args...);
1053 }
1054
1055 F m_lambda;
1056 };
1057 }
1058
1085 template <class F, class... E>
1086 inline auto make_lambda_xfunction(F&& lambda, E&&... args)
1087 {
1088 using xfunction_type = typename detail::xfunction_type<detail::lambda_adapt<F>, E...>::type;
1089 return xfunction_type(detail::lambda_adapt<F>(std::forward<F>(lambda)), std::forward<E>(args)...);
1090 }
1091
1101 template <class E1>
1102 inline auto square(E1&& e1) noexcept
1103 {
1104 auto fnct = [](auto x) -> decltype(x * x)
1105 {
1106 return x * x;
1107 };
1108 return make_lambda_xfunction(std::move(fnct), std::forward<E1>(e1));
1109 }
1110
1120 template <class E1>
1121 inline auto cube(E1&& e1) noexcept
1122 {
1123 auto fnct = [](auto x) -> decltype(x * x * x)
1124 {
1125 return x * x * x;
1126 };
1127 return make_lambda_xfunction(std::move(fnct), std::forward<E1>(e1));
1128 }
1129
1130 namespace detail
1131 {
1132 // Thanks to Matt Pharr in http://pbrt.org/hair.pdf
1133 template <std::size_t N>
1134 struct pow_impl;
1135
1136 template <std::size_t N>
1137 struct pow_impl
1138 {
1139 template <class T>
1140 auto operator()(T v) const -> decltype(v * v)
1141 {
1142 T temp = pow_impl<N / 2>{}(v);
1143 return temp * temp * pow_impl<N & 1>{}(v);
1144 }
1145 };
1146
1147 template <>
1148 struct pow_impl<1>
1149 {
1150 template <class T>
1151 auto operator()(T v) const -> T
1152 {
1153 return v;
1154 }
1155 };
1156
1157 template <>
1158 struct pow_impl<0>
1159 {
1160 template <class T>
1161 auto operator()(T /*v*/) const -> T
1162 {
1163 return T(1);
1164 }
1165 };
1166 }
1167
1185 template <std::size_t N, class E>
1186 inline auto pow(E&& e) noexcept
1187 {
1188 static_assert(N > 0, "integer power cannot be negative");
1189 return make_lambda_xfunction(detail::pow_impl<N>{}, std::forward<E>(e));
1190 }
1191
1201 template <class E>
1202 inline auto sqrt(E&& e) noexcept -> detail::xfunction_type_t<math::sqrt_fun, E>
1203 {
1204 return detail::make_xfunction<math::sqrt_fun>(std::forward<E>(e));
1205 }
1206
1216 template <class E>
1217 inline auto cbrt(E&& e) noexcept -> detail::xfunction_type_t<math::cbrt_fun, E>
1218 {
1219 return detail::make_xfunction<math::cbrt_fun>(std::forward<E>(e));
1220 }
1221
1234 template <class E1, class E2>
1235 inline auto hypot(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::hypot_fun, E1, E2>
1236 {
1237 return detail::make_xfunction<math::hypot_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
1238 }
1239
1240 /***************************
1241 * trigonometric functions *
1242 ***************************/
1243
1247
1257 template <class E>
1258 inline auto sin(E&& e) noexcept -> detail::xfunction_type_t<math::sin_fun, E>
1259 {
1260 return detail::make_xfunction<math::sin_fun>(std::forward<E>(e));
1261 }
1262
1272 template <class E>
1273 inline auto cos(E&& e) noexcept -> detail::xfunction_type_t<math::cos_fun, E>
1274 {
1275 return detail::make_xfunction<math::cos_fun>(std::forward<E>(e));
1276 }
1277
1287 template <class E>
1288 inline auto tan(E&& e) noexcept -> detail::xfunction_type_t<math::tan_fun, E>
1289 {
1290 return detail::make_xfunction<math::tan_fun>(std::forward<E>(e));
1291 }
1292
1302 template <class E>
1303 inline auto asin(E&& e) noexcept -> detail::xfunction_type_t<math::asin_fun, E>
1304 {
1305 return detail::make_xfunction<math::asin_fun>(std::forward<E>(e));
1306 }
1307
1317 template <class E>
1318 inline auto acos(E&& e) noexcept -> detail::xfunction_type_t<math::acos_fun, E>
1319 {
1320 return detail::make_xfunction<math::acos_fun>(std::forward<E>(e));
1321 }
1322
1332 template <class E>
1333 inline auto atan(E&& e) noexcept -> detail::xfunction_type_t<math::atan_fun, E>
1334 {
1335 return detail::make_xfunction<math::atan_fun>(std::forward<E>(e));
1336 }
1337
1350 template <class E1, class E2>
1351 inline auto atan2(E1&& e1, E2&& e2) noexcept -> detail::xfunction_type_t<math::atan2_fun, E1, E2>
1352 {
1353 return detail::make_xfunction<math::atan2_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
1354 }
1355
1356 /************************
1357 * hyperbolic functions *
1358 ************************/
1359
1363
1373 template <class E>
1374 inline auto sinh(E&& e) noexcept -> detail::xfunction_type_t<math::sinh_fun, E>
1375 {
1376 return detail::make_xfunction<math::sinh_fun>(std::forward<E>(e));
1377 }
1378
1388 template <class E>
1389 inline auto cosh(E&& e) noexcept -> detail::xfunction_type_t<math::cosh_fun, E>
1390 {
1391 return detail::make_xfunction<math::cosh_fun>(std::forward<E>(e));
1392 }
1393
1403 template <class E>
1404 inline auto tanh(E&& e) noexcept -> detail::xfunction_type_t<math::tanh_fun, E>
1405 {
1406 return detail::make_xfunction<math::tanh_fun>(std::forward<E>(e));
1407 }
1408
1418 template <class E>
1419 inline auto asinh(E&& e) noexcept -> detail::xfunction_type_t<math::asinh_fun, E>
1420 {
1421 return detail::make_xfunction<math::asinh_fun>(std::forward<E>(e));
1422 }
1423
1433 template <class E>
1434 inline auto acosh(E&& e) noexcept -> detail::xfunction_type_t<math::acosh_fun, E>
1435 {
1436 return detail::make_xfunction<math::acosh_fun>(std::forward<E>(e));
1437 }
1438
1448 template <class E>
1449 inline auto atanh(E&& e) noexcept -> detail::xfunction_type_t<math::atanh_fun, E>
1450 {
1451 return detail::make_xfunction<math::atanh_fun>(std::forward<E>(e));
1452 }
1453
1454 /*****************************
1455 * error and gamma functions *
1456 *****************************/
1457
1461
1471 template <class E>
1472 inline auto erf(E&& e) noexcept -> detail::xfunction_type_t<math::erf_fun, E>
1473 {
1474 return detail::make_xfunction<math::erf_fun>(std::forward<E>(e));
1475 }
1476
1486 template <class E>
1487 inline auto erfc(E&& e) noexcept -> detail::xfunction_type_t<math::erfc_fun, E>
1488 {
1489 return detail::make_xfunction<math::erfc_fun>(std::forward<E>(e));
1490 }
1491
1501 template <class E>
1502 inline auto tgamma(E&& e) noexcept -> detail::xfunction_type_t<math::tgamma_fun, E>
1503 {
1504 return detail::make_xfunction<math::tgamma_fun>(std::forward<E>(e));
1505 }
1506
1516 template <class E>
1517 inline auto lgamma(E&& e) noexcept -> detail::xfunction_type_t<math::lgamma_fun, E>
1518 {
1519 return detail::make_xfunction<math::lgamma_fun>(std::forward<E>(e));
1520 }
1521
1522 /*********************************************
1523 * nearest integer floating point operations *
1524 *********************************************/
1525
1529
1539 template <class E>
1540 inline auto ceil(E&& e) noexcept -> detail::xfunction_type_t<math::ceil_fun, E>
1541 {
1542 return detail::make_xfunction<math::ceil_fun>(std::forward<E>(e));
1543 }
1544
1554 template <class E>
1555 inline auto floor(E&& e) noexcept -> detail::xfunction_type_t<math::floor_fun, E>
1556 {
1557 return detail::make_xfunction<math::floor_fun>(std::forward<E>(e));
1558 }
1559
1569 template <class E>
1570 inline auto trunc(E&& e) noexcept -> detail::xfunction_type_t<math::trunc_fun, E>
1571 {
1572 return detail::make_xfunction<math::trunc_fun>(std::forward<E>(e));
1573 }
1574
1585 template <class E>
1586 inline auto round(E&& e) noexcept -> detail::xfunction_type_t<math::round_fun, E>
1587 {
1588 return detail::make_xfunction<math::round_fun>(std::forward<E>(e));
1589 }
1590
1601 template <class E>
1602 inline auto nearbyint(E&& e) noexcept -> detail::xfunction_type_t<math::nearbyint_fun, E>
1603 {
1604 return detail::make_xfunction<math::nearbyint_fun>(std::forward<E>(e));
1605 }
1606
1617 template <class E>
1618 inline auto rint(E&& e) noexcept -> detail::xfunction_type_t<math::rint_fun, E>
1619 {
1620 return detail::make_xfunction<math::rint_fun>(std::forward<E>(e));
1621 }
1622
1623 /****************************
1624 * classification functions *
1625 ****************************/
1626
1630
1640 template <class E>
1641 inline auto isfinite(E&& e) noexcept -> detail::xfunction_type_t<math::isfinite_fun, E>
1642 {
1643 return detail::make_xfunction<math::isfinite_fun>(std::forward<E>(e));
1644 }
1645
1655 template <class E>
1656 inline auto isinf(E&& e) noexcept -> detail::xfunction_type_t<math::isinf_fun, E>
1657 {
1658 return detail::make_xfunction<math::isinf_fun>(std::forward<E>(e));
1659 }
1660
1670 template <class E>
1671 inline auto isnan(E&& e) noexcept -> detail::xfunction_type_t<math::isnan_fun, E>
1672 {
1673 return detail::make_xfunction<math::isnan_fun>(std::forward<E>(e));
1674 }
1675
1676 namespace detail
1677 {
1678 template <class FUNCTOR, class T, std::size_t... Is>
1679 inline auto get_functor(T&& args, std::index_sequence<Is...>)
1680 {
1681 return FUNCTOR(std::get<Is>(args)...);
1682 }
1683
1684 template <class F, class... A, class... E>
1685 inline auto make_xfunction(std::tuple<A...>&& f_args, E&&... e) noexcept
1686 {
1687 using functor_type = F;
1688 using expression_tag = xexpression_tag_t<E...>;
1689 using type = select_xfunction_expression_t<expression_tag, functor_type, const_xclosure_t<E>...>;
1690 auto functor = get_functor<functor_type>(
1691 std::forward<std::tuple<A...>>(f_args),
1692 std::make_index_sequence<sizeof...(A)>{}
1693 );
1694 return type(std::move(functor), std::forward<E>(e)...);
1695 }
1696
1697 struct isclose
1698 {
1699 using result_type = bool;
1700
1701 isclose(double rtol, double atol, bool equal_nan)
1702 : m_rtol(rtol)
1703 , m_atol(atol)
1704 , m_equal_nan(equal_nan)
1705 {
1706 }
1707
1708 template <class A1, class A2>
1709 bool operator()(const A1& a, const A2& b) const
1710 {
1711 using internal_type = xtl::promote_type_t<A1, A2, double>;
1712 if (math::isnan(a) && math::isnan(b))
1713 {
1714 return m_equal_nan;
1715 }
1716 if (math::isinf(a) && math::isinf(b))
1717 {
1718 // check for both infinity signs equal
1719 return a == b;
1720 }
1721 auto d = math::abs(internal_type(a) - internal_type(b));
1722 return d <= m_atol
1723 || d <= m_rtol
1724 * double((std::max)(math::abs(internal_type(a)), math::abs(internal_type(b)))
1725 );
1726 }
1727
1728 private:
1729
1730 double m_rtol;
1731 double m_atol;
1732 bool m_equal_nan;
1733 };
1734 }
1735
1751 template <class E1, class E2>
1752 inline auto
1753 isclose(E1&& e1, E2&& e2, double rtol = 1e-05, double atol = 1e-08, bool equal_nan = false) noexcept
1754 {
1755 return detail::make_xfunction<detail::isclose>(
1756 std::make_tuple(rtol, atol, equal_nan),
1757 std::forward<E1>(e1),
1758 std::forward<E2>(e2)
1759 );
1760 }
1761
1775 template <class E1, class E2>
1776 inline auto allclose(E1&& e1, E2&& e2, double rtol = 1e-05, double atol = 1e-08) noexcept
1777 {
1778 return xt::all(isclose(std::forward<E1>(e1), std::forward<E2>(e2), rtol, atol));
1779 }
1780
1781 /**********************
1782 * Reducing functions *
1783 **********************/
1784
1788
1804 XTENSOR_REDUCER_FUNCTION(sum, detail::plus, typename std::decay_t<E>::value_type, 0)
1805
1806
1824 XTENSOR_REDUCER_FUNCTION(prod, detail::multiplies, typename std::decay_t<E>::value_type, 1)
1825
1826 namespace detail
1827 {
1828 template <class T, class S, class ST>
1829 inline auto mean_division(S&& s, ST e_size)
1830 {
1831 using value_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
1832 // Avoids floating point exception when s.size is 0
1833 value_type div = s.size() != ST(0) ? static_cast<value_type>(e_size / s.size()) : value_type(0);
1834 return std::move(s) / std::move(div);
1835 }
1836
1837 template <
1838 class T,
1839 class E,
1840 class X,
1841 class D,
1842 class EVS,
1843 XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<D>)>
1844 inline auto mean(E&& e, X&& axes, const D& ddof, EVS es)
1845 {
1846 // sum cannot always be a double. It could be a complex number which cannot operate on
1847 // std::plus<double>.
1848 using size_type = typename std::decay_t<E>::size_type;
1849 const size_type size = e.size();
1850 XTENSOR_ASSERT(static_cast<size_type>(ddof) <= size);
1851 auto s = sum<T>(std::forward<E>(e), std::forward<X>(axes), es);
1852 return mean_division<T>(std::move(s), size - static_cast<size_type>(ddof));
1853 }
1854
1855 template <class T, class E, class I, std::size_t N, class D, class EVS>
1856 inline auto mean(E&& e, const I (&axes)[N], const D& ddof, EVS es)
1857 {
1858 using size_type = typename std::decay_t<E>::size_type;
1859 const size_type size = e.size();
1860 XTENSOR_ASSERT(static_cast<size_type>(ddof) <= size);
1861 auto s = sum<T>(std::forward<E>(e), axes, es);
1862 return mean_division<T>(std::move(s), size - static_cast<size_type>(ddof));
1863 }
1864
1865 template <class T, class E, class D, class EVS, XTL_REQUIRES(is_reducer_options<EVS>, xtl::is_integral<D>)>
1866 inline auto mean_noaxis(E&& e, const D& ddof, EVS es)
1867 {
1868 using value_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
1869 using size_type = typename std::decay_t<E>::size_type;
1870 const size_type size = e.size();
1871 XTENSOR_ASSERT(static_cast<size_type>(ddof) <= size);
1872 auto s = sum<T>(std::forward<E>(e), es);
1873 return std::move(s) / static_cast<value_type>((size - static_cast<size_type>(ddof)));
1874 }
1875 }
1876
1892 template <
1893 class T = void,
1894 class E,
1895 class X,
1896 class EVS = DEFAULT_STRATEGY_REDUCERS,
1897 XTL_REQUIRES(std::negation<is_reducer_options<X>>)>
1898 inline auto mean(E&& e, X&& axes, EVS es = EVS())
1899 {
1900 return detail::mean<T>(std::forward<E>(e), std::forward<X>(axes), 0u, es);
1901 }
1902
1903 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
1904 inline auto mean(E&& e, EVS es = EVS())
1905 {
1906 return detail::mean_noaxis<T>(std::forward<E>(e), 0u, es);
1907 }
1908
1909 template <class T = void, class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
1910 inline auto mean(E&& e, const I (&axes)[N], EVS es = EVS())
1911 {
1912 return detail::mean<T>(std::forward<E>(e), axes, 0u, es);
1913 }
1914
1932 template <
1933 class T = void,
1934 class E,
1935 class W,
1936 class X,
1937 class EVS = DEFAULT_STRATEGY_REDUCERS,
1938 XTL_REQUIRES(is_reducer_options<EVS>, std::negation<xtl::is_integral<X>>)>
1939 inline auto average(E&& e, W&& weights, X&& axes, EVS ev = EVS())
1940 {
1941 xindex_type_t<typename std::decay_t<E>::shape_type> broadcast_shape;
1942 xt::resize_container(broadcast_shape, e.dimension());
1943 auto ax = normalize_axis(e, axes);
1944 if (weights.dimension() == 1)
1945 {
1946 if (weights.size() != e.shape()[ax[0]])
1947 {
1948 XTENSOR_THROW(std::runtime_error, "Weights need to have the same shape as expression at axes.");
1949 }
1950
1951 std::fill(broadcast_shape.begin(), broadcast_shape.end(), std::size_t(1));
1952 broadcast_shape[ax[0]] = weights.size();
1953 }
1954 else
1955 {
1956 if (!same_shape(e.shape(), weights.shape()))
1957 {
1958 XTENSOR_THROW(
1959 std::runtime_error,
1960 "Weights with dim > 1 need to have the same shape as expression."
1961 );
1962 }
1963
1964 std::copy_n(e.shape().begin(), e.shape().size(), broadcast_shape.begin());
1965 }
1966
1967 constexpr layout_type L = default_assignable_layout(std::decay_t<W>::static_layout);
1968 auto weights_view = reshape_view<L>(std::forward<W>(weights), std::move(broadcast_shape));
1969 auto scl = sum<T>(weights_view, ax, xt::evaluation_strategy::immediate);
1970 return sum<T>(std::forward<E>(e) * std::move(weights_view), std::move(ax), ev) / std::move(scl);
1971 }
1972
1973 template <
1974 class T = void,
1975 class E,
1976 class W,
1977 class X,
1978 class EVS = DEFAULT_STRATEGY_REDUCERS,
1979 XTL_REQUIRES(is_reducer_options<EVS>, xtl::is_integral<X>)>
1980 inline auto average(E&& e, W&& weights, X axis, EVS ev = EVS())
1981 {
1982 return average(std::forward<E>(e), std::forward<W>(weights), {axis}, std::forward<EVS>(ev));
1983 }
1984
1985 template <class T = void, class E, class W, class X, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
1986 inline auto average(E&& e, W&& weights, const X (&axes)[N], EVS ev = EVS())
1987 {
1988 // need to select the X&& overload and forward to different type
1989 using ax_t = std::array<std::size_t, N>;
1990 return average<T>(std::forward<E>(e), std::forward<W>(weights), xt::forward_normalize<ax_t>(e, axes), ev);
1991 }
1992
1993 template <class T = void, class E, class W, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
1994 inline auto average(E&& e, W&& weights, EVS ev = EVS())
1995 {
1996 if (weights.dimension() != e.dimension()
1997 || !std::equal(weights.shape().begin(), weights.shape().end(), e.shape().begin()))
1998 {
1999 XTENSOR_THROW(std::runtime_error, "Weights need to have the same shape as expression.");
2000 }
2001
2002 auto div = sum<T>(weights, evaluation_strategy::immediate)();
2003 auto s = sum<T>(std::forward<E>(e) * std::forward<W>(weights), ev) / std::move(div);
2004 return s;
2005 }
2006
2007 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2008 inline auto average(E&& e, EVS ev = EVS())
2009 {
2010 return mean<T>(e, ev);
2011 }
2012
2013 namespace detail
2014 {
2015 template <typename E>
2016 std::enable_if_t<std::is_lvalue_reference<E>::value, E> shared_forward(E e) noexcept
2017 {
2018 return e;
2019 }
2020
2021 template <typename E>
2022 std::enable_if_t<!std::is_lvalue_reference<E>::value, xshared_expression<E>> shared_forward(E e) noexcept
2023 {
2024 return make_xshared(std::move(e));
2025 }
2026 }
2027
2028 template <
2029 class T = void,
2030 class E,
2031 class D,
2032 class EVS = DEFAULT_STRATEGY_REDUCERS,
2033 XTL_REQUIRES(is_reducer_options<EVS>, xtl::is_integral<D>)>
2034 inline auto variance(E&& e, const D& ddof, EVS es = EVS())
2035 {
2036 auto cached_mean = mean<T>(e, es)();
2037 return detail::mean_noaxis<T>(square(std::forward<E>(e) - std::move(cached_mean)), ddof, es);
2038 }
2039
2040 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2041 inline auto variance(E&& e, EVS es = EVS())
2042 {
2043 return variance<T>(std::forward<E>(e), 0u, es);
2044 }
2045
2046 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2047 inline auto stddev(E&& e, EVS es = EVS())
2048 {
2049 return sqrt(variance<T>(std::forward<E>(e), es));
2050 }
2051
2076 template <
2077 class T = void,
2078 class E,
2079 class X,
2080 class D,
2081 class EVS = DEFAULT_STRATEGY_REDUCERS,
2082 XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<D>)>
2083 inline auto variance(E&& e, X&& axes, const D& ddof, EVS es = EVS())
2084 {
2085 decltype(auto) sc = detail::shared_forward<E>(e);
2086 // note: forcing copy of first axes argument -- is there a better solution?
2087 auto axes_copy = axes;
2088 // always eval to prevent repeated evaluations in the next calls
2089 auto inner_mean = eval(mean<T>(sc, std::move(axes_copy), evaluation_strategy::immediate));
2090
2091 // fake keep_dims = 1
2092 // Since the inner_shape might have a reference semantic (e.g. xbuffer_adaptor in bindings)
2093 // We need to map it to another type before modifying it.
2094 // We pragmatically abuse `get_strides_t`
2095 using tmp_shape_t = get_strides_t<typename std::decay_t<E>::shape_type>;
2096 tmp_shape_t keep_dim_shape = xtl::forward_sequence<tmp_shape_t, decltype(e.shape())>(e.shape());
2097 for (const auto& el : axes)
2098 {
2099 keep_dim_shape[el] = 1u;
2100 }
2101
2102 auto mrv = reshape_view<XTENSOR_DEFAULT_LAYOUT>(std::move(inner_mean), std::move(keep_dim_shape));
2103 return detail::mean<T>(square(sc - std::move(mrv)), std::forward<X>(axes), ddof, es);
2104 }
2105
2106 template <
2107 class T = void,
2108 class E,
2109 class X,
2110 class EVS = DEFAULT_STRATEGY_REDUCERS,
2111 XTL_REQUIRES(std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<std::decay_t<X>>>, is_reducer_options<EVS>)>
2112 inline auto variance(E&& e, X&& axes, EVS es = EVS())
2113 {
2114 return variance<T>(std::forward<E>(e), std::forward<X>(axes), 0u, es);
2115 }
2116
2138 template <
2139 class T = void,
2140 class E,
2141 class X,
2142 class EVS = DEFAULT_STRATEGY_REDUCERS,
2143 XTL_REQUIRES(std::negation<is_reducer_options<X>>)>
2144 inline auto stddev(E&& e, X&& axes, EVS es = EVS())
2145 {
2146 return sqrt(variance<T>(std::forward<E>(e), std::forward<X>(axes), es));
2147 }
2148
2149 template <class T = void, class E, class A, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2150 inline auto stddev(E&& e, const A (&axes)[N], EVS es = EVS())
2151 {
2152 return stddev<T>(
2153 std::forward<E>(e),
2154 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2155 es
2156 );
2157 }
2158
2159 template <
2160 class T = void,
2161 class E,
2162 class A,
2163 std::size_t N,
2164 class EVS = DEFAULT_STRATEGY_REDUCERS,
2165 XTL_REQUIRES(is_reducer_options<EVS>)>
2166 inline auto variance(E&& e, const A (&axes)[N], EVS es = EVS())
2167 {
2168 return variance<T>(
2169 std::forward<E>(e),
2170 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2171 es
2172 );
2173 }
2174
2175 template <class T = void, class E, class A, std::size_t N, class D, class EVS = DEFAULT_STRATEGY_REDUCERS>
2176 inline auto variance(E&& e, const A (&axes)[N], const D& ddof, EVS es = EVS())
2177 {
2178 return variance<T>(
2179 std::forward<E>(e),
2180 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2181 ddof,
2182 es
2183 );
2184 }
2185
2196 template <class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2197 inline auto minmax(E&& e, EVS es = EVS())
2198 {
2199 using std::max;
2200 using std::min;
2201 using value_type = typename std::decay_t<E>::value_type;
2202 using result_type = std::array<value_type, 2>;
2203 using init_value_fct = xt::const_value<result_type>;
2204
2205 auto reduce_func = [](auto r, const auto& v)
2206 {
2207 r[0] = (min) (r[0], v);
2208 r[1] = (max) (r[1], v);
2209 return r;
2210 };
2211
2212 auto init_func = init_value_fct(
2213 result_type{std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::lowest()}
2214 );
2215
2216 auto merge_func = [](auto r, const auto& s)
2217 {
2218 r[0] = (min) (r[0], s[0]);
2219 r[1] = (max) (r[1], s[1]);
2220 return r;
2221 };
2222 return xt::reduce(
2223 make_xreducer_functor(std::move(reduce_func), std::move(init_func), std::move(merge_func)),
2224 std::forward<E>(e),
2225 arange(e.dimension()),
2226 es
2227 );
2228 }
2229
2233
2248 template <class T = void, class E>
2249 inline auto cumsum(E&& e, std::ptrdiff_t axis)
2250 {
2251 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2252 return accumulate(
2253 make_xaccumulator_functor(detail::plus(), detail::accumulator_identity<init_value_type>()),
2254 std::forward<E>(e),
2255 axis
2256 );
2257 }
2258
2259 template <class T = void, class E>
2260 inline auto cumsum(E&& e)
2261 {
2262 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2263 return accumulate(
2264 make_xaccumulator_functor(detail::plus(), detail::accumulator_identity<init_value_type>()),
2265 std::forward<E>(e)
2266 );
2267 }
2268
2283 template <class T = void, class E>
2284 inline auto cumprod(E&& e, std::ptrdiff_t axis)
2285 {
2286 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2287 return accumulate(
2288 make_xaccumulator_functor(detail::multiplies(), detail::accumulator_identity<init_value_type>()),
2289 std::forward<E>(e),
2290 axis
2291 );
2292 }
2293
2294 template <class T = void, class E>
2295 inline auto cumprod(E&& e)
2296 {
2297 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2298 return accumulate(
2299 make_xaccumulator_functor(detail::multiplies(), detail::accumulator_identity<init_value_type>()),
2300 std::forward<E>(e)
2301 );
2302 }
2303
2304 /*****************
2305 * nan functions *
2306 *****************/
2307
2308 namespace detail
2309 {
2310 struct nan_to_num_functor
2311 {
2312 template <class A>
2313 inline auto operator()(const A& a) const
2314 {
2315 if (math::isnan(a))
2316 {
2317 return A(0);
2318 }
2319 if (math::isinf(a))
2320 {
2321 if (a < 0)
2322 {
2323 return std::numeric_limits<A>::lowest();
2324 }
2325 else
2326 {
2327 return (std::numeric_limits<A>::max)();
2328 }
2329 }
2330 return a;
2331 }
2332 };
2333
2334 struct nan_min
2335 {
2336 template <class T, class U>
2337 constexpr auto operator()(const T lhs, const U rhs) const
2338 {
2339 // Clunky expression for working with GCC 4.9
2340 return math::isnan(lhs)
2341 ? rhs
2342 : (math::isnan(rhs) ? lhs
2343 : std::common_type_t<T, U>(
2344 detail::make_xfunction<math::minimum<void>>(lhs, rhs)
2345 ));
2346 }
2347 };
2348
2349 struct nan_max
2350 {
2351 template <class T, class U>
2352 constexpr auto operator()(const T lhs, const U rhs) const
2353 {
2354 // Clunky expression for working with GCC 4.9
2355 return math::isnan(lhs)
2356 ? rhs
2357 : (math::isnan(rhs) ? lhs
2358 : std::common_type_t<T, U>(
2359 detail::make_xfunction<math::maximum<void>>(lhs, rhs)
2360 ));
2361 }
2362 };
2363
2364 struct nan_plus
2365 {
2366 template <class T, class U>
2367 constexpr auto operator()(const T lhs, const U rhs) const
2368 {
2369 return !math::isnan(rhs) ? lhs + rhs : lhs;
2370 }
2371 };
2372
2373 struct nan_multiplies
2374 {
2375 template <class T, class U>
2376 constexpr auto operator()(const T lhs, const U rhs) const
2377 {
2378 return !math::isnan(rhs) ? lhs * rhs : lhs;
2379 }
2380 };
2381
2382 template <class T, int V>
2383 struct nan_init
2384 {
2385 using value_type = T;
2386 using result_type = T;
2387
2388 constexpr result_type operator()(const value_type lhs) const
2389 {
2390 return math::isnan(lhs) ? result_type(V) : lhs;
2391 }
2392 };
2393 }
2394
2398
2409 template <class E>
2410 inline auto nan_to_num(E&& e)
2411 {
2412 return detail::make_xfunction<detail::nan_to_num_functor>(std::forward<E>(e));
2413 }
2414
2428 XTENSOR_REDUCER_FUNCTION(nanmin, detail::nan_min, typename std::decay_t<E>::value_type, std::nan("0"))
2429
2430
2443 XTENSOR_REDUCER_FUNCTION(nanmax, detail::nan_max, typename std::decay_t<E>::value_type, std::nan("0"))
2444
2460 XTENSOR_REDUCER_FUNCTION(nansum, detail::nan_plus, typename std::decay_t<E>::value_type, 0)
2461
2477 XTENSOR_REDUCER_FUNCTION(nanprod, detail::nan_multiplies, typename std::decay_t<E>::value_type, 1)
2478
2479#define COUNT_NON_ZEROS_CONTENT \
2480 using value_type = typename std::decay_t<E>::value_type; \
2481 using result_type = xt::detail::xreducer_size_type_t<value_type>; \
2482 using init_value_fct = xt::const_value<result_type>; \
2483 \
2484 auto init_fct = init_value_fct(0); \
2485 \
2486 auto reduce_fct = [](const auto& lhs, const auto& rhs) \
2487 { \
2488 using value_t = xt::detail::xreducer_temporary_type_t<std::decay_t<decltype(rhs)>>; \
2489 using result_t = std::decay_t<decltype(lhs)>; \
2490 \
2491 return (rhs != value_t(0)) ? lhs + result_t(1) : lhs; \
2492 }; \
2493 auto merge_func = detail::plus();
2494
2495 template <class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2496 inline auto count_nonzero(E&& e, EVS es = EVS())
2497 {
2498 COUNT_NON_ZEROS_CONTENT;
2499 return xt::reduce(
2500 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
2501 std::forward<E>(e),
2502 es
2503 );
2504 }
2505
2506 template <
2507 class E,
2508 class X,
2509 class EVS = DEFAULT_STRATEGY_REDUCERS,
2510 XTL_REQUIRES(std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<X>>)>
2511 inline auto count_nonzero(E&& e, X&& axes, EVS es = EVS())
2512 {
2513 COUNT_NON_ZEROS_CONTENT;
2514 return xt::reduce(
2515 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
2516 std::forward<E>(e),
2517 std::forward<X>(axes),
2518 es
2519 );
2520 }
2521
2522 template <
2523 class E,
2524 class X,
2525 class EVS = DEFAULT_STRATEGY_REDUCERS,
2526 XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<X>)>
2527 inline auto count_nonzero(E&& e, X axis, EVS es = EVS())
2528 {
2529 return count_nonzero(std::forward<E>(e), {axis}, es);
2530 }
2531
2532 template <class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2533 inline auto count_nonzero(E&& e, const I (&axes)[N], EVS es = EVS())
2534 {
2535 COUNT_NON_ZEROS_CONTENT;
2536 return xt::reduce(
2537 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
2538 std::forward<E>(e),
2539 axes,
2540 es
2541 );
2542 }
2543
2544#undef COUNT_NON_ZEROS_CONTENT
2545
2546 template <class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2547 inline auto count_nonnan(E&& e, EVS es = EVS())
2548 {
2549 return xt::count_nonzero(!xt::isnan(std::forward<E>(e)), es);
2550 }
2551
2552 template <
2553 class E,
2554 class X,
2555 class EVS = DEFAULT_STRATEGY_REDUCERS,
2556 XTL_REQUIRES(std::negation<is_reducer_options<X>>, std::negation<xtl::is_integral<X>>)>
2557 inline auto count_nonnan(E&& e, X&& axes, EVS es = EVS())
2558 {
2559 return xt::count_nonzero(!xt::isnan(std::forward<E>(e)), std::forward<X>(axes), es);
2560 }
2561
2562 template <
2563 class E,
2564 class X,
2565 class EVS = DEFAULT_STRATEGY_REDUCERS,
2566 XTL_REQUIRES(std::negation<is_reducer_options<X>>, xtl::is_integral<X>)>
2567 inline auto count_nonnan(E&& e, X&& axes, EVS es = EVS())
2568 {
2569 return xt::count_nonzero(!xt::isnan(std::forward<E>(e)), {axes}, es);
2570 }
2571
2572 template <class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2573 inline auto count_nonnan(E&& e, const I (&axes)[N], EVS es = EVS())
2574 {
2575 return xt::count_nonzero(!xt::isnan(std::forward<E>(e)), axes, es);
2576 }
2577
2592 template <class T = void, class E>
2593 inline auto nancumsum(E&& e, std::ptrdiff_t axis)
2594 {
2595 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2596 return accumulate(
2597 make_xaccumulator_functor(detail::nan_plus(), detail::nan_init<init_value_type, 0>()),
2598 std::forward<E>(e),
2599 axis
2600 );
2601 }
2602
2603 template <class T = void, class E>
2604 inline auto nancumsum(E&& e)
2605 {
2606 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2607 return accumulate(
2608 make_xaccumulator_functor(detail::nan_plus(), detail::nan_init<init_value_type, 0>()),
2609 std::forward<E>(e)
2610 );
2611 }
2612
2627 template <class T = void, class E>
2628 inline auto nancumprod(E&& e, std::ptrdiff_t axis)
2629 {
2630 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2631 return accumulate(
2632 make_xaccumulator_functor(detail::nan_multiplies(), detail::nan_init<init_value_type, 1>()),
2633 std::forward<E>(e),
2634 axis
2635 );
2636 }
2637
2638 template <class T = void, class E>
2639 inline auto nancumprod(E&& e)
2640 {
2641 using init_value_type = std::conditional_t<std::is_same<T, void>::value, typename std::decay_t<E>::value_type, T>;
2642 return accumulate(
2643 make_xaccumulator_functor(detail::nan_multiplies(), detail::nan_init<init_value_type, 1>()),
2644 std::forward<E>(e)
2645 );
2646 }
2647
2648 namespace detail
2649 {
2650 template <class T>
2651 struct diff_impl
2652 {
2653 template <class Arg>
2654 inline void operator()(
2655 Arg& ad,
2656 const std::size_t& n,
2657 xstrided_slice_vector& slice1,
2658 xstrided_slice_vector& slice2,
2659 std::size_t saxis
2660 )
2661 {
2662 for (std::size_t i = 0; i < n; ++i)
2663 {
2664 slice2[saxis] = range(xnone(), ad.shape()[saxis] - 1);
2665 ad = strided_view(ad, slice1) - strided_view(ad, slice2);
2666 }
2667 }
2668 };
2669
2670 template <>
2671 struct diff_impl<bool>
2672 {
2673 template <class Arg>
2674 inline void operator()(
2675 Arg& ad,
2676 const std::size_t& n,
2677 xstrided_slice_vector& slice1,
2678 xstrided_slice_vector& slice2,
2679 std::size_t saxis
2680 )
2681 {
2682 for (std::size_t i = 0; i < n; ++i)
2683 {
2684 slice2[saxis] = range(xnone(), ad.shape()[saxis] - 1);
2685 ad = not_equal(strided_view(ad, slice1), strided_view(ad, slice2));
2686 }
2687 }
2688 };
2689 }
2690
2706 template <
2707 class T = void,
2708 class E,
2709 class X,
2710 class EVS = DEFAULT_STRATEGY_REDUCERS,
2711 XTL_REQUIRES(std::negation<is_reducer_options<X>>)>
2712 inline auto nanmean(E&& e, X&& axes, EVS es = EVS())
2713 {
2714 decltype(auto) sc = detail::shared_forward<E>(e);
2715 // note: forcing copy of first axes argument -- is there a better solution?
2716 auto axes_copy = axes;
2717 using value_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
2718 using sum_type = typename std::conditional_t<
2719 std::is_same<T, void>::value,
2720 typename std::common_type_t<typename std::decay_t<E>::value_type, value_type>,
2721 T>;
2722 // sum cannot always be a double. It could be a complex number which cannot operate on
2723 // std::plus<double>.
2724 return nansum<sum_type>(sc, std::forward<X>(axes), es)
2725 / xt::cast<value_type>(count_nonnan(sc, std::move(axes_copy), es));
2726 }
2727
2728 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2729 inline auto nanmean(E&& e, EVS es = EVS())
2730 {
2731 decltype(auto) sc = detail::shared_forward<E>(e);
2732 using value_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
2733 using sum_type = typename std::conditional_t<
2734 std::is_same<T, void>::value,
2735 typename std::common_type_t<typename std::decay_t<E>::value_type, value_type>,
2736 T>;
2737 return nansum<sum_type>(sc, es) / xt::cast<value_type>(count_nonnan(sc, es));
2738 }
2739
2740 template <class T = void, class E, class I, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2741 inline auto nanmean(E&& e, const I (&axes)[N], EVS es = EVS())
2742 {
2743 return nanmean<T>(
2744 std::forward<E>(e),
2745 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2746 es
2747 );
2748 }
2749
2750 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2751 inline auto nanvar(E&& e, EVS es = EVS())
2752 {
2753 decltype(auto) sc = detail::shared_forward<E>(e);
2754 return nanmean<T>(square(sc - nanmean<T>(sc)), es);
2755 }
2756
2757 template <class T = void, class E, class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2758 inline auto nanstd(E&& e, EVS es = EVS())
2759 {
2760 return sqrt(nanvar<T>(std::forward<E>(e), es));
2761 }
2762
2783 template <
2784 class T = void,
2785 class E,
2786 class X,
2787 class EVS = DEFAULT_STRATEGY_REDUCERS,
2788 XTL_REQUIRES(std::negation<is_reducer_options<X>>)>
2789 inline auto nanvar(E&& e, X&& axes, EVS es = EVS())
2790 {
2791 decltype(auto) sc = detail::shared_forward<E>(e);
2792 // note: forcing copy of first axes argument -- is there a better solution?
2793 auto axes_copy = axes;
2794 using result_type = typename std::conditional_t<std::is_same<T, void>::value, double, T>;
2795 auto inner_mean = nanmean<result_type>(sc, std::move(axes_copy));
2796
2797 // fake keep_dims = 1
2798 // Since the inner_shape might have a reference semantic (e.g. xbuffer_adaptor in bindings)
2799 // We need to map it to another type before modifying it.
2800 // We pragmatically abuse `get_strides_t`
2801 using tmp_shape_t = get_strides_t<typename std::decay_t<E>::shape_type>;
2802 tmp_shape_t keep_dim_shape = xtl::forward_sequence<tmp_shape_t, decltype(e.shape())>(e.shape());
2803 for (const auto& el : axes)
2804 {
2805 keep_dim_shape[el] = 1;
2806 }
2807 auto mrv = reshape_view<XTENSOR_DEFAULT_LAYOUT>(std::move(inner_mean), std::move(keep_dim_shape));
2808 return nanmean<result_type>(square(cast<result_type>(sc) - std::move(mrv)), std::forward<X>(axes), es);
2809 }
2810
2831 template <
2832 class T = void,
2833 class E,
2834 class X,
2835 class EVS = DEFAULT_STRATEGY_REDUCERS,
2836 XTL_REQUIRES(std::negation<is_reducer_options<X>>)>
2837 inline auto nanstd(E&& e, X&& axes, EVS es = EVS())
2838 {
2839 return sqrt(nanvar<T>(std::forward<E>(e), std::forward<X>(axes), es));
2840 }
2841
2842 template <class T = void, class E, class A, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2843 inline auto nanstd(E&& e, const A (&axes)[N], EVS es = EVS())
2844 {
2845 return nanstd<T>(
2846 std::forward<E>(e),
2847 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2848 es
2849 );
2850 }
2851
2852 template <class T = void, class E, class A, std::size_t N, class EVS = DEFAULT_STRATEGY_REDUCERS>
2853 inline auto nanvar(E&& e, const A (&axes)[N], EVS es = EVS())
2854 {
2855 return nanvar<T>(
2856 std::forward<E>(e),
2857 xtl::forward_sequence<std::array<std::size_t, N>, decltype(axes)>(axes),
2858 es
2859 );
2860 }
2861
2873 template <class T>
2874 auto diff(const xexpression<T>& a, std::size_t n = 1, std::ptrdiff_t axis = -1)
2875 {
2876 typename std::decay_t<T>::temporary_type ad = a.derived_cast();
2877 std::size_t saxis = normalize_axis(ad.dimension(), axis);
2878 if (n <= ad.size())
2879 {
2880 if (n != std::size_t(0))
2881 {
2882 xstrided_slice_vector slice1(ad.dimension(), all());
2883 xstrided_slice_vector slice2(ad.dimension(), all());
2884 slice1[saxis] = range(1, xnone());
2885
2886 detail::diff_impl<typename T::value_type> impl;
2887 impl(ad, n, slice1, slice2, saxis);
2888 }
2889 }
2890 else
2891 {
2892 auto shape = ad.shape();
2893 shape[saxis] = std::size_t(0);
2894 ad.resize(shape);
2895 }
2896 return ad;
2897 }
2898
2910 template <class T>
2911 auto trapz(const xexpression<T>& y, double dx = 1.0, std::ptrdiff_t axis = -1)
2912 {
2913 auto& yd = y.derived_cast();
2914 std::size_t saxis = normalize_axis(yd.dimension(), axis);
2915
2916 xstrided_slice_vector slice1(yd.dimension(), all());
2917 xstrided_slice_vector slice2(yd.dimension(), all());
2918 slice1[saxis] = range(1, xnone());
2919 slice2[saxis] = range(xnone(), yd.shape()[saxis] - 1);
2920
2921 auto trap = dx * (strided_view(yd, slice1) + strided_view(yd, slice2)) * 0.5;
2922
2923 return eval(sum(trap, {saxis}));
2924 }
2925
2937 template <class T, class E>
2938 auto trapz(const xexpression<T>& y, const xexpression<E>& x, std::ptrdiff_t axis = -1)
2939 {
2940 auto& yd = y.derived_cast();
2941 auto& xd = x.derived_cast();
2942 decltype(diff(x)) dx;
2943
2944 std::size_t saxis = normalize_axis(yd.dimension(), axis);
2945
2946 if (xd.dimension() == 1)
2947 {
2948 dx = diff(x);
2949 typename std::decay_t<decltype(yd)>::shape_type shape;
2950 resize_container(shape, yd.dimension());
2951 std::fill(shape.begin(), shape.end(), 1);
2952 shape[saxis] = dx.shape()[0];
2953 dx.reshape(shape);
2954 }
2955 else
2956 {
2957 dx = diff(x, 1, axis);
2958 }
2959
2960 xstrided_slice_vector slice1(yd.dimension(), all());
2961 xstrided_slice_vector slice2(yd.dimension(), all());
2962 slice1[saxis] = range(1, xnone());
2963 slice2[saxis] = range(xnone(), yd.shape()[saxis] - 1);
2964
2965 auto trap = dx * (strided_view(yd, slice1) + strided_view(yd, slice2)) * 0.5;
2966
2967 return eval(sum(trap, {saxis}));
2968 }
2969
2982 template <class E1, class E2, class E3, typename T>
2983 inline auto interp(const E1& x, const E2& xp, const E3& fp, T left, T right)
2984 {
2985 using size_type = common_size_type_t<E1, E2, E3>;
2986 using value_type = typename E3::value_type;
2987
2988 // basic checks
2989 XTENSOR_ASSERT(xp.dimension() == 1);
2990 XTENSOR_ASSERT(std::is_sorted(x.cbegin(), x.cend()));
2991 XTENSOR_ASSERT(std::is_sorted(xp.cbegin(), xp.cend()));
2992
2993 // allocate output
2994 auto f = xtensor<value_type, 1>::from_shape(x.shape());
2995
2996 // counter in "x": from left
2997 size_type i = 0;
2998
2999 // fill f[i] for x[i] <= xp[0]
3000 for (; i < x.size(); ++i)
3001 {
3002 if (x[i] > xp[0])
3003 {
3004 break;
3005 }
3006 f[i] = static_cast<value_type>(left);
3007 }
3008
3009 // counter in "x": from right
3010 // (index counts one right, to terminate the reverse loop, without risking being negative)
3011 size_type imax = x.size();
3012
3013 // fill f[i] for x[-1] >= xp[-1]
3014 for (; imax > 0; --imax)
3015 {
3016 if (x[imax - 1] < xp[xp.size() - 1])
3017 {
3018 break;
3019 }
3020 f[imax - 1] = static_cast<value_type>(right);
3021 }
3022
3023 // catch edge case: all entries are "right"
3024 if (imax == 0)
3025 {
3026 return f;
3027 }
3028
3029 // set "imax" as actual index
3030 // (counted one right, see above)
3031 --imax;
3032
3033 // counter in "xp"
3034 size_type ip = 1;
3035
3036 // fill f[i] for the interior
3037 for (; i <= imax; ++i)
3038 {
3039 // - search next value in "xp"
3040 while (x[i] > xp[ip])
3041 {
3042 ++ip;
3043 }
3044 // - distances as doubles
3045 double dfp = static_cast<double>(fp[ip] - fp[ip - 1]);
3046 double dxp = static_cast<double>(xp[ip] - xp[ip - 1]);
3047 double dx = static_cast<double>(x[i] - xp[ip - 1]);
3048 // - interpolate
3049 f[i] = fp[ip - 1] + static_cast<value_type>(dfp / dxp * dx);
3050 }
3051
3052 return f;
3053 }
3054
3055 namespace detail
3056 {
3057 template <class E1, class E2>
3058 auto calculate_discontinuity(E1&& discontinuity, E2&&)
3059 {
3060 return discontinuity;
3061 }
3062
3063 template <class E2>
3064 auto calculate_discontinuity(xt::placeholders::xtuph, E2&& period)
3065 {
3066 return 0.5 * period;
3067 }
3068
3069 template <class E1, class E2>
3070 auto
3071 calculate_interval(E2&& period, typename std::enable_if<std::is_integral<E1>::value, E1>::type* = 0)
3072 {
3073 auto interval_high = 0.5 * period;
3074 uint64_t remainder = static_cast<uint64_t>(period) % 2;
3075 auto boundary_ambiguous = (remainder == 0);
3076 return std::make_tuple(interval_high, boundary_ambiguous);
3077 }
3078
3079 template <class E1, class E2>
3080 auto
3081 calculate_interval(E2&& period, typename std::enable_if<std::is_floating_point<E1>::value, E1>::type* = 0)
3082 {
3083 auto interval_high = 0.5 * period;
3084 auto boundary_ambiguous = true;
3085 return std::make_tuple(interval_high, boundary_ambiguous);
3086 }
3087 }
3088
3101
3102 template <class E1, class E2 = xt::placeholders::xtuph, class E3 = double>
3103 inline auto unwrap(
3104 E1&& p,
3105 E2 discontinuity = xnone(),
3106 std::ptrdiff_t axis = -1,
3107 E3 period = 2.0 * xt::numeric_constants<double>::PI
3108 )
3109 {
3110 auto discont = detail::calculate_discontinuity(discontinuity, period);
3111 using value_type = typename std::decay_t<E1>::value_type;
3112 std::size_t saxis = normalize_axis(p.dimension(), axis);
3113 auto dd = diff(p, 1, axis);
3114 xstrided_slice_vector slice(p.dimension(), all());
3115 slice[saxis] = range(1, xnone());
3116 auto interval_tuple = detail::calculate_interval<value_type>(period);
3117 auto interval_high = std::get<0>(interval_tuple);
3118 auto boundary_ambiguous = std::get<1>(interval_tuple);
3119 auto interval_low = -interval_high;
3120 auto ddmod = xt::eval(xt::fmod(xt::fmod(dd - interval_low, period) + period, period) + interval_low);
3121 if (boundary_ambiguous)
3122 {
3123 // for `mask = (abs(dd) == period/2)`, the above line made
3124 //`ddmod[mask] == -period/2`. correct these such that
3125 //`ddmod[mask] == sign(dd[mask])*period/2`.
3126 auto boolmap = xt::equal(ddmod, interval_low) && (xt::greater(dd, 0.0));
3127 ddmod = xt::where(boolmap, interval_high, ddmod);
3128 }
3129 auto ph_correct = xt::eval(ddmod - dd);
3130 ph_correct = xt::where(xt::abs(dd) < discont, 0.0, ph_correct);
3131 E1 up(p);
3132 strided_view(up, slice) = strided_view(p, slice)
3133 + xt::cumsum(ph_correct, static_cast<std::ptrdiff_t>(saxis));
3134 return up;
3135 }
3136
3147 template <class E1, class E2, class E3>
3148 inline auto interp(const E1& x, const E2& xp, const E3& fp)
3149 {
3150 return interp(x, xp, fp, fp[0], fp[fp.size() - 1]);
3151 }
3152
3159 template <class E1>
3160 inline auto cov(const E1& x, const E1& y = E1())
3161 {
3162 using value_type = typename E1::value_type;
3163
3164 if (y.dimension() == 0)
3165 {
3166 auto s = x.shape();
3167 using size_type = std::decay_t<decltype(s[0])>;
3168 if (x.dimension() == 1)
3169 {
3170 auto covar = eval(zeros<value_type>({1, 1}));
3171 auto x_norm = x - eval(mean(x));
3172 covar(0, 0) = std::inner_product(x_norm.begin(), x_norm.end(), x_norm.begin(), 0.0)
3173 / value_type(s[0] - 1);
3174 return covar;
3175 }
3176
3177 XTENSOR_ASSERT(x.dimension() == 2);
3178
3179 auto covar = eval(zeros<value_type>({s[0], s[0]}));
3180 auto m = eval(mean(x, {1}));
3181 m.reshape({m.shape()[0], 1});
3182 auto x_norm = x - m;
3183 for (size_type i = 0; i < s[0]; i++)
3184 {
3185 auto xi = strided_view(x_norm, {range(i, i + 1), all()});
3186 for (size_type j = i; j < s[0]; j++)
3187 {
3188 auto xj = strided_view(x_norm, {range(j, j + 1), all()});
3189 covar(j, i) = std::inner_product(xi.begin(), xi.end(), xj.begin(), 0.0)
3190 / value_type(s[1] - 1);
3191 }
3192 }
3193 return eval(covar + transpose(covar) - diag(diagonal(covar)));
3194 }
3195 else
3196 {
3197 return cov(eval(stack(xtuple(x, y))));
3198 }
3199 }
3200
3201 /*
3202 * convolution mode placeholders for selecting the algorithm
3203 * used in computing a 1D convolution.
3204 * Same as NumPy's mode parameter.
3205 */
3206 namespace convolve_mode
3207 {
3208 struct valid
3209 {
3210 };
3211
3212 struct full
3213 {
3214 };
3215 }
3216
3217 namespace detail
3218 {
3219 template <class E1, class E2>
3220 inline auto convolve_impl(E1&& e1, E2&& e2, convolve_mode::valid)
3221 {
3222 using value_type = typename std::decay<E1>::type::value_type;
3223
3224 const std::size_t na = e1.size();
3225 const std::size_t nv = e2.size();
3226 const std::size_t n = na - nv + 1;
3228 for (std::size_t i = 0; i < n; i++)
3229 {
3230 for (std::size_t j = 0; j < nv; j++)
3231 {
3232 out(i) += e1(j) * e2(j + i);
3233 }
3234 }
3235 return out;
3236 }
3237
3238 template <class E1, class E2>
3239 inline auto convolve_impl(E1&& e1, E2&& e2, convolve_mode::full)
3240 {
3241 using value_type = typename std::decay<E1>::type::value_type;
3242
3243 const std::size_t na = e1.size();
3244 const std::size_t nv = e2.size();
3245 const std::size_t n = na + nv - 1;
3247 for (std::size_t i = 0; i < n; i++)
3248 {
3249 const std::size_t jmn = (i >= nv - 1) ? i - (nv - 1) : 0;
3250 const std::size_t jmx = (i < na - 1) ? i : na - 1;
3251 for (std::size_t j = jmn; j <= jmx; ++j)
3252 {
3253 out(i) += e1(j) * e2(i - j);
3254 }
3255 }
3256 return out;
3257 }
3258 }
3259
3260 /*
3261 * @brief computes the 1D convolution between two 1D expressions
3262 *
3263 * @param a 1D expression
3264 * @param v 1D expression
3265 * @param mode placeholder Select algorithm #convolve_mode
3266 *
3267 * @detail the algorithm convolves a with v and will incur a copy overhead
3268 * should v be longer than a.
3269 */
3270 template <class E1, class E2, class E3>
3271 inline auto convolve(E1&& a, E2&& v, E3 mode)
3272 {
3273 if (a.dimension() != 1 || v.dimension() != 1)
3274 {
3275 XTENSOR_THROW(std::runtime_error, "Invalid dimentions convolution arguments must be 1D expressions");
3276 }
3277
3278 XTENSOR_ASSERT(a.size() > 0 && v.size() > 0);
3279
3280 // swap them so a is always the longest one
3281 if (a.size() < v.size())
3282 {
3283 return detail::convolve_impl(std::forward<E2>(v), std::forward<E1>(a), mode);
3284 }
3285 else
3286 {
3287 return detail::convolve_impl(std::forward<E1>(a), std::forward<E2>(v), mode);
3288 }
3289 }
3290}
3291
3292
3293#endif
Base class for xexpressions.
derived_type & derived_cast() &noexcept
Returns a reference to the actual derived type of the xexpression.
auto cumprod(E &&e, std::ptrdiff_t axis)
Cumulative product.
Definition xmath.hpp:2284
auto cumsum(E &&e, std::ptrdiff_t axis)
Cumulative sum.
Definition xmath.hpp:2249
auto fma(E1 &&e1, E2 &&e2, E3 &&e3) noexcept -> detail::xfunction_type_t< math::fma_fun, E1, E2, E3 >
Fused multiply-add operation.
Definition xmath.hpp:511
auto deg2rad(E &&e) noexcept -> detail::xfunction_type_t< math::deg2rad, E >
Convert angles from degrees to radians.
Definition xmath.hpp:685
auto amax(E &&e, X &&axes, EVS es=EVS())
Maximum element along given axis.
Definition xmath.hpp:783
auto remainder(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::remainder_fun, E1, E2 >
Signed remainder of the division operation.
Definition xmath.hpp:493
auto degrees(E &&e) noexcept -> detail::xfunction_type_t< math::rad2deg, E >
Convert angles from radians to degrees.
Definition xmath.hpp:730
auto interp(const E1 &x, const E2 &xp, const E3 &fp, T left, T right)
Returns the one-dimensional piecewise linear interpolant to a function with given discrete data point...
Definition xmath.hpp:2983
auto fmod(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmod_fun, E1, E2 >
Remainder of the floating point division operation.
Definition xmath.hpp:476
auto abs(E &&e) noexcept -> detail::xfunction_type_t< math::abs_fun, E >
Absolute value function.
Definition xmath.hpp:444
auto fabs(E &&e) noexcept -> detail::xfunction_type_t< math::fabs_fun, E >
Absolute value function.
Definition xmath.hpp:459
auto minimum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::minimum< void >, E1, E2 >
Elementwise minimum.
Definition xmath.hpp:762
auto maximum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::maximum< void >, E1, E2 >
Elementwise maximum.
Definition xmath.hpp:746
auto fmax(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmax_fun, E1, E2 >
Maximum function.
Definition xmath.hpp:532
auto clip(E1 &&e1, E2 &&lo, E3 &&hi) noexcept -> detail::xfunction_type_t< math::clamp_fun, E1, E2, E3 >
Clip values between hi and lo.
Definition xmath.hpp:816
auto radians(E &&e) noexcept -> detail::xfunction_type_t< math::deg2rad, E >
Convert angles from degrees to radians.
Definition xmath.hpp:700
auto fdim(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fdim_fun, E1, E2 >
Positive difference function.
Definition xmath.hpp:566
auto amin(E &&e, X &&axes, EVS es=EVS())
Minimum element along given axis.
Definition xmath.hpp:801
auto rad2deg(E &&e) noexcept -> detail::xfunction_type_t< math::rad2deg, E >
Convert angles from radians to degrees.
Definition xmath.hpp:715
auto fmin(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmin_fun, E1, E2 >
Minimum function.
Definition xmath.hpp:549
auto sign(E &&e) noexcept -> detail::xfunction_type_t< math::sign_fun, E >
Returns an element-wise indication of the sign of a number.
Definition xmath.hpp:878
auto unwrap(E1 &&p, E2 discontinuity=xnone(), std::ptrdiff_t axis=-1, E3 period=2.0 *xt::numeric_constants< double >::PI)
Unwrap by taking the complement of large deltas with respect to the period.
Definition xmath.hpp:3103
auto cast(E &&e) noexcept -> detail::xfunction_type_t< typename detail::cast< R >::functor, E >
Element-wise static_cast.
auto allclose(E1 &&e1, E2 &&e2, double rtol=1e-05, double atol=1e-08) noexcept
Check if all elements in e1 are close to the corresponding elements in e2.
Definition xmath.hpp:1776
auto isfinite(E &&e) noexcept -> detail::xfunction_type_t< math::isfinite_fun, E >
finite value check
Definition xmath.hpp:1641
auto isnan(E &&e) noexcept -> detail::xfunction_type_t< math::isnan_fun, E >
NaN check.
Definition xmath.hpp:1671
auto isclose(E1 &&e1, E2 &&e2, double rtol=1e-05, double atol=1e-08, bool equal_nan=false) noexcept
Element-wise closeness detection.
Definition xmath.hpp:1753
auto isinf(E &&e) noexcept -> detail::xfunction_type_t< math::isinf_fun, E >
infinity check
Definition xmath.hpp:1656
auto not_equal(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::not_equal_to, E1, E2 >
Element-wise inequality.
auto equal(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< detail::equal_to, E1, E2 >
Element-wise equality.
auto greater(E1 &&e1, E2 &&e2) noexcept -> decltype(std::forward< E1 >(e1) > std::forward< E2 >(e2))
Greater than.
auto lgamma(E &&e) noexcept -> detail::xfunction_type_t< math::lgamma_fun, E >
Natural logarithm of the gamma function.
Definition xmath.hpp:1517
auto erfc(E &&e) noexcept -> detail::xfunction_type_t< math::erfc_fun, E >
Complementary error function.
Definition xmath.hpp:1487
auto erf(E &&e) noexcept -> detail::xfunction_type_t< math::erf_fun, E >
Error function.
Definition xmath.hpp:1472
auto tgamma(E &&e) noexcept -> detail::xfunction_type_t< math::tgamma_fun, E >
Gamma function.
Definition xmath.hpp:1502
auto log1p(E &&e) noexcept -> detail::xfunction_type_t< math::log1p_fun, E >
Natural logarithm of one plus function.
Definition xmath.hpp:991
auto expm1(E &&e) noexcept -> detail::xfunction_type_t< math::expm1_fun, E >
Natural exponential minus one function.
Definition xmath.hpp:931
auto exp2(E &&e) noexcept -> detail::xfunction_type_t< math::exp2_fun, E >
Base 2 exponential function.
Definition xmath.hpp:916
auto log(E &&e) noexcept -> detail::xfunction_type_t< math::log_fun, E >
Natural logarithm function.
Definition xmath.hpp:946
auto log2(E &&e) noexcept -> detail::xfunction_type_t< math::log2_fun, E >
Base 2 logarithm function.
Definition xmath.hpp:976
auto exp(E &&e) noexcept -> detail::xfunction_type_t< math::exp_fun, E >
Natural exponential function.
Definition xmath.hpp:901
auto log10(E &&e) noexcept -> detail::xfunction_type_t< math::log10_fun, E >
Base 10 logarithm function.
Definition xmath.hpp:961
auto asinh(E &&e) noexcept -> detail::xfunction_type_t< math::asinh_fun, E >
Inverse hyperbolic sine function.
Definition xmath.hpp:1419
auto tanh(E &&e) noexcept -> detail::xfunction_type_t< math::tanh_fun, E >
Hyperbolic tangent function.
Definition xmath.hpp:1404
auto cosh(E &&e) noexcept -> detail::xfunction_type_t< math::cosh_fun, E >
Hyperbolic cosine function.
Definition xmath.hpp:1389
auto sinh(E &&e) noexcept -> detail::xfunction_type_t< math::sinh_fun, E >
Hyperbolic sine function.
Definition xmath.hpp:1374
auto acosh(E &&e) noexcept -> detail::xfunction_type_t< math::acosh_fun, E >
Inverse hyperbolic cosine function.
Definition xmath.hpp:1434
auto atanh(E &&e) noexcept -> detail::xfunction_type_t< math::atanh_fun, E >
Inverse hyperbolic tangent function.
Definition xmath.hpp:1449
bool all(E &&e)
Any.
auto where(E1 &&e1, E2 &&e2, E3 &&e3) noexcept -> detail::xfunction_type_t< detail::conditional_ternary, E1, E2, E3 >
Ternary selection.
auto nanmax(E &&e, X &&axes, EVS es=EVS())
Maximum element along given axes, ignoring NaNs.
Definition xmath.hpp:2443
auto nancumsum(E &&e, std::ptrdiff_t axis)
Cumulative sum, replacing nan with 0.
Definition xmath.hpp:2593
auto nancumprod(E &&e, std::ptrdiff_t axis)
Cumulative product, replacing nan with 1.
Definition xmath.hpp:2628
auto nanmean(E &&e, X &&axes, EVS es=EVS())
Mean of elements over given axes, excluding NaNs.
Definition xmath.hpp:2712
auto nanmin(E &&e, X &&axes, EVS es=EVS())
Minimum element over given axes, ignoring NaNs.
Definition xmath.hpp:2428
auto nanprod(E &&e, X &&axes, EVS es=EVS())
Product of elements over given axes, replacing NaN with 1.
Definition xmath.hpp:2477
auto nansum(E &&e, X &&axes, EVS es=EVS())
Sum of elements over given axes, replacing NaN with 0.
Definition xmath.hpp:2460
auto nan_to_num(E &&e)
Convert nan or +/- inf to numbers.
Definition xmath.hpp:2410
auto ceil(E &&e) noexcept -> detail::xfunction_type_t< math::ceil_fun, E >
ceil function.
Definition xmath.hpp:1540
auto trunc(E &&e) noexcept -> detail::xfunction_type_t< math::trunc_fun, E >
trunc function.
Definition xmath.hpp:1570
auto nearbyint(E &&e) noexcept -> detail::xfunction_type_t< math::nearbyint_fun, E >
nearbyint function.
Definition xmath.hpp:1602
auto floor(E &&e) noexcept -> detail::xfunction_type_t< math::floor_fun, E >
floor function.
Definition xmath.hpp:1555
auto round(E &&e) noexcept -> detail::xfunction_type_t< math::round_fun, E >
round function.
Definition xmath.hpp:1586
auto rint(E &&e) noexcept -> detail::xfunction_type_t< math::rint_fun, E >
rint function.
Definition xmath.hpp:1618
auto cube(E1 &&e1) noexcept
Cube power function, equivalent to e1 * e1 * e1.
Definition xmath.hpp:1121
auto sqrt(E &&e) noexcept -> detail::xfunction_type_t< math::sqrt_fun, E >
Square root function.
Definition xmath.hpp:1202
auto square(E1 &&e1) noexcept
Square power function, equivalent to e1 * e1.
Definition xmath.hpp:1102
auto pow(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::pow_fun, E1, E2 >
Power function.
Definition xmath.hpp:1016
auto hypot(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::hypot_fun, E1, E2 >
Hypotenuse function.
Definition xmath.hpp:1235
auto cbrt(E &&e) noexcept -> detail::xfunction_type_t< math::cbrt_fun, E >
Cubic root function.
Definition xmath.hpp:1217
auto sum(E &&e, X &&axes, EVS es=EVS())
Sum of elements over given axes.
Definition xmath.hpp:1804
auto stddev(E &&e, X &&axes, EVS es=EVS())
Compute the standard deviation along the specified axis.
Definition xmath.hpp:2144
auto variance(E &&e, X &&axes, const D &ddof, EVS es=EVS())
Average of elements over given axes using weights.
Definition xmath.hpp:2083
auto prod(E &&e, X &&axes, EVS es=EVS())
Product of elements over given axes.
Definition xmath.hpp:1824
auto trapz(const xexpression< T > &y, double dx=1.0, std::ptrdiff_t axis=-1)
Integrate along the given axis using the composite trapezoidal rule.
Definition xmath.hpp:2911
auto diff(const xexpression< T > &a, std::size_t n=1, std::ptrdiff_t axis=-1)
Calculate the n-th discrete difference along the given axis.
Definition xmath.hpp:2874
auto minmax(E &&e, EVS es=EVS())
Minimum and maximum among the elements of an array or expression.
Definition xmath.hpp:2197
auto mean(E &&e, X &&axes, EVS es=EVS())
Mean of elements over given axes.
Definition xmath.hpp:1898
auto atan(E &&e) noexcept -> detail::xfunction_type_t< math::atan_fun, E >
Arctangent function.
Definition xmath.hpp:1333
auto atan2(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::atan2_fun, E1, E2 >
Artangent function, using signs to determine quadrants.
Definition xmath.hpp:1351
auto asin(E &&e) noexcept -> detail::xfunction_type_t< math::asin_fun, E >
Arcsine function.
Definition xmath.hpp:1303
auto cos(E &&e) noexcept -> detail::xfunction_type_t< math::cos_fun, E >
Cosine function.
Definition xmath.hpp:1273
auto sin(E &&e) noexcept -> detail::xfunction_type_t< math::sin_fun, E >
Sine function.
Definition xmath.hpp:1258
auto tan(E &&e) noexcept -> detail::xfunction_type_t< math::tan_fun, E >
Tangent function.
Definition xmath.hpp:1288
auto acos(E &&e) noexcept -> detail::xfunction_type_t< math::acos_fun, E >
Arccosine function.
Definition xmath.hpp:1318
auto conj(E &&e) noexcept
Return an xt::xfunction evaluating to the complex conjugate of the given expression.
Definition xcomplex.hpp:207
auto eval(T &&t) -> std::enable_if_t< detail::is_container< std::decay_t< T > >::value, T && >
Force evaluation of xexpression.
Definition xeval.hpp:46
auto transpose(E &&e) noexcept
Returns a transpose view by reversing the dimensions of xexpression e.
bool same_shape(const S1 &s1, const S2 &s2) noexcept
Check if two objects have the same shape.
Definition xshape.hpp:109
standard mathematical functions for xexpressions
auto stack(std::tuple< CT... > &&t, std::size_t axis=0)
Stack xexpressions along axis.
Definition xbuilder.hpp:883
auto range(A start_val, B stop_val)
Select a range from start_val to stop_val (excluded).
Definition xslice.hpp:737
auto arange(T start, T stop, S step=1) noexcept
Generates numbers evenly spaced within given half-open interval [start, stop).
Definition xbuilder.hpp:432
auto all() noexcept
Returns a slice representing a full dimension, to be used as an argument of view function.
Definition xslice.hpp:221
std::vector< xstrided_slice< std::ptrdiff_t > > xstrided_slice_vector
vector of slices used to build a xstrided_view
auto make_lambda_xfunction(F &&lambda, E &&... args)
Create a xfunction from a lambda.
Definition xmath.hpp:1086
auto reduce(F &&f, E &&e, X &&axes, EVS &&options=EVS())
Returns an xexpression applying the specified reducing function to an expression over the given axes.
Definition xreducer.hpp:993
layout_type
Definition xlayout.hpp:24
auto zeros(S shape) noexcept
Returns an xexpression containing zeros of the specified shape.
Definition xbuilder.hpp:66
auto accumulate(F &&f, E &&e, EVS evaluation_strategy=EVS())
Accumulate and flatten array NOTE This function is not lazy!
xtensor_container< uvector< T, A >, N, L > xtensor
Alias template on xtensor_container with default parameters for data container type.
auto diagonal(E &&arr, int offset=0, std::size_t axis_1=0, std::size_t axis_2=1)
Returns the elements on the diagonal of arr If arr has more than two dimensions, then the axes specif...
xshared_expression< E > make_xshared(xexpression< E > &&expr)
Helper function to create shared expression from any xexpression.
auto strided_view(E &&e, S &&shape, X &&stride, std::size_t offset=0, layout_type layout=L) noexcept
Construct a strided view from an xexpression, shape, strides and offset.
auto diag(E &&arr, int k=0)
xexpression with values of arr on the diagonal, zeroes otherwise
auto xtuple(Types &&... args)
Creates tuples from arguments for concatenate and stack.
Definition xbuilder.hpp:707
auto cov(const E1 &x, const E1 &y=E1())
Returns the covariance matrix.
Definition xmath.hpp:3160