diff --git a/include/xtensor/xmath.hpp b/include/xtensor/xmath.hpp index 37d61500f..9d80f243b 100644 --- a/include/xtensor/xmath.hpp +++ b/include/xtensor/xmath.hpp @@ -2610,8 +2610,8 @@ namespace detail { XTL_REQUIRES(is_reducer_options)> inline auto nanvar(E&& e, EVS es = EVS()) { - decltype(auto) sc = detail::shared_forward(e); - return nanmean(square(sc - nanmean(sc)), es); + auto cached_mean = nanmean(e, es)(); + return nanmean(square(std::forward(e) - cached_mean), es); } template ::value, double, T>; - auto inner_mean = nanmean(sc, std::move(axes_copy)); + auto inner_mean = eval(nanmean(sc, std::move(axes_copy), evaluation_strategy::immediate)); // fake keep_dims = 1 auto keep_dim_shape = e.shape();