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.jacobi;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.analysis.differentiation.Gradient;
22  import org.hipparchus.special.elliptic.legendre.LegendreEllipticIntegral;
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  class FieldJacobiThetaTest {
32  
33      @Test
34      void testNoConvergence() {
35          doTestNoConvergence(Binary64Field.getInstance());
36      }
37  
38      private <T extends CalculusFieldElement<T>> void doTestNoConvergence(Field<T> field) {
39          assertTrue(new FieldJacobiTheta<>(field.getZero().newInstance(Double.NaN)).values(field.getZero()).theta1().isNaN());
40      }
41  
42      @Test
43      void testRealZero() {
44          doTestRealZero(Binary64Field.getInstance());
45      }
46  
47      private <T extends CalculusFieldElement<T>> void doTestRealZero(Field<T> field) {
48          final T k      = field.getZero().newInstance(0.675);
49          final T m      = k.multiply(k);
50          final T q      = LegendreEllipticIntegral.nome(m);
51          final T t3Ref  = field.getOne().add(q.
52                                              add(FastMath.pow(q, 4)).
53                                              add(FastMath.pow(q, 9)).
54                                              add(FastMath.pow(q, 16)).twice());
55          final T theta3 = new FieldJacobiTheta<>(q).values(field.getZero()).theta3();
56          assertEquals(t3Ref.getReal(), theta3.getReal(), 1.0e-12);
57      }
58  
59      @Test
60      void testQuarterPeriod() {
61          doTestQuarterPeriod(Binary64Field.getInstance());
62      }
63  
64      private <T extends CalculusFieldElement<T>> void doTestQuarterPeriod(Field<T> field) {
65          final T k      = field.getZero().newInstance(0.675);
66          final T m      = k.multiply(k);
67          final T q      = LegendreEllipticIntegral.nome(m);
68          final T theta3 = new FieldJacobiTheta<>(q).values(field.getZero()).theta3();
69          assertEquals(LegendreEllipticIntegral.bigK(m).getReal(),
70                              theta3.multiply(theta3).multiply(MathUtils.SEMI_PI).getReal(),
71                              1.0e-12);
72      }
73  
74      @Test
75      void testEllipticFunctions() {
76          doTestEllipticFunctions(Binary64Field.getInstance());
77      }
78  
79      private <T extends CalculusFieldElement<T>> void doTestEllipticFunctions(Field<T> field) {
80  
81          final T                   z      = field.getZero().newInstance(1.3);
82          final T                   k      = field.getZero().newInstance(0.675);
83          final T                   m      = k.multiply(k);
84          final T                   q      =  LegendreEllipticIntegral.nome(m);
85          final T                   bigK   = LegendreEllipticIntegral.bigK(m);
86          final T                   zeta   = z.divide(bigK).multiply(MathUtils.SEMI_PI);
87          final FieldJacobiTheta<T> jt     = new FieldJacobiTheta<>(q);
88          final FieldTheta<T>       theta0 = jt.values(field.getZero());
89          final FieldTheta<T>       thetaZ = jt.values(zeta);
90  
91          // the theta functions are related to the elliptic functions
92          // see https://dlmf.nist.gov/22.2
93          final FieldJacobiElliptic<T> je = JacobiEllipticBuilder.build(k.multiply(k));
94          final FieldCopolarN<T> valuesN = je.valuesN(z);
95          final FieldCopolarD<T> valuesD = je.valuesD(z);
96          final FieldCopolarC<T> valuesC = je.valuesC(z);
97          final T t02 = theta0.theta2();
98          final T t03 = theta0.theta3();
99          final T t04 = theta0.theta4();
100         final T tz1 = thetaZ.theta1();
101         final T tz2 = thetaZ.theta2();
102         final T tz3 = thetaZ.theta3();
103         final T tz4 = thetaZ.theta4();
104         assertEquals(valuesN.sn().getReal(), t03.multiply(tz1)              .divide(t02.multiply(tz4)).getReal(),               3.0e-15);
105         assertEquals(valuesN.cn().getReal(), t04.multiply(tz2)              .divide(t02.multiply(tz4)).getReal(),               3.0e-15);
106         assertEquals(valuesN.dn().getReal(), t04.multiply(tz3)              .divide(t03.multiply(tz4)).getReal(),               3.0e-15);
107         assertEquals(valuesD.sd().getReal(), t03.multiply(t03).multiply(tz1).divide(t02.multiply(t04).multiply(tz3)).getReal(), 3.0e-15);
108         assertEquals(valuesD.cd().getReal(), t03.multiply(tz2)              .divide(t02.multiply(tz3)).getReal(),               3.0e-15);
109         assertEquals(valuesC.sc().getReal(), t03.multiply(tz1)              .divide(t04.multiply(tz2)).getReal(),               3.0e-15);
110 
111     }
112 
113     @Test
114     void testValueIndependentOfDerivatives() {
115         // the value of theta functions must not depend on the derivatives carried by the arguments
116         final FieldTheta<Gradient> ref = new FieldJacobiTheta<>(new Gradient(0.3, 0.0, 0.0)).values(new Gradient(0.4, 0.0, 0.0));
117         for (final double seed : new double[] { 1.0, 1.0e3, 1.0e6 }) {
118             final FieldTheta<Gradient> t = new FieldJacobiTheta<>(new Gradient(0.3, seed, 0.0)).values(new Gradient(0.4, 0.0, seed));
119             assertEquals(ref.theta1().getValue(), t.theta1().getValue(), 1.0e-15 * FastMath.abs(ref.theta1().getValue()));
120             assertEquals(ref.theta2().getValue(), t.theta2().getValue(), 1.0e-15 * FastMath.abs(ref.theta2().getValue()));
121             assertEquals(ref.theta3().getValue(), t.theta3().getValue(), 1.0e-15 * FastMath.abs(ref.theta3().getValue()));
122             assertEquals(ref.theta4().getValue(), t.theta4().getValue(), 1.0e-15 * FastMath.abs(ref.theta4().getValue()));
123         }
124     }
125 
126 }