NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
cauchy.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::random
41 {
42  namespace detail
43  {
44  //============================================================================
45  // Method Description:
54  template<typename dtype, typename GeneratorType = std::mt19937>
55  dtype cauchy(GeneratorType& generator, dtype inMean = 0, dtype inSigma = 1)
56  {
58 
59  if (inSigma <= 0)
60  {
61  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
62  }
63 
64  std::cauchy_distribution<dtype> dist(inMean, inSigma);
65  return dist(generator);
66  }
67 
68  //============================================================================
69  // Method Description:
80  template<typename dtype, typename GeneratorType = std::mt19937>
81  NdArray<dtype> cauchy(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
82  {
84 
85  if (inSigma <= 0)
86  {
87  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
88  }
89 
90  NdArray<dtype> returnArray(inShape);
91 
92  std::cauchy_distribution<dtype> dist(inMean, inSigma);
93 
94  std::for_each(returnArray.begin(),
95  returnArray.end(),
96  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
97 
98  return returnArray;
99  }
100  } // namespace detail
101 
102  //============================================================================
103  // Method Description:
111  template<typename dtype>
112  dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
113  {
114  return detail::cauchy(generator_, inMean, inSigma);
115  }
116 
117  //============================================================================
118  // Method Description:
128  template<typename dtype>
129  NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
130  {
131  return detail::cauchy(generator_, inShape, inMean, inSigma);
132  }
133 } // 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:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1576
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype cauchy(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:55
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:112
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225