NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
bincount.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
58  template<typename dtype>
59  NdArray<dtype> bincount(const NdArray<dtype>& inArray, uint16 inMinLength = 1)
60  {
61  STATIC_ASSERT_INTEGER(dtype);
62 
63  dtype maxValue = inArray.max().item();
64  if (maxValue < 0)
65  {
66  // no positive values so just return an empty array
67  return NdArray<dtype>(0);
68  }
69 
70  if (maxValue + 1 > DtypeInfo<dtype>::max())
71  {
72  THROW_INVALID_ARGUMENT_ERROR("array values too large, will result in gigantic array that will take up alot of memory...");
73  }
74 
75  const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
76  NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
77 
78  NdArray<dtype> outArray(1, outArraySize);
79  outArray.zeros();
80  std::for_each(clippedArray.cbegin(), clippedArray.cend(),
81  [&outArray](dtype value) noexcept -> void
82  {
83  ++outArray[value];
84  });
85 
86  return outArray;
87  }
88 
89  //============================================================================
90  // Method Description:
110  template<typename dtype>
111  NdArray<dtype> bincount(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, uint16 inMinLength = 1)
112  {
113  STATIC_ASSERT_INTEGER(dtype);
114 
115  if (inArray.shape() != inWeights.shape())
116  {
117  THROW_INVALID_ARGUMENT_ERROR("weights array must be the same shape as the input array.");
118  }
119 
120  dtype maxValue = inArray.max().item();
121  if (maxValue < 0)
122  {
123  // no positive values so just return an empty array
124  return NdArray<dtype>(0);
125  }
126 
127  if (maxValue + 1 > DtypeInfo<dtype>::max())
128  {
129  THROW_INVALID_ARGUMENT_ERROR("array values too large, will result in gigantic array that will take up alot of memory...");
130  }
131 
132  const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
133  NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
134 
135  NdArray<dtype> outArray(1, outArraySize);
136  outArray.zeros();
137  uint32 counter = 0;
138  std::for_each(clippedArray.cbegin(), clippedArray.cend(),
139  [&outArray, &inWeights, &counter](dtype value) noexcept -> void
140  {
141  outArray[value] += inWeights[counter++];
142  });
143 
144  return outArray;
145  }
146 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > clip(value_type inMin, value_type inMax) const
Definition: NdArrayCore.hpp:2438
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:3123
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4859
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:45
NdArray< dtype > bincount(const NdArray< dtype > &inArray, uint16 inMinLength=1)
Definition: bincount.hpp:59
std::uint16_t uint16
Definition: Types.hpp:41
std::uint32_t uint32
Definition: Types.hpp:40