Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
interpolating_function_p< float_type > Class Template Reference

create a cubic spline interpolation of a set of (x,y) pairsThis is one of the main reasons for c2_function objects to exist. More...

#include <c2_function.hh>

Inheritance diagram for interpolating_function_p< float_type >:
Collaboration diagram for interpolating_function_p< float_type >:

Public Member Functions

 interpolating_function_p ()
 an empty linear-linear cubic-spline interpolating_function_p More...
 
 interpolating_function_p (const c2_function_transformation< float_type > &transform)
 an empty cubic-spline interpolating_function_p with a specific transform More...
 
interpolating_function_p
< float_type > & 
load (const std::vector< float_type > &x, const std::vector< float_type > &f, bool lowerSlopeNatural, float_type lowerSlope, bool upperSlopeNatural, float_type upperSlope, bool splined=true)
 do the dirty work of constructing the spline from a function. More...
 
interpolating_function_p
< float_type > & 
load_pairs (std::vector< std::pair< float_type, float_type > > &data, bool lowerSlopeNatural, float_type lowerSlope, bool upperSlopeNatural, float_type upperSlope, bool splined=true)
 do the dirty work of constructing the spline from a function. More...
 
interpolating_function_p
< float_type > & 
sample_function (const c2_function< float_type > &func, float_type amin, float_type amax, float_type abs_tol, float_type rel_tol, bool lowerSlopeNatural, float_type lowerSlope, bool upperSlopeNatural, float_type upperSlope)
 do the dirty work of constructing the spline from a function. More...
 
interpolating_function_p
< float_type > & 
load_random_generator_function (const std::vector< float_type > &bincenters, const c2_function< float_type > &binheights)
 initialize from a grid of points and a c2_function (un-normalized) to an interpolator which, when evaluated with a uniform random variate on [0,1] returns random numbers distributed as the input function. More...
 
interpolating_function_p
< float_type > & 
load_random_generator_bins (const std::vector< float_type > &bins, const std::vector< float_type > &binheights, bool splined=true)
 
virtual float_type value_with_derivatives (float_type x, float_type *yprime, float_type *yprime2) const
 get the value and derivatives. More...
 
virtual ~interpolating_function_p ()
 destructor More...
 
virtual
interpolating_function_p
< float_type > & 
clone () const
 
void get_data (std::vector< float_type > &xvals, std::vector< float_type > &yvals) const
 
void get_internal_data (std::vector< float_type > &xvals, std::vector< float_type > &yvals, std::vector< float_type > &y2vals) const
 
void set_lower_extrapolation (float_type bound)
 
void set_upper_extrapolation (float_type bound)
 
interpolating_function_p
< float_type > & 
unary_operator (const c2_function< float_type > &source) const
 
interpolating_function_p
< float_type > & 
binary_operator (const c2_function< float_type > &rhs, const c2_binary_function< float_type > *combining_stub) const
 
interpolating_function_p
< float_type > & 
add_pointwise (const c2_function< float_type > &rhs) const
 
interpolating_function_p
< float_type > & 
subtract_pointwise (const c2_function< float_type > &rhs) const
 
interpolating_function_p
< float_type > & 
multiply_pointwise (const c2_function< float_type > &rhs) const
 
interpolating_function_p
< float_type > & 
divide_pointwise (const c2_function< float_type > &rhs) const
 
void clone_data (const interpolating_function_p< float_type > &rhs)
 
- Public Member Functions inherited from c2_function< float_type >
const std::string cvs_header_vers () const
 get versioning information for the header file More...
 
const std::string cvs_file_vers () const
 get versioning information for the source file More...
 
virtual ~c2_function ()
 destructor More...
 
float_type operator() (float_type x) const
 evaluate the function in the classic way, ignoring derivatives. More...
 
float_type operator() (float_type x, float_type *yprime, float_type *yprime2) const
 get the value and derivatives. More...
 
float_type find_root (float_type lower_bracket, float_type upper_bracket, float_type start, float_type value, int *error=0, float_type *final_yprime=0, float_type *final_yprime2=0) const
 solve f(x)==value very efficiently, with explicit knowledge of derivatives of the function More...
 
float_type partial_integrals (std::vector< float_type > xgrid, std::vector< float_type > *partials=0, float_type abs_tol=1e-12, float_type rel_tol=1e-12, int derivs=2, bool adapt=true, bool extrapolate=true) const
 
