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.QuinticFunction;
25 import org.hipparchus.analysis.UnivariateFunction;
26 import org.hipparchus.analysis.function.Sin;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.util.FastMath;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40
41
42 final class MidPointIntegratorTest {
43
44
45
46
47 @Test
48 void testLowAccuracy() {
49 UnivariateFunction f = new QuinticFunction();
50 UnivariateIntegrator integrator = new MidPointIntegrator(0.01, 1.0e-10, 2, 4);
51
52 double min = -10;
53 double max = -9;
54 double expected = -3697001.0 / 48.0;
55 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
56 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
57 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
58 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
59 assertEquals(expected, result, tolerance);
60
61 }
62
63
64
65
66 @Test
67 void testSinFunction() {
68 UnivariateFunction f = new Sin();
69 UnivariateIntegrator integrator = new MidPointIntegrator();
70
71 double min = 0;
72 double max = FastMath.PI;
73 double expected = 2;
74 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
75 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
76 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
77 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
78 assertEquals(expected, result, tolerance);
79
80 min = -FastMath.PI/3;
81 max = 0;
82 expected = -0.5;
83 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
84 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
85 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
86 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
87 assertEquals(expected, result, tolerance);
88
89 }
90
91
92
93
94 @Test
95 void testQuinticFunction() {
96 UnivariateFunction f = new QuinticFunction();
97 UnivariateIntegrator integrator = new MidPointIntegrator();
98
99 double min = 0;
100 double max = 1;
101 double expected = -1.0 / 48;
102 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
103 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
104 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
105 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
106 assertEquals(expected, result, tolerance);
107
108 min = 0;
109 max = 0.5;
110 expected = 11.0 / 768;
111 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
112 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
113 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
114 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
115 assertEquals(expected, result, tolerance);
116
117 min = -1;
118 max = 4;
119 expected = 2048 / 3.0 - 78 + 1.0 / 48;
120 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
121 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
122 assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
123 assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
124 assertEquals(expected, result, tolerance);
125
126 }
127
128
129
130
131 @Test
132 void testParameters() {
133 UnivariateFunction f = new Sin();
134
135 try {
136
137 new MidPointIntegrator().integrate(1000, f, 1, -1);
138 fail("Expecting MathIllegalArgumentException - bad interval");
139 } catch (MathIllegalArgumentException ex) {
140
141 }
142 try {
143
144 new MidPointIntegrator(5, 4);
145 fail("Expecting MathIllegalArgumentException - bad iteration limits");
146 } catch (MathIllegalArgumentException ex) {
147
148 }
149 try {
150
151 new MidPointIntegrator(10, 99);
152 fail("Expecting MathIllegalArgumentException - bad iteration limits");
153 } catch (MathIllegalArgumentException ex) {
154
155 }
156 }
157 }