26 #ifndef _GKINTEGRATION_ 27 #define _GKINTEGRATION_ 36 static const int n = 8;
37 static const double xgk[
n];
38 static const double wg[
n / 2];
39 static const double wgk[
n];
45 static const int n = 11;
46 static const double xgk[
n];
47 static const double wg[
n / 2];
48 static const double wgk[
n];
54 static const int n = 16;
55 static const double xgk[
n];
56 static const double wg[
n / 2];
57 static const double wgk[
n];
63 static const int n = 21;
64 static const double xgk[
n];
65 static const double wg[(
n + 1) / 2];
66 static const double wgk[
n];
72 static const int n = 26;
73 static const double xgk[
n];
74 static const double wg[
n / 2];
75 static const double wgk[
n];
81 static const int n = 31;
82 static const double xgk[
n];
83 static const double wg[
n / 2];
84 static const double wgk[
n];
89 template<
class F,
class GKRule = GK31>
104 os <<
"[a= " <<
ir.a <<
" b= " <<
ir.b <<
" result= " <<
ir.result <<
" error/L= " <<
ir.err / (
ir.b -
ir.a)
105 <<
" error= " <<
ir.err <<
" ]";
110 std::list<IntervalResult>
ir;
127 const double center = 0.5 * (r.
a + r.
b);
128 const double halfLength = 0.5 * r.
delta;
129 const double fCenter =
f(center);
131 double resultGauss = 0;
132 double resultKronrod = fCenter * wgk[
n - 1];
136 resultGauss = fCenter * wg[
n / 2 - 1];
139 for (
int j = 0; j < (
n - 1) / 2; j++)
141 const int jtw = j * 2 + 1;
142 const double xx = halfLength * xgk[jtw];
143 const double fval1 =
f(center - xx);
144 const double fval2 =
f(center + xx);
145 const double fsum = fval1 + fval2;
146 resultGauss += wg[j] * fsum;
147 resultKronrod += wgk[jtw] * fsum;
150 for (
int j = 0; j <
n / 2; j++)
153 const double xx = halfLength * xgk[jtwm1];
154 const double fval1 =
f(center - xx);
155 const double fval2 =
f(center + xx);
156 resultKronrod += wgk[jtwm1] * (fval1 + fval2);
160 resultGauss *= halfLength;
161 resultKronrod *= halfLength;
171 std::cout <<
"/------------------------------------------\\" << std::endl;
173 for (
typename std::list<IntervalResult>::iterator p =
ir.begin(); p !=
ir.end(); p++)
178 std::cout <<
"\\------------------------------------------/" << std::endl;
185 typename std::list<IntervalResult>::iterator rMax(
ir.begin());
187 for (
typename std::list<IntervalResult>::iterator r =
ir.begin()++; r !=
ir.end(); r++)
189 if ((*r).ErrorL() > (*rMax).ErrorL())
201 for (
typename std::list<IntervalResult>::iterator p =
ir.begin(); p !=
ir.end(); p++)
203 typename std::list<IntervalResult>::iterator pn = p;
206 if (((*p).err) < ((*pn).err))
210 ::error(
"Ordering problem in list");
217 double errorSum = 0.0;
221 for (
typename std::list<IntervalResult>::iterator p =
ir.begin(); p !=
ir.end(); p++)
223 errorSum += (*p).err;
230 error(
"CheckError", errorSum, err);
234 if (err / errorSum - 1.0 > 1
e-8 &&
std::abs(err - errorSum) > 1
e-14)
235 error(
"CheckError", errorSum, err, errorSum - err);
238 BMWrite4(
"PassedErrorCheck", errorSum, err, errorSum - err);
243 double errorSum = 0.0;
247 for (
typename std::list<IntervalResult>::iterator p =
ir.begin(); p !=
ir.end(); p++)
249 errorSum += (*p).err;
267 if (
ir.back().err >= r.
err)
273 if (r.
err >=
ir.front().err)
280 typename std::list<IntervalResult>::iterator p =
ir.end();
284 while (r.
err > (*p).err)
295 const double absError,
296 const bool absErrorFlag,
297 const double relError,
298 const bool relErrorFlag,
305 std::cout <<
"Beginning integration" << std::endl;
308 double errorUnresolved = 0.0;
309 const int iterationMax = 30;
311 double lengthMin =
ldexp(b - a, -iterationMax);
315 double result = r0.
result;
317 double errLast = err;
329 while (
ir.size() > 0)
332 double lengthTest = rTest.
delta;
333 if (lengthTest < lengthMin)
335 warning(
"KC:Interval was divided too many times", iterationMax, rTest.a, rTest.b, rTest.err,
ir.size());
338 warning(
"Absolute accuracy = ", absError);
340 warning(
"Relative accuracy = ", relError,
"->absolute accuracy=", relError *
std::abs(result));
343 errorUnresolved += rTest.err;
351 if (
ir.size() == 0 || errorUnresolved > 0.0)
358 double center = 0.5 * (r.a + r.b);
367 err += r1.
err + r2.
err - r.err;
370 std::cout.setf(std::ios::scientific);
371 std::cout <<
"Refined [ " << r.a <<
" " << r.b <<
" ] err/L=" << (r1.
err + r2.
err) / (r.b - r.a)
372 <<
" error=" << err << std::endl;
384 if (err < 1
e-6 * errLast)
393 const bool relOk = (err < relError *
std::abs(result) || result == 0.0);
394 const bool absOk = (err < absError);
396 if (absErrorFlag && relErrorFlag)
398 quitFlag = andFlag ? (relOk && absOk) : (relOk || absOk);
402 quitFlag = absErrorFlag ? absOk : relOk;
411 if (errorUnresolved > 0.0)
413 warning(
"KC:Unresolved error sum=", errorUnresolved,
"for integration interval", a, b);
414 warning(
"KC:--> Result=", result,
"total error=", err,
415 "rel. error=", ((result != 0.0) ? err /
std::abs(result) : 0.0));
423 std::cout <<
"End integration" << std::endl;
426 int numIntervals = 0;
427 for (
typename std::list<IntervalResult>::iterator p =
ir.begin(); p !=
ir.end(); p++)
436 double badSum =
std::abs((result - sum) / sum);
437 if ((badSum > 1.0
e-7) && (
std::abs(result - sum) > absError))
439 std::cerr <<
"absError tolerance = " << absError << std::endl;
440 std::cerr <<
"Percent error = " << badSum * 100.0 << std::endl;
441 std::cerr <<
"Number of intervals = " << numIntervals << std::endl;
442 std::cerr <<
"result = " << result <<
" sum = " << sum << std::endl;
450 double Integrate(
const double a,
const double b,
const double acc)
454 double Integrate(
const double a,
const double b,
const double accAbs,
const double accRel,
const bool andFlag)
456 return Integrate(a, b, accAbs,
true, accRel,
true, andFlag);
460 #endif // _GKINTEGRATION_
double Integrate(const double a, const double b, const double absError, const bool absErrorFlag, const double relError, const bool relErrorFlag, const bool andFlag)
static const double wg[n/2]
void SetRelativeErrorMode()
std::list< IntervalResult > ir
static const double wg[n/2]
void GK(IntervalResult &r)
static const double xgk[n]
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
static const double xgk[n]
static const double wg[(n+1)/2]
MakeReturn< BinaryNode< FnLdexp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t ldexp(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
void CheckError(const double err)
void GKGeneral(const int n, const double xgk[], const double wg[], const double wgk[], IntervalResult &r)
static const double xgk[n]
IntervalResult(const double a_, const double b_, const double delta_)
void SetAbsoluteErrorMode()
void error(char const *m)
friend std::ostream & operator<<(std::ostream &os, const IntervalResult &ir)
static const double xgk[n]
static const double wgk[n]
static const double wgk[n]
#define BMWrite4(i, j, k, l)
double Integrate(const double a, const double b, const double acc)
double Integrate(const double a, const double b, const double accAbs, const double accRel, const bool andFlag)
static const double wg[n/2]
void Insert(const IntervalResult &r)
static const double wg[n/2]
static const double wgk[n]
static const double wg[n/2]
static const double wgk[n]
static const double xgk[n]
static const double wgk[n]
static const double wgk[n]
static const double xgk[n]