NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
geometric.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:
57  template<typename dtype, typename GeneratorType = std::mt19937>
58  dtype geometric(GeneratorType& generator, double inP = 0.5)
59  {
60  STATIC_ASSERT_INTEGER(dtype);
61 
62  if (inP < 0 || inP > 1)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
65  }
66 
67  std::geometric_distribution<dtype> dist(inP);
68  return dist(generator);
69  }
70 
71  //============================================================================
72  // Method Description:
84  template<typename dtype, typename GeneratorType = std::mt19937>
85  NdArray<dtype> geometric(GeneratorType& generator, const Shape& inShape, double inP = 0.5)
86  {
87  STATIC_ASSERT_INTEGER(dtype);
88 
89  if (inP < 0 || inP > 1)
90  {
91  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
92  }
93 
94  NdArray<dtype> returnArray(inShape);
95 
96  std::geometric_distribution<dtype> dist(inP);
97 
98  std::for_each(returnArray.begin(),
99  returnArray.end(),
100  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
101 
102  return returnArray;
103  }
104  } // namespace detail
105 
106  //============================================================================
107  // Method Description:
116  template<typename dtype>
117  dtype geometric(double inP = 0.5)
118  {
119  return detail::geometric<dtype>(generator_, inP);
120  }
121 
122  //============================================================================
123  // Method Description:
134  template<typename dtype>
135  NdArray<dtype> geometric(const Shape& inShape, double inP = 0.5)
136  {
137  return detail::geometric<dtype>(generator_, inShape, inP);
138  }
139  } // namespace random
140 } // 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 geometric(GeneratorType &generator, double inP=0.5)
Definition: geometric.hpp:58
dtype geometric(double inP=0.5)
Definition: geometric.hpp:117
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