1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.integration.gauss;
18
19 import org.hipparchus.special.Erf;
20 import org.hipparchus.util.Binary64;
21 import org.hipparchus.util.Binary64Field;
22 import org.hipparchus.util.FastMath;
23 import org.hipparchus.util.Pair;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30
31
32
33
34 class FieldChebyshevSecondKindTest {
35
36 private static final double TOLERANCE = 1.0e-15;
37
38 @Test
39 void matchesGenGCQuadSixteenPointTableAfterPcCircleWeightConversion() {
40 assertMatchesGenGCQuadTable(ChebyshevSecondKindTest.GEN_GC_QUAD_16);
41 }
42
43 @Test
44 void matchesGenGCQuadSixtyFourPointTableAfterPcCircleWeightConversion() {
45 assertMatchesGenGCQuadTable(ChebyshevSecondKindTest.GEN_GC_QUAD_64);
46 }
47
48
49
50
51
52
53
54
55 private static void assertMatchesGenGCQuadTable(final double[][] genGcQuadTable) {
56 final int order = genGcQuadTable.length;
57
58 final FieldChebyshevSecondKindRuleFactory<Binary64> factory =
59 new FieldChebyshevSecondKindRuleFactory<>(Binary64Field.getInstance());
60 final Pair<Binary64[], Binary64[]> rule = factory.getRule(order);
61
62 final Binary64[] secondKindPoints = rule.getFirst();
63 final Binary64[] secondKindWeights = rule.getSecond();
64
65 Assertions.assertEquals(order, secondKindPoints.length);
66 Assertions.assertEquals(order, secondKindWeights.length);
67
68 for (int tableIndex = 0; tableIndex < order; tableIndex++) {
69 final double expectedX = genGcQuadTable[tableIndex][0];
70 final double expectedY = genGcQuadTable[tableIndex][1];
71 final double expectedPcCircleWeight = genGcQuadTable[tableIndex][2];
72
73 final Binary64 x = secondKindPoints[tableIndex];
74 final Binary64 y = FastMath.sqrt(Binary64.ONE.subtract(x.square()));
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 final Binary64 pcCircleWeight =
90 secondKindWeights[tableIndex].divide(y).divide(FastMath.sqrt(8.0 * FastMath.PI));
91
92 Assertions.assertEquals(expectedX, x.getReal(), TOLERANCE, "xGC mismatch at table index " + tableIndex);
93 Assertions.assertEquals(expectedY, y.getReal(), TOLERANCE, "yGC mismatch at table index " + tableIndex);
94 Assertions.assertEquals(expectedPcCircleWeight, pcCircleWeight.getReal(), TOLERANCE,
95 "wGC mismatch at table index " + tableIndex);
96 }
97 }
98
99 @Test
100 void matchesMatlabPcCircleIntegrandForModerateOffsetWithOrder16() {
101 assertMatchesMatlabReference(
102 0.35,
103 0.20,
104 1.10,
105 0.75,
106 0.45,
107 16,
108 1.06055364621781625e-01
109 );
110 }
111
112 @Test
113 void matchesMatlabPcCircleIntegrandForModerateOffsetWithOrder64() {
114 assertMatchesMatlabReference(
115 0.35,
116 0.20,
117 1.10,
118 0.75,
119 0.45,
120 64,
121 1.06055364621781625e-01
122 );
123 }
124
125 @Test
126 void matchesMatlabPcCircleIntegrandForNearCenterWithOrder16() {
127 assertMatchesMatlabReference(
128 0.05,
129 0.08,
130 0.80,
131 1.25,
132 0.30,
133 16,
134 4.37336618192281021e-02
135 );
136 }
137
138 @Test
139 void matchesMatlabPcCircleIntegrandForNearCenterWithOrder64() {
140 assertMatchesMatlabReference(
141 0.05,
142 0.08,
143 0.80,
144 1.25,
145 0.30,
146 64,
147 4.37336618192281021e-02
148 );
149 }
150
151 @Test
152 void matchesMatlabPcCircleIntegrandForLargerRadiusWithOrder16() {
153 assertMatchesMatlabReference(
154 0.90,
155 0.40,
156 1.50,
157 0.65,
158 0.70,
159 16,
160 1.56273946267817959e-01
161 );
162 }
163
164 @Test
165 void matchesMatlabPcCircleIntegrandForLargerRadiusWithOrder64() {
166 assertMatchesMatlabReference(
167 0.90,
168 0.40,
169 1.50,
170 0.65,
171 0.70,
172 64,
173 1.56273946267818015e-01
174 );
175 }
176
177 private static void assertMatchesMatlabReference(final double xm,
178 final double zm,
179 final double sx,
180 final double sz,
181 final double hbr,
182 final int order,
183 final double expected) {
184 final Binary64 actual = integratePcCircleStyle(
185 new Binary64(xm),
186 new Binary64(zm),
187 new Binary64(sx),
188 new Binary64(sz),
189 new Binary64(hbr),
190 order
191 );
192
193 Assertions.assertEquals(expected, actual.getReal(), TOLERANCE,
194 "PcCircle-style field second-kind quadrature mismatch for order " + order);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209 private static Binary64 integratePcCircleStyle(final Binary64 xm,
210 final Binary64 zm,
211 final Binary64 sx,
212 final Binary64 sz,
213 final Binary64 hbr,
214 final int order) {
215 final FieldGaussIntegratorFactory<Binary64> factory =
216 new FieldGaussIntegratorFactory<>(Binary64Field.getInstance());
217 final FieldGaussIntegrator<Binary64> integrator = factory.chebyshevSecondKind(order);
218
219 final Binary64 sqrt2 = FastMath.sqrt(new Binary64(2.0));
220 final Binary64 dx = sx.multiply(sqrt2);
221 final Binary64 dz = sz.multiply(sqrt2);
222
223 final Binary64 integral = integrator.integrate(u -> {
224 final Binary64 y = FastMath.sqrt(Binary64.ONE.subtract(u.square()));
225 final Binary64 x = xm.add(hbr.multiply(u));
226 final Binary64 hx = hbr.multiply(y);
227
228 final Binary64 exponential = FastMath.exp(x.divide(dx).square().negate());
229 final Binary64 errorFunctionDifference = Erf.erf(zm.subtract(hx).divide(dz),
230 zm.add(hx).divide(dz));
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 return exponential.multiply(errorFunctionDifference)
250 .divide(y.multiply(FastMath.sqrt(8.0 * FastMath.PI)));
251 });
252
253 return hbr.divide(sx).multiply(integral);
254 }
255 }