Alexandria  2.19
Please provide a description of the project.
Classes | Typedefs | Enumerations | Functions | Variables
Euclid::MathUtils Namespace Reference

Classes

interface  Differentiable
 Interface representing a differentiable function. More...
 
interface  Function
 Interface class representing a function. More...
 
interface  NumericalIntegrationScheme
 Interface class representing a numerical integration scheme. More...
 
class  FunctionAdapter
 Adapt a std::function<double(double)> to the Function Interface. More...
 
interface  Integrable
 Interface representing an integrable function. More...
 
class  Piecewise
 Represents a piecewise function. More...
 
class  Polynomial
 Represents a polynomial function. More...
 
struct  InterpolationException
 
class  AdaptativeIntegration
 Class implementing the NumericalIntegrationScheme interface. More...
 
class  SimpsonsRule
 
class  Cumulative
 Class for build cumulative from PDF and extract feature out of it. More...
 
class  ModeInfo
 Class for storing the information of a PDF mode. More...
 
class  DefaultMultiplication
 

Typedefs

typedef std::unique_ptr< Function >(* MultiplyFunction) (const Function &, const Function &)
 Alias of a function which multiplies Function objects. More...
 

Enumerations

enum class  InterpolationType { LINEAR , CUBIC_SPLINE }
 Enumeration of the different supported interpolation types. More...
 

Functions

