NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
discrete.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <algorithm>
37 #include <random>
38 
39 namespace nc
40 {
41  namespace random
42  {
43  //============================================================================
44  // Method Description:
53  template<typename dtype>
54  dtype discrete(const NdArray<double>& inWeights)
55  {
56  STATIC_ASSERT_INTEGER(dtype);
57 
58  std::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
59  return dist(generator_);
60  }
61 
62  //============================================================================
63  // Method Description:
74  template<typename dtype>
75  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
76  {
77  STATIC_ASSERT_INTEGER(dtype);
78 
79  NdArray<dtype> returnArray(inShape);
80 
81  std::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
82 
83  std::for_each(returnArray.begin(), returnArray.end(),
84  [&dist](dtype& value) -> void
85  {
86  value = dist(generator_);
87  });
88 
89  return returnArray;
90  }
91  } // namespace random
92 } // namespace nc
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1216
iterator end() noexcept
Definition: NdArrayCore.hpp:1474
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype discrete(const NdArray< double > &inWeights)
Definition: discrete.hpp:54
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45