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 java.util.SortedMap;
20  
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.Field;
23  import org.hipparchus.util.MathArrays;
24  import org.orekit.propagation.semianalytical.dsst.utilities.CoefficientsFactory.NSKey;
25  
26  /** Compute the L<sub>n</sub><sup>s</sup>(γ).
27   *  <p>
28   *  The fomula used is: <br>
29   *  L<sub>n</sub><sup>s</sup>(γ) = ( R / a )<sup>n</sup>V<sub>ns</sub>Q<sup>ns</sup>(γ)
30   *  </p>
31   *  @author Lucian Barbulescu
32   * @param <T> type of the field elements
33   */
34  public class FieldLnsCoefficients <T extends CalculusFieldElement<T>> {
35  
36      /** The coefficients L<sub>n</sub><sup>s</sup>(γ). */
37      private final T[][] lns;
38  
39      /** The coefficients dL<sub>n</sub><sup>s</sup>(γ) / dγ. */
40      private final T[][] dlns;
41  
42      /** Create a set of L<sub>n</sub><sup>s</sup>(γ) coefficients.
43      *
44      * @param nMax maximum value for n
45      * @param sMax maximum value for s
46      * @param Qns the Q<sup>ns</sup>(γ) coefficients
47      * @param Vns the V<sub>ns</sub> coefficients
48      * @param roa (R / a)
49      * @param field field used by default
50      */
51      public FieldLnsCoefficients(final int nMax, final int sMax,
52                                  final T[][] Qns, final SortedMap<NSKey, Double> Vns, final T roa,
53                                  final Field<T> field) {
54          final T zero      = field.getZero();
55          final int rows    = nMax + 1;
56          final int columns = sMax + 1;
57          this.lns          = MathArrays.buildArray(field, rows, columns);
58          this.dlns         = MathArrays.buildArray(field, rows, columns);
59  
60          final T[] roaPow = MathArrays.buildArray(field, rows);
61          roaPow[0] = zero.newInstance(1.);
62          for (int i = 1; i <= nMax; i++) {
63              roaPow[i] = roa.multiply(roaPow[i - 1]);
64          }
65          for (int s = 0; s <= sMax; s++) {
66              for (int n = s; n <= nMax; n++) {
67                  // if (n - s) is not even L<sub>n</sub><sup>s</sup>(γ) is 0
68                  if ((n - s) % 2 == 0) {
69                      final T coef = roaPow[n].multiply(Vns.get(new NSKey(n, s)));
70                      lns[n][s] = coef.multiply(Qns[n][s]);
71                      if ( n == s) {
72                          // if n == s the derivative is 0 because Q[n][s+1] == Q[n][n+1] is 0
73                          dlns[n][s] = zero;
74                      } else {
75                          dlns[n][s] = coef.multiply(Qns[n][s + 1]);
76                      }
77                  } else {
78                      lns[n][s]  = zero;
79                      dlns[n][s] = zero;
80                  }
81              }
82          }
83  
84      }
85  
86     /**Get the value of L<sub>n</sub><sup>s</sup>(γ).
87      *
88      * @param n n index
89      * @param s s index
90      * @return L<sub>n</sub><sup>s</sup>(γ)
91      */
92      public T getLns(final int n, final int s) {
93          return lns[n][s];
94      }
95  
96     /**Get the value of dL<sub>n</sub><sup>s</sup> / dγ (γ).
97      *
98      * @param n n index
99      * @param s s index
100     * @return L<sub>n</sub><sup>s</sup>(γ)
101     */
102     public T getdLnsdGamma(final int n, final int s) {
103         return dlns[n][s];
104     }
105 }