NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
triangle.hpp
Go to the documentation of this file.
1 
29 #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:
61  template<typename dtype>
62  dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
63  {
64  STATIC_ASSERT_FLOAT(dtype);
65 
66  if (inA < 0)
67  {
68  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
69  }
70 
71  if (inB < 0)
72  {
73  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
74  }
75 
76  if (inC < 0)
77  {
78  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
79  }
80 
81  const bool aLessB = inA <= inB;
82  const bool bLessC = inB <= inC;
83  if (!(aLessB && bLessC))
84  {
85  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
86  }
87 
88  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
89  return dist(generator_);
90  }
91 
92  //============================================================================
93  // Method Description:
107  template<typename dtype>
108  NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
109  {
110  STATIC_ASSERT_FLOAT(dtype);
111 
112  if (inA < 0)
113  {
114  THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
115  }
116 
117  if (inB < 0)
118  {
119  THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
120  }
121 
122  if (inC < 0)
123  {
124  THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
125  }
126 
127  const bool aLessB = inA <= inB;
128  const bool bLessC = inB <= inC;
129  if (!(aLessB && bLessC))
130  {
131  THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
132  }
133 
134  NdArray<dtype> returnArray(inShape);
135 
136  boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
137 
138  std::for_each(returnArray.begin(), returnArray.end(),
139  [&dist](dtype& value) -> void
140  {
141  value = dist(generator_);
142  });
143 
144  return returnArray;
145  }
146  } // namespace random
147 } // namespace nc
148 
149 #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:1558
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
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:62
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