NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
discrete.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <random>
32
35#include "NumCpp/Core/Shape.hpp"
36#include "NumCpp/NdArray.hpp"
38
39namespace nc::random
40{
41 namespace detail
42 {
43 //============================================================================
44 // Method Description:
54 template<typename dtype, typename GeneratorType = std::mt19937>
55 dtype discrete(GeneratorType& generator, const NdArray<double>& inWeights)
56 {
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, typename GeneratorType = std::mt19937>
77 NdArray<dtype> discrete(GeneratorType& generator, const Shape& inShape, const NdArray<double>& inWeights)
78 {
80
81 NdArray<dtype> returnArray(inShape);
82
83 std::discrete_distribution<dtype> dist(inWeights.cbegin(), inWeights.cend());
84
85 std::for_each(returnArray.begin(),
86 returnArray.end(),
87 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
88
89 return returnArray;
90 }
91 } // namespace detail
92
93 //============================================================================
94 // Method Description:
103 template<typename dtype>
104 dtype discrete(const NdArray<double>& inWeights)
105 {
106 return detail::discrete<dtype>(generator_, inWeights);
107 }
108
109 //============================================================================
110 // Method Description:
121 template<typename dtype>
122 NdArray<dtype> discrete(const Shape& inShape, const NdArray<double>& inWeights)
123 {
124 return detail::discrete<dtype>(generator_, inShape, inWeights);
125 }
126} // namespace nc::random
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:43
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype discrete(GeneratorType &generator, const NdArray< double > &inWeights)
Definition: discrete.hpp:55
Definition: Random/bernoulli.hpp:41
dtype discrete(const NdArray< double > &inWeights)
Definition: discrete.hpp:104
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225