1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.integration;
23
24 import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.Binary64;
27 import org.hipparchus.util.Binary64Field;
28 import org.hipparchus.util.FastMath;
29 import org.junit.Assert;
30 import org.junit.Test;
31
32
33
34
35
36
37
38
39
40
41 public final class FieldRombergIntegratorTest {
42
43
44
45
46 @Test
47 public void testSinFunction() {
48 FieldUnivariateIntegrator<Binary64> integrator = new FieldRombergIntegrator<>(Binary64Field.getInstance());
49
50 Binary64 min = new Binary64(0);
51 Binary64 max = new Binary64(FastMath.PI);
52 double expected = 2;
53 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
54 double result = integrator.integrate(100, x -> x.sin(), min, max).getReal();
55 Assert.assertTrue(integrator.getEvaluations() < 50);
56 Assert.assertTrue(integrator.getIterations() < 10);
57 Assert.assertEquals(expected, result, tolerance);
58
59 min = new Binary64(-FastMath.PI/3);
60 max = new Binary64(0);
61 expected = -0.5;
62 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
63 result = integrator.integrate(100, x -> x.sin(), min, max).getReal();
64 Assert.assertTrue(integrator.getEvaluations() < 50);
65 Assert.assertTrue(integrator.getIterations() < 10);
66 Assert.assertEquals(expected, result, tolerance);
67 }
68
69
70
71
72 @Test
73 public void testQuinticFunction() {
74 CalculusFieldUnivariateFunction<Binary64> f =
75 t -> t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1));
76 FieldUnivariateIntegrator<Binary64> integrator = new FieldRombergIntegrator<>(Binary64Field.getInstance());
77
78 Binary64 min = new Binary64(0);
79 Binary64 max = new Binary64(1);
80 double expected = -1.0 / 48;
81 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
82 double result = integrator.integrate(100, f, min, max).getReal();
83 Assert.assertTrue(integrator.getEvaluations() < 10);
84 Assert.assertTrue(integrator.getIterations() < 5);
85 Assert.assertEquals(expected, result, tolerance);
86
87 min = new Binary64(0);
88 max = new Binary64(0.5);
89 expected = 11.0 / 768;
90 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
91 result = integrator.integrate(100, f, min, max).getReal();
92 Assert.assertTrue(integrator.getEvaluations() < 10);
93 Assert.assertTrue(integrator.getIterations() < 5);
94 Assert.assertEquals(expected, result, tolerance);
95
96 min = new Binary64(-1);
97 max = new Binary64(4);
98 expected = 2048 / 3.0 - 78 + 1.0 / 48;
99 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
100 result = integrator.integrate(100, f, min, max).getReal();
101 Assert.assertTrue(integrator.getEvaluations() < 10);
102 Assert.assertTrue(integrator.getIterations() < 5);
103 Assert.assertEquals(expected, result, tolerance);
104 }
105
106
107
108
109 @Test
110 public void testParameters() {
111
112 try {
113
114 new FieldRombergIntegrator<>(Binary64Field.getInstance()).integrate(1000, x -> x.sin(),
115 new Binary64(1), new Binary64(-1));
116 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
117 } catch (MathIllegalArgumentException ex) {
118
119 }
120 try {
121
122 new FieldRombergIntegrator<>(Binary64Field.getInstance(), 5, 4);
123 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
124 } catch (MathIllegalArgumentException ex) {
125
126 }
127 try {
128
129 new FieldRombergIntegrator<>(Binary64Field.getInstance(), 10, 50);
130 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
131 } catch (MathIllegalArgumentException ex) {
132
133 }
134 }
135 }