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