NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
choice.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31
32#include "NumCpp/Core/Enums.hpp"
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
39
40namespace nc::random
41{
42 namespace detail
43 {
44 //============================================================================
45 // Method Description:
52 template<typename dtype, typename GeneratorType = std::mt19937>
53 dtype choice(GeneratorType& generator, const NdArray<dtype>& inArray)
54 {
55 uint32 randIdx = detail::randInt<uint32>(generator, inArray.size());
56 return inArray[randIdx];
57 }
58
59 //============================================================================
60 // Method Description:
69 template<typename dtype, typename GeneratorType = std::mt19937>
70 NdArray<dtype> choice(GeneratorType& generator,
71 const NdArray<dtype>& inArray,
72 uint32 inNum,
74 {
75 if (replace == Replace::NO && inNum > inArray.size())
76 {
77 THROW_INVALID_ARGUMENT_ERROR("when Replace::NO 'inNum' must be <= inArray.size()");
78 }
79
80 if (replace == Replace::YES)
81 {
82 NdArray<dtype> outArray(1, inNum);
83 std::for_each(outArray.begin(),
84 outArray.end(),
85 [&generator, &inArray](dtype& value) -> void { value = choice(generator, inArray); });
86
87 return outArray;
88 }
89
90 return detail::permutation(generator, inArray)[Slice(inNum)];
91 }
92 } // namespace detail
93
94 //============================================================================
95 // Method Description:
101 template<typename dtype>
102 dtype choice(const NdArray<dtype>& inArray)
103 {
104 return detail::choice(generator_, inArray);
105 }
106
107 //============================================================================
108 // Method Description:
116 template<typename dtype>
118 {
119 return detail::choice(generator_, inArray, inNum, replace);
120 }
121} // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Class for slicing into NdArrays.
Definition: Slice.hpp:45
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:53
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition: permutation.hpp:52
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype choice(const NdArray< dtype > &inArray)
Definition: choice.hpp:102
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
Replace
Replace boolean.
Definition: Enums.hpp:101
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40