NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
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>
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
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 {
131 }
132
133 //============================================================================
134 // Method Description:
146 template<typename dtype>
148 {
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
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
std::mt19937_64 generator_
generator function
Definition generator.hpp:35
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59