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

Description

Trapezoidal distribution.

Definition at line 304 of file Distributions.h.

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

Public Member Functions

 DistributionTrapezoid ()
 
 DistributionTrapezoid (double center, double left, double middle, double right)
 
 DistributionTrapezoid (std::vector< double > P)
 
void checkNodeArgs () const
 Raises exception if a parameter value is invalid. More...
 
std::string className () const final
 Returns the class name, to be hard-coded in each leaf class that inherits from INode. More...
 
DistributionTrapezoidclone () const override
 
std::vector< double > equidistantPoints (size_t nbr_samples, double sigma_factor, const RealLimits &limits=RealLimits()) const override
 generate list of sample values More...
 
virtual std::vector< double > equidistantPointsInRange (size_t nbr_samples, double xmin, double xmax) const
 Returns equidistant interpolation points from xmin to xmax. More...
 
std::vector< ParameterSampleequidistantSamples (size_t nbr_samples, double sigma_factor=0., const RealLimits &limits=RealLimits()) const
 Returns equidistant samples, using intrinsic parameters, weighted with probabilityDensity(). More...
 
std::vector< ParameterSampleequidistantSamplesInRange (size_t nbr_samples, double xmin, double xmax) const
 Returns equidistant samples from xmin to xmax, weighted with probabilityDensity(). More...
 
double getLeftWidth () const
 
double getMiddleWidth () const
 
double getRightWidth () const
 
bool isDelta () const override
 Returns true if the distribution is in the limit case of a Dirac delta distribution. More...
 
double mean () const override
 Returns the distribution-specific mean. More...
 
virtual std::vector< const INode * > nodeChildren () const
 Returns all children. More...
 
std::vector< const INode * > nodeOffspring () const
 Returns all descendants. More...
 
std::vector< ParaMetaparDefs () const final
 Returns the parameter definitions, to be hard-coded in each leaf class. More...
 
double probabilityDensity (double x) const override
 Returns the distribution-specific probability density for value x. More...
 
std::string pythonConstructor (const std::string &units) const override
 Prints distribution with constructor parameters in given units. ba.DistributionGaussian(2.0*deg, 0.02*deg) More...
 
virtual void transferToCPP ()
 Used for Python overriding of clone (see swig/tweaks.py) More...
 

Protected Member Functions

void adjustMinMaxForLimits (double &xmin, double &xmax, const RealLimits &limits) const
 modifies xmin and xmax if they are outside of limits More...
 
std::vector< ParameterSamplegenerateSamplesFromValues (const std::vector< double > &sample_values) const
 Returns weighted samples from given interpolation points and probabilityDensity(). More...
 

Protected Attributes

std::vector< double > m_P
 

Private Member Functions

void adjustLimitsToNonZeroSamples (double &min, double &max, size_t nbr_samples) const
 

Private Attributes

const double & m_center
 
const double & m_left
 
const double & m_middle
 
const double & m_right
 

Constructor & Destructor Documentation

◆ DistributionTrapezoid() [1/3]

DistributionTrapezoid::DistributionTrapezoid ( std::vector< double >  P)

Definition at line 394 of file Distributions.cpp.

395  : IDistribution1D(P)
396  , m_center(m_P[0])
397  , m_left(m_P[1])
398  , m_middle(m_P[2])
399  , m_right(m_P[3])
400 {
401  checkNodeArgs();
402  if (m_left < 0.0)
403  throw std::runtime_error("DistributionTrapezoid: leftWidth < 0");
404  if (m_middle < 0.0)
405  throw std::runtime_error("DistributionTrapezoid: middleWidth < 0");
406  if (m_right < 0.0)
407  throw std::runtime_error("DistributionTrapezoid: rightWidth < 0");
408 }
const double & m_center
const double & m_left
const double & m_middle
const double & m_right
IDistribution1D(const std::vector< double > &PValues)
void checkNodeArgs() const
Raises exception if a parameter value is invalid.
Definition: INode.cpp:27
std::vector< double > m_P
Definition: INode.h:63

