NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
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#include <string>
34
35#include "boost/random/non_central_chi_squared_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 nonCentralChiSquared(GeneratorType& generator, dtype inK = 1, dtype inLambda = 1)
62 {
64
65 if (inK <= 0)
66 {
67 THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
68 }
69
70 if (inLambda <= 0)
71 {
72 THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
73 }
74
75 boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
76 return dist(generator);
77 }
78
79 //============================================================================
80 // Method Description:
94 template<typename dtype, typename GeneratorType = std::mt19937>
96 nonCentralChiSquared(GeneratorType& generator, const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
97 {
99
100 if (inK <= 0)
101 {
102 THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
103 }
104
105 if (inLambda <= 0)
106 {
107 THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
108 }
109
110 NdArray<dtype> returnArray(inShape);
111
112 boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
113
114 std::for_each(returnArray.begin(),
115 returnArray.end(),
116 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
117
118 return returnArray;
119 }
120 } // namespace detail
121
122 //============================================================================
123 // Method Description:
134 template<typename dtype>
135 dtype nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
136 {
137 return detail::nonCentralChiSquared(generator_, inK, inLambda);
138 }
139
140 //============================================================================
141 // Method Description:
154 template<typename dtype>
155 NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
156 {
157 return detail::nonCentralChiSquared(generator_, inShape, inK, inLambda);
158 }
159} // namespace nc::random
160
161#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 nonCentralChiSquared(GeneratorType &generator, dtype inK=1, dtype inLambda=1)
Definition: nonCentralChiSquared.hpp:61
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition: nonCentralChiSquared.hpp:135
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225