31 #ifndef __TASMANIAN_DREAM_CORE_RANDOM_HPP
32 #define __TASMANIAN_DREAM_CORE_RANDOM_HPP
68 if (magnitude == 0.0)
return;
69 for(
auto &v : x) v += magnitude * (2.0 * get_random01() -1.0);
78 if (magnitude == 0.0)
return;
84 double r = magnitude * std::sqrt(-2.0 * std::log(get_random01())), t = 2.0 *
DreamMaths::pi * get_random01();
99 inline void genUniformSamples(
const std::vector<double> &lower,
const std::vector<double> &upper,
int num_samples, std::vector<double> &x, std::function<
double(
void)> get_random01 =
tsgCoreUniform01){
100 if (lower.size() != upper.size())
throw std::runtime_error(
"ERROR: genUniformSamples() requires lower and upper vectors with matching size.");
101 if (x.size() != lower.size() * num_samples) x.resize(lower.size() * num_samples);
102 for(
auto &v : x) v = get_random01();
104 std::vector<double> length(lower.size());
105 std::transform(lower.begin(), lower.end(), upper.begin(), length.begin(), [&](
double l,
double u)->double{ return (u - l); });
108 while(ix != x.end()){
109 auto ilow = lower.begin();
110 for(
auto l : length){
121 inline std::vector<double>
genUniformSamples(
const std::vector<double> &lower,
const std::vector<double> &upper,
122 int num_samples, std::function<
double(
void)> get_random01 =
tsgCoreUniform01){
123 std::vector<double> x;
134 inline void genGaussianSamples(
const std::vector<double> &means,
const std::vector<double> &deviations,
135 int num_samples, std::vector<double> &x, std::function<
double(
void)> get_random01 =
tsgCoreUniform01){
136 if (means.size() != deviations.size())
throw std::runtime_error(
"ERROR: genGaussianSamples() means and deviations vectors must have the same size.");
137 if (x.size() != means.size() * num_samples) x.resize(means.size() * num_samples);
139 std::fill_n(x.data(), x.size(), 0.0);
143 while(ix != x.end()){
144 auto im = means.begin();
145 for(
auto s : deviations){
156 inline std::vector<double>
genGaussianSamples(
const std::vector<double> &means,
const std::vector<double> &deviations,
157 int num_samples, std::function<
double(
void)> get_random01 =
tsgCoreUniform01){
158 std::vector<double> x;
void genUniformSamples(const std::vector< double > &lower, const std::vector< double > &upper, int num_samples, std::vector< double > &x, std::function< double(void)> get_random01=tsgCoreUniform01)
Generate uniform random samples in the hypercube defined by lower and upper limits.
Definition: tsgDreamCoreRandom.hpp:99
void applyGaussianUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, sue Gaussian distribution with zero mean and standard deviation...
Definition: tsgDreamCoreRandom.hpp:77
double tsgCoreUniform01()
Default random sampler, using rand() divided by RAND_MAX.
Definition: tsgDreamCoreRandom.hpp:61
void genGaussianSamples(const std::vector< double > &means, const std::vector< double > &deviations, int num_samples, std::vector< double > &x, std::function< double(void)> get_random01=tsgCoreUniform01)
Generate standard normal samples with given means and standard deviations.
Definition: tsgDreamCoreRandom.hpp:134
void applyUniformUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, use uniform samples over (-magnitude, magnitude).
Definition: tsgDreamCoreRandom.hpp:67
constexpr double pi
Half-period of the std::sin() and std::cos() functions.
Definition: tsgMathUtils.hpp:117
Encapsulates the Tasmanian DREAM module.
Definition: TasmanianDREAM.hpp:80