FieldGammaMnsFunction.java

  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. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.fraction.BigFraction;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathArrays;

  23. import java.util.Arrays;

  24. /** Compute the &Gamma;<sup>m</sup><sub>n,s</sub>(γ) function from equation 2.7.1-(13).
  25.  * @param <T> type of the field elements
  26.  */
  27. public class FieldGammaMnsFunction <T extends CalculusFieldElement<T>> {

  28.     /** Factorial ratios. */
  29.     private static double[] PRECOMPUTED_RATIOS;

  30.     /** Factorial ratios. */
  31.     private final double[] ratios;

  32.     /** Storage array. */
  33.     private final T[] values;

  34.     /** 1 + I * γ. */
  35.     private final T opIg;

  36.     /** I = +1 for a prograde orbit, -1 otherwise. */
  37.     private final int    I;

  38.     /** Simple constructor.
  39.      *  @param nMax max value for n
  40.      *  @param gamma γ
  41.      *  @param I retrograde factor
  42.      *  @param field field element
  43.      */
  44.     public FieldGammaMnsFunction(final int nMax, final T gamma, final int I, final Field<T> field) {
  45.         final int size = (nMax + 1) * (nMax + 2) * (4 * nMax + 3) / 6;
  46.         this.values = MathArrays.buildArray(field, size);
  47.         this.ratios = getRatios(nMax, size);
  48.         Arrays.fill(values, field.getZero().add(Double.NaN));
  49.         this.opIg   = gamma.multiply(I).add(1.);
  50.         this.I      = I;
  51.     }

  52.     /** Compute the array index.
  53.      *  @param m m
  54.      *  @param n n
  55.      *  @param s s
  56.      *  @return index for element m, n, s
  57.      */
  58.     private static int index(final int m, final int n, final int s) {
  59.         return n * (n + 1) * (4 * n - 1) / 6 + // index for 0, n, 0
  60.                m * (2 * n + 1) +               // index for m, n, 0
  61.                s + n;                          // index for m, n, s
  62.     }

  63.     /** Get the ratios for the given size.
  64.      * @param nMax max value for n
  65.      * @param size ratio size array
  66.      * @return factorial ratios
  67.      */
  68.     private static double[] getRatios(final int nMax, final int size) {
  69.         synchronized (FieldGammaMnsFunction.class) {
  70.             if (PRECOMPUTED_RATIOS == null || PRECOMPUTED_RATIOS.length < size) {
  71.                 // we need to compute a larger reference array

  72.                 final BigFraction[] bF = new BigFraction[size];
  73.                 for (int n = 0; n <= nMax; ++n) {

  74.                     // populate ratios for s = 0
  75.                     bF[index(0, n, 0)] = BigFraction.ONE;
  76.                     for (int m = 1; m <= n; ++m) {
  77.                         bF[index(m, n, 0)] = bF[index(m - 1, n, 0)].multiply(n + m).divide(n - (m - 1));
  78.                     }

  79.                     // populate ratios for s != 0
  80.                     for (int absS = 1; absS <= n; ++absS) {
  81.                         for (int m = 0; m <= n; ++m) {
  82.                             bF[index(m, n, +absS)] = bF[index(m, n, absS - 1)].divide(n + absS).multiply(n - (absS - 1));
  83.                             bF[index(m, n, -absS)] = bF[index(m, n, absS)];
  84.                         }
  85.                     }

  86.                 }

  87.                 // convert to double
  88.                 PRECOMPUTED_RATIOS = new double[size];
  89.                 for (int i = 0; i < bF.length; ++i) {
  90.                     PRECOMPUTED_RATIOS[i] = bF[i].doubleValue();
  91.                 }

  92.             }
  93.             return PRECOMPUTED_RATIOS;
  94.         }
  95.     }

  96.     /** Get &Gamma; function value.
  97.      *  @param m m
  98.      *  @param n n
  99.      *  @param s s
  100.      *  @return &Gamma;<sup>m</sup><sub>n, s</sub>(γ)
  101.      */
  102.     public T getValue(final int m, final int n, final int s) {
  103.         final int i = index(m, n, s);
  104.         if (Double.isNaN(values[i].getReal())) {
  105.             if (s <= -m) {
  106.                 values[i] = FastMath.scalb(FastMath.pow(opIg, -I * m), s).multiply(((m - s) & 0x1) == 0 ? +1 : -1);
  107.             } else if (s <= m) {
  108.                 values[i] = FastMath.scalb(FastMath.pow(opIg, I * s), -m).multiply(ratios[i]).multiply(((m - s) & 0x1) == 0 ? +1 : -1);
  109.             } else {
  110.                 values[i] = FastMath.scalb(FastMath.pow(opIg, I * m), -s);
  111.             }
  112.         }
  113.         return values[i];
  114.     }

  115.     /** Get &Gamma; function derivative.
  116.      * @param m m
  117.      * @param n n
  118.      * @param s s
  119.      * @return d&Gamma;<sup>m</sup><sub>n,s</sub>(γ)/dγ
  120.      */
  121.     public T getDerivative(final int m, final int n, final int s) {
  122.         if (s <= -m) {
  123.             return getValue(m, n, s).multiply(I).multiply(-m).divide(opIg);
  124.         } else if (s >= m) {
  125.             return getValue(m, n, s).multiply(I).multiply(m).divide(opIg);
  126.         } else {
  127.             return getValue(m, n, s).multiply(I).multiply(s).divide(opIg);
  128.         }
  129.     }

  130. }