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