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