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
21
22
23
24
25
26
27
28
29
30 public abstract class QuadratureIntegrator<T extends Quadrature> extends Integrator {
31
32
33
34
35 public static final double EPS = 1e-10;
36
37
38
39
40 private static final int JMIN = 5;
41
42
43
44
45 private static final int JMAX = 35;
46
47
48
49
50 private final T q;
51
52
53
54
55 private final double eps;
56
57
58
59
60
61
62
63 protected QuadratureIntegrator(final T q, final double eps) {
64 this.q = q;
65 this.eps = eps;
66 }
67
68
69
70
71
72
73
74 @Override
75 public double integrate() throws IntegrationException {
76 try {
77 double s;
78
79 var olds = 0.0;
80
81 for (var j = 0; j < JMAX; j++) {
82 s = q.next();
83 if (j > JMIN && (Math.abs(s - olds) < eps * Math.abs(olds) || (s == 0.0 && olds == 0.0))) {
84
85 return s;
86 }
87 olds = s;
88 }
89 } catch (final EvaluationException e) {
90 throw new IntegrationException(e);
91 }
92
93
94 throw new IntegrationException();
95 }
96
97
98
99
100
101
102 @Override
103 public IntegratorType getIntegratorType() {
104 return IntegratorType.QUADRATURE;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118 public static QuadratureIntegrator<Quadrature> create(
119 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener, final double eps,
120 final QuadratureType quadratureType) {
121 return switch (quadratureType) {
122 case TRAPEZOIDAL -> cast(new TrapezoidalQuadratureIntegrator(a, b, listener, eps));
123 case MID_POINT -> cast(new MidPointQuadratureIntegrator(a, b, listener, eps));
124 case INFINITY_MID_POINT -> cast(new InfinityMidPointQuadratureIntegrator(a, b, listener, eps));
125 case LOWER_SQUARE_ROOT_MID_POINT ->
126 cast(new LowerSquareRootMidPointQuadratureIntegrator(a, b, listener, eps));
127 case UPPER_SQUARE_ROOT_MID_POINT ->
128 cast(new UpperSquareRootMidPointQuadratureIntegrator(a, b, listener, eps));
129 case DOUBLE_EXPONENTIAL_RULE -> cast(new DoubleExponentialRuleQuadratureIntegrator(a, b, listener, eps));
130 default -> throw new IllegalArgumentException();
131 };
132 }
133
134
135
136
137
138
139
140
141
142
143
144 public static QuadratureIntegrator<Quadrature> create(
145 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener,
146 final QuadratureType quadratureType) {
147 return switch (quadratureType) {
148 case TRAPEZOIDAL -> cast(new TrapezoidalQuadratureIntegrator(a, b, listener));
149 case MID_POINT -> cast(new MidPointQuadratureIntegrator(a, b, listener));
150 case INFINITY_MID_POINT -> cast(new InfinityMidPointQuadratureIntegrator(a, b, listener));
151 case LOWER_SQUARE_ROOT_MID_POINT -> cast(new LowerSquareRootMidPointQuadratureIntegrator(a, b, listener));
152 case UPPER_SQUARE_ROOT_MID_POINT -> cast(new UpperSquareRootMidPointQuadratureIntegrator(a, b, listener));
153 case DOUBLE_EXPONENTIAL_RULE -> cast(new DoubleExponentialRuleQuadratureIntegrator(a, b, listener));
154 default -> throw new IllegalArgumentException();
155 };
156 }
157
158
159
160
161
162
163
164
165
166
167 public static QuadratureIntegrator<Quadrature> create(
168 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener, final double eps) {
169 return create(a, b, listener, eps, DEFAULT_QUADRATURE_TYPE);
170 }
171
172
173
174
175
176
177
178
179
180 public static QuadratureIntegrator<Quadrature> create(
181 final double a, final double b, final SingleDimensionFunctionEvaluatorListener listener) {
182 return create(a, b, listener, DEFAULT_QUADRATURE_TYPE);
183 }
184
185
186
187
188
189
190
191 private static QuadratureIntegrator<Quadrature> cast(QuadratureIntegrator<?> integrator) {
192
193 return (QuadratureIntegrator<Quadrature>) integrator;
194 }
195 }