BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IDistribution1DSampler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Correlations/IDistribution1DSampler.cpp
6 //! @brief Defines interface class IProfile1D, and children thereof
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <random>
17 
19 
21 {
22  // BornAgain Cauchy Distribution = std library Exponential distribution
23  std::random_device rd; // random device class instance
24  std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
25  std::exponential_distribution<double> expDist(m_lambda);
26  double value = expDist(gen);
27 
28  std::bernoulli_distribution bernoulliDist(0.5);
29  bool sign = bernoulliDist(gen);
30 
31  return sign ? value : -value;
32 }
33 
35 {
36  // BornAgain Gauss Distribution = std library Normal distribution
37  std::random_device rd;
38  std::mt19937 gen(rd());
39  std::normal_distribution<double> normalDist(m_mean, m_stddev);
40 
41  return normalDist(gen);
42 }
43 
45 {
46  // BornAgain Gate Distribution = std library Uniform distribution
47  std::random_device rd;
48  std::mt19937 gen(rd());
49  std::uniform_real_distribution<double> uniformDist(m_a, m_b);
50 
51  return uniformDist(gen);
52 }
53 
55 {
56  std::random_device rd;
57  std::mt19937 gen(rd());
58 
59  // generate a cdf value between 0 and 1
60  std::uniform_real_distribution<> uniformDist(0.0, 1.0);
61  double cdf_value = uniformDist(gen);
62 
63  // solve for x by inverting the cdf of Triangle Distribution
64  if (cdf_value <= 0.5)
65  return (-m_omega + m_omega * std::sqrt(2 * cdf_value));
66  return (m_omega - m_omega * std::sqrt(2 * (1 - cdf_value)));
67 }
68 
70 {
71  std::random_device rd;
72  std::mt19937 gen(rd());
73 
74  // generate a cdf value between 0 and 1
75  std::uniform_real_distribution<> uniformDist(0.0, 1.0);
76  double cdf_value = uniformDist(gen);
77 
78  // solve for x from the cdf of Cosine Distribution using Newton-Raphson method
79  double func = 0.0, funcDeriv = 0.0, x = 0.0;
80 
81  // initial guess for x
82  if (cdf_value <= 0.5)
83  x = -m_omega / 2;
84  else
85  x = m_omega / 2;
86 
87  bool convergedSoln = false;
88  while (!convergedSoln) {
89  func = x + m_omega / M_PI * std::sin(M_PI * x / m_omega) + m_omega * (1 - 2 * cdf_value);
90  funcDeriv = 1 + std::cos(M_PI * x / m_omega);
91 
92  x = x - func / funcDeriv;
93 
94  if (std::abs(func / funcDeriv) < 0.001)
95  convergedSoln = true;
96  }
97 
98  return x;
99 }
#define M_PI
Definition: Constants.h:44
Defines interface class IProfile1D, and children thereof.
double randomSample() const override
double randomSample() const override
double randomSample() const override
double randomSample() const override
double randomSample() const override
virtual ~IDistribution1DSampler()