NumCpp  2.10.1
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::random
45 {
46  namespace detail
47  {
48  //============================================================================
49  // Method Description:
62  template<typename dtype, typename GeneratorType = std::mt19937>
63  dtype triangle(GeneratorType& generator, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
64  {
65  STATIC_ASSERT_FLOAT(dtype);
66 
67  if (inA < 0)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
70  }
71 
72  if (inB < 0)
73  {
74  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
75  }
76 
77  if (inC < 0)
78  {
79  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
80  }
81 
82  const bool aLessB = inA <= inB;
83  const bool bLessC = inB <= inC;
84  if (!(aLessB && bLessC))
85  {
86  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
87  }
88 
89  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
90  return dist(generator);
91  }
92 
93  //============================================================================
94  // Method Description:
109  template<typename dtype, typename GeneratorType = std::mt19937>
111  triangle(GeneratorType& generator, const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
112  {
113  STATIC_ASSERT_FLOAT(dtype);
114 
115  if (inA < 0)
116  {
117  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
118  }
119 
120  if (inB < 0)
121  {
122  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
123  }
124 
125  if (inC < 0)
126  {
127  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
128  }
129 
130  const bool aLessB = inA <= inB;
131  const bool bLessC = inB <= inC;
132  if (!(aLessB && bLessC))
133  {
134  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
135  }
136 
137  NdArray<dtype> returnArray(inShape);
138 
139  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
140 
141  std::for_each(returnArray.begin(),
142  returnArray.end(),
143  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
144 
145  return returnArray;
146  }
147  } // namespace detail
148 
149  //============================================================================
150  // Method Description:
162  template<typename dtype>
163  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
164  {
165  return detail::triangle(generator_, inA, inB, inC);
166  }
167 
168  //============================================================================
169  // Method Description:
183  template<typename dtype>
184  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
185  {
186  return detail::triangle(generator_, inShape, inA, inB, inC);
187  }
188 } // namespace nc::random
189 
190 #endif // #ifndef NUMCPP_NO_USE_BOOST
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1566
iterator begin() noexcept
Definition: NdArrayCore.hpp:1258
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:63
Definition: Random/bernoulli.hpp:41
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition: triangle.hpp:163
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225