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