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 public final class FieldTrapezoidIntegratorTest {
41
42
43
44
45 @Test
46 public void testSinFunction() {
47 FieldUnivariateIntegrator<Binary64> integrator = new FieldTrapezoidIntegrator<>(Binary64Field.getInstance());
48
49 Binary64 min = new Binary64(0);
50 Binary64 max = new Binary64(FastMath.PI);
51 double expected = 2;
52 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
53 double result = integrator.integrate(10000, x -> x.sin(), min, max).getReal();
54 Assert.assertTrue(integrator.getEvaluations() < 2500);
55 Assert.assertTrue(integrator.getIterations() < 15);
56 Assert.assertEquals(expected, result, tolerance);
57
58 min = new Binary64(-FastMath.PI/3);
59 max = new Binary64(0);
60 expected = -0.5;
61 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
62 result = integrator.integrate(10000, x -> x.sin(), min, max).getReal();
63 Assert.assertTrue(integrator.getEvaluations() < 2500);
64 Assert.assertTrue(integrator.getIterations() < 15);
65 Assert.assertEquals(expected, result, tolerance);
66 }
67
68
69
70
71 @Test
72 public void testQuinticFunction() {
73 CalculusFieldUnivariateFunction<Binary64> f =
74 t -> t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1));
75 FieldUnivariateIntegrator<Binary64> integrator = new FieldTrapezoidIntegrator<>(Binary64Field.getInstance());
76
77 Binary64 min = new Binary64(0);
78 Binary64 max = new Binary64(1);
79 double expected = -1.0 / 48;
80 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
81 double result = integrator.integrate(10000, f, min, max).getReal();
82 Assert.assertTrue(integrator.getEvaluations() < 5000);
83 Assert.assertTrue(integrator.getIterations() < 15);
84 Assert.assertEquals(expected, result, tolerance);
85
86 min = new Binary64(0);
87 max = new Binary64(0.5);
88 expected = 11.0 / 768;
89 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
90 result = integrator.integrate(10000, f, min, max).getReal();
91 Assert.assertTrue(integrator.getEvaluations() < 2500);
92 Assert.assertTrue(integrator.getIterations() < 15);
93 Assert.assertEquals(expected, result, tolerance);
94
95 min = new Binary64(-1);
96 max = new Binary64(4);
97 expected = 2048 / 3.0 - 78 + 1.0 / 48;
98 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
99 result = integrator.integrate(10000, f, min, max).getReal();
100 Assert.assertTrue(integrator.getEvaluations() < 5000);
101 Assert.assertTrue(integrator.getIterations() < 15);
102 Assert.assertEquals(expected, result, tolerance);
103
104 }
105
106
107
108
109 @Test
110 public void testParameters() {
111 try {
112
113 new FieldTrapezoidIntegrator<>(Binary64Field.getInstance()).integrate(1000, x -> x.sin(),
114 new Binary64(1), new Binary64(-1));
115 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
116 } catch (MathIllegalArgumentException ex) {
117
118 }
119 try {
120
121 new FieldTrapezoidIntegrator<>(Binary64Field.getInstance(), 5, 4);
122 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
123 } catch (MathIllegalArgumentException ex) {
124
125 }
126 try {
127
128 new FieldTrapezoidIntegrator<>(Binary64Field.getInstance(), 10,99);
129 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
130 } catch (MathIllegalArgumentException ex) {
131
132 }
133 }
134 }