NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
lognormal.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 lognormal(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::lognormal_distribution<dtype> dist(inMean, inSigma);
70  return dist(generator);
71  }
72 
73  //============================================================================
74  // Method Description:
88  template<typename dtype, typename GeneratorType = std::mt19937>
90  lognormal(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
91  {
93 
94  if (inSigma <= 0)
95  {
96  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
97  }
98 
99  NdArray<dtype> returnArray(inShape);
100 
101  std::lognormal_distribution<dtype> dist(inMean, inSigma);
102 
103  std::for_each(returnArray.begin(),
104  returnArray.end(),
105  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
106 
107  return returnArray;
108  }
109  } // namespace detail
110 
111  //============================================================================
112  // Method Description:
123  template<typename dtype>
124  dtype lognormal(dtype inMean = 0, dtype inSigma = 1)
125  {
126  return detail::lognormal(generator_, inMean, inSigma);
127  }
128 
129  //============================================================================
130  // Method Description:
143  template<typename dtype>
144  NdArray<dtype> lognormal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
145  {
146  return detail::lognormal(generator_, inShape, inMean, inSigma);
147  }
148  } // namespace random
149 } // 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 lognormal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: lognormal.hpp:60
dtype lognormal(dtype inMean=0, dtype inSigma=1)
Definition: lognormal.hpp:124
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