NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
binomial.hpp
Go to the documentation of this file.
1 #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 
40 namespace nc
41 {
42  namespace random
43  {
44  namespace detail
45  {
46  //============================================================================
47  // Method Description:
58  template<typename dtype, typename GeneratorType = std::mt19937>
59  dtype binomial(GeneratorType& generator, dtype inN, double inP = 0.5)
60  {
61  STATIC_ASSERT_INTEGER(dtype);
62 
63  if (inN < 0)
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
66  }
67 
68  if (inP < 0 || inP > 1)
69  {
70  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
71  }
72 
73  std::binomial_distribution<dtype> dist(inN, inP);
74  return dist(generator);
75  }
76 
77  //============================================================================
78  // Method Description:
91  template<typename dtype, typename GeneratorType = std::mt19937>
92  NdArray<dtype> binomial(GeneratorType& generator, const Shape& inShape, dtype inN, double inP = 0.5)
93  {
94  STATIC_ASSERT_INTEGER(dtype);
95 
96  if (inN < 0)
97  {
98  THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
99  }
100 
101  if (inP < 0 || inP > 1)
102  {
103  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
104  }
105 
106  NdArray<dtype> returnArray(inShape);
107 
108  std::binomial_distribution<dtype> dist(inN, inP);
109 
110  std::for_each(returnArray.begin(),
111  returnArray.end(),
112  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
113 
114  return returnArray;
115  }
116  } // namespace detail
117 
118  //============================================================================
119  // Method Description:
129  template<typename dtype>
130  dtype binomial(dtype inN, double inP = 0.5)
131  {
132  return detail::binomial(generator_, inN, inP);
133  }
134 
135  //============================================================================
136  // Method Description:
148  template<typename dtype>
149  NdArray<dtype> binomial(const Shape& inShape, dtype inN, double inP = 0.5)
150  {
151  return detail::binomial(generator_, inShape, inN, inP);
152  }
153  } // namespace random
154 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype binomial(GeneratorType &generator, dtype inN, double inP=0.5)
Definition: binomial.hpp:59
dtype binomial(dtype inN, double inP=0.5)
Definition: binomial.hpp:130
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45