NumCpp  2.12.1
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
30#include <string>
31
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //============================================================================
41 // Method Description:
57 template<typename dtype>
58 NdArray<dtype> bincount(const NdArray<dtype>& inArray, uint16 inMinLength = 1)
59 {
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 {
72 "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(),
81 clippedArray.cend(),
82 [&outArray](dtype value) noexcept -> void { ++outArray[value]; });
83
84 return outArray;
85 }
86
87 //============================================================================
88 // Method Description:
107 template<typename dtype>
108 NdArray<dtype> bincount(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, uint16 inMinLength = 1)
109 {
111
112 if (inArray.shape() != inWeights.shape())
113 {
114 THROW_INVALID_ARGUMENT_ERROR("weights array must be the same shape as the input array.");
115 }
116
117 dtype maxValue = inArray.max().item();
118 if (maxValue < 0)
119 {
120 // no positive values so just return an empty array
121 return NdArray<dtype>(0);
122 }
123
124 if (maxValue + 1 > DtypeInfo<dtype>::max())
125 {
127 "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(),
137 clippedArray.cend(),
138 [&outArray, &inWeights, &counter](dtype value) noexcept -> void
139 { outArray[value] += inWeights[counter++]; });
140
141 return outArray;
142 }
143} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:43
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:139
self_type max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:3041
self_type & zeros() noexcept
Definition: NdArrayCore.hpp:4900
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
value_type item() const
Definition: NdArrayCore.hpp:3022
self_type clip(value_type inMin, value_type inMax) const
Definition: NdArrayCore.hpp:2373
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
Definition: Cartesian.hpp:40
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
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:44