NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
lognormal.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <random>
32
35#include "NumCpp/Core/Shape.hpp"
36#include "NumCpp/NdArray.hpp"
38
39namespace nc::random
40{
41 namespace detail
42 {
43 //============================================================================
44 // Method Description:
56 template<typename dtype, typename GeneratorType = std::mt19937>
57 dtype lognormal(GeneratorType& generator, dtype inMean = 0, dtype inSigma = 1)
58 {
60
61 if (inSigma <= 0)
62 {
63 THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
64 }
65
66 std::lognormal_distribution<dtype> dist(inMean, inSigma);
67 return dist(generator);
68 }
69
70 //============================================================================
71 // Method Description:
85 template<typename dtype, typename GeneratorType = std::mt19937>
86 NdArray<dtype> lognormal(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
87 {
89
90 if (inSigma <= 0)
91 {
92 THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
93 }
94
95 NdArray<dtype> returnArray(inShape);
96
97 std::lognormal_distribution<dtype> dist(inMean, inSigma);
98
99 std::for_each(returnArray.begin(),
100 returnArray.end(),
101 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
102
103 return returnArray;
104 }
105 } // namespace detail
106
107 //============================================================================
108 // Method Description:
119 template<typename dtype>
120 dtype lognormal(dtype inMean = 0, dtype inSigma = 1)
121 {
122 return detail::lognormal(generator_, inMean, inSigma);
123 }
124
125 //============================================================================
126 // Method Description:
139 template<typename dtype>
140 NdArray<dtype> lognormal(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
141 {
142 return detail::lognormal(generator_, inShape, inMean, inSigma);
143 }
144} // 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:135
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Random/bernoulli.hpp:41
dtype lognormal(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition lognormal.hpp:57
Definition Random/bernoulli.hpp:39
dtype lognormal(dtype inMean=0, dtype inSigma=1)
Definition lognormal.hpp:120
std::mt19937_64 generator_
generator function
Definition generator.hpp:35