Expression Templates Library (ETL)
ranged_noise.hpp
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
8 #pragma once
9 
10 #include <ctime>
11 
12 namespace etl {
13 
20 template <typename G, typename T, typename E>
22  static constexpr bool linear = true;
23  static constexpr bool thread_safe = false;
24  static constexpr bool desc_func = true;
25 
31  template <vector_mode_t V>
32  static constexpr bool vectorizable = false;
33 
37  template <typename L, typename R>
38  static constexpr bool gpu_computable = false;
39 
44  static constexpr int complexity() {
45  return 1;
46  }
47 
49 
53  ranged_noise_binary_g_op(G& rand_engine) : rand_engine(rand_engine) {
54  //Nothing else to init
55  }
56 
63  T apply(const T& x, E value) {
64  std::normal_distribution<double> normal_distribution(0.0, 1.0);
65  auto noise = std::bind(normal_distribution, rand_engine);
66 
67  if (x == 0.0 || x == value) {
68  return x;
69  } else {
70  return x + noise();
71  }
72  }
73 
78  static std::string desc() noexcept {
79  return "ranged_noise";
80  }
81 };
82 
89 template <typename T, typename E>
91  static constexpr bool linear = true;
92  static constexpr bool thread_safe = false;
93  static constexpr bool desc_func = true;
94 
100  template <vector_mode_t V>
101  static constexpr bool vectorizable = false;
102 
106  template <typename L, typename R>
107  static constexpr bool gpu_computable = false;
108 
115  static T apply(const T& x, E value) {
116  static random_engine rand_engine(std::time(nullptr));
117  static std::normal_distribution<double> normal_distribution(0.0, 1.0);
118  static auto noise = std::bind(normal_distribution, rand_engine);
119 
120  if (x == 0.0 || x == value) {
121  return x;
122  } else {
123  return x + noise();
124  }
125  }
126 
131  static std::string desc() noexcept {
132  return "ranged_noise";
133  }
134 };
135 
136 } //end of namespace etl
static constexpr bool desc_func
Indicates if the description must be printed as function.
Definition: ranged_noise.hpp:24
Binary operator for ranged noise generation.
Definition: ranged_noise.hpp:90
ranged_noise_binary_g_op(G &rand_engine)
Construct a new ranged_noise_binary_g_op.
Definition: ranged_noise.hpp:53
static constexpr bool linear
Indicates if the operator is linear or not.
Definition: ranged_noise.hpp:22
G & rand_engine
The random engine.
Definition: ranged_noise.hpp:48
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: ranged_noise.hpp:38
static constexpr int complexity()
Estimate the complexity of operator.
Definition: ranged_noise.hpp:44
static T apply(const T &x, E value)
Apply the unary operator on lhs and rhs.
Definition: ranged_noise.hpp:115
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: ranged_noise.hpp:32
T apply(const T &x, E value)
Apply the unary operator on lhs and rhs.
Definition: ranged_noise.hpp:63
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: ranged_noise.hpp:78
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: ranged_noise.hpp:131
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: ranged_noise.hpp:23
Binary operator for ranged noise generation.
Definition: ranged_noise.hpp:21
std::mt19937_64 random_engine
The random engine used by the library.
Definition: random.hpp:22