NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Random/beta.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #ifndef NUMCPP_NO_USE_BOOST
31 
32 #include <algorithm>
33 #include <string>
34 
35 #include "boost/random/beta_distribution.hpp"
36 
39 #include "NumCpp/Core/Shape.hpp"
40 #include "NumCpp/NdArray.hpp"
42 
43 namespace nc
44 {
45  namespace random
46  {
47  namespace detail
48  {
49  //============================================================================
50  // Method Description:
62  template<typename dtype, typename GeneratorType = std::mt19937>
63  dtype beta(GeneratorType& generator, dtype inAlpha, dtype inBeta)
64  {
66 
67  if (inAlpha < 0)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
70  }
71 
72  if (inBeta < 0)
73  {
74  THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
75  }
76 
77  boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
78  return dist(generator);
79  }
80 
81  //============================================================================
82  // Method Description:
96  template<typename dtype, typename GeneratorType = std::mt19937>
97  NdArray<dtype> beta(GeneratorType& generator, const Shape& inShape, dtype inAlpha, dtype inBeta)
98  {
100 
101  if (inAlpha < 0)
102  {
103  THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
104  }
105 
106  if (inBeta < 0)
107  {
108  THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
109  }
110 
111  NdArray<dtype> returnArray(inShape);
112 
113  boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
114 
115  std::for_each(returnArray.begin(),
116  returnArray.end(),
117  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
118 
119  return returnArray;
120  }
121  } // namespace detail
122 
123  //============================================================================
124  // Method Description:
135  template<typename dtype>
136  dtype beta(dtype inAlpha, dtype inBeta)
137  {
138  return detail::beta(generator_, inAlpha, inBeta);
139  }
140 
141  //============================================================================
142  // Method Description:
155  template<typename dtype>
156  NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
157  {
158  return detail::beta(generator_, inShape, inAlpha, inBeta);
159  }
160  } // namespace random
161 } // namespace nc
162 
163 #endif // #ifndef NUMCPP_NO_USE_BOOST
#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 beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:63
dtype beta(dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:136
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