-
Notifications
You must be signed in to change notification settings - Fork 709
/
clipnormal.h
91 lines (74 loc) · 2.46 KB
/
clipnormal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/util/defines.h"
#include <cmath>
#include <random>
namespace seal
{
namespace util
{
class ClippedNormalDistribution
{
public:
using result_type = double;
using param_type = ClippedNormalDistribution;
ClippedNormalDistribution(result_type mean, result_type standard_deviation, result_type max_deviation);
template <typename RNG>
SEAL_NODISCARD inline result_type operator()(RNG &engine, const param_type &parm) noexcept
{
param(parm);
return operator()(engine);
}
template <typename RNG>
SEAL_NODISCARD inline result_type operator()(RNG &engine) noexcept
{
result_type mean = normal_.mean();
while (true)
{
result_type value = normal_(engine);
result_type deviation = std::abs(value - mean);
if (deviation <= max_deviation_)
{
return value;
}
}
}
SEAL_NODISCARD inline result_type mean() const noexcept
{
return normal_.mean();
}
SEAL_NODISCARD inline result_type standard_deviation() const noexcept
{
return normal_.stddev();
}
SEAL_NODISCARD inline result_type max_deviation() const noexcept
{
return max_deviation_;
}
SEAL_NODISCARD inline result_type min() const noexcept
{
return normal_.mean() - max_deviation_;
}
SEAL_NODISCARD inline result_type max() const noexcept
{
return normal_.mean() + max_deviation_;
}
SEAL_NODISCARD inline param_type param() const noexcept
{
return *this;
}
inline void param(const param_type &parm) noexcept
{
*this = parm;
}
inline void reset() noexcept
{
normal_.reset();
}
private:
std::normal_distribution<result_type> normal_;
result_type max_deviation_;
};
} // namespace util
} // namespace seal