NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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
34#include "boost/random/beta_distribution.hpp"
35
38#include "NumCpp/Core/Shape.hpp"
39#include "NumCpp/NdArray.hpp"
41
42namespace nc::random
43{
44 namespace detail
45 {
46 //============================================================================
47 // Method Description:
59 template<typename dtype, typename GeneratorType = std::mt19937>
60 dtype beta(GeneratorType& generator, dtype inAlpha, dtype inBeta)
61 {
63
64 if (inAlpha < 0)
65 {
66 THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
67 }
68
69 if (inBeta < 0)
70 {
71 THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
72 }
73
74 boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
75 return dist(generator);
76 }
77
78 //============================================================================
79 // Method Description:
93 template<typename dtype, typename GeneratorType = std::mt19937>
94 NdArray<dtype> beta(GeneratorType& generator, const Shape& inShape, dtype inAlpha, dtype inBeta)
95 {
97
98 if (inAlpha < 0)
99 {
100 THROW_INVALID_ARGUMENT_ERROR("input alpha must be greater than zero.");
101 }
102
103 if (inBeta < 0)
104 {
105 THROW_INVALID_ARGUMENT_ERROR("input beta must be greater than zero.");
106 }
107
108 NdArray<dtype> returnArray(inShape);
109
110 boost::random::beta_distribution<dtype> dist(inAlpha, inBeta);
111
112 std::for_each(returnArray.begin(),
113 returnArray.end(),
114 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
115
116 return returnArray;
117 }
118 } // namespace detail
119
120 //============================================================================
121 // Method Description:
132 template<typename dtype>
133 dtype beta(dtype inAlpha, dtype inBeta)
134 {
135 return detail::beta(generator_, inAlpha, inBeta);
136 }
137
138 //============================================================================
139 // Method Description:
152 template<typename dtype>
153 NdArray<dtype> beta(const Shape& inShape, dtype inAlpha, dtype inBeta)
154 {
155 return detail::beta(generator_, inShape, inAlpha, inBeta);
156 }
157} // namespace nc::random
158
159#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:135
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Random/bernoulli.hpp:41
dtype beta(GeneratorType &generator, dtype inAlpha, dtype inBeta)
Definition Random/beta.hpp:60
Definition Random/bernoulli.hpp:39
dtype beta(dtype inAlpha, dtype inBeta)
Definition Random/beta.hpp:133
std::mt19937_64 generator_
generator function
Definition generator.hpp:35