NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Random/gamma.hpp
Go to the documentation of this file.
1 #pragma once
30 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/NdArray.hpp"
37 
38 #include "boost/random/gamma_distribution.hpp"
39 
40 #include <string>
41 
42 namespace nc
43 {
44  namespace random
45  {
46  //============================================================================
47  // Method Description:
57  template<typename dtype>
58  dtype gamma(dtype inGammaShape, dtype inScaleValue = 1)
59  {
61 
62  if (inGammaShape <= 0)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("input gamma shape should be greater than zero.");
65  }
66 
67  if (inScaleValue <= 0)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input scale should be greater than zero.");
70  }
71 
72  boost::random::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
73  return dist(generator_);
74 
75  }
76  //============================================================================
77  // Method Description:
89  template<typename dtype>
90  NdArray<dtype> gamma(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  boost::random::gamma_distribution<dtype> dist(inGammaShape, inScaleValue);
107 
108  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
109  [&dist](dtype& value) -> void
110  {
111  value = dist(generator_);
112  });
113 
114  return returnArray;
115  }
116  } // namespace random
117 } // namespace nc
StaticAsserts.hpp
Error.hpp
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:38
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:40
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
nc::random::gamma
dtype gamma(dtype inGammaShape, dtype inScaleValue=1)
Definition: Random/gamma.hpp:58
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091