NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
histogram.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <utility>
31
34#include "NumCpp/Core/Types.hpp"
38#include "NumCpp/NdArray.hpp"
39
40namespace nc
41{
42 //============================================================================
43 // Method Description:
55 template<typename dtype>
56 NdArray<uint32> histogram(const NdArray<dtype>& inArray, const NdArray<double>& inBinEdges)
57 {
59
60 if (inBinEdges.size() < 2)
61 {
62 THROW_INVALID_ARGUMENT_ERROR("number of bin edges must be >= 2.");
63 }
64
65 // sort just in case the user hasn't already
66 const auto binEdges = sort(inBinEdges);
67
68 NdArray<uint32> histo = zeros<uint32>(1, binEdges.size() - 1);
69 for (const auto value : inArray)
70 {
71 if (value < binEdges.front() || value > binEdges.back())
72 {
73 continue;
74 }
75
76 // binary search to find the bin idx
77 constexpr bool keepSearching = true;
78 uint32 lowIdx = 0;
79 uint32 highIdx = binEdges.size() - 1;
80 while (keepSearching)
81 {
82 const uint32 idx = (lowIdx + highIdx) / 2; // integer division
83 if (lowIdx == highIdx || lowIdx == highIdx - 1)
84 {
85 // we found the bin
86 ++histo[lowIdx];
87 break;
88 }
89
90 if (value > binEdges[idx])
91 {
92 lowIdx = idx;
93 }
94 else if (value < binEdges[idx])
95 {
96 highIdx = idx;
97 }
98 else
99 {
100 // we found the bin
101 ++histo[idx];
102 break;
103 }
104 }
105 }
106
107 return histo;
108 }
109
110 //============================================================================
111 // Method Description:
122 template<typename dtype>
123 std::pair<NdArray<uint32>, NdArray<double>> histogram(const NdArray<dtype>& inArray, uint32 inNumBins = 10)
124 {
126
127 if (inNumBins == 0)
128 {
129 THROW_INVALID_ARGUMENT_ERROR("number of bins must be positive.");
130 }
131
132 const NdArray<double> binEdges = linspace(static_cast<double>(inArray.min().item()),
133 static_cast<double>(inArray.max().item()),
134 inNumBins + 1,
136
137 const auto histo = histogram(inArray, binEdges);
138 return std::make_pair(histo, binEdges);
139 }
140} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
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
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
value_type item() const
Definition NdArrayCore.hpp:3098
self_type min(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:3161
Definition Cartesian.hpp:39
NdArray< dtype > sort(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sort.hpp:45
NdArray< dtype > linspace(dtype inStart, dtype inStop, uint32 inNum=50, EndPoint endPoint=EndPoint::YES)
Definition linspace.hpp:59
NdArray< dtype > zeros(uint32 inSquareSize)
Definition zeros.hpp:48
NdArray< uint32 > histogram(const NdArray< dtype > &inArray, const NdArray< double > &inBinEdges)
Definition histogram.hpp:56
std::uint32_t uint32
Definition Types.hpp:40
@ YES
Definition Enums.hpp:75