1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.numerical.integration;
17
18 import com.irurueta.numerical.EvaluationException;
19 import com.irurueta.numerical.SingleDimensionFunctionEvaluatorListener;
20 import com.irurueta.numerical.interpolation.InterpolationException;
21 import com.irurueta.numerical.interpolation.PolynomialInterpolator;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class RombergIntegrator<T extends Quadrature> extends Integrator {
36
37
38
39
40 public static final double EPS = 3.0e-9;
41
42
43
44
45 private static final int JMAX = 14;
46
47
48
49
50 private static final int JMAXP = JMAX + 1;
51
52
53
54
55 private static final int K = 5;
56
57
58
59
60 protected final T q;
61
62
63
64
65 protected final double eps;
66
67
68
69
70 private final double[] s = new double[JMAX];
71
72
73
74
75 private final double[] h = new double[JMAXP];
76
77
78
79
80 private final PolynomialInterpolator interpolator = new PolynomialInterpolator(h, s, K, false);
81
82
83
84
85
86
87
88 protected RombergIntegrator(final T q, final double eps) {
89 this.q = q;
90 this.eps = eps;
91 }
92
93
94
95
96
97
98
99
100 @Override
101 public double integrate() throws IntegrationException {
102 try {
103 h[0] = 1.0;
104 for (var j = 1; j <= JMAX; j++) {
105 s[j - 1] = q.next();
106 if (j >= K) {
107 final var ss = interpolator.rawinterp(j - K, 0.0);
108 if (Math.abs(interpolator.getDy()) <= eps * Math.abs(ss)) {
109 return ss;
110 }
111 }
112 h[j] = h[j - 1] / 9.0;
113 }
114 } catch (final EvaluationException | InterpolationException e) {
115 throw new IntegrationException(e);
116 }
117
118
119 throw new IntegrationException();
120 }
121
122
123
124
125
126
127 @Override
128 public IntegratorType getIntegratorType() {
129 return IntegratorType.ROMBERG;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public static RombergIntegrator<Quadrature> create(
145 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener, final double eps,
146 final QuadratureType quadratureType) {
147 return switch (quadratureType) {
148 case MID_POINT -> cast(new RombergMidPointQuadratureIntegrator(a, b, listener, eps));
149 case INFINITY_MID_POINT -> cast(new RombergInfinityMidPointQuadratureIntegrator(a, b, listener, eps));
150 case LOWER_SQUARE_ROOT_MID_POINT ->
151 cast(new RombergLowerSquareRootMidPointQuadratureIntegrator(a, b, listener, eps));
152 case UPPER_SQUARE_ROOT_MID_POINT ->
153 cast(new RombergUpperSquareRootMidPointQuadratureIntegrator(a, b, listener, eps));
154 case EXPONENTIAL_MID_POINT -> cast(new RombergExponentialMidPointQuadratureIntegrator(a, listener, eps));
155 case DOUBLE_EXPONENTIAL_RULE ->
156 cast(new RombergDoubleExponentialRuleQuadratureIntegrator(a, b, listener, eps));
157 default -> cast(new RombergTrapezoidalQuadratureIntegrator(a, b, listener, eps));
158 };
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 public static RombergIntegrator<Quadrature> create(
173 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener,
174 final QuadratureType quadratureType) {
175 return switch (quadratureType) {
176 case MID_POINT -> cast(new RombergMidPointQuadratureIntegrator(a, b, listener));
177 case INFINITY_MID_POINT -> cast(new RombergInfinityMidPointQuadratureIntegrator(a, b, listener));
178 case LOWER_SQUARE_ROOT_MID_POINT ->
179 cast(new RombergLowerSquareRootMidPointQuadratureIntegrator(a, b, listener));
180 case UPPER_SQUARE_ROOT_MID_POINT ->
181 cast(new RombergUpperSquareRootMidPointQuadratureIntegrator(a, b, listener));
182 case EXPONENTIAL_MID_POINT -> cast(new RombergExponentialMidPointQuadratureIntegrator(a, listener));
183 case DOUBLE_EXPONENTIAL_RULE -> cast(new RombergDoubleExponentialRuleQuadratureIntegrator(a, b, listener));
184 default -> cast(new RombergTrapezoidalQuadratureIntegrator(a, b, listener));
185 };
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199 public static RombergIntegrator<Quadrature> create(
200 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener, final double eps) {
201 return create(a, b, listener, eps, DEFAULT_QUADRATURE_TYPE);
202 }
203
204
205
206
207
208
209
210
211
212
213
214 public static RombergIntegrator<Quadrature> create(
215 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener) {
216 return create(a, b, listener, DEFAULT_QUADRATURE_TYPE);
217 }
218
219
220
221
222
223
224
225 private static RombergIntegrator<Quadrature> cast(final RombergIntegrator<?> integrator) {
226
227 return (RombergIntegrator<Quadrature>) integrator;
228 }
229 }