14#ifndef XTENSOR_MATH_HPP
15#define XTENSOR_MATH_HPP
24#include <xtl/xcomplex.hpp>
25#include <xtl/xsequence.hpp>
26#include <xtl/xtype_traits.hpp>
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"
39 template <
class T =
double>
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>;
60#define XTENSOR_UNSIGNED_ABS_FUNC(T) \
61 constexpr inline T abs(const T& x) \
66#define XTENSOR_INT_SPECIALIZATION_IMPL(FUNC_NAME, RETURN_VAL, T) \
67 constexpr inline bool FUNC_NAME(const T& ) noexcept \
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);
85#define XTENSOR_UNARY_MATH_FUNCTOR(NAME) \
89 constexpr auto operator()(const T& arg) const \
95 constexpr auto simd_apply(const B& arg) const \
102#define XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(NAME) \
106 constexpr auto operator()(const T& arg) const \
112 constexpr auto simd_apply(const B& arg) const \
119#define XTENSOR_BINARY_MATH_FUNCTOR(NAME) \
122 template <class T1, class T2> \
123 constexpr auto operator()(const T1& arg1, const T2& arg2) const \
126 return NAME(arg1, arg2); \
129 constexpr auto simd_apply(const B& arg1, const B& arg2) const \
132 return NAME(arg1, arg2); \
136#define XTENSOR_TERNARY_MATH_FUNCTOR(NAME) \
139 template <class T1, class T2, class T3> \
140 constexpr auto operator()(const T1& arg1, const T2& arg2, const T3& arg3) const \
143 return NAME(arg1, arg2, arg3); \
146 auto simd_apply(const B& arg1, const B& arg2, const B& arg3) const \
149 return NAME(arg1, arg2, arg3); \
189 using std::nearbyint;
190 using std::remainder;
208#if !defined(_MSC_VER)
219 using std::fpclassify;
225 inline std::enable_if_t<xtl::is_arithmetic<T>::value,
bool>
isinf(
const T& t)
227 return bool(std::isinf(t));
231 inline std::enable_if_t<xtl::is_arithmetic<T>::value,
bool> isnan(
const T& t)
233 return bool(std::isnan(t));
237 inline std::enable_if_t<xtl::is_arithmetic<T>::value,
bool> isfinite(
const T& t)
239 return bool(std::isfinite(t));
245 inline bool isinf(
const std::complex<T>& c)
247 return std::isinf(std::real(c)) || std::isinf(std::imag(c));
251 inline bool isnan(
const std::complex<T>& c)
253 return std::isnan(std::real(c)) || std::isnan(std::imag(c));
257 inline bool isfinite(
const std::complex<T>& c)
259 return !isinf(c) && !isnan(c);
264#if defined(_WIN32) && defined(XTENSOR_USE_XSIMD)
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)
291 XTENSOR_INT_SPECIALIZATION(isinf,
false);
292 XTENSOR_INT_SPECIALIZATION(isnan,
false);
293 XTENSOR_INT_SPECIALIZATION(isfinite,
true);
296 XTENSOR_UNARY_MATH_FUNCTOR_COMPLEX_REDUCING(abs);
298 XTENSOR_UNARY_MATH_FUNCTOR(
fabs);
299 XTENSOR_BINARY_MATH_FUNCTOR(
fmod);
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);
308 XTENSOR_UNARY_MATH_FUNCTOR(
log);
310 XTENSOR_UNARY_MATH_FUNCTOR(
log2);
312 XTENSOR_BINARY_MATH_FUNCTOR(
pow);
313 XTENSOR_UNARY_MATH_FUNCTOR(
sqrt);
314 XTENSOR_UNARY_MATH_FUNCTOR(
cbrt);
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);
323 XTENSOR_UNARY_MATH_FUNCTOR(
sinh);
324 XTENSOR_UNARY_MATH_FUNCTOR(
cosh);
325 XTENSOR_UNARY_MATH_FUNCTOR(
tanh);
329 XTENSOR_UNARY_MATH_FUNCTOR(
erf);
330 XTENSOR_UNARY_MATH_FUNCTOR(
erfc);
333 XTENSOR_UNARY_MATH_FUNCTOR(
ceil);
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);
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
353 template <
class R,
class T>
354 std::enable_if_t<!iterable_expression<R>, R> fill_init(T init)
359 template <
class R,
class T>
360 std::enable_if_t<iterable_expression<R>, R> fill_init(T init)
363 std::fill(std::begin(result), std::end(result), init);
368#define XTENSOR_REDUCER_FUNCTION(NAME, FUNCTOR, INIT_VALUE_TYPE, INIT) \
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()) \
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>; \
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), \
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()) \
396 return NAME(std::forward<E>(e), {axis}, es); \
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()) \
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>; \
406 make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
407 std::forward<E>(e), \
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()) \
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>; \
419 make_xreducer_functor(functor_type(), init_value_fct(detail::fill_init<init_value_type>(INIT))), \
420 std::forward<E>(e), \
444 inline auto abs(E&& e)
noexcept -> detail::xfunction_type_t<math::abs_fun, E>
446 return detail::make_xfunction<math::abs_fun>(std::forward<E>(e));
459 inline auto fabs(E&& e)
noexcept -> detail::xfunction_type_t<math::fabs_fun, E>
461 return detail::make_xfunction<math::fabs_fun>(std::forward<E>(e));
475 template <
class E1,
class E2>
476 inline auto fmod(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::fmod_fun, E1, E2>
478 return detail::make_xfunction<math::fmod_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
492 template <
class E1,
class E2>
493 inline auto remainder(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::remainder_fun, E1, E2>
495 return detail::make_xfunction<math::remainder_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
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>
513 return detail::make_xfunction<math::fma_fun>(
514 std::forward<E1>(e1),
515 std::forward<E2>(e2),
531 template <
class E1,
class E2>
532 inline auto fmax(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::fmax_fun, E1, E2>
534 return detail::make_xfunction<math::fmax_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
548 template <
class E1,
class E2>
549 inline auto fmin(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::fmin_fun, E1, E2>
551 return detail::make_xfunction<math::fmin_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
565 template <
class E1,
class E2>
566 inline auto fdim(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::fdim_fun, E1, E2>
568 return detail::make_xfunction<math::fdim_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
573 template <
class T =
void>
576 template <
class A1,
class A2>
577 constexpr auto operator()(
const A1& t1,
const A2& t2)
const noexcept
579 return xtl::select(t1 < t2, t1, t2);
582 template <
class A1,
class A2>
583 constexpr auto simd_apply(
const A1& t1,
const A2& t2)
const noexcept
585 return xt_simd::select(t1 < t2, t1, t2);
589 template <
class T =
void>
592 template <
class A1,
class A2>
593 constexpr auto operator()(
const A1& t1,
const A2& t2)
const noexcept
595 return xtl::select(t1 > t2, t1, t2);
598 template <
class A1,
class A2>
599 constexpr auto simd_apply(
const A1& t1,
const A2& t2)
const noexcept
601 return xt_simd::select(t1 > t2, t1, t2);
607 template <
class A1,
class A2,
class A3>
608 constexpr auto operator()(
const A1& v,
const A2& lo,
const A3& hi)
const
610 return xtl::select(lo < hi, xtl::select(v < lo, lo, xtl::select(hi < v, hi, v)), hi);
613 template <
class A1,
class A2,
class A3>
614 constexpr auto simd_apply(
const A1& v,
const A2& lo,
const A3& hi)
const
616 return xt_simd::select(lo < hi, xt_simd::select(v < lo, lo, xt_simd::select(hi < v, hi, v)), hi);
622 template <class A, std::enable_if_t<xtl::is_integral<A>::value,
int> = 0>
623 constexpr double operator()(
const A& a)
const noexcept
625 return a * xt::numeric_constants<double>::PI / 180.0;
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
631 return a * xt::numeric_constants<A>::PI / A(180.0);
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
637 return a * xt::numeric_constants<double>::PI / 180.0;
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
643 return a * xt::numeric_constants<A>::PI / A(180.0);
649 template <class A, std::enable_if_t<xtl::is_integral<A>::value,
int> = 0>
650 constexpr double operator()(
const A& a)
const noexcept
652 return a * 180.0 / xt::numeric_constants<double>::PI;
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
658 return a * A(180.0) / xt::numeric_constants<A>::PI;
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
664 return a * 180.0 / xt::numeric_constants<double>::PI;
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
670 return a * A(180.0) / xt::numeric_constants<A>::PI;
685 inline auto deg2rad(E&& e)
noexcept -> detail::xfunction_type_t<math::deg2rad, E>
687 return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
700 inline auto radians(E&& e)
noexcept -> detail::xfunction_type_t<math::deg2rad, E>
702 return detail::make_xfunction<math::deg2rad>(std::forward<E>(e));
715 inline auto rad2deg(E&& e)
noexcept -> detail::xfunction_type_t<math::rad2deg, E>
717 return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
730 inline auto degrees(E&& e)
noexcept -> detail::xfunction_type_t<math::rad2deg, E>
732 return detail::make_xfunction<math::rad2deg>(std::forward<E>(e));
745 template <
class E1,
class E2>
746 inline auto maximum(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::maximum<void>, E1, E2>
748 return detail::make_xfunction<math::maximum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
761 template <
class E1,
class E2>
762 inline auto minimum(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::minimum<void>, E1, E2>
764 return detail::make_xfunction<math::minimum<void>>(std::forward<E1>(e1), std::forward<E2>(e2));
778 XTENSOR_REDUCER_FUNCTION(
781 typename std::decay_t<E>::value_type,
782 std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::lowest()
796 XTENSOR_REDUCER_FUNCTION(
799 typename std::decay_t<E>::value_type,
800 std::numeric_limits<xvalue_type_t<std::decay_t<E>>>::max()
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>
819 return detail::make_xfunction<math::clamp_fun>(
820 std::forward<E1>(e1),
821 std::forward<E2>(lo),
831 template <
class XT = T>
832 static constexpr std::enable_if_t<xtl::is_signed<XT>::value, T> run(T x)
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));
839 template <
class XT = T>
840 static constexpr std::enable_if_t<xtl::is_complex<XT>::value, T> run(T x)
843 sign_impl<typename T::value_type>::run(
844 (x.real() !=
typename T::value_type(0)) ? x.real() : x.imag()
850 template <
class XT = T>
851 static constexpr std::enable_if_t<std::is_unsigned<XT>::value, T> run(T x)
860 constexpr auto operator()(
const T& x)
const
862 return sign_impl<T>::run(x);
878 inline auto sign(E&& e)
noexcept -> detail::xfunction_type_t<math::sign_fun, E>
880 return detail::make_xfunction<math::sign_fun>(std::forward<E>(e));
901 inline auto exp(E&& e)
noexcept -> detail::xfunction_type_t<math::exp_fun, E>
903 return detail::make_xfunction<math::exp_fun>(std::forward<E>(e));
916 inline auto exp2(E&& e)
noexcept -> detail::xfunction_type_t<math::exp2_fun, E>
918 return detail::make_xfunction<math::exp2_fun>(std::forward<E>(e));
931 inline auto expm1(E&& e)
noexcept -> detail::xfunction_type_t<math::expm1_fun, E>
933 return detail::make_xfunction<math::expm1_fun>(std::forward<E>(e));
946 inline auto log(E&& e)
noexcept -> detail::xfunction_type_t<math::log_fun, E>
948 return detail::make_xfunction<math::log_fun>(std::forward<E>(e));
961 inline auto log10(E&& e)
noexcept -> detail::xfunction_type_t<math::log10_fun, E>
963 return detail::make_xfunction<math::log10_fun>(std::forward<E>(e));
976 inline auto log2(E&& e)
noexcept -> detail::xfunction_type_t<math::log2_fun, E>
978 return detail::make_xfunction<math::log2_fun>(std::forward<E>(e));
991 inline auto log1p(E&& e)
noexcept -> detail::xfunction_type_t<math::log1p_fun, E>
993 return detail::make_xfunction<math::log1p_fun>(std::forward<E>(e));
1015 template <
class E1,
class E2>
1016 inline auto pow(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::pow_fun, E1, E2>
1018 return detail::make_xfunction<math::pow_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
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(...);
1027 template <
class... T>
1030 template <
class F,
class... T>
1031 struct supports<F(T...)> : decltype(supports_test(std::declval<F>(), std::declval<T>()...))
1038 explicit lambda_adapt(F&& lmbd)
1039 : m_lambda(std::move(lmbd))
1043 template <
class... T>
1044 auto operator()(T... args)
const
1046 return m_lambda(args...);
1049 template <
class... T, XTL_REQUIRES(detail::supports<F(T...)>)>
1050 auto simd_apply(T... args)
const
1052 return m_lambda(args...);
1085 template <
class F,
class... E>
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)...);
1104 auto fnct = [](
auto x) ->
decltype(x * x)
1123 auto fnct = [](
auto x) ->
decltype(x * x * x)
1133 template <std::
size_t N>
1136 template <std::
size_t N>
1140 auto operator()(T v)
const ->
decltype(v * v)
1142 T temp = pow_impl<N / 2>{}(v);
1143 return temp * temp * pow_impl<N & 1>{}(v);
1151 auto operator()(T v)
const -> T
1161 auto operator()(T )
const -> T
1185 template <std::
size_t N,
class E>
1186 inline auto pow(E&& e)
noexcept
1188 static_assert(N > 0,
"integer power cannot be negative");
1202 inline auto sqrt(E&& e)
noexcept -> detail::xfunction_type_t<math::sqrt_fun, E>
1204 return detail::make_xfunction<math::sqrt_fun>(std::forward<E>(e));
1217 inline auto cbrt(E&& e)
noexcept -> detail::xfunction_type_t<math::cbrt_fun, E>
1219 return detail::make_xfunction<math::cbrt_fun>(std::forward<E>(e));
1234 template <
class E1,
class E2>
1235 inline auto hypot(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::hypot_fun, E1, E2>
1237 return detail::make_xfunction<math::hypot_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
1258 inline auto sin(E&& e)
noexcept -> detail::xfunction_type_t<math::sin_fun, E>
1260 return detail::make_xfunction<math::sin_fun>(std::forward<E>(e));
1273 inline auto cos(E&& e)
noexcept -> detail::xfunction_type_t<math::cos_fun, E>
1275 return detail::make_xfunction<math::cos_fun>(std::forward<E>(e));
1288 inline auto tan(E&& e)
noexcept -> detail::xfunction_type_t<math::tan_fun, E>
1290 return detail::make_xfunction<math::tan_fun>(std::forward<E>(e));
1303 inline auto asin(E&& e)
noexcept -> detail::xfunction_type_t<math::asin_fun, E>
1305 return detail::make_xfunction<math::asin_fun>(std::forward<E>(e));
1318 inline auto acos(E&& e)
noexcept -> detail::xfunction_type_t<math::acos_fun, E>
1320 return detail::make_xfunction<math::acos_fun>(std::forward<E>(e));
1333 inline auto atan(E&& e)
noexcept -> detail::xfunction_type_t<math::atan_fun, E>
1335 return detail::make_xfunction<math::atan_fun>(std::forward<E>(e));
1350 template <
class E1,
class E2>
1351 inline auto atan2(E1&& e1, E2&& e2)
noexcept -> detail::xfunction_type_t<math::atan2_fun, E1, E2>
1353 return detail::make_xfunction<math::atan2_fun>(std::forward<E1>(e1), std::forward<E2>(e2));
1374 inline auto sinh(E&& e)
noexcept -> detail::xfunction_type_t<math::sinh_fun, E>
1376 return detail::make_xfunction<math::sinh_fun>(std::forward<E>(e));
1389 inline auto cosh(E&& e)
noexcept -> detail::xfunction_type_t<math::cosh_fun, E>
1391 return detail::make_xfunction<math::cosh_fun>(std::forward<E>(e));
1404 inline auto tanh(E&& e)
noexcept -> detail::xfunction_type_t<math::tanh_fun, E>
1406 return detail::make_xfunction<math::tanh_fun>(std::forward<E>(e));
1419 inline auto asinh(E&& e)
noexcept -> detail::xfunction_type_t<math::asinh_fun, E>
1421 return detail::make_xfunction<math::asinh_fun>(std::forward<E>(e));
1434 inline auto acosh(E&& e)
noexcept -> detail::xfunction_type_t<math::acosh_fun, E>
1436 return detail::make_xfunction<math::acosh_fun>(std::forward<E>(e));
1449 inline auto atanh(E&& e)
noexcept -> detail::xfunction_type_t<math::atanh_fun, E>
1451 return detail::make_xfunction<math::atanh_fun>(std::forward<E>(e));
1472 inline auto erf(E&& e)
noexcept -> detail::xfunction_type_t<math::erf_fun, E>
1474 return detail::make_xfunction<math::erf_fun>(std::forward<E>(e));
1487 inline auto erfc(E&& e)
noexcept -> detail::xfunction_type_t<math::erfc_fun, E>
1489 return detail::make_xfunction<math::erfc_fun>(std::forward<E>(e));
1502 inline auto tgamma(E&& e)
noexcept -> detail::xfunction_type_t<math::tgamma_fun, E>
1504 return detail::make_xfunction<math::tgamma_fun>(std::forward<E>(e));
1517 inline auto lgamma(E&& e)
noexcept -> detail::xfunction_type_t<math::lgamma_fun, E>
1519 return detail::make_xfunction<math::lgamma_fun>(std::forward<E>(e));
1540 inline auto ceil(E&& e)
noexcept -> detail::xfunction_type_t<math::ceil_fun, E>
1542 return detail::make_xfunction<math::ceil_fun>(std::forward<E>(e));
1555 inline auto floor(E&& e)
noexcept -> detail::xfunction_type_t<math::floor_fun, E>
1557 return detail::make_xfunction<math::floor_fun>(std::forward<E>(e));
1570 inline auto trunc(E&& e)
noexcept -> detail::xfunction_type_t<math::trunc_fun, E>
1572 return detail::make_xfunction<math::trunc_fun>(std::forward<E>(e));
1586 inline auto round(E&& e)
noexcept -> detail::xfunction_type_t<math::round_fun, E>
1588 return detail::make_xfunction<math::round_fun>(std::forward<E>(e));
1602 inline auto nearbyint(E&& e)
noexcept -> detail::xfunction_type_t<math::nearbyint_fun, E>
1604 return detail::make_xfunction<math::nearbyint_fun>(std::forward<E>(e));
1618 inline auto rint(E&& e)
noexcept -> detail::xfunction_type_t<math::rint_fun, E>
1620 return detail::make_xfunction<math::rint_fun>(std::forward<E>(e));
1641 inline auto isfinite(E&& e)
noexcept -> detail::xfunction_type_t<math::isfinite_fun, E>
1643 return detail::make_xfunction<math::isfinite_fun>(std::forward<E>(e));
1656 inline auto isinf(E&& e)
noexcept -> detail::xfunction_type_t<math::isinf_fun, E>
1658 return detail::make_xfunction<math::isinf_fun>(std::forward<E>(e));
1671 inline auto isnan(E&& e)
noexcept -> detail::xfunction_type_t<math::isnan_fun, E>
1673 return detail::make_xfunction<math::isnan_fun>(std::forward<E>(e));
1678 template <
class FUNCTOR,
class T, std::size_t... Is>
1679 inline auto get_functor(T&& args, std::index_sequence<Is...>)
1681 return FUNCTOR(std::get<Is>(args)...);
1684 template <
class F,
class... A,
class... E>
1685 inline auto make_xfunction(std::tuple<A...>&& f_args, E&&... e)
noexcept
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)>{}
1694 return type(std::move(functor), std::forward<E>(e)...);
1699 using result_type = bool;
1701 isclose(
double rtol,
double atol,
bool equal_nan)
1704 , m_equal_nan(equal_nan)
1708 template <
class A1,
class A2>
1709 bool operator()(
const A1& a,
const A2& b)
const
1711 using internal_type = xtl::promote_type_t<A1, A2, double>;
1712 if (math::isnan(a) && math::isnan(b))
1716 if (math::isinf(a) && math::isinf(b))
1721 auto d = math::abs(internal_type(a) - internal_type(b));
1724 * double((std::max)(math::abs(internal_type(a)), math::abs(internal_type(b)))
1751 template <
class E1,
class E2>
1753 isclose(E1&& e1, E2&& e2,
double rtol = 1e-05,
double atol = 1e-08,
bool equal_nan =
false) noexcept
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)
1775 template <
class E1,
class E2>
1776 inline auto allclose(E1&& e1, E2&& e2,
double rtol = 1e-05,
double atol = 1e-08) noexcept
1778 return xt::all(
isclose(std::forward<E1>(e1), std::forward<E2>(e2), rtol, atol));
1804 XTENSOR_REDUCER_FUNCTION(
sum, detail::plus,
typename std::decay_t<E>::value_type, 0)
1824 XTENSOR_REDUCER_FUNCTION(
prod, detail::multiplies, typename std::decay_t<E>::value_type, 1)
1828 template <
class T,
class S,
class ST>
1829 inline auto mean_division(S&& s, ST e_size)
1831 using value_type =
typename std::conditional_t<std::is_same<T, void>::value, double, T>;
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);
1844 inline auto mean(E&& e, X&& axes,
const D& ddof, EVS es)
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));
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)
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));
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)
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)));
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())
1900 return detail::mean<T>(std::forward<E>(e), std::forward<X>(axes), 0u, es);
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())
1906 return detail::mean_noaxis<T>(std::forward<E>(e), 0u, es);
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())
1912 return detail::mean<T>(std::forward<E>(e), axes, 0u, es);
1937 class EVS = DEFAULT_STRATEGY_REDUCERS,
1939 inline auto average(E&& e, W&& weights, X&& axes, EVS ev = EVS())
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)
1946 if (weights.size() != e.shape()[ax[0]])
1948 XTENSOR_THROW(std::runtime_error,
"Weights need to have the same shape as expression at axes.");
1951 std::fill(broadcast_shape.begin(), broadcast_shape.end(), std::size_t(1));
1952 broadcast_shape[ax[0]] = weights.size();
1960 "Weights with dim > 1 need to have the same shape as expression."
1964 std::copy_n(e.shape().begin(), e.shape().size(), broadcast_shape.begin());
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);
1978 class EVS = DEFAULT_STRATEGY_REDUCERS,
1980 inline auto average(E&& e, W&& weights, X axis, EVS ev = EVS())
1982 return average(std::forward<E>(e), std::forward<W>(weights), {axis}, std::forward<EVS>(ev));
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())
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);
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())
1996 if (weights.dimension() != e.dimension()
1997 || !std::equal(weights.shape().begin(), weights.shape().end(), e.shape().begin()))
1999 XTENSOR_THROW(std::runtime_error,
"Weights need to have the same shape as expression.");
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);
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())
2015 template <
typename E>
2016 std::enable_if_t<std::is_lvalue_reference<E>::value, E> shared_forward(E e)
noexcept
2021 template <
typename E>
2022 std::enable_if_t<!std::is_lvalue_reference<E>::value, xshared_expression<E>> shared_forward(E e)
noexcept
2032 class EVS = DEFAULT_STRATEGY_REDUCERS,
2034 inline auto variance(E&& e,
const D& ddof, EVS es = EVS())
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);
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())
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())
2081 class EVS = DEFAULT_STRATEGY_REDUCERS,
2083 inline auto variance(E&& e, X&& axes,
const D& ddof, EVS es = EVS())
2085 decltype(
auto) sc = detail::shared_forward<E>(e);
2087 auto axes_copy = axes;
2089 auto inner_mean =
eval(
mean<T>(sc, std::move(axes_copy), evaluation_strategy::immediate));
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)
2099 keep_dim_shape[el] = 1u;
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);
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())
2114 return variance<T>(std::forward<E>(e), std::forward<X>(axes), 0u, es);
2142 class EVS = DEFAULT_STRATEGY_REDUCERS,
2144 inline auto stddev(E&& e, X&& axes, EVS es = EVS())
2146 return sqrt(
variance<T>(std::forward<E>(e), std::forward<X>(axes), es));
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())
2154 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
2164 class EVS = DEFAULT_STRATEGY_REDUCERS,
2166 inline auto variance(E&& e,
const A (&axes)[N], EVS es = EVS())
2170 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
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())
2180 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
2196 template <
class E,
class EVS = DEFAULT_STRATEGY_REDUCERS, XTL_REQUIRES(is_reducer_options<EVS>)>
2201 using value_type =
typename std::decay_t<E>::value_type;
2202 using result_type = std::array<value_type, 2>;
2205 auto reduce_func = [](
auto r,
const auto& v)
2207 r[0] = (min) (r[0], v);
2208 r[1] = (max) (r[1], v);
2212 auto init_func = init_value_fct(
2213 result_type{std::numeric_limits<value_type>::max(), std::numeric_limits<value_type>::lowest()}
2216 auto merge_func = [](
auto r,
const auto& s)
2218 r[0] = (min) (r[0], s[0]);
2219 r[1] = (max) (r[1], s[1]);
2223 make_xreducer_functor(std::move(reduce_func), std::move(init_func), std::move(merge_func)),
2248 template <
class T =
void,
class E>
2249 inline auto cumsum(E&& e, std::ptrdiff_t axis)
2251 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2253 make_xaccumulator_functor(detail::plus(), detail::accumulator_identity<init_value_type>()),
2259 template <
class T =
void,
class E>
2260 inline auto cumsum(E&& e)
2262 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2264 make_xaccumulator_functor(detail::plus(), detail::accumulator_identity<init_value_type>()),
2283 template <
class T =
void,
class E>
2286 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2288 make_xaccumulator_functor(detail::multiplies(), detail::accumulator_identity<init_value_type>()),
2294 template <
class T =
void,
class E>
2297 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2299 make_xaccumulator_functor(detail::multiplies(), detail::accumulator_identity<init_value_type>()),
2310 struct nan_to_num_functor
2313 inline auto operator()(
const A& a)
const
2323 return std::numeric_limits<A>::lowest();
2327 return (std::numeric_limits<A>::max)();
2336 template <
class T,
class U>
2337 constexpr auto operator()(
const T lhs,
const U rhs)
const
2340 return math::isnan(lhs)
2342 : (math::isnan(rhs) ? lhs
2343 : std::common_type_t<T, U>(
2344 detail::make_xfunction<math::minimum<void>>(lhs, rhs)
2351 template <
class T,
class U>
2352 constexpr auto operator()(
const T lhs,
const U rhs)
const
2355 return math::isnan(lhs)
2357 : (math::isnan(rhs) ? lhs
2358 : std::common_type_t<T, U>(
2359 detail::make_xfunction<math::maximum<void>>(lhs, rhs)
2366 template <
class T,
class U>
2367 constexpr auto operator()(
const T lhs,
const U rhs)
const
2369 return !math::isnan(rhs) ? lhs + rhs : lhs;
2373 struct nan_multiplies
2375 template <
class T,
class U>
2376 constexpr auto operator()(
const T lhs,
const U rhs)
const
2378 return !math::isnan(rhs) ? lhs * rhs : lhs;
2382 template <
class T,
int V>
2385 using value_type = T;
2386 using result_type = T;
2388 constexpr result_type operator()(
const value_type lhs)
const
2390 return math::isnan(lhs) ? result_type(V) : lhs;
2412 return detail::make_xfunction<detail::nan_to_num_functor>(std::forward<E>(e));
2428 XTENSOR_REDUCER_FUNCTION(
nanmin, detail::nan_min,
typename std::decay_t<E>::value_type, std::nan(
"0"))
2443 XTENSOR_REDUCER_FUNCTION(
nanmax, detail::nan_max, typename std::decay_t<E>::value_type, std::nan(
"0"))
2460 XTENSOR_REDUCER_FUNCTION(
nansum, detail::nan_plus, typename std::decay_t<E>::value_type, 0)
2477 XTENSOR_REDUCER_FUNCTION(
nanprod, detail::nan_multiplies, typename std::decay_t<E>::value_type, 1)
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>; \
2484 auto init_fct = init_value_fct(0); \
2486 auto reduce_fct = [](const auto& lhs, const auto& rhs) \
2488 using value_t = xt::detail::xreducer_temporary_type_t<std::decay_t<decltype(rhs)>>; \
2489 using result_t = std::decay_t<decltype(lhs)>; \
2491 return (rhs != value_t(0)) ? lhs + result_t(1) : lhs; \
2493 auto merge_func = detail::plus();
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())
2498 COUNT_NON_ZEROS_CONTENT;
2500 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
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())
2513 COUNT_NON_ZEROS_CONTENT;
2515 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
2517 std::forward<X>(axes),
2525 class EVS = DEFAULT_STRATEGY_REDUCERS,
2527 inline auto count_nonzero(E&& e, X axis, EVS es = EVS())
2529 return count_nonzero(std::forward<E>(e), {axis}, es);
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())
2535 COUNT_NON_ZEROS_CONTENT;
2537 make_xreducer_functor(std::move(reduce_fct), std::move(init_fct), std::move(merge_func)),
2544#undef COUNT_NON_ZEROS_CONTENT
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())
2549 return xt::count_nonzero(!
xt::isnan(std::forward<E>(e)), es);
2555 class EVS = DEFAULT_STRATEGY_REDUCERS,
2557 inline auto count_nonnan(E&& e, X&& axes, EVS es = EVS())
2559 return xt::count_nonzero(!
xt::isnan(std::forward<E>(e)), std::forward<X>(axes), es);
2565 class EVS = DEFAULT_STRATEGY_REDUCERS,
2567 inline auto count_nonnan(E&& e, X&& axes, EVS es = EVS())
2569 return xt::count_nonzero(!
xt::isnan(std::forward<E>(e)), {axes}, es);
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())
2575 return xt::count_nonzero(!
xt::isnan(std::forward<E>(e)), axes, es);
2592 template <
class T =
void,
class E>
2595 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2597 make_xaccumulator_functor(detail::nan_plus(), detail::nan_init<init_value_type, 0>()),
2603 template <
class T =
void,
class E>
2606 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2608 make_xaccumulator_functor(detail::nan_plus(), detail::nan_init<init_value_type, 0>()),
2627 template <
class T =
void,
class E>
2630 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2632 make_xaccumulator_functor(detail::nan_multiplies(), detail::nan_init<init_value_type, 1>()),
2638 template <
class T =
void,
class E>
2641 using init_value_type = std::conditional_t<std::is_same<T, void>::value,
typename std::decay_t<E>::value_type, T>;
2643 make_xaccumulator_functor(detail::nan_multiplies(), detail::nan_init<init_value_type, 1>()),
2653 template <
class Arg>
2654 inline void operator()(
2656 const std::size_t& n,
2662 for (std::size_t i = 0; i < n; ++i)
2664 slice2[saxis] =
range(xnone(), ad.shape()[saxis] - 1);
2671 struct diff_impl<bool>
2673 template <
class Arg>
2674 inline void operator()(
2676 const std::size_t& n,
2682 for (std::size_t i = 0; i < n; ++i)
2684 slice2[saxis] =
range(xnone(), ad.shape()[saxis] - 1);
2710 class EVS = DEFAULT_STRATEGY_REDUCERS,
2712 inline auto nanmean(E&& e, X&& axes, EVS es = EVS())
2714 decltype(
auto) sc = detail::shared_forward<E>(e);
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>,
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())
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>,
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())
2745 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
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())
2753 decltype(
auto) sc = detail::shared_forward<E>(e);
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())
2760 return sqrt(nanvar<T>(std::forward<E>(e), es));
2787 class EVS = DEFAULT_STRATEGY_REDUCERS,
2789 inline auto nanvar(E&& e, X&& axes, EVS es = EVS())
2791 decltype(
auto) sc = detail::shared_forward<E>(e);
2793 auto axes_copy = axes;
2794 using result_type =
typename std::conditional_t<std::is_same<T, void>::value, double, 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)
2805 keep_dim_shape[el] = 1;
2807 auto mrv = reshape_view<XTENSOR_DEFAULT_LAYOUT>(std::move(inner_mean), std::move(keep_dim_shape));
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())
2839 return sqrt(nanvar<T>(std::forward<E>(e), std::forward<X>(axes), es));
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())
2847 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
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())
2857 xtl::forward_sequence<std::array<std::size_t, N>,
decltype(axes)>(axes),
2876 typename std::decay_t<T>::temporary_type ad = a.
derived_cast();
2877 std::size_t saxis = normalize_axis(ad.dimension(), axis);
2880 if (n != std::size_t(0))
2884 slice1[saxis] =
range(1, xnone());
2886 detail::diff_impl<typename T::value_type> impl;
2887 impl(ad, n, slice1, slice2, saxis);
2892 auto shape = ad.shape();
2893 shape[saxis] = std::size_t(0);
2914 std::size_t saxis = normalize_axis(yd.dimension(), axis);
2918 slice1[saxis] =
range(1, xnone());
2919 slice2[saxis] =
range(xnone(), yd.shape()[saxis] - 1);
2923 return eval(
sum(trap, {saxis}));
2937 template <
class T,
class E>
2942 decltype(
diff(x)) dx;
2944 std::size_t saxis = normalize_axis(yd.dimension(), axis);
2946 if (xd.dimension() == 1)
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];
2957 dx =
diff(x, 1, axis);
2962 slice1[saxis] =
range(1, xnone());
2963 slice2[saxis] =
range(xnone(), yd.shape()[saxis] - 1);
2967 return eval(
sum(trap, {saxis}));
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)
2985 using size_type = common_size_type_t<E1, E2, E3>;
2986 using value_type =
typename E3::value_type;
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()));
2994 auto f = xtensor<value_type, 1>::from_shape(x.shape());
3000 for (; i < x.size(); ++i)
3006 f[i] =
static_cast<value_type
>(left);
3011 size_type imax = x.size();
3014 for (; imax > 0; --imax)
3016 if (x[imax - 1] < xp[xp.size() - 1])
3020 f[imax - 1] =
static_cast<value_type
>(right);
3037 for (; i <= imax; ++i)
3040 while (x[i] > xp[ip])
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]);
3049 f[i] = fp[ip - 1] +
static_cast<value_type
>(dfp / dxp * dx);
3057 template <
class E1,
class E2>
3058 auto calculate_discontinuity(E1&& discontinuity, E2&&)
3060 return discontinuity;
3064 auto calculate_discontinuity(xt::placeholders::xtuph, E2&& period)
3066 return 0.5 * period;
3069 template <
class E1,
class E2>
3071 calculate_interval(E2&& period,
typename std::enable_if<std::is_integral<E1>::value, E1>::type* = 0)
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);
3079 template <
class E1,
class E2>
3081 calculate_interval(E2&& period,
typename std::enable_if<std::is_floating_point<E1>::value, E1>::type* = 0)
3083 auto interval_high = 0.5 * period;
3084 auto boundary_ambiguous =
true;
3085 return std::make_tuple(interval_high, boundary_ambiguous);
3102 template <
class E1,
class E2 = xt::placeholders::xtuph,
class E3 =
double>
3105 E2 discontinuity = xnone(),
3106 std::ptrdiff_t axis = -1,
3107 E3 period = 2.0 * xt::numeric_constants<double>::PI
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);
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;
3121 if (boundary_ambiguous)
3127 ddmod =
xt::where(boolmap, interval_high, ddmod);
3129 auto ph_correct =
xt::eval(ddmod - dd);
3133 +
xt::cumsum(ph_correct,
static_cast<std::ptrdiff_t
>(saxis));
3147 template <
class E1,
class E2,
class E3>
3148 inline auto interp(
const E1& x,
const E2& xp,
const E3& fp)
3150 return interp(x, xp, fp, fp[0], fp[fp.size() - 1]);
3160 inline auto cov(
const E1& x,
const E1& y = E1())
3162 using value_type =
typename E1::value_type;
3164 if (y.dimension() == 0)
3167 using size_type = std::decay_t<
decltype(s[0])>;
3168 if (x.dimension() == 1)
3172 covar(0, 0) = std::inner_product(x_norm.begin(), x_norm.end(), x_norm.begin(), 0.0)
3173 / value_type(s[0] - 1);
3177 XTENSOR_ASSERT(x.dimension() == 2);
3181 m.reshape({m.shape()[0], 1});
3182 auto x_norm = x - m;
3183 for (size_type i = 0; i < s[0]; i++)
3186 for (size_type j = i; j < s[0]; j++)
3189 covar(j, i) = std::inner_product(xi.begin(), xi.end(), xj.begin(), 0.0)
3190 / value_type(s[1] - 1);
3206 namespace convolve_mode
3219 template <
class E1,
class E2>
3222 using value_type =
typename std::decay<E1>::type::value_type;
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++)
3230 for (std::size_t j = 0; j < nv; j++)
3232 out(i) += e1(j) * e2(j + i);
3238 template <
class E1,
class E2>
3239 inline auto convolve_impl(E1&& e1, E2&& e2, convolve_mode::full)
3241 using value_type =
typename std::decay<E1>::type::value_type;
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++)
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)
3253 out(i) += e1(j) * e2(i - j);
3270 template <
class E1,
class E2,
class E3>
3271 inline auto convolve(E1&& a, E2&& v, E3 mode)
3273 if (a.dimension() != 1 || v.dimension() != 1)
3275 XTENSOR_THROW(std::runtime_error,
"Invalid dimentions convolution arguments must be 1D expressions");
3278 XTENSOR_ASSERT(a.size() > 0 && v.size() > 0);
3281 if (a.size() < v.size())
3283 return detail::convolve_impl(std::forward<E2>(v), std::forward<E1>(a), mode);
3287 return detail::convolve_impl(std::forward<E1>(a), std::forward<E2>(v), mode);
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.
auto cumsum(E &&e, std::ptrdiff_t axis)
Cumulative sum.
auto fma(E1 &&e1, E2 &&e2, E3 &&e3) noexcept -> detail::xfunction_type_t< math::fma_fun, E1, E2, E3 >
Fused multiply-add operation.
auto deg2rad(E &&e) noexcept -> detail::xfunction_type_t< math::deg2rad, E >
Convert angles from degrees to radians.
auto amax(E &&e, X &&axes, EVS es=EVS())
Maximum element along given axis.
auto remainder(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::remainder_fun, E1, E2 >
Signed remainder of the division operation.
auto degrees(E &&e) noexcept -> detail::xfunction_type_t< math::rad2deg, E >
Convert angles from radians to degrees.
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...
auto fmod(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmod_fun, E1, E2 >
Remainder of the floating point division operation.
auto abs(E &&e) noexcept -> detail::xfunction_type_t< math::abs_fun, E >
Absolute value function.
auto fabs(E &&e) noexcept -> detail::xfunction_type_t< math::fabs_fun, E >
Absolute value function.
auto minimum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::minimum< void >, E1, E2 >
Elementwise minimum.
auto maximum(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::maximum< void >, E1, E2 >
Elementwise maximum.
auto fmax(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmax_fun, E1, E2 >
Maximum function.
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.
auto radians(E &&e) noexcept -> detail::xfunction_type_t< math::deg2rad, E >
Convert angles from degrees to radians.
auto fdim(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fdim_fun, E1, E2 >
Positive difference function.
auto amin(E &&e, X &&axes, EVS es=EVS())
Minimum element along given axis.
auto rad2deg(E &&e) noexcept -> detail::xfunction_type_t< math::rad2deg, E >
Convert angles from radians to degrees.
auto fmin(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::fmin_fun, E1, E2 >
Minimum function.
auto sign(E &&e) noexcept -> detail::xfunction_type_t< math::sign_fun, E >
Returns an element-wise indication of the sign of a number.
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.
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.
auto isfinite(E &&e) noexcept -> detail::xfunction_type_t< math::isfinite_fun, E >
finite value check
auto isnan(E &&e) noexcept -> detail::xfunction_type_t< math::isnan_fun, E >
NaN check.
auto isclose(E1 &&e1, E2 &&e2, double rtol=1e-05, double atol=1e-08, bool equal_nan=false) noexcept
Element-wise closeness detection.
auto isinf(E &&e) noexcept -> detail::xfunction_type_t< math::isinf_fun, E >
infinity check
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.
auto erfc(E &&e) noexcept -> detail::xfunction_type_t< math::erfc_fun, E >
Complementary error function.
auto erf(E &&e) noexcept -> detail::xfunction_type_t< math::erf_fun, E >
Error function.
auto tgamma(E &&e) noexcept -> detail::xfunction_type_t< math::tgamma_fun, E >
Gamma function.
auto log1p(E &&e) noexcept -> detail::xfunction_type_t< math::log1p_fun, E >
Natural logarithm of one plus function.
auto expm1(E &&e) noexcept -> detail::xfunction_type_t< math::expm1_fun, E >
Natural exponential minus one function.
auto exp2(E &&e) noexcept -> detail::xfunction_type_t< math::exp2_fun, E >
Base 2 exponential function.
auto log(E &&e) noexcept -> detail::xfunction_type_t< math::log_fun, E >
Natural logarithm function.
auto log2(E &&e) noexcept -> detail::xfunction_type_t< math::log2_fun, E >
Base 2 logarithm function.
auto exp(E &&e) noexcept -> detail::xfunction_type_t< math::exp_fun, E >
Natural exponential function.
auto log10(E &&e) noexcept -> detail::xfunction_type_t< math::log10_fun, E >
Base 10 logarithm function.
auto asinh(E &&e) noexcept -> detail::xfunction_type_t< math::asinh_fun, E >
Inverse hyperbolic sine function.
auto tanh(E &&e) noexcept -> detail::xfunction_type_t< math::tanh_fun, E >
Hyperbolic tangent function.
auto cosh(E &&e) noexcept -> detail::xfunction_type_t< math::cosh_fun, E >
Hyperbolic cosine function.
auto sinh(E &&e) noexcept -> detail::xfunction_type_t< math::sinh_fun, E >
Hyperbolic sine function.
auto acosh(E &&e) noexcept -> detail::xfunction_type_t< math::acosh_fun, E >
Inverse hyperbolic cosine function.
auto atanh(E &&e) noexcept -> detail::xfunction_type_t< math::atanh_fun, E >
Inverse hyperbolic tangent function.
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.
auto nancumsum(E &&e, std::ptrdiff_t axis)
Cumulative sum, replacing nan with 0.
auto nancumprod(E &&e, std::ptrdiff_t axis)
Cumulative product, replacing nan with 1.
auto nanmean(E &&e, X &&axes, EVS es=EVS())
Mean of elements over given axes, excluding NaNs.
auto nanmin(E &&e, X &&axes, EVS es=EVS())
Minimum element over given axes, ignoring NaNs.
auto nanprod(E &&e, X &&axes, EVS es=EVS())
Product of elements over given axes, replacing NaN with 1.
auto nansum(E &&e, X &&axes, EVS es=EVS())
Sum of elements over given axes, replacing NaN with 0.
auto nan_to_num(E &&e)
Convert nan or +/- inf to numbers.
auto ceil(E &&e) noexcept -> detail::xfunction_type_t< math::ceil_fun, E >
ceil function.
auto trunc(E &&e) noexcept -> detail::xfunction_type_t< math::trunc_fun, E >
trunc function.
auto nearbyint(E &&e) noexcept -> detail::xfunction_type_t< math::nearbyint_fun, E >
nearbyint function.
auto floor(E &&e) noexcept -> detail::xfunction_type_t< math::floor_fun, E >
floor function.
auto round(E &&e) noexcept -> detail::xfunction_type_t< math::round_fun, E >
round function.
auto rint(E &&e) noexcept -> detail::xfunction_type_t< math::rint_fun, E >
rint function.
auto cube(E1 &&e1) noexcept
Cube power function, equivalent to e1 * e1 * e1.
auto sqrt(E &&e) noexcept -> detail::xfunction_type_t< math::sqrt_fun, E >
Square root function.
auto square(E1 &&e1) noexcept
Square power function, equivalent to e1 * e1.
auto pow(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::pow_fun, E1, E2 >
Power function.
auto hypot(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::hypot_fun, E1, E2 >
Hypotenuse function.
auto cbrt(E &&e) noexcept -> detail::xfunction_type_t< math::cbrt_fun, E >
Cubic root function.
auto sum(E &&e, X &&axes, EVS es=EVS())
Sum of elements over given axes.
auto stddev(E &&e, X &&axes, EVS es=EVS())
Compute the standard deviation along the specified axis.
auto variance(E &&e, X &&axes, const D &ddof, EVS es=EVS())
Average of elements over given axes using weights.
auto prod(E &&e, X &&axes, EVS es=EVS())
Product of elements over given axes.
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.
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.
auto minmax(E &&e, EVS es=EVS())
Minimum and maximum among the elements of an array or expression.
auto mean(E &&e, X &&axes, EVS es=EVS())
Mean of elements over given axes.
auto atan(E &&e) noexcept -> detail::xfunction_type_t< math::atan_fun, E >
Arctangent function.
auto atan2(E1 &&e1, E2 &&e2) noexcept -> detail::xfunction_type_t< math::atan2_fun, E1, E2 >
Artangent function, using signs to determine quadrants.
auto asin(E &&e) noexcept -> detail::xfunction_type_t< math::asin_fun, E >
Arcsine function.
auto cos(E &&e) noexcept -> detail::xfunction_type_t< math::cos_fun, E >
Cosine function.
auto sin(E &&e) noexcept -> detail::xfunction_type_t< math::sin_fun, E >
Sine function.
auto tan(E &&e) noexcept -> detail::xfunction_type_t< math::tan_fun, E >
Tangent function.
auto acos(E &&e) noexcept -> detail::xfunction_type_t< math::acos_fun, E >
Arccosine function.
auto conj(E &&e) noexcept
Return an xt::xfunction evaluating to the complex conjugate of the given expression.
auto eval(T &&t) -> std::enable_if_t< detail::is_container< std::decay_t< T > >::value, T && >
Force evaluation of xexpression.
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.
standard mathematical functions for xexpressions
auto stack(std::tuple< CT... > &&t, std::size_t axis=0)
Stack xexpressions along axis.
auto range(A start_val, B stop_val)
Select a range from start_val to stop_val (excluded).
auto arange(T start, T stop, S step=1) noexcept
Generates numbers evenly spaced within given half-open interval [start, stop).
auto all() noexcept
Returns a slice representing a full dimension, to be used as an argument of view function.
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.
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.
auto zeros(S shape) noexcept
Returns an xexpression containing zeros of the specified shape.
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.
auto cov(const E1 &x, const E1 &y=E1())
Returns the covariance matrix.