float_type integral (float_type amin, float_type amax, std::vector< float_type > *partials=0, float_type abs_tol=1e-12, float_type rel_tol=1e-12, int derivs=2, bool adapt=true, bool extrapolate=true) const
 a fully-automated integrator which uses the information provided by the get_sampling_grid() function to figure out what to do. More...
 
c2_piecewise_function_p
< float_type > * 
adaptively_sample (float_type amin, float_type amax, float_type abs_tol=1e-12, float_type rel_tol=1e-12, int derivs=2, std::vector< float_type > *xvals=0, std::vector< float_type > *yvals=0) const
 create a c2_piecewise_function_p from c2_connector_function_p segments which is a representation of the parent function to the specified accuracy, but maybe much cheaper to evaluate More...
 
float_type xmin () const
 
float_type xmax () const
 
void set_domain (float_type amin, float_type amax)
 
size_t get_evaluations () const
 
void reset_evaluations () const
 reset the counter More...
 
void increment_evaluations () const
 count evaluations More...
 
bool check_monotonicity (const std::vector< float_type > &data, const char message[]) const
 check that a vector is monotonic, throw an exception if not, and return a flag if it is reversed More...
 
virtual void set_sampling_grid (const std::vector< float_type > &grid)
 establish a grid of 'interesting' points on the function. More...
 
std::vector< float_type > * get_sampling_grid_pointer () const
 get the sampling grid, which may be a null pointer More...
 
virtual void get_sampling_grid (float_type amin, float_type amax, std::vector< float_type > &grid) const
 
void preen_sampling_grid (std::vector< float_type > *result) const
 The grid is modified in place. More...
 
void refine_sampling_grid (std::vector< float_type > &grid, size_t refinement) const
 
c2_function< float_type > & normalized_function (float_type amin, float_type amax, float_type norm=1.0) const
 create a new c2_function from this one which is normalized on the interval More...
 
c2_function< float_type > & square_normalized_function (float_type amin, float_type amax, float_type norm=1.0) const
 
c2_function< float_type > & square_normalized_function (float_type amin, float_type amax, const c2_function< float_type > &weight, float_type norm=1.0) const
 create a new c2_function from this one which is square-normalized with the provided weight on the interval More...
 
c2_sum_p< float_type > & operator+ (const c2_function< float_type > &rhs) const
 factory function to create a c2_sum_p from a regular algebraic expression. More...
 
c2_diff_p< float_type > & operator- (const c2_function< float_type > &rhs) const
 factory function to create a c2_diff_p from a regular algebraic expression. More...
 
c2_product_p< float_type > & operator* (const c2_function< float_type > &rhs) const
 factory function to create a c2_product_p from a regular algebraic expression. More...
 
c2_ratio_p< float_type > & operator/ (const c2_function< float_type > &rhs) const
 
c2_composed_function_p
< float_type > & 
operator() (const c2_function< float_type > &inner) const
 compose this function outside another. More...
 
float_type get_trouble_point () const
 Find out where a calculation ran into trouble, if it got a nan. If the most recent computation did not return a nan, this is undefined. More...
 
void claim_ownership () const
 increment our reference count. Destruction is only legal if the count is zero. More...
 
size_t release_ownership_for_return () const
 decrement our reference count. Do not destroy at zero. More...
 
void release_ownership () const
 
size_t count_owners () const
 get the reference count, mostly for debugging More...
 
void fill_fblock (c2_fblock< float_type > &fb) const
 fill in a c2_fblock<float_type>... a shortcut for the integrator & sampler More...
 

Public Attributes

const
c2_function_transformation
< float_type > & 
fTransform
 

Protected Member Functions

void spline (bool lowerSlopeNatural, float_type lowerSlope, bool upperSlopeNatural, float_type upperSlope)
 create the spline coefficients More...
 
- Protected Member Functions inherited from c2_function< float_type >
 c2_function (const c2_function< float_type > &src)
 
 c2_function ()
 
virtual void set_sampling_grid_pointer (std::vector< float_type > &grid)
 

Static Protected Member Functions

static bool comp_pair (std::pair< float_type, float_type > const &i, std::pair< float_type, float_type > const &j)
 

Protected Attributes

std::vector< float_type > Xraw
 
std::vector< float_type > X
 
std::vector< float_type > F
 
std::vector< float_type > y2
 
c2_const_ptr< float_type > sampler_function
 
bool xInverted
 
size_t lastKLow
 
- Protected Attributes inherited from c2_function< float_type >
std::vector< float_type > * sampling_grid
 
