Geant4-11
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Private Attributes
G4INCL::InvFInterpolationTable Class Reference

Class for interpolating the inverse of a 1-dimensional function. More...

#include <G4INCLInvFInterpolationTable.hh>

Inheritance diagram for G4INCL::InvFInterpolationTable:
G4INCL::InterpolationTable G4INCL::IFunction1D

Public Types

typedef G4double(*const ManipulatorFunc) (const G4double)
 Typedef to simplify the syntax of inverseCDFTable. More...
 

Public Member Functions

std::vector< G4doublegetNodeAbscissae () const
 
std::vector< G4doublegetNodeValues () const
 
unsigned int getNumberOfNodes () const
 
virtual G4double getXMaximum () const
 Return the maximum allowed value of the independent variable. More...
 
virtual G4double getXMinimum () const
 Return the minimum allowed value of the independent variable. More...
 
virtual G4double integrate (const G4double x0, const G4double x1, const G4double step=-1.) const
 Integrate the function between two values. More...
 
InterpolationTableinverseCDFTable (ManipulatorFunc fWrap=0, const G4int nNodes=60) const
 Return a pointer to the inverse of the CDF of this function. More...
 
 InvFInterpolationTable (IFunction1D const &f, const unsigned int nNodes=30)
 
G4double operator() (const G4double x) const
 Compute the value of the function. More...
 
IFunction1Dprimitive () const
 Return a pointer to the (numerical) primitive to this function. More...
 
std::string print () const
 
virtual ~InvFInterpolationTable ()
 

Protected Member Functions

void initDerivatives ()
 Initialise the values of the node derivatives. More...
 

Protected Attributes

std::vector< InterpolationNodenodes
 Interpolating nodes. More...
 
G4double xMax
 Maximum value of the independent variable. More...
 
G4double xMin
 Minimum value of the independent variable. More...
 

Static Private Attributes

static const G4double integrationCoefficients []
 Coefficients for numerical integration. More...
 

Detailed Description

Class for interpolating the inverse of a 1-dimensional function.

Definition at line 56 of file G4INCLInvFInterpolationTable.hh.

Member Typedef Documentation

◆ ManipulatorFunc

typedef G4double(*const G4INCL::IFunction1D::ManipulatorFunc) (const G4double)
inherited

Typedef to simplify the syntax of inverseCDFTable.

Definition at line 93 of file G4INCLIFunction1D.hh.

Constructor & Destructor Documentation

◆ InvFInterpolationTable()

G4INCL::InvFInterpolationTable::InvFInterpolationTable ( IFunction1D const &  f,
const unsigned int  nNodes = 30 
)

Definition at line 52 of file G4INCLInvFInterpolationTable.cc.

52 {
53// assert(nNodes>2);
54
55 const G4double x0 = f.getXMinimum();
56 const G4double x1 = f.getXMaximum();
57
58 // Build the nodes
59 G4double last = f(x0);
60 InterpolationNode firstNode(last, x0, 0.);
61 nodes.push_back(firstNode);
62 G4int skippedNodes = 0;
63 for(unsigned i = 1; i < nNodes; i++) {
64 const G4double xi = x0 + i*(x1-x0)/((G4double)(nNodes-1));
65 // Make sure that the x vector is sorted (corresponding to a monotonous
66 // function)
67 const G4double value = f(xi);
68 if(value <= last) {
69 ++skippedNodes;
70 continue;
71 }
72 InterpolationNode node(value, xi, 0.);
73 nodes.push_back(node);
74 last = value;
75 }
76
77// assert(nNodes==nodes.size()+skippedNodes);
78
79 // Initialise the "derivative" values
81 }
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
std::vector< InterpolationNode > nodes
Interpolating nodes.
void initDerivatives()
Initialise the values of the node derivatives.

References G4INCL::IFunction1D::getXMaximum(), G4INCL::IFunction1D::getXMinimum(), G4INCL::InterpolationTable::initDerivatives(), and G4INCL::InterpolationTable::nodes.

