NumCpp  2.5.1
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:
54  template<typename dtype>
55  dtype discrete(const NdArray<double>& inWeights)
56  {
57  STATIC_ASSERT_INTEGER(dtype);
58 
59  std::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
60  return dist(generator_);
61  }
62 
63  //============================================================================
64  // Method Description:
76  template<typename dtype>
77  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
78  {
79  STATIC_ASSERT_INTEGER(dtype);
80 
81  NdArray<dtype> returnArray(inShape);
82 
83  std::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
84 
85  std::for_each(returnArray.begin(), returnArray.end(),
86  [&dist](dtype& value) -> void
87  {
88  value = dist(generator_);
89  });
90 
91  return returnArray;
92  }
93  } // namespace random
94 } // namespace nc
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
iterator end() noexcept
Definition: NdArrayCore.hpp:1558
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype discrete(const NdArray< double > &inWeights)
Definition: discrete.hpp:55
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