NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Random/beta.hpp
Go to the documentation of this file.
1
28#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
43namespace nc::random
44{
45 namespace detail
46 {
47 //============================================================================
48 // Method Description:
60 template<typename dtype, typename GeneratorType = std::mt19937>
61 dtype beta(GeneratorType& generator, dtype inAlpha, dtype inBeta)
62 {
64
65 if (inAlpha < 0)
66 {
67 THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
68 }
69
70 if (inBeta < 0)
71 {
72 THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
73 }
74
75 boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
76 return dist(generator);
77 }
78
79 //============================================================================
80 // Method Description:
94 template<typename dtype, typename GeneratorType = std::mt19937>
95 NdArray<dtype> beta(GeneratorType& generator, const Shape& inShape, dtype inAlpha, dtype inBeta)
96 {
98
99 if (inAlpha < 0)
100 {
101 THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
102 }
103
104 if (inBeta < 0)
105 {
106 THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
107 }
108
109 NdArray<dtype> returnArray(inShape);
110
111 boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
112
113 std::for_each(returnArray.begin(),
114 returnArray.end(),
115 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
116
117 return returnArray;
118 }
119 } // namespace detail
120
121 //============================================================================
122 // Method Description:
133 template<typename dtype>
134 dtype beta(dtype inAlpha, dtype inBeta)
135 {
136 return detail::beta(generator_, inAlpha, inBeta);
137 }
138
139 //============================================================================
140 // Method Description:
153 template<typename dtype>
154 NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
155 {
156 return detail::beta(generator_, inShape, inAlpha, inBeta);
157 }
158} // namespace nc::random
159
160#endif // #ifndef NUMCPP_NO_USE_BOOST
#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:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:61
Definition: Random/bernoulli.hpp:41
dtype beta(dtype inAlpha, dtype inBeta)
Definition: Random/beta.hpp:134
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225