NumCpp  2.10.1
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::random
41 {
42  namespace detail
43  {
44  //============================================================================
45  // Method Description:
53  template<typename dtype, typename GeneratorType = std::mt19937>
54  dtype extremeValue(GeneratorType& generator, dtype inA = 1, dtype inB = 1)
55  {
57 
58  if (inA <= 0)
59  {
60  THROW_INVALID_ARGUMENT_ERROR("input a must be greater than zero.");
61  }
62 
63  if (inB <= 0)
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input b must be greater than zero.");
66  }
67 
68  std::extreme_value_distribution<dtype> dist(inA, inB);
69  return dist(generator);
70  }
71 
72  //============================================================================
73  // Method Description:
83  template<typename dtype, typename GeneratorType = std::mt19937>
84  NdArray<dtype> extremeValue(GeneratorType& generator, const Shape& inShape, dtype inA = 1, dtype inB = 1)
85  {
87 
88  if (inA <= 0)
89  {
90  THROW_INVALID_ARGUMENT_ERROR("input a must be greater than zero.");
91  }
92 
93  if (inB <= 0)
94  {
95  THROW_INVALID_ARGUMENT_ERROR("input b must be greater than zero.");
96  }
97 
98  NdArray<dtype> returnArray(inShape);
99 
100  std::extreme_value_distribution<dtype> dist(inA, inB);
101 
102  std::for_each(returnArray.begin(),
103  returnArray.end(),
104  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
105 
106  return returnArray;
107  }
108  } // namespace detail
109 
110  //============================================================================
111  // Method Description:
118  template<typename dtype>
119  dtype extremeValue(dtype inA = 1, dtype inB = 1)
120  {
121  return detail::extremeValue<dtype>(generator_, inA, inB);
122  }
123 
124  //============================================================================
125  // Method Description:
134  template<typename dtype>
135  NdArray<dtype> extremeValue(const Shape& inShape, dtype inA = 1, dtype inB = 1)
136  {
137  return detail::extremeValue<dtype>(generator_, inShape, inA, inB);
138  }
139 } // 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:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1566
iterator begin() noexcept
Definition: NdArrayCore.hpp:1258
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype extremeValue(GeneratorType &generator, dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:54
Definition: Random/bernoulli.hpp:41
dtype extremeValue(dtype inA=1, dtype inB=1)
Definition: extremeValue.hpp:119
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225