NumCpp  2.9.0
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
41 {
42  namespace random
43  {
44  namespace detail
45  {
46  //============================================================================
47  // Method Description:
58  template<typename dtype, typename GeneratorType = std::mt19937>
59  dtype gamma(GeneratorType& generator, dtype inGammaShape, dtype inScaleValue = 1)
60  {
62 
63  if (inGammaShape <= 0)
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input gamma shape should be greater than zero.");
66  }
67 
68  if (inScaleValue <= 0)
69  {
70  THROW_INVALID_ARGUMENT_ERROR("input scale should be greater than zero.");
71  }
72 
73  std::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
74  return dist(generator);
75  }
76 
77  //============================================================================
78  // Method Description:
91  template<typename dtype, typename GeneratorType = std::mt19937>
93  gamma(GeneratorType& generator, const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
94  {
96 
97  if (inGammaShape <= 0)
98  {
99  THROW_INVALID_ARGUMENT_ERROR("input gamma shape should be greater than zero.");
100  }
101 
102  if (inScaleValue <= 0)
103  {
104  THROW_INVALID_ARGUMENT_ERROR("input scale should be greater than zero.");
105  }
106 
107  NdArray<dtype> returnArray(inShape);
108 
109  std::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
110 
111  std::for_each(returnArray.begin(),
112  returnArray.end(),
113  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
114 
115  return returnArray;
116  }
117  } // namespace detail
118 
119  //============================================================================
120  // Method Description:
130  template<typename dtype>
131  dtype gamma(dtype inGammaShape, dtype inScaleValue = 1)
132  {
133  return detail::gamma(generator_, inGammaShape, inScaleValue);
134  }
135 
136  //============================================================================
137  // Method Description:
149  template<typename dtype>
150  NdArray<dtype> gamma(const Shape& inShape, dtype inGammaShape, dtype inScaleValue = 1)
151  {
152  return detail::gamma(generator_, inShape, inGammaShape, inScaleValue);
153  }
154  } // namespace random
155 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
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 gamma(GeneratorType &generator, dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:59
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:131
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