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