NumCpp  2.1.0
A C++ implementation of the Python Numpy library
triangle.hpp
Go to the documentation of this file.
1 #pragma once
31 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/NdArray.hpp"
38 
39 #include "boost/random/triangle_distribution.hpp"
40 
41 #include <string>
42 
43 namespace nc
44 {
45  namespace random
46  {
47  //============================================================================
48  // Method Description:
59  template<typename dtype>
60  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
61  {
62  STATIC_ASSERT_FLOAT(dtype);
63 
64  if (inA < 0)
65  {
66  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
67  }
68 
69  if (inB < 0)
70  {
71  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
72  }
73 
74  if (inC < 0)
75  {
76  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
77  }
78 
79  const bool aLessB = inA <= inB;
80  const bool bLessC = inB <= inC;
81  if (!(aLessB && bLessC))
82  {
83  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
84  }
85 
86  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
87  return dist(generator_);
88  }
89 
90  //============================================================================
91  // Method Description:
104  template<typename dtype>
105  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
106  {
107  STATIC_ASSERT_FLOAT(dtype);
108 
109  if (inA < 0)
110  {
111  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
112  }
113 
114  if (inB < 0)
115  {
116  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
117  }
118 
119  if (inC < 0)
120  {
121  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
122  }
123 
124  const bool aLessB = inA <= inB;
125  const bool bLessC = inB <= inC;
126  if (!(aLessB && bLessC))
127  {
128  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
129  }
130 
131  NdArray<dtype> returnArray(inShape);
132 
133  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
134 
135  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
136  [&dist](dtype& value) -> void
137  {
138  value = dist(generator_);
139  });
140 
141  return returnArray;
142  }
143  } // namespace random
144 } // namespace nc
StaticAsserts.hpp
nc::random::triangle
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:60
Error.hpp
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
STATIC_ASSERT_FLOAT
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:44
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
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