NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
cauchy.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:
53 template<typename dtype, typename GeneratorType = std::mt19937>
54 dtype cauchy(GeneratorType& generator, dtype inMean = 0, dtype inSigma = 1)
55 {
57
58 if (inSigma <= 0)
59 {
60 THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
61 }
62
63 std::cauchy_distribution<dtype> dist(inMean, inSigma);
64 return dist(generator);
65 }
66
67 //============================================================================
68 // Method Description:
79 template<typename dtype, typename GeneratorType = std::mt19937>
80 NdArray<dtype> cauchy(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
81 {
83
84 if (inSigma <= 0)
85 {
86 THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
87 }
88
89 NdArray<dtype> returnArray(inShape);
90
91 std::cauchy_distribution<dtype> dist(inMean, inSigma);
92
93 std::for_each(returnArray.begin(),
94 returnArray.end(),
95 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
96
97 return returnArray;
98 }
99 } // namespace detail
100
101 //============================================================================
102 // Method Description:
110 template<typename dtype>
111 dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
112 {
113 return detail::cauchy(generator_, inMean, inSigma);
114 }
115
116 //============================================================================
117 // Method Description:
127 template<typename dtype>
128 NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
129 {
130 return detail::cauchy(generator_, inShape, inMean, inSigma);
131 }
132} // 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 cauchy(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition cauchy.hpp:54
Definition Random/bernoulli.hpp:39
std::mt19937_64 generator_
generator function
Definition generator.hpp:35
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition cauchy.hpp:111