BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Math Namespace Reference

Various mathematical functions. More...

Namespaces

 Bessel
 Real and complex Bessel functions.
 
 internal
 

Functions

double cot (double x)
 cotangent function: $cot(x)\equiv1/tan(x)$ More...
 
double erf (double arg)
 Error function of real-valued argument. More...
 
double Gaussian (double x, double average, double std_dev)
 
double GeneratePoissonRandom (double average)
 
template<size_t N, typename Indices = std::make_index_sequence<N>>
constexpr std::array< double, N > generateReciprocalFactorialArray ()
 Returns a compile-time generated std::array of reciprocal factorials. More...
 
double IntegratedGaussian (double x, double average, double std_dev)
 
double Laue (const double x, size_t N)
 Real Laue function: $Laue(x,N)\equiv\sin(Nx)/sin(x)$. More...
 
complex_t sinc (const complex_t z)
 Complex sinc function: $sinc(x)\equiv\sin(x)/x$. More...
 
double sinc (double x)
 sinc function: $sinc(x)\equiv\sin(x)/x$ More...
 
double StandardNormal (double x)
 
complex_t tanhc (const complex_t z)
 Complex tanhc function: $tanhc(x)\equiv\tanh(x)/x$. More...
 

Detailed Description

Various mathematical functions.

Function Documentation

◆ cot()

◆ erf()

double Math::erf ( double  arg)

Error function of real-valued argument.

Definition at line 87 of file Functions.cpp.

88 {
89  if (arg < 0.0)
90  throw std::runtime_error("Error in Math::erf: negative argument is not allowed");
91  if (std::isinf(arg))
92  return 1.0;
93  return gsl_sf_erf(arg);
94 }

Referenced by FootprintGauss::calculate().

◆ Gaussian()

double Math::Gaussian ( double  x,
double  average,
double  std_dev 
)

Definition at line 36 of file Functions.cpp.

37 {
38  return StandardNormal((x - average) / std_dev) / std_dev;
39 }
double StandardNormal(double x)
Definition: Functions.cpp:31

References StandardNormal().

Here is the call graph for this function:

◆ GeneratePoissonRandom()

double Math::GeneratePoissonRandom ( double  average)

Definition at line 123 of file Functions.cpp.

124 {
125  unsigned seed =
126  static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count());
127  std::default_random_engine generator(seed);
128  if (average <= 0.0)
129  return 0.0;
130  if (average < 1000.0) { // Use std::poisson_distribution
131  std::poisson_distribution<int> distribution(average);
132  int sample = distribution(generator);
133  return (double)sample;
134  } else { // Use normal approximation
135  std::normal_distribution<double> distribution(average, std::sqrt(average));
136  double sample = distribution(generator);
137  return std::max(0.0, sample);
138  }
139 }

Referenced by PoissonNoiseBackground::addBackground().

◆ generateReciprocalFactorialArray()

template<size_t N, typename Indices = std::make_index_sequence<N>>
constexpr std::array<double, N> Math::generateReciprocalFactorialArray ( )
constexpr

Returns a compile-time generated std::array of reciprocal factorials.

Definition at line 49 of file Precomputed.h.

50 {
51  return internal::generateArrayHelper<internal::ReciprocalFactorial>(Indices{});
52 };

◆ IntegratedGaussian()

double Math::IntegratedGaussian ( double  x,
double  average,
double  std_dev 
)

Definition at line 41 of file Functions.cpp.

42 {
43  double normalized_x = (x - average) / std_dev;
44  static double root2 = std::sqrt(2.0);
45  return (gsl_sf_erf(normalized_x / root2) + 1.0) / 2.0;
46 }

Referenced by ResolutionFunction2DGaussian::evaluateCDF().

◆ Laue()

double Math::Laue ( const double  x,
size_t  N 
)

Real Laue function: $Laue(x,N)\equiv\sin(Nx)/sin(x)$.

Definition at line 76 of file Functions.cpp.

77 {
78  static const double SQRT6DOUBLE_EPS = std::sqrt(6.0 * std::numeric_limits<double>::epsilon());
79  auto nd = static_cast<double>(N);
80  if (std::abs(nd * x) < SQRT6DOUBLE_EPS)
81  return nd;
82  double num = std::sin(nd * x);
83  double den = std::sin(x);
84  return num / den;
85 }

Referenced by InterferenceFunction2DSuperLattice::iff_without_dw(), InterferenceFunctionFinite3DLattice::iff_without_dw(), and InterferenceFunctionFinite2DLattice::interferenceForXi().

◆ sinc() [1/2]

complex_t Math::sinc ( const complex_t  z)

Complex sinc function: $sinc(x)\equiv\sin(x)/x$.

Definition at line 58 of file Functions.cpp.

59 {
60  // This is an exception from the rule that we must not test floating-point numbers for equality.
61  // For small non-zero arguments, sin(z) returns quite accurately z or z-z^3/6.
62  // There is no loss of precision in computing sin(z)/z.
63  // Therefore there is no need for an expensive test like abs(z)<eps.
64  if (z == complex_t(0., 0.))
65  return 1.0;
66  return std::sin(z) / z;
67 }
std::complex< double > complex_t
Definition: Complex.h:20

◆ sinc() [2/2]

◆ StandardNormal()

double Math::StandardNormal ( double  x)

Definition at line 31 of file Functions.cpp.

32 {
33  return std::exp(-x * x / 2.0) / std::sqrt(M_TWOPI);
34 }
#define M_TWOPI
Definition: Constants.h:54

References M_TWOPI.

Referenced by Gaussian().

◆ tanhc()

complex_t Math::tanhc ( const complex_t  z)

Complex tanhc function: $tanhc(x)\equiv\tanh(x)/x$.

Definition at line 69 of file Functions.cpp.

70 {
71  if (std::abs(z) < std::numeric_limits<double>::epsilon())
72  return 1.0;
73  return std::tanh(z) / z;
74 }

Referenced by SpecularMagneticTanhStrategy::computeRoughnessMatrix(), and SpecularScalarTanhStrategy::transition().