NumCpp  2.1.0
A C++ implementation of the Python Numpy library
discrete.hpp
Go to the documentation of this file.
1 #pragma once
30 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/NdArray.hpp"
37 
38 #include "boost/random/discrete_distribution.hpp"
39 
40 
41 namespace nc
42 {
43  namespace random
44  {
45  //============================================================================
46  // Method Description:
56  template<typename dtype>
57  dtype discrete(const NdArray<double>& inWeights)
58  {
59  STATIC_ASSERT_INTEGER(dtype);
60 
61  boost::random::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
62  return dist(generator_);
63  }
64 
65  //============================================================================
66  // Method Description:
78  template<typename dtype>
79  NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
80  {
81  STATIC_ASSERT_INTEGER(dtype);
82 
83  NdArray<dtype> returnArray(inShape);
84 
85  boost::random::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
86 
87  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
88  [&dist](dtype& value) -> void
89  {
90  value = dist(generator_);
91  });
92 
93  return returnArray;
94  }
95  } // namespace random
96 } // namespace nc
STATIC_ASSERT_INTEGER
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:41
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:214
NdArray.hpp
nc::random::discrete
dtype discrete(const NdArray< double > &inWeights)
Definition: discrete.hpp:57
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:40
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091