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