NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
bernoilli.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <algorithm>
37 #include <random>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace random
43  {
44  //============================================================================
45  // Method Description:
52  inline bool bernoulli(double inP = 0.5)
53  {
54  if (inP < 0 || inP > 1)
55  {
56  THROW_INVALID_ARGUMENT_ERROR("input probability of success must be of the range [0, 1].");
57  }
58 
59  std::bernoulli_distribution dist(inP);
60  return dist(generator_);
61  }
62 
63  //============================================================================
64  // Method Description:
73  inline NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
74  {
75  if (inP < 0 || inP > 1)
76  {
77  THROW_INVALID_ARGUMENT_ERROR("input probability of success must be of the range [0, 1].");
78  }
79 
80  NdArray<bool> returnArray(inShape);
81 
82  std::bernoulli_distribution dist(inP);
83 
84  std::for_each(returnArray.begin(), returnArray.end(),
85  [&dist](bool& value) -> void
86  {
87  value = dist(generator_);
88  });
89 
90  return returnArray;
91  }
92  } // namespace random
93 } // namespace nc
StaticAsserts.hpp
Error.hpp
generator.hpp
nc::NdArray< bool >
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:1431
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
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1087
nc::random::bernoulli
bool bernoulli(double inP=0.5)
Definition: bernoilli.hpp:52