1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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    *   http://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.orekit.propagation.semianalytical.dsst.utilities;
18  
19  import org.hipparchus.complex.Complex;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /** Compute the S<sub>j</sub>(k, h) and the C<sub>j</sub>(k, h) series
25   *  and their partial derivatives with respect to k and h.
26   *  <p>
27   *  Those series are given in Danielson paper by expression 2.5.3-(5):
28   *
29   *  <p> C<sub>j</sub>(k, h) + i S<sub>j</sub>(k, h) = (k+ih)<sup>j</sup>
30   *
31   *  <p>
32   *  The C<sub>j</sub>(k, h) and the S<sub>j</sub>(k, h) elements are store as an
33   *  {@link ArrayList} of {@link Complex} number, the C<sub>j</sub>(k, h) being
34   *  represented by the real and the S<sub>j</sub>(k, h) by the imaginary part.
35   */
36  public class CjSjCoefficient {
37  
38      /** Last computed order j. */
39      private int jLast;
40  
41      /** Complex base (k + ih) of the C<sub>j</sub>, S<sub>j</sub> series. */
42      private final Complex kih;
43  
44      /** List of computed elements. */
45      private final List<Complex> cjsj;
46  
47      /** C<sub>j</sub>(k, h) and S<sub>j</sub>(k, h) constructor.
48       * @param k k value
49       * @param h h value
50       */
51      public CjSjCoefficient(final double k, final double h) {
52          kih  = new Complex(k, h);
53          cjsj = new ArrayList<>();
54          cjsj.add(new Complex(1, 0));
55          cjsj.add(kih);
56          jLast = 1;
57      }
58  
59      /** Get the C<sub>j</sub> coefficient.
60       * @param j order
61       * @return C<sub>j</sub>
62       */
63      public double getCj(final int j) {
64          if (j > jLast) {
65              // Update to order j
66              updateCjSj(j);
67          }
68          return cjsj.get(j).getReal();
69      }
70  
71      /** Get the S<sub>j</sub> coefficient.
72       * @param j order
73       * @return S<sub>j</sub>
74       */
75      public double getSj(final int j) {
76          if (j > jLast) {
77              // Update to order j
78              updateCjSj(j);
79          }
80          return cjsj.get(j).getImaginary();
81      }
82  
83      /** Get the dC<sub>j</sub> / dk coefficient.
84       * @param j order
85       * @return dC<sub>j</sub> / d<sub>k</sub>
86       */
87      public double getDcjDk(final int j) {
88          return j == 0 ? 0 : j * getCj(j - 1);
89      }
90  
91      /** Get the dS<sub>j</sub> / dk coefficient.
92       * @param j order
93       * @return dS<sub>j</sub> / d<sub>k</sub>
94       */
95      public double getDsjDk(final int j) {
96          return j == 0 ? 0 : j * getSj(j - 1);
97      }
98  
99      /** Get the dC<sub>j</sub> / dh coefficient.
100      * @param j order
101      * @return dC<sub>i</sub> / d<sub>k</sub>
102      */
103     public double getDcjDh(final int j) {
104         return j == 0 ? 0 : -j * getSj(j - 1);
105     }
106 
107     /** Get the dS<sub>j</sub> / dh coefficient.
108      * @param j order
109      * @return dS<sub>j</sub> / d<sub>h</sub>
110      */
111     public double getDsjDh(final int j) {
112         return j == 0 ? 0 : j * getCj(j - 1);
113     }
114 
115     /** Update the cjsj up to order j.
116      * @param j order
117      */
118     private void updateCjSj(final int j) {
119         Complex last = cjsj.get(cjsj.size() - 1);
120         for (int i = jLast; i < j; i++) {
121             final Complex next = last.multiply(kih);
122             cjsj.add(next);
123             last = next;
124         }
125         jLast = j;
126     }
127 }