NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
normal.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:
59  template<typename dtype, typename GeneratorType = std::mt19937>
60  dtype normal(GeneratorType& generator, dtype inMean = 0, dtype inSigma = 1)
61  {
63 
64  if (inSigma <= 0)
65  {
66  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
67  }
68 
69  std::normal_distribution<dtype> dist(inMean, inSigma);
70  return dist(generator);
71  }
72 
73  //============================================================================
74  // Method Description:
88  template<typename dtype, typename GeneratorType = std::mt19937>
89  NdArray<dtype> normal(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
90  {
92 
93  if (inSigma <= 0)
94  {
95  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
96  }
97 
98  NdArray<dtype> returnArray(inShape);
99 
100  std::normal_distribution<dtype> dist(inMean, inSigma);
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:
122  template<typename dtype>
123  dtype normal(dtype inMean = 0, dtype inSigma = 1)
124  {
125  return detail::normal(generator_, inMean, inSigma);
126  }
127 
128  //============================================================================
129  // Method Description:
142  template<typename dtype>
143  NdArray<dtype> normal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
144  {
145  return detail::normal(generator_, inShape, inMean, inSigma);
146  }
147  } // namespace random
148 } // 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 normal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: normal.hpp:60
dtype normal(dtype inMean=0, dtype inSigma=1)
Definition: normal.hpp:123
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