GammaMnsFunction.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.fraction.BigFraction;
  19. import org.hipparchus.util.FastMath;

  20. import java.util.Arrays;

  21. /** Compute the &Gamma;<sup>m</sup><sub>n,s</sub>(γ) function from equation 2.7.1-(13).
  22.  *
  23.  *  @author Romain Di Costanzo
  24.  */
  25. public class GammaMnsFunction {

  26.     /** Factorial ratios. */
  27.     private static double[] PRECOMPUTED_RATIOS;

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

  30.     /** Storage array. */
  31.     private final double[] values;

  32.     /** 1 + I * γ. */
  33.     private final double opIg;

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

  36.     /** Simple constructor.
  37.      *  @param nMax max value for n
  38.      *  @param gamma γ
  39.      *  @param I retrograde factor
  40.      */
  41.     public GammaMnsFunction(final int nMax, final double gamma, final int I) {
  42.         final int size = (nMax + 1) * (nMax + 2) * (4 * nMax + 3) / 6;
  43.         this.values = new double[size];
  44.         this.ratios = getRatios(nMax, size);
  45.         Arrays.fill(values, Double.NaN);
  46.         this.opIg   = 1. + I * gamma;
  47.         this.I      = I;
  48.     }

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

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

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

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

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

  83.                 }

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

  89.             }
  90.             return PRECOMPUTED_RATIOS;
  91.         }
  92.     }

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

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

  127. }