bool no_overwrite_grid
 
float_type fXMin
 
float_type fXMax
 
size_t evaluations
 
float_type bad_x_point
 this point may be used to record where a calculation ran into trouble More...
 

Detailed Description

template<typename float_type = double>
class interpolating_function_p< float_type >

create a cubic spline interpolation of a set of (x,y) pairs

This is one of the main reasons for c2_function objects to exist.

It provides support for cubic spline interpolation of data provides from tables of x, y pairs. It supports automatic, transparent linearization of the data before storing in its tables (through subclasses such as log_lin_interpolating_function, lin_log_interpolating_function, and log_log_interpolating_function) to permit very high accuracy representations of data which have a suitable structure. It provides utility functions LinearInterpolatingGrid() and LogLogInterpolatingGrid() to create grids for mapping other functions onto a arithmetic or geometric grid.

In its simplest form, an untransformed cubic spline of a data set, using natural boundary conditions (vanishing second derivative), is created as:

std::vector<double> xvals(10), yvals(10);
// < fill in xvals and yvals >
c2p myfunc=c2.interpolating_function().load(xvals, yvals,true,0,true,0);
// and it can be evaluated at a point for its value only by:
double y=myfunc(x);
// or it can be evaluated with its derivatives by
double yprime, yprime2;
double y=myfunc(x,&yprime, &yprime2);

The factory function c2_factory::interpolating_function() creates *new interpolating_function_p()

Definition at line 1513 of file c2_function.hh.

Constructor & Destructor Documentation

template<typename float_type = double>
interpolating_function_p< float_type >::interpolating_function_p ( )
inline

an empty linear-linear cubic-spline interpolating_function_p

Definition at line 1518 of file c2_function.hh.

a transformation of a function in and out of lin-lin space
const c2_function_transformation< float_type > & fTransform
the parent class for all c2_functions.c2_functions know their value, first, and second derivative at ...
Definition: c2_function.hh:147
template<typename float_type = double>
interpolating_function_p< float_type >::interpolating_function_p ( const c2_function_transformation< float_type > &  transform)
inline

an empty cubic-spline interpolating_function_p with a specific transform

Definition at line 1524 of file c2_function.hh.

1527  fTransform(transform) { }
const c2_function_transformation< float_type > & fTransform
the parent class for all c2_functions.c2_functions know their value, first, and second derivative at ...
Definition: c2_function.hh:147
template<typename float_type = double>
virtual interpolating_function_p< float_type >::~interpolating_function_p ( )
inlinevirtual

destructor

Definition at line 1661 of file c2_function.hh.

1661 { delete &fTransform; }
const c2_function_transformation< float_type > & fTransform

Member Function Documentation

template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::add_pointwise ( const c2_function< float_type > &  rhs) const
inline

Definition at line 1687 of file c2_function.hh.

1687  {
1688  return binary_operator(rhs, new c2_sum_p<float_type>()); }
interpolating_function_p< float_type > & binary_operator(const c2_function< float_type > &rhs, const c2_binary_function< float_type > *combining_stub) const

Here is the call graph for this function:

template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::binary_operator ( const c2_function< float_type > &  rhs,
const c2_binary_function< float_type > *  combining_stub 
) const

Here is the caller graph for this function:

template<typename float_type = double>
virtual interpolating_function_p<float_type>& interpolating_function_p< float_type >::clone ( ) const
inlinevirtual

Reimplemented in arrhenius_interpolating_function_p< float_type >, log_log_interpolating_function_p< float_type >, lin_log_interpolating_function_p< float_type >, and log_lin_interpolating_function_p< float_type >.

Definition at line 1663 of file c2_function.hh.

1665  { return *new interpolating_function_p<float_type>(); }
create a cubic spline interpolation of a set of (x,y) pairsThis is one of the main reasons for c2_fun...
template<typename float_type = double>
void interpolating_function_p< float_type >::clone_data ( const interpolating_function_p< float_type > &  rhs)
inline

Definition at line 1698 of file c2_function.hh.

1698  {
1699  Xraw=rhs.Xraw; X=rhs.X; F=rhs.F; y2=rhs.y2;
1701  }
std::vector< float_type > y2
std::vector< float_type > F
std::vector< float_type > X
std::vector< float_type > Xraw
virtual void set_sampling_grid_pointer(std::vector< float_type > &grid)
Definition: c2_function.hh:538

Here is the call graph for this function:

