NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
extremeValue.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:
55  template<typename dtype, typename GeneratorType = std::mt19937>
56  dtype extremeValue(GeneratorType& generator, dtype inA = 1, dtype inB = 1)
57  {
59 
60  if (inA <= 0)
61  {
62  THROW_INVALID_ARGUMENT_ERROR("input a must be greater than zero.");
63  }
64 
65  if (inB <= 0)
66  {
67  THROW_INVALID_ARGUMENT_ERROR("input b must be greater than zero.");
68  }
69 
70  std::extreme_value_distribution<dtype> dist(inA, inB);
71  return dist(generator);
72  }
73 
74  //============================================================================
75  // Method Description:
85  template<typename dtype, typename GeneratorType = std::mt19937>
86  NdArray<dtype> extremeValue(GeneratorType& generator, const Shape& inShape, dtype inA = 1, dtype inB = 1)
87  {
89 
90  if (inA <= 0)
91  {
92  THROW_INVALID_ARGUMENT_ERROR("input a must be greater than zero.");
93  }
94 
95  if (inB <= 0)
96  {
97  THROW_INVALID_ARGUMENT_ERROR("input b must be greater than zero.");
98  }
99 
100  NdArray<dtype> returnArray(inShape);
101 
102  std::extreme_value_distribution<dtype> dist(inA, inB);
103 
104  std::for_each(returnArray.begin(),
105  returnArray.end(),
106  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
107 
108  return returnArray;
109  }
110  } // namespace detail
111 
112  //============================================================================
113  // Method Description:
120  template<typename dtype>
121  dtype extremeValue(dtype inA = 1, dtype inB = 1)
122  {
123  return detail::extremeValue<dtype>(generator_, inA, inB);
124  }
125 
126  //============================================================================
127  // Method Description:
136  template<typename dtype>
137  NdArray<dtype> extremeValue(const Shape& inShape, dtype inA = 1, dtype inB = 1)
138  {
139  return detail::extremeValue<dtype>(generator_, inShape, inA, inB);
140  }
141  } // namespace random
142 } // 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 extremeValue(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:56
dtype extremeValue(dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:121
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