JacobiPolynomials.java

  1. /* Copyright 2002-2022 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.PolynomialFunction;
  26. import org.hipparchus.analysis.polynomials.PolynomialsUtils;
  27. import org.orekit.propagation.semianalytical.dsst.forces.DSSTThirdBody;

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

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

  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<PolynomialFunction>());
  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.     /** Inner class for Jacobi polynomials keys.
  151.      * <p>
  152.      * Please note that this class is not original content but is a copy from the
  153.      * Hipparchus library. This library is published under the
  154.      * Apache License, version 2.0.
  155.      * </p>
  156.      *
  157.      * @see org.hipparchus.analysis.polynomials.PolynomialsUtils
  158.      */
  159.     private static class JacobiKey {

  160.         /** First exponent. */
  161.         private final int v;

  162.         /** Second exponent. */
  163.         private final int w;

  164.         /** Simple constructor.
  165.          * @param v first exponent
  166.          * @param w second exponent
  167.          */
  168.         JacobiKey(final int v, final int w) {
  169.             this.v = v;
  170.             this.w = w;
  171.         }

  172.         /** Get hash code.
  173.          * @return hash code
  174.          */
  175.         @Override
  176.         public int hashCode() {
  177.             return (v << 16) ^ w;
  178.         }

  179.         /** Check if the instance represent the same key as another instance.
  180.          * @param key other key
  181.          * @return true if the instance and the other key refer to the same polynomial
  182.          */
  183.         @Override
  184.         public boolean equals(final Object key) {

  185.             if (!(key instanceof JacobiKey)) {
  186.                 return false;
  187.             }

  188.             final JacobiKey otherK = (JacobiKey) key;
  189.             return v == otherK.v && w == otherK.w;

  190.         }
  191.     }
  192. }