NumCpp  2.8.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
40 {
41  namespace random
42  {
43  namespace detail
44  {
45  //============================================================================
46  // Method Description:
53  template<typename dtype, typename GeneratorType = std::mt19937>
54  dtype choice(GeneratorType& generator, const NdArray<dtype>& inArray)
55  {
56  uint32 randIdx = detail::randInt<uint32>(generator, inArray.size());
57  return inArray[randIdx];
58  }
59 
60  //============================================================================
61  // Method Description:
70  template<typename dtype, typename GeneratorType = std::mt19937>
72  choice(GeneratorType& generator, const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
73  {
74  if (!replace && inNum > inArray.size())
75  {
76  THROW_INVALID_ARGUMENT_ERROR("when 'replace' == false 'inNum' must be <= inArray.size()");
77  }
78 
79  if (replace)
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>
116  NdArray<dtype> choice(const NdArray<dtype>& inArray, uint32 inNum, bool replace = true)
117  {
118  return detail::choice(generator_, inArray, inNum, replace = true);
119  }
120  } // namespace random
121 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4289
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition: choice.hpp:54
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition: permutation.hpp:55
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
dtype choice(const NdArray< dtype > &inArray)
Definition: choice.hpp:101
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition: replace.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40