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