Browse code

move cquad tests and rewrite

Bas Nijholt authored on 01/11/2017 16:43:19
Showing 3 changed files
1 1
similarity index 100%
2 2
rename from algorithm_4.py
3 3
rename to adaptive/tests/algorithm_4.py
4 4
new file mode 100644
... ...
@@ -0,0 +1,72 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import numpy as np
4
+import pytest
5
+from ..learner import IntegratorLearner
6
+from .algorithm_4 import algorithm_4, f0, f7, f21, f24, f63, fdiv
7
+
8
+
9
+def run_integrator_learner(f, a, b, tol, nr_points):
10
+    learner = IntegratorLearner(f, bounds=(a, b), tol=tol)
11
+    for _ in range(nr_points):
12
+        points, _ = learner.choose_points(1)
13
+        learner.add_data(points, map(learner.function, points))
14
+    return learner
15
+
16
+
17
+def same_ivals(f, a, b, tol, verbose=True):
18
+        igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
19
+
20
+        learner = run_integrator_learner(f, a, b, tol, nr_points)
21
+
22
+        if verbose:
23
+            print('igral difference', learner.igral-igral,
24
+                  'err difference', learner.err - err)
25
+
26
+        return learner.equal(ivals, verbose=verbose)
27
+
28
+
29
+def test_cquad():
30
+    for i, args in enumerate([[f0, 0, 3, 1e-5],
31
+                              [f7, 0, 1, 1e-6],
32
+                              [f21, 0, 1, 1e-3],
33
+                              [f24, 0, 3, 1e-3]]):
34
+        assert same_ivals(*args, verbose=True), 'Function {}'.format(i)
35
+
36
+
37
+@pytest.mark.xfail
38
+def test_machine_precision(verbose=True):
39
+    f, a, b, tol = [f63, 0, 1, 1e-10]
40
+    igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
41
+
42
+    learner = run_integrator_learner(f, a, b, tol, nr_points)
43
+
44
+    if verbose:
45
+        print('igral difference', learner.igral-igral,
46
+              'err difference', learner.err - err)
47
+
48
+    assert learner.equal(ivals, verbose=verbose)
49
+
50
+
51
+def test_machine_precision2():
52
+    f, a, b, tol = [f63, 0, 1, 1e-10]
53
+    igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
54
+    
55
+    learner = run_integrator_learner(f, a, b, tol, nr_points)
56
+
57
+    np.testing.assert_almost_equal(igral, learner.igral)
58
+    np.testing.assert_almost_equal(err, learner.err)
59
+
60
+
61
+def test_divergence():
62
+    """This function should raise a DivergentIntegralError."""
63
+    f, a, b, tol = fdiv, 0, 1, 1e-6
64
+    try:
65
+        igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
66
+    except Exception:
67
+        print('The integral is diverging.')
68
+
69
+    try:
70
+        learner = run_integrator_learner(f, a, b, tol, nr_points)
71
+    except Exception:
72
+        print('The integral is diverging.')
0 73
deleted file mode 100644
... ...
@@ -1,66 +0,0 @@
1
-import numpy as np
2
-from adaptive.learner import IntegratorLearner
3
-from algorithm_4 import algorithm_4
4
-
5
-def same_ivals(f, a, b, tol, verbose):
6
-        igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
7
-
8
-        learner = IntegratorLearner(f, bounds=(a, b), tol=tol)
9
-        for i in range(nr_points):
10
-            points, loss_improvement = learner.choose_points(1)
11
-            learner.add_data(points, map(learner.function, points))
12
-        if verbose:
13
-            print('igral diff, ', learner.igral-igral, 'err diff', learner.err - err)
14
-        return learner.equal(ivals, verbose=verbose)
15
-
16
-
17
-def same_ivals_up_to(f, a, b, tol):
18
-        igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
19
-
20
-        learner = IntegratorLearner(f, bounds=(a, b), tol=tol)
21
-        j = 0
22
-        equal_till = 0
23
-        for i in range(nr_points):
24
-            points, loss_improvement = learner.choose_points(1)
25
-            learner.add_data(points, map(learner.function, points))
26
-            if not learner._stack:
27
-                try:
28
-                    j += 1
29
-                    if learner.equal(ivals):
30
-                        equal_till = i + 1
31
-                except:
32
-                    all_equal = False
33
-
34
-        return 'equal_till nr_points={} of {}'.format(equal_till, nr_points)
35
-
36
-if __name__ == '__main__':
37
-    old_settings = np.seterr(all='ignore')
38
-    from algorithm_4 import f0, f7, f24, f21, f63, fdiv
39
-    for i, args in enumerate([[f0, 0, 3, 1e-5],
40
-                              [f7, 0, 1, 1e-6],
41
-                              [f21, 0, 1, 1e-3],
42
-                              [f24, 0, 3, 1e-3],
43
-                              [f63, 0, 1, 1e-10]]):
44
-        print('\nFunction {}'.format(i))
45
-        if same_ivals(*args, verbose=True):
46
-            print(True)
47
-        else:
48
-            print(same_ivals_up_to(*args))
49
-    
50
-    # This function should raise a DivergentIntegralError.
51
-    print('Function ', i+1)
52
-    f, a, b, tol = [fdiv, 0, 1, 1e-6]
53
-    try:
54
-        igral, err, nr_points, ivals = algorithm_4(f, a, b, tol)
55
-    except Exception:
56
-        print('The integral is diverging.')
57
-
58
-    try:
59
-        learner = IntegratorLearner(f, bounds=(a, b), tol=tol)
60
-        for i in range(nr_points):
61
-            points, loss_improvement = learner.choose_points(1)
62
-            learner.add_data(points, map(learner.function, points))
63
-    except Exception:
64
-        print('The integral is diverging.')
65
-
66
-    np.seterr(**old_settings)