NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
geometric.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:
55 template<typename dtype, typename GeneratorType = std::mt19937>
56 dtype geometric(GeneratorType& generator, double inP = 0.5)
57 {
59
60 if (inP < 0 || inP > 1)
61 {
62 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
63 }
64
65 std::geometric_distribution<dtype> dist(inP);
66 return dist(generator);
67 }
68
69 //============================================================================
70 // Method Description:
82 template<typename dtype, typename GeneratorType = std::mt19937>
83 NdArray<dtype> geometric(GeneratorType& generator, const Shape& inShape, double inP = 0.5)
84 {
86
87 if (inP < 0 || inP > 1)
88 {
89 THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
90 }
91
92 NdArray<dtype> returnArray(inShape);
93
94 std::geometric_distribution<dtype> dist(inP);
95
96 std::for_each(returnArray.begin(),
97 returnArray.end(),
98 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
99
100 return returnArray;
101 }
102 } // namespace detail
103
104 //============================================================================
105 // Method Description:
114 template<typename dtype>
115 dtype geometric(double inP = 0.5)
116 {
117 return detail::geometric<dtype>(generator_, inP);
118 }
119
120 //============================================================================
121 // Method Description:
132 template<typename dtype>
133 NdArray<dtype> geometric(const Shape& inShape, double inP = 0.5)
134 {
135 return detail::geometric<dtype>(generator_, inShape, inP);
136 }
137} // 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 geometric(GeneratorType &generator, double inP=0.5)
Definition: geometric.hpp:56
Definition: Random/bernoulli.hpp:41
dtype geometric(double inP=0.5)
Definition: geometric.hpp:115
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225