NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
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
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);
77
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>
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
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);
132
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 clip(value_type inMin, value_type inMax) const
Definition NdArrayCore.hpp:2449
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
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
std::uint32_t uint32
Definition Types.hpp:40