NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
triangle.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #ifndef NUMCPP_NO_USE_BOOST
32 
33 #include <algorithm>
34 #include <string>
35 
36 #include "boost/random/triangle_distribution.hpp"
37 
40 #include "NumCpp/Core/Shape.hpp"
41 #include "NumCpp/NdArray.hpp"
43 
44 namespace nc
45 {
46  namespace random
47  {
48  namespace detail
49  {
50  //============================================================================
51  // Method Description:
64  template<typename dtype, typename GeneratorType = std::mt19937>
65  dtype triangle(GeneratorType& generator, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
66  {
67  STATIC_ASSERT_FLOAT(dtype);
68 
69  if (inA < 0)
70  {
71  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
72  }
73 
74  if (inB < 0)
75  {
76  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
77  }
78 
79  if (inC < 0)
80  {
81  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
82  }
83 
84  const bool aLessB = inA <= inB;
85  const bool bLessC = inB <= inC;
86  if (!(aLessB && bLessC))
87  {
88  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
89  }
90 
91  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
92  return dist(generator);
93  }
94 
95  //============================================================================
96  // Method Description:
111  template<typename dtype, typename GeneratorType = std::mt19937>
113  triangle(GeneratorType& generator, const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
114  {
115  STATIC_ASSERT_FLOAT(dtype);
116 
117  if (inA < 0)
118  {
119  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
120  }
121 
122  if (inB < 0)
123  {
124  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
125  }
126 
127  if (inC < 0)
128  {
129  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
130  }
131 
132  const bool aLessB = inA <= inB;
133  const bool bLessC = inB <= inC;
134  if (!(aLessB && bLessC))
135  {
136  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
137  }
138 
139  NdArray<dtype> returnArray(inShape);
140 
141  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
142 
143  std::for_each(returnArray.begin(),
144  returnArray.end(),
145  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
146 
147  return returnArray;
148  }
149  } // namespace detail
150 
151  //============================================================================
152  // Method Description:
164  template<typename dtype>
165  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
166  {
167  return detail::triangle(generator_, inA, inB, inC);
168  }
169 
170  //============================================================================
171  // Method Description:
185  template<typename dtype>
186  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
187  {
188  return detail::triangle(generator_, inShape, inA, inB, inC);
189  }
190  } // namespace random
191 } // namespace nc
192 
193 #endif // #ifndef NUMCPP_NO_USE_BOOST
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:45
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 triangle(GeneratorType &generator, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:65
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:165
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45