NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
studentT.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  // Method Description:
56  template<typename dtype, typename GeneratorType = std::mt19937>
57  dtype studentT(GeneratorType& generator, dtype inDof)
58  {
60 
61  if (inDof <= 0)
62  {
63  THROW_INVALID_ARGUMENT_ERROR("degrees of freedom must be greater than zero.");
64  }
65 
66  std::student_t_distribution<dtype> dist(inDof);
67  return dist(generator);
68  }
69 
70  //============================================================================
71  // Method Description:
83  template<typename dtype, typename GeneratorType = std::mt19937>
84  NdArray<dtype> studentT(GeneratorType& generator, const Shape& inShape, dtype inDof)
85  {
87 
88  if (inDof <= 0)
89  {
90  THROW_INVALID_ARGUMENT_ERROR("degrees of freedom must be greater than zero.");
91  }
92 
93  NdArray<dtype> returnArray(inShape);
94 
95  std::student_t_distribution<dtype> dist(inDof);
96 
97  std::for_each(returnArray.begin(),
98  returnArray.end(),
99  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
100 
101  return returnArray;
102  }
103  } // namespace detail
104 
105  //============================================================================
106  // Method Description:
115  template<typename dtype>
116  dtype studentT(dtype inDof)
117  {
118  return detail::studentT(generator_, inDof);
119  }
120 
121  //============================================================================
122  // Method Description:
133  template<typename dtype>
134  NdArray<dtype> studentT(const Shape& inShape, dtype inDof)
135  {
136  return detail::studentT(generator_, inShape, inDof);
137  }
138  } // namespace random
139 } // 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 studentT(GeneratorType &generator, dtype inDof)
Definition: studentT.hpp:57
dtype studentT(dtype inDof)
Definition: studentT.hpp:116
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45