NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
normal.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <algorithm>
37 #include <random>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace random
43  {
44  //============================================================================
45  // Method Description:
55  template<typename dtype>
56  dtype normal(dtype inMean = 0, dtype inSigma = 1)
57  {
59 
60  if (inSigma <= 0)
61  {
62  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
63  }
64 
65  std::normal_distribution<dtype> dist(inMean, inSigma);
66  return dist(generator_);
67  }
68 
69  //============================================================================
70  // Method Description:
82  template<typename dtype>
83  NdArray<dtype> normal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
84  {
86 
87  if (inSigma <= 0)
88  {
89  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
90  }
91 
92  NdArray<dtype> returnArray(inShape);
93 
94  std::normal_distribution<dtype> dist(inMean, inSigma);
95 
96  std::for_each(returnArray.begin(), returnArray.end(),
97  [&dist](dtype& value) -> void
98  {
99  value = dist(generator_);
100  });
101 
102  return returnArray;
103  }
104  } // namespace random
105 } // 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:1558
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype normal(dtype inMean=0, dtype inSigma=1)
Definition: normal.hpp:56
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45