template<typename float_type = double>
static bool interpolating_function_p< float_type >::comp_pair ( std::pair< float_type, float_type > const &  i,
std::pair< float_type, float_type > const &  j 
)
inlinestaticprotected

Definition at line 1712 of file c2_function.hh.

1714  {return i.first<j.first;}
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::divide_pointwise ( const c2_function< float_type > &  rhs) const
inline

Definition at line 1696 of file c2_function.hh.

1696  {
1697  return binary_operator(rhs, new c2_ratio_p<float_type>()); }
create a c2_function which is the ratio of two other c2_functions.This should always be constructed u...
Definition: c2_function.hh:83
interpolating_function_p< float_type > & binary_operator(const c2_function< float_type > &rhs, const c2_binary_function< float_type > *combining_stub) const

Here is the call graph for this function:

template<typename float_type = double>
void interpolating_function_p< float_type >::get_data ( std::vector< float_type > &  xvals,
std::vector< float_type > &  yvals 
) const
template<typename float_type = double>
void interpolating_function_p< float_type >::get_internal_data ( std::vector< float_type > &  xvals,
std::vector< float_type > &  yvals,
std::vector< float_type > &  y2vals 
) const
inline

Definition at line 1670 of file c2_function.hh.

1674  { xvals=X; yvals=F; y2vals=y2; }
std::vector< float_type > y2
std::vector< float_type > F
std::vector< float_type > X
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::load ( const std::vector< float_type > &  x,
const std::vector< float_type > &  f,
bool  lowerSlopeNatural,
float_type  lowerSlope,
bool  upperSlopeNatural,
float_type  upperSlope,
bool  splined = true 
)

do the dirty work of constructing the spline from a function.

Parameters
xthe list of abscissas. Must be either strictly increasing or strictly decreasing. Strictly increasing is preferred, as less memory is used since a copy is not required for the sampling grid.
fthe list of function values.
lowerSlopeNaturalif true, set y''(first point)=0, otherwise compute it from lowerSope
lowerSlopederivative of the function at the lower bound, used only if lowerSlopeNatural is false
upperSlopeNaturalif true, set y''(last point)=0, otherwise compute it from upperSope
upperSlopederivative of the function at the upper bound, used only if upperSlopeNatural is false
splinedif true (default), use cubic spline, if false, use linear interpolation.
Returns
the same interpolating function, filled
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::load_pairs ( std::vector< std::pair< float_type, float_type > > &  data,
bool  lowerSlopeNatural,
float_type  lowerSlope,
bool  upperSlopeNatural,
float_type  upperSlope,
bool  splined = true 
)

do the dirty work of constructing the spline from a function.

Parameters
datastd::vector of std::pairs of x,y. Will be sorted into x increasing order in place.
lowerSlopeNaturalif true, set y''(first point)=0, otherwise compute it from lowerSope
lowerSlopederivative of the function at the lower bound, used only if lowerSlopeNatural is false
upperSlopeNaturalif true, set y''(last point)=0, otherwise compute it from upperSope
upperSlopederivative of the function at the upper bound, used only if upperSlopeNatural is false
splinedif true (default), use cubic spline, if false, use linear interpolation.
Returns
the same interpolating function, filled
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::load_random_generator_bins ( const std::vector< float_type > &  bins,
const std::vector< float_type > &  binheights,
bool  splined = true 
)
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::load_random_generator_function ( const std::vector< float_type > &  bincenters,
const c2_function< float_type > &  binheights 
)

initialize from a grid of points and a c2_function (un-normalized) to an interpolator which, when evaluated with a uniform random variate on [0,1] returns random numbers distributed as the input function.

See Also
Arbitrary random generation inverse_integrated_density starts derivatives a probability density std::vector, generates the integral, and generates an interpolating_function_p of the inverse function which, when evaluated using a uniform random on [0,1] returns values derivatives a density distribution equal to the input distribution If the data are passed in reverse order (large X first), the integral is carried out from the big end.
Parameters
bincentersthe positions at which to sample the function binheights
binheightsa function which describes the density of the random number distribution to be produced.
Returns
an initialized interpolator, which if evaluated randomly with a uniform variate on [0,1] produces numbers distributed according to binheights
template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::multiply_pointwise ( const c2_function< float_type > &  rhs) const
inline

Definition at line 1693 of file c2_function.hh.