References INode::checkNodeArgs(), m_left, m_middle, and m_right.

Here is the call graph for this function:

◆ DistributionTrapezoid() [2/3]

DistributionTrapezoid::DistributionTrapezoid ( double  center,
double  left,
double  middle,
double  right 
)

Definition at line 410 of file Distributions.cpp.

412  : DistributionTrapezoid(std::vector<double>{center, left, middle, right})
413 {
414 }

◆ DistributionTrapezoid() [3/3]

DistributionTrapezoid::DistributionTrapezoid ( )

Definition at line 416 of file Distributions.cpp.

417  : DistributionTrapezoid(0., 0., 1., 0.)
418 {
419 }

Referenced by clone().

Member Function Documentation

◆ adjustLimitsToNonZeroSamples()

void DistributionTrapezoid::adjustLimitsToNonZeroSamples ( double &  min,
double &  max,
size_t  nbr_samples 
) const
private

Definition at line 457 of file Distributions.cpp.

459 {
460  if (nbr_samples <= 1)
461  return;
462  size_t N = nbr_samples;
463  if (m_left > 0.0)
464  ++N;
465  if (m_right > 0.0)
466  ++N;
467  if (N == nbr_samples)
468  return;
469  double step = (max - min) / (N - 1);
470  if (m_left > 0.0)
471  min += step;
472  if (m_right > 0.0)
473  max -= step;
474 }
#define N
Definition: mixmax.h:31

References m_left, m_right, and N.

Referenced by equidistantPoints().

◆ adjustMinMaxForLimits()

void IDistribution1D::adjustMinMaxForLimits ( double &  xmin,
double &  xmax,
const RealLimits limits 
) const
protectedinherited

modifies xmin and xmax if they are outside of limits

Definition at line 81 of file Distributions.cpp.

83 {
84  if (limits.hasLowerLimit() && xmin < limits.lowerLimit())
85  xmin = limits.lowerLimit();
86  if (limits.hasUpperLimit() && xmax > limits.upperLimit())
87  xmax = limits.upperLimit();
88  if (xmin > xmax) {
89  std::ostringstream ostr;
90  ostr << "IDistribution1D::adjustMinMaxForLimits() -> Error. Can't' adjust ";
91  ostr << "xmin:" << xmin << " xmax:" << xmax << " for given limits " << limits << std::endl;
92  throw std::runtime_error(ostr.str());
93  }
94 }
bool hasUpperLimit() const
if has upper limit
Definition: RealLimits.cpp:66
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:71
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:49
bool hasLowerLimit() const
if has lower limit
Definition: RealLimits.cpp:44

References RealLimits::hasLowerLimit(), RealLimits::hasUpperLimit(), RealLimits::lowerLimit(), and RealLimits::upperLimit().

Referenced by DistributionGate::equidistantPoints(), DistributionLorentz::equidistantPoints(), DistributionGaussian::equidistantPoints(), DistributionLogNormal::equidistantPoints(), DistributionCosine::equidistantPoints(), and equidistantPoints().

Here is the call graph for this function:

◆ checkNodeArgs()

void INode::checkNodeArgs ( ) const
inherited

Raises exception if a parameter value is invalid.

Definition at line 27 of file INode.cpp.

