NumCpp  2.3.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 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/NdArray.hpp"
36 
37 #include "boost/random/discrete_distribution.hpp"
38 
39 
40 namespace nc
41 {
42  namespace random
43  {
44  //============================================================================
45  // Method Description:
55  template<typename dtype>
56  dtype discrete(const NdArray<double>& inWeights)
57  {
58  STATIC_ASSERT_INTEGER(dtype);
59 
60  boost::random::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
61  return dist(generator_);
62  }
63 
64  //============================================================================
65  // Method Description:
77  template<typename dtype>
78  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
79  {
80  STATIC_ASSERT_INTEGER(dtype);
81 
82  NdArray<dtype> returnArray(inShape);
83 
84  boost::random::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
85 
86  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
87  [&dist](dtype& value) -> void
88  {
89  value = dist(generator_);
90  });
91 
92  return returnArray;
93  }
94  } // namespace random
95 } // namespace nc
STATIC_ASSERT_INTEGER
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
StaticAsserts.hpp
Error.hpp
generator.hpp
nc::NdArray< double >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
NdArray.hpp
nc::random::discrete
dtype discrete(const NdArray< double > &inWeights)
Definition: discrete.hpp:56
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1434
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1490
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1146
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1090