ELEMENTS_API double integrate (const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
 
ELEMENTS_API std::unique_ptr< Functionmultiply (const Function &f1, const Function &f2)
 
ELEMENTS_API std::unique_ptr< Functioninterpolate (const std::vector< double > &x, const std::vector< double > &y, InterpolationType type, bool extrapolate=false)
 
ELEMENTS_API std::unique_ptr< Functioninterpolate (const Euclid::XYDataset::XYDataset &dataset, InterpolationType type, bool extrapolate=false)
 
double derivative (const Function &f, const double x)
 
double derivative2nd (const Function &f, const double x)
 
std::vector< ModeInfoextractNHighestModes (const XYDataset::XYDataset &pdf, double merge_ratio, size_t n)
 
std::vector< ModeInfoextractNHighestModes (std::vector< double > &x_sampling, std::vector< double > &pdf_sampling, double merge_ratio, size_t n)
 
std::vector< ModeInfoextractNBigestModes (const XYDataset::XYDataset &pdf, double merge_ratio, size_t n)
 
std::vector< ModeInfoextractNBigestModes (std::vector< double > &x_sampling, std::vector< double > &pdf_sampling, double merge_ratio, size_t n)
 
std::unique_ptr< FunctionmultiplyPolynomials (const Function &f1, const Function &f2)
 Function for multiplying two Polynomials. It multiplies their coefficients. More...
 
std::unique_ptr< FunctionmultiplyPiecewiseWithGeneric (const Function &f1, const Function &f2)
 
std::vector< double > overlappingKnots (const std::vector< double > &knots1, const std::vector< double > &knots2)
 Returns a vector of the overlapping knots from the given vectors. More...
 
std::unique_ptr< FunctionmultiplyPiecewises (const Function &f1, const Function &f2)
 
std::unique_ptr< FunctionlinearInterpolation (const std::vector< double > &x, const std::vector< double > &y, bool extrapolate)
 Performs linear interpolation for the given set of data points. More...
 
std::unique_ptr< FunctionsplineInterpolation (const std::vector< double > &x, const std::vector< double > &y, bool extrapolate)
 Performs cubic spline interpolation for the given set of data points. More...
 
static double guess_h (double initial_h, double x)
 
std::pair< std::vector< double >, std::vector< double > > getXYs (const XYDataset::XYDataset &pdf)
 
size_t findMaximumIndex (const std::vector< double > &pdf)
 
std::pair< size_t, size_t > catchPeak (const std::vector< double > &pdf, size_t center_index, double merge_ratio)
 
std::pair< double, double > avgArea (std::pair< std::vector< double >, std::vector< double >> &pdf, size_t min_x, size_t max_x)
 
double getInterpolationAround (const std::pair< std::vector< double >, std::vector< double >> &pdf, size_t x_index)
 
std::pair< std::vector< double >, std::vector< double > > flatternPeak (const std::pair< std::vector< double >, std::vector< double >> &pdf, size_t min_x, size_t max_x, double value)
 

Variables

ELEMENTS_API std::map< std::pair< std::type_index, std::type_index >, MultiplyFunctionmultiplySpecificSpecificMap
 
ELEMENTS_API std::map< std::type_index, MultiplyFunctionmultiplySpecificGenericMap {{typeid(Piecewise), multiplyPiecewiseWithGeneric}}
 

Typedef Documentation

◆ MultiplyFunction

typedef std::unique_ptr<Function>(* Euclid::MathUtils::MultiplyFunction) (const Function &, const Function &)

Alias of a function which multiplies Function objects.

Definition at line 40 of file multiplication.h.

Enumeration Type Documentation

◆ InterpolationType

Enumeration of the different supported interpolation types.

Enumerator
LINEAR 
CUBIC_SPLINE 

Definition at line 41 of file interpolation.h.

Function Documentation

◆ avgArea()

std::pair<double, double> Euclid::MathUtils::avgArea ( std::pair< std::vector< double >, std::vector< double >> &  pdf,
size_t  min_x,
size_t  max_x 
)

Definition at line 99 of file PdfModeExtraction.cpp.

References std::make_pair().

Referenced by extractNBigestModes(), and extractNHighestModes().

Here is the call graph for this function:

◆ catchPeak()

std::pair<size_t, size_t> Euclid::MathUtils::catchPeak ( const std::vector< double > &  pdf,
size_t  center_index,
double  merge_ratio 
)

Definition at line 65 of file PdfModeExtraction.cpp.

References std::make_pair(), and std::vector< T >::size().

Referenced by extractNBigestModes(), and extractNHighestModes().

Here is the call graph for this function:

◆ derivative()

double Euclid::MathUtils::derivative ( const Function f,
const double  x 
)

Derivative using finite differences: \( f'(x) = \frac{f(x+h) - f(x)}{h} \)

Note
h is computed automatically following the same approach as boost numeric differentiation: \( h =(x + \sqrt{\epsilon} * 2) - x \) This guarantees that xh is representable using floating point (since (x + h) - x is not necessarily equal to h) If h happens to be 0, std::nextafter is called
Parameters
fThe Function to derive
xThe point where to do the derivation
Returns
An approximation of f'(x)

Definition at line 41 of file FiniteDifference.cpp.

References guess_h(), and std::sqrt().

Here is the call graph for this function:

◆ derivative2nd()

double Euclid::MathUtils::derivative2nd ( const Function f,
const double  x 
)

Second derivative using finite differences: \( f''(x) = \frac{f(x+h) - 2f(x) + f(x - h)}{h^2} \)

Note
h is computed automatically as follows: \( h =(x + \sqrt[4]{\epsilon} * 2) - x \) This guarantees that xh is representable using floating point (since (x + h) - x is not necessarily equal to h) If h happens to be 0, std::nextafter is called
Parameters
fThe Function to derive
xThe point where to do the derivation
Returns
An approximation of f''(x)

Definition at line 50 of file FiniteDifference.cpp.

References guess_h(), and std::sqrt().

Here is the call graph for this function:

◆ extractNBigestModes() [1/2]

std::vector< ModeInfo > Euclid::MathUtils::extractNBigestModes ( const XYDataset::XYDataset pdf,
double  merge_ratio,
size_t  n 
)

Extract the n modes with biggest area in the provided pdf and compute for each of them the location of the mode and its area. A mode is discovered as the highest point of the pdf, then, on both sides, samples are added until the pdf starts to rise again. In order to avoid truncating the mode due to a noisy pdf it is possible to specify a merge_ratio. In this case the samples are added until their values is below the hight of the peak times the merge ratio, then additional point are added until the pdf rise again.

Parameters
pdfThe sampling of the pdf to be analysed.
merge_ratioThe parameter for mode cutting.
nThe (maximum) number of modes to be returned.
Returns
A vector of ModeInfo containing the position and the area of the modes.

Definition at line 238 of file PdfModeExtraction.cpp.

References getXYs().

Here is the call graph for this function:

◆ extractNBigestModes() [2/2]

std::vector< ModeInfo > Euclid::MathUtils::extractNBigestModes ( std::vector< double > &  x_sampling,
std::vector< double > &  pdf_sampling,
double  merge_ratio,
size_t  n 
)

Extract the n modes with biggest area in the provided pdf and compute for each of them the location of the mode and its area. A mode is discovered as the highest point of the pdf, then, on both sides, samples are added until the pdf starts to rise again. In order to avoid truncating the mode due to a noisy pdf it is possible to specify a merge_ratio. In this case the samples are added until their values is below the hight of the peak times the merge ratio, then additional point are added until the pdf rise again.

Parameters
x_samplingThe horizontal sampling of the pdf to be analysed.
pdf_samplingThe sampling of the pdf to be analysed.
merge_ratioThe parameter for mode cutting.
nThe (maximum) number of modes to be returned.
Returns
A vector of ModeInfo containing the position and the area of the modes.

Definition at line 197 of file PdfModeExtraction.cpp.

References avgArea(), catchPeak(), findMaximumIndex(), flatternPeak(), getInterpolationAround(), std::make_pair(), and std::vector< T >::size().

Here is the call graph for this function:

◆ extractNHighestModes() [1/2]

std::vector< ModeInfo > Euclid::MathUtils::extractNHighestModes ( const XYDataset::XYDataset pdf,
double  merge_ratio,
size_t  n 
)

Extract the n highest modes in the provided pdf and compute for each of them the location of the mode and its area. A mode is discovered as the highest point of the pdf, then, on both sides, samples are added until the pdf starts to rise again. In order to avoid truncating the mode due to a noisy pdf it is possible to specify a merge_ratio. In this case the samples are added until their values is below the hight of the peak times the merge ratio, then additional point are added until the pdf rise again.

Parameters
pdfThe sampling of the pdf to be analysed.
merge_ratioThe parameter for mode cutting.
nThe (maximum) number of modes to be returned.
Returns
A vector of ModeInfo containing the position and the area of the modes.

Definition at line 192 of file PdfModeExtraction.cpp.

References getXYs().

Here is the call graph for this function:

◆ extractNHighestModes() [2/2]

std::vector< ModeInfo > Euclid::MathUtils::extractNHighestModes ( std::vector< double > &  x_sampling,
std::vector< double > &  pdf_sampling,
double  merge_ratio,
size_t  n 
)

Extract the n highest modes in the provided pdf and compute for each of them the location of the mode and its area. A mode is discovered as the highest point of the pdf, then, on both sides, samples are added until the pdf starts to rise again. In order to avoid truncating the mode due to a noisy pdf it is possible to specify a merge_ratio. In this case the samples are added until their values is below the hight of the peak times the merge ratio, then additional point are added until the pdf rise again.

Parameters
x_samplingThe horizontal sampling of the pdf to be analysed.
pdf_samplingThe sampling of the pdf to be analysed.
merge_ratioThe parameter for mode cutting.
nThe (maximum) number of modes to be returned.
Returns
A vector of ModeInfo containing the position and the area of the modes.

Definition at line 175 of file PdfModeExtraction.cpp.

References avgArea(), catchPeak(), findMaximumIndex(), flatternPeak(), getInterpolationAround(), and std::make_pair().

Here is the call graph for this function:

◆ findMaximumIndex()

size_t Euclid::MathUtils::findMaximumIndex ( const std::vector< double > &  pdf)

Definition at line 50 of file PdfModeExtraction.cpp.

References std::vector< T >::begin(), and std::vector< T >::end().

Referenced by extractNBigestModes(), and extractNHighestModes().

Here is the call graph for this function:

◆ flatternPeak()

std::pair<std::vector<double>, std::vector<double> > Euclid::MathUtils::flatternPeak ( const std::pair< std::vector< double >, std::vector< double >> &  pdf,
size_t  min_x,
size_t  max_x,
double  value 
)

Definition at line 166 of file PdfModeExtraction.cpp.

References std::make_pair().

Referenced by extractNBigestModes(), and extractNHighestModes().

Here is the call graph for this function:

◆ getInterpolationAround()

double Euclid::MathUtils::getInterpolationAround ( const std::pair< std::vector< double >, std::vector< double >> &  pdf,
size_t  x_index 
)

Definition at line 120 of file PdfModeExtraction.cpp.

Referenced by extractNBigestModes(), and extractNHighestModes().

◆ getXYs()

std::pair<std::vector<double>, std::vector<double> > Euclid::MathUtils::getXYs ( const XYDataset::XYDataset pdf)

Definition at line 36 of file PdfModeExtraction.cpp.

References Euclid::XYDataset::XYDataset::begin(), Euclid::XYDataset::XYDataset::end(), and std::make_pair().

Referenced by extractNBigestModes(), and extractNHighestModes().

Here is the call graph for this function:

◆ guess_h()

static double Euclid::MathUtils::guess_h ( double  initial_h,
double  x 
)
static

Definition at line 32 of file FiniteDifference.cpp.

References std::nextafter().

Referenced by derivative(), and derivative2nd().

Here is the call graph for this function:

◆ integrate()

double Euclid::MathUtils::integrate ( const Function function,
const double  min,
const double  max,
std::unique_ptr< NumericalIntegrationScheme numericalIntegrationScheme = nullptr 
)

Returns the integral of the given function inside the range [min,max]. This method will take advantage of Functions which also implement the Integrable interface. For other Function the integration will be delegated to the NumericalIntegrationScheme. Note that at the moment there is no default numerical implementation, so if the given function is not Integrable and no numerical integration scheme is provided an exception will be thrown.

Parameters
functionThe function to integrate
minThe minimum range of the integration
maxThe maximum range of the integration
numericalIntegrationSchemeThe class in charge of the numerical integration (if the function do not implement the Integrable interface).
Returns
The integral in the range [min,max]

Definition at line 33 of file function_tools.cpp.

References Euclid::MathUtils::Integrable::integrate().

Referenced by Euclid::PhysicsUtils::CosmologicalDistances::comovingDistance(), and Euclid::MathUtils::Piecewise::integrate().

Here is the call graph for this function:

◆ interpolate() [1/2]

std::unique_ptr< Function > Euclid::MathUtils::interpolate ( const Euclid::XYDataset::XYDataset dataset,
InterpolationType  type,
bool  extrapolate = false 
)

Returns a Function which performs interpolation for the data points of the given dataset.

Parameters
datasetThe dataset containing the data points
typeThe type of the interpolation to perform
Returns
A function representing the interpolation
Exceptions
InterpolationExceptionif there are decreasing x values
InterpolationExceptionif there are (X,Y) pairs with same X value but different Y value (step functions)

Definition at line 91 of file interpolation.cpp.

References std::vector< T >::emplace_back(), interpolate(), std::vector< T >::reserve(), and Euclid::XYDataset::XYDataset::size().

Here is the call graph for this function:

◆ interpolate() [2/2]

std::unique_ptr< Function > Euclid::MathUtils::interpolate ( const std::vector< double > &  x,
const std::vector< double > &  y,
InterpolationType  type,
bool  extrapolate = false 
)

Returns a Function which performs interpolation for the given set of data points. Duplicate (x,y) pairs are ignored and treated as a single one.

Parameters
xThe x values of the data points
yThe y values of the data points
typeThe type of the interpolation to perform
Returns
A function representing the interpolation
Exceptions
InterpolationExceptionif the x and y vectors do not have the same size
InterpolationExceptionif there are decreasing x values
InterpolationExceptionif there are (X,Y) pairs with same X value but different Y value (step functions)

Definition at line 40 of file interpolation.cpp.

References CUBIC_SPLINE, std::vector< T >::emplace_back(), std::vector< T >::front(), LINEAR, linearInterpolation(), Euclid::Configuration::logger, std::vector< T >::reserve(), std::vector< T >::size(), splineInterpolation(), and Elements::Logging::warn().

Referenced by interpolate().

Here is the call graph for this function:

◆ linearInterpolation()

std::unique_ptr< Function > Euclid::MathUtils::linearInterpolation ( const std::vector< double > &  x,
const std::vector< double > &  y,
bool  extrapolate 
)

Performs linear interpolation for the given set of data points.

Definition at line 34 of file linear.cpp.

References std::vector< T >::back(), std::vector< T >::front(), std::numeric_limits::lowest(), std::numeric_limits::max(), std::move(), std::vector< T >::push_back(), and std::vector< T >::size().

Referenced by interpolate().

Here is the call graph for this function:

◆ multiply()

std::unique_ptr< Function > Euclid::MathUtils::multiply ( const Function f1,
const Function f2 
)

Returns a function which represents the multiplication of the two parameters. This method makes use of the multiplySpecificSpecificMap and multiplySpecificGenericMap maps for detecting efficient ways of multiplying the parameters.

Parameters
f1The first function to multiply
f2The second function to multiply
Returns
A function representing the multiplication of the parameters

Definition at line 63 of file function_tools.cpp.

References multiplySpecificGenericMap, and multiplySpecificSpecificMap.

Referenced by multiplyPiecewises(), and multiplyPiecewiseWithGeneric().

◆ multiplyPiecewises()

std::unique_ptr<Function> Euclid::MathUtils::multiplyPiecewises ( const Function f1,
const Function f2 
)

Definition at line 92 of file multiplication.cpp.

References Euclid::MathUtils::Piecewise::getFunctions(), Euclid::MathUtils::Piecewise::getKnots(), multiply(), and overlappingKnots().

Here is the call graph for this function:

◆ multiplyPiecewiseWithGeneric()

std::unique_ptr<Function> Euclid::MathUtils::multiplyPiecewiseWithGeneric ( const Function f1,
const Function f2 
)

Function for multiplying a Piecewise with any other type. It multiplies each sub-function of the Piecewise with the other function.

Definition at line 52 of file multiplication.cpp.

References Euclid::MathUtils::Piecewise::getFunctions(), Euclid::MathUtils::Piecewise::getKnots(), multiply(), and std::vector< T >::push_back().

Here is the call graph for this function:

◆ multiplyPolynomials()

std::unique_ptr<Function> Euclid::MathUtils::multiplyPolynomials ( const Function f1,
const Function f2 
)

Function for multiplying two Polynomials. It multiplies their coefficients.

Definition at line 38 of file multiplication.cpp.

References Euclid::MathUtils::Polynomial::getCoefficients(), and std::vector< T >::size().

Here is the call graph for this function:

◆ overlappingKnots()

std::vector<double> Euclid::MathUtils::overlappingKnots ( const std::vector< double > &  knots1,
const std::vector< double > &  knots2 
)

Returns a vector of the overlapping knots from the given vectors.

Definition at line 62 of file multiplication.cpp.

References std::vector< T >::begin(), and std::vector< T >::end().

Referenced by multiplyPiecewises().

Here is the call graph for this function:

◆ splineInterpolation()

std::unique_ptr< Function > Euclid::MathUtils::splineInterpolation ( const std::vector< double > &  x,
const std::vector< double > &  y,
bool  extrapolate 
)

Performs cubic spline interpolation for the given set of data points.

Definition at line 34 of file spline.cpp.

References std::vector< T >::back(), std::vector< T >::emplace_back(), std::vector< T >::front(), g, std::numeric_limits::lowest(), std::numeric_limits::max(), std::move(), std::vector< T >::reserve(), and std::vector< T >::size().

Referenced by interpolate().

Here is the call graph for this function:

Variable Documentation

◆ multiplySpecificGenericMap

std::map< std::type_index, MultiplyFunction > Euclid::MathUtils::multiplySpecificGenericMap {{typeid(Piecewise), multiplyPiecewiseWithGeneric}}

A map for retrieving specific function multiplication implementations. The keys of the map are the type of a Function which can be multiplied with any other function and the value of the map is the function which can be used for performing this multiplication in an efficient way.

Definition at line 127 of file multiplication.cpp.

Referenced by multiply().

◆ multiplySpecificSpecificMap

std::map< std::pair< std::type_index, std::type_index >, MultiplyFunction > Euclid::MathUtils::multiplySpecificSpecificMap
Initial value:
{
{std::pair<std::type_index, std::type_index>(typeid(Polynomial), typeid(Polynomial)), multiplyPolynomials},
{std::pair<std::type_index, std::type_index>(typeid(Piecewise), typeid(Piecewise)), multiplyPiecewises}}
std::unique_ptr< Function > multiplyPiecewises(const Function &f1, const Function &f2)
std::unique_ptr< Function > multiplyPolynomials(const Function &f1, const Function &f2)
Function for multiplying two Polynomials. It multiplies their coefficients.

A map for retrieving specific function multiplication implementations. The keys of the map are the pairs of the Function types and the value of the map is the function which can be used for performing this multiplication in an efficient way.

Definition at line 123 of file multiplication.cpp.

Referenced by multiply().