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.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   * Tests field-based Gauss-Chebyshev quadrature rules of the second kind against
29   * reference data generated by the {@code GenGCQuad} MATLAB function from NASA's
30   * CARA Analysis Tools.
31   *
32   * @see <a href="https://github.com/nasa/CARA_Analysis_Tools">NASA CARA Analysis Tools</a>
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       * Verifies that the generated quadrature rule matches one of the reference
50       * tables in the {@code GenGCQuad} MATLAB function after converting the
51       * standard second-kind weights to the table's PcCircle weight convention.
52       *
53       * @param genGcQuadTable reference table from {@code GenGCQuad}
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               * Standard second-kind weight:
78               *
79               *   W = pi / (n + 1) * sin^2(theta)
80               *
81               * PcCircle-customized table weight:
82               *
83               *   wGC = pi / (n + 1) * sin(theta) / sqrt(8*pi)
84               *
85               * Since y = sin(theta):
86               *
87               *   wGC = W / y / sqrt(8*pi)
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      * Uses the quadrature rule in the PcCircle convention to test probability of
199      * collision calculations against MATLAB results for simple reference cases.
200      *
201      * @param xm    projected miss distance along the x-axis
202      * @param zm    projected miss distance along the z-axis
203      * @param sx    standard deviation along the x-axis
204      * @param sz    standard deviation along the z-axis
205      * @param hbr   hard-body radius
206      * @param order quadrature rule order
207      * @return probability of collision estimate
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              * PcCircle's GenGCQuad convention uses:
234              *
235              *   wGC = pi / (n + 1) * y / sqrt(8*pi)
236              *
237              * while the standard second-kind rule uses:
238              *
239              *   W = pi / (n + 1) * y^2
240              *
241              * Therefore the integrand passed to FieldGaussIntegrator must include:
242              *
243              *   1 / (y * sqrt(8*pi))
244              *
245              * so that:
246              *
247              *   W * F(u) / (y * sqrt(8*pi)) == wGC * F(u)
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 }