sum_in_bins¶
- halotools.utils.sum_in_bins(arr, sorted_bin_numbers, testing_mode=False)[source]¶
Given an array of values
arr
and another equal-length arraysorted_bin_numbers
storing how these values have been binned into Nbins, calculate the sum of the values in each bin.- Parameters:
- arrarray
Array of length Nvals storing the quantity to be summed in the bins defined by
sorted_bin_numbers
.- sorted_bin_numbersarray
Integer array of length Nvals storing the bin numbers of each entry of the input
arr
, e.g., the result of np.digitize(arr, bins). Thesorted_bin_numbers
array may have repeated entries but must be in ascending order. That is, the subhalos whose property is stored in arrayarr
will be presumed to be pre-grouped according to, for example, host halo mass, with lowest halo masses first, and higher halo masses at higher indices, in monotonic fashion.- testing_modebool, optional
Boolean specifying whether input arrays will be tested to see if they satisfy the assumptions required by the algorithm. Setting
testing_mode
to True is useful for unit-testing purposes, while setting it to False improves performance. Default is False.
- Returns:
- binned_sumarray
Array of length-Nbins storing the sum of
arr
values within the bins defined bysorted_bin_numbers
.
Examples
>>> Nvals = 5 >>> arr = np.arange(Nvals) >>> sorted_bin_numbers = np.array((1, 2, 2, 6, 7)) >>> result = sum_in_bins(arr, sorted_bin_numbers)