◆ ~InvFInterpolationTable()

virtual G4INCL::InvFInterpolationTable::~InvFInterpolationTable ( )
inlinevirtual

Definition at line 59 of file G4INCLInvFInterpolationTable.hh.

59{}

Member Function Documentation

◆ getNodeAbscissae()

std::vector< G4double > G4INCL::InterpolationTable::getNodeAbscissae ( ) const
inherited

Definition at line 67 of file G4INCLInterpolationTable.cc.

67 {
68 std::vector<G4double> x(nodes.size());
69 std::transform(nodes.begin(), nodes.end(), x.begin(),
70 std::mem_fn(&InterpolationNode::getX));
71 return x;
72 }
G4bool transform(G4String &input, const G4String &type)

References G4INCL::InterpolationNode::getX(), G4INCL::InterpolationTable::nodes, and G4coutFormatters::anonymous_namespace{G4coutFormatters.cc}::transform().

◆ getNodeValues()

std::vector< G4double > G4INCL::InterpolationTable::getNodeValues ( ) const
inherited

Definition at line 74 of file G4INCLInterpolationTable.cc.

74 {
75 std::vector<G4double> y(nodes.size());
76 std::transform(nodes.begin(), nodes.end(), y.begin(),
77 std::mem_fn(&InterpolationNode::getY));
78 return y;
79 }

References G4INCL::InterpolationNode::getY(), G4INCL::InterpolationTable::nodes, and G4coutFormatters::anonymous_namespace{G4coutFormatters.cc}::transform().

◆ getNumberOfNodes()

unsigned int G4INCL::InterpolationTable::getNumberOfNodes ( ) const
inlineinherited

Definition at line 111 of file G4INCLInterpolationTable.hh.

111{ return nodes.size(); }

References G4INCL::InterpolationTable::nodes.

◆ getXMaximum()

virtual G4double G4INCL::IFunction1D::getXMaximum ( ) const
inlinevirtualinherited

Return the maximum allowed value of the independent variable.

Definition at line 75 of file G4INCLIFunction1D.hh.

75{ return xMax; }
G4double xMax
Maximum value of the independent variable.

References G4INCL::IFunction1D::xMax.

Referenced by G4INCL::IFunction1D::inverseCDFTable(), InvFInterpolationTable(), and G4INCL::IFunction1D::primitive().

◆ getXMinimum()

virtual G4double G4INCL::IFunction1D::getXMinimum ( ) const
inlinevirtualinherited

Return the minimum allowed value of the independent variable.

Definition at line 72 of file G4INCLIFunction1D.hh.

72{ return xMin; }
G4double xMin
Minimum value of the independent variable.

References G4INCL::IFunction1D::xMin.

Referenced by G4INCL::IFunction1D::inverseCDFTable(), InvFInterpolationTable(), and G4INCL::IFunction1D::primitive().

◆ initDerivatives()

void G4INCL::InterpolationTable::initDerivatives ( )
protectedinherited

Initialise the values of the node derivatives.

Definition at line 81 of file G4INCLInterpolationTable.cc.

81 {
82 for(unsigned i = 0; i < nodes.size()-1; i++) {
83 if((nodes.at(i+1).getX() - nodes.at(i).getX()) == 0.0) // Safeguard against division by zero
84 nodes[i].setYPrime(0.0);
85 else
86 nodes[i].setYPrime((nodes.at(i+1).getY() - nodes.at(i).getY())/(nodes.at(i+1).getX() - nodes.at(i).getX()));
87 }
88 nodes.back().setYPrime(nodes.at(nodes.size()-2).getYPrime()); // Duplicate the last value
89 }

References G4INCL::InterpolationTable::nodes.

Referenced by G4INCL::InterpolationTable::InterpolationTable(), and InvFInterpolationTable().

◆ integrate()

G4double G4INCL::IFunction1D::integrate ( const G4double  x0,
const G4double  x1,
const G4double  step = -1. 
) const
virtualinherited

