NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
binomial.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:
55 template<typename dtype, typename GeneratorType = std::mt19937>
56 dtype binomial(GeneratorType& generator, dtype inN, double inP = 0.5)
57 {
59
60 if (inN < 0)
61 {
62 THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
63 }
64
65 if (inP < 0 || inP > 1)
66 {
67 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
68 }
69
70 std::binomial_distribution<dtype> dist(inN, inP);
71 return dist(generator);
72 }
73
74 //============================================================================
75 // Method Description:
88 template<typename dtype, typename GeneratorType = std::mt19937>
89 NdArray<dtype> binomial(GeneratorType& generator, const Shape& inShape, dtype inN, double inP = 0.5)
90 {
92
93 if (inN < 0)
94 {
95 THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
96 }
97
98 if (inP < 0 || inP > 1)
99 {
100 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
101 }
102
103 NdArray<dtype> returnArray(inShape);
104
105 std::binomial_distribution<dtype> dist(inN, inP);
106
107 std::for_each(returnArray.begin(),
108 returnArray.end(),
109 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
110
111 return returnArray;
112 }
113 } // namespace detail
114
115 //============================================================================
116 // Method Description:
126 template<typename dtype>
127 dtype binomial(dtype inN, double inP = 0.5)
128 {
129 return detail::binomial(generator_, inN, inP);
130 }
131
132 //============================================================================
133 // Method Description:
145 template<typename dtype>
146 NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
147 {
148 return detail::binomial(generator_, inShape, inN, inP);
149 }
150} // 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:135
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Random/bernoulli.hpp:41
dtype binomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition binomial.hpp:56
Definition Random/bernoulli.hpp:39
dtype binomial(dtype inN, double inP=0.5)
Definition binomial.hpp:127
std::mt19937_64 generator_
generator function
Definition generator.hpp:35