NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
studentT.hpp
Go to the documentation of this file.
1
28#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
40namespace nc::random
41{
42 namespace detail
43 {
44 // Method Description:
54 template<typename dtype, typename GeneratorType = std::mt19937>
55 dtype studentT(GeneratorType& generator, dtype inDof)
56 {
58
59 if (inDof <= 0)
60 {
61 THROW_INVALID_ARGUMENT_ERROR("degrees of freedom must be greater than zero.");
62 }
63
64 std::student_t_distribution<dtype> dist(inDof);
65 return dist(generator);
66 }
67
68 //============================================================================
69 // Method Description:
81 template<typename dtype, typename GeneratorType = std::mt19937>
82 NdArray<dtype> studentT(GeneratorType& generator, const Shape& inShape, dtype inDof)
83 {
85
86 if (inDof <= 0)
87 {
88 THROW_INVALID_ARGUMENT_ERROR("degrees of freedom must be greater than zero.");
89 }
90
91 NdArray<dtype> returnArray(inShape);
92
93 std::student_t_distribution<dtype> dist(inDof);
94
95 std::for_each(returnArray.begin(),
96 returnArray.end(),
97 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
98
99 return returnArray;
100 }
101 } // namespace detail
102
103 //============================================================================
104 // Method Description:
113 template<typename dtype>
114 dtype studentT(dtype inDof)
115 {
116 return detail::studentT(generator_, inDof);
117 }
118
119 //============================================================================
120 // Method Description:
131 template<typename dtype>
132 NdArray<dtype> studentT(const Shape& inShape, dtype inDof)
133 {
134 return detail::studentT(generator_, inShape, inDof);
135 }
136} // 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:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype studentT(GeneratorType &generator, dtype inDof)
Definition: studentT.hpp:55
Definition: Random/bernoulli.hpp:41
dtype studentT(dtype inDof)
Definition: studentT.hpp:114
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225