NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Random/laplace.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#ifndef NUMCPP_NO_USE_BOOST
31
32#include <algorithm>
33
34#include "boost/random/laplace_distribution.hpp"
35
37#include "NumCpp/Core/Shape.hpp"
38#include "NumCpp/NdArray.hpp"
40
41namespace nc::random
42{
43 namespace detail
44 {
45 //============================================================================
46 // Method Description:
58 template<typename dtype, typename GeneratorType = std::mt19937>
59 dtype laplace(GeneratorType& generator, dtype inLoc = 0, dtype inScale = 1)
60 {
62
63 boost::random::laplace_distribution<dtype> dist(inLoc, inScale);
64 return dist(generator);
65 }
66
67 //============================================================================
68 // Method Description:
82 template<typename dtype, typename GeneratorType = std::mt19937>
83 NdArray<dtype> laplace(GeneratorType& generator, const Shape& inShape, dtype inLoc = 0, dtype inScale = 1)
84 {
86
87 NdArray<dtype> returnArray(inShape);
88
89 boost::random::laplace_distribution<dtype> dist(inLoc, inScale);
90
91 std::for_each(returnArray.begin(),
92 returnArray.end(),
93 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
94
95 return returnArray;
96 }
97 } // namespace detail
98
99 //============================================================================
100 // Method Description:
111 template<typename dtype>
112 dtype laplace(dtype inLoc = 0, dtype inScale = 1)
113 {
114 return detail::laplace(generator_, inLoc, inScale);
115 }
116
117 //============================================================================
118 // Method Description:
131 template<typename dtype>
132 NdArray<dtype> laplace(const Shape& inShape, dtype inLoc = 0, dtype inScale = 1)
133 {
134 return detail::laplace(generator_, inShape, inLoc, inScale);
135 }
136} // namespace nc::random
137
138#endif // #ifndef NUMCPP_NO_USE_BOOST
#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:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype laplace(GeneratorType &generator, dtype inLoc=0, dtype inScale=1)
Definition: Random/laplace.hpp:59
Definition: Random/bernoulli.hpp:41
dtype laplace(dtype inLoc=0, dtype inScale=1)
Definition: Random/laplace.hpp:112
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225