cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
gaussian.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_RANDOM_GAUSSIAN_H
4#define pRC_CORE_RANDOM_GAUSSIAN_H
5
12
13namespace pRC
14{
15 template<class T>
17 {
18 public:
19 constexpr explicit GaussianDistribution(T const mean = zero(),
20 T const stdDev = identity())
21 : mMean(mean)
22 , mStdDev(stdDev)
23 {
24 if(!(mStdDev > zero()))
25 {
27 "Parameter standard deviation of Gaussian distribution "
28 "must be positive.");
29 }
30 }
31
32 constexpr auto reset()
33 {
34 mHot = false;
35 }
36
37 constexpr auto &mean() const
38 {
39 return mMean;
40 }
41
42 constexpr auto &stdDev() const
43 {
44 return mStdDev;
45 }
46
47 constexpr auto min() const
48 {
50 }
51
52 constexpr auto max() const
53 {
54 return NumericLimits<T>::max();
55 }
56
57 template<class URNG>
58 constexpr auto operator()(URNG &rng)
59 {
60 auto basic = [&]() -> T
61 {
62 if(mHot)
63 {
64 mHot = false;
65 return mNextValue;
66 }
67 else
68 {
69 T u, v, s;
71
72 do
73 {
74 u = uniform(rng);
75 v = uniform(rng);
76 s = u * u + v * v;
77 }
78 while(s > T(1) || s == zero());
79
80 auto m = sqrt(T(-2) * log(s) / s);
81
82 mNextValue = v * m;
83 mHot = true;
84
85 return u * m;
86 }
87 }();
88 return basic * stdDev() + mean();
89 }
90
91 private:
92 T mMean;
93 T mStdDev;
94 T mNextValue = zero();
95 Bool mHot = false;
96 };
97
98 template<class T>
99 constexpr auto operator==(GaussianDistribution<T> const &lhs,
101 {
102 if(lhs.mean() == rhs.mean() && lhs.stdDev() == rhs.stdDev())
103 {
104 if(lhs.mHot == rhs.mHot)
105 {
106 if(!lhs.mHot || lhs.mNextValue == rhs.mNextValue)
107 {
108 return true;
109 }
110 }
111 }
112
113 return false;
114 }
115
116 template<class T>
119 {
120 return !(lhs == rhs);
121 }
122}
123#endif // pRC_CORE_RANDOM_GAUSSIAN_H
constexpr auto operator()(URNG &rng)
Definition gaussian.hpp:58
constexpr auto & stdDev() const
Definition gaussian.hpp:42
constexpr auto max() const
Definition gaussian.hpp:52
constexpr auto & mean() const
Definition gaussian.hpp:37
constexpr auto reset()
Definition gaussian.hpp:32
constexpr auto min() const
Definition gaussian.hpp:47
constexpr GaussianDistribution(T const mean=zero(), T const stdDev=identity())
Definition gaussian.hpp:19
Definition type_traits.hpp:57
Definition type_traits.hpp:49
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
bool Bool
Definition type_traits.hpp:18
static constexpr auto mean(Xs &&...args)
Definition mean.hpp:15
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
static constexpr auto zero()
Definition zero.hpp:12
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
static constexpr auto operator!=(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:304
static constexpr auto log(Complex< T > const &a)
Definition log.hpp:11
static constexpr auto operator==(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:297
static constexpr auto sqrt(Complex< T > const &a)
Definition sqrt.hpp:12
static constexpr auto identity()
Definition identity.hpp:12
Definition type_traits.hpp:16
Definition limits.hpp:13