1693  {
1694  return binary_operator(rhs, new c2_product_p<float_type>()); }
create a c2_function which is the product of two other c2_functions.This should always be constructed...
Definition: c2_function.hh:82
interpolating_function_p< float_type > & binary_operator(const c2_function< float_type > &rhs, const c2_binary_function< float_type > *combining_stub) const

Here is the call graph for this function:

template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::sample_function ( const c2_function< float_type > &  func,
float_type  amin,
float_type  amax,
float_type  abs_tol,
float_type  rel_tol,
bool  lowerSlopeNatural,
float_type  lowerSlope,
bool  upperSlopeNatural,
float_type  upperSlope 
)

do the dirty work of constructing the spline from a function.

Parameters
funca function without any requirement of valid derivatives to sample into an interpolating function. Very probably a c2_classic_function.
aminthe lower bound of the region to sample
amaxthe upper bound of the region to sample
abs_tolthe maximum absolute error permitted when linearly interpolating the points. the real error will be much smaller, since this uses cubic splines at the end.
rel_tolthe maximum relative error permitted when linearly interpolating the points. the real error will be much smaller, since this uses cubic splines at the end.
lowerSlopeNaturalif true, set y'(first point) from 3-point parabola, otherwise compute it from lowerSope
lowerSlopederivative of the function at the lower bound, used only if lowerSlopeNatural is false
upperSlopeNaturalif true, set y'(last point) from 3-point parabola, otherwise compute it from upperSope
upperSlopederivative of the function at the upper bound, used only if upperSlopeNatural is false
Returns
the same interpolating function, filled
Note
If the interpolator being filled has a log vertical axis, put the desired relative error in abs_tol, and 0 in rel_tol since the absolute error on the log of a function is the relative error on the function itself.
template<typename float_type = double>
void interpolating_function_p< float_type >::set_lower_extrapolation ( float_type  bound)
template<typename float_type = double>
void interpolating_function_p< float_type >::set_upper_extrapolation ( float_type  bound)
template<typename float_type = double>
void interpolating_function_p< float_type >::spline ( bool  lowerSlopeNatural,
float_type  lowerSlope,
bool  upperSlopeNatural,
float_type  upperSlope 
)
protected

create the spline coefficients

template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::subtract_pointwise ( const c2_function< float_type > &  rhs) const
inline

Definition at line 1690 of file c2_function.hh.

1690  {
1691  return binary_operator(rhs, new c2_diff_p<float_type>()); }
interpolating_function_p< float_type > & binary_operator(const c2_function< float_type > &rhs, const c2_binary_function< float_type > *combining_stub) const

Here is the call graph for this function:

template<typename float_type = double>
interpolating_function_p<float_type>& interpolating_function_p< float_type >::unary_operator ( const c2_function< float_type > &  source) const
template<typename float_type = double>
virtual float_type interpolating_function_p< float_type >::value_with_derivatives ( float_type  x,
float_type *  yprime,
float_type *  yprime2 
) const
virtual

get the value and derivatives.

There is required checking for null pointers on the derivatives, and most implementations should operate faster if derivatives are not

Parameters
[in]xthe point at which to evaluate the function
[out]yprimethe first derivative (if pointer is non-null)
[out]yprime2the second derivative (if pointer is non-null)
Returns
the value of the function

Implements c2_function< float_type >.

Member Data Documentation

template<typename float_type = double>
std::vector<float_type> interpolating_function_p< float_type >::F
protected

Definition at line 1716 of file c2_function.hh.

template<typename float_type = double>
const c2_function_transformation<float_type>& interpolating_function_p< float_type >::fTransform

Definition at line 1703 of file c2_function.hh.

template<typename float_type = double>
size_t interpolating_function_p< float_type >::lastKLow
mutableprotected

Definition at line 1719 of file c2_function.hh.

template<typename float_type = double>
c2_const_ptr<float_type> interpolating_function_p< float_type >::sampler_function
protected

Definition at line 1717 of file c2_function.hh.

template<typename float_type = double>
std::vector<float_type> interpolating_function_p< float_type >::X
protected

Definition at line 1716 of file c2_function.hh.

template<typename float_type = double>
bool interpolating_function_p< float_type >::xInverted
protected

Definition at line 1718 of file c2_function.hh.

template<typename float_type = double>
std::vector<float_type> interpolating_function_p< float_type >::Xraw
protected

Definition at line 1716 of file c2_function.hh.

template<typename float_type = double>
std::vector<float_type> interpolating_function_p< float_type >::y2
protected

Definition at line 1716 of file c2_function.hh.


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