NumCpp  2.9.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
41 {
42  namespace random
43  {
44  namespace detail
45  {
46  //============================================================================
47  // Method Description:
56  template<typename dtype, typename GeneratorType = std::mt19937>
57  dtype cauchy(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::cauchy_distribution<dtype> dist(inMean, inSigma);
67  return dist(generator);
68  }
69 
70  //============================================================================
71  // Method Description:
82  template<typename dtype, typename GeneratorType = std::mt19937>
83  NdArray<dtype> cauchy(GeneratorType& generator, const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
84  {
86 
87  if (inSigma <= 0)
88  {
89  THROW_INVALID_ARGUMENT_ERROR("input sigma must be greater than zero.");
90  }
91 
92  NdArray<dtype> returnArray(inShape);
93 
94  std::cauchy_distribution<dtype> dist(inMean, inSigma);
95 
96  std::for_each(returnArray.begin(),
97  returnArray.end(),
98  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
99 
100  return returnArray;
101  }
102  } // namespace detail
103 
104  //============================================================================
105  // Method Description:
113  template<typename dtype>
114  dtype cauchy(dtype inMean = 0, dtype inSigma = 1)
115  {
116  return detail::cauchy(generator_, inMean, inSigma);
117  }
118 
119  //============================================================================
120  // Method Description:
130  template<typename dtype>
131  NdArray<dtype> cauchy(const Shape& inShape, dtype inMean = 0, dtype inSigma = 1)
132  {
133  return detail::cauchy(generator_, inShape, inMean, inSigma);
134  }
135  } // namespace random
136 } // 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 cauchy(GeneratorType &generator, dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:57
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
dtype cauchy(dtype inMean=0, dtype inSigma=1)
Definition: cauchy.hpp:114
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45