NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
chiSquare.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <random>
32 #include <string>
33 
36 #include "NumCpp/Core/Shape.hpp"
37 #include "NumCpp/NdArray.hpp"
39 
40 namespace nc::random
41 {
42  namespace detail
43  {
44  //============================================================================
45  // Method Description:
55  template<typename dtype, typename GeneratorType = std::mt19937>
56  dtype chiSquare(GeneratorType& generator, dtype inDof)
57  {
59 
60  if (inDof <= 0)
61  {
62  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom must be greater than zero.");
63  }
64 
65  std::chi_squared_distribution<dtype> dist(inDof);
66  return dist(generator);
67  }
68 
69  //============================================================================
70  // Method Description:
82  template<typename dtype, typename GeneratorType = std::mt19937>
83  NdArray<dtype> chiSquare(GeneratorType& generator, const Shape& inShape, dtype inDof)
84  {
86 
87  if (inDof <= 0)
88  {
89  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom must be greater than zero.");
90  }
91 
92  NdArray<dtype> returnArray(inShape);
93 
94  std::chi_squared_distribution<dtype> dist(inDof);
95 
96  std::for_each(returnArray.begin(),
97  returnArray.end(),
98  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
99 
100  return returnArray;
101  }
102  } // namespace detail
103 
104  //============================================================================
105  // Method Description:
114  template<typename dtype>
115  dtype chiSquare(dtype inDof)
116  {
117  return detail::chiSquare(generator_, inDof);
118  }
119 
120  //============================================================================
121  // Method Description:
132  template<typename dtype>
133  NdArray<dtype> chiSquare(const Shape& inShape, dtype inDof)
134  {
135  return detail::chiSquare(generator_, inShape, inDof);
136  }
137 } // namespace nc::random
#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:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1576
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype chiSquare(GeneratorType &generator, dtype inDof)
Definition: chiSquare.hpp:56
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype chiSquare(dtype inDof)
Definition: chiSquare.hpp:115
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225