35#define INCLXX_IN_GEANT4_MODE 1
56 std::pair<G4double,G4double> RootFinder::solution;
58 const G4double RootFinder::toleranceY = 1.e-4;
63 if( std::abs(y0) < toleranceY ) {
64 solution = std::make_pair(x0,y0);
69 std::pair<G4double,G4double> bracket = bracketRoot(f,x0);
76 if(std::abs(y_at_zero)<=toleranceY) {
78 solution = std::make_pair(0.,y_at_zero);
81 WARN(
"Root-finding algorithm could not bracket the root." << std::endl);
97 G4int lastUpdated = 0;
99 for(
G4int iterations=0; std::abs(y) > toleranceY; iterations++) {
101 if(iterations > maxIterations) {
102 WARN(
"Root-finding algorithm did not converge." << std::endl);
108 x = (y1*x2-y2*x1)/(y1-y2);
117 if(lastUpdated==-1) y2 *= 0.5;
122 if(lastUpdated==1) y1 *= 0.5;
131 solution = std::make_pair(x,y);
136 std::pair<G4double,G4double> RootFinder::bracketRoot(
RootFunctor const *
const f,
G4double x0) {
149 return std::make_pair(x0,x1);
151 const G4double scaleFactorMinus1 = 1./scaleFactor;
155 if(iterations > maxIterations) {
156 DEBUG(
"Could not bracket the root." << std::endl);
164 x0 *= scaleFactorMinus1;
172 return std::make_pair(x0,oldx0);
174 return std::make_pair(oldx1,x1);
Static root-finder algorithm.
static G4bool solve(RootFunctor const *const f, const G4double x0)
Numerically solve a one-dimensional equation.
virtual void cleanUp(const G4bool success) const =0