14#ifndef XTENSOR_HISTOGRAM_HPP
15#define XTENSOR_HISTOGRAM_HPP
17#include "xset_operation.hpp"
22using namespace xt::placeholders;
35 template <
class E1,
class E2>
38 XTENSOR_ASSERT(
bin_edges.dimension() == 1);
49 template <
class R =
double,
class E1,
class E2,
class E3>
53 using value_type =
typename std::decay_t<E3>::value_type;
55 XTENSOR_ASSERT(data.dimension() == 1);
56 XTENSOR_ASSERT(
weights.dimension() == 1);
57 XTENSOR_ASSERT(
bin_edges.dimension() == 1);
58 XTENSOR_ASSERT(
weights.size() == data.size());
71 for (
size_t i = 0;
i < data.size(); ++
i)
73 auto v =
static_cast<double>(data(
i));
88 auto sorter = xt::argsort(data);
94 auto item = data[idx];
119 R n =
static_cast<R>(data.size());
141 template <
class R =
double,
class E1,
class E2,
class E3>
144 return detail::histogram_imp<R>(
145 std::forward<E1>(data),
162 template <
class R =
double,
class E1,
class E2>
165 using value_type =
typename std::decay_t<E1>::value_type;
167 auto n = data.size();
169 return detail::histogram_imp<R>(
170 std::forward<E1>(data),
187 template <
class R =
double,
class E1>
190 using value_type =
typename std::decay_t<E1>::value_type;
192 auto n = data.size();
194 return detail::histogram_imp<R>(
195 std::forward<E1>(data),
214 template <
class R =
double,
class E1,
class E2>
217 using value_type =
typename std::decay_t<E1>::value_type;
219 auto n = data.size();
221 return detail::histogram_imp<R>(
222 std::forward<E1>(data),
240 template <
class R =
double,
class E1,
class E2>
243 return detail::histogram_imp<R>(
244 std::forward<E1>(data),
264 template <
class R =
double,
class E1,
class E2,
class E3>
267 return detail::histogram_imp<R>(
268 std::forward<E1>(data),
300 template <
class E1,
class E2,
class E3>
306 std::size_t
bins = 10,
312 using value_type =
typename std::decay_t<E1>::value_type;
313 using weights_type =
typename std::decay_t<E2>::value_type;
317 XTENSOR_ASSERT(data.dimension() == 1);
318 XTENSOR_ASSERT(
weights.dimension() == 1);
320 XTENSOR_ASSERT(
weights.size() == data.size());
324 XTENSOR_ASSERT(
bins > std::size_t(0));
330 case histogram_algorithm::automatic:
337 case histogram_algorithm::linspace:
344 case histogram_algorithm::logspace:
346 using rhs_value_type = std::conditional_t<xtl::is_integral<value_type>::value,
double, value_type>;
360 case histogram_algorithm::uniform:
363 auto sorter = xt::argsort(data);
376 std::vector<size_t> shape = {
bins + 1};
386 for (size_type
i = 0;
i <
weights.size(); ++
i)
417 template <
class E1,
class E2>
421 std::size_t
bins = 10,
425 using value_type =
typename std::decay_t<E1>::value_type;
430 std::forward<E1>(data),
452 using value_type =
typename std::decay_t<E1>::value_type;
454 auto n = data.size();
459 std::forward<E1>(data),
479 template <
class E1,
class E2>
484 std::size_t
bins = 10,
488 using value_type =
typename std::decay_t<E1>::value_type;
490 auto n = data.size();
512 template <
class E1,
class E2, XTL_REQUIRES(is_xexpression<std::decay_t<E2>>)>
517 using size_type =
typename std::decay_t<E1>::size_type;
520 xtl::is_integral<typename std::decay_t<E1>::value_type>::value,
521 "Bincount data has to be integral type."
523 XTENSOR_ASSERT(data.dimension() == 1);
524 XTENSOR_ASSERT(
weights.dimension() == 1);
531 XTENSOR_THROW(std::runtime_error,
"Data argument for bincount can only contain positive integers!");
538 for (size_type
i = 0;
i < data.size(); ++
i)
550 std::forward<E1>(data),
551 xt::ones<
typename std::decay_t<E1>::value_type>(data.shape()),
568 if (
weights.size() <= std::size_t(1))
574#ifdef XTENSOR_ENABLE_ASSERT
575 using value_type =
typename std::decay_t<E>::value_type;
auto cumsum(E &&e, std::ptrdiff_t axis)
Cumulative sum.
auto amax(E &&e, X &&axes, EVS es=EVS())
Maximum element along given axis.
auto amin(E &&e, X &&axes, EVS es=EVS())
Minimum element along given axis.
auto ceil(E &&e) noexcept -> detail::xfunction_type_t< math::ceil_fun, E >
ceil function.
auto sum(E &&e, X &&axes, EVS es=EVS())
Sum of elements over given axes.
auto minmax(E &&e, EVS es=EVS())
Minimum and maximum among the elements of an array or expression.
auto norm(E &&e) noexcept
Calculates the squared magnitude elementwise for the complex numbers in e.
standard mathematical functions for xexpressions
auto range(A start_val, B stop_val)
Select a range from start_val to stop_val (excluded).
auto all() noexcept
Returns a slice representing a full dimension, to be used as an argument of view function.
auto ones(S shape) noexcept
Returns an xexpression containing ones of the specified shape.
auto histogram_bin_edges(E1 &&data, E2 &&weights, E3 left, E3 right, std::size_t bins=10, histogram_algorithm mode=histogram_algorithm::automatic)
Compute the bin-edges of a histogram of a set of data using different algorithms.
xt::xtensor< size_t, 1 > bin_items(size_t N, E &&weights)
Get the number of items in each bin, given the fraction of items per bin.
auto searchsorted(E1 &&a, E2 &&v, bool right=true)
Find indices where elements should be inserted to maintain order.
auto digitize(E1 &&data, E2 &&bin_edges, bool right=false)
Return the indices of the bins to which each value in input array belongs.
auto linspace(T start, T stop, std::size_t num_samples=50, bool endpoint=true) noexcept
Generates num_samples evenly spaced numbers over given interval.
auto histogram(E1 &&data, E2 &&bin_edges, E3 &&weights, bool density=false)
Compute the histogram of a set of data.
detail::disable_integral_keep< T > keep(T &&indices)
Create a non-contigous slice from a container of indices to keep.
histogram_algorithm
Defines different algorithms to be used in "histogram_bin_edges".
auto logspace(T start, T stop, std::size_t num_samples, T base=10, bool endpoint=true) noexcept
Generates num_samples numbers evenly spaced on a log scale over given interval.
auto bincount(E1 &&data, E2 &&weights, std::size_t minlength=0)
Count number of occurrences of each value in array of non-negative ints.
auto view(E &&e, S &&... slices)
Constructs and returns a view on the specified xexpression.