NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
f.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 f(GeneratorType& generator, dtype inDofN, dtype inDofD)
59  {
61 
62  if (inDofN <= 0)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom should be greater than zero.");
65  }
66 
67  if (inDofD <= 0)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("denominator degrees of freedom should be greater than zero.");
70  }
71 
72  std::fisher_f_distribution<dtype> dist(inDofN, inDofD);
73  return dist(generator);
74  }
75 
76  //============================================================================
77  // Method Description:
89  template<typename dtype, typename GeneratorType = std::mt19937>
90  NdArray<dtype> f(GeneratorType& generator, const Shape& inShape, dtype inDofN, dtype inDofD)
91  {
93 
94  if (inDofN <= 0)
95  {
96  THROW_INVALID_ARGUMENT_ERROR("numerator degrees of freedom should be greater than zero.");
97  }
98 
99  if (inDofD <= 0)
100  {
101  THROW_INVALID_ARGUMENT_ERROR("denominator degrees of freedom should be greater than zero.");
102  }
103 
104  NdArray<dtype> returnArray(inShape);
105 
106  std::fisher_f_distribution<dtype> dist(inDofN, inDofD);
107 
108  std::for_each(returnArray.begin(),
109  returnArray.end(),
110  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
111 
112  return returnArray;
113  }
114  } // namespace detail
115 
116  //============================================================================
117  // Method Description:
126  template<typename dtype>
127  dtype f(dtype inDofN, dtype inDofD)
128  {
129  return detail::f(generator_, inDofN, inDofD);
130  }
131 
132  //============================================================================
133  // Method Description:
144  template<typename dtype>
145  NdArray<dtype> f(const Shape& inShape, dtype inDofN, dtype inDofD)
146  {
147  return detail::f(generator_, inShape, inDofN, inDofD);
148  }
149  } // namespace random
150 } // 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 f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:127
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