NumCpp  2.7.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 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/NdArray.hpp"
37 
38 #include "boost/random/non_central_chi_squared_distribution.hpp"
39 
40 #include <algorithm>
41 #include <string>
42 
43 namespace nc
44 {
45  namespace random
46  {
47  //============================================================================
48  // Method Description:
58  template<typename dtype>
59  dtype nonCentralChiSquared(dtype inK = 1, dtype inLambda = 1)
60  {
62 
63  if (inK <= 0)
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
66  }
67 
68  if (inLambda <= 0)
69  {
70  THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
71  }
72 
73  boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
74  return dist(generator_);
75  }
76 
77  //============================================================================
78  // Method Description:
90  template<typename dtype>
91  NdArray<dtype> nonCentralChiSquared(const Shape& inShape, dtype inK = 1, dtype inLambda = 1)
92  {
94 
95  if (inK <= 0)
96  {
97  THROW_INVALID_ARGUMENT_ERROR("input k must be greater than zero.");
98  }
99 
100  if (inLambda <= 0)
101  {
102  THROW_INVALID_ARGUMENT_ERROR("input lambda must be greater than zero.");
103  }
104 
105  NdArray<dtype> returnArray(inShape);
106 
107  boost::random::non_central_chi_squared_distribution<dtype> dist(inK, inLambda);
108 
109  std::for_each(returnArray.begin(), returnArray.end(),
110  [&dist](dtype& value) -> void
111  {
112  value = dist(generator_);
113  });
114 
115  return returnArray;
116  }
117  } // namespace random
118 } // namespace nc
119 
120 #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:1474
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
dtype nonCentralChiSquared(dtype inK=1, dtype inLambda=1)
Definition: nonCentralChiSquared.hpp:59
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45