NumCpp  2.3.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
bernoilli.hpp
Go to the documentation of this file.
1 #pragma once
29 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/NdArray.hpp"
36 
37 #include "boost/random/bernoulli_distribution.hpp"
38 
39 #include <string>
40 
41 namespace nc
42 {
43  namespace random
44  {
45  //============================================================================
46  // Method Description:
53  template<typename dtype>
54  dtype bernoulli(dtype inP)
55  {
57 
58  if (inP < 0 || inP > 1)
59  {
60  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
61  }
62 
63  const boost::random::bernoulli_distribution<dtype> dist(inP);
64  return dist(generator_); ;
65  }
66 
67  //============================================================================
68  // Method Description:
77  template<typename dtype>
78  NdArray<dtype> bernoulli(const Shape& inShape, dtype inP)
79  {
81 
82  if (inP < 0 || inP > 1)
83  {
84  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
85  }
86 
87  NdArray<dtype> returnArray(inShape);
88 
89  const boost::random::bernoulli_distribution<dtype> dist(inP);
90 
91  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
92  [&dist](dtype& value) -> void
93  {
94  value = dist(generator_);
95  });
96 
97  return returnArray;
98  }
99  } // namespace random
100 } // namespace nc
StaticAsserts.hpp
Error.hpp
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1434
nc::random::bernoulli
dtype bernoulli(dtype inP)
Definition: bernoilli.hpp:54
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1090