NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
binomial.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:
56 template<typename dtype, typename GeneratorType = std::mt19937>
57 dtype binomial(GeneratorType& generator, dtype inN, double inP = 0.5)
58 {
60
61 if (inN < 0)
62 {
63 THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
64 }
65
66 if (inP < 0 || inP > 1)
67 {
68 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
69 }
70
71 std::binomial_distribution<dtype> dist(inN, inP);
72 return dist(generator);
73 }
74
75 //============================================================================
76 // Method Description:
89 template<typename dtype, typename GeneratorType = std::mt19937>
90 NdArray<dtype> binomial(GeneratorType& generator, const Shape& inShape, dtype inN, double inP = 0.5)
91 {
93
94 if (inN < 0)
95 {
96 THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
97 }
98
99 if (inP < 0 || inP > 1)
100 {
101 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
102 }
103
104 NdArray<dtype> returnArray(inShape);
105
106 std::binomial_distribution<dtype> dist(inN, inP);
107
108 std::for_each(returnArray.begin(),
109 returnArray.end(),
110 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
111
112 return returnArray;
113 }
114 } // namespace detail
115
116 //============================================================================
117 // Method Description:
127 template<typename dtype>
128 dtype binomial(dtype inN, double inP = 0.5)
129 {
130 return detail::binomial(generator_, inN, inP);
131 }
132
133 //============================================================================
134 // Method Description:
146 template<typename dtype>
147 NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
148 {
149 return detail::binomial(generator_, inShape, inN, inP);
150 }
151} // 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 binomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: binomial.hpp:57
Definition: Random/bernoulli.hpp:41
dtype binomial(dtype inN, double inP=0.5)
Definition: binomial.hpp:128
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225