NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
bincount.hpp
Go to the documentation of this file.
1 #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:
57  template<typename dtype>
58  NdArray<dtype> bincount(const NdArray<dtype>& inArray, uint16 inMinLength = 1)
59  {
60  STATIC_ASSERT_INTEGER(dtype);
61 
62  dtype maxValue = inArray.max().item();
63  if (maxValue < 0)
64  {
65  // no positive values so just return an empty array
66  return NdArray<dtype>(0);
67  }
68 
69  if (maxValue + 1 > DtypeInfo<dtype>::max())
70  {
71  THROW_INVALID_ARGUMENT_ERROR("array values too large, will result in gigantic array that will take up alot of memory...");
72  }
73 
74  const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
75  NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
76 
77  NdArray<dtype> outArray(1, outArraySize);
78  outArray.zeros();
79  std::for_each(clippedArray.cbegin(), clippedArray.cend(),
80  [&outArray](dtype value) noexcept -> void
81  {
82  ++outArray[value];
83  });
84 
85  return outArray;
86  }
87 
88  //============================================================================
89  // Method Description:
108  template<typename dtype>
109  NdArray<dtype> bincount(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, uint16 inMinLength = 1)
110  {
111  STATIC_ASSERT_INTEGER(dtype);
112 
113  if (inArray.shape() != inWeights.shape())
114  {
115  THROW_INVALID_ARGUMENT_ERROR("weights array must be the same shape as the input array.");
116  }
117 
118  dtype maxValue = inArray.max().item();
119  if (maxValue < 0)
120  {
121  // no positive values so just return an empty array
122  return NdArray<dtype>(0);
123  }
124 
125  if (maxValue + 1 > DtypeInfo<dtype>::max())
126  {
127  THROW_INVALID_ARGUMENT_ERROR("array values too large, will result in gigantic array that will take up alot of memory...");
128  }
129 
130  const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
131  NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
132 
133  NdArray<dtype> outArray(1, outArraySize);
134  outArray.zeros();
135  uint32 counter = 0;
136  std::for_each(clippedArray.cbegin(), clippedArray.cend(),
137  [&outArray, &inWeights, &counter](dtype value) noexcept -> void
138  {
139  outArray[value] += inWeights[counter++];
140  });
141 
142  return outArray;
143  }
144 } // 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:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > clip(value_type inMin, value_type inMax) const
Definition: NdArrayCore.hpp:2293
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2950
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4647
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:44
NdArray< dtype > bincount(const NdArray< dtype > &inArray, uint16 inMinLength=1)
Definition: bincount.hpp:58
std::uint16_t uint16
Definition: Types.hpp:41
std::uint32_t uint32
Definition: Types.hpp:40