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

Description

Various mathematical functions.

Namespace for new Math classes and functions. See the Math Libraries page for a detailed description.

Namespaces

 Bessel
 Real and complex Bessel functions.
 

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)
 
double IntegratedGaussian (double x, double average, double std_dev)
 
double Laue (double x, size_t N)
 Real Laue function: $Laue(x,N)\equiv\sin(Nx)/sin(x)$. More...
 
complex_t sinc (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 (complex_t z)
 Complex tanhc function: $tanhc(x)\equiv\tanh(x)/x$. More...
 

Function Documentation

◆ cot()

double Math::cot ( double  x)

cotangent function: $cot(x)\equiv1/tan(x)$

Definition at line 47 of file Functions.cpp.

48 {
49  return tan(M_PI_2 - x);
50 }
#define M_PI_2
Definition: Constants.h:45

References M_PI_2.

Referenced by Bipyramid4::Bipyramid4(), Cone::Cone(), Pyramid2::Pyramid2(), Pyramid3::Pyramid3(), Pyramid4::Pyramid4(), and Pyramid6::Pyramid6().

◆ erf()

double Math::erf ( double  arg)

Error function of real-valued argument.

Definition at line 88 of file Functions.cpp.

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

Referenced by FootprintGauss::calculate().

◆ Gaussian()

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

Definition at line 35 of file Functions.cpp.

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

References StandardNormal().

Here is the call graph for this function:

◆ GeneratePoissonRandom()

double Math::GeneratePoissonRandom ( double  average)

Definition at line 124 of file Functions.cpp.

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

Referenced by PoissonBackground::addBackground().

◆ IntegratedGaussian()

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

Definition at line 40 of file Functions.cpp.

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

Referenced by ResolutionFunction2DGaussian::evaluateCDF().

◆ Laue()

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

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

Definition at line 77 of file Functions.cpp.

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

References N.

Referenced by InterferenceFinite3DLattice::iff_without_dw(), Interference2DSuperLattice::iff_without_dw(), and InterferenceFinite2DLattice::interferenceForXi().

◆ sinc() [1/2]

complex_t Math::sinc ( complex_t  z)

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

Definition at line 59 of file Functions.cpp.

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

◆ sinc() [2/2]

◆ StandardNormal()

double Math::StandardNormal ( double  x)

Definition at line 30 of file Functions.cpp.

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

References M_TWOPI.

Referenced by Gaussian().

◆ tanhc()

complex_t Math::tanhc ( complex_t  z)

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

Definition at line 70 of file Functions.cpp.

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