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.hansen;
18  
19  import org.hipparchus.analysis.polynomials.PolynomialFunction;
20  import org.hipparchus.util.FastMath;
21  
22  /**
23   * Hansen coefficients K(t,n,s) for t=0 and n > 0.
24   * <p>
25   * Implements Collins 4-254 or Danielson 2.7.3-(7) for Hansen Coefficients and
26   * Danielson 3.2-(3) for derivatives. The recursions are transformed into
27   * composition of linear transformations to obtain the associated polynomials
28   * for coefficients and their derivatives - see Petre's paper
29   *
30   * @author Petre Bazavan
31   * @author Lucian Barbulescu
32   */
33  public class HansenThirdBodyLinear {
34  
35      /** The number of coefficients that will be computed with a set of roots. */
36      private  static final int SLICE = 10;
37  
38      /**
39       * The first vector of polynomials associated to Hansen coefficients and
40       * derivatives.
41       */
42      private final PolynomialFunction[][] mpvec;
43  
44      /** The second vector of polynomials associated only to derivatives. */
45      private final PolynomialFunction[][] mpvecDeriv;
46  
47      /** The Hansen coefficients used as roots. */
48      private final double[][] hansenRoot;
49  
50      /** The derivatives of the Hansen coefficients used as roots. */
51      private final double[][] hansenDerivRoot;
52  
53      /** The number of slices needed to compute the coefficients. */
54      private final int numSlices;
55  
56      /** The s index. */
57      private final int s;
58  
59      /** (-1)<sup>s</sup> * (2*s + 1)!! / (s+1)!  */
60      private double twosp1dfosp1f;
61  
62      /** (-1)<sup>s</sup> * (2*s + 1)!! / (s+2)!  */
63      private final double twosp1dfosp2f;
64  
65      /** (-1)<sup>s</sup> * 2 * (2*s + 1)!! / (s+2)!  */
66      private final double two2sp1dfosp2f;
67  
68      /** (2*s + 3). */
69      private final double twosp3;
70  
71      /**
72       * Constructor.
73       *
74       * @param nMax the maximum value of n
75       * @param s the value of s
76       */
77      public HansenThirdBodyLinear(final int nMax, final int s) {
78  
79          // initialise fields
80          this.s = s;
81  
82          //Compute the fields that will be used to determine the initial values for the coefficients
83          this.twosp1dfosp1f = (s % 2 == 0) ? 1.0 : -1.0;
84          for (int i = s; i >= 1; i--) {
85              this.twosp1dfosp1f *= (2.0 * i + 1.0) / (i + 1.0);
86          }
87  
88          this.twosp1dfosp2f = this.twosp1dfosp1f / (s + 2.);
89          this.twosp3 = 2 * s + 3;
90          this.two2sp1dfosp2f = 2 * this.twosp1dfosp2f;
91  
92          // initialization of structures for stored data
93          mpvec = new PolynomialFunction[nMax + 1][];
94          mpvecDeriv = new PolynomialFunction[nMax + 1][];
95  
96          this.numSlices  = FastMath.max(1, (nMax - s + SLICE - 2) / SLICE);
97          hansenRoot      = new double[numSlices][2];
98          hansenDerivRoot = new double[numSlices][2];
99  
100         // Prepare the database of the associated polynomials
101         HansenUtilities.generateThirdBodyPolynomials(s, nMax, SLICE, s,
102                                                      mpvec, mpvecDeriv);
103 
104     }
105 
106     /**
107      * Compute the initial values (see Collins, 4-255, 4-256 and 4.259)
108      * <p>
109      * K₀<sup>s, s</sup> = (-1)<sup>s</sup> * ( (2*s+1)!! / (s+1)! )
110      * </p>
111      * <p>
112      * K₀<sup>s+1, s</sup> = (-1)<sup>s</sup> * ( (2*s+1)!! / (s+2)!
113      * ) * (2*s+3 - χ<sup>-2</sup>)
114      * </p>
115      * <p>
116      * dK₀<sup>s+1, s</sup> / dχ = = (-1)<sup>s</sup> * 2 * (
117      * (2*s+1)!! / (s+2)! ) * χ<sup>-3</sup>
118      * </p>
119      * @param chitm1 sqrt(1 - e²)
120      * @param chitm2 sqrt(1 - e²)²
121      * @param chitm3 sqrt(1 - e²)³
122      */
123     public void computeInitValues(final double chitm1, final double chitm2, final double chitm3) {
124         this.hansenRoot[0][0] = this.twosp1dfosp1f;
125         this.hansenRoot[0][1] = this.twosp1dfosp2f * (this.twosp3 - chitm2);
126         this.hansenDerivRoot[0][0] = 0;
127         this.hansenDerivRoot[0][1] = this.two2sp1dfosp2f * chitm3;
128 
129         for (int i = 1; i < numSlices; i++) {
130             for (int j = 0; j < 2; j++) {
131                 // Get the required polynomials
132                 final PolynomialFunction[] mv = mpvec[s + (i * SLICE) + j];
133                 final PolynomialFunction[] sv = mpvecDeriv[s + (i * SLICE) + j];
134 
135                 //Compute the root derivatives
136                 hansenDerivRoot[i][j] = mv[1].value(chitm1) * hansenDerivRoot[i - 1][1] +
137                                         mv[0].value(chitm1) * hansenDerivRoot[i - 1][0] +
138                                         sv[1].value(chitm1) * hansenRoot[i - 1][1] +
139                                         sv[0].value(chitm1) * hansenRoot[i - 1][0];
140 
141                 //Compute the root Hansen coefficients
142                 hansenRoot[i][j] =  mv[1].value(chitm1) * hansenRoot[i - 1][1] +
143                                     mv[0].value(chitm1) * hansenRoot[i - 1][0];
144             }
145         }
146     }
147 
148     /**
149      * Compute the value of the Hansen coefficient K₀<sup>n, s</sup>.
150      *
151      * @param n n value
152      * @param chitm1 χ<sup>-1</sup>
153      * @return the coefficient K₀<sup>n, s</sup>
154      */
155     public double getValue(final int n, final double chitm1) {
156         //Compute the potential slice
157         int sliceNo = (n - s) / SLICE;
158         if (sliceNo < numSlices) {
159             //Compute the index within the slice
160             final int indexInSlice = (n - s) % SLICE;
161 
162             //Check if a root must be returned
163             if (indexInSlice <= 1) {
164                 return hansenRoot[sliceNo][indexInSlice];
165             }
166         } else {
167             //the value was a potential root for a slice, but that slice was not required
168             //Decrease the slice number
169             sliceNo--;
170         }
171 
172         // Danielson 2.7.3-(6c)/Collins 4-242 and Petre's paper
173         final PolynomialFunction[] v = mpvec[n];
174         double ret = v[1].value(chitm1) * hansenRoot[sliceNo][1];
175         if (hansenRoot[sliceNo][0] != 0) {
176             ret += v[0].value(chitm1) * hansenRoot[sliceNo][0];
177         }
178 
179         return ret;
180 
181     }
182 
183     /**
184      * Compute the value of the Hansen coefficient dK₀<sup>n, s</sup> / d&Chi;.
185      *
186      * @param n n value
187      * @param chitm1 χ<sup>-1</sup>
188      * @return the coefficient dK₀<sup>n, s</sup> / d&Chi;
189      */
190     public double getDerivative(final int n, final double chitm1) {
191         //Compute the potential slice
192         int sliceNo = (n - s) / SLICE;
193         if (sliceNo < numSlices) {
194             //Compute the index within the slice
195             final int indexInSlice = (n - s) % SLICE;
196 
197             //Check if a root must be returned
198             if (indexInSlice <= 1) {
199                 return hansenDerivRoot[sliceNo][indexInSlice];
200             }
201         } else {
202             //the value was a potential root for a slice, but that slice was not required
203             //Decrease the slice number
204             sliceNo--;
205         }
206 
207         final PolynomialFunction[] v = mpvec[n];
208         double ret = v[1].value(chitm1) * hansenDerivRoot[sliceNo][1];
209         if (hansenDerivRoot[sliceNo][0] != 0) {
210             ret += v[0].value(chitm1) * hansenDerivRoot[sliceNo][0];
211         }
212 
213         // Danielson 2.7.3-(7c)/Collins 4-254 and Petre's paper
214         final PolynomialFunction[] v1 = mpvecDeriv[n];
215         ret += v1[1].value(chitm1) * hansenRoot[sliceNo][1];
216         if (hansenRoot[sliceNo][0] != 0) {
217             ret += v1[0].value(chitm1) * hansenRoot[sliceNo][0];
218         }
219         return ret;
220 
221     }
222 
223 }