File: t_GaussKronrod_std.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (78 lines) | stat: -rwxr-xr-x 2,098 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python

import openturns as ot
import math

ot.TESTPREAMBLE()

# First, a smooth function
f = ot.SymbolicFunction("x", "sin(x)")
a = -2.5
b = 4.5
# Default parameters
algo = ot.GaussKronrod()
rules = ot.GaussKronrod.GetRules()
for i in range(len(rules)):
    algo.setRule(rules[i])
    print("Algo=", algo)
    # High-level interface
    error = ot.Point()
    value = algo.integrate(f, ot.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 = Point()
    # bi = Point()
    # fi = Sample()
    # ei = Point()
    # value2 = algo.integrate(f, a, b, error, ai, bi, fi, ei)[0]
    # ai.add(b)
    # g = f.draw(a, b, 512)
    # lower = Cloud(ai, Point(ai.getDimension()))
    # lower.setColor("magenta")
    # g.add(lower)

# Second, a piecewise smooth function
f = ot.SymbolicFunction("x", "abs(sin(x))")
a = -2.5
b = 4.5
algo = ot.GaussKronrod()
rules = ot.GaussKronrod.GetRules()
rules[0] = ot.GaussKronrod.GetRuleFromName("G3K7")

for i in range(len(rules)):
    algo.setRule(rules[i])
    print("Algo=", algo)
    error = ot.Point()
    value = algo.integrate(f, ot.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 = Point()
    # bi = Point()
    # fi = Sample()
    # ei = Point()
    # 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, Point(ai.getDimension()))
    # lower.setColor("magenta")
    # g.add(lower)