NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
mode.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31#include <unordered_map>
32
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //===========================================================================
41 // Method Description:
51 template<typename dtype, typename HashFunction = std::hash<dtype>>
53 {
54 const auto modeFunction =
55 [](typename NdArray<dtype>::const_iterator iterBegin, typename NdArray<dtype>::const_iterator iterEnd)
56 {
57 std::unordered_map<dtype, int, HashFunction> counts{};
58 auto greatestCount = int{ 0 };
59 dtype mode{};
60 for (auto iter = iterBegin; iter != iterEnd; ++iter)
61 {
62 const auto& value = *iter;
63
64 if (counts.count(value) > 0)
65 {
66 auto& count = counts[value];
67 ++count;
68 if (count > greatestCount)
69 {
70 greatestCount = count;
71 mode = value;
72 }
73 }
74 else
75 {
76 counts.insert({ value, 1 });
77 }
78 }
79
80 return mode;
81 };
82
83 switch (inAxis)
84 {
85 case Axis::NONE:
86 {
87 NdArray<dtype> returnArray = { modeFunction(inArray.cbegin(), inArray.cend()) };
88 return returnArray;
89 }
90 case Axis::COL:
91 {
92 NdArray<dtype> returnArray(1, inArray.numRows());
93 for (uint32 row = 0; row < inArray.numRows(); ++row)
94 {
95 returnArray(0, row) = modeFunction(inArray.cbegin(row), inArray.cend(row));
96 }
97
98 return returnArray;
99 }
100 case Axis::ROW:
101 {
102 return mode(inArray.transpose(), Axis::COL);
103 }
104 default:
105 {
106 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
107 return {};
108 }
109 }
110 }
111
112 //============================================================================
113 // Method Description:
123 template<typename dtype>
124 NdArray<std::complex<dtype>> mode(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
125 {
127
128 return mode<std::complex<dtype>, ComplexHash<dtype>>(inArray, inAxis);
129 }
130} // 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
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
self_type transpose() const
Definition NdArrayCore.hpp:4959
NdArrayConstIterator< dtype, const_pointer, difference_type > const_iterator
Definition NdArrayCore.hpp:157
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3553
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
Definition Cartesian.hpp:39
NdArray< dtype > mode(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition mode.hpp:52
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
std::uint32_t uint32
Definition Types.hpp:40
Definition StdComplexOperators.hpp:124