Integrate the function between two values.

Parameters
x0lower integration bound
x1upper integration bound
steplargest integration step size; if <0, 45 steps will be used
Returns
$\int_{x_0}^{x_1} f(x) dx$

Definition at line 66 of file G4INCLIFunction1D.cc.

66 {
67 G4double xi = std::max(x0, xMin);
68 G4double xa = std::min(x1, xMax);
70
71 if(x1 <= x0) {
72 sign = -1.0;
73 std::swap(xi, xa);
74 } else
75 sign = 1.0;
76
77 const G4double interval = xa - xi;
78
79 G4int nIntervals;
80 if(step<0.) {
81 nIntervals = 45;
82 } else {
83 nIntervals = G4int(interval/step);
84
85 // Round up nIntervals to the closest multiple of 9
86 G4int remainder = nIntervals % 9;
87 if (remainder != 0)
88 nIntervals += 9 - remainder;
89
90 nIntervals = std::max(nIntervals, 9);
91 }
92
93 const G4double dx = interval/nIntervals;
94 G4double result = (operator()(xi) + operator()(xa)) * integrationCoefficients[0]/2;
95 for(G4int j = 1; j<nIntervals; ++j) {
96 const G4double x = xi + interval*G4double(j)/G4double(nIntervals);
97 const unsigned index = j%9;
98 result += operator()(x) * integrationCoefficients[index];
99 }
100
101 return result*dx*sign;
102
103 }
virtual G4double operator()(const G4double x) const =0
Compute the value of the function.
static const G4double integrationCoefficients[]
Coefficients for numerical integration.
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
G4int sign(const T t)

References G4INCL::IFunction1D::integrationCoefficients, G4INCL::Math::max(), G4INCL::Math::min(), G4INCL::IFunction1D::operator()(), G4INCL::Math::sign(), G4INCL::IFunction1D::xMax, and G4INCL::IFunction1D::xMin.

◆ inverseCDFTable()

InterpolationTable * G4INCL::IFunction1D::inverseCDFTable ( IFunction1D::ManipulatorFunc  fWrap = 0,
const G4int  nNodes = 60 
) const
inherited

Return a pointer to the inverse of the CDF of this function.

The function parameter fWrap is wrapped around the return value of operator(). If fWrap=NULL (default), fWrap=identity.

Definition at line 123 of file G4INCLIFunction1D.cc.

123 {
124 class InverseCDF : public IFunction1D {
125 public:
126 InverseCDF(IFunction1D const * const f, ManipulatorFunc fw) :
128 theFunction(f),
129 normalisation(1./theFunction->integrate(xMin,xMax)),
130 fWrap(fw)
131 {}
132
133 G4double operator()(const G4double x) const {
134 if(fWrap)
135 return fWrap(std::min(1., normalisation * theFunction->integrate(xMin,x)));
136 else
137 return std::min(1., normalisation * theFunction->integrate(xMin,x));
138 }
139 private:
140 IFunction1D const * const theFunction;
141 const G4double normalisation;
142 ManipulatorFunc fWrap;
143 } *theInverseCDF = new InverseCDF(this, fWrap);
144
145 InterpolationTable *theTable = new InvFInterpolationTable(*theInverseCDF, nNodes);
146 delete theInverseCDF;
147 return theTable;
148 }
virtual G4double getXMaximum() const
Return the maximum allowed value of the independent variable.
G4double(*const ManipulatorFunc)(const G4double)
Typedef to simplify the syntax of inverseCDFTable.
virtual G4double integrate(const G4double x0, const G4double x1, const G4double step=-1.) const
Integrate the function between two values.
virtual G4double getXMinimum() const
Return the minimum allowed value of the independent variable.

References G4INCL::IFunction1D::getXMaximum(), G4INCL::IFunction1D::getXMinimum(), G4INCL::IFunction1D::IFunction1D(), G4INCL::Math::min(), G4INCL::IFunction1D::operator()(), G4INCL::IFunction1D::xMax, and G4INCL::IFunction1D::xMin.

