NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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/Types.hpp"
35#include "NumCpp/NdArray.hpp"
38
39namespace nc::random
40{
41 namespace detail
42 {
43 //============================================================================
44 // Method Description:
51 template<typename dtype, typename GeneratorType = std::mt19937>
52 dtype choice(GeneratorType& generator, const NdArray<dtype>& inArray)
53 {
54 uint32 randIdx = detail::randInt<uint32>(generator, inArray.size());
55 return inArray[randIdx];
56 }
57
58 //============================================================================
59 // Method Description:
68 template<typename dtype, typename GeneratorType = std::mt19937>
69 NdArray<dtype> choice(GeneratorType& generator,
70 const NdArray<dtype>& inArray,
71 uint32 inNum,
73 {
74 if (replace == Replace::NO && inNum > inArray.size())
75 {
76 THROW_INVALID_ARGUMENT_ERROR("when Replace::NO 'inNum' must be <= inArray.size()");
77 }
78
79 if (replace == Replace::YES)
80 {
81 NdArray<dtype> outArray(1, inNum);
82 std::for_each(outArray.begin(),
83 outArray.end(),
84 [&generator, &inArray](dtype& value) -> void { value = choice(generator, inArray); });
85
86 return outArray;
87 }
88
89 return detail::permutation(generator, inArray)[Slice(inNum)];
90 }
91 } // namespace detail
92
93 //============================================================================
94 // Method Description:
100 template<typename dtype>
101 dtype choice(const NdArray<dtype>& inArray)
102 {
103 return detail::choice(generator_, inArray);
104 }
105
106 //============================================================================
107 // Method Description:
115 template<typename dtype>
117 {
118 return detail::choice(generator_, inArray, inNum, replace);
119 }
120} // 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:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Class for slicing into NdArrays.
Definition Slice.hpp:45
Definition Random/bernoulli.hpp:41
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition choice.hpp:52
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition permutation.hpp:52
dtype randInt(GeneratorType &generator, dtype inLow, dtype inHigh=0)
Definition randInt.hpp:60
Definition Random/bernoulli.hpp:39
std::mt19937_64 generator_
generator function
Definition generator.hpp:35
dtype choice(const NdArray< dtype > &inArray)
Definition choice.hpp:101
Replace
Replace boolean.
Definition Enums.hpp:101
@ YES
Definition Enums.hpp:102
@ NO
Definition Enums.hpp:103
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition replace.hpp:45
std::uint32_t uint32
Definition Types.hpp:40