NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
bincount.hpp
Go to the documentation of this file.
1
28#pragma once
29
32#include "NumCpp/Core/Types.hpp"
33#include "NumCpp/NdArray.hpp"
34
35namespace nc
36{
37 //============================================================================
38 // Method Description:
54 template<typename dtype>
55 NdArray<dtype> bincount(const NdArray<dtype>& inArray, uint16 inMinLength = 1)
56 {
58
59 dtype maxValue = inArray.max().item();
60 if (maxValue < 0)
61 {
62 // no positive values so just return an empty array
63 return NdArray<dtype>(0);
64 }
65
66 if (maxValue + 1 > DtypeInfo<dtype>::max())
67 {
69 "array values too large, will result in gigantic array that will take up alot of memory...");
70 }
71
72 const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
73 NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
74
75 NdArray<dtype> outArray(1, outArraySize);
76 outArray.zeros();
77 std::for_each(clippedArray.cbegin(),
78 clippedArray.cend(),
79 [&outArray](dtype value) noexcept -> void { ++outArray[value]; });
80
81 return outArray;
82 }
83
84 //============================================================================
85 // Method Description:
104 template<typename dtype>
105 NdArray<dtype> bincount(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, uint16 inMinLength = 1)
106 {
108
109 if (inArray.shape() != inWeights.shape())
110 {
111 THROW_INVALID_ARGUMENT_ERROR("weights array must be the same shape as the input array.");
112 }
113
114 dtype maxValue = inArray.max().item();
115 if (maxValue < 0)
116 {
117 // no positive values so just return an empty array
118 return NdArray<dtype>(0);
119 }
120
121 if (maxValue + 1 > DtypeInfo<dtype>::max())
122 {
124 "array values too large, will result in gigantic array that will take up alot of memory...");
125 }
126
127 const uint16 outArraySize = std::max(static_cast<uint16>(maxValue + 1), inMinLength);
128 NdArray<dtype> clippedArray = inArray.clip(0, maxValue);
129
130 NdArray<dtype> outArray(1, outArraySize);
131 outArray.zeros();
132 uint32 counter = 0;
133 std::for_each(clippedArray.cbegin(),
134 clippedArray.cend(),
135 [&outArray, &inWeights, &counter](dtype value) noexcept -> void
136 { outArray[value] += inWeights[counter++]; });
137
138 return outArray;
139 }
140} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_INTEGER(dtype)
Definition StaticAsserts.hpp:43
static constexpr dtype max() noexcept
Definition DtypeInfo.hpp:110
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
self_type max(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3117
self_type & zeros() noexcept
Definition NdArrayCore.hpp:4977
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
value_type item() const
Definition NdArrayCore.hpp:3098
self_type clip(value_type inMin, value_type inMax) const
Definition NdArrayCore.hpp:2445
Definition Cartesian.hpp:39
NdArray< dtype > bincount(const NdArray< dtype > &inArray, uint16 inMinLength=1)
Definition bincount.hpp:55
std::uint16_t uint16
Definition Types.hpp:41
std::uint32_t uint32
Definition Types.hpp:40