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.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.fraction.BigFraction;
22  import org.hipparchus.util.FastMath;
23  import org.hipparchus.util.MathArrays;
24  
25  import java.util.Arrays;
26  
27  /** Compute the &Gamma;<sup>m</sup><sub>n,s</sub>(γ) function from equation 2.7.1-(13).
28   * @param <T> type of the field elements
29   */
30  public class FieldGammaMnsFunction <T extends CalculusFieldElement<T>> {
31  
32      /** Factorial ratios. */
33      private static double[] PRECOMPUTED_RATIOS;
34  
35      /** Factorial ratios. */
36      private final double[] ratios;
37  
38      /** Storage array. */
39      private final T[] values;
40  
41      /** 1 + I * γ. */
42      private final T opIg;
43  
44      /** I = +1 for a prograde orbit, -1 otherwise. */
45      private final int    I;
46  
47      /** Simple constructor.
48       *  @param nMax max value for n
49       *  @param gamma γ
50       *  @param I retrograde factor
51       *  @param field field element
52       */
53      public FieldGammaMnsFunction(final int nMax, final T gamma, final int I, final Field<T> field) {
54          final int size = (nMax + 1) * (nMax + 2) * (4 * nMax + 3) / 6;
55          this.values = MathArrays.buildArray(field, size);
56          this.ratios = getRatios(nMax, size);
57          Arrays.fill(values, field.getZero().add(Double.NaN));
58          this.opIg   = gamma.multiply(I).add(1.);
59          this.I      = I;
60      }
61  
62      /** Compute the array index.
63       *  @param m m
64       *  @param n n
65       *  @param s s
66       *  @return index for element m, n, s
67       */
68      private static int index(final int m, final int n, final int s) {
69          return n * (n + 1) * (4 * n - 1) / 6 + // index for 0, n, 0
70                 m * (2 * n + 1) +               // index for m, n, 0
71                 s + n;                          // index for m, n, s
72      }
73  
74      /** Get the ratios for the given size.
75       * @param nMax max value for n
76       * @param size ratio size array
77       * @return factorial ratios
78       */
79      private static double[] getRatios(final int nMax, final int size) {
80          synchronized (FieldGammaMnsFunction.class) {
81              if (PRECOMPUTED_RATIOS == null || PRECOMPUTED_RATIOS.length < size) {
82                  // we need to compute a larger reference array
83  
84                  final BigFraction[] bF = new BigFraction[size];
85                  for (int n = 0; n <= nMax; ++n) {
86  
87                      // populate ratios for s = 0
88                      bF[index(0, n, 0)] = BigFraction.ONE;
89                      for (int m = 1; m <= n; ++m) {
90                          bF[index(m, n, 0)] = bF[index(m - 1, n, 0)].multiply(n + m).divide(n - (m - 1));
91                      }
92  
93                      // populate ratios for s != 0
94                      for (int absS = 1; absS <= n; ++absS) {
95                          for (int m = 0; m <= n; ++m) {
96                              bF[index(m, n, +absS)] = bF[index(m, n, absS - 1)].divide(n + absS).multiply(n - (absS - 1));
97                              bF[index(m, n, -absS)] = bF[index(m, n, absS)];
98                          }
99                      }
100 
101                 }
102 
103                 // convert to double
104                 PRECOMPUTED_RATIOS = new double[size];
105                 for (int i = 0; i < bF.length; ++i) {
106                     PRECOMPUTED_RATIOS[i] = bF[i].doubleValue();
107                 }
108 
109             }
110             return PRECOMPUTED_RATIOS;
111         }
112     }
113 
114     /** Get &Gamma; function value.
115      *  @param m m
116      *  @param n n
117      *  @param s s
118      *  @return &Gamma;<sup>m</sup><sub>n, s</sub>(γ)
119      */
120     public T getValue(final int m, final int n, final int s) {
121         final int i = index(m, n, s);
122         if (Double.isNaN(values[i].getReal())) {
123             if (s <= -m) {
124                 values[i] = FastMath.scalb(FastMath.pow(opIg, -I * m), s).multiply(((m - s) & 0x1) == 0 ? +1 : -1);
125             } else if (s <= m) {
126                 values[i] = FastMath.scalb(FastMath.pow(opIg, I * s), -m).multiply(ratios[i]).multiply(((m - s) & 0x1) == 0 ? +1 : -1);
127             } else {
128                 values[i] = FastMath.scalb(FastMath.pow(opIg, I * m), -s);
129             }
130         }
131         return values[i];
132     }
133 
134     /** Get &Gamma; function derivative.
135      * @param m m
136      * @param n n
137      * @param s s
138      * @return d&Gamma;<sup>m</sup><sub>n,s</sub>(γ)/dγ
139      */
140     public T getDerivative(final int m, final int n, final int s) {
141         if (s <= -m) {
142             return getValue(m, n, s).multiply(I).multiply(-m).divide(opIg);
143         } else if (s >= m) {
144             return getValue(m, n, s).multiply(I).multiply(m).divide(opIg);
145         } else {
146             return getValue(m, n, s).multiply(I).multiply(s).divide(opIg);
147         }
148     }
149 
150 }