NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Random/gamma.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::random
41 {
42  namespace detail
43  {
44  //============================================================================
45  // Method Description:
56  template<typename dtype, typename GeneratorType = std::mt19937>
57  dtype gamma(GeneratorType& generator, dtype inGammaShape, dtype inScaleValue = 1)
58  {
60 
61  if (inGammaShape <= 0)
62  {
63  THROW_INVALID_ARGUMENT_ERROR("input gamma shape should be greater than zero.");
64  }
65 
66  if (inScaleValue <= 0)
67  {
68  THROW_INVALID_ARGUMENT_ERROR("input scale should be greater than zero.");
69  }
70 
71  std::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
72  return dist(generator);
73  }
74 
75  //============================================================================
76  // Method Description:
89  template<typename dtype, typename GeneratorType = std::mt19937>
90  NdArray<dtype> gamma(GeneratorType& generator, const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
91  {
93 
94  if (inGammaShape <= 0)
95  {
96  THROW_INVALID_ARGUMENT_ERROR("input gamma shape should be greater than zero.");
97  }
98 
99  if (inScaleValue <= 0)
100  {
101  THROW_INVALID_ARGUMENT_ERROR("input scale should be greater than zero.");
102  }
103 
104  NdArray<dtype> returnArray(inShape);
105 
106  std::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
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 gamma(dtype inGammaShape, dtype inScaleValue = 1)
129  {
130  return detail::gamma(generator_, inGammaShape, inScaleValue);
131  }
132 
133  //============================================================================
134  // Method Description:
146  template<typename dtype>
147  NdArray<dtype> gamma(const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
148  {
149  return detail::gamma(generator_, inShape, inGammaShape, inScaleValue);
150  }
151 } // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1566
iterator begin() noexcept
Definition: NdArrayCore.hpp:1258
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype gamma(GeneratorType &generator, dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:57
Definition: Random/bernoulli.hpp:41
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:128
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225