NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
nonCentralChiSquared.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/non_central_chi_squared_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 nonCentralChiSquared(GeneratorType& generator, dtype inK = 1, dtype inLambda = 1)
61 {
63
64 if (inK <= 0)
65 {
66 THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
67 }
68
69 if (inLambda <= 0)
70 {
71 THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
72 }
73
74 boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
75 return dist(generator);
76 }
77
78 //============================================================================
79 // Method Description:
93 template<typename dtype, typename GeneratorType = std::mt19937>
95 nonCentralChiSquared(GeneratorType& generator, const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
96 {
98
99 if (inK <= 0)
100 {
101 THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
102 }
103
104 if (inLambda <= 0)
105 {
106 THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
107 }
108
109 NdArray<dtype> returnArray(inShape);
110
111 boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
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 nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
135 {
136 return detail::nonCentralChiSquared(generator_, inK, inLambda);
137 }
138
139 //============================================================================
140 // Method Description:
153 template<typename dtype>
154 NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
155 {
156 return detail::nonCentralChiSquared(generator_, inShape, inK, inLambda);
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: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 nonCentralChiSquared(GeneratorType &generator, dtype inK=1, dtype inLambda=1)
Definition nonCentralChiSquared.hpp:60
Definition Random/bernoulli.hpp:39
std::mt19937_64 generator_
generator function
Definition generator.hpp:35
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition nonCentralChiSquared.hpp:134