NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
poisson.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <random>
32#include <string>
33
36#include "NumCpp/Core/Shape.hpp"
37#include "NumCpp/NdArray.hpp"
39
40namespace nc::random
41{
42 namespace detail
43 {
44 //============================================================================
45 // Method Description:
55 template<typename dtype, typename GeneratorType = std::mt19937>
56 dtype poisson(GeneratorType& generator, double inMean = 1)
57 {
59
60 if (inMean <= 0)
61 {
62 THROW_INVALID_ARGUMENT_ERROR("input mean must be greater than zero.");
63 }
64
65 std::poisson_distribution<dtype> dist(inMean);
66 return dist(generator);
67 }
68
69 //============================================================================
70 // Method Description:
82 template<typename dtype, typename GeneratorType = std::mt19937>
83 NdArray<dtype> poisson(GeneratorType& generator, const Shape& inShape, double inMean = 1)
84 {
86
87 if (inMean <= 0)
88 {
89 THROW_INVALID_ARGUMENT_ERROR("input mean must be greater than zero.");
90 }
91
92 NdArray<dtype> returnArray(inShape);
93
94 std::poisson_distribution<dtype> dist(inMean);
95
96 std::for_each(returnArray.begin(),
97 returnArray.end(),
98 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
99
100 return returnArray;
101 }
102 } // namespace detail
103
104 //============================================================================
105 // Method Description:
114 template<typename dtype>
115 dtype poisson(double inMean = 1)
116 {
117 return detail::poisson<dtype>(generator_, inMean);
118 }
119
120 //============================================================================
121 // Method Description:
132 template<typename dtype>
133 NdArray<dtype> poisson(const Shape& inShape, double inMean = 1)
134 {
135 return detail::poisson<dtype>(generator_, inShape, inMean);
136 }
137} // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:43
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype poisson(GeneratorType &generator, double inMean=1)
Definition: poisson.hpp:56
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype poisson(double inMean=1)
Definition: poisson.hpp:115
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225