NumCpp  2.8.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
41 {
42  namespace random
43  {
44  namespace detail
45  {
46  //============================================================================
47  // Method Description:
57  template<typename dtype, typename GeneratorType = std::mt19937>
58  dtype chiSquare(GeneratorType& generator, dtype inDof)
59  {
61 
62  if (inDof <= 0)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom must be greater than zero.");
65  }
66 
67  std::chi_squared_distribution<dtype> dist(inDof);
68  return dist(generator);
69  }
70 
71  //============================================================================
72  // Method Description:
84  template<typename dtype, typename GeneratorType = std::mt19937>
85  NdArray<dtype> chiSquare(GeneratorType& generator, const Shape& inShape, dtype inDof)
86  {
88 
89  if (inDof <= 0)
90  {
91  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom must be greater than zero.");
92  }
93 
94  NdArray<dtype> returnArray(inShape);
95 
96  std::chi_squared_distribution<dtype> dist(inDof);
97 
98  std::for_each(returnArray.begin(),
99  returnArray.end(),
100  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
101 
102  return returnArray;
103  }
104  } // namespace detail
105 
106  //============================================================================
107  // Method Description:
116  template<typename dtype>
117  dtype chiSquare(dtype inDof)
118  {
119  return detail::chiSquare(generator_, inDof);
120  }
121 
122  //============================================================================
123  // Method Description:
134  template<typename dtype>
135  NdArray<dtype> chiSquare(const Shape& inShape, dtype inDof)
136  {
137  return detail::chiSquare(generator_, inShape, inDof);
138  }
139  } // namespace random
140 } // namespace nc
#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 chiSquare(GeneratorType &generator, dtype inDof)
Definition: chiSquare.hpp:58
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
dtype chiSquare(dtype inDof)
Definition: chiSquare.hpp:117
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45