1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
// File: math_KronrodSingleIntegration.cxx
// Created: Thu Dec 8 15:08:52 2005
// Author: Sergey KHROMOV
// <skv@dimox>
#include <math_KronrodSingleIntegration.ixx>
#include <math_Vector.hxx>
#include <math.hxx>
#include <TColStd_SequenceOfReal.hxx>
//==========================================================================
//function : An empty constructor.
//
//==========================================================================
math_KronrodSingleIntegration::math_KronrodSingleIntegration() :
myIsDone(Standard_False),
myValue(0.),
myErrorReached(0.),
myNbPntsReached(0),
myNbIterReached(0)
{
}
//==========================================================================
//function : Constructor
//
//==========================================================================
math_KronrodSingleIntegration::math_KronrodSingleIntegration
( math_Function &theFunction,
const Standard_Real theLower,
const Standard_Real theUpper,
const Standard_Integer theNbPnts):
myIsDone(Standard_False),
myValue(0.),
myErrorReached(0.),
myNbPntsReached(0)
{
Perform(theFunction, theLower, theUpper, theNbPnts);
}
//==========================================================================
//function : Constructor
//
//==========================================================================
math_KronrodSingleIntegration::math_KronrodSingleIntegration
( math_Function &theFunction,
const Standard_Real theLower,
const Standard_Real theUpper,
const Standard_Integer theNbPnts,
const Standard_Real theTolerance,
const Standard_Integer theMaxNbIter) :
myIsDone(Standard_False),
myValue(0.),
myErrorReached(0.),
myNbPntsReached(0)
{
Perform(theFunction, theLower, theUpper, theNbPnts,
theTolerance, theMaxNbIter);
}
//==========================================================================
//function : Perform
// Computation of the integral.
//==========================================================================
void math_KronrodSingleIntegration::Perform
( math_Function &theFunction,
const Standard_Real theLower,
const Standard_Real theUpper,
const Standard_Integer theNbPnts)
{
const Standard_Real aPtol = 1.e-9;
myNbIterReached = 0;
if (theNbPnts < 3 ) {
myIsDone = Standard_False;
return;
}
if(theUpper - theLower < aPtol) {
myIsDone = Standard_False;
return;
}
// Get an odd value of number of initial points.
myNbPntsReached = (theNbPnts%2 == 0) ? theNbPnts + 1 : theNbPnts;
myErrorReached = RealLast();
Standard_Integer aNGauss = myNbPntsReached/2;
math_Vector aKronrodP(1, myNbPntsReached);
math_Vector aKronrodW(1, myNbPntsReached);
math_Vector aGaussP(1, aNGauss);
math_Vector aGaussW(1, aNGauss);
if (!math::KronrodPointsAndWeights(myNbPntsReached, aKronrodP, aKronrodW) ||
!math::OrderedGaussPointsAndWeights(aNGauss, aGaussP, aGaussW)) {
myIsDone = Standard_False;
return;
}
myIsDone = GKRule(theFunction, theLower, theUpper, aGaussP, aGaussW, aKronrodP, aKronrodW,
myValue, myErrorReached);
if(!myIsDone) return;
myAbsolutError = myErrorReached;
//if (anAbsVal > aMinVol)
//myErrorReached /= anAbsVal;
myNbIterReached++;
}
//=======================================================================
//function : Perform
//purpose :
//=======================================================================
void math_KronrodSingleIntegration::Perform
( math_Function &theFunction,
const Standard_Real theLower,
const Standard_Real theUpper,
const Standard_Integer theNbPnts,
const Standard_Real theTolerance,
const Standard_Integer theMaxNbIter)
{
Standard_Real aMinVol = Epsilon(1.);
myNbIterReached = 0;
// Check prerequisites.
if (theNbPnts < 3 ||
theTolerance <= 0.) {
myIsDone = Standard_False;
return;
}
// Get an odd value of number of initial points.
myNbPntsReached = (theNbPnts%2 == 0) ? theNbPnts + 1 : theNbPnts;
Standard_Integer aNGauss = myNbPntsReached/2;
math_Vector aKronrodP(1, myNbPntsReached);
math_Vector aKronrodW(1, myNbPntsReached);
math_Vector aGaussP(1, aNGauss);
math_Vector aGaussW(1, aNGauss);
if (!math::KronrodPointsAndWeights(myNbPntsReached, aKronrodP, aKronrodW) ||
!math::OrderedGaussPointsAndWeights(aNGauss, aGaussP, aGaussW)) {
myIsDone = Standard_False;
return;
}
//First iteration
myIsDone = GKRule(theFunction, theLower, theUpper, aGaussP, aGaussW, aKronrodP, aKronrodW,
myValue, myErrorReached);
if(!myIsDone) return;
Standard_Real anAbsVal = Abs(myValue);
myAbsolutError = myErrorReached;
if (anAbsVal > aMinVol)
myErrorReached /= anAbsVal;
myNbIterReached++;
if(myErrorReached <= theTolerance) return;
if(myNbIterReached >= theMaxNbIter) return;
TColStd_SequenceOfReal anIntervals;
TColStd_SequenceOfReal anErrors;
TColStd_SequenceOfReal aValues;
anIntervals.Append(theLower);
anIntervals.Append(theUpper);
anErrors.Append(myAbsolutError);
aValues.Append(myValue);
Standard_Integer i, nint, nbints;
Standard_Real maxerr;
Standard_Integer count = 0;
while(myErrorReached > theTolerance && myNbIterReached < theMaxNbIter) {
//Searching interval with max error
nbints = anIntervals.Length() - 1;
nint = 0;
maxerr = 0.;
for(i = 1; i <= nbints; ++i) {
if(anErrors(i) > maxerr) {
maxerr = anErrors(i);
nint = i;
}
}
Standard_Real a = anIntervals(nint);
Standard_Real b = anIntervals(nint+1);
Standard_Real c = 0.5*(a + b);
Standard_Real v1, v2, e1, e2;
myIsDone = GKRule(theFunction, a, c, aGaussP, aGaussW, aKronrodP, aKronrodW,
v1, e1);
if(!myIsDone) return;
myIsDone = GKRule(theFunction, c, b, aGaussP, aGaussW, aKronrodP, aKronrodW,
v2, e2);
if(!myIsDone) return;
myNbIterReached++;
Standard_Real deltav = v1 + v2 - aValues(nint);
myValue += deltav;
if(Abs(deltav) <= Epsilon(Abs(myValue))) ++count;
Standard_Real deltae = e1 + e2 - anErrors(nint);
myAbsolutError += deltae;
if(myAbsolutError <= Epsilon(Abs(myValue))) ++count;
if(Abs(myValue) > aMinVol) myErrorReached = myAbsolutError/Abs(myValue);
else myErrorReached = myAbsolutError;
if(count > 50) return;
//Inserting new interval
anIntervals.InsertAfter(nint, c);
anErrors(nint) = e1;
anErrors.InsertAfter(nint, e2);
aValues(nint) = v1;
aValues.InsertAfter(nint, v2);
}
}
//=======================================================================
//function : GKRule
//purpose :
//=======================================================================
Standard_Boolean math_KronrodSingleIntegration::GKRule(
math_Function &theFunction,
const Standard_Real theLower,
const Standard_Real theUpper,
const math_Vector& theGaussP,
const math_Vector& theGaussW,
const math_Vector& theKronrodP,
const math_Vector& theKronrodW,
Standard_Real& theValue,
Standard_Real& theError)
{
Standard_Boolean IsDone;
Standard_Integer aNKronrod = theKronrodP.Length();
Standard_Real aGaussVal;
Standard_Integer aNPnt2 = (aNKronrod + 1)/2;
Standard_Integer i;
Standard_Real aDx;
Standard_Real aVal1;
Standard_Real aVal2;
math_Vector f1(1, aNPnt2-1);
math_Vector f2(1, aNPnt2-1);
Standard_Real aXm = 0.5*(theUpper + theLower);
Standard_Real aXr = 0.5*(theUpper - theLower);
// Compute Gauss quadrature
aGaussVal = 0.;
theValue = 0.;
for (i = 2; i < aNPnt2; i += 2) {
aDx = aXr*theKronrodP.Value(i);
if (!theFunction.Value(aXm + aDx, aVal1) ||
!theFunction.Value(aXm - aDx, aVal2)) {
IsDone = Standard_False;
return IsDone;
}
f1(i) = aVal1;
f2(i) = aVal2;
aGaussVal += (aVal1 + aVal2)*theGaussW.Value(i/2);
theValue += (aVal1 + aVal2)*theKronrodW.Value(i);
}
// Compute value in the middle point.
if (!theFunction.Value(aXm, aVal1)) {
IsDone = Standard_False;
return IsDone;
}
Standard_Real fc = aVal1;
theValue += aVal1*theKronrodW.Value(aNPnt2);
if (i == aNPnt2)
aGaussVal += aVal1*theGaussW.Value(aNPnt2/2);
// Compute Kronrod quadrature
for (i = 1; i < aNPnt2; i += 2) {
aDx = aXr*theKronrodP.Value(i);
if (!theFunction.Value(aXm + aDx, aVal1) ||
!theFunction.Value(aXm - aDx, aVal2)) {
IsDone = Standard_False;
return IsDone;
}
f1(i) = aVal1;
f2(i) = aVal2;
theValue += (aVal1 + aVal2)*theKronrodW.Value(i);
}
Standard_Real mean = 0.5*theValue;
Standard_Real asc = Abs(fc-mean)*theKronrodW.Value(aNPnt2);
for(i = 1; i < aNPnt2; ++i) {
asc += theKronrodW.Value(i)*(Abs(f1(i) - mean) + Abs(f2(i) - mean));
}
asc *= aXr;
theValue *= aXr;
aGaussVal *= aXr;
// Compute the error and the new number of Kronrod points.
theError = Abs(theValue - aGaussVal);
Standard_Real scale =1.;
if(asc != 0. && theError != 0.) scale = Pow((200.*theError/asc), 1.5);
if(scale < 1.) theError = Min(theError, asc*scale);
//theFunction.GetStateNumber();
return Standard_True;
}
|