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