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.special.elliptic.legendre;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.analysis.differentiation.Gradient;
22  import org.hipparchus.special.elliptic.carlson.CarlsonEllipticIntegral;
23  import org.hipparchus.util.Binary64Field;
24  import org.hipparchus.util.FastMath;
25  import org.hipparchus.util.MathUtils;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  
31  public class FieldLegendreEllipticIntegralTest {
32  
33      @Test
34      void testNoConvergence() {
35          doTestNoConvergence(Binary64Field.getInstance());
36      }
37  
38      private <T extends CalculusFieldElement<T>> void doTestNoConvergence(final Field<T> field) {
39          assertTrue(LegendreEllipticIntegral.bigK(field.getZero().newInstance(Double.NaN)).isNaN());
40      }
41  
42      @Test
43      void testComplementary() {
44          doTestComplementary(Binary64Field.getInstance());
45      }
46  
47      private <T extends CalculusFieldElement<T>> void doTestComplementary(final Field<T> field) {
48          for (double m = 0.01; m < 1; m += 0.01) {
49              T k1 = LegendreEllipticIntegral.bigK(field.getZero().newInstance(m));
50              T k2 = LegendreEllipticIntegral.bigKPrime(field.getZero().newInstance(1 - m));
51              assertEquals(k1.getReal(), k2.getReal(), FastMath.ulp(k1).getReal());
52          }
53      }
54  
55      @Test
56      void testAbramowitzStegunExample3() {
57          doTestAbramowitzStegunExample3(Binary64Field.getInstance());
58      }
59  
60      private <T extends CalculusFieldElement<T>> void doTestAbramowitzStegunExample3(final Field<T> field) {
61          T k = LegendreEllipticIntegral.bigK(field.getZero().newInstance(80.0 / 81.0));
62          assertEquals(3.591545001, k.getReal(), 2.0e-9);
63      }
64  
65      public void testAbramowitzStegunExample4() {
66          doTestBigE(Binary64Field.getInstance(), 80.0 / 81.0, 1.019106060, 2.0e-8);
67      }
68  
69      @Test
70      void testAbramowitzStegunExample8() {
71          final double m    = 1.0 / 5.0;
72          doTestBigF(Binary64Field.getInstance(), FastMath.acos(FastMath.sqrt(2) / 3.0), m, 1.115921, 1.0e-6);
73          doTestBigF(Binary64Field.getInstance(), FastMath.acos(FastMath.sqrt(2) / 2.0), m, 0.800380, 1.0e-6);
74      }
75  
76      @Test
77      void testAbramowitzStegunExample9() {
78          final double m    = 1.0 / 2.0;
79          doTestBigF(Binary64Field.getInstance(), MathUtils.SEMI_PI, m, 1.854075, 1.0e-6);
80          doTestBigF(Binary64Field.getInstance(), FastMath.PI / 6.0, m, 0.535623, 1.0e-6);
81      }
82  
83      @Test
84      void testAbramowitzStegunExample10() {
85          final double m    = 4.0 / 5.0;
86          doTestBigF(Binary64Field.getInstance(), FastMath.PI / 6.0, m, 0.543604, 1.0e-6);
87      }
88  
89      @Test
90      void testAbramowitzStegunExample14() {
91          final double k    = 3.0 / 5.0;
92          doTestBigE(Binary64Field.getInstance(), FastMath.asin(FastMath.sqrt(5.0) / 3.0),          k * k, 0.80904, 1.0e-5);
93          doTestBigE(Binary64Field.getInstance(), FastMath.asin(5.0 / (3.0 * FastMath.sqrt(17.0))), k * k, 0.41192, 1.0e-5);
94      }
95  
96      @Test
97      void testAbramowitzStegunTable175() {
98          final double sinAlpha1 = FastMath.sin(FastMath.toRadians(32));
99          doTestBigF(Binary64Field.getInstance(), FastMath.toRadians(15), sinAlpha1 * sinAlpha1, 0.26263487, 1.0e-8);
100         final double sinAlpha2 = FastMath.sin(FastMath.toRadians(46));
101         doTestBigF(Binary64Field.getInstance(), FastMath.toRadians(80), sinAlpha2 * sinAlpha2, 1.61923762, 1.0e-8);
102     }
103 
104     @Test
105     void testAbramowitzStegunTable176() {
106         final double sinAlpha1 = FastMath.sin(FastMath.toRadians(64));
107         doTestBigE(Binary64Field.getInstance(), FastMath.toRadians(25), sinAlpha1 * sinAlpha1, 0.42531712, 1.0e-8);
108         final double sinAlpha2 = FastMath.sin(FastMath.toRadians(76));
109         doTestBigE(Binary64Field.getInstance(), FastMath.toRadians(70), sinAlpha2 * sinAlpha2, 0.96208074, 1.0e-8);
110     }
111 
112     @Test
113     void testAbramowitzStegunTable179() {
114         final double sinAlpha1 = FastMath.sin(FastMath.toRadians(15));
115         doTestBigPi(Binary64Field.getInstance(), FastMath.toRadians(75), 0.4, sinAlpha1 * sinAlpha1, 1.62298, 1.0e-5);
116         final double sinAlpha2 = FastMath.sin(FastMath.toRadians(60));
117         doTestBigPi(Binary64Field.getInstance(), FastMath.toRadians(45), 0.8, sinAlpha2 * sinAlpha2, 1.03076, 1.0e-5);
118         final double sinAlpha3 = FastMath.sin(FastMath.toRadians(15));
119         doTestBigPi(Binary64Field.getInstance(), FastMath.toRadians(75), 0.9, sinAlpha3 * sinAlpha3, 2.79990, 1.0e-5);
120     }
121 
122     @Test
123     void testCompleteVsIncompleteF() {
124         doTestCompleteVsIncompleteF(Binary64Field.getInstance());
125     }
126 
127     @Test
128     void testCompleteVsIncompleteE() {
129         doTestCompleteVsIncompleteE(Binary64Field.getInstance());
130     }
131 
132     @Test
133     void testCompleteVsIncompleteD() {
134         doTestCompleteVsIncompleteD(Binary64Field.getInstance());
135     }
136 
137     @Test
138     void testCompleteVsIncompletePi() {
139         doTestCompleteVsIncompletePi(Binary64Field.getInstance());
140     }
141 
142     @Test
143     void testNomeMediumParameter() {
144         doTestNomeMediumParameter(Binary64Field.getInstance());
145     }
146 
147     @Test
148     void testNomeSmallParameter() {
149         doTestNomeSmallParameter(Binary64Field.getInstance());
150     }
151 
152     @Test
153     void testPrecomputedDelta() {
154         doTestPrecomputedDelta(Binary64Field.getInstance());
155     }
156 
157     @Test
158     void testIntegralsSmallParameter() {
159         doTestIntegralsSmallParameter(Binary64Field.getInstance());
160     }
161 
162     private <T extends CalculusFieldElement<T>> void doTestBigE(final Field<T> field, final double m,
163                                                                 final double expected, final double tol) {
164         assertEquals(expected,
165                             LegendreEllipticIntegral.bigE(field.getZero().newInstance(m)).getReal(),
166                             tol);
167     }
168 
169     private <T extends CalculusFieldElement<T>> void doTestBigE(final Field<T> field,
170                                                                 final double phi, final double m,
171                                                                 final double expected, final double tol) {
172         assertEquals(expected,
173                             LegendreEllipticIntegral.bigE(field.getZero().newInstance(phi),
174                                                           field.getZero().newInstance(m)).getReal(),
175                             tol);
176     }
177 
178     private <T extends CalculusFieldElement<T>> void doTestBigF(final Field<T> field,
179                                                                 final double phi, final double m,
180                                                                 final double expected, final double tol) {
181         assertEquals(expected,
182                             LegendreEllipticIntegral.bigF(field.getZero().newInstance(phi),
183                                                           field.getZero().newInstance(m)).getReal(),
184                             tol);
185     }
186 
187     private <T extends CalculusFieldElement<T>> void doTestBigPi(final Field<T> field,
188                                                                  final double phi, final double alpha2, final double m,
189                                                                  final double expected, final double tol) {
190         assertEquals(expected,
191                             LegendreEllipticIntegral.bigPi(field.getZero().newInstance(alpha2),
192                                                            field.getZero().newInstance(phi),
193                                                            field.getZero().newInstance(m)).getReal(),
194                             tol);
195     }
196 
197     private <T extends CalculusFieldElement<T>> void doTestCompleteVsIncompleteF(final Field<T> field) {
198         for (double m = 0.01; m < 1; m += 0.01) {
199             double complete   = LegendreEllipticIntegral.bigK(field.getZero().newInstance(m)).getReal();
200             double incomplete = LegendreEllipticIntegral.bigF(field.getZero().newInstance(MathUtils.SEMI_PI),
201                                                               field.getZero().newInstance(m)).getReal();
202             assertEquals(complete, incomplete, FastMath.ulp(complete));
203         }
204     }
205 
206     private <T extends CalculusFieldElement<T>> void doTestCompleteVsIncompleteE(final Field<T> field) {
207         for (double m = 0.01; m < 1; m += 0.01) {
208             double complete   = LegendreEllipticIntegral.bigE(field.getZero().newInstance(m)).getReal();
209             double incomplete = LegendreEllipticIntegral.bigE(field.getZero().newInstance(MathUtils.SEMI_PI),
210                                                               field.getZero().newInstance(m)).getReal();
211             assertEquals(complete, incomplete, 4 * FastMath.ulp(complete));
212         }
213     }
214 
215     private <T extends CalculusFieldElement<T>> void doTestCompleteVsIncompleteD(final Field<T> field) {
216         for (double m = 0.01; m < 1; m += 0.01) {
217             double complete   = LegendreEllipticIntegral.bigD(field.getZero().newInstance(m)).getReal();
218             double incomplete = LegendreEllipticIntegral.bigD(field.getZero().newInstance(MathUtils.SEMI_PI),
219                                                               field.getZero().newInstance(m)).getReal();
220             assertEquals(complete, incomplete, FastMath.ulp(complete));
221         }
222     }
223 
224     private <T extends CalculusFieldElement<T>> void doTestCompleteVsIncompletePi(final Field<T> field) {
225         for (double alpha2 = 0.01; alpha2 < 1; alpha2 += 0.01) {
226             for (double m = 0.01; m < 1; m += 0.01) {
227                 double complete   = LegendreEllipticIntegral.bigPi(field.getZero().newInstance(alpha2),
228                                                                    field.getZero().newInstance(m)).getReal();
229                 double incomplete = LegendreEllipticIntegral.bigPi(field.getZero().newInstance(alpha2),
230                                                                    field.getZero().newInstance(MathUtils.SEMI_PI),
231                                                                    field.getZero().newInstance(m)).getReal();
232                 assertEquals(complete, incomplete, FastMath.ulp(complete));
233             }
234         }
235     }
236 
237     private <T extends CalculusFieldElement<T>> void doTestNomeMediumParameter(final Field<T> field) {
238         assertEquals(0.0857957337021947665168, LegendreEllipticIntegral.nome(field.getZero().newInstance(0.75)).getReal(), 1.0e-15);
239     }
240 
241     private <T extends CalculusFieldElement<T>> void doTestNomeSmallParameter(final Field<T> field) {
242         assertEquals(5.9375e-18, LegendreEllipticIntegral.nome(field.getZero().newInstance(0.95e-16)).getReal(), 1.0e-22);
243     }
244 
245     private <T extends CalculusFieldElement<T>> void doTestIntegralsSmallParameter(final Field<T> field) {
246         assertEquals(7.8539816428e-10,
247                             LegendreEllipticIntegral.bigK(field.getZero().newInstance(2.0e-9)).getReal() - MathUtils.SEMI_PI,
248                             1.0e-15);
249     }
250 
251     private <T extends CalculusFieldElement<T>> void doTestPrecomputedDelta(final Field<T> field) {
252 
253         T n   = field.getZero().newInstance(0.7);
254         T m   = field.getZero().newInstance(0.2);
255         T phi = field.getZero().newInstance(1.2);
256         T ref = field.getZero().newInstance(1.8264362537906997);
257         assertEquals(0.0, LegendreEllipticIntegral.bigPi(n, phi, m).subtract(ref).getReal(), 1.0e-15);
258 
259         // no argument reduction and no precomputed delta
260         final T csc     = phi.sin().reciprocal();
261         final T csc2    = csc.multiply(csc);
262         final T cM1     = csc2.subtract(1);
263         final T cMm     = csc2.subtract(m);
264         final T cMn     = csc2.subtract(n);
265         final T pinphim = CarlsonEllipticIntegral.rF(cM1, cMm, csc2).
266                           add(CarlsonEllipticIntegral.rJ(cM1, cMm, csc2, cMn).multiply(n).divide(3));
267         assertEquals(0.0, pinphim.subtract(ref).getReal(), 1.0e-15);
268 
269     }
270 
271     @Test
272     void testValueIndependentOfDerivatives() {
273         // the value of the integrals must not depend on the derivatives carried by the arguments
274         for (final double m : new double[] { 1.0e-300, 1.0e-12, 0.3, 0.9 }) {
275             final double refK = LegendreEllipticIntegral.bigK(m);
276             final double refE = LegendreEllipticIntegral.bigE(m);
277             final double refF = LegendreEllipticIntegral.bigF(0.7, m);
278             final Gradient k1 = LegendreEllipticIntegral.bigK(new Gradient(m, 1.0, 1.0));
279             final Gradient f1 = LegendreEllipticIntegral.bigF(new Gradient(0.7, 1.0, 0.0), new Gradient(m, 1.0, 1.0));
280             for (final double seed : new double[] { 0.0, 1.0, 1.0e3, 1.0e6 }) {
281                 final Gradient gm  = new Gradient(m, seed, seed);
282                 final Gradient phi = new Gradient(0.7, seed, 0.0);
283                 final Gradient k   = LegendreEllipticIntegral.bigK(gm);
284                 final Gradient f   = LegendreEllipticIntegral.bigF(phi, gm);
285                 assertEquals(refK, k.getValue(), 1.0e-15 * FastMath.abs(refK));
286                 assertEquals(refE, LegendreEllipticIntegral.bigE(gm).getValue(), 1.0e-15 * FastMath.abs(refE));
287                 assertEquals(refF, f.getValue(), 1.0e-15 * FastMath.abs(refF));
288                 // derivatives are linear in the seed: same iterations whatever the seed size
289                 assertEquals(seed * k1.getPartialDerivative(0), k.getPartialDerivative(0),
290                              1.0e-14 * FastMath.abs(seed * k1.getPartialDerivative(0)));
291                 assertEquals(seed * f1.getPartialDerivative(0), f.getPartialDerivative(0),
292                              1.0e-14 * FastMath.abs(seed * f1.getPartialDerivative(0)));
293             }
294         }
295     }
296 
297     @Test
298     void testNomeIndependentOfDerivatives() {
299         // small m selects the series expansion; the choice must not depend on the derivatives
300         for (final double m : new double[] { 1.0e-20, 1.0e-15 }) {
301             final double ref = LegendreEllipticIntegral.nome(new Gradient(m, 0.0, 0.0)).getValue();
302             for (final double seed : new double[] { 1.0, 1.0e3 }) {
303                 assertEquals(ref, LegendreEllipticIntegral.nome(new Gradient(m, seed, seed)).getValue(),
304                              1.0e-15 * FastMath.abs(ref));
305             }
306         }
307     }
308 
309 }