Geant4-11
G4SimpleIntegration.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4SimpleIntegration class implementation
27//
28// Author: V.Grichine, 26.03.1997
29// --------------------------------------------------------------------
30
32#include "globals.hh"
33
35 : fFunction(pFunction)
36{}
37
39 G4double pTolerance)
40 : fFunction(pFunction)
41 , fTolerance(pTolerance)
42{}
43
45
46// Simple integration methods
47
49 G4int iterationNumber)
50{
51 G4double Step = (xFinal - xInitial) / iterationNumber;
52 G4double mean = (fFunction(xInitial) + fFunction(xFinal)) * 0.5;
53 G4double x = xInitial;
54 for(G4int i = 1; i < iterationNumber; ++i)
55 {
56 x += Step;
57 mean += fFunction(x);
58 }
59 return mean * Step;
60}
61
63 G4int iterationNumber)
64{
65 G4double Step = (xFinal - xInitial) / iterationNumber;
66 G4double x = xInitial + 0.5 * Step;
67 G4double mean = fFunction(x);
68 for(G4int i = 1; i < iterationNumber; ++i)
69 {
70 x += Step;
71 mean += fFunction(x);
72 }
73 return mean * Step;
74}
75
77 G4int iterationNumber)
78{
79 G4double x = 0.;
80 static const G4double root = 1.0 / std::sqrt(3.0);
81 G4double Step = (xFinal - xInitial) / (2.0 * iterationNumber);
82 G4double delta = Step * root;
83 G4double mean = 0.0;
84 for(G4int i = 0; i < iterationNumber; ++i)
85 {
86 x = (2 * i + 1) * Step;
87 mean += (fFunction(x + delta) + fFunction(x - delta));
88 }
89 return mean * Step;
90}
91
93 G4int iterationNumber)
94{
95 G4double Step = (xFinal - xInitial) / iterationNumber;
96 G4double x = xInitial;
97 G4double xPlus = xInitial + 0.5 * Step;
98 G4double mean = (fFunction(xInitial) + fFunction(xFinal)) * 0.5;
99 G4double sum = fFunction(xPlus);
100 for(G4int i = 1; i < iterationNumber; ++i)
101 {
102 x += Step;
103 xPlus += Step;
104 mean += fFunction(x);
105 sum += fFunction(xPlus);
106 }
107 mean += 2.0 * sum;
108 return mean * Step / 3.0;
109}
110
111// Adaptive Gauss integration
112
114 G4double xFinal)
115{
116 G4int depth = 0;
117 G4double sum = 0.0;
118 AdaptGauss(xInitial, xFinal, sum, depth);
119 return sum;
120}
121
123{
124 static const G4double root = 1.0 / std::sqrt(3.0);
125
126 G4double xMean = (xInitial + xFinal) / 2.0;
127 G4double Step = (xFinal - xInitial) / 2.0;
128 G4double delta = Step * root;
129 G4double sum = (fFunction(xMean + delta) + fFunction(xMean - delta));
130
131 return sum * Step;
132}
133
135 G4double& sum, G4int& depth)
136{
137 if(depth > fMaxDepth)
138 {
139 G4Exception("G4SimpleIntegration::AdaptGauss()", "Error", FatalException,
140 "Function varies too rapidly !");
141 }
142 G4double xMean = (xInitial + xFinal) / 2.0;
143 G4double leftHalf = Gauss(xInitial, xMean);
144 G4double rightHalf = Gauss(xMean, xFinal);
145 G4double full = Gauss(xInitial, xFinal);
146 if(std::fabs(leftHalf + rightHalf - full) < fTolerance)
147 {
148 sum += full;
149 }
150 else
151 {
152 ++depth;
153 AdaptGauss(xInitial, xMean, sum, depth);
154 AdaptGauss(xMean, xFinal, sum, depth);
155 }
156}
G4double(* function)(G4double)
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
G4double AdaptGaussIntegration(G4double xInitial, G4double xFinal)
G4double Gauss(G4double xInitial, G4double xFinal, G4int iterationNumber)
G4SimpleIntegration(function pFunction)
G4double Trapezoidal(G4double xInitial, G4double xFinal, G4int iterationNumber)
G4double Simpson(G4double xInitial, G4double xFinal, G4int iterationNumber)
void AdaptGauss(G4double xInitial, G4double xFinal, G4double &sum, G4int &depth)
G4double MidPoint(G4double xInitial, G4double xFinal, G4int iterationNumber)