View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.analysis.integration.gauss;
18  
19  import org.hipparchus.special.Erf;
20  import org.hipparchus.util.FastMath;
21  import org.hipparchus.util.Pair;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests Gauss-Chebyshev quadrature rules of the second kind against reference
27   * data generated by the {@code GenGCQuad} MATLAB function from NASA's CARA
28   * Analysis Tools.
29   *
30   * @see <a href="https://github.com/nasa/CARA_Analysis_Tools">NASA CARA Analysis Tools</a>
31   */
32  class ChebyshevSecondKindTest {
33  
34      private static final double TOLERANCE = 1.0e-15;
35  
36      static final double[][] GEN_GC_QUAD_16 = {
37              {-9.829730996839018e-01, 1.837495178165702e-01, 6.773407894247465e-03},
38              {-9.324722294043558e-01, 3.612416661871530e-01, 1.331615550646369e-02},
39              {-8.502171357296142e-01, 5.264321628773557e-01, 1.940543741387547e-02},
40              {-7.390089172206590e-01, 6.736956436465573e-01, 2.483389042441458e-02},
41              {-6.026346363792563e-01, 7.980172272802396e-01, 2.941665508151885e-02},
42              {-4.457383557765382e-01, 8.951632913550623e-01, 3.299767083121100e-02},
43              {-2.736629900720829e-01, 9.618256431728190e-01, 3.545499047709023e-02},
44              {-9.226835946330189e-02, 9.957341762950346e-01, 3.670493294584622e-02},
45              {9.226835946330202e-02, 9.957341762950345e-01, 3.670493294584622e-02},
46              {2.736629900720828e-01, 9.618256431728190e-01, 3.545499047709023e-02},
47              {4.457383557765383e-01, 8.951632913550623e-01, 3.299767083121100e-02},
48              {6.026346363792564e-01, 7.980172272802395e-01, 2.941665508151885e-02},
49              {7.390089172206591e-01, 6.736956436465573e-01, 2.483389042441458e-02},
50              {8.502171357296142e-01, 5.264321628773557e-01, 1.940543741387547e-02},
51              {9.324722294043558e-01, 3.612416661871530e-01, 1.331615550646369e-02},
52              {9.829730996839018e-01, 1.837495178165702e-01, 6.773407894247465e-03}
53      };
54  
55      static final double[][] GEN_GC_QUAD_64 = {
56              {-9.988322268323265e-01, 4.831337952550822e-02, 4.657833967754515e-04},
57              {-9.953316347176486e-01, 9.651392091451513e-02, 9.304789348454766e-04},
58              {-9.895063994510511e-01, 1.444890495692213e-01, 1.393001296249116e-03},
59              {-9.813701261394134e-01, 1.921267173537084e-01, 1.852270238580170e-03},
60              {-9.709418174260519e-01, 2.393156642875583e-01, 2.307213117943439e-03},
61              {-9.582458291091662e-01, 2.859456783986892e-01, 2.756767394164220e-03},
62              {-9.433118132577431e-01, 3.319078531285286e-01, 3.199883112400166e-03},
63              {-9.261746489577764e-01, 3.770948416883209e-01, 3.635525355359408e-03},
64              {-9.068743608505453e-01, 4.214011077725294e-01, 4.062676660397876e-03},
65              {-8.854560256532098e-01, 4.647231720437687e-01, 4.480339395850453e-03},
66              {-8.619696668800491e-01, 5.069598538135907e-01, 4.887538091045943e-03},
67              {-8.364701380102265e-01, 5.480125073546703e-01, 5.283321714564022e-03},
68              {-8.090169943749473e-01, 5.877852522924733e-01, 5.666765895413191e-03},
69              {-7.796743540632223e-01, 6.261851975383138e-01, 6.036975081942061e-03},
70              {-7.485107481711009e-01, 6.631226582407955e-01, 6.393084633441722e-03},
71              {-7.155989607441211e-01, 6.985113652489370e-01, 6.734262839554183e-03},
72              {-6.810158587867969e-01, 7.322686665977737e-01, 7.059712862770465e-03},
73              {-6.448422127361704e-01, 7.643157205458485e-01, 7.368674599481501e-03},
74              {-6.071625078187112e-01, 7.945776797137543e-01, 7.660426455235353e-03},
75              {-5.680647467311557e-01, 8.229838658936565e-01, 7.934287030054488e-03},
76              {-5.276402441061325e-01, 8.494679351215212e-01, 8.189616709876989e-03},
77              {-4.859834132426061e-01, 8.739680326265179e-01, 8.425819160404840e-03},
78              {-4.431915455992412e-01, 8.964269372957038e-01, 8.642342719870315e-03},
79              {-3.993645835656953e-01, 9.167921953165825e-01, 8.838681687467587e-03},
80              {-3.546048870425355e-01, 9.350162426854148e-01, 9.014377504440393e-03},
81              {-3.090169943749473e-01, 9.510565162951535e-01, 9.169019825067272e-03},
82              {-2.627073781985868e-01, 9.648755533435515e-01, 9.302247475042994e-03},
83              {-2.157841967678060e-01, 9.764410788292721e-01, 9.413749295017889e-03},
84              {-1.683570413470385e-01, 9.857260809316509e-01, 9.503264867324932e-03},
85              {-1.205366802553229e-01, 9.927088740980540e-01, 9.570585124197263e-03},
86              {-7.243480016176228e-02, 9.973731496914912e-01, 9.615552836055650e-03},
87              {-2.416374523613207e-02, 9.997080140801929e-01, 9.638062978725454e-03},
88              {2.416374523613242e-02, 9.997080140801929e-01, 9.638062978725454e-03},
89              {7.243480016176240e-02, 9.973731496914912e-01, 9.615552836055650e-03},
90              {1.205366802553232e-01, 9.927088740980540e-01, 9.570585124197263e-03},
91              {1.683570413470386e-01, 9.857260809316508e-01, 9.503264867324930e-03},
92              {2.157841967678064e-01, 9.764410788292720e-01, 9.413749295017888e-03},
93              {2.627073781985870e-01, 9.648755533435515e-01, 9.302247475042994e-03},
94              {3.090169943749475e-01, 9.510565162951535e-01, 9.169019825067272e-03},
95              {3.546048870425358e-01, 9.350162426854147e-01, 9.014377504440393e-03},
96              {3.993645835656957e-01, 9.167921953165823e-01, 8.838681687467584e-03},
97              {4.431915455992413e-01, 8.964269372957038e-01, 8.642342719870315e-03},
98              {4.859834132426062e-01, 8.739680326265179e-01, 8.425819160404840e-03},
99              {5.276402441061328e-01, 8.494679351215211e-01, 8.189616709876989e-03},
100             {5.680647467311559e-01, 8.229838658936564e-01, 7.934287030054487e-03},
101             {6.071625078187113e-01, 7.945776797137542e-01, 7.660426455235352e-03},
102             {6.448422127361706e-01, 7.643157205458483e-01, 7.368674599481499e-03},
103             {6.810158587867972e-01, 7.322686665977735e-01, 7.059712862770463e-03},
104             {7.155989607441211e-01, 6.985113652489370e-01, 6.734262839554183e-03},
105             {7.485107481711012e-01, 6.631226582407951e-01, 6.393084633441719e-03},
106             {7.796743540632225e-01, 6.261851975383136e-01, 6.036975081942058e-03},
107             {8.090169943749475e-01, 5.877852522924731e-01, 5.666765895413190e-03},
108             {8.364701380102267e-01, 5.480125073546700e-01, 5.283321714564020e-03},
109             {8.619696668800493e-01, 5.069598538135903e-01, 4.887538091045939e-03},
110             {8.854560256532099e-01, 4.647231720437685e-01, 4.480339395850450e-03},
111             {9.068743608505454e-01, 4.214011077725293e-01, 4.062676660397874e-03},
112             {9.261746489577766e-01, 3.770948416883203e-01, 3.635525355359402e-03},
113             {9.433118132577432e-01, 3.319078531285284e-01, 3.199883112400164e-03},
114             {9.582458291091662e-01, 2.859456783986892e-01, 2.756767394164220e-03},
115             {9.709418174260520e-01, 2.393156642875579e-01, 2.307213117943434e-03},
116             {9.813701261394134e-01, 1.921267173537084e-01, 1.852270238580170e-03},
117             {9.895063994510511e-01, 1.444890495692213e-01, 1.393001296249116e-03},
118             {9.953316347176486e-01, 9.651392091451513e-02, 9.304789348454766e-04},
119             {9.988322268323266e-01, 4.831337952550593e-02, 4.657833967754294e-04}
120     };
121 
122     @Test
123     void matchesGenGCQuadSixteenPointTableAfterPcCircleWeightConversion() {
124         assertMatchesGenGCQuadTable(GEN_GC_QUAD_16);
125     }
126 
127     @Test
128     void matchesGenGCQuadSixtyFourPointTableAfterPcCircleWeightConversion() {
129         assertMatchesGenGCQuadTable(GEN_GC_QUAD_64);
130     }
131 
132     /**
133      * Verifies that the generated quadrature rule matches one of the reference
134      * tables in the {@code GenGCQuad} MATLAB function after converting the
135      * standard second-kind weights to the table's PcCircle weight convention.
136      *
137      * @param genGcQuadTable reference table from {@code GenGCQuad}
138      */
139     private static void assertMatchesGenGCQuadTable(final double[][] genGcQuadTable) {
140         final int order = genGcQuadTable.length;
141 
142         final ChebyshevSecondKindRuleFactory factory = new ChebyshevSecondKindRuleFactory();
143         final Pair<double[], double[]> rule = factory.getRule(order);
144 
145         final double[] secondKindPoints = rule.getFirst();
146         final double[] secondKindWeights = rule.getSecond();
147 
148         Assertions.assertEquals(order, secondKindPoints.length);
149         Assertions.assertEquals(order, secondKindWeights.length);
150 
151         for (int tableIndex = 0; tableIndex < order; tableIndex++) {
152 
153             final double expectedX = genGcQuadTable[tableIndex][0];
154             final double expectedY = genGcQuadTable[tableIndex][1];
155             final double expectedPcCircleWeight = genGcQuadTable[tableIndex][2];
156 
157             final double x = secondKindPoints[tableIndex];
158             final double y = FastMath.sqrt(1.0 - x * x);
159 
160             /*
161              * Standard second-kind weight:
162              *
163              *   W = pi / (n + 1) * sin^2(theta)
164              *
165              * PcCircle-customized table weight:
166              *
167              *   wGC = pi / (n + 1) * sin(theta) / sqrt(8*pi)
168              *
169              * Since y = sin(theta):
170              *
171              *   wGC = W / y / sqrt(8*pi)
172              */
173             final double pcCircleWeight = secondKindWeights[tableIndex] / y / FastMath.sqrt(8.0 * FastMath.PI);
174 
175             Assertions.assertEquals(expectedX, x, TOLERANCE, "xGC mismatch at table index " + tableIndex);
176             Assertions.assertEquals(expectedY, y, TOLERANCE, "yGC mismatch at table index " + tableIndex);
177             Assertions.assertEquals(expectedPcCircleWeight, pcCircleWeight, TOLERANCE,
178                                     "wGC mismatch at table index " + tableIndex);
179         }
180     }
181 
182 
183     @Test
184     void matchesMatlabPcCircleIntegrandForModerateOffsetWithOrder16() {
185         assertMatchesMatlabReference(
186                 0.35,
187                 0.20,
188                 1.10,
189                 0.75,
190                 0.45,
191                 16,
192                 1.06055364621781625e-01
193         );
194     }
195 
196     @Test
197     void matchesMatlabPcCircleIntegrandForModerateOffsetWithOrder64() {
198         assertMatchesMatlabReference(
199                 0.35,
200                 0.20,
201                 1.10,
202                 0.75,
203                 0.45,
204                 64,
205                 1.06055364621781625e-01
206         );
207     }
208 
209     @Test
210     void matchesMatlabPcCircleIntegrandForNearCenterWithOrder16() {
211         assertMatchesMatlabReference(
212                 0.05,
213                 0.08,
214                 0.80,
215                 1.25,
216                 0.30,
217                 16,
218                 4.37336618192281021e-02
219         );
220     }
221 
222     @Test
223     void matchesMatlabPcCircleIntegrandForNearCenterWithOrder64() {
224         assertMatchesMatlabReference(
225                 0.05,
226                 0.08,
227                 0.80,
228                 1.25,
229                 0.30,
230                 64,
231                 4.37336618192281021e-02
232         );
233     }
234 
235     @Test
236     void matchesMatlabPcCircleIntegrandForLargerRadiusWithOrder16() {
237         assertMatchesMatlabReference(
238                 0.90,
239                 0.40,
240                 1.50,
241                 0.65,
242                 0.70,
243                 16,
244                 1.56273946267817959e-01
245         );
246     }
247 
248     @Test
249     void matchesMatlabPcCircleIntegrandForLargerRadiusWithOrder64() {
250         assertMatchesMatlabReference(
251                 0.90,
252                 0.40,
253                 1.50,
254                 0.65,
255                 0.70,
256                 64,
257                 1.56273946267818015e-01
258         );
259     }
260 
261     private static void assertMatchesMatlabReference(final double xm,
262                                                      final double zm,
263                                                      final double sx,
264                                                      final double sz,
265                                                      final double hbr,
266                                                      final int order,
267                                                      final double expected) {
268         final double actual = integratePcCircleStyle(xm, zm, sx, sz, hbr, order);
269 
270         Assertions.assertEquals(expected, actual, TOLERANCE,
271                                 "PcCircle-style second-kind quadrature mismatch for order " + order);
272     }
273 
274     /**
275      * Uses the quadrature rule in the PcCircle convention to test probability of
276      * collision calculations against MATLAB results for simple reference cases.
277      *
278      * @param xm    projected miss distance along the x-axis
279      * @param zm    projected miss distance along the z-axis
280      * @param sx    standard deviation along the x-axis
281      * @param sz    standard deviation along the z-axis
282      * @param hbr   hard-body radius
283      * @param order quadrature rule order
284      * @return probability of collision estimate
285      */
286     private static double integratePcCircleStyle(final double xm,
287                                                  final double zm,
288                                                  final double sx,
289                                                  final double sz,
290                                                  final double hbr,
291                                                  final int order) {
292         final GaussIntegratorFactory factory = new GaussIntegratorFactory();
293         final GaussIntegrator integrator = factory.chebyshevSecondKind(order);
294 
295         final double sqrt2 = FastMath.sqrt(2.0);
296         final double dx = sqrt2 * sx;
297         final double dz = sqrt2 * sz;
298 
299         final double integral = integrator.integrate(u -> {
300             final double y = FastMath.sqrt(1.0 - u * u);
301             final double x = xm + hbr * u;
302             final double hx = hbr * y;
303 
304             final double exponential = FastMath.exp(-FastMath.pow(x / dx, 2.0));
305             final double errorFunctionDifference = Erf.erf((zm - hx) / dz, (zm + hx) / dz);
306 
307             /*
308              * PcCircle's GenGCQuad convention uses:
309              *
310              *   wGC = pi / (n + 1) * y / sqrt(8*pi)
311              *
312              * while the standard second-kind rule uses:
313              *
314              *   W = pi / (n + 1) * y^2
315              *
316              * Therefore the integrand passed to GaussIntegrator must include:
317              *
318              *   1 / (y * sqrt(8*pi))
319              *
320              * so that:
321              *
322              *   W * F(u) / (y * sqrt(8*pi)) == wGC * F(u)
323              */
324             return exponential * errorFunctionDifference / (y * FastMath.sqrt(8.0 * FastMath.PI));
325         });
326 
327         return hbr / sx * integral;
328     }
329 }