BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Distribution1DCosineSampler Class Reference
Inheritance diagram for Distribution1DCosineSampler:
Collaboration diagram for Distribution1DCosineSampler:

Public Member Functions

 Distribution1DCosineSampler (double omega)
 
double randomSample () const final
 

Private Attributes

double m_omega
 

Detailed Description

Definition at line 67 of file IDistribution1DSampler.h.

Constructor & Destructor Documentation

◆ Distribution1DCosineSampler()

Distribution1DCosineSampler::Distribution1DCosineSampler ( double  omega)
inline

Definition at line 70 of file IDistribution1DSampler.h.

Member Function Documentation

◆ randomSample()

double Distribution1DCosineSampler::randomSample ( ) const
finalvirtual

Implements IDistribution1DSampler.

Definition at line 73 of file IDistribution1DSampler.cpp.

74 {
75  std::random_device rd;
76  std::mt19937 gen(rd());
77 
78  // generate a cdf value between 0 and 1
79  std::uniform_real_distribution<> uniformDist(0.0, 1.0);
80  double cdf_value = uniformDist(gen);
81 
82  // solve for x from the cdf of Cosine Distribution using Newton-Raphson method
83  double func = 0.0, funcDeriv = 0.0, x = 0.0;
84 
85  // initial guess for x
86  if (cdf_value <= 0.5)
87  x = -m_omega / 2;
88  else
89  x = m_omega / 2;
90 
91  bool convergedSoln = false;
92  while (!convergedSoln) {
93  func = x + m_omega / M_PI * std::sin(M_PI * x / m_omega) + m_omega * (1 - 2 * cdf_value);
94  funcDeriv = 1 + std::cos(M_PI * x / m_omega);
95 
96  x = x - func / funcDeriv;
97 
98  if (std::abs(func / funcDeriv) < 0.001)
99  convergedSoln = true;
100  }
101 
102  return x;
103 }
#define M_PI
Definition: MathConstants.h:39

References m_omega, and M_PI.

Member Data Documentation

◆ m_omega

double Distribution1DCosineSampler::m_omega
private

Definition at line 74 of file IDistribution1DSampler.h.

Referenced by randomSample().


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