BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Distribution1DCosineSampler Class Reference

Description

Definition at line 81 of file IDistribution1DSampler.h.

Inheritance diagram for Distribution1DCosineSampler:
[legend]
Collaboration diagram for Distribution1DCosineSampler:
[legend]

Public Member Functions

 Distribution1DCosineSampler (double omega)
 
double randomSample () const override
 

Private Attributes

double m_omega
 

Constructor & Destructor Documentation

◆ Distribution1DCosineSampler()

Distribution1DCosineSampler::Distribution1DCosineSampler ( double  omega)
inline

Definition at line 83 of file IDistribution1DSampler.h.

84  : m_omega(omega)
85  {
86  }

Member Function Documentation

◆ randomSample()

double Distribution1DCosineSampler::randomSample ( ) const
overridevirtual

Implements IDistribution1DSampler.

Definition at line 69 of file IDistribution1DSampler.cpp.

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

References m_omega, and M_PI.

Member Data Documentation

◆ m_omega

double Distribution1DCosineSampler::m_omega
private

Definition at line 90 of file IDistribution1DSampler.h.

Referenced by randomSample().


The documentation for this class was generated from the following files: