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
|
#! /usr/bin/env python
from __future__ import print_function
from openturns import *
import math
TESTPREAMBLE()
RandomGenerator.SetSeed(0)
try:
# First, a smooth function
f = NumericalMathFunction("x", "sin(x)")
a = -2.5
b = 4.5
# Default parameters
algo = GaussKronrod()
rules = [GaussKronrodRule(GaussKronrodRule.G3K7), GaussKronrodRule(
GaussKronrodRule.G7K15), GaussKronrodRule(GaussKronrodRule.G11K23), GaussKronrodRule(GaussKronrodRule.G15K31), GaussKronrodRule(GaussKronrodRule.G25K51)]
for i in range(len(rules)):
algo.setRule(rules[i])
print("Algo=", algo)
# High-level interface
error = NumericalPoint()
value = algo.integrate(f, Interval(a, b), error)[0]
ref = math.cos(a) - math.cos(b)
print("value=%.6f" % value, ", ref=%.6f" % ref, ", true error below bound? ", abs(ref - value)
< algo.getMaximumError(), ", estimated error below bound? ", error[0] < algo.getMaximumError())
# Low-level interface
#ai = NumericalPoint()
#bi = NumericalPoint()
#fi = NumericalSample()
#ei = NumericalPoint()
#value2 = algo.integrate(f, a, b, error, ai, bi, fi, ei)[0]
# ai.add(b)
#g = f.draw(a, b, 512)
#lower = Cloud(ai, NumericalPoint(ai.getDimension()))
# lower.setColor("magenta")
# g.add(lower)
#g.draw("Smooth_function_adaptation_" + str(i))
# Second, a piecewise smooth function
f = NumericalMathFunction("x", "abs(sin(x))")
a = -2.5
b = 4.5
algo = GaussKronrod()
rules = [GaussKronrodRule(GaussKronrodRule.G3K7), GaussKronrodRule(
GaussKronrodRule.G7K15), GaussKronrodRule(GaussKronrodRule.G11K23), GaussKronrodRule(GaussKronrodRule.G15K31), GaussKronrodRule(GaussKronrodRule.G25K51)]
for i in range(len(rules)):
algo.setRule(rules[i])
print("Algo=", algo)
error = NumericalPoint()
value = algo.integrate(f, Interval(a, b), error)[0]
ref = 4.0 + math.cos(b) - math.cos(a)
print("value=%.6f" % value, ", ref=%.6f" % ref, ", true error below bound? ", abs(ref - value)
< algo.getMaximumError(), ", estimated error below bound? ", error[0] < algo.getMaximumError())
# Low-level interface
## ai = NumericalPoint()
## bi = NumericalPoint()
## fi = NumericalSample()
## ei = NumericalPoint()
## value2 = algo.integrate(f, a, b, error, ai, bi, fi, ei)[0]
# print "ai=", ai
# print "bi=", bi
# print "fi=", fi
# print "ei=", ei
# ai.add(b)
## g = f.draw(a, b, 512)
## lower = Cloud(ai, NumericalPoint(ai.getDimension()))
# lower.setColor("magenta")
# g.add(lower)
## g.draw("Nonsmooth_function_adaptation_" + str(i))
except:
import sys
print("t_GaussKronrod_std.py", sys.exc_info()[0], sys.exc_info()[1])
|