Referenced by G4INCL::NuclearDensityFactory::createPCDFTable(), G4INCL::NuclearDensityFactory::createRCDFTable(), and G4INCL::NuclearDensityFactory::createRPCorrelationTable().

◆ operator()()

G4double G4INCL::InterpolationTable::operator() ( const G4double  x) const
virtualinherited

Compute the value of the function.

Implements G4INCL::IFunction1D.

Definition at line 91 of file G4INCLInterpolationTable.cc.

91 {
92 // Find the relevant interpolation bin
93 InterpolationNode xNode(x,0.,0.);
94 std::vector<InterpolationNode>::const_iterator iter =
95 std::lower_bound(nodes.begin(), nodes.end(), xNode);
96
97 if(iter==nodes.begin())
98 return nodes.front().getY();
99
100 if(iter==nodes.end())
101 return nodes.back().getY();
102
103 std::vector<InterpolationNode>::const_iterator previousIter = iter - 1;
104 const G4double dx = x - previousIter->getX();
105 return previousIter->getY() + previousIter->getYPrime()*dx;
106 }

References G4INCL::InterpolationTable::nodes.

◆ primitive()

IFunction1D * G4INCL::IFunction1D::primitive ( ) const
inherited

Return a pointer to the (numerical) primitive to this function.

Definition at line 105 of file G4INCLIFunction1D.cc.

105 {
106 class Primitive : public IFunction1D {
107 public:
108 Primitive(IFunction1D const * const f) :
110 theFunction(f)
111 {}
112
113 G4double operator()(const G4double x) const {
114 return theFunction->integrate(xMin,x);
115 }
116 private:
117 IFunction1D const * const theFunction;
118 } *thePrimitive = new Primitive(this);
119
120 return thePrimitive;
121 }

References G4INCL::IFunction1D::getXMaximum(), G4INCL::IFunction1D::getXMinimum(), G4INCL::IFunction1D::IFunction1D(), G4INCL::IFunction1D::operator()(), Primitive, and G4INCL::IFunction1D::xMin.

◆ print()

std::string G4INCL::InterpolationTable::print ( ) const
inherited

Definition at line 108 of file G4INCLInterpolationTable.cc.

108 {
109 std::string message;
110 for(std::vector<InterpolationNode>::const_iterator n=nodes.begin(), e=nodes.end(); n!=e; ++n)
111 message += n->print();
112 return message;
113 }

References CLHEP::detail::n, and G4INCL::InterpolationTable::nodes.

Referenced by G4INCL::NuclearDensityFactory::createPCDFTable(), G4INCL::NuclearDensityFactory::createRCDFTable(), and G4INCL::NuclearDensityFactory::createRPCorrelationTable().

Field Documentation

◆ integrationCoefficients

const G4double G4INCL::IFunction1D::integrationCoefficients
staticprivateinherited
Initial value:
= {
2.*95.0/288.0,
317.0/240.0,
23.0/30.0,
793.0/720.0,
157.0/160.0,
157.0/160.0,
793.0/720.0,
23.0/30.0,
317.0/240.0,
}

Coefficients for numerical integration.

Definition at line 110 of file G4INCLIFunction1D.hh.

Referenced by G4INCL::IFunction1D::integrate().

◆ nodes

std::vector<InterpolationNode> G4INCL::InterpolationTable::nodes
protectedinherited

◆ xMax

G4double G4INCL::IFunction1D::xMax
protectedinherited

Maximum value of the independent variable.

Definition at line 106 of file G4INCLIFunction1D.hh.

Referenced by G4INCL::IFunction1D::getXMaximum(), G4INCL::IFunction1D::integrate(), and G4INCL::IFunction1D::inverseCDFTable().

◆ xMin

G4double G4INCL::IFunction1D::xMin
protectedinherited

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