Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Simon Heybrock 5 : #pragma once 6 : 7 : #include "scipp/core/flags.h" 8 : #include "scipp/variable/astype.h" 9 : #include "scipp/variable/reciprocal.h" 10 : #include "scipp/variable/variable.h" 11 : #include "scipp/variable/variable_factory.h" 12 : 13 : namespace scipp::variable { 14 : 15 : // Helpers for in-place reductions and reductions with groupby. 16 : SCIPP_VARIABLE_EXPORT Variable mean_impl(const Variable &var, const Dim dim, 17 : const Variable &masks_sum); 18 : SCIPP_VARIABLE_EXPORT Variable nanmean_impl(const Variable &var, const Dim dim, 19 : const Variable &masks_sum); 20 : 21 318 : template <class T> T normalize_impl(const T &numerator, T denominator) { 22 : // Numerator may be an int or a Eigen::Vector3d => use double 23 : // This approach would be wrong if we supported vectors of float 24 318 : const auto type = 25 318 : numerator.dtype() == dtype<float> ? dtype<float> : dtype<double>; 26 318 : denominator.setUnit(units::one); 27 : return numerator * 28 318 : reciprocal(astype(denominator, type, CopyPolicy::TryAvoid)); 29 : } 30 : 31 : SCIPP_VARIABLE_EXPORT void expect_valid_bin_indices(const Variable &indices, 32 : const Dim dim, 33 : const Sizes &buffer_sizes); 34 : 35 : template <class T> 36 : Variable make_bins_impl(Variable indices, const Dim dim, T &&buffer); 37 : 38 42459 : template <class T, class Op> auto reduce_all_dims(const T &obj, const Op &op) { 39 42459 : if (obj.dims().empty()) { 40 20842 : if (is_bins(obj)) 41 29 : return op(obj, Dim::Invalid); 42 : else 43 20813 : return copy(obj); 44 : } 45 21617 : auto out = op(obj, obj.dims().inner()); 46 22558 : while (!out.dims().empty()) 47 941 : out = op(out, out.dims().inner()); 48 21617 : return out; 49 21617 : } 50 : 51 : } // namespace scipp::variable