NewcombOperators.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.analysis.polynomials.PolynomialFunction;
  19. import org.hipparchus.util.FastMath;

  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.SortedMap;
  24. import java.util.TreeMap;

  25. /**
  26.  * Implementation of the Modified Newcomb Operators.
  27.  *
  28.  *  <p> From equations 2.7.3 - (12)(13) of the Danielson paper, those operators
  29.  *  are defined as:
  30.  *
  31.  *  <p>
  32.  *  4(ρ + σ)Y<sub>ρ,σ</sub><sup>n,s</sup> = <br>
  33.  *     2(2s - n)Y<sub>ρ-1,σ</sub><sup>n,s+1</sup> + (s - n)Y<sub>ρ-2,σ</sub><sup>n,s+2</sup> <br>
  34.  *   - 2(2s + n)Y<sub>ρ,σ-1</sub><sup>n,s-1</sup> - (s+n)Y<sub>ρ,σ-2</sub><sup>n,s-2</sup> <br>
  35.  *   + 2(2ρ + 2σ + 2 + 3n)Y<sub>ρ-1,σ-1</sub><sup>n,s</sup>
  36.  *
  37.  *  <p> Initialization is given by : Y<sub>0,0</sub><sup>n,s</sup> = 1
  38.  *
  39.  *  <p> Internally, the Modified Newcomb Operators are stored as an array of
  40.  *  {@link PolynomialFunction} :
  41.  *
  42.  *  <p> Y<sub>ρ,σ</sub><sup>n,s</sup> = P<sub>k0</sub> + P<sub>k1</sub>n + ... +
  43.  *  P<sub>kj</sub>n<sup>j</sup>
  44.  *
  45.  * <p> where the P<sub>kj</sub> are given by
  46.  *
  47.  * <p> P<sub>kj</sub> = ∑<sub>j=0;ρ</sub> a<sub>j</sub>s<sup>j</sup>
  48.  *
  49.  * @author Romain Di Costanzo
  50.  * @author Pascal Parraud
  51.  */
  52. public class NewcombOperators {

  53.     /** Storage map. */
  54.     private static final Map<NewKey, Double> MAP = new TreeMap<>((k1, k2) -> {
  55.         if (k1.n == k2.n) {
  56.             if (k1.s == k2.s) {
  57.                 if (k1.rho == k2.rho) {
  58.                     return Integer.compare(k1.sigma, k2.sigma);
  59.                 } else if (k1.rho < k2.rho) {
  60.                     return -1;
  61.                 } else {
  62.                     return 1;
  63.                 }
  64.             } else if (k1.s < k2.s) {
  65.                 return -1;
  66.             } else {
  67.                 return 1;
  68.             }
  69.         } else if (k1.n < k2.n) {
  70.             return -1;
  71.         }
  72.         return 1;
  73.     });

  74.     /** Private constructor as class is a utility.
  75.      */
  76.     private NewcombOperators() {
  77.     }

  78.     /** Get the Newcomb operator evaluated at n, s, ρ, σ.
  79.      * <p>
  80.      * This method is guaranteed to be thread-safe
  81.      * </p>
  82.      *  @param rho ρ index
  83.      *  @param sigma σ index
  84.      *  @param n n index
  85.      *  @param s s index
  86.      *  @return Y<sub>ρ,σ</sub><sup>n,s</sup>
  87.      */
  88.     public static double getValue(final int rho, final int sigma, final int n, final int s) {

  89.         final NewKey key = new NewKey(n, s, rho, sigma);
  90.         synchronized (MAP) {
  91.             if (MAP.containsKey(key)) {
  92.                 return MAP.get(key);
  93.             }
  94.         }

  95.         // Get the Newcomb polynomials for the given rho and sigma
  96.         final List<PolynomialFunction> polynomials = PolynomialsGenerator.getPolynomials(rho, sigma);

  97.         // Compute the value from the list of polynomials for the given n and s
  98.         double nPower = 1.;
  99.         double value = 0.0;
  100.         for (final PolynomialFunction polynomial : polynomials) {
  101.             value += polynomial.value(s) * nPower;
  102.             nPower = n * nPower;
  103.         }
  104.         synchronized (MAP) {
  105.             MAP.put(key, value);
  106.         }

  107.         return value;

  108.     }

  109.     /** Generator for Newcomb polynomials. */
  110.     private static class PolynomialsGenerator {

  111.         /** Polynomials storage. */
  112.         private static final SortedMap<Couple, List<PolynomialFunction>> POLYNOMIALS =
  113.                 new TreeMap<>((c1, c2) -> {
  114.                     if (c1.rho == c2.rho) {
  115.                         if (c1.sigma < c2.sigma) {
  116.                             return -1;
  117.                         } else if (c1.sigma == c2.sigma) {
  118.                             return 0;
  119.                         }
  120.                     } else if (c1.rho < c2.rho) {
  121.                         return -1;
  122.                     }
  123.                     return 1;
  124.                 });

  125.         /** Private constructor as class is a utility.
  126.          */
  127.         private PolynomialsGenerator() {
  128.         }

  129.         /** Get the list of polynomials representing the Newcomb Operator for the (ρ,σ) couple.
  130.          * <p>
  131.          * This method is guaranteed to be thread-safe
  132.          * </p>
  133.          *  @param rho ρ value
  134.          *  @param sigma σ value
  135.          *  @return Polynomials representing the Newcomb Operator for the (ρ,σ) couple.
  136.          */
  137.         private static List<PolynomialFunction> getPolynomials(final int rho, final int sigma) {

  138.             final Couple couple = new Couple(rho, sigma);

  139.             synchronized (POLYNOMIALS) {

  140.                 if (POLYNOMIALS.isEmpty()) {
  141.                     // Initialize lists
  142.                     final List<PolynomialFunction> l00 = new ArrayList<>();
  143.                     final List<PolynomialFunction> l01 = new ArrayList<>();
  144.                     final List<PolynomialFunction> l10 = new ArrayList<>();
  145.                     final List<PolynomialFunction> l11 = new ArrayList<>();

  146.                     // Y(rho = 0, sigma = 0) = 1
  147.                     l00.add(new PolynomialFunction(new double[] {
  148.                         1.
  149.                     }));
  150.                     // Y(rho = 0, sigma = 1) =  -s - n/2
  151.                     l01.add(new PolynomialFunction(new double[] {
  152.                         0, -1.
  153.                     }));
  154.                     l01.add(new PolynomialFunction(new double[] {
  155.                         -0.5
  156.                     }));
  157.                     // Y(rho = 1, sigma = 0) =  s - n/2
  158.                     l10.add(new PolynomialFunction(new double[] {
  159.                         0, 1.
  160.                     }));
  161.                     l10.add(new PolynomialFunction(new double[] {
  162.                         -0.5
  163.                     }));
  164.                     // Y(rho = 1, sigma = 1) = 3/2 - s² + 5n/4 + n²/4
  165.                     l11.add(new PolynomialFunction(new double[] {
  166.                         1.5, 0., -1.
  167.                     }));
  168.                     l11.add(new PolynomialFunction(new double[] {
  169.                         1.25
  170.                     }));
  171.                     l11.add(new PolynomialFunction(new double[] {
  172.                         0.25
  173.                     }));

  174.                     // Initialize polynomials
  175.                     POLYNOMIALS.put(new Couple(0, 0), l00);
  176.                     POLYNOMIALS.put(new Couple(0, 1), l01);
  177.                     POLYNOMIALS.put(new Couple(1, 0), l10);
  178.                     POLYNOMIALS.put(new Couple(1, 1), l11);

  179.                 }

  180.                 // If order hasn't been computed yet, update the Newcomb polynomials
  181.                 if (!POLYNOMIALS.containsKey(couple)) {
  182.                     PolynomialsGenerator.computeFor(rho, sigma);
  183.                 }

  184.                 return POLYNOMIALS.get(couple);

  185.             }
  186.         }

  187.         /** Compute the Modified Newcomb Operators up to a given (ρ, σ) couple.
  188.          *  <p>
  189.          *  The recursive computation uses equation 2.7.3-(12) of the Danielson paper.
  190.          *  </p>
  191.          *  @param rho ρ value to reach
  192.          *  @param sigma σ value to reach
  193.          */
  194.         private static void computeFor(final int rho, final int sigma) {

  195.             // Initialize result :
  196.             List<PolynomialFunction> result = new ArrayList<>();

  197.             // Get the coefficient from the recurrence relation
  198.             final Map<Integer, List<PolynomialFunction>> map = generateRecurrenceCoefficients(rho, sigma);

  199.             // Compute (s - n) * Y[rho - 2, sigma][n, s + 2]
  200.             if (rho >= 2) {
  201.                 final List<PolynomialFunction> poly = map.get(0);
  202.                 final List<PolynomialFunction> list = getPolynomials(rho - 2, sigma);
  203.                 result = multiplyPolynomialList(poly, shiftList(list, 2));
  204.             }

  205.             // Compute 2(2rho + 2sigma + 2 + 3n) * Y[rho - 1, sigma - 1][n, s]
  206.             if (rho >= 1 && sigma >= 1) {
  207.                 final List<PolynomialFunction> poly = map.get(1);
  208.                 final List<PolynomialFunction> list = getPolynomials(rho - 1, sigma - 1);
  209.                 result = sumPolynomialList(result, multiplyPolynomialList(poly, list));
  210.             }

  211.             // Compute 2(2s - n) * Y[rho - 1, sigma][n, s + 1]
  212.             if (rho >= 1) {
  213.                 final List<PolynomialFunction> poly = map.get(2);
  214.                 final List<PolynomialFunction> list = getPolynomials(rho - 1, sigma);
  215.                 result = sumPolynomialList(result, multiplyPolynomialList(poly, shiftList(list, 1)));
  216.             }

  217.             // Compute -(s + n) * Y[rho, sigma - 2][n, s - 2]
  218.             if (sigma >= 2) {
  219.                 final List<PolynomialFunction> poly = map.get(3);
  220.                 final List<PolynomialFunction> list = getPolynomials(rho, sigma - 2);
  221.                 result = sumPolynomialList(result, multiplyPolynomialList(poly, shiftList(list, -2)));
  222.             }

  223.             // Compute -2(2s + n) * Y[rho, sigma - 1][n, s - 1]
  224.             if (sigma >= 1) {
  225.                 final List<PolynomialFunction> poly = map.get(4);
  226.                 final List<PolynomialFunction> list = getPolynomials(rho, sigma - 1);
  227.                 result = sumPolynomialList(result, multiplyPolynomialList(poly, shiftList(list, -1)));
  228.             }

  229.             // Save polynomials for current (rho, sigma) couple
  230.             final Couple couple = new Couple(rho, sigma);
  231.             POLYNOMIALS.put(couple, result);
  232.         }

  233.         /** Multiply two lists of polynomials defined as the internal representation of the Newcomb Operator.
  234.          *  <p>
  235.          *  Let's call R<sub>s</sub>(n) the result returned by the method :
  236.          *  <pre>
  237.          *  R<sub>s</sub>(n) = (P<sub>s₀</sub> + P<sub>s₁</sub>n + ... + P<sub>s<sub>j</sub></sub>n<sup>j</sup>) *(Q<sub>s₀</sub> + Q<sub>s₁</sub>n + ... + Q<sub>s<sub>k</sub></sub>n<sup>k</sup>)
  238.          *  </pre>
  239.          *
  240.          *  where the P<sub>s<sub>j</sub></sub> and Q<sub>s<sub>k</sub></sub> are polynomials in s,
  241.          *  s being the index of the Y<sub>ρ,σ</sub><sup>n,s</sup> function
  242.          *
  243.          *  @param poly1 first list of polynomials
  244.          *  @param poly2 second list of polynomials
  245.          *  @return R<sub>s</sub>(n) as a list of {@link PolynomialFunction}
  246.          */
  247.         private static List<PolynomialFunction> multiplyPolynomialList(final List<PolynomialFunction> poly1,
  248.                                                                        final List<PolynomialFunction> poly2) {
  249.             // Initialize the result list of polynomial function
  250.             final List<PolynomialFunction> result = new ArrayList<>();
  251.             initializeListOfPolynomials(poly1.size() + poly2.size() - 1, result);

  252.             int i = 0;
  253.             // Iterate over first polynomial list
  254.             for (PolynomialFunction f1 : poly1) {
  255.                 // Iterate over second polynomial list
  256.                 for (int j = i; j < poly2.size() + i; j++) {
  257.                     final PolynomialFunction p2 = poly2.get(j - i);
  258.                     // Get previous polynomial for current 'n' order
  259.                     final PolynomialFunction previousP2 = result.get(j);
  260.                     // Replace the current order by summing the product of both of the polynomials
  261.                     result.set(j, previousP2.add(f1.multiply(p2)));
  262.                 }
  263.                 // shift polynomial order in 'n'
  264.                 i++;
  265.             }
  266.             return result;
  267.         }

  268.         /** Sum two lists of {@link PolynomialFunction}.
  269.          *  @param poly1 first list
  270.          *  @param poly2 second list
  271.          *  @return the summed list
  272.          */
  273.         private static List<PolynomialFunction> sumPolynomialList(final List<PolynomialFunction> poly1,
  274.                                                                   final List<PolynomialFunction> poly2) {
  275.             // identify the lowest degree polynomial
  276.             final int lowLength  = FastMath.min(poly1.size(), poly2.size());
  277.             final int highLength = FastMath.max(poly1.size(), poly2.size());
  278.             // Initialize the result list of polynomial function
  279.             final List<PolynomialFunction> result = new ArrayList<>();
  280.             initializeListOfPolynomials(highLength, result);

  281.             for (int i = 0; i < lowLength; i++) {
  282.                 // Add polynomials by increasing order of 'n'
  283.                 result.set(i, poly1.get(i).add(poly2.get(i)));
  284.             }
  285.             // Complete the list if lists are of different size:
  286.             for (int i = lowLength; i < highLength; i++) {
  287.                 if (poly1.size() < poly2.size()) {
  288.                     result.set(i, poly2.get(i));
  289.                 } else {
  290.                     result.set(i, poly1.get(i));
  291.                 }
  292.             }
  293.             return result;
  294.         }

  295.         /** Initialize an empty list of polynomials.
  296.          *  @param i order
  297.          *  @param result list into which polynomials should be added
  298.          */
  299.         private static void initializeListOfPolynomials(final int i,
  300.                                                         final List<PolynomialFunction> result) {
  301.             for (int k = 0; k < i; k++) {
  302.                 result.add(new PolynomialFunction(new double[] {0.}));
  303.             }
  304.         }

  305.         /** Shift a list of {@link PolynomialFunction}.
  306.          *
  307.          *  @param polynomialList list of {@link PolynomialFunction}
  308.          *  @param shift shift value
  309.          *  @return new list of shifted {@link PolynomialFunction}
  310.          */
  311.         private static List<PolynomialFunction> shiftList(final List<PolynomialFunction> polynomialList,
  312.                                                           final int shift) {
  313.             final List<PolynomialFunction> shiftedList = new ArrayList<>();
  314.             for (PolynomialFunction function : polynomialList) {
  315.                 shiftedList.add(new PolynomialFunction(shift(function.getCoefficients(), shift)));
  316.             }
  317.             return shiftedList;
  318.         }

  319.         /**
  320.          * Compute the coefficients of the polynomial \(P_s(x)\)
  321.          * whose values at point {@code x} will be the same as the those from the
  322.          * original polynomial \(P(x)\) when computed at {@code x + shift}.
  323.          * <p>
  324.          * More precisely, let \(\Delta = \) {@code shift} and let
  325.          * \(P_s(x) = P(x + \Delta)\).  The returned array
  326.          * consists of the coefficients of \(P_s\).  So if \(a_0, ..., a_{n-1}\)
  327.          * are the coefficients of \(P\), then the returned array
  328.          * \(b_0, ..., b_{n-1}\) satisfies the identity
  329.          * \(\sum_{i=0}^{n-1} b_i x^i = \sum_{i=0}^{n-1} a_i (x + \Delta)^i\) for all \(x\).
  330.          * </p>
  331.          * <p>
  332.          * This method is a modified version of the method with the same name
  333.          * in Hipparchus {@code PolynomialsUtils} class, simply changing
  334.          * computation of binomial coefficients so degrees higher than 66 can be used.
  335.          * </p>
  336.          *
  337.          * @param coefficients Coefficients of the original polynomial.
  338.          * @param shift Shift value.
  339.          * @return the coefficients \(b_i\) of the shifted
  340.          * polynomial.
  341.          */
  342.         public static double[] shift(final double[] coefficients,
  343.                                      final double shift) {
  344.             final int dp1 = coefficients.length;
  345.             final double[] newCoefficients = new double[dp1];

  346.             // Pascal triangle.
  347.             final double[][] coeff = new double[dp1][dp1];
  348.             coeff[0][0] = 1;
  349.             for (int i = 1; i < dp1; i++) {
  350.                 coeff[i][0] = 1;
  351.                 for (int j = 1; j < i; j++) {
  352.                     coeff[i][j] = coeff[i - 1][j - 1] + coeff[i - 1][j];
  353.                 }
  354.                 coeff[i][i] = 1;
  355.             }

  356.             // First polynomial coefficient.
  357.             double shiftI = 1;
  358.             for (double coefficient : coefficients) {
  359.                 newCoefficients[0] += coefficient * shiftI;
  360.                 shiftI *= shift;
  361.             }

  362.             // Superior order.
  363.             final int d = dp1 - 1;
  364.             for (int i = 0; i < d; i++) {
  365.                 double shiftJmI = 1;
  366.                 for (int j = i; j < d; j++) {
  367.                     newCoefficients[i + 1] += coeff[j + 1][j - i] * coefficients[j + 1] * shiftJmI;
  368.                     shiftJmI *= shift;
  369.                 }
  370.             }

  371.             return newCoefficients;
  372.         }

  373.         /** Generate recurrence coefficients.
  374.          *
  375.          *  @param rho ρ value
  376.          *  @param sigma σ value
  377.          *  @return recurrence coefficients
  378.          */
  379.         private static Map<Integer, List<PolynomialFunction>> generateRecurrenceCoefficients(final int rho, final int sigma) {
  380.             final double den   = 1. / (4. * (rho + sigma));
  381.             final double denx2 = 2. * den;
  382.             final double denx4 = 4. * den;
  383.             // Initialization :
  384.             final Map<Integer, List<PolynomialFunction>> list = new TreeMap<>();
  385.             final List<PolynomialFunction> poly0 = new ArrayList<>();
  386.             final List<PolynomialFunction> poly1 = new ArrayList<>();
  387.             final List<PolynomialFunction> poly2 = new ArrayList<>();
  388.             final List<PolynomialFunction> poly3 = new ArrayList<>();
  389.             final List<PolynomialFunction> poly4 = new ArrayList<>();
  390.             // (s - n)
  391.             poly0.add(new PolynomialFunction(new double[] {0., den}));
  392.             poly0.add(new PolynomialFunction(new double[] {-den}));
  393.             // 2(2 * rho + 2 * sigma + 2 + 3*n)
  394.             poly1.add(new PolynomialFunction(new double[] {1. + denx4}));
  395.             poly1.add(new PolynomialFunction(new double[] {denx2 + denx4}));
  396.             // 2(2s - n)
  397.             poly2.add(new PolynomialFunction(new double[] {0., denx4}));
  398.             poly2.add(new PolynomialFunction(new double[] {-denx2}));
  399.             // - (s + n)
  400.             poly3.add(new PolynomialFunction(new double[] {0., -den}));
  401.             poly3.add(new PolynomialFunction(new double[] {-den}));
  402.             // - 2(2s + n)
  403.             poly4.add(new PolynomialFunction(new double[] {0., -denx4}));
  404.             poly4.add(new PolynomialFunction(new double[] {-denx2}));

  405.             // Fill the map :
  406.             list.put(0, poly0);
  407.             list.put(1, poly1);
  408.             list.put(2, poly2);
  409.             list.put(3, poly3);
  410.             list.put(4, poly4);
  411.             return list;
  412.         }

  413.     }

  414.     /** Private class to define a couple of value. */
  415.     private static class Couple {

  416.         /** first couple value. */
  417.         private final int rho;

  418.         /** second couple value. */
  419.         private final int sigma;

  420.         /** Constructor.
  421.          * @param rho first couple value
  422.          * @param sigma second couple value
  423.          */
  424.         private Couple(final int rho, final int sigma) {
  425.             this.rho = rho;
  426.             this.sigma = sigma;
  427.         }

  428.     }

  429.     /** Newcomb operator's key. */
  430.     private static class NewKey {

  431.         /** n value. */
  432.         private final int n;

  433.         /** s value. */
  434.         private final int s;

  435.         /** ρ value. */
  436.         private final int rho;

  437.         /** σ value. */
  438.         private final int sigma;

  439.         /** Simpleconstructor.
  440.          * @param n n
  441.          * @param s s
  442.          * @param rho ρ
  443.          * @param sigma σ
  444.          */
  445.         NewKey(final int n, final int s, final int rho, final int sigma) {
  446.             this.n = n;
  447.             this.s = s;
  448.             this.rho = rho;
  449.             this.sigma = sigma;
  450.         }

  451.     }

  452. }