1#! /usr/bin/env python
2from __future__ import print_function
3import openturns as ot
4import math
5import openturns.testing as ott
6
7ot.TESTPREAMBLE()
8
9f = ot.SymbolicFunction(["x"], ["sin(x)"])
10a = -2.5
11b = 4.5
12# Integrate sin(t) between a & b --> cos(b) - sin(b)
13ref = math.cos(a) - math.cos(b)
14
15all_methods = [ot.FejerAlgorithm.FEJERTYPE1,
16               ot.FejerAlgorithm.FEJERTYPE2, ot.FejerAlgorithm.CLENSHAWCURTIS]
17# 1D checking
18for method in all_methods:
19    algo = ot.FejerAlgorithm([100],  method)
20    value, adaptedNodes = algo.integrateWithNodes(f, ot.Interval(a, b))
21    ott.assert_almost_equal(value[0], ref, 1e-10, 1e-10)
22
23g = ot.SymbolicFunction(["x", "y"], ["cos(pi_ * x / 2) * sin(pi_ * y)"])
24ref = 8 / (math.pi * math.pi)
25interval = ot.Interval([-1, 0], [1, 1])
26for method in all_methods:
27    algo = ot.FejerAlgorithm([64, 64], method)
28    value, adaptedNodes = algo.integrateWithNodes(g, interval)
29    ott.assert_almost_equal(value[0], ref, 1e-10, 1e-10)
30
31# Now we use the same calculus using variables changes
32h = ot.SymbolicFunction(
33    ["x", "y"], ["cos(pi_ * x / 2) * sin(pi_ * y / 2 + pi_/2 ) / 2"])
34interval = ot.Interval([-1, -1], [1, 1])
35for method in all_methods:
36    algo = ot.FejerAlgorithm([64, 64],  method)
37    value, adaptedNodes = algo.integrateWithNodes(h, interval)
38    ott.assert_almost_equal(value[0], ref, 1e-10, 1e-10)
39