JacobiPolynomials.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 java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.analysis.differentiation.FieldGradient;
  24. import org.hipparchus.analysis.differentiation.Gradient;
  25. import org.hipparchus.analysis.polynomials.JacobiKey;
  26. import org.hipparchus.analysis.polynomials.PolynomialFunction;
  27. import org.hipparchus.analysis.polynomials.PolynomialsUtils;
  28. import org.orekit.propagation.semianalytical.dsst.forces.DSSTThirdBody;

  29. /** Provider of the Jacobi polynomials P<sub>l</sub><sup>v,w</sup>.
  30.  * <p>
  31.  * This class is used for {@link
  32.  * org.orekit.propagation.semianalytical.dsst.forces.DSSTTesseral
  33.  * tesseral contribution} computation and {@link DSSTThirdBody}.
  34.  * </p>
  35.  *
  36.  * @author Nicolas Bernard
  37.  * @since 6.1
  38.  */
  39. public class JacobiPolynomials {

  40.     /** Storage map. */
  41.     private static final Map<JacobiKey, List<PolynomialFunction>> MAP = new HashMap<>();

  42.     /** Private constructor as class is a utility. */
  43.     private JacobiPolynomials() {
  44.     }

  45.     /** Returns the value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup> evaluated at γ.
  46.      * <p>This method is guaranteed to be thread-safe
  47.      * <p>It was added to improve performances of DSST propagation with tesseral gravity field or third-body perturbations.
  48.      * <p>See issue <a href="https://gitlab.orekit.org/orekit/orekit/-/issues/1098">1098</a>.
  49.      * <p>It appeared the "Gradient" version was degrading performances. This last was however kept for validation purposes.
  50.      * @param l degree of the polynomial
  51.      * @param v v value
  52.      * @param w w value
  53.      * @param x x value
  54.      * @return value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup>(γ)
  55.      * @since 11.3.3
  56.      */
  57.     public static double[] getValueAndDerivative(final int l, final int v, final int w, final double x) {
  58.         // compute value and derivative
  59.         return getValueAndDerivative(computePolynomial(l, v, w), x);
  60.     }

  61.     /** Get value and 1st-order of a mono-variate polynomial.
  62.      *
  63.      * <p> This method was added to improve performances of DSST propagation with tesseral gravity field or third-body perturbations.
  64.      * <p> See issue <a href="https://gitlab.orekit.org/orekit/orekit/-/issues/1098">1098</a>.
  65.      * @param polynomial polynomial to evaluate
  66.      * @param x value to evaluate on
  67.      * @return value and 1s-order derivative as a double array
  68.      * @since 11.3.3
  69.      */
  70.     private static double[] getValueAndDerivative(final PolynomialFunction polynomial, final double x) {

  71.         // Polynomial coefficients
  72.         final double[] coefficients = polynomial.getCoefficients();

  73.         // Degree of the polynomial
  74.         final int degree = polynomial.degree();

  75.         // Initialize value and 1st-order derivative
  76.         double value      = coefficients[degree];
  77.         double derivative = value * degree;
  78.         for (int j = degree - 1; j >= 1; j--) {

  79.             // Increment both value and derivative
  80.             final double coef = coefficients[j];
  81.             value        = value      * x +  coef;
  82.             derivative   = derivative * x +  coef * j;
  83.         }
  84.         // If degree > 0, perform last operation
  85.         if (degree > 0) {
  86.             value = value * x + coefficients[0];
  87.         }

  88.         // Return value and 1st-order derivative as double array
  89.         return new double[] {value, derivative};
  90.     }

  91.     /** Returns the value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup> evaluated at γ.
  92.      *
  93.      * <p>This method is guaranteed to be thread-safe
  94.      * <p>It's not used in the code anymore, see {@link #getValueAndDerivative(int, int, int, double)}, but was kept for validation purpose.
  95.      * @param l degree of the polynomial
  96.      * @param v v value
  97.      * @param w w value
  98.      * @param gamma γ value
  99.      * @return value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup>(γ)
  100.      * @since 10.2
  101.      */
  102.     public static Gradient getValue(final int l, final int v, final int w, final Gradient gamma) {
  103.         // compute value and derivative
  104.         return computePolynomial(l, v, w).value(gamma);
  105.     }

  106.     /** Returns the value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup> evaluated at γ.
  107.      * <p>
  108.      * This method is guaranteed to be thread-safe
  109.      * </p>
  110.      * @param <T> the type of the field elements
  111.      * @param l degree of the polynomial
  112.      * @param v v value
  113.      * @param w w value
  114.      * @param gamma γ value
  115.      * @return value and derivatives of the Jacobi polynomial P<sub>l</sub><sup>v,w</sup>(γ)
  116.      * @since 10.2
  117.      */
  118.     public static <T extends CalculusFieldElement<T>> FieldGradient<T> getValue(final int l, final int v, final int w,
  119.                                                                                 final FieldGradient<T> gamma) {
  120.         // compute value and derivative
  121.         return computePolynomial(l, v, w).value(gamma);

  122.     }

  123.     /** Initializes the polynomial to evalutate.
  124.      * @param l degree of the polynomial
  125.      * @param v v value
  126.      * @param w w value
  127.      * @return the polynomial to evaluate
  128.      */
  129.     private static PolynomialFunction computePolynomial(final int l, final int v, final int w) {
  130.         final List<PolynomialFunction> polyList;
  131.         synchronized (MAP) {

  132.             final JacobiKey key = new JacobiKey(v, w);

  133.             // Check the existence of the corresponding key in the map.
  134.             if (!MAP.containsKey(key)) {
  135.                 MAP.put(key, new ArrayList<>());
  136.             }

  137.             polyList = MAP.get(key);

  138.         }

  139.         final PolynomialFunction polynomial;
  140.         synchronized (polyList) {
  141.             // If the l-th degree polynomial has not been computed yet, the polynomials
  142.             // up to this degree are computed.
  143.             for (int degree = polyList.size(); degree <= l; degree++) {
  144.                 polyList.add(degree, PolynomialsUtils.createJacobiPolynomial(degree, v, w));
  145.             }
  146.             polynomial = polyList.get(l);
  147.         }

  148.         return polynomial;
  149.     }
  150. }