NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
triangle.hpp
Go to the documentation of this file.
1
29#pragma once
30
31#ifndef NUMCPP_NO_USE_BOOST
32
33#include <algorithm>
34
35#include "boost/random/triangle_distribution.hpp"
36
39#include "NumCpp/Core/Shape.hpp"
40#include "NumCpp/NdArray.hpp"
42
43namespace nc::random
44{
45 namespace detail
46 {
47 //============================================================================
48 // Method Description:
61 template<typename dtype, typename GeneratorType = std::mt19937>
62 dtype triangle(GeneratorType& generator, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
63 {
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:
108 template<typename dtype, typename GeneratorType = std::mt19937>
110 triangle(GeneratorType& generator, const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
111 {
112 STATIC_ASSERT_FLOAT(dtype);
113
114 if (inA < 0)
115 {
116 THROW_INVALID_ARGUMENT_ERROR("input A must be greater than or equal to zero.");
117 }
118
119 if (inB < 0)
120 {
121 THROW_INVALID_ARGUMENT_ERROR("input B must be greater than or equal to zero.");
122 }
123
124 if (inC < 0)
125 {
126 THROW_INVALID_ARGUMENT_ERROR("input C must be greater than or equal to zero.");
127 }
128
129 const bool aLessB = inA <= inB;
130 const bool bLessC = inB <= inC;
131 if (!(aLessB && bLessC))
132 {
133 THROW_INVALID_ARGUMENT_ERROR("inputs must be a <= b <= c.");
134 }
135
136 NdArray<dtype> returnArray(inShape);
137
138 boost::random::triangle_distribution<dtype> dist(inA, inB, inC);
139
140 std::for_each(returnArray.begin(),
141 returnArray.end(),
142 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
143
144 return returnArray;
145 }
146 } // namespace detail
147
148 //============================================================================
149 // Method Description:
161 template<typename dtype>
162 dtype triangle(dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
163 {
164 return detail::triangle(generator_, inA, inB, inC);
165 }
166
167 //============================================================================
168 // Method Description:
182 template<typename dtype>
183 NdArray<dtype> triangle(const Shape& inShape, dtype inA = 0, dtype inB = 0.5, dtype inC = 1)
184 {
185 return detail::triangle(generator_, inShape, inA, inB, inC);
186 }
187} // namespace nc::random
188
189#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:135
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Random/bernoulli.hpp:41
dtype triangle(GeneratorType &generator, dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition triangle.hpp:62
Definition Random/bernoulli.hpp:39
dtype triangle(dtype inA=0, dtype inB=0.5, dtype inC=1)
Definition triangle.hpp:162
std::mt19937_64 generator_
generator function
Definition generator.hpp:35