28 {
29  size_t nP = m_P.size();
30  if (parDefs().size() != nP) {
31  std::cerr << "BUG in class " << className() << std::endl;
32  std::cerr << "#m_P = " << nP << std::endl;
33  std::cerr << "#PDf = " << parDefs().size() << std::endl;
34  for (const ParaMeta& pm : parDefs())
35  std::cerr << " PDf: " << pm.name << std::endl;
36  ASSERT(0);
37  }
38  ASSERT(parDefs().size() == nP);
39  for (size_t i = 0; i < nP; ++i) {
40  const ParaMeta pm = parDefs()[i];
41 
43  if (pm.vMin == -INF) {
44  ASSERT(pm.vMax == +INF);
45  // nothing to do
46  } else if (pm.vMax == +INF) {
47  ASSERT(pm.vMin == 0);
48  limits = RealLimits::nonnegative();
49  } else {
50  limits = RealLimits::limited(pm.vMin, pm.vMax);
51  }
52  limits.check(pm.name, m_P[i]);
53  }
54 }
#define ASSERT(condition)
Definition: Assert.h:45
const double INF
Definition: INode.h:26
virtual std::vector< ParaMeta > parDefs() const
Returns the parameter definitions, to be hard-coded in each leaf class.
Definition: INode.h:51
virtual std::string className() const =0
Returns the class name, to be hard-coded in each leaf class that inherits from INode.
Limits for a real fit parameter.
Definition: RealLimits.h:24
static RealLimits limitless()
Creates an object without bounds (default)
Definition: RealLimits.cpp:139
void check(const std::string &name, double value) const
Throws if value is outside limits. Parameter 'name' is for exception message.
Definition: RealLimits.cpp:170
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:124
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:134
Metadata of one model parameter.
Definition: INode.h:29
double vMin
Definition: INode.h:33
double vMax
Definition: INode.h:34
std::string name
Definition: INode.h:30

References ASSERT, RealLimits::check(), INode::className(), INF, RealLimits::limited(), RealLimits::limitless(), INode::m_P, ParaMeta::name, RealLimits::nonnegative(), INode::parDefs(), ParaMeta::vMax, and ParaMeta::vMin.

Referenced by BarGauss::BarGauss(), BarLorentz::BarLorentz(), Bipyramid4::Bipyramid4(), Box::Box(), CantellatedCube::CantellatedCube(), Cone::Cone(), ConstantBackground::ConstantBackground(), CosineRippleBox::CosineRippleBox(), CosineRippleGauss::CosineRippleGauss(), CosineRippleLorentz::CosineRippleLorentz(), Cylinder::Cylinder(), DistributionCosine::DistributionCosine(), DistributionGate::DistributionGate(), DistributionGaussian::DistributionGaussian(), DistributionLogNormal::DistributionLogNormal(), DistributionLorentz::DistributionLorentz(), DistributionTrapezoid(), Dodecahedron::Dodecahedron(), EllipsoidalCylinder::EllipsoidalCylinder(), FootprintGauss::FootprintGauss(), FootprintSquare::FootprintSquare(), FuzzySphere::FuzzySphere(), GaussSphere::GaussSphere(), HemiEllipsoid::HemiEllipsoid(), HollowSphere::HollowSphere(), HorizontalCylinder::HorizontalCylinder(), Icosahedron::Icosahedron(), LongBoxGauss::LongBoxGauss(), LongBoxLorentz::LongBoxLorentz(), PlatonicOctahedron::PlatonicOctahedron(), PlatonicTetrahedron::PlatonicTetrahedron(), Prism3::Prism3(), Prism6::Prism6(), Profile1DCauchy::Profile1DCauchy(), Profile1DCosine::Profile1DCosine(), Profile1DGate::Profile1DGate(), Profile1DGauss::Profile1DGauss(), Profile1DTriangle::Profile1DTriangle(), Profile1DVoigt::Profile1DVoigt(), Profile2DCauchy::Profile2DCauchy(), Profile2DCone::Profile2DCone(), Profile2DGate::Profile2DGate(), Profile2DGauss::Profile2DGauss(), Profile2DVoigt::Profile2DVoigt(), Pyramid2::Pyramid2(), Pyramid3::Pyramid3(), Pyramid4::Pyramid4(), Pyramid6::Pyramid6(), RotationEuler::RotationEuler(), RotationX::RotationX(), RotationY::RotationY(), RotationZ::RotationZ(), SawtoothRippleBox::SawtoothRippleBox(), SawtoothRippleGauss::SawtoothRippleGauss(), SawtoothRippleLorentz::SawtoothRippleLorentz(), Sphere::Sphere(), Spheroid::Spheroid(), TruncatedCube::TruncatedCube(), TruncatedSphere::TruncatedSphere(), and TruncatedSpheroid::TruncatedSpheroid().

Here is the call graph for this function:

◆ className()

std::string DistributionTrapezoid::className ( ) const
inlinefinalvirtual

Returns the class name, to be hard-coded in each leaf class that inherits from INode.

Implements INode.

Definition at line 314 of file Distributions.h.

314 { return "DistributionTrapezoid"; }

Referenced by pythonConstructor().

◆ clone()

DistributionTrapezoid* DistributionTrapezoid::clone ( ) const
inlineoverridevirtual

Implements IDistribution1D.

Definition at line 310 of file Distributions.h.

311  {
313  }

References DistributionTrapezoid(), m_center, m_left, m_middle, and m_right.

Here is the call graph for this function:

◆ equidistantPoints()

std::vector< double > DistributionTrapezoid::equidistantPoints ( size_t  nbr_samples,
double  sigma_factor,
const RealLimits limits = RealLimits() 
) const
overridevirtual

generate list of sample values

Implements IDistribution1D.

Definition at line 436 of file Distributions.cpp.

438 {
439  double xmin = m_center - m_middle / 2.0 - m_left;
440  double xmax = xmin + m_left + m_middle + m_right;
441  adjustLimitsToNonZeroSamples(xmin, xmax, nbr_samples);
442  adjustMinMaxForLimits(xmin, xmax, limits);
443  return equidistantPointsInRange(nbr_samples, xmin, xmax);
444 }
void adjustLimitsToNonZeroSamples(double &min, double &max, size_t nbr_samples) const
void adjustMinMaxForLimits(double &xmin, double &xmax, const RealLimits &limits) const
modifies xmin and xmax if they are outside of limits
virtual std::vector< double > equidistantPointsInRange(size_t nbr_samples, double xmin, double xmax) const
Returns equidistant interpolation points from xmin to xmax.

References adjustLimitsToNonZeroSamples(), IDistribution1D::adjustMinMaxForLimits(), IDistribution1D::equidistantPointsInRange(), m_center, m_left, m_middle, and m_right.

Here is the call graph for this function:

◆ equidistantPointsInRange()

std::vector< double > IDistribution1D::equidistantPointsInRange ( size_t  nbr_samples,
double  xmin,
double  xmax 
) const
virtualinherited

Returns equidistant interpolation points from xmin to xmax.

Definition at line 70 of file Distributions.cpp.

72 {
73  if (nbr_samples < 2 || DoubleEqual(xmin, xmax))
74  return {mean()};
75  std::vector<double> result(nbr_samples);
76  for (size_t i = 0; i < nbr_samples; ++i)
77  result[i] = xmin + i * (xmax - xmin) / (nbr_samples - 1.0);
78  return result;
79 }
virtual double mean() const =0
Returns the distribution-specific mean.

References IDistribution1D::mean().

Referenced by DistributionGate::equidistantPoints(), DistributionLorentz::equidistantPoints(), DistributionGaussian::equidistantPoints(), DistributionLogNormal::equidistantPoints(), DistributionCosine::equidistantPoints(), equidistantPoints(), and IDistribution1D::equidistantSamplesInRange().

Here is the call graph for this function:

◆ equidistantSamples()

std::vector< ParameterSample > IDistribution1D::equidistantSamples ( size_t  nbr_samples,
double  sigma_factor = 0.,
const RealLimits limits = RealLimits() 
) const
inherited

Returns equidistant samples, using intrinsic parameters, weighted with probabilityDensity().

Definition at line 43 of file Distributions.cpp.

46 {
47  if (nbr_samples == 0)
48  throw std::runtime_error("IDistribution1D::generateSamples: "
49  "number of generated samples must be bigger than zero");
50  if (isDelta())
51  return {ParameterSample(mean())};
52  return generateSamplesFromValues(equidistantPoints(nbr_samples, sigma_factor, limits));
53 }
std::vector< ParameterSample > generateSamplesFromValues(const std::vector< double > &sample_values) const
Returns weighted samples from given interpolation points and probabilityDensity().
virtual bool isDelta() const =0
Returns true if the distribution is in the limit case of a Dirac delta distribution.
virtual std::vector< double > equidistantPoints(size_t nbr_samples, double sigma_factor, const RealLimits &limits=RealLimits()) const =0
Returns equidistant interpolation points, with range computed in distribution-specific way from mean ...
A parameter value with a weight, as obtained when sampling from a distribution.

References IDistribution1D::equidistantPoints(), IDistribution1D::generateSamplesFromValues(), IDistribution1D::isDelta(), and IDistribution1D::mean().

Here is the call graph for this function:

◆ equidistantSamplesInRange()

std::vector< ParameterSample > IDistribution1D::equidistantSamplesInRange ( size_t  nbr_samples,
double  xmin,
double  xmax 
) const
inherited

Returns equidistant samples from xmin to xmax, weighted with probabilityDensity().

Definition at line 58 of file Distributions.cpp.

59 {
60  if (nbr_samples == 0)
61  throw std::runtime_error("IDistribution1D::generateSamples: "
62  "number of generated samples must be bigger than zero");
63  if (isDelta())
64  return {ParameterSample(mean())};
65  return generateSamplesFromValues(equidistantPointsInRange(nbr_samples, xmin, xmax));
66 }

References IDistribution1D::equidistantPointsInRange(), IDistribution1D::generateSamplesFromValues(), IDistribution1D::isDelta(), and IDistribution1D::mean().

Here is the call graph for this function:

◆ generateSamplesFromValues()

std::vector< ParameterSample > IDistribution1D::generateSamplesFromValues ( const std::vector< double > &  sample_values) const
protectedinherited

Returns weighted samples from given interpolation points and probabilityDensity().

Definition at line 99 of file Distributions.cpp.

100 {
101  std::vector<ParameterSample> result;
102  double norm_factor = 0.0;
103  for (double value : sample_values) {
104  double pdf = probabilityDensity(value);
105  result.emplace_back(value, pdf);
106  norm_factor += pdf;
107  }
108  if (norm_factor <= 0.0)
109  throw std::runtime_error("IDistribution1D::generateSamples: "
110  "total probability must be bigger than zero");
111  for (ParameterSample& sample : result)
112  sample.weight /= norm_factor;
113  return result;
114 }
virtual double probabilityDensity(double x) const =0
Returns the distribution-specific probability density for value x.

References IDistribution1D::probabilityDensity().

Referenced by IDistribution1D::equidistantSamples(), and IDistribution1D::equidistantSamplesInRange().

Here is the call graph for this function:

◆ getLeftWidth()

double DistributionTrapezoid::getLeftWidth ( ) const
inline

Definition at line 326 of file Distributions.h.

326 { return m_left; }

References m_left.

◆ getMiddleWidth()

double DistributionTrapezoid::getMiddleWidth ( ) const
inline

Definition at line 327 of file Distributions.h.

327 { return m_middle; }

References m_middle.

◆ getRightWidth()

double DistributionTrapezoid::getRightWidth ( ) const
inline

Definition at line 328 of file Distributions.h.

328 { return m_right; }

References m_right.

◆ isDelta()

bool DistributionTrapezoid::isDelta ( ) const
overridevirtual

Returns true if the distribution is in the limit case of a Dirac delta distribution.

Implements IDistribution1D.

Definition at line 446 of file Distributions.cpp.

447 {
448  return (m_left + m_middle + m_right) == 0.0;
449 }

References m_left, m_middle, and m_right.

◆ mean()

double DistributionTrapezoid::mean ( ) const
inlineoverridevirtual

Returns the distribution-specific mean.

Implements IDistribution1D.

Definition at line 325 of file Distributions.h.

325 { return m_center; }

References m_center.

◆ nodeChildren()

◆ nodeOffspring()

std::vector< const INode * > INode::nodeOffspring ( ) const
inherited

Returns all descendants.

Definition at line 61 of file INode.cpp.

62 {
63  std::vector<const INode*> result;
64  result.push_back(this);
65  for (const auto* child : nodeChildren()) {
66  for (const auto* p : child->nodeOffspring())
67  result.push_back(p);
68  }
69  return result;
70 }
virtual std::vector< const INode * > nodeChildren() const
Returns all children.
Definition: INode.cpp:56

References INode::nodeChildren().

Here is the call graph for this function:

◆ parDefs()

std::vector<ParaMeta> DistributionTrapezoid::parDefs ( ) const
inlinefinalvirtual

Returns the parameter definitions, to be hard-coded in each leaf class.

Reimplemented from INode.

Definition at line 316 of file Distributions.h.

317  {
318  return {{"Center", "", "para_tooltip", -INF, +INF, 0},
319  {"LeftWidth", "", "para_tooltip", -INF, +INF, 0},
320  {"MiddleWidth", "", "para_tooltip", -INF, +INF, 0},
321  {"RightWidth", "", "para_tooltip", -INF, +INF, 0}};
322  }

References INF.

◆ probabilityDensity()

double DistributionTrapezoid::probabilityDensity ( double  x) const
overridevirtual

Returns the distribution-specific probability density for value x.

Implements IDistribution1D.

Definition at line 421 of file Distributions.cpp.

422 {
423  double height = 2.0 / (m_left + 2.0 * m_middle + m_right);
424  double min = m_center - m_middle / 2.0 - m_left;
425  if (x < min)
426  return 0.0;
427  if (x < min + m_left)
428  return (x - min) * height / m_left;
429  if (x < min + m_left + m_middle)
430  return height;
431  if (x < min + m_left + m_middle + m_right)
432  return height - (x - min - m_left - m_middle) * height / m_right;
433  return 0.0;
434 }

References m_center, m_left, m_middle, and m_right.

◆ pythonConstructor()

std::string DistributionTrapezoid::pythonConstructor ( const std::string &  units) const
overridevirtual

Prints distribution with constructor parameters in given units. ba.DistributionGaussian(2.0*deg, 0.02*deg)

Implements IDistribution1D.

Definition at line 451 of file Distributions.cpp.

452 {
453  return Py::Fmt::printFunction(
454  className(), {{m_center, units}, {m_left, units}, {m_middle, units}, {m_right, units}});
455 }
std::string className() const final
Returns the class name, to be hard-coded in each leaf class that inherits from INode.
std::string printFunction(const std::string &name, const std::vector< std::pair< double, std::string >> &arguments)
Print a function in the form "<name>(<arguments>)". arguments will be processed by printArguments(),...
Definition: PyFmt.cpp:168

References className(), m_center, m_left, m_middle, m_right, and Py::Fmt::printFunction().

Here is the call graph for this function:

◆ transferToCPP()

virtual void ICloneable::transferToCPP ( )
inlinevirtualinherited

Used for Python overriding of clone (see swig/tweaks.py)

Definition at line 32 of file ICloneable.h.

Member Data Documentation

◆ m_center

const double& DistributionTrapezoid::m_center
private

◆ m_left

const double& DistributionTrapezoid::m_left
private

◆ m_middle

const double& DistributionTrapezoid::m_middle
private

◆ m_P

◆ m_right

const double& DistributionTrapezoid::m_right
private

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