NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
choice.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
38 
39 namespace 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>
70  choice(GeneratorType& generator, const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
71  {
72  if (!replace && inNum > inArray.size())
73  {
74  THROW_INVALID_ARGUMENT_ERROR("when 'replace' == false 'inNum' must be <= inArray.size()");
75  }
76 
77  if (replace)
78  {
79  NdArray<dtype> outArray(1, inNum);
80  std::for_each(outArray.begin(),
81  outArray.end(),
82  [&generator, &inArray](dtype& value) -> void { value = choice(generator, inArray); });
83 
84  return outArray;
85  }
86 
87  return detail::permutation(generator, inArray)[Slice(inNum)];
88  }
89  } // namespace detail
90 
91  //============================================================================
92  // Method Description:
98  template<typename dtype>
99  dtype choice(const NdArray<dtype>& inArray)
100  {
101  return detail::choice(generator_, inArray);
102  }
103 
104  //============================================================================
105  // Method Description:
113  template<typename dtype>
114  NdArray<dtype> choice(const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
115  {
116  return detail::choice(generator_, inArray, inNum, replace = true);
117  }
118 } // 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:138
size_type size() const noexcept
Definition: NdArrayCore.hpp:4477
iterator end() noexcept
Definition: NdArrayCore.hpp:1576
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
A Class for slicing into NdArrays.
Definition: Slice.hpp:45
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:52
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:99
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40