DSSTZonal.java

  1. /* Copyright 2002-2020 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.forces;

  18. import java.lang.reflect.Array;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.TreeMap;

  27. import org.hipparchus.Field;
  28. import org.hipparchus.RealFieldElement;
  29. import org.hipparchus.exception.LocalizedCoreFormats;
  30. import org.hipparchus.util.CombinatoricsUtils;
  31. import org.hipparchus.util.FastMath;
  32. import org.hipparchus.util.FieldSinCos;
  33. import org.hipparchus.util.MathArrays;
  34. import org.hipparchus.util.SinCos;
  35. import org.orekit.attitudes.AttitudeProvider;
  36. import org.orekit.errors.OrekitException;
  37. import org.orekit.errors.OrekitInternalError;
  38. import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider;
  39. import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider.UnnormalizedSphericalHarmonics;
  40. import org.orekit.orbits.FieldOrbit;
  41. import org.orekit.orbits.Orbit;
  42. import org.orekit.propagation.FieldSpacecraftState;
  43. import org.orekit.propagation.PropagationType;
  44. import org.orekit.propagation.SpacecraftState;
  45. import org.orekit.propagation.events.EventDetector;
  46. import org.orekit.propagation.events.FieldEventDetector;
  47. import org.orekit.propagation.semianalytical.dsst.utilities.AuxiliaryElements;
  48. import org.orekit.propagation.semianalytical.dsst.utilities.CjSjCoefficient;
  49. import org.orekit.propagation.semianalytical.dsst.utilities.CoefficientsFactory;
  50. import org.orekit.propagation.semianalytical.dsst.utilities.CoefficientsFactory.NSKey;
  51. import org.orekit.propagation.semianalytical.dsst.utilities.FieldAuxiliaryElements;
  52. import org.orekit.propagation.semianalytical.dsst.utilities.FieldCjSjCoefficient;
  53. import org.orekit.propagation.semianalytical.dsst.utilities.FieldGHIJjsPolynomials;
  54. import org.orekit.propagation.semianalytical.dsst.utilities.FieldLnsCoefficients;
  55. import org.orekit.propagation.semianalytical.dsst.utilities.FieldShortPeriodicsInterpolatedCoefficient;
  56. import org.orekit.propagation.semianalytical.dsst.utilities.GHIJjsPolynomials;
  57. import org.orekit.propagation.semianalytical.dsst.utilities.LnsCoefficients;
  58. import org.orekit.propagation.semianalytical.dsst.utilities.ShortPeriodicsInterpolatedCoefficient;
  59. import org.orekit.propagation.semianalytical.dsst.utilities.UpperBounds;
  60. import org.orekit.propagation.semianalytical.dsst.utilities.hansen.FieldHansenZonalLinear;
  61. import org.orekit.propagation.semianalytical.dsst.utilities.hansen.HansenZonalLinear;
  62. import org.orekit.time.AbsoluteDate;
  63. import org.orekit.time.FieldAbsoluteDate;
  64. import org.orekit.utils.FieldTimeSpanMap;
  65. import org.orekit.utils.ParameterDriver;
  66. import org.orekit.utils.TimeSpanMap;

  67. /** Zonal contribution to the central body gravitational perturbation.
  68.  *
  69.  *   @author Romain Di Costanzo
  70.  *   @author Pascal Parraud
  71.  *   @author Bryan Cazabonne (field translation)
  72.  */
  73. public class DSSTZonal implements DSSTForceModel {

  74.     /**  Name of the prefix for short period coefficients keys. */
  75.     public static final String SHORT_PERIOD_PREFIX = "DSST-central-body-zonal-";

  76.     /** Retrograde factor I.
  77.      *  <p>
  78.      *  DSST model needs equinoctial orbit as internal representation.
  79.      *  Classical equinoctial elements have discontinuities when inclination
  80.      *  is close to zero. In this representation, I = +1. <br>
  81.      *  To avoid this discontinuity, another representation exists and equinoctial
  82.      *  elements can be expressed in a different way, called "retrograde" orbit.
  83.      *  This implies I = -1. <br>
  84.      *  As Orekit doesn't implement the retrograde orbit, I is always set to +1.
  85.      *  But for the sake of consistency with the theory, the retrograde factor
  86.      *  has been kept in the formulas.
  87.      *  </p>
  88.      */
  89.     private static final int I = 1;

  90.     /** Number of points for interpolation. */
  91.     private static final int INTERPOLATION_POINTS = 3;

  92.     /** Truncation tolerance. */
  93.     private static final double TRUNCATION_TOLERANCE = 1e-4;

  94.     /** Central attraction scaling factor.
  95.      * <p>
  96.      * We use a power of 2 to avoid numeric noise introduction
  97.      * in the multiplications/divisions sequences.
  98.      * </p>
  99.      */
  100.     private static final double MU_SCALE = FastMath.scalb(1.0, 32);

  101.     /** Coefficient used to define the mean disturbing function V<sub>ns</sub> coefficient. */
  102.     private final TreeMap<NSKey, Double> Vns;

  103.     /** Provider for spherical harmonics. */
  104.     private final UnnormalizedSphericalHarmonicsProvider provider;

  105.     /** Maximal degree to consider for harmonics potential. */
  106.     private final int maxDegree;

  107.     /** Maximal degree to consider for harmonics potential. */
  108.     private final int maxDegreeShortPeriodics;

  109.     /** Highest power of the eccentricity to be used in short periodic computations. */
  110.     private final int maxEccPowShortPeriodics;

  111.     /** Maximum frequency in true longitude for short periodic computations. */
  112.     private final int maxFrequencyShortPeriodics;

  113.     /** Highest power of the eccentricity to be used in mean elements computations. */
  114.     private int maxEccPowMeanElements;

  115.     /** Highest power of the eccentricity. */
  116.     private int maxEccPow;

  117.     /** Short period terms. */
  118.     private ZonalShortPeriodicCoefficients zonalSPCoefs;

  119.     /** Short period terms. */
  120.     private Map<Field<?>, FieldZonalShortPeriodicCoefficients<?>> zonalFieldSPCoefs;

  121.     /** Driver for gravitational parameter. */
  122.     private final ParameterDriver gmParameterDriver;

  123.     /** Hansen objects. */
  124.     private HansenObjects hansen;

  125.     /** Hansen objects for field elements. */
  126.     private Map<Field<?>, FieldHansenObjects<?>> fieldHansen;

  127.     /** Flag for force model initialization with field elements. */
  128.     private boolean pendingInitialization;

  129.     /** Constructor with default reference values.
  130.      * <p>
  131.      * When this constructor is used, maximum allowed values are used
  132.      * for the short periodic coefficients:
  133.      * </p>
  134.      * <ul>
  135.      *    <li> {@link #maxDegreeShortPeriodics} is set to {@code provider.getMaxDegree()} </li>
  136.      *    <li> {@link #maxEccPowShortPeriodics} is set to {@code min(provider.getMaxDegree() - 1, 4)}.
  137.      *         This parameter should not exceed 4 as higher values will exceed computer capacity </li>
  138.      *    <li> {@link #maxFrequencyShortPeriodics} is set to {@code 2 * provider.getMaxDegree() + 1} </li>
  139.      * </ul>
  140.      * @param provider provider for spherical harmonics
  141.      * @since 10.1
  142.      */
  143.     public DSSTZonal(final UnnormalizedSphericalHarmonicsProvider provider) {
  144.         this(provider, provider.getMaxDegree(), FastMath.min(4, provider.getMaxDegree() - 1), 2 * provider.getMaxDegree() + 1);
  145.     }

  146.     /** Simple constructor.
  147.      * @param provider provider for spherical harmonics
  148.      * @param maxDegreeShortPeriodics maximum degree to consider for short periodics zonal harmonics potential
  149.      * (must be between 2 and {@code provider.getMaxDegree()})
  150.      * @param maxEccPowShortPeriodics maximum power of the eccentricity to be used in short periodic computations
  151.      * (must be between 0 and {@code maxDegreeShortPeriodics - 1}, but should typically not exceed 4 as higher
  152.      * values will exceed computer capacity)
  153.      * @param maxFrequencyShortPeriodics maximum frequency in true longitude for short periodic computations
  154.      * (must be between 1 and {@code 2 * maxDegreeShortPeriodics + 1})
  155.      * @since 7.2
  156.      */
  157.     public DSSTZonal(final UnnormalizedSphericalHarmonicsProvider provider,
  158.                      final int maxDegreeShortPeriodics,
  159.                      final int maxEccPowShortPeriodics,
  160.                      final int maxFrequencyShortPeriodics) {

  161.         gmParameterDriver = new ParameterDriver(DSSTNewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT,
  162.                                                 provider.getMu(), MU_SCALE,
  163.                                                 0.0, Double.POSITIVE_INFINITY);

  164.         // Vns coefficients
  165.         this.Vns = CoefficientsFactory.computeVns(provider.getMaxDegree() + 1);

  166.         this.provider  = provider;
  167.         this.maxDegree = provider.getMaxDegree();

  168.         checkIndexRange(maxDegreeShortPeriodics, 2, provider.getMaxDegree());
  169.         this.maxDegreeShortPeriodics = maxDegreeShortPeriodics;

  170.         checkIndexRange(maxEccPowShortPeriodics, 0, maxDegreeShortPeriodics - 1);
  171.         this.maxEccPowShortPeriodics = maxEccPowShortPeriodics;

  172.         checkIndexRange(maxFrequencyShortPeriodics, 1, 2 * maxDegreeShortPeriodics + 1);
  173.         this.maxFrequencyShortPeriodics = maxFrequencyShortPeriodics;

  174.         // Initialize default values
  175.         this.maxEccPowMeanElements = (maxDegree == 2) ? 0 : Integer.MIN_VALUE;


  176.         pendingInitialization = true;

  177.         zonalFieldSPCoefs = new HashMap<>();
  178.         fieldHansen       = new HashMap<>();

  179.     }

  180.     /** Check an index range.
  181.      * @param index index value
  182.      * @param min minimum value for index
  183.      * @param max maximum value for index
  184.      */
  185.     private void checkIndexRange(final int index, final int min, final int max) {
  186.         if (index < min || index > max) {
  187.             throw new OrekitException(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE, index, min, max);
  188.         }
  189.     }

  190.     /** Get the spherical harmonics provider.
  191.      *  @return the spherical harmonics provider
  192.      */
  193.     public UnnormalizedSphericalHarmonicsProvider getProvider() {
  194.         return provider;
  195.     }

  196.     /** {@inheritDoc}
  197.      *  <p>
  198.      *  Computes the highest power of the eccentricity to appear in the truncated
  199.      *  analytical power series expansion.
  200.      *  </p>
  201.      *  <p>
  202.      *  This method computes the upper value for the central body potential and
  203.      *  determines the maximal power for the eccentricity producing potential
  204.      *  terms bigger than a defined tolerance.
  205.      *  </p>
  206.      */
  207.     @Override
  208.     public List<ShortPeriodTerms> initialize(final AuxiliaryElements auxiliaryElements,
  209.                                              final PropagationType type,
  210.                                              final double[] parameters) {

  211.         computeMeanElementsTruncations(auxiliaryElements, parameters);

  212.         switch (type) {
  213.             case MEAN:
  214.                 maxEccPow = maxEccPowMeanElements;
  215.                 break;
  216.             case OSCULATING:
  217.                 maxEccPow = FastMath.max(maxEccPowMeanElements, maxEccPowShortPeriodics);
  218.                 break;
  219.             default:
  220.                 throw new OrekitInternalError(null);
  221.         }

  222.         hansen = new HansenObjects();

  223.         final List<ShortPeriodTerms> list = new ArrayList<ShortPeriodTerms>();
  224.         zonalSPCoefs = new ZonalShortPeriodicCoefficients(maxFrequencyShortPeriodics,
  225.                                                           INTERPOLATION_POINTS,
  226.                                                           new TimeSpanMap<Slot>(new Slot(maxFrequencyShortPeriodics,
  227.                                                                                          INTERPOLATION_POINTS)));
  228.         list.add(zonalSPCoefs);
  229.         return list;

  230.     }

  231.     /** {@inheritDoc}
  232.      *  <p>
  233.      *  Computes the highest power of the eccentricity to appear in the truncated
  234.      *  analytical power series expansion.
  235.      *  </p>
  236.      *  <p>
  237.      *  This method computes the upper value for the central body potential and
  238.      *  determines the maximal power for the eccentricity producing potential
  239.      *  terms bigger than a defined tolerance.
  240.      *  </p>
  241.      */
  242.     @Override
  243.     public <T extends RealFieldElement<T>> List<FieldShortPeriodTerms<T>> initialize(final FieldAuxiliaryElements<T> auxiliaryElements,
  244.                                                                                      final PropagationType type,
  245.                                                                                      final T[] parameters) {

  246.         // Field used by default
  247.         final Field<T> field = auxiliaryElements.getDate().getField();

  248.         if (pendingInitialization == true) {
  249.             computeMeanElementsTruncations(auxiliaryElements, parameters, field);

  250.             switch (type) {
  251.                 case MEAN:
  252.                     maxEccPow = maxEccPowMeanElements;
  253.                     break;
  254.                 case OSCULATING:
  255.                     maxEccPow = FastMath.max(maxEccPowMeanElements, maxEccPowShortPeriodics);
  256.                     break;
  257.                 default:
  258.                     throw new OrekitInternalError(null);
  259.             }

  260.             fieldHansen.put(field, new FieldHansenObjects<>(field));

  261.             pendingInitialization = false;
  262.         }

  263.         final FieldZonalShortPeriodicCoefficients<T> fzspc =
  264.                         new FieldZonalShortPeriodicCoefficients<>(maxFrequencyShortPeriodics,
  265.                                                                   INTERPOLATION_POINTS,
  266.                                                                   new FieldTimeSpanMap<>(new FieldSlot<>(maxFrequencyShortPeriodics,
  267.                                                                                                          INTERPOLATION_POINTS),
  268.                                                                                          field));
  269.         zonalFieldSPCoefs.put(field, fzspc);
  270.         return Collections.singletonList(fzspc);

  271.     }

  272.     /** Compute indices truncations for mean elements computations.
  273.      * @param auxiliaryElements auxiliary elements
  274.      * @param parameters values of the force model parameters
  275.      */
  276.     private void computeMeanElementsTruncations(final AuxiliaryElements auxiliaryElements, final double[] parameters) {

  277.         final DSSTZonalContext context = new DSSTZonalContext(auxiliaryElements, provider, parameters);
  278.         //Compute the max eccentricity power for the mean element rate expansion
  279.         if (maxDegree == 2) {
  280.             maxEccPowMeanElements = 0;
  281.         } else {
  282.             // Initializes specific parameters.
  283.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(auxiliaryElements.getDate());

  284.             // Utilities for truncation
  285.             final double ax2or = 2. * auxiliaryElements.getSma() / provider.getAe();
  286.             double xmuran = parameters[0] / auxiliaryElements.getSma();
  287.             // Set a lower bound for eccentricity
  288.             final double eo2  = FastMath.max(0.0025, auxiliaryElements.getEcc() / 2.);
  289.             final double x2o2 = context.getXX() / 2.;
  290.             final double[] eccPwr = new double[maxDegree + 1];
  291.             final double[] chiPwr = new double[maxDegree + 1];
  292.             final double[] hafPwr = new double[maxDegree + 1];
  293.             eccPwr[0] = 1.;
  294.             chiPwr[0] = context.getX();
  295.             hafPwr[0] = 1.;
  296.             for (int i = 1; i <= maxDegree; i++) {
  297.                 eccPwr[i] = eccPwr[i - 1] * eo2;
  298.                 chiPwr[i] = chiPwr[i - 1] * x2o2;
  299.                 hafPwr[i] = hafPwr[i - 1] * 0.5;
  300.                 xmuran  /= ax2or;
  301.             }

  302.             // Set highest power of e and degree of current spherical harmonic.
  303.             maxEccPowMeanElements = 0;
  304.             int maxDeg = maxDegree;
  305.             // Loop over n
  306.             do {
  307.                 // Set order of current spherical harmonic.
  308.                 int m = 0;
  309.                 // Loop over m
  310.                 do {
  311.                     // Compute magnitude of current spherical harmonic coefficient.
  312.                     final double cnm = harmonics.getUnnormalizedCnm(maxDeg, m);
  313.                     final double snm = harmonics.getUnnormalizedSnm(maxDeg, m);
  314.                     final double csnm = FastMath.hypot(cnm, snm);
  315.                     if (csnm == 0.) {
  316.                         break;
  317.                     }
  318.                     // Set magnitude of last spherical harmonic term.
  319.                     double lastTerm = 0.;
  320.                     // Set current power of e and related indices.
  321.                     int nsld2 = (maxDeg - maxEccPowMeanElements - 1) / 2;
  322.                     int l = maxDeg - 2 * nsld2;
  323.                     // Loop over l
  324.                     double term = 0.;
  325.                     do {
  326.                         // Compute magnitude of current spherical harmonic term.
  327.                         if (m < l) {
  328.                             term =  csnm * xmuran *
  329.                                     (CombinatoricsUtils.factorialDouble(maxDeg - l) / (CombinatoricsUtils.factorialDouble(maxDeg - m))) *
  330.                                     (CombinatoricsUtils.factorialDouble(maxDeg + l) / (CombinatoricsUtils.factorialDouble(nsld2) * CombinatoricsUtils.factorialDouble(nsld2 + l))) *
  331.                                     eccPwr[l] * UpperBounds.getDnl(context.getXX(), chiPwr[l], maxDeg, l) *
  332.                                     (UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, l, m, 1, I) + UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, l, m, -1, I));
  333.                         } else {
  334.                             term =  csnm * xmuran *
  335.                                     (CombinatoricsUtils.factorialDouble(maxDeg + m) / (CombinatoricsUtils.factorialDouble(nsld2) * CombinatoricsUtils.factorialDouble(nsld2 + l))) *
  336.                                     eccPwr[l] * hafPwr[m - l] * UpperBounds.getDnl(context.getXX(), chiPwr[l], maxDeg, l) *
  337.                                     (UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, m, l, 1, I) + UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, m, l, -1, I));
  338.                         }
  339.                         // Is the current spherical harmonic term bigger than the truncation tolerance ?
  340.                         if (term >= TRUNCATION_TOLERANCE) {
  341.                             maxEccPowMeanElements = l;
  342.                         } else {
  343.                             // Is the current term smaller than the last term ?
  344.                             if (term < lastTerm) {
  345.                                 break;
  346.                             }
  347.                         }
  348.                         // Proceed to next power of e.
  349.                         lastTerm = term;
  350.                         l += 2;
  351.                         nsld2--;
  352.                     } while (l < maxDeg);
  353.                     // Is the current spherical harmonic term bigger than the truncation tolerance ?
  354.                     if (term >= TRUNCATION_TOLERANCE) {
  355.                         maxEccPowMeanElements = FastMath.min(maxDegree - 2, maxEccPowMeanElements);
  356.                         return;
  357.                     }
  358.                     // Proceed to next order.
  359.                     m++;
  360.                 } while (m <= FastMath.min(maxDeg, provider.getMaxOrder()));
  361.                 // Proceed to next degree.
  362.                 xmuran *= ax2or;
  363.                 maxDeg--;
  364.             } while (maxDeg > maxEccPowMeanElements + 2);

  365.             maxEccPowMeanElements = FastMath.min(maxDegree - 2, maxEccPowMeanElements);
  366.         }
  367.     }

  368.     /** Compute indices truncations for mean elements computations.
  369.      * @param <T> type of the elements
  370.      * @param auxiliaryElements auxiliary elements
  371.      * @param parameters values of the force model parameters
  372.      * @param field field used by default
  373.      */
  374.     private <T extends RealFieldElement<T>> void computeMeanElementsTruncations(final FieldAuxiliaryElements<T> auxiliaryElements,
  375.                                                                                 final T[] parameters,
  376.                                                                                 final Field<T> field) {

  377.         final T zero = field.getZero();
  378.         final FieldDSSTZonalContext<T> context = new FieldDSSTZonalContext<>(auxiliaryElements, provider, parameters);
  379.         //Compute the max eccentricity power for the mean element rate expansion
  380.         if (maxDegree == 2) {
  381.             maxEccPowMeanElements = 0;
  382.         } else {
  383.             // Initializes specific parameters.
  384.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(auxiliaryElements.getDate().toAbsoluteDate());

  385.             // Utilities for truncation
  386.             final T ax2or = auxiliaryElements.getSma().multiply(2.).divide(provider.getAe());
  387.             T xmuran = parameters[0].divide(auxiliaryElements.getSma());
  388.             // Set a lower bound for eccentricity
  389.             final T eo2  = FastMath.max(zero.add(0.0025), auxiliaryElements.getEcc().divide(2.));
  390.             final T x2o2 = context.getXX().divide(2.);
  391.             final T[] eccPwr = MathArrays.buildArray(field, maxDegree + 1);
  392.             final T[] chiPwr = MathArrays.buildArray(field, maxDegree + 1);
  393.             final T[] hafPwr = MathArrays.buildArray(field, maxDegree + 1);
  394.             eccPwr[0] = zero.add(1.);
  395.             chiPwr[0] = context.getX();
  396.             hafPwr[0] = zero.add(1.);
  397.             for (int i = 1; i <= maxDegree; i++) {
  398.                 eccPwr[i] = eccPwr[i - 1].multiply(eo2);
  399.                 chiPwr[i] = chiPwr[i - 1].multiply(x2o2);
  400.                 hafPwr[i] = hafPwr[i - 1].multiply(0.5);
  401.                 xmuran  = xmuran.divide(ax2or);
  402.             }

  403.             // Set highest power of e and degree of current spherical harmonic.
  404.             maxEccPowMeanElements = 0;
  405.             int maxDeg = maxDegree;
  406.             // Loop over n
  407.             do {
  408.                 // Set order of current spherical harmonic.
  409.                 int m = 0;
  410.                 // Loop over m
  411.                 do {
  412.                     // Compute magnitude of current spherical harmonic coefficient.
  413.                     final T cnm = zero.add(harmonics.getUnnormalizedCnm(maxDeg, m));
  414.                     final T snm = zero.add(harmonics.getUnnormalizedSnm(maxDeg, m));
  415.                     final T csnm = FastMath.hypot(cnm, snm);
  416.                     if (csnm.getReal() == 0.) {
  417.                         break;
  418.                     }
  419.                     // Set magnitude of last spherical harmonic term.
  420.                     T lastTerm = zero;
  421.                     // Set current power of e and related indices.
  422.                     int nsld2 = (maxDeg - maxEccPowMeanElements - 1) / 2;
  423.                     int l = maxDeg - 2 * nsld2;
  424.                     // Loop over l
  425.                     T term = zero;
  426.                     do {
  427.                         // Compute magnitude of current spherical harmonic term.
  428.                         if (m < l) {
  429.                             term = csnm.multiply(xmuran).
  430.                                    multiply((CombinatoricsUtils.factorialDouble(maxDeg - l) / (CombinatoricsUtils.factorialDouble(maxDeg - m))) *
  431.                                    (CombinatoricsUtils.factorialDouble(maxDeg + l) / (CombinatoricsUtils.factorialDouble(nsld2) * CombinatoricsUtils.factorialDouble(nsld2 + l)))).
  432.                                    multiply(eccPwr[l]).multiply(UpperBounds.getDnl(context.getXX(), chiPwr[l], maxDeg, l)).
  433.                                    multiply(UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, l, m, 1, I).add(UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, l, m, -1, I)));
  434.                         } else {
  435.                             term = csnm.multiply(xmuran).
  436.                                    multiply(CombinatoricsUtils.factorialDouble(maxDeg + m) / (CombinatoricsUtils.factorialDouble(nsld2) * CombinatoricsUtils.factorialDouble(nsld2 + l))).
  437.                                    multiply(eccPwr[l]).multiply(hafPwr[m - l]).multiply(UpperBounds.getDnl(context.getXX(), chiPwr[l], maxDeg, l)).
  438.                                    multiply(UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, m, l, 1, I).add(UpperBounds.getRnml(auxiliaryElements.getGamma(), maxDeg, m, l, -1, I)));
  439.                         }
  440.                         // Is the current spherical harmonic term bigger than the truncation tolerance ?
  441.                         if (term.getReal() >= TRUNCATION_TOLERANCE) {
  442.                             maxEccPowMeanElements = l;
  443.                         } else {
  444.                             // Is the current term smaller than the last term ?
  445.                             if (term.getReal() < lastTerm.getReal()) {
  446.                                 break;
  447.                             }
  448.                         }
  449.                         // Proceed to next power of e.
  450.                         lastTerm = term;
  451.                         l += 2;
  452.                         nsld2--;
  453.                     } while (l < maxDeg);
  454.                     // Is the current spherical harmonic term bigger than the truncation tolerance ?
  455.                     if (term.getReal() >= TRUNCATION_TOLERANCE) {
  456.                         maxEccPowMeanElements = FastMath.min(maxDegree - 2, maxEccPowMeanElements);
  457.                         return;
  458.                     }
  459.                     // Proceed to next order.
  460.                     m++;
  461.                 } while (m <= FastMath.min(maxDeg, provider.getMaxOrder()));
  462.                 // Proceed to next degree.
  463.                 xmuran = xmuran.multiply(ax2or);
  464.                 maxDeg--;
  465.             } while (maxDeg > maxEccPowMeanElements + 2);

  466.             maxEccPowMeanElements = FastMath.min(maxDegree - 2, maxEccPowMeanElements);
  467.         }
  468.     }

  469.     /** Performs initialization at each integration step for the current force model.
  470.      *  <p>
  471.      *  This method aims at being called before mean elements rates computation.
  472.      *  </p>
  473.      *  @param auxiliaryElements auxiliary elements related to the current orbit
  474.      *  @param parameters values of the force model parameters
  475.      *  @return new force model context
  476.      */
  477.     private DSSTZonalContext initializeStep(final AuxiliaryElements auxiliaryElements, final double[] parameters) {
  478.         return new DSSTZonalContext(auxiliaryElements, provider, parameters);
  479.     }

  480.     /** Performs initialization at each integration step for the current force model.
  481.      *  <p>
  482.      *  This method aims at being called before mean elements rates computation.
  483.      *  </p>
  484.      *  @param <T> type of the elements
  485.      *  @param auxiliaryElements auxiliary elements related to the current orbit
  486.      *  @param parameters values of the force model parameters
  487.      *  @return new force model context
  488.      */
  489.     private <T extends RealFieldElement<T>> FieldDSSTZonalContext<T> initializeStep(final FieldAuxiliaryElements<T> auxiliaryElements,
  490.                                                                                     final T[] parameters) {
  491.         return new FieldDSSTZonalContext<>(auxiliaryElements, provider, parameters);
  492.     }

  493.     /** {@inheritDoc} */
  494.     @Override
  495.     public double[] getMeanElementRate(final SpacecraftState spacecraftState,
  496.                                        final AuxiliaryElements auxiliaryElements, final double[] parameters) {

  497.         // Container of attributes
  498.         final DSSTZonalContext context = initializeStep(auxiliaryElements, parameters);
  499.         // Access to potential U derivatives
  500.         final UAnddU udu = new UAnddU(spacecraftState.getDate(), context, auxiliaryElements, hansen);

  501.         return computeMeanElementRates(spacecraftState.getDate(), context, udu);

  502.     }

  503.     /** {@inheritDoc} */
  504.     @Override
  505.     public <T extends RealFieldElement<T>> T[] getMeanElementRate(final FieldSpacecraftState<T> spacecraftState,
  506.                                                                   final FieldAuxiliaryElements<T> auxiliaryElements,
  507.                                                                   final T[] parameters) {

  508.         // Field used by default
  509.         final Field<T> field = auxiliaryElements.getDate().getField();
  510.         // Container of attributes
  511.         final FieldDSSTZonalContext<T> context = initializeStep(auxiliaryElements, parameters);

  512.         @SuppressWarnings("unchecked")
  513.         final FieldHansenObjects<T> fho = (FieldHansenObjects<T>) fieldHansen.get(field);

  514.         // Access to potential U derivatives
  515.         final FieldUAnddU<T> udu = new FieldUAnddU<>(spacecraftState.getDate(), context, auxiliaryElements, fho);

  516.         return computeMeanElementRates(spacecraftState.getDate(), context, udu);

  517.     }

  518.     /** {@inheritDoc} */
  519.     @Override
  520.     public EventDetector[] getEventsDetectors() {
  521.         return null;
  522.     }

  523.     /** {@inheritDoc} */
  524.     @Override
  525.     public <T extends RealFieldElement<T>> FieldEventDetector<T>[] getFieldEventsDetectors(final Field<T> field) {
  526.         return null;
  527.     }

  528.     /** Compute the mean element rates.
  529.      * @param date current date
  530.      * @param context container for attributes
  531.      * @param udu derivatives of the gravitational potential U
  532.      * @return the mean element rates
  533.      */
  534.     private double[] computeMeanElementRates(final AbsoluteDate date,
  535.                                              final DSSTZonalContext context,
  536.                                              final UAnddU udu) {

  537.         // Auxiliary elements related to the current orbit
  538.         final AuxiliaryElements auxiliaryElements = context.getAuxiliaryElements();

  539.         // Compute cross derivatives [Eq. 2.2-(8)]
  540.         // U(alpha,gamma) = alpha * dU/dgamma - gamma * dU/dalpha
  541.         final double UAlphaGamma   = auxiliaryElements.getAlpha() * udu.getdUdGa() - auxiliaryElements.getGamma() * udu.getdUdAl();
  542.         // U(beta,gamma) = beta * dU/dgamma - gamma * dU/dbeta
  543.         final double UBetaGamma    =  auxiliaryElements.getBeta() * udu.getdUdGa() - auxiliaryElements.getGamma() * udu.getdUdBe();
  544.         // Common factor
  545.         final double pUAGmIqUBGoAB = (auxiliaryElements.getP() * UAlphaGamma - I * auxiliaryElements.getQ() * UBetaGamma) * context.getOoAB();

  546.         // Compute mean elements rates [Eq. 3.1-(1)]
  547.         final double da =  0.;
  548.         final double dh =  context.getBoA() * udu.getdUdk() + auxiliaryElements.getK() * pUAGmIqUBGoAB;
  549.         final double dk = -context.getBoA() * udu.getdUdh() - auxiliaryElements.getH() * pUAGmIqUBGoAB;
  550.         final double dp =  context.getMCo2AB() * UBetaGamma;
  551.         final double dq =  context.getMCo2AB() * UAlphaGamma * I;
  552.         final double dM =  context.getM2aoA() * udu.getdUda() + context.getBoABpo() * (auxiliaryElements.getH() * udu.getdUdh() + auxiliaryElements.getK() * udu.getdUdk()) + pUAGmIqUBGoAB;

  553.         return new double[] {da, dk, dh, dq, dp, dM};
  554.     }

  555.     /** Compute the mean element rates.
  556.      * @param <T> type of the elements
  557.      * @param date current date
  558.      * @param context container for attributes
  559.      * @param udu derivatives of the gravitational potential U
  560.      * @return the mean element rates
  561.      */
  562.     private <T extends RealFieldElement<T>> T[] computeMeanElementRates(final FieldAbsoluteDate<T> date,
  563.                                                                         final FieldDSSTZonalContext<T> context,
  564.                                                                         final FieldUAnddU<T> udu) {

  565.         // Auxiliary elements related to the current orbit
  566.         final FieldAuxiliaryElements<T> auxiliaryElements = context.getFieldAuxiliaryElements();

  567.         // Parameter for array building
  568.         final Field<T> field = date.getField();

  569.         // Compute cross derivatives [Eq. 2.2-(8)]
  570.         // U(alpha,gamma) = alpha * dU/dgamma - gamma * dU/dalpha
  571.         final T UAlphaGamma   = udu.getdUdGa().multiply(auxiliaryElements.getAlpha()).subtract(udu.getdUdAl().multiply(auxiliaryElements.getGamma()));
  572.         // U(beta,gamma) = beta * dU/dgamma - gamma * dU/dbeta
  573.         final T UBetaGamma    =  udu.getdUdGa().multiply(auxiliaryElements.getBeta()).subtract(udu.getdUdBe().multiply(auxiliaryElements.getGamma()));
  574.         // Common factor
  575.         final T pUAGmIqUBGoAB = (UAlphaGamma.multiply(auxiliaryElements.getP()).subtract(UBetaGamma.multiply(I).multiply(auxiliaryElements.getQ()))).multiply(context.getOoAB());

  576.         // Compute mean elements rates [Eq. 3.1-(1)]
  577.         final T da =  field.getZero();
  578.         final T dh =  udu.getdUdk().multiply(context.getBoA()).add(pUAGmIqUBGoAB.multiply(auxiliaryElements.getK()));
  579.         final T dk =  (udu.getdUdh().multiply(context.getBoA()).negate()).subtract(pUAGmIqUBGoAB.multiply(auxiliaryElements.getH()));
  580.         final T dp =  UBetaGamma.multiply(context.getMCo2AB());
  581.         final T dq =  UAlphaGamma.multiply(context.getMCo2AB()).multiply(I);
  582.         final T dM =  pUAGmIqUBGoAB.add(udu.getdUda().multiply(context.getM2aoA())).add((udu.getdUdh().multiply(auxiliaryElements.getH()).add(udu.getdUdk().multiply(auxiliaryElements.getK()))).multiply(context.getBoABpo()));

  583.         final T[] elements =  MathArrays.buildArray(field, 6);
  584.         elements[0] = da;
  585.         elements[1] = dk;
  586.         elements[2] = dh;
  587.         elements[3] = dq;
  588.         elements[4] = dp;
  589.         elements[5] = dM;

  590.         return elements;
  591.     }

  592.     /** {@inheritDoc} */
  593.     @Override
  594.     public void registerAttitudeProvider(final AttitudeProvider attitudeProvider) {
  595.         //nothing is done since this contribution is not sensitive to attitude
  596.     }

  597.     /** Check if an index is within the accepted interval.
  598.      *
  599.      * @param index the index to check
  600.      * @param lowerBound the lower bound of the interval
  601.      * @param upperBound the upper bound of the interval
  602.      * @return true if the index is between the lower and upper bounds, false otherwise
  603.      */
  604.     private boolean isBetween(final int index, final int lowerBound, final int upperBound) {
  605.         return index >= lowerBound && index <= upperBound;
  606.     }

  607.     /** {@inheritDoc} */
  608.     @Override
  609.     public void updateShortPeriodTerms(final double[] parameters, final SpacecraftState... meanStates) {

  610.         final Slot slot = zonalSPCoefs.createSlot(meanStates);
  611.         for (final SpacecraftState meanState : meanStates) {

  612.             // Auxiliary elements related to the current orbit
  613.             final AuxiliaryElements auxiliaryElements = new AuxiliaryElements(meanState.getOrbit(), I);

  614.             // Container of attributes
  615.             final DSSTZonalContext context = initializeStep(auxiliaryElements, parameters);

  616.             // Access to potential U derivatives
  617.             final UAnddU udu = new UAnddU(meanState.getDate(), context, auxiliaryElements, hansen);

  618.             // Compute rhoj and sigmaj
  619.             final double[][] rhoSigma = computeRhoSigmaCoefficients(meanState.getDate(), slot, auxiliaryElements);

  620.             // Compute Di
  621.             computeDiCoefficients(meanState.getDate(), slot, context, udu);

  622.             // generate the Cij and Sij coefficients
  623.             final FourierCjSjCoefficients cjsj = new FourierCjSjCoefficients(meanState.getDate(),
  624.                                                                              maxDegreeShortPeriodics,
  625.                                                                              maxEccPowShortPeriodics,
  626.                                                                              maxFrequencyShortPeriodics,
  627.                                                                              context,
  628.                                                                              hansen);

  629.             computeCijSijCoefficients(meanState.getDate(), slot, cjsj, rhoSigma, context, auxiliaryElements, udu);
  630.         }

  631.     }

  632.     /** {@inheritDoc} */
  633.     @Override
  634.     @SuppressWarnings("unchecked")
  635.     public <T extends RealFieldElement<T>> void updateShortPeriodTerms(final T[] parameters,
  636.                                                                        final FieldSpacecraftState<T>... meanStates) {

  637.         // Field used by default
  638.         final Field<T> field = meanStates[0].getDate().getField();

  639.         final FieldZonalShortPeriodicCoefficients<T> fzspc = (FieldZonalShortPeriodicCoefficients<T>) zonalFieldSPCoefs.get(field);
  640.         final FieldSlot<T> slot = fzspc.createSlot(meanStates);
  641.         for (final FieldSpacecraftState<T> meanState : meanStates) {

  642.             // Auxiliary elements related to the current orbit
  643.             final FieldAuxiliaryElements<T> auxiliaryElements = new FieldAuxiliaryElements<>(meanState.getOrbit(), I);

  644.             // Container of attributes
  645.             final FieldDSSTZonalContext<T> context = initializeStep(auxiliaryElements, parameters);

  646.             final FieldHansenObjects<T> fho = (FieldHansenObjects<T>) fieldHansen.get(field);

  647.             // Access to potential U derivatives
  648.             final FieldUAnddU<T> udu = new FieldUAnddU<>(meanState.getDate(), context, auxiliaryElements, fho);

  649.             // Compute rhoj and sigmaj
  650.             final T[][] rhoSigma = computeRhoSigmaCoefficients(meanState.getDate(), slot, auxiliaryElements, field);

  651.             // Compute Di
  652.             computeDiCoefficients(meanState.getDate(), slot, context, field, udu);

  653.             // generate the Cij and Sij coefficients
  654.             final FieldFourierCjSjCoefficients<T> cjsj = new FieldFourierCjSjCoefficients<>(meanState.getDate(),
  655.                                                                                             maxDegreeShortPeriodics,
  656.                                                                                             maxEccPowShortPeriodics,
  657.                                                                                             maxFrequencyShortPeriodics,
  658.                                                                                             context,
  659.                                                                                             fho);


  660.             computeCijSijCoefficients(meanState.getDate(), slot, cjsj, rhoSigma, context, auxiliaryElements, field, udu);
  661.         }
  662.     }

  663.     /** {@inheritDoc} */
  664.     public ParameterDriver[] getParametersDrivers() {
  665.         return new ParameterDriver[] {
  666.             gmParameterDriver
  667.         };
  668.     }

  669.     /** Generate the values for the D<sub>i</sub> coefficients.
  670.      * @param date target date
  671.      * @param slot slot to which the coefficients belong
  672.      * @param context container for attributes
  673.      * @param udu derivatives of the gravitational potential U
  674.      */
  675.     private void computeDiCoefficients(final AbsoluteDate date,
  676.                                        final Slot slot,
  677.                                        final DSSTZonalContext context,
  678.                                        final UAnddU udu) {

  679.         final double[] meanElementRates = computeMeanElementRates(date, context, udu);

  680.         final double[] currentDi = new double[6];

  681.         // Add the coefficients to the interpolation grid
  682.         for (int i = 0; i < 6; i++) {
  683.             currentDi[i] = meanElementRates[i] / context.getMeanMotion();

  684.             if (i == 5) {
  685.                 currentDi[i] += -1.5 * 2 * udu.getU() * context.getOON2A2();
  686.             }

  687.         }

  688.         slot.di.addGridPoint(date, currentDi);

  689.     }

  690.     /** Generate the values for the D<sub>i</sub> coefficients.
  691.      * @param <T> type of the elements
  692.      * @param date target date
  693.      * @param slot slot to which the coefficients belong
  694.      * @param context container for attributes
  695.      * @param field field used by default
  696.      * @param udu derivatives of the gravitational potential U
  697.      */
  698.     private <T extends RealFieldElement<T>> void computeDiCoefficients(final FieldAbsoluteDate<T> date,
  699.                                                                        final FieldSlot<T> slot,
  700.                                                                        final FieldDSSTZonalContext<T> context,
  701.                                                                        final Field<T> field,
  702.                                                                        final FieldUAnddU<T> udu) {

  703.         final T[] meanElementRates = computeMeanElementRates(date, context, udu);

  704.         final T[] currentDi = MathArrays.buildArray(field, 6);

  705.         // Add the coefficients to the interpolation grid
  706.         for (int i = 0; i < 6; i++) {
  707.             currentDi[i] = meanElementRates[i].divide(context.getMeanMotion());

  708.             if (i == 5) {
  709.                 currentDi[i] = currentDi[i].add(context.getOON2A2().multiply(udu.getU()).multiply(2.).multiply(-1.5));
  710.             }

  711.         }

  712.         slot.di.addGridPoint(date, currentDi);

  713.     }

  714.     /**
  715.      * Generate the values for the C<sub>i</sub><sup>j</sup> and the S<sub>i</sub><sup>j</sup> coefficients.
  716.      * @param date date of computation
  717.      * @param slot slot to which the coefficients belong
  718.      * @param cjsj Fourier coefficients
  719.      * @param rhoSigma ρ<sub>j</sub> and σ<sub>j</sub>
  720.      * @param context container for attributes
  721.      * @param auxiliaryElements auxiliary elements related to the current orbit
  722.      * @param udu derivatives of the gravitational potential U
  723.      */
  724.     private void computeCijSijCoefficients(final AbsoluteDate date, final Slot slot,
  725.                                            final FourierCjSjCoefficients cjsj,
  726.                                            final double[][] rhoSigma, final DSSTZonalContext context,
  727.                                            final AuxiliaryElements auxiliaryElements,
  728.                                            final UAnddU udu) {

  729.         final int nMax = maxDegreeShortPeriodics;

  730.         // The C<sub>i</sub>⁰ coefficients
  731.         final double[] currentCi0 = new double[] {0., 0., 0., 0., 0., 0.};
  732.         for (int j = 1; j < slot.cij.length; j++) {

  733.             // Create local arrays
  734.             final double[] currentCij = new double[] {0., 0., 0., 0., 0., 0.};
  735.             final double[] currentSij = new double[] {0., 0., 0., 0., 0., 0.};

  736.             // j == 1
  737.             if (j == 1) {
  738.                 final double coef1 = 4 * auxiliaryElements.getK() * udu.getU() - context.getHK() * cjsj.getCj(1) + context.getK2MH2O2() * cjsj.getSj(1);
  739.                 final double coef2 = 4 * auxiliaryElements.getH() * udu.getU() + context.getK2MH2O2() * cjsj.getCj(1) + context.getHK() * cjsj.getSj(1);
  740.                 final double coef3 = (auxiliaryElements.getK() * cjsj.getCj(1) + auxiliaryElements.getH() * cjsj.getSj(1)) / 4.;
  741.                 final double coef4 = (8 * udu.getU() - auxiliaryElements.getH() * cjsj.getCj(1) + auxiliaryElements.getK() * cjsj.getSj(1)) / 4.;

  742.                 //Coefficients for a
  743.                 currentCij[0] += coef1;
  744.                 currentSij[0] += coef2;

  745.                 //Coefficients for k
  746.                 currentCij[1] += coef4;
  747.                 currentSij[1] += coef3;

  748.                 //Coefficients for h
  749.                 currentCij[2] -= coef3;
  750.                 currentSij[2] += coef4;

  751.                 //Coefficients for λ
  752.                 currentCij[5] -= coef2 / 2;
  753.                 currentSij[5] += coef1 / 2;
  754.             }

  755.             // j == 2
  756.             if (j == 2) {
  757.                 final double coef1 = context.getK2MH2() * udu.getU();
  758.                 final double coef2 = 2 * context.getHK() * udu.getU();
  759.                 final double coef3 = auxiliaryElements.getH() * udu.getU() / 2;
  760.                 final double coef4 = auxiliaryElements.getK() * udu.getU() / 2;

  761.                 //Coefficients for a
  762.                 currentCij[0] += coef1;
  763.                 currentSij[0] += coef2;

  764.                 //Coefficients for k
  765.                 currentCij[1] += coef4;
  766.                 currentSij[1] += coef3;

  767.                 //Coefficients for h
  768.                 currentCij[2] -= coef3;
  769.                 currentSij[2] += coef4;

  770.                 //Coefficients for λ
  771.                 currentCij[5] -= coef2 / 2;
  772.                 currentSij[5] += coef1 / 2;
  773.             }

  774.             // j between 1 and 2N-3
  775.             if (isBetween(j, 1, 2 * nMax - 3) && j + 2 < cjsj.jMax) {
  776.                 final double coef1 = ( j + 2 ) * (-context.getHK() * cjsj.getCj(j + 2) + context.getK2MH2O2() * cjsj.getSj(j + 2));
  777.                 final double coef2 = ( j + 2 ) * (context.getK2MH2O2() * cjsj.getCj(j + 2) + context.getHK() * cjsj.getSj(j + 2));
  778.                 final double coef3 = ( j + 2 ) * (auxiliaryElements.getK() * cjsj.getCj(j + 2) + auxiliaryElements.getH() * cjsj.getSj(j + 2)) / 4;
  779.                 final double coef4 = ( j + 2 ) * (auxiliaryElements.getH() * cjsj.getCj(j + 2) - auxiliaryElements.getK() * cjsj.getSj(j + 2)) / 4;

  780.                 //Coefficients for a
  781.                 currentCij[0] += coef1;
  782.                 currentSij[0] -= coef2;

  783.                 //Coefficients for k
  784.                 currentCij[1] += -coef4;
  785.                 currentSij[1] -= coef3;

  786.                 //Coefficients for h
  787.                 currentCij[2] -= coef3;
  788.                 currentSij[2] += coef4;

  789.                 //Coefficients for λ
  790.                 currentCij[5] -= coef2 / 2;
  791.                 currentSij[5] += -coef1 / 2;
  792.             }

  793.             // j between 1 and 2N-2
  794.             if (isBetween(j, 1, 2 * nMax - 2) && j + 1 < cjsj.jMax) {
  795.                 final double coef1 = 2 * ( j + 1 ) * (-auxiliaryElements.getH() * cjsj.getCj(j + 1) + auxiliaryElements.getK() * cjsj.getSj(j + 1));
  796.                 final double coef2 = 2 * ( j + 1 ) * (auxiliaryElements.getK() * cjsj.getCj(j + 1) + auxiliaryElements.getH() * cjsj.getSj(j + 1));
  797.                 final double coef3 = ( j + 1 ) * cjsj.getCj(j + 1);
  798.                 final double coef4 = ( j + 1 ) * cjsj.getSj(j + 1);

  799.                 //Coefficients for a
  800.                 currentCij[0] += coef1;
  801.                 currentSij[0] -= coef2;

  802.                 //Coefficients for k
  803.                 currentCij[1] += coef4;
  804.                 currentSij[1] -= coef3;

  805.                 //Coefficients for h
  806.                 currentCij[2] -= coef3;
  807.                 currentSij[2] -= coef4;

  808.                 //Coefficients for λ
  809.                 currentCij[5] -= coef2 / 2;
  810.                 currentSij[5] += -coef1 / 2;
  811.             }

  812.             // j between 2 and 2N
  813.             if (isBetween(j, 2, 2 * nMax) && j - 1 < cjsj.jMax) {
  814.                 final double coef1 = 2 * ( j - 1 ) * (auxiliaryElements.getH() * cjsj.getCj(j - 1) + auxiliaryElements.getK() * cjsj.getSj(j - 1));
  815.                 final double coef2 = 2 * ( j - 1 ) * (auxiliaryElements.getK() * cjsj.getCj(j - 1) - auxiliaryElements.getH() * cjsj.getSj(j - 1));
  816.                 final double coef3 = ( j - 1 ) * cjsj.getCj(j - 1);
  817.                 final double coef4 = ( j - 1 ) * cjsj.getSj(j - 1);

  818.                 //Coefficients for a
  819.                 currentCij[0] += coef1;
  820.                 currentSij[0] -= coef2;

  821.                 //Coefficients for k
  822.                 currentCij[1] += coef4;
  823.                 currentSij[1] -= coef3;

  824.                 //Coefficients for h
  825.                 currentCij[2] += coef3;
  826.                 currentSij[2] += coef4;

  827.                 //Coefficients for λ
  828.                 currentCij[5] += coef2 / 2;
  829.                 currentSij[5] += coef1 / 2;
  830.             }

  831.             // j between 3 and 2N + 1
  832.             if (isBetween(j, 3, 2 * nMax + 1) && j - 2 < cjsj.jMax) {
  833.                 final double coef1 = ( j - 2 ) * (context.getHK() * cjsj.getCj(j - 2) + context.getK2MH2O2() * cjsj.getSj(j - 2));
  834.                 final double coef2 = ( j - 2 ) * (-context.getK2MH2O2() * cjsj.getCj(j - 2) + context.getHK() * cjsj.getSj(j - 2));
  835.                 final double coef3 = ( j - 2 ) * (auxiliaryElements.getK() * cjsj.getCj(j - 2) - auxiliaryElements.getH() * cjsj.getSj(j - 2)) / 4;
  836.                 final double coef4 = ( j - 2 ) * (auxiliaryElements.getH() * cjsj.getCj(j - 2) + auxiliaryElements.getK() * cjsj.getSj(j - 2)) / 4;
  837.                 final double coef5 = ( j - 2 ) * (context.getK2MH2O2() * cjsj.getCj(j - 2) - context.getHK() * cjsj.getSj(j - 2));

  838.                 //Coefficients for a
  839.                 currentCij[0] += coef1;
  840.                 currentSij[0] += coef2;

  841.                 //Coefficients for k
  842.                 currentCij[1] += coef4;
  843.                 currentSij[1] += -coef3;

  844.                 //Coefficients for h
  845.                 currentCij[2] += coef3;
  846.                 currentSij[2] += coef4;

  847.                 //Coefficients for λ
  848.                 currentCij[5] += coef5 / 2;
  849.                 currentSij[5] += coef1 / 2;
  850.             }

  851.             //multiply by the common factor
  852.             //for a (i == 0) -> χ³ / (n² * a)
  853.             currentCij[0] *= context.getX3ON2A();
  854.             currentSij[0] *= context.getX3ON2A();
  855.             //for k (i == 1) -> χ / (n² * a²)
  856.             currentCij[1] *= context.getXON2A2();
  857.             currentSij[1] *= context.getXON2A2();
  858.             //for h (i == 2) -> χ / (n² * a²)
  859.             currentCij[2] *= context.getXON2A2();
  860.             currentSij[2] *= context.getXON2A2();
  861.             //for λ (i == 5) -> (χ²) / (n² * a² * (χ + 1 ) )
  862.             currentCij[5] *= context.getX2ON2A2XP1();
  863.             currentSij[5] *= context.getX2ON2A2XP1();

  864.             // j is between 1 and 2 * N - 1
  865.             if (isBetween(j, 1, 2 * nMax - 1) && j < cjsj.jMax) {
  866.                 // Compute cross derivatives
  867.                 // Cj(alpha,gamma) = alpha * dC/dgamma - gamma * dC/dalpha
  868.                 final double CjAlphaGamma   = auxiliaryElements.getAlpha() * cjsj.getdCjdGamma(j) - auxiliaryElements.getGamma() * cjsj.getdCjdAlpha(j);
  869.                 // Cj(alpha,beta) = alpha * dC/dbeta - beta * dC/dalpha
  870.                 final double CjAlphaBeta   = auxiliaryElements.getAlpha() * cjsj.getdCjdBeta(j) - auxiliaryElements.getBeta() * cjsj.getdCjdAlpha(j);
  871.                 // Cj(beta,gamma) = beta * dC/dgamma - gamma * dC/dbeta
  872.                 final double CjBetaGamma    =  auxiliaryElements.getBeta() * cjsj.getdCjdGamma(j) - auxiliaryElements.getGamma() * cjsj.getdCjdBeta(j);
  873.                 // Cj(h,k) = h * dC/dk - k * dC/dh
  874.                 final double CjHK   = auxiliaryElements.getH() * cjsj.getdCjdK(j) - auxiliaryElements.getK() * cjsj.getdCjdH(j);
  875.                 // Sj(alpha,gamma) = alpha * dS/dgamma - gamma * dS/dalpha
  876.                 final double SjAlphaGamma   = auxiliaryElements.getAlpha() * cjsj.getdSjdGamma(j) - auxiliaryElements.getGamma() * cjsj.getdSjdAlpha(j);
  877.                 // Sj(alpha,beta) = alpha * dS/dbeta - beta * dS/dalpha
  878.                 final double SjAlphaBeta   = auxiliaryElements.getAlpha() * cjsj.getdSjdBeta(j) - auxiliaryElements.getBeta() * cjsj.getdSjdAlpha(j);
  879.                 // Sj(beta,gamma) = beta * dS/dgamma - gamma * dS/dbeta
  880.                 final double SjBetaGamma    =  auxiliaryElements.getBeta() * cjsj.getdSjdGamma(j) - auxiliaryElements.getGamma() * cjsj.getdSjdBeta(j);
  881.                 // Sj(h,k) = h * dS/dk - k * dS/dh
  882.                 final double SjHK   = auxiliaryElements.getH() * cjsj.getdSjdK(j) - auxiliaryElements.getK() * cjsj.getdSjdH(j);

  883.                 //Coefficients for a
  884.                 final double coef1 = context.getX3ON2A() * (3 - context.getBB()) * j;
  885.                 currentCij[0] += coef1 * cjsj.getSj(j);
  886.                 currentSij[0] -= coef1 * cjsj.getCj(j);

  887.                 //Coefficients for k and h
  888.                 final double coef2 = auxiliaryElements.getP() * CjAlphaGamma - I * auxiliaryElements.getQ() * CjBetaGamma;
  889.                 final double coef3 = auxiliaryElements.getP() * SjAlphaGamma - I * auxiliaryElements.getQ() * SjBetaGamma;
  890.                 currentCij[1] -= context.getXON2A2() * (auxiliaryElements.getH() * coef2 + context.getBB() * cjsj.getdCjdH(j) - 1.5 * auxiliaryElements.getK() * j * cjsj.getSj(j));
  891.                 currentSij[1] -= context.getXON2A2() * (auxiliaryElements.getH() * coef3 + context.getBB() * cjsj.getdSjdH(j) + 1.5 * auxiliaryElements.getK() * j * cjsj.getCj(j));
  892.                 currentCij[2] += context.getXON2A2() * (auxiliaryElements.getK() * coef2 + context.getBB() * cjsj.getdCjdK(j) + 1.5 * auxiliaryElements.getH() * j * cjsj.getSj(j));
  893.                 currentSij[2] += context.getXON2A2() * (auxiliaryElements.getK() * coef3 + context.getBB() * cjsj.getdSjdK(j) - 1.5 * auxiliaryElements.getH() * j * cjsj.getCj(j));

  894.                 //Coefficients for q and p
  895.                 final double coef4 = CjHK - CjAlphaBeta - j * cjsj.getSj(j);
  896.                 final double coef5 = SjHK - SjAlphaBeta + j * cjsj.getCj(j);
  897.                 currentCij[3] = context.getCXO2N2A2() * (-I * CjAlphaGamma + auxiliaryElements.getQ() * coef4);
  898.                 currentSij[3] = context.getCXO2N2A2() * (-I * SjAlphaGamma + auxiliaryElements.getQ() * coef5);
  899.                 currentCij[4] = context.getCXO2N2A2() * (-CjBetaGamma + auxiliaryElements.getP() * coef4);
  900.                 currentSij[4] = context.getCXO2N2A2() * (-SjBetaGamma + auxiliaryElements.getP() * coef5);

  901.                 //Coefficients for λ
  902.                 final double coef6 = auxiliaryElements.getH() * cjsj.getdCjdH(j) + auxiliaryElements.getK() * cjsj.getdCjdK(j);
  903.                 final double coef7 = auxiliaryElements.getH() * cjsj.getdSjdH(j) + auxiliaryElements.getK() * cjsj.getdSjdK(j);
  904.                 currentCij[5] += context.getOON2A2() * (-2 * auxiliaryElements.getSma() * cjsj.getdCjdA(j) + coef6 / (context.getX() + 1) + context.getX() * coef2 - 3 * cjsj.getCj(j));
  905.                 currentSij[5] += context.getOON2A2() * (-2 * auxiliaryElements.getSma() * cjsj.getdSjdA(j) + coef7 / (context.getX() + 1) + context.getX() * coef3 - 3 * cjsj.getSj(j));
  906.             }

  907.             for (int i = 0; i < 6; i++) {
  908.                 //Add the current coefficients contribution to C<sub>i</sub>⁰
  909.                 currentCi0[i] -= currentCij[i] * rhoSigma[j][0] + currentSij[i] * rhoSigma[j][1];
  910.             }

  911.             // Add the coefficients to the interpolation grid
  912.             slot.cij[j].addGridPoint(date, currentCij);
  913.             slot.sij[j].addGridPoint(date, currentSij);

  914.         }

  915.         //Add C<sub>i</sub>⁰ to the interpolation grid
  916.         slot.cij[0].addGridPoint(date, currentCi0);

  917.     }

  918.     /**
  919.      * Generate the values for the C<sub>i</sub><sup>j</sup> and the S<sub>i</sub><sup>j</sup> coefficients.
  920.      * @param <T> type of the elements
  921.      * @param date date of computation
  922.      * @param slot slot to which the coefficients belong
  923.      * @param cjsj Fourier coefficients
  924.      * @param rhoSigma ρ<sub>j</sub> and σ<sub>j</sub>
  925.      * @param context container for attributes
  926.      * @param auxiliaryElements auxiliary elements related to the current orbit
  927.      * @param field field used by default
  928.      * @param udu derivatives of the gravitational potential U
  929.      */
  930.     private <T extends RealFieldElement<T>> void computeCijSijCoefficients(final FieldAbsoluteDate<T> date,
  931.                                                                            final FieldSlot<T> slot,
  932.                                                                            final FieldFourierCjSjCoefficients<T> cjsj,
  933.                                                                            final T[][] rhoSigma,
  934.                                                                            final FieldDSSTZonalContext<T> context,
  935.                                                                            final FieldAuxiliaryElements<T> auxiliaryElements,
  936.                                                                            final Field<T> field,
  937.                                                                            final FieldUAnddU<T> udu) {

  938.         // Zero
  939.         final T zero = field.getZero();

  940.         final int nMax = maxDegreeShortPeriodics;

  941.         // The C<sub>i</sub>⁰ coefficients
  942.         final T[] currentCi0 = MathArrays.buildArray(field, 6);
  943.         Arrays.fill(currentCi0, zero);

  944.         for (int j = 1; j < slot.cij.length; j++) {

  945.             // Create local arrays
  946.             final T[] currentCij = MathArrays.buildArray(field, 6);
  947.             final T[] currentSij = MathArrays.buildArray(field, 6);

  948.             Arrays.fill(currentCij, zero);
  949.             Arrays.fill(currentSij, zero);

  950.             // j == 1
  951.             if (j == 1) {
  952.                 final T coef1 = auxiliaryElements.getK().multiply(udu.getU()).multiply(4.).subtract(context.getHK().multiply(cjsj.getCj(1))).add(context.getK2MH2O2().multiply(cjsj.getSj(1)));
  953.                 final T coef2 = auxiliaryElements.getH().multiply(udu.getU()).multiply(4.).add(context.getK2MH2O2().multiply(cjsj.getCj(1))).add(context.getHK().multiply(cjsj.getSj(1)));
  954.                 final T coef3 = auxiliaryElements.getK().multiply(cjsj.getCj(1)).add(auxiliaryElements.getH().multiply(cjsj.getSj(1))).divide(4.);
  955.                 final T coef4 = udu.getU().multiply(8.).subtract(auxiliaryElements.getH().multiply(cjsj.getCj(1))).add(auxiliaryElements.getK().multiply(cjsj.getSj(1))).divide(4.);

  956.                 //Coefficients for a
  957.                 currentCij[0] = currentCij[0].add(coef1);
  958.                 currentSij[0] = currentSij[0].add(coef2);

  959.                 //Coefficients for k
  960.                 currentCij[1] = currentCij[1].add(coef4);
  961.                 currentSij[1] = currentSij[1].add(coef3);

  962.                 //Coefficients for h
  963.                 currentCij[2] = currentCij[2].subtract(coef3);
  964.                 currentSij[2] = currentSij[2].add(coef4);

  965.                 //Coefficients for λ
  966.                 currentCij[5] = currentCij[5].subtract(coef2.divide(2.));
  967.                 currentSij[5] = currentSij[5].add(coef1.divide(2.));
  968.             }

  969.             // j == 2
  970.             if (j == 2) {
  971.                 final T coef1 = context.getK2MH2().multiply(udu.getU());
  972.                 final T coef2 = context.getHK().multiply(udu.getU()).multiply(2.);
  973.                 final T coef3 = auxiliaryElements.getH().multiply(udu.getU()).divide(2.);
  974.                 final T coef4 = auxiliaryElements.getK().multiply(udu.getU()).divide(2.);

  975.                 //Coefficients for a
  976.                 currentCij[0] = currentCij[0].add(coef1);
  977.                 currentSij[0] = currentSij[0].add(coef2);

  978.                 //Coefficients for k
  979.                 currentCij[1] = currentCij[1].add(coef4);
  980.                 currentSij[1] = currentSij[1].add(coef3);

  981.                 //Coefficients for h
  982.                 currentCij[2] = currentCij[2].subtract(coef3);
  983.                 currentSij[2] = currentSij[2].add(coef4);

  984.                 //Coefficients for λ
  985.                 currentCij[5] = currentCij[5].subtract(coef2.divide(2.));
  986.                 currentSij[5] = currentSij[5].add(coef1.divide(2.));
  987.             }

  988.             // j between 1 and 2N-3
  989.             if (isBetween(j, 1, 2 * nMax - 3) && j + 2 < cjsj.jMax) {
  990.                 final T coef1 = context.getHK().negate().multiply(cjsj.getCj(j + 2)).add(context.getK2MH2O2().multiply(cjsj.getSj(j + 2))).multiply(j + 2);
  991.                 final T coef2 = context.getK2MH2O2().multiply(cjsj.getCj(j + 2)).add(context.getHK().multiply(cjsj.getSj(j + 2))).multiply(j + 2);
  992.                 final T coef3 = auxiliaryElements.getK().multiply(cjsj.getCj(j + 2)).add(auxiliaryElements.getH().multiply(cjsj.getSj(j + 2))).multiply(j + 2).divide(4.);
  993.                 final T coef4 = auxiliaryElements.getH().multiply(cjsj.getCj(j + 2)).subtract(auxiliaryElements.getK().multiply(cjsj.getSj(j + 2))).multiply(j + 2).divide(4.);

  994.                 //Coefficients for a
  995.                 currentCij[0] = currentCij[0].add(coef1);
  996.                 currentSij[0] = currentSij[0].subtract(coef2);

  997.                 //Coefficients for k
  998.                 currentCij[1] = currentCij[1].add(coef4.negate());
  999.                 currentSij[1] = currentSij[1].subtract(coef3);

  1000.                 //Coefficients for h
  1001.                 currentCij[2] = currentCij[2].subtract(coef3);
  1002.                 currentSij[2] = currentSij[2].add(coef4);

  1003.                 //Coefficients for λ
  1004.                 currentCij[5] = currentCij[5].subtract(coef2.divide(2.));
  1005.                 currentSij[5] = currentSij[5].add(coef1.negate().divide(2.));
  1006.             }

  1007.             // j between 1 and 2N-2
  1008.             if (isBetween(j, 1, 2 * nMax - 2) && j + 1 < cjsj.jMax) {
  1009.                 final T coef1 = auxiliaryElements.getH().negate().multiply(cjsj.getCj(j + 1)).add(auxiliaryElements.getK().multiply(cjsj.getSj(j + 1))).multiply(2. * (j + 1));
  1010.                 final T coef2 = auxiliaryElements.getK().multiply(cjsj.getCj(j + 1)).add(auxiliaryElements.getH().multiply(cjsj.getSj(j + 1))).multiply(2. * (j + 1));
  1011.                 final T coef3 = cjsj.getCj(j + 1).multiply(j + 1);
  1012.                 final T coef4 = cjsj.getSj(j + 1).multiply(j + 1);

  1013.                 //Coefficients for a
  1014.                 currentCij[0] = currentCij[0].add(coef1);
  1015.                 currentSij[0] = currentSij[0].subtract(coef2);

  1016.                 //Coefficients for k
  1017.                 currentCij[1] = currentCij[1].add(coef4);
  1018.                 currentSij[1] = currentSij[1].subtract(coef3);

  1019.                 //Coefficients for h
  1020.                 currentCij[2] = currentCij[2].subtract(coef3);
  1021.                 currentSij[2] = currentSij[2].subtract(coef4);

  1022.                 //Coefficients for λ
  1023.                 currentCij[5] = currentCij[5].subtract(coef2.divide(2.));
  1024.                 currentSij[5] = currentSij[5].add(coef1.negate().divide(2.));
  1025.             }

  1026.             // j between 2 and 2N
  1027.             if (isBetween(j, 2, 2 * nMax) && j - 1 < cjsj.jMax) {
  1028.                 final T coef1 = auxiliaryElements.getH().multiply(cjsj.getCj(j - 1)).add(auxiliaryElements.getK().multiply(cjsj.getSj(j - 1))).multiply(2 * ( j - 1 ));
  1029.                 final T coef2 = auxiliaryElements.getK().multiply(cjsj.getCj(j - 1)).subtract(auxiliaryElements.getH().multiply(cjsj.getSj(j - 1))).multiply(2 * ( j - 1 ));
  1030.                 final T coef3 = cjsj.getCj(j - 1).multiply(j - 1);
  1031.                 final T coef4 = cjsj.getSj(j - 1).multiply(j - 1);

  1032.                 //Coefficients for a
  1033.                 currentCij[0] = currentCij[0].add(coef1);
  1034.                 currentSij[0] = currentSij[0].subtract(coef2);

  1035.                 //Coefficients for k
  1036.                 currentCij[1] = currentCij[1].add(coef4);
  1037.                 currentSij[1] = currentSij[1].subtract(coef3);

  1038.                 //Coefficients for h
  1039.                 currentCij[2] = currentCij[2].add(coef3);
  1040.                 currentSij[2] = currentSij[2].add(coef4);

  1041.                 //Coefficients for λ
  1042.                 currentCij[5] = currentCij[5].add(coef2.divide(2.));
  1043.                 currentSij[5] = currentSij[5].add(coef1.divide(2.));
  1044.             }

  1045.             // j between 3 and 2N + 1
  1046.             if (isBetween(j, 3, 2 * nMax + 1) && j - 2 < cjsj.jMax) {
  1047.                 final T coef1 = context.getHK().multiply(cjsj.getCj(j - 2)).add(context.getK2MH2O2().multiply(cjsj.getSj(j - 2))).multiply(j - 2);
  1048.                 final T coef2 = context.getK2MH2O2().negate().multiply(cjsj.getCj(j - 2)).add(context.getHK().multiply(cjsj.getSj(j - 2))).multiply(j - 2);
  1049.                 final T coef3 = auxiliaryElements.getK().multiply(cjsj.getCj(j - 2)).subtract(auxiliaryElements.getH().multiply(cjsj.getSj(j - 2))).multiply(j - 2).divide(4.);
  1050.                 final T coef4 = auxiliaryElements.getH().multiply(cjsj.getCj(j - 2)).add(auxiliaryElements.getK().multiply(cjsj.getSj(j - 2))).multiply(j - 2).divide(4.);
  1051.                 final T coef5 = context.getK2MH2O2().multiply(cjsj.getCj(j - 2)).subtract(context.getHK().multiply(cjsj.getSj(j - 2))).multiply(j - 2);

  1052.                 //Coefficients for a
  1053.                 currentCij[0] = currentCij[0].add(coef1);
  1054.                 currentSij[0] = currentSij[0].add(coef2);

  1055.                 //Coefficients for k
  1056.                 currentCij[1] = currentCij[1].add(coef4);
  1057.                 currentSij[1] = currentSij[1].add(coef3.negate());

  1058.                 //Coefficients for h
  1059.                 currentCij[2] = currentCij[2].add(coef3);
  1060.                 currentSij[2] = currentSij[2].add(coef4);

  1061.                 //Coefficients for λ
  1062.                 currentCij[5] = currentCij[5].add(coef5.divide(2.));
  1063.                 currentSij[5] = currentSij[5].add(coef1.divide(2.));
  1064.             }

  1065.             //multiply by the common factor
  1066.             //for a (i == 0) -> χ³ / (n² * a)
  1067.             currentCij[0] = currentCij[0].multiply(context.getX3ON2A());
  1068.             currentSij[0] = currentSij[0].multiply(context.getX3ON2A());
  1069.             //for k (i == 1) -> χ / (n² * a²)
  1070.             currentCij[1] = currentCij[1].multiply(context.getXON2A2());
  1071.             currentSij[1] = currentSij[1].multiply(context.getXON2A2());
  1072.             //for h (i == 2) -> χ / (n² * a²)
  1073.             currentCij[2] = currentCij[2].multiply(context.getXON2A2());
  1074.             currentSij[2] = currentSij[2].multiply(context.getXON2A2());
  1075.             //for λ (i == 5) -> (χ²) / (n² * a² * (χ + 1 ) )
  1076.             currentCij[5] = currentCij[5].multiply(context.getX2ON2A2XP1());
  1077.             currentSij[5] = currentSij[5].multiply(context.getX2ON2A2XP1());

  1078.             // j is between 1 and 2 * N - 1
  1079.             if (isBetween(j, 1, 2 * nMax - 1) && j < cjsj.jMax) {
  1080.                 // Compute cross derivatives
  1081.                 // Cj(alpha,gamma) = alpha * dC/dgamma - gamma * dC/dalpha
  1082.                 final T CjAlphaGamma = auxiliaryElements.getAlpha().multiply(cjsj.getdCjdGamma(j)).subtract(auxiliaryElements.getGamma().multiply(cjsj.getdCjdAlpha(j)));
  1083.                 // Cj(alpha,beta) = alpha * dC/dbeta - beta * dC/dalpha
  1084.                 final T CjAlphaBeta  = auxiliaryElements.getAlpha().multiply(cjsj.getdCjdBeta(j)).subtract(auxiliaryElements.getBeta().multiply(cjsj.getdCjdAlpha(j)));
  1085.                 // Cj(beta,gamma) = beta * dC/dgamma - gamma * dC/dbeta
  1086.                 final T CjBetaGamma  = auxiliaryElements.getBeta().multiply(cjsj.getdCjdGamma(j)).subtract(auxiliaryElements.getGamma().multiply(cjsj.getdCjdBeta(j)));
  1087.                 // Cj(h,k) = h * dC/dk - k * dC/dh
  1088.                 final T CjHK         = auxiliaryElements.getH().multiply(cjsj.getdCjdK(j)).subtract(auxiliaryElements.getK().multiply(cjsj.getdCjdH(j)));
  1089.                 // Sj(alpha,gamma) = alpha * dS/dgamma - gamma * dS/dalpha
  1090.                 final T SjAlphaGamma = auxiliaryElements.getAlpha().multiply(cjsj.getdSjdGamma(j)).subtract(auxiliaryElements.getGamma().multiply(cjsj.getdSjdAlpha(j)));
  1091.                 // Sj(alpha,beta) = alpha * dS/dbeta - beta * dS/dalpha
  1092.                 final T SjAlphaBeta  = auxiliaryElements.getAlpha().multiply(cjsj.getdSjdBeta(j)).subtract(auxiliaryElements.getBeta().multiply(cjsj.getdSjdAlpha(j)));
  1093.                 // Sj(beta,gamma) = beta * dS/dgamma - gamma * dS/dbeta
  1094.                 final T SjBetaGamma  = auxiliaryElements.getBeta().multiply(cjsj.getdSjdGamma(j)).subtract(auxiliaryElements.getGamma().multiply(cjsj.getdSjdBeta(j)));
  1095.                 // Sj(h,k) = h * dS/dk - k * dS/dh
  1096.                 final T SjHK         = auxiliaryElements.getH().multiply(cjsj.getdSjdK(j)).subtract(auxiliaryElements.getK().multiply(cjsj.getdSjdH(j)));

  1097.                 //Coefficients for a
  1098.                 final T coef1 = context.getX3ON2A().multiply(context.getBB().negate().add(3.)).multiply(j);
  1099.                 currentCij[0] = currentCij[0].add(coef1.multiply(cjsj.getSj(j)));
  1100.                 currentSij[0] = currentSij[0].subtract(coef1.multiply(cjsj.getCj(j)));

  1101.                 //Coefficients for k and h
  1102.                 final T coef2 = auxiliaryElements.getP().multiply(CjAlphaGamma).subtract(auxiliaryElements.getQ().multiply(CjBetaGamma).multiply(I));
  1103.                 final T coef3 = auxiliaryElements.getP().multiply(SjAlphaGamma).subtract(auxiliaryElements.getQ().multiply(SjBetaGamma).multiply(I));
  1104.                 currentCij[1] = currentCij[1].subtract(context.getXON2A2().multiply(auxiliaryElements.getH().multiply(coef2).add(context.getBB().multiply(cjsj.getdCjdH(j))).subtract(auxiliaryElements.getK().multiply(1.5).multiply(j).multiply(cjsj.getSj(j)))));
  1105.                 currentSij[1] = currentSij[1].subtract(context.getXON2A2().multiply(auxiliaryElements.getH().multiply(coef3).add(context.getBB().multiply(cjsj.getdSjdH(j))).add(auxiliaryElements.getK().multiply(1.5).multiply(j).multiply(cjsj.getCj(j)))));
  1106.                 currentCij[2] = currentCij[2].add(context.getXON2A2().multiply(auxiliaryElements.getK().multiply(coef2).add(context.getBB().multiply(cjsj.getdCjdK(j))).add(auxiliaryElements.getH().multiply(1.5).multiply(j).multiply(cjsj.getSj(j)))));
  1107.                 currentSij[2] = currentSij[2].add(context.getXON2A2().multiply(auxiliaryElements.getK().multiply(coef3).add(context.getBB().multiply(cjsj.getdSjdK(j))).subtract(auxiliaryElements.getH().multiply(1.5).multiply(j).multiply(cjsj.getCj(j)))));

  1108.                 //Coefficients for q and p
  1109.                 final T coef4 = CjHK.subtract(CjAlphaBeta).subtract(cjsj.getSj(j).multiply(j));
  1110.                 final T coef5 = SjHK.subtract(SjAlphaBeta).add(cjsj.getCj(j).multiply(j));
  1111.                 currentCij[3] = context.getCXO2N2A2().multiply(CjAlphaGamma.multiply(-I).add(auxiliaryElements.getQ().multiply(coef4)));
  1112.                 currentSij[3] = context.getCXO2N2A2().multiply(SjAlphaGamma.multiply(-I).add(auxiliaryElements.getQ().multiply(coef5)));
  1113.                 currentCij[4] = context.getCXO2N2A2().multiply(CjBetaGamma.negate().add(auxiliaryElements.getP().multiply(coef4)));
  1114.                 currentSij[4] = context.getCXO2N2A2().multiply(SjBetaGamma.negate().add(auxiliaryElements.getP().multiply(coef5)));

  1115.                 //Coefficients for λ
  1116.                 final T coef6 = auxiliaryElements.getH().multiply(cjsj.getdCjdH(j)).add(auxiliaryElements.getK().multiply(cjsj.getdCjdK(j)));
  1117.                 final T coef7 = auxiliaryElements.getH().multiply(cjsj.getdSjdH(j)).add(auxiliaryElements.getK().multiply(cjsj.getdSjdK(j)));
  1118.                 currentCij[5] = currentCij[5].add(context.getOON2A2().multiply(auxiliaryElements.getSma().multiply(-2.).multiply(cjsj.getdCjdA(j)).add(coef6.divide(context.getX().add(1.))).add(context.getX().multiply(coef2)).subtract(cjsj.getCj(j).multiply(3.))));
  1119.                 currentSij[5] = currentSij[5].add(context.getOON2A2().multiply(auxiliaryElements.getSma().multiply(-2.).multiply(cjsj.getdSjdA(j)).add(coef7.divide(context.getX().add(1.))).add(context.getX().multiply(coef3)).subtract(cjsj.getSj(j).multiply(3.))));
  1120.             }

  1121.             for (int i = 0; i < 6; i++) {
  1122.                 //Add the current coefficients contribution to C<sub>i</sub>⁰
  1123.                 currentCi0[i] = currentCi0[i].subtract(currentCij[i].multiply(rhoSigma[j][0]).add(currentSij[i].multiply(rhoSigma[j][1])));
  1124.             }

  1125.             // Add the coefficients to the interpolation grid
  1126.             slot.cij[j].addGridPoint(date, currentCij);
  1127.             slot.sij[j].addGridPoint(date, currentSij);

  1128.         }

  1129.         //Add C<sub>i</sub>⁰ to the interpolation grid
  1130.         slot.cij[0].addGridPoint(date, currentCi0);

  1131.     }

  1132.     /**
  1133.      * Compute the auxiliary quantities ρ<sub>j</sub> and σ<sub>j</sub>.
  1134.      * <p>
  1135.      * The expressions used are equations 2.5.3-(4) from the Danielson paper. <br/>
  1136.      *  ρ<sub>j</sub> = (1+jB)(-b)<sup>j</sup>C<sub>j</sub>(k, h) <br/>
  1137.      *  σ<sub>j</sub> = (1+jB)(-b)<sup>j</sup>S<sub>j</sub>(k, h) <br/>
  1138.      * </p>
  1139.      * @param date target date
  1140.      * @param slot slot to which the coefficients belong
  1141.      * @param auxiliaryElements auxiliary elements related to the current orbit
  1142.      * @return array containing ρ<sub>j</sub> and σ<sub>j</sub>
  1143.      */
  1144.     private double[][] computeRhoSigmaCoefficients(final AbsoluteDate date,
  1145.                                                    final Slot slot,
  1146.                                                    final AuxiliaryElements auxiliaryElements) {

  1147.         final CjSjCoefficient cjsjKH = new CjSjCoefficient(auxiliaryElements.getK(), auxiliaryElements.getH());
  1148.         final double b = 1. / (1 + auxiliaryElements.getB());

  1149.         // (-b)<sup>j</sup>
  1150.         double mbtj = 1;

  1151.         final double[][] rhoSigma = new double[slot.cij.length][2];
  1152.         for (int j = 1; j < rhoSigma.length; j++) {

  1153.             //Compute current rho and sigma;
  1154.             mbtj *= -b;
  1155.             final double coef  = (1 + j * auxiliaryElements.getB()) * mbtj;
  1156.             final double rho   = coef * cjsjKH.getCj(j);
  1157.             final double sigma = coef * cjsjKH.getSj(j);

  1158.             // Add the coefficients to the interpolation grid
  1159.             rhoSigma[j][0] = rho;
  1160.             rhoSigma[j][1] = sigma;
  1161.         }

  1162.         return rhoSigma;

  1163.     }

  1164.     /**
  1165.      * Compute the auxiliary quantities ρ<sub>j</sub> and σ<sub>j</sub>.
  1166.      * <p>
  1167.      * The expressions used are equations 2.5.3-(4) from the Danielson paper. <br/>
  1168.      *  ρ<sub>j</sub> = (1+jB)(-b)<sup>j</sup>C<sub>j</sub>(k, h) <br/>
  1169.      *  σ<sub>j</sub> = (1+jB)(-b)<sup>j</sup>S<sub>j</sub>(k, h) <br/>
  1170.      * </p>
  1171.      * @param <T> type of the elements
  1172.      * @param date target date
  1173.      * @param slot slot to which the coefficients belong
  1174.      * @param auxiliaryElements auxiliary elements related to the current orbit
  1175.      * @param field field used by default
  1176.      * @return array containing ρ<sub>j</sub> and σ<sub>j</sub>
  1177.      */
  1178.     private <T extends RealFieldElement<T>> T[][] computeRhoSigmaCoefficients(final FieldAbsoluteDate<T> date,
  1179.                                                                               final FieldSlot<T> slot,
  1180.                                                                               final FieldAuxiliaryElements<T> auxiliaryElements,
  1181.                                                                               final Field<T> field) {
  1182.         final T zero = field.getZero();

  1183.         final FieldCjSjCoefficient<T> cjsjKH = new FieldCjSjCoefficient<>(auxiliaryElements.getK(), auxiliaryElements.getH(), field);
  1184.         final T b = auxiliaryElements.getB().add(1.).reciprocal();

  1185.         // (-b)<sup>j</sup>
  1186.         T mbtj = zero.add(1.);

  1187.         final T[][] rhoSigma = MathArrays.buildArray(field, slot.cij.length, 2);
  1188.         for (int j = 1; j < rhoSigma.length; j++) {

  1189.             //Compute current rho and sigma;
  1190.             mbtj = mbtj.multiply(b.negate());
  1191.             final T coef  = mbtj.multiply(auxiliaryElements.getB().multiply(j).add(1.));
  1192.             final T rho   = coef.multiply(cjsjKH.getCj(j));
  1193.             final T sigma = coef.multiply(cjsjKH.getSj(j));

  1194.             // Add the coefficients to the interpolation grid
  1195.             rhoSigma[j][0] = rho;
  1196.             rhoSigma[j][1] = sigma;
  1197.         }

  1198.         return rhoSigma;

  1199.     }

  1200.     /** The coefficients used to compute the short-periodic zonal contribution.
  1201.      *
  1202.      * <p>
  1203.      * Those coefficients are given in Danielson paper by expressions 4.1-(20) to 4.1.-(25)
  1204.      * </p>
  1205.      * <p>
  1206.      * The coefficients are: <br>
  1207.      * - C<sub>i</sub><sup>j</sup> and S<sub>i</sub><sup>j</sup> <br>
  1208.      * - ρ<sub>j</sub> and σ<sub>j</sub> <br>
  1209.      * - C<sub>i</sub>⁰
  1210.      * </p>
  1211.      *
  1212.      * @author Lucian Barbulescu
  1213.      */
  1214.     private static class ZonalShortPeriodicCoefficients implements ShortPeriodTerms {

  1215.         /** Maximum value for j index. */
  1216.         private final int maxFrequencyShortPeriodics;

  1217.         /** Number of points used in the interpolation process. */
  1218.         private final int interpolationPoints;

  1219.         /** All coefficients slots. */
  1220.         private final transient TimeSpanMap<Slot> slots;

  1221.         /** Constructor.
  1222.          * @param maxFrequencyShortPeriodics maximum value for j index
  1223.          * @param interpolationPoints number of points used in the interpolation process
  1224.          * @param slots all coefficients slots
  1225.          */
  1226.         ZonalShortPeriodicCoefficients(final int maxFrequencyShortPeriodics, final int interpolationPoints,
  1227.                                        final TimeSpanMap<Slot> slots) {

  1228.             // Save parameters
  1229.             this.maxFrequencyShortPeriodics = maxFrequencyShortPeriodics;
  1230.             this.interpolationPoints        = interpolationPoints;
  1231.             this.slots                      = slots;

  1232.         }

  1233.         /** Get the slot valid for some date.
  1234.          * @param meanStates mean states defining the slot
  1235.          * @return slot valid at the specified date
  1236.          */
  1237.         public Slot createSlot(final SpacecraftState... meanStates) {
  1238.             final Slot         slot  = new Slot(maxFrequencyShortPeriodics, interpolationPoints);
  1239.             final AbsoluteDate first = meanStates[0].getDate();
  1240.             final AbsoluteDate last  = meanStates[meanStates.length - 1].getDate();
  1241.             if (first.compareTo(last) <= 0) {
  1242.                 slots.addValidAfter(slot, first);
  1243.             } else {
  1244.                 slots.addValidBefore(slot, first);
  1245.             }
  1246.             return slot;
  1247.         }

  1248.         /** {@inheritDoc} */
  1249.         @Override
  1250.         public double[] value(final Orbit meanOrbit) {

  1251.             // select the coefficients slot
  1252.             final Slot slot = slots.get(meanOrbit.getDate());

  1253.             // Get the True longitude L
  1254.             final double L = meanOrbit.getLv();

  1255.             // Compute the center
  1256.             final double center = L - meanOrbit.getLM();

  1257.             // Initialize short periodic variations
  1258.             final double[] shortPeriodicVariation = slot.cij[0].value(meanOrbit.getDate());
  1259.             final double[] d = slot.di.value(meanOrbit.getDate());
  1260.             for (int i = 0; i < 6; i++) {
  1261.                 shortPeriodicVariation[i] +=  center * d[i];
  1262.             }

  1263.             for (int j = 1; j <= maxFrequencyShortPeriodics; j++) {
  1264.                 final double[] c = slot.cij[j].value(meanOrbit.getDate());
  1265.                 final double[] s = slot.sij[j].value(meanOrbit.getDate());
  1266.                 final SinCos sc  = FastMath.sinCos(j * L);
  1267.                 for (int i = 0; i < 6; i++) {
  1268.                     // add corresponding term to the short periodic variation
  1269.                     shortPeriodicVariation[i] += c[i] * sc.cos();
  1270.                     shortPeriodicVariation[i] += s[i] * sc.sin();
  1271.                 }
  1272.             }

  1273.             return shortPeriodicVariation;
  1274.         }

  1275.         /** {@inheritDoc} */
  1276.         @Override
  1277.         public String getCoefficientsKeyPrefix() {
  1278.             return DSSTZonal.SHORT_PERIOD_PREFIX;
  1279.         }

  1280.         /** {@inheritDoc}
  1281.          * <p>
  1282.          * For zonal terms contributions,there are maxJ cj coefficients,
  1283.          * maxJ sj coefficients and 2 dj coefficients, where maxJ depends
  1284.          * on the orbit. The j index is the integer multiplier for the true
  1285.          * longitude argument in the cj and sj coefficients and the degree
  1286.          * in the polynomial dj coefficients.
  1287.          * </p>
  1288.          */
  1289.         @Override
  1290.         public Map<String, double[]> getCoefficients(final AbsoluteDate date, final Set<String> selected) {

  1291.             // select the coefficients slot
  1292.             final Slot slot = slots.get(date);

  1293.             final Map<String, double[]> coefficients = new HashMap<String, double[]>(2 * maxFrequencyShortPeriodics + 2);
  1294.             storeIfSelected(coefficients, selected, slot.cij[0].value(date), "d", 0);
  1295.             storeIfSelected(coefficients, selected, slot.di.value(date), "d", 1);
  1296.             for (int j = 1; j <= maxFrequencyShortPeriodics; j++) {
  1297.                 storeIfSelected(coefficients, selected, slot.cij[j].value(date), "c", j);
  1298.                 storeIfSelected(coefficients, selected, slot.sij[j].value(date), "s", j);
  1299.             }
  1300.             return coefficients;

  1301.         }

  1302.         /** Put a coefficient in a map if selected.
  1303.          * @param map map to populate
  1304.          * @param selected set of coefficients that should be put in the map
  1305.          * (empty set means all coefficients are selected)
  1306.          * @param value coefficient value
  1307.          * @param id coefficient identifier
  1308.          * @param indices list of coefficient indices
  1309.          */
  1310.         private void storeIfSelected(final Map<String, double[]> map, final Set<String> selected,
  1311.                                      final double[] value, final String id, final int... indices) {
  1312.             final StringBuilder keyBuilder = new StringBuilder(getCoefficientsKeyPrefix());
  1313.             keyBuilder.append(id);
  1314.             for (int index : indices) {
  1315.                 keyBuilder.append('[').append(index).append(']');
  1316.             }
  1317.             final String key = keyBuilder.toString();
  1318.             if (selected.isEmpty() || selected.contains(key)) {
  1319.                 map.put(key, value);
  1320.             }
  1321.         }

  1322.     }

  1323.     /** The coefficients used to compute the short-periodic zonal contribution.
  1324.     *
  1325.     * <p>
  1326.     * Those coefficients are given in Danielson paper by expressions 4.1-(20) to 4.1.-(25)
  1327.     * </p>
  1328.     * <p>
  1329.     * The coefficients are: <br>
  1330.     * - C<sub>i</sub><sup>j</sup> and S<sub>i</sub><sup>j</sup> <br>
  1331.     * - ρ<sub>j</sub> and σ<sub>j</sub> <br>
  1332.     * - C<sub>i</sub>⁰
  1333.     * </p>
  1334.     *
  1335.     * @author Lucian Barbulescu
  1336.     */
  1337.     private static class FieldZonalShortPeriodicCoefficients <T extends RealFieldElement<T>> implements FieldShortPeriodTerms<T> {

  1338.         /** Maximum value for j index. */
  1339.         private final int maxFrequencyShortPeriodics;

  1340.         /** Number of points used in the interpolation process. */
  1341.         private final int interpolationPoints;

  1342.         /** All coefficients slots. */
  1343.         private final transient FieldTimeSpanMap<FieldSlot<T>, T> slots;

  1344.        /** Constructor.
  1345.         * @param maxFrequencyShortPeriodics maximum value for j index
  1346.         * @param interpolationPoints number of points used in the interpolation process
  1347.         * @param slots all coefficients slots
  1348.         */
  1349.         FieldZonalShortPeriodicCoefficients(final int maxFrequencyShortPeriodics, final int interpolationPoints,
  1350.                                             final FieldTimeSpanMap<FieldSlot<T>, T> slots) {

  1351.             // Save parameters
  1352.             this.maxFrequencyShortPeriodics = maxFrequencyShortPeriodics;
  1353.             this.interpolationPoints        = interpolationPoints;
  1354.             this.slots                      = slots;

  1355.         }

  1356.        /** Get the slot valid for some date.
  1357.         * @param meanStates mean states defining the slot
  1358.         * @return slot valid at the specified date
  1359.         */
  1360.         @SuppressWarnings("unchecked")
  1361.         public FieldSlot<T> createSlot(final FieldSpacecraftState<T>... meanStates) {
  1362.             final FieldSlot<T>         slot  = new FieldSlot<>(maxFrequencyShortPeriodics, interpolationPoints);
  1363.             final FieldAbsoluteDate<T> first = meanStates[0].getDate();
  1364.             final FieldAbsoluteDate<T> last  = meanStates[meanStates.length - 1].getDate();
  1365.             if (first.compareTo(last) <= 0) {
  1366.                 slots.addValidAfter(slot, first);
  1367.             } else {
  1368.                 slots.addValidBefore(slot, first);
  1369.             }
  1370.             return slot;
  1371.         }

  1372.         /** {@inheritDoc} */
  1373.         @Override
  1374.         public T[] value(final FieldOrbit<T> meanOrbit) {

  1375.             // select the coefficients slot
  1376.             final FieldSlot<T> slot = slots.get(meanOrbit.getDate());

  1377.             // Get the True longitude L
  1378.             final T L = meanOrbit.getLv();

  1379.             // Compute the center
  1380.             final T center = L.subtract(meanOrbit.getLM());

  1381.             // Initialize short periodic variations
  1382.             final T[] shortPeriodicVariation = slot.cij[0].value(meanOrbit.getDate());
  1383.             final T[] d = slot.di.value(meanOrbit.getDate());
  1384.             for (int i = 0; i < 6; i++) {
  1385.                 shortPeriodicVariation[i] = shortPeriodicVariation[i].add(center.multiply(d[i]));
  1386.             }

  1387.             for (int j = 1; j <= maxFrequencyShortPeriodics; j++) {
  1388.                 final T[]            c   = slot.cij[j].value(meanOrbit.getDate());
  1389.                 final T[]            s   = slot.sij[j].value(meanOrbit.getDate());
  1390.                 final FieldSinCos<T> sc  = FastMath.sinCos(L.multiply(j));
  1391.                 for (int i = 0; i < 6; i++) {
  1392.                     // add corresponding term to the short periodic variation
  1393.                     shortPeriodicVariation[i] = shortPeriodicVariation[i].add(c[i].multiply(sc.cos()));
  1394.                     shortPeriodicVariation[i] = shortPeriodicVariation[i].add(s[i].multiply(sc.sin()));
  1395.                 }
  1396.             }

  1397.             return shortPeriodicVariation;
  1398.         }

  1399.         /** {@inheritDoc} */
  1400.         @Override
  1401.         public String getCoefficientsKeyPrefix() {
  1402.             return DSSTZonal.SHORT_PERIOD_PREFIX;
  1403.         }

  1404.        /** {@inheritDoc}
  1405.         * <p>
  1406.         * For zonal terms contributions,there are maxJ cj coefficients,
  1407.         * maxJ sj coefficients and 2 dj coefficients, where maxJ depends
  1408.         * on the orbit. The j index is the integer multiplier for the true
  1409.         * longitude argument in the cj and sj coefficients and the degree
  1410.         * in the polynomial dj coefficients.
  1411.         * </p>
  1412.         */
  1413.         @Override
  1414.         public Map<String, T[]> getCoefficients(final FieldAbsoluteDate<T> date, final Set<String> selected) {

  1415.             // select the coefficients slot
  1416.             final FieldSlot<T> slot = slots.get(date);

  1417.             final Map<String, T[]> coefficients = new HashMap<String, T[]>(2 * maxFrequencyShortPeriodics + 2);
  1418.             storeIfSelected(coefficients, selected, slot.cij[0].value(date), "d", 0);
  1419.             storeIfSelected(coefficients, selected, slot.di.value(date), "d", 1);
  1420.             for (int j = 1; j <= maxFrequencyShortPeriodics; j++) {
  1421.                 storeIfSelected(coefficients, selected, slot.cij[j].value(date), "c", j);
  1422.                 storeIfSelected(coefficients, selected, slot.sij[j].value(date), "s", j);
  1423.             }
  1424.             return coefficients;

  1425.         }

  1426.        /** Put a coefficient in a map if selected.
  1427.         * @param map map to populate
  1428.         * @param selected set of coefficients that should be put in the map
  1429.         * (empty set means all coefficients are selected)
  1430.         * @param value coefficient value
  1431.         * @param id coefficient identifier
  1432.         * @param indices list of coefficient indices
  1433.         */
  1434.         private void storeIfSelected(final Map<String, T[]> map, final Set<String> selected,
  1435.                                      final T[] value, final String id, final int... indices) {
  1436.             final StringBuilder keyBuilder = new StringBuilder(getCoefficientsKeyPrefix());
  1437.             keyBuilder.append(id);
  1438.             for (int index : indices) {
  1439.                 keyBuilder.append('[').append(index).append(']');
  1440.             }
  1441.             final String key = keyBuilder.toString();
  1442.             if (selected.isEmpty() || selected.contains(key)) {
  1443.                 map.put(key, value);
  1444.             }
  1445.         }

  1446.     }

  1447.     /** Compute the C<sup>j</sup> and the S<sup>j</sup> coefficients.
  1448.      *  <p>
  1449.      *  Those coefficients are given in Danielson paper by expressions 4.1-(13) to 4.1.-(16b)
  1450.      *  </p>
  1451.      */
  1452.     private class FourierCjSjCoefficients {

  1453.         /** The G<sub>js</sub>, H<sub>js</sub>, I<sub>js</sub> and J<sub>js</sub> polynomials. */
  1454.         private final GHIJjsPolynomials ghijCoef;

  1455.         /** L<sub>n</sub><sup>s</sup>(γ). */
  1456.         private final LnsCoefficients lnsCoef;

  1457.         /** Maximum possible value for n. */
  1458.         private final int nMax;

  1459.         /** Maximum possible value for s. */
  1460.         private final int sMax;

  1461.         /** Maximum possible value for j. */
  1462.         private final int jMax;

  1463.         /** The C<sup>j</sup> coefficients and their derivatives.
  1464.          * <p>
  1465.          * Each column of the matrix contains the following values: <br/>
  1466.          * - C<sup>j</sup> <br/>
  1467.          * - dC<sup>j</sup> / da <br/>
  1468.          * - dC<sup>j</sup> / dh <br/>
  1469.          * - dC<sup>j</sup> / dk <br/>
  1470.          * - dC<sup>j</sup> / dα <br/>
  1471.          * - dC<sup>j</sup> / dβ <br/>
  1472.          * - dC<sup>j</sup> / dγ <br/>
  1473.          * </p>
  1474.          */
  1475.         private final double[][] cCoef;

  1476.         /** The S<sup>j</sup> coefficients and their derivatives.
  1477.          * <p>
  1478.          * Each column of the matrix contains the following values: <br/>
  1479.          * - S<sup>j</sup> <br/>
  1480.          * - dS<sup>j</sup> / da <br/>
  1481.          * - dS<sup>j</sup> / dh <br/>
  1482.          * - dS<sup>j</sup> / dk <br/>
  1483.          * - dS<sup>j</sup> / dα <br/>
  1484.          * - dS<sup>j</sup> / dβ <br/>
  1485.          * - dS<sup>j</sup> / dγ <br/>
  1486.          * </p>
  1487.          */
  1488.         private final double[][] sCoef;

  1489.         /** h * &Chi;³. */
  1490.         private final double hXXX;
  1491.         /** k * &Chi;³. */
  1492.         private final double kXXX;

  1493.         /** Create a set of C<sup>j</sup> and the S<sup>j</sup> coefficients.
  1494.          *  @param date the current date
  1495.          *  @param nMax maximum possible value for n
  1496.          *  @param sMax maximum possible value for s
  1497.          *  @param jMax maximum possible value for j
  1498.          *  @param context container for attributes
  1499.          *  @param hansenObjects initialization of hansen objects
  1500.          */
  1501.         FourierCjSjCoefficients(final AbsoluteDate date,
  1502.                                 final int nMax, final int sMax, final int jMax, final DSSTZonalContext context,
  1503.                                 final HansenObjects hansenObjects) {

  1504.             final AuxiliaryElements auxiliaryElements = context.getAuxiliaryElements();

  1505.             this.ghijCoef = new GHIJjsPolynomials(auxiliaryElements.getK(), auxiliaryElements.getH(), auxiliaryElements.getAlpha(), auxiliaryElements.getBeta());
  1506.             // Qns coefficients
  1507.             final double[][] Qns  = CoefficientsFactory.computeQns(auxiliaryElements.getGamma(), nMax, nMax);

  1508.             this.lnsCoef = new LnsCoefficients(nMax, nMax, Qns, Vns, context.getRoa());
  1509.             this.nMax = nMax;
  1510.             this.sMax = sMax;
  1511.             this.jMax = jMax;

  1512.             // compute the common factors that depends on the mean elements
  1513.             this.hXXX = auxiliaryElements.getH() * context.getXXX();
  1514.             this.kXXX = auxiliaryElements.getK() * context.getXXX();

  1515.             this.cCoef = new double[7][jMax + 1];
  1516.             this.sCoef = new double[7][jMax + 1];

  1517.             for (int s = 0; s <= sMax; s++) {
  1518.                 //Initialise the Hansen roots
  1519.                 hansenObjects.computeHansenObjectsInitValues(context, s);
  1520.             }
  1521.             generateCoefficients(date, context, auxiliaryElements, hansenObjects);
  1522.         }

  1523.         /** Generate all coefficients.
  1524.          * @param date the current date
  1525.          * @param context container for attributes
  1526.          * @param auxiliaryElements auxiliary elements related to the current orbit
  1527.          * @param hansenObjects initialization of hansen objects
  1528.          */
  1529.         private void generateCoefficients(final AbsoluteDate date,
  1530.                                           final DSSTZonalContext context,
  1531.                                           final AuxiliaryElements auxiliaryElements,
  1532.                                           final HansenObjects hansenObjects) {

  1533.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(date);
  1534.             for (int j = 1; j <= jMax; j++) {

  1535.                 //init arrays
  1536.                 for (int i = 0; i <= 6; i++) {
  1537.                     cCoef[i][j] = 0.;
  1538.                     sCoef[i][j] = 0.;
  1539.                 }

  1540.                 if (isBetween(j, 1, nMax - 1)) {

  1541.                     //compute first double sum where s: j -> N-1 and n: s+1 -> N
  1542.                     for (int s = j; s <= FastMath.min(nMax - 1, sMax); s++) {
  1543.                         // j - s
  1544.                         final int jms = j - s;
  1545.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1546.                         final int d0smj = (s == j) ? 1 : 2;

  1547.                         for (int n = s + 1; n <= nMax; n++) {
  1548.                             // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1549.                             if ((n + jms) % 2 == 0) {
  1550.                                 // (2 - delta(0,s-j)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1551.                                 final double lns = lnsCoef.getLns(n, -jms);
  1552.                                 final double dlns = lnsCoef.getdLnsdGamma(n, -jms);

  1553.                                 final double hjs = ghijCoef.getHjs(s, -jms);
  1554.                                 final double dHjsdh = ghijCoef.getdHjsdh(s, -jms);
  1555.                                 final double dHjsdk = ghijCoef.getdHjsdk(s, -jms);
  1556.                                 final double dHjsdAlpha = ghijCoef.getdHjsdAlpha(s, -jms);
  1557.                                 final double dHjsdBeta = ghijCoef.getdHjsdBeta(s, -jms);

  1558.                                 final double gjs = ghijCoef.getGjs(s, -jms);
  1559.                                 final double dGjsdh = ghijCoef.getdGjsdh(s, -jms);
  1560.                                 final double dGjsdk = ghijCoef.getdGjsdk(s, -jms);
  1561.                                 final double dGjsdAlpha = ghijCoef.getdGjsdAlpha(s, -jms);
  1562.                                 final double dGjsdBeta = ghijCoef.getdGjsdBeta(s, -jms);

  1563.                                 // J<sub>n</sub>
  1564.                                 final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1565.                                 // K₀<sup>-n-1,s</sup>
  1566.                                 final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1567.                                 final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1568.                                 final double coef0 = d0smj * jn;
  1569.                                 final double coef1 = coef0 * lns;
  1570.                                 final double coef2 = coef1 * kns;
  1571.                                 final double coef3 = coef2 * hjs;
  1572.                                 final double coef4 = coef2 * gjs;

  1573.                                 // Add the term to the coefficients
  1574.                                 cCoef[0][j] += coef3;
  1575.                                 cCoef[1][j] += coef3 * (n + 1);
  1576.                                 cCoef[2][j] += coef1 * (kns * dHjsdh + hjs * hXXX * dkns);
  1577.                                 cCoef[3][j] += coef1 * (kns * dHjsdk + hjs * kXXX * dkns);
  1578.                                 cCoef[4][j] += coef2 * dHjsdAlpha;
  1579.                                 cCoef[5][j] += coef2 * dHjsdBeta;
  1580.                                 cCoef[6][j] += coef0 * dlns * kns * hjs;

  1581.                                 sCoef[0][j] += coef4;
  1582.                                 sCoef[1][j] += coef4 * (n + 1);
  1583.                                 sCoef[2][j] += coef1 * (kns * dGjsdh + gjs * hXXX * dkns);
  1584.                                 sCoef[3][j] += coef1 * (kns * dGjsdk + gjs * kXXX * dkns);
  1585.                                 sCoef[4][j] += coef2 * dGjsdAlpha;
  1586.                                 sCoef[5][j] += coef2 * dGjsdBeta;
  1587.                                 sCoef[6][j] += coef0 * dlns * kns * gjs;
  1588.                             }
  1589.                         }
  1590.                     }

  1591.                     //compute second double sum where s: 0 -> N-j and n: max(j+s, j+1) -> N
  1592.                     for (int s = 0; s <= FastMath.min(nMax - j, sMax); s++) {
  1593.                         // j + s
  1594.                         final int jps = j + s;
  1595.                         // Kronecker symbols (2 - delta(0,j+s))
  1596.                         final double d0spj = (s == -j) ? 1 : 2;

  1597.                         for (int n = FastMath.max(j + s, j + 1); n <= nMax; n++) {
  1598.                             // if n + (j+s) is odd, then the term is equal to zero due to the factor Vn,s+j
  1599.                             if ((n + jps) % 2 == 0) {
  1600.                                 // (2 - delta(0,s+j)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j+s</sup>
  1601.                                 final double lns = lnsCoef.getLns(n, jps);
  1602.                                 final double dlns = lnsCoef.getdLnsdGamma(n, jps);

  1603.                                 final double hjs = ghijCoef.getHjs(s, jps);
  1604.                                 final double dHjsdh = ghijCoef.getdHjsdh(s, jps);
  1605.                                 final double dHjsdk = ghijCoef.getdHjsdk(s, jps);
  1606.                                 final double dHjsdAlpha = ghijCoef.getdHjsdAlpha(s, jps);
  1607.                                 final double dHjsdBeta = ghijCoef.getdHjsdBeta(s, jps);

  1608.                                 final double gjs = ghijCoef.getGjs(s, jps);
  1609.                                 final double dGjsdh = ghijCoef.getdGjsdh(s, jps);
  1610.                                 final double dGjsdk = ghijCoef.getdGjsdk(s, jps);
  1611.                                 final double dGjsdAlpha = ghijCoef.getdGjsdAlpha(s, jps);
  1612.                                 final double dGjsdBeta = ghijCoef.getdGjsdBeta(s, jps);

  1613.                                 // J<sub>n</sub>
  1614.                                 final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1615.                                 // K₀<sup>-n-1,s</sup>
  1616.                                 final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1617.                                 final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1618.                                 final double coef0 = d0spj * jn;
  1619.                                 final double coef1 = coef0 * lns;
  1620.                                 final double coef2 = coef1 * kns;

  1621.                                 final double coef3 = coef2 * hjs;
  1622.                                 final double coef4 = coef2 * gjs;

  1623.                                 // Add the term to the coefficients
  1624.                                 cCoef[0][j] -= coef3;
  1625.                                 cCoef[1][j] -= coef3 * (n + 1);
  1626.                                 cCoef[2][j] -= coef1 * (kns * dHjsdh + hjs * hXXX * dkns);
  1627.                                 cCoef[3][j] -= coef1 * (kns * dHjsdk + hjs * kXXX * dkns);
  1628.                                 cCoef[4][j] -= coef2 * dHjsdAlpha;
  1629.                                 cCoef[5][j] -= coef2 * dHjsdBeta;
  1630.                                 cCoef[6][j] -= coef0 * dlns * kns * hjs;

  1631.                                 sCoef[0][j] += coef4;
  1632.                                 sCoef[1][j] += coef4 * (n + 1);
  1633.                                 sCoef[2][j] += coef1 * (kns * dGjsdh + gjs * hXXX * dkns);
  1634.                                 sCoef[3][j] += coef1 * (kns * dGjsdk + gjs * kXXX * dkns);
  1635.                                 sCoef[4][j] += coef2 * dGjsdAlpha;
  1636.                                 sCoef[5][j] += coef2 * dGjsdBeta;
  1637.                                 sCoef[6][j] += coef0 * dlns * kns * gjs;
  1638.                             }
  1639.                         }
  1640.                     }

  1641.                     //compute third double sum where s: 1 -> j and  n: j+1 -> N
  1642.                     for (int s = 1; s <= FastMath.min(j, sMax); s++) {
  1643.                         // j - s
  1644.                         final int jms = j - s;
  1645.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1646.                         final int d0smj = (s == j) ? 1 : 2;

  1647.                         for (int n = j + 1; n <= nMax; n++) {
  1648.                             // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1649.                             if ((n + jms) % 2 == 0) {
  1650.                                 // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1651.                                 final double lns = lnsCoef.getLns(n, jms);
  1652.                                 final double dlns = lnsCoef.getdLnsdGamma(n, jms);

  1653.                                 final double ijs = ghijCoef.getIjs(s, jms);
  1654.                                 final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1655.                                 final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1656.                                 final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1657.                                 final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1658.                                 final double jjs = ghijCoef.getJjs(s, jms);
  1659.                                 final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1660.                                 final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1661.                                 final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1662.                                 final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1663.                                 // J<sub>n</sub>
  1664.                                 final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1665.                                 // K₀<sup>-n-1,s</sup>
  1666.                                 final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1667.                                 final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1668.                                 final double coef0 = d0smj * jn;
  1669.                                 final double coef1 = coef0 * lns;
  1670.                                 final double coef2 = coef1 * kns;

  1671.                                 final double coef3 = coef2 * ijs;
  1672.                                 final double coef4 = coef2 * jjs;

  1673.                                 // Add the term to the coefficients
  1674.                                 cCoef[0][j] -= coef3;
  1675.                                 cCoef[1][j] -= coef3 * (n + 1);
  1676.                                 cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1677.                                 cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1678.                                 cCoef[4][j] -= coef2 * dIjsdAlpha;
  1679.                                 cCoef[5][j] -= coef2 * dIjsdBeta;
  1680.                                 cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1681.                                 sCoef[0][j] += coef4;
  1682.                                 sCoef[1][j] += coef4 * (n + 1);
  1683.                                 sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1684.                                 sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1685.                                 sCoef[4][j] += coef2 * dJjsdAlpha;
  1686.                                 sCoef[5][j] += coef2 * dJjsdBeta;
  1687.                                 sCoef[6][j] += coef0 * dlns * kns * jjs;
  1688.                             }
  1689.                         }
  1690.                     }
  1691.                 }

  1692.                 if (isBetween(j, 2, nMax)) {
  1693.                     //add first term
  1694.                     // J<sub>j</sub>
  1695.                     final double jj = -harmonics.getUnnormalizedCnm(j, 0);
  1696.                     double kns = hansenObjects.getHansenObjects()[0].getValue(-j - 1, context.getX());
  1697.                     double dkns = hansenObjects.getHansenObjects()[0].getDerivative(-j - 1, context.getX());

  1698.                     double lns = lnsCoef.getLns(j, j);
  1699.                     //dlns is 0 because n == s == j

  1700.                     final double hjs = ghijCoef.getHjs(0, j);
  1701.                     final double dHjsdh = ghijCoef.getdHjsdh(0, j);
  1702.                     final double dHjsdk = ghijCoef.getdHjsdk(0, j);
  1703.                     final double dHjsdAlpha = ghijCoef.getdHjsdAlpha(0, j);
  1704.                     final double dHjsdBeta = ghijCoef.getdHjsdBeta(0, j);

  1705.                     final double gjs = ghijCoef.getGjs(0, j);
  1706.                     final double dGjsdh = ghijCoef.getdGjsdh(0, j);
  1707.                     final double dGjsdk = ghijCoef.getdGjsdk(0, j);
  1708.                     final double dGjsdAlpha = ghijCoef.getdGjsdAlpha(0, j);
  1709.                     final double dGjsdBeta = ghijCoef.getdGjsdBeta(0, j);

  1710.                     // 2 * J<sub>j</sub> * K₀<sup>-j-1,0</sup> * L<sub>j</sub><sup>j</sup>
  1711.                     double coef0 = 2 * jj;
  1712.                     double coef1 = coef0 * lns;
  1713.                     double coef2 = coef1 * kns;

  1714.                     double coef3 = coef2 * hjs;
  1715.                     double coef4 = coef2 * gjs;

  1716.                     // Add the term to the coefficients
  1717.                     cCoef[0][j] -= coef3;
  1718.                     cCoef[1][j] -= coef3 * (j + 1);
  1719.                     cCoef[2][j] -= coef1 * (kns * dHjsdh + hjs * hXXX * dkns);
  1720.                     cCoef[3][j] -= coef1 * (kns * dHjsdk + hjs * kXXX * dkns);
  1721.                     cCoef[4][j] -= coef2 * dHjsdAlpha;
  1722.                     cCoef[5][j] -= coef2 * dHjsdBeta;
  1723.                     //no contribution to cCoef[6][j] because dlns is 0

  1724.                     sCoef[0][j] += coef4;
  1725.                     sCoef[1][j] += coef4 * (j + 1);
  1726.                     sCoef[2][j] += coef1 * (kns * dGjsdh + gjs * hXXX * dkns);
  1727.                     sCoef[3][j] += coef1 * (kns * dGjsdk + gjs * kXXX * dkns);
  1728.                     sCoef[4][j] += coef2 * dGjsdAlpha;
  1729.                     sCoef[5][j] += coef2 * dGjsdBeta;
  1730.                     //no contribution to sCoef[6][j] because dlns is 0

  1731.                     //compute simple sum where s: 1 -> j-1
  1732.                     for (int s = 1; s <= FastMath.min(j - 1, sMax); s++) {
  1733.                         // j - s
  1734.                         final int jms = j - s;
  1735.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1736.                         final int d0smj = (s == j) ? 1 : 2;

  1737.                         // if s is odd, then the term is equal to zero due to the factor Vj,s-j
  1738.                         if (s % 2 == 0) {
  1739.                             // (2 - delta(0,j-s)) * J<sub>j</sub> * K₀<sup>-j-1,s</sup> * L<sub>j</sub><sup>j-s</sup>
  1740.                             kns   = hansenObjects.getHansenObjects()[s].getValue(-j - 1, context.getX());
  1741.                             dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-j - 1, context.getX());

  1742.                             lns = lnsCoef.getLns(j, jms);
  1743.                             final double dlns = lnsCoef.getdLnsdGamma(j, jms);

  1744.                             final double ijs = ghijCoef.getIjs(s, jms);
  1745.                             final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1746.                             final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1747.                             final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1748.                             final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1749.                             final double jjs = ghijCoef.getJjs(s, jms);
  1750.                             final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1751.                             final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1752.                             final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1753.                             final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1754.                             coef0 = d0smj * jj;
  1755.                             coef1 = coef0 * lns;
  1756.                             coef2 = coef1 * kns;

  1757.                             coef3 = coef2 * ijs;
  1758.                             coef4 = coef2 * jjs;

  1759.                             // Add the term to the coefficients
  1760.                             cCoef[0][j] -= coef3;
  1761.                             cCoef[1][j] -= coef3 * (j + 1);
  1762.                             cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1763.                             cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1764.                             cCoef[4][j] -= coef2 * dIjsdAlpha;
  1765.                             cCoef[5][j] -= coef2 * dIjsdBeta;
  1766.                             cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1767.                             sCoef[0][j] += coef4;
  1768.                             sCoef[1][j] += coef4 * (j + 1);
  1769.                             sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1770.                             sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1771.                             sCoef[4][j] += coef2 * dJjsdAlpha;
  1772.                             sCoef[5][j] += coef2 * dJjsdBeta;
  1773.                             sCoef[6][j] += coef0 * dlns * kns * jjs;
  1774.                         }
  1775.                     }
  1776.                 }

  1777.                 if (isBetween(j, 3, 2 * nMax - 1)) {
  1778.                     //compute uppercase sigma expressions

  1779.                     //min(j-1,N)
  1780.                     final int minjm1on = FastMath.min(j - 1, nMax);

  1781.                     //if j is even
  1782.                     if (j % 2 == 0) {
  1783.                         //compute first double sum where s: j-min(j-1,N) -> j/2-1 and n: j-s -> min(j-1,N)
  1784.                         for (int s = j - minjm1on; s <= FastMath.min(j / 2 - 1, sMax); s++) {
  1785.                             // j - s
  1786.                             final int jms = j - s;
  1787.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1788.                             final int d0smj = (s == j) ? 1 : 2;

  1789.                             for (int n = j - s; n <= minjm1on; n++) {
  1790.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1791.                                 if ((n + jms) % 2 == 0) {
  1792.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1793.                                     final double lns = lnsCoef.getLns(n, jms);
  1794.                                     final double dlns = lnsCoef.getdLnsdGamma(n, jms);

  1795.                                     final double ijs = ghijCoef.getIjs(s, jms);
  1796.                                     final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1797.                                     final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1798.                                     final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1799.                                     final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1800.                                     final double jjs = ghijCoef.getJjs(s, jms);
  1801.                                     final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1802.                                     final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1803.                                     final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1804.                                     final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1805.                                     // J<sub>n</sub>
  1806.                                     final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1807.                                     // K₀<sup>-n-1,s</sup>
  1808.                                     final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1809.                                     final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1810.                                     final double coef0 = d0smj * jn;
  1811.                                     final double coef1 = coef0 * lns;
  1812.                                     final double coef2 = coef1 * kns;

  1813.                                     final double coef3 = coef2 * ijs;
  1814.                                     final double coef4 = coef2 * jjs;

  1815.                                     // Add the term to the coefficients
  1816.                                     cCoef[0][j] -= coef3;
  1817.                                     cCoef[1][j] -= coef3 * (n + 1);
  1818.                                     cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1819.                                     cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1820.                                     cCoef[4][j] -= coef2 * dIjsdAlpha;
  1821.                                     cCoef[5][j] -= coef2 * dIjsdBeta;
  1822.                                     cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1823.                                     sCoef[0][j] += coef4;
  1824.                                     sCoef[1][j] += coef4 * (n + 1);
  1825.                                     sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1826.                                     sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1827.                                     sCoef[4][j] += coef2 * dJjsdAlpha;
  1828.                                     sCoef[5][j] += coef2 * dJjsdBeta;
  1829.                                     sCoef[6][j] += coef0 * dlns * kns * jjs;
  1830.                                 }
  1831.                             }
  1832.                         }

  1833.                         //compute second double sum where s: j/2 -> min(j-1,N)-1 and n: s+1 -> min(j-1,N)
  1834.                         for (int s = j / 2; s <=  FastMath.min(minjm1on - 1, sMax); s++) {
  1835.                             // j - s
  1836.                             final int jms = j - s;
  1837.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1838.                             final int d0smj = (s == j) ? 1 : 2;

  1839.                             for (int n = s + 1; n <= minjm1on; n++) {
  1840.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1841.                                 if ((n + jms) % 2 == 0) {
  1842.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1843.                                     final double lns = lnsCoef.getLns(n, jms);
  1844.                                     final double dlns = lnsCoef.getdLnsdGamma(n, jms);

  1845.                                     final double ijs = ghijCoef.getIjs(s, jms);
  1846.                                     final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1847.                                     final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1848.                                     final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1849.                                     final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1850.                                     final double jjs = ghijCoef.getJjs(s, jms);
  1851.                                     final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1852.                                     final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1853.                                     final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1854.                                     final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1855.                                     // J<sub>n</sub>
  1856.                                     final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1857.                                     // K₀<sup>-n-1,s</sup>
  1858.                                     final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1859.                                     final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1860.                                     final double coef0 = d0smj * jn;
  1861.                                     final double coef1 = coef0 * lns;
  1862.                                     final double coef2 = coef1 * kns;

  1863.                                     final double coef3 = coef2 * ijs;
  1864.                                     final double coef4 = coef2 * jjs;

  1865.                                     // Add the term to the coefficients
  1866.                                     cCoef[0][j] -= coef3;
  1867.                                     cCoef[1][j] -= coef3 * (n + 1);
  1868.                                     cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1869.                                     cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1870.                                     cCoef[4][j] -= coef2 * dIjsdAlpha;
  1871.                                     cCoef[5][j] -= coef2 * dIjsdBeta;
  1872.                                     cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1873.                                     sCoef[0][j] += coef4;
  1874.                                     sCoef[1][j] += coef4 * (n + 1);
  1875.                                     sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1876.                                     sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1877.                                     sCoef[4][j] += coef2 * dJjsdAlpha;
  1878.                                     sCoef[5][j] += coef2 * dJjsdBeta;
  1879.                                     sCoef[6][j] += coef0 * dlns * kns * jjs;
  1880.                                 }
  1881.                             }
  1882.                         }
  1883.                     }

  1884.                     //if j is odd
  1885.                     else {
  1886.                         //compute first double sum where s: (j-1)/2 -> min(j-1,N)-1 and n: s+1 -> min(j-1,N)
  1887.                         for (int s = (j - 1) / 2; s <= FastMath.min(minjm1on - 1, sMax); s++) {
  1888.                             // j - s
  1889.                             final int jms = j - s;
  1890.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1891.                             final int d0smj = (s == j) ? 1 : 2;

  1892.                             for (int n = s + 1; n <= minjm1on; n++) {
  1893.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1894.                                 if ((n + jms) % 2 == 0) {
  1895.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1896.                                     final double lns = lnsCoef.getLns(n, jms);
  1897.                                     final double dlns = lnsCoef.getdLnsdGamma(n, jms);

  1898.                                     final double ijs = ghijCoef.getIjs(s, jms);
  1899.                                     final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1900.                                     final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1901.                                     final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1902.                                     final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1903.                                     final double jjs = ghijCoef.getJjs(s, jms);
  1904.                                     final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1905.                                     final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1906.                                     final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1907.                                     final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1908.                                     // J<sub>n</sub>
  1909.                                     final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1910.                                     // K₀<sup>-n-1,s</sup>

  1911.                                     final double kns = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1912.                                     final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1913.                                     final double coef0 = d0smj * jn;
  1914.                                     final double coef1 = coef0 * lns;
  1915.                                     final double coef2 = coef1 * kns;

  1916.                                     final double coef3 = coef2 * ijs;
  1917.                                     final double coef4 = coef2 * jjs;

  1918.                                     // Add the term to the coefficients
  1919.                                     cCoef[0][j] -= coef3;
  1920.                                     cCoef[1][j] -= coef3 * (n + 1);
  1921.                                     cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1922.                                     cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1923.                                     cCoef[4][j] -= coef2 * dIjsdAlpha;
  1924.                                     cCoef[5][j] -= coef2 * dIjsdBeta;
  1925.                                     cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1926.                                     sCoef[0][j] += coef4;
  1927.                                     sCoef[1][j] += coef4 * (n + 1);
  1928.                                     sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1929.                                     sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1930.                                     sCoef[4][j] += coef2 * dJjsdAlpha;
  1931.                                     sCoef[5][j] += coef2 * dJjsdBeta;
  1932.                                     sCoef[6][j] += coef0 * dlns * kns * jjs;
  1933.                                 }
  1934.                             }
  1935.                         }

  1936.                         //the second double sum is added only if N >= 4 and j between 5 and 2*N-3
  1937.                         if (nMax >= 4 && isBetween(j, 5, 2 * nMax - 3)) {
  1938.                             //compute second double sum where s: j-min(j-1,N) -> (j-3)/2 and n: j-s -> min(j-1,N)
  1939.                             for (int s = j - minjm1on; s <= FastMath.min((j - 3) / 2, sMax); s++) {
  1940.                                 // j - s
  1941.                                 final int jms = j - s;
  1942.                                 // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  1943.                                 final int d0smj = (s == j) ? 1 : 2;

  1944.                                 for (int n = j - s; n <= minjm1on; n++) {
  1945.                                     // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  1946.                                     if ((n + jms) % 2 == 0) {
  1947.                                         // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  1948.                                         final double lns = lnsCoef.getLns(n, jms);
  1949.                                         final double dlns = lnsCoef.getdLnsdGamma(n, jms);

  1950.                                         final double ijs = ghijCoef.getIjs(s, jms);
  1951.                                         final double dIjsdh = ghijCoef.getdIjsdh(s, jms);
  1952.                                         final double dIjsdk = ghijCoef.getdIjsdk(s, jms);
  1953.                                         final double dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  1954.                                         final double dIjsdBeta = ghijCoef.getdIjsdBeta(s, jms);

  1955.                                         final double jjs = ghijCoef.getJjs(s, jms);
  1956.                                         final double dJjsdh = ghijCoef.getdJjsdh(s, jms);
  1957.                                         final double dJjsdk = ghijCoef.getdJjsdk(s, jms);
  1958.                                         final double dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  1959.                                         final double dJjsdBeta = ghijCoef.getdJjsdBeta(s, jms);

  1960.                                         // J<sub>n</sub>
  1961.                                         final double jn = -harmonics.getUnnormalizedCnm(n, 0);

  1962.                                         // K₀<sup>-n-1,s</sup>
  1963.                                         final double kns   = hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  1964.                                         final double dkns  = hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  1965.                                         final double coef0 = d0smj * jn;
  1966.                                         final double coef1 = coef0 * lns;
  1967.                                         final double coef2 = coef1 * kns;

  1968.                                         final double coef3 = coef2 * ijs;
  1969.                                         final double coef4 = coef2 * jjs;

  1970.                                         // Add the term to the coefficients
  1971.                                         cCoef[0][j] -= coef3;
  1972.                                         cCoef[1][j] -= coef3 * (n + 1);
  1973.                                         cCoef[2][j] -= coef1 * (kns * dIjsdh + ijs * hXXX * dkns);
  1974.                                         cCoef[3][j] -= coef1 * (kns * dIjsdk + ijs * kXXX * dkns);
  1975.                                         cCoef[4][j] -= coef2 * dIjsdAlpha;
  1976.                                         cCoef[5][j] -= coef2 * dIjsdBeta;
  1977.                                         cCoef[6][j] -= coef0 * dlns * kns * ijs;

  1978.                                         sCoef[0][j] += coef4;
  1979.                                         sCoef[1][j] += coef4 * (n + 1);
  1980.                                         sCoef[2][j] += coef1 * (kns * dJjsdh + jjs * hXXX * dkns);
  1981.                                         sCoef[3][j] += coef1 * (kns * dJjsdk + jjs * kXXX * dkns);
  1982.                                         sCoef[4][j] += coef2 * dJjsdAlpha;
  1983.                                         sCoef[5][j] += coef2 * dJjsdBeta;
  1984.                                         sCoef[6][j] += coef0 * dlns * kns * jjs;
  1985.                                     }
  1986.                                 }
  1987.                             }
  1988.                         }
  1989.                     }
  1990.                 }

  1991.                 cCoef[0][j] *= -context.getMuoa() / j;
  1992.                 cCoef[1][j] *=  context.getMuoa() / ( j * auxiliaryElements.getSma() );
  1993.                 cCoef[2][j] *= -context.getMuoa() / j;
  1994.                 cCoef[3][j] *= -context.getMuoa() / j;
  1995.                 cCoef[4][j] *= -context.getMuoa() / j;
  1996.                 cCoef[5][j] *= -context.getMuoa() / j;
  1997.                 cCoef[6][j] *= -context.getMuoa() / j;

  1998.                 sCoef[0][j] *= -context.getMuoa() / j;
  1999.                 sCoef[1][j] *=  context.getMuoa() / ( j * auxiliaryElements.getSma() );
  2000.                 sCoef[2][j] *= -context.getMuoa() / j;
  2001.                 sCoef[3][j] *= -context.getMuoa() / j;
  2002.                 sCoef[4][j] *= -context.getMuoa() / j;
  2003.                 sCoef[5][j] *= -context.getMuoa() / j;
  2004.                 sCoef[6][j] *= -context.getMuoa() / j;

  2005.             }
  2006.         }

  2007.         /** Check if an index is within the accepted interval.
  2008.          *
  2009.          * @param index the index to check
  2010.          * @param lowerBound the lower bound of the interval
  2011.          * @param upperBound the upper bound of the interval
  2012.          * @return true if the index is between the lower and upper bounds, false otherwise
  2013.          */
  2014.         private boolean isBetween(final int index, final int lowerBound, final int upperBound) {
  2015.             return index >= lowerBound && index <= upperBound;
  2016.         }

  2017.         /**Get the value of C<sup>j</sup>.
  2018.          *
  2019.          * @param j j index
  2020.          * @return C<sup>j</sup>
  2021.          */
  2022.         public double getCj(final int j) {
  2023.             return cCoef[0][j];
  2024.         }

  2025.         /**Get the value of dC<sup>j</sup> / da.
  2026.          *
  2027.          * @param j j index
  2028.          * @return dC<sup>j</sup> / da
  2029.          */
  2030.         public double getdCjdA(final int j) {
  2031.             return cCoef[1][j];
  2032.         }

  2033.         /**Get the value of dC<sup>j</sup> / dh.
  2034.          *
  2035.          * @param j j index
  2036.          * @return dC<sup>j</sup> / dh
  2037.          */
  2038.         public double getdCjdH(final int j) {
  2039.             return cCoef[2][j];
  2040.         }

  2041.         /**Get the value of dC<sup>j</sup> / dk.
  2042.          *
  2043.          * @param j j index
  2044.          * @return dC<sup>j</sup> / dk
  2045.          */
  2046.         public double getdCjdK(final int j) {
  2047.             return cCoef[3][j];
  2048.         }

  2049.         /**Get the value of dC<sup>j</sup> / dα.
  2050.          *
  2051.          * @param j j index
  2052.          * @return dC<sup>j</sup> / dα
  2053.          */
  2054.         public double getdCjdAlpha(final int j) {
  2055.             return cCoef[4][j];
  2056.         }

  2057.         /**Get the value of dC<sup>j</sup> / dβ.
  2058.          *
  2059.          * @param j j index
  2060.          * @return dC<sup>j</sup> / dβ
  2061.          */
  2062.         public double getdCjdBeta(final int j) {
  2063.             return cCoef[5][j];
  2064.         }

  2065.         /**Get the value of dC<sup>j</sup> / dγ.
  2066.          *
  2067.          * @param j j index
  2068.          * @return dC<sup>j</sup> / dγ
  2069.          */
  2070.         public double getdCjdGamma(final int j) {
  2071.             return cCoef[6][j];
  2072.         }

  2073.         /**Get the value of S<sup>j</sup>.
  2074.          *
  2075.          * @param j j index
  2076.          * @return S<sup>j</sup>
  2077.          */
  2078.         public double getSj(final int j) {
  2079.             return sCoef[0][j];
  2080.         }

  2081.         /**Get the value of dS<sup>j</sup> / da.
  2082.          *
  2083.          * @param j j index
  2084.          * @return dS<sup>j</sup> / da
  2085.          */
  2086.         public double getdSjdA(final int j) {
  2087.             return sCoef[1][j];
  2088.         }

  2089.         /**Get the value of dS<sup>j</sup> / dh.
  2090.          *
  2091.          * @param j j index
  2092.          * @return dS<sup>j</sup> / dh
  2093.          */
  2094.         public double getdSjdH(final int j) {
  2095.             return sCoef[2][j];
  2096.         }

  2097.         /**Get the value of dS<sup>j</sup> / dk.
  2098.          *
  2099.          * @param j j index
  2100.          * @return dS<sup>j</sup> / dk
  2101.          */
  2102.         public double getdSjdK(final int j) {
  2103.             return sCoef[3][j];
  2104.         }

  2105.         /**Get the value of dS<sup>j</sup> / dα.
  2106.          *
  2107.          * @param j j index
  2108.          * @return dS<sup>j</sup> / dα
  2109.          */
  2110.         public double getdSjdAlpha(final int j) {
  2111.             return sCoef[4][j];
  2112.         }

  2113.         /**Get the value of dS<sup>j</sup> / dβ.
  2114.          *
  2115.          * @param j j index
  2116.          * @return dS<sup>j</sup> / dβ
  2117.          */
  2118.         public double getdSjdBeta(final int j) {
  2119.             return sCoef[5][j];
  2120.         }

  2121.         /**Get the value of dS<sup>j</sup> /  dγ.
  2122.          *
  2123.          * @param j j index
  2124.          * @return dS<sup>j</sup> /  dγ
  2125.          */
  2126.         public double getdSjdGamma(final int j) {
  2127.             return sCoef[6][j];
  2128.         }
  2129.     }

  2130.     /** Compute the C<sup>j</sup> and the S<sup>j</sup> coefficients.
  2131.      *  <p>
  2132.      *  Those coefficients are given in Danielson paper by expressions 4.1-(13) to 4.1.-(16b)
  2133.      *  </p>
  2134.      */
  2135.     private class FieldFourierCjSjCoefficients <T extends RealFieldElement<T>> {

  2136.         /** The G<sub>js</sub>, H<sub>js</sub>, I<sub>js</sub> and J<sub>js</sub> polynomials. */
  2137.         private final FieldGHIJjsPolynomials<T> ghijCoef;

  2138.         /** L<sub>n</sub><sup>s</sup>(γ). */
  2139.         private final FieldLnsCoefficients<T> lnsCoef;

  2140.         /** Maximum possible value for n. */
  2141.         private final int nMax;

  2142.         /** Maximum possible value for s. */
  2143.         private final int sMax;

  2144.         /** Maximum possible value for j. */
  2145.         private final int jMax;

  2146.         /** The C<sup>j</sup> coefficients and their derivatives.
  2147.          * <p>
  2148.          * Each column of the matrix contains the following values: <br/>
  2149.          * - C<sup>j</sup> <br/>
  2150.          * - dC<sup>j</sup> / da <br/>
  2151.          * - dC<sup>j</sup> / dh <br/>
  2152.          * - dC<sup>j</sup> / dk <br/>
  2153.          * - dC<sup>j</sup> / dα <br/>
  2154.          * - dC<sup>j</sup> / dβ <br/>
  2155.          * - dC<sup>j</sup> / dγ <br/>
  2156.          * </p>
  2157.          */
  2158.         private final T[][] cCoef;

  2159.         /** The S<sup>j</sup> coefficients and their derivatives.
  2160.          * <p>
  2161.          * Each column of the matrix contains the following values: <br/>
  2162.          * - S<sup>j</sup> <br/>
  2163.          * - dS<sup>j</sup> / da <br/>
  2164.          * - dS<sup>j</sup> / dh <br/>
  2165.          * - dS<sup>j</sup> / dk <br/>
  2166.          * - dS<sup>j</sup> / dα <br/>
  2167.          * - dS<sup>j</sup> / dβ <br/>
  2168.          * - dS<sup>j</sup> / dγ <br/>
  2169.          * </p>
  2170.          */
  2171.         private final T[][] sCoef;

  2172.         /** h * &Chi;³. */
  2173.         private final T hXXX;
  2174.         /** k * &Chi;³. */
  2175.         private final T kXXX;

  2176.         /** Create a set of C<sup>j</sup> and the S<sup>j</sup> coefficients.
  2177.          *  @param date the current date
  2178.          *  @param nMax maximum possible value for n
  2179.          *  @param sMax maximum possible value for s
  2180.          *  @param jMax maximum possible value for j
  2181.          *  @param context container for attributes
  2182.          *  @param hansenObjects initialization of hansen objects
  2183.          */
  2184.         FieldFourierCjSjCoefficients(final FieldAbsoluteDate<T> date,
  2185.                                      final int nMax, final int sMax, final int jMax,
  2186.                                      final FieldDSSTZonalContext<T> context,
  2187.                                      final FieldHansenObjects<T> hansenObjects) {

  2188.             //Field used by default
  2189.             final Field<T> field = date.getField();

  2190.             final FieldAuxiliaryElements<T> auxiliaryElements = context.getFieldAuxiliaryElements();

  2191.             this.ghijCoef = new FieldGHIJjsPolynomials<>(auxiliaryElements.getK(), auxiliaryElements.getH(), auxiliaryElements.getAlpha(), auxiliaryElements.getBeta());
  2192.             // Qns coefficients
  2193.             final T[][] Qns = CoefficientsFactory.computeQns(auxiliaryElements.getGamma(), nMax, nMax);

  2194.             this.lnsCoef = new FieldLnsCoefficients<>(nMax, nMax, Qns, Vns, context.getRoa(), field);
  2195.             this.nMax = nMax;
  2196.             this.sMax = sMax;
  2197.             this.jMax = jMax;

  2198.             // compute the common factors that depends on the mean elements
  2199.             this.hXXX = auxiliaryElements.getH().multiply(context.getXXX());
  2200.             this.kXXX = auxiliaryElements.getK().multiply(context.getXXX());

  2201.             this.cCoef = MathArrays.buildArray(field, 7, jMax + 1);
  2202.             this.sCoef = MathArrays.buildArray(field, 7, jMax + 1);

  2203.             for (int s = 0; s <= sMax; s++) {
  2204.                 //Initialise the Hansen roots
  2205.                 hansenObjects.computeHansenObjectsInitValues(context, s);
  2206.             }
  2207.             generateCoefficients(date, context, auxiliaryElements, hansenObjects, field);
  2208.         }

  2209.         /** Generate all coefficients.
  2210.          * @param date the current date
  2211.          * @param context container for attributes
  2212.          * @param hansenObjects initialization of hansen objects
  2213.          * @param auxiliaryElements auxiliary elements related to the current orbit
  2214.          * @param field field used by default
  2215.          */
  2216.         private void generateCoefficients(final FieldAbsoluteDate<T> date,
  2217.                                           final FieldDSSTZonalContext<T> context,
  2218.                                           final FieldAuxiliaryElements<T> auxiliaryElements,
  2219.                                           final FieldHansenObjects<T> hansenObjects,
  2220.                                           final Field<T> field) {

  2221.             //Zero
  2222.             final T zero = field.getZero();

  2223.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(date.toAbsoluteDate());
  2224.             for (int j = 1; j <= jMax; j++) {

  2225.                 //init arrays
  2226.                 for (int i = 0; i <= 6; i++) {
  2227.                     cCoef[i][j] = zero;
  2228.                     sCoef[i][j] = zero;
  2229.                 }

  2230.                 if (isBetween(j, 1, nMax - 1)) {

  2231.                     //compute first double sum where s: j -> N-1 and n: s+1 -> N
  2232.                     for (int s = j; s <= FastMath.min(nMax - 1, sMax); s++) {
  2233.                         // j - s
  2234.                         final int jms = j - s;
  2235.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2236.                         final int d0smj = (s == j) ? 1 : 2;

  2237.                         for (int n = s + 1; n <= nMax; n++) {
  2238.                             // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2239.                             if ((n + jms) % 2 == 0) {
  2240.                                 // (2 - delta(0,s-j)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2241.                                 final T lns  = lnsCoef.getLns(n, -jms);
  2242.                                 final T dlns = lnsCoef.getdLnsdGamma(n, -jms);

  2243.                                 final T hjs        = ghijCoef.getHjs(s, -jms);
  2244.                                 final T dHjsdh     = ghijCoef.getdHjsdh(s, -jms);
  2245.                                 final T dHjsdk     = ghijCoef.getdHjsdk(s, -jms);
  2246.                                 final T dHjsdAlpha = ghijCoef.getdHjsdAlpha(s, -jms);
  2247.                                 final T dHjsdBeta  = ghijCoef.getdHjsdBeta(s, -jms);

  2248.                                 final T gjs        = ghijCoef.getGjs(s, -jms);
  2249.                                 final T dGjsdh     = ghijCoef.getdGjsdh(s, -jms);
  2250.                                 final T dGjsdk     = ghijCoef.getdGjsdk(s, -jms);
  2251.                                 final T dGjsdAlpha = ghijCoef.getdGjsdAlpha(s, -jms);
  2252.                                 final T dGjsdBeta  = ghijCoef.getdGjsdBeta(s, -jms);

  2253.                                 // J<sub>n</sub>
  2254.                                 final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2255.                                 // K₀<sup>-n-1,s</sup>
  2256.                                 final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2257.                                 final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2258.                                 final T coef0 = jn.multiply(d0smj);
  2259.                                 final T coef1 = coef0.multiply(lns);
  2260.                                 final T coef2 = coef1.multiply(kns);
  2261.                                 final T coef3 = coef2.multiply(hjs);
  2262.                                 final T coef4 = coef2.multiply(gjs);

  2263.                                 // Add the term to the coefficients
  2264.                                 cCoef[0][j] = cCoef[0][j].add(coef3);
  2265.                                 cCoef[1][j] = cCoef[1][j].add(coef3.multiply(n + 1));
  2266.                                 cCoef[2][j] = cCoef[2][j].add(coef1.multiply(kns.multiply(dHjsdh).add(hjs.multiply(hXXX).multiply(dkns))));
  2267.                                 cCoef[3][j] = cCoef[3][j].add(coef1.multiply(kns.multiply(dHjsdk).add(hjs.multiply(kXXX).multiply(dkns))));
  2268.                                 cCoef[4][j] = cCoef[4][j].add(coef2.multiply(dHjsdAlpha));
  2269.                                 cCoef[5][j] = cCoef[5][j].add(coef2.multiply(dHjsdBeta));
  2270.                                 cCoef[6][j] = cCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(hjs));

  2271.                                 sCoef[0][j] = sCoef[0][j].add(coef4);
  2272.                                 sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2273.                                 sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dGjsdh).add(gjs.multiply(hXXX).multiply(dkns))));
  2274.                                 sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dGjsdk).add(gjs.multiply(kXXX).multiply(dkns))));
  2275.                                 sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dGjsdAlpha));
  2276.                                 sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dGjsdBeta));
  2277.                                 sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(gjs));
  2278.                             }
  2279.                         }
  2280.                     }

  2281.                     //compute second double sum where s: 0 -> N-j and n: max(j+s, j+1) -> N
  2282.                     for (int s = 0; s <= FastMath.min(nMax - j, sMax); s++) {
  2283.                         // j + s
  2284.                         final int jps = j + s;
  2285.                         // Kronecker symbols (2 - delta(0,j+s))
  2286.                         final double d0spj = (s == -j) ? 1 : 2;

  2287.                         for (int n = FastMath.max(j + s, j + 1); n <= nMax; n++) {
  2288.                             // if n + (j+s) is odd, then the term is equal to zero due to the factor Vn,s+j
  2289.                             if ((n + jps) % 2 == 0) {
  2290.                                 // (2 - delta(0,s+j)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j+s</sup>
  2291.                                 final T lns  = lnsCoef.getLns(n, jps);
  2292.                                 final T dlns = lnsCoef.getdLnsdGamma(n, jps);

  2293.                                 final T hjs        = ghijCoef.getHjs(s, jps);
  2294.                                 final T dHjsdh     = ghijCoef.getdHjsdh(s, jps);
  2295.                                 final T dHjsdk     = ghijCoef.getdHjsdk(s, jps);
  2296.                                 final T dHjsdAlpha = ghijCoef.getdHjsdAlpha(s, jps);
  2297.                                 final T dHjsdBeta  = ghijCoef.getdHjsdBeta(s, jps);

  2298.                                 final T gjs        = ghijCoef.getGjs(s, jps);
  2299.                                 final T dGjsdh     = ghijCoef.getdGjsdh(s, jps);
  2300.                                 final T dGjsdk     = ghijCoef.getdGjsdk(s, jps);
  2301.                                 final T dGjsdAlpha = ghijCoef.getdGjsdAlpha(s, jps);
  2302.                                 final T dGjsdBeta  = ghijCoef.getdGjsdBeta(s, jps);

  2303.                                 // J<sub>n</sub>
  2304.                                 final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2305.                                 // K₀<sup>-n-1,s</sup>
  2306.                                 final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2307.                                 final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2308.                                 final T coef0 = jn.multiply(d0spj);
  2309.                                 final T coef1 = coef0.multiply(lns);
  2310.                                 final T coef2 = coef1.multiply(kns);

  2311.                                 final T coef3 = coef2.multiply(hjs);
  2312.                                 final T coef4 = coef2.multiply(gjs);

  2313.                                 // Add the term to the coefficients
  2314.                                 cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2315.                                 cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2316.                                 cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dHjsdh).add(hjs.multiply(hXXX).multiply(dkns))));
  2317.                                 cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dHjsdk).add(hjs.multiply(kXXX).multiply(dkns))));
  2318.                                 cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dHjsdAlpha));
  2319.                                 cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dHjsdBeta));
  2320.                                 cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(hjs));

  2321.                                 sCoef[0][j] = sCoef[0][j].add(coef4);
  2322.                                 sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2323.                                 sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dGjsdh).add(gjs.multiply(hXXX).multiply(dkns))));
  2324.                                 sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dGjsdk).add(gjs.multiply(kXXX).multiply(dkns))));
  2325.                                 sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dGjsdAlpha));
  2326.                                 sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dGjsdBeta));
  2327.                                 sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(gjs));
  2328.                             }
  2329.                         }
  2330.                     }

  2331.                     //compute third double sum where s: 1 -> j and  n: j+1 -> N
  2332.                     for (int s = 1; s <= FastMath.min(j, sMax); s++) {
  2333.                         // j - s
  2334.                         final int jms = j - s;
  2335.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2336.                         final int d0smj = (s == j) ? 1 : 2;

  2337.                         for (int n = j + 1; n <= nMax; n++) {
  2338.                             // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2339.                             if ((n + jms) % 2 == 0) {
  2340.                                 // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2341.                                 final T lns  = lnsCoef.getLns(n, jms);
  2342.                                 final T dlns = lnsCoef.getdLnsdGamma(n, jms);

  2343.                                 final T ijs        = ghijCoef.getIjs(s, jms);
  2344.                                 final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2345.                                 final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2346.                                 final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2347.                                 final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2348.                                 final T jjs        = ghijCoef.getJjs(s, jms);
  2349.                                 final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2350.                                 final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2351.                                 final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2352.                                 final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2353.                                 // J<sub>n</sub>
  2354.                                 final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2355.                                 // K₀<sup>-n-1,s</sup>
  2356.                                 final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2357.                                 final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2358.                                 final T coef0 = jn.multiply(d0smj);
  2359.                                 final T coef1 = coef0.multiply(lns);
  2360.                                 final T coef2 = coef1.multiply(kns);

  2361.                                 final T coef3 = coef2.multiply(ijs);
  2362.                                 final T coef4 = coef2.multiply(jjs);

  2363.                                 // Add the term to the coefficients
  2364.                                 cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2365.                                 cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2366.                                 cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2367.                                 cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2368.                                 cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2369.                                 cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2370.                                 cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2371.                                 sCoef[0][j] = sCoef[0][j].add(coef4);
  2372.                                 sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2373.                                 sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2374.                                 sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2375.                                 sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2376.                                 sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2377.                                 sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2378.                             }
  2379.                         }
  2380.                     }
  2381.                 }

  2382.                 if (isBetween(j, 2, nMax)) {
  2383.                     //add first term
  2384.                     // J<sub>j</sub>
  2385.                     final T jj = zero.subtract(harmonics.getUnnormalizedCnm(j, 0));
  2386.                     T kns  = (T) hansenObjects.getHansenObjects()[0].getValue(-j - 1, context.getX());
  2387.                     T dkns = (T) hansenObjects.getHansenObjects()[0].getDerivative(-j - 1, context.getX());

  2388.                     T lns = lnsCoef.getLns(j, j);
  2389.                     //dlns is 0 because n == s == j

  2390.                     final T hjs        = ghijCoef.getHjs(0, j);
  2391.                     final T dHjsdh     = ghijCoef.getdHjsdh(0, j);
  2392.                     final T dHjsdk     = ghijCoef.getdHjsdk(0, j);
  2393.                     final T dHjsdAlpha = ghijCoef.getdHjsdAlpha(0, j);
  2394.                     final T dHjsdBeta  = ghijCoef.getdHjsdBeta(0, j);

  2395.                     final T gjs        = ghijCoef.getGjs(0, j);
  2396.                     final T dGjsdh     = ghijCoef.getdGjsdh(0, j);
  2397.                     final T dGjsdk     = ghijCoef.getdGjsdk(0, j);
  2398.                     final T dGjsdAlpha = ghijCoef.getdGjsdAlpha(0, j);
  2399.                     final T dGjsdBeta  = ghijCoef.getdGjsdBeta(0, j);

  2400.                     // 2 * J<sub>j</sub> * K₀<sup>-j-1,0</sup> * L<sub>j</sub><sup>j</sup>
  2401.                     T coef0 = jj.multiply(2.);
  2402.                     T coef1 = coef0.multiply(lns);
  2403.                     T coef2 = coef1.multiply(kns);

  2404.                     T coef3 = coef2.multiply(hjs);
  2405.                     T coef4 = coef2.multiply(gjs);

  2406.                     // Add the term to the coefficients
  2407.                     cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2408.                     cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(j + 1));
  2409.                     cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dHjsdh).add(hjs.multiply(hXXX).multiply(dkns))));
  2410.                     cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dHjsdk).add(hjs.multiply(kXXX).multiply(dkns))));
  2411.                     cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dHjsdAlpha));
  2412.                     cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dHjsdBeta));
  2413.                     //no contribution to cCoef[6][j] because dlns is 0

  2414.                     sCoef[0][j] = sCoef[0][j].add(coef4);
  2415.                     sCoef[1][j] = sCoef[1][j].add(coef4.multiply(j + 1));
  2416.                     sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dGjsdh).add(gjs.multiply(hXXX).multiply(dkns))));
  2417.                     sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dGjsdk).add(gjs.multiply(kXXX).multiply(dkns))));
  2418.                     sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dGjsdAlpha));
  2419.                     sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dGjsdBeta));
  2420.                     //no contribution to sCoef[6][j] because dlns is 0

  2421.                     //compute simple sum where s: 1 -> j-1
  2422.                     for (int s = 1; s <= FastMath.min(j - 1, sMax); s++) {
  2423.                         // j - s
  2424.                         final int jms = j - s;
  2425.                         // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2426.                         final int d0smj = (s == j) ? 1 : 2;

  2427.                         // if s is odd, then the term is equal to zero due to the factor Vj,s-j
  2428.                         if (s % 2 == 0) {
  2429.                             // (2 - delta(0,j-s)) * J<sub>j</sub> * K₀<sup>-j-1,s</sup> * L<sub>j</sub><sup>j-s</sup>
  2430.                             kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-j - 1, context.getX());
  2431.                             dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-j - 1, context.getX());

  2432.                             lns = lnsCoef.getLns(j, jms);
  2433.                             final T dlns = lnsCoef.getdLnsdGamma(j, jms);

  2434.                             final T ijs        = ghijCoef.getIjs(s, jms);
  2435.                             final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2436.                             final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2437.                             final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2438.                             final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2439.                             final T jjs        = ghijCoef.getJjs(s, jms);
  2440.                             final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2441.                             final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2442.                             final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2443.                             final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2444.                             coef0 = jj.multiply(d0smj);
  2445.                             coef1 = coef0.multiply(lns);
  2446.                             coef2 = coef1.multiply(kns);

  2447.                             coef3 = coef2.multiply(ijs);
  2448.                             coef4 = coef2.multiply(jjs);

  2449.                             // Add the term to the coefficients
  2450.                             cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2451.                             cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(j + 1));
  2452.                             cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2453.                             cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2454.                             cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2455.                             cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2456.                             cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2457.                             sCoef[0][j] = sCoef[0][j].add(coef4);
  2458.                             sCoef[1][j] = sCoef[1][j].add(coef4.multiply(j + 1));
  2459.                             sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2460.                             sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2461.                             sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2462.                             sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2463.                             sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2464.                         }
  2465.                     }
  2466.                 }

  2467.                 if (isBetween(j, 3, 2 * nMax - 1)) {
  2468.                     //compute uppercase sigma expressions

  2469.                     //min(j-1,N)
  2470.                     final int minjm1on = FastMath.min(j - 1, nMax);

  2471.                     //if j is even
  2472.                     if (j % 2 == 0) {
  2473.                         //compute first double sum where s: j-min(j-1,N) -> j/2-1 and n: j-s -> min(j-1,N)
  2474.                         for (int s = j - minjm1on; s <= FastMath.min(j / 2 - 1, sMax); s++) {
  2475.                             // j - s
  2476.                             final int jms = j - s;
  2477.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2478.                             final int d0smj = (s == j) ? 1 : 2;

  2479.                             for (int n = j - s; n <= minjm1on; n++) {
  2480.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2481.                                 if ((n + jms) % 2 == 0) {
  2482.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2483.                                     final T lns  = lnsCoef.getLns(n, jms);
  2484.                                     final T dlns = lnsCoef.getdLnsdGamma(n, jms);

  2485.                                     final T ijs        = ghijCoef.getIjs(s, jms);
  2486.                                     final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2487.                                     final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2488.                                     final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2489.                                     final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2490.                                     final T jjs        = ghijCoef.getJjs(s, jms);
  2491.                                     final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2492.                                     final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2493.                                     final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2494.                                     final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2495.                                     // J<sub>n</sub>
  2496.                                     final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2497.                                     // K₀<sup>-n-1,s</sup>
  2498.                                     final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2499.                                     final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2500.                                     final T coef0 = jn.multiply(d0smj);
  2501.                                     final T coef1 = coef0.multiply(lns);
  2502.                                     final T coef2 = coef1.multiply(kns);

  2503.                                     final T coef3 = coef2.multiply(ijs);
  2504.                                     final T coef4 = coef2.multiply(jjs);

  2505.                                     // Add the term to the coefficients
  2506.                                     cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2507.                                     cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2508.                                     cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2509.                                     cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2510.                                     cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2511.                                     cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2512.                                     cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2513.                                     sCoef[0][j] = sCoef[0][j].add(coef4);
  2514.                                     sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2515.                                     sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2516.                                     sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2517.                                     sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2518.                                     sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2519.                                     sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2520.                                 }
  2521.                             }
  2522.                         }

  2523.                         //compute second double sum where s: j/2 -> min(j-1,N)-1 and n: s+1 -> min(j-1,N)
  2524.                         for (int s = j / 2; s <=  FastMath.min(minjm1on - 1, sMax); s++) {
  2525.                             // j - s
  2526.                             final int jms = j - s;
  2527.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2528.                             final int d0smj = (s == j) ? 1 : 2;

  2529.                             for (int n = s + 1; n <= minjm1on; n++) {
  2530.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2531.                                 if ((n + jms) % 2 == 0) {
  2532.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2533.                                     final T lns  = lnsCoef.getLns(n, jms);
  2534.                                     final T dlns = lnsCoef.getdLnsdGamma(n, jms);

  2535.                                     final T ijs        = ghijCoef.getIjs(s, jms);
  2536.                                     final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2537.                                     final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2538.                                     final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2539.                                     final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2540.                                     final T jjs        = ghijCoef.getJjs(s, jms);
  2541.                                     final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2542.                                     final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2543.                                     final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2544.                                     final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2545.                                     // J<sub>n</sub>
  2546.                                     final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2547.                                     // K₀<sup>-n-1,s</sup>
  2548.                                     final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2549.                                     final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2550.                                     final T coef0 = jn.multiply(d0smj);
  2551.                                     final T coef1 = coef0.multiply(lns);
  2552.                                     final T coef2 = coef1.multiply(kns);

  2553.                                     final T coef3 = coef2.multiply(ijs);
  2554.                                     final T coef4 = coef2.multiply(jjs);

  2555.                                     // Add the term to the coefficients
  2556.                                     cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2557.                                     cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2558.                                     cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2559.                                     cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2560.                                     cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2561.                                     cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2562.                                     cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2563.                                     sCoef[0][j] = sCoef[0][j].add(coef4);
  2564.                                     sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2565.                                     sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2566.                                     sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2567.                                     sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2568.                                     sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2569.                                     sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2570.                                 }
  2571.                             }
  2572.                         }
  2573.                     }

  2574.                     //if j is odd
  2575.                     else {
  2576.                         //compute first double sum where s: (j-1)/2 -> min(j-1,N)-1 and n: s+1 -> min(j-1,N)
  2577.                         for (int s = (j - 1) / 2; s <= FastMath.min(minjm1on - 1, sMax); s++) {
  2578.                             // j - s
  2579.                             final int jms = j - s;
  2580.                             // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2581.                             final int d0smj = (s == j) ? 1 : 2;

  2582.                             for (int n = s + 1; n <= minjm1on; n++) {
  2583.                                 // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2584.                                 if ((n + jms) % 2 == 0) {
  2585.                                     // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2586.                                     final T lns  = lnsCoef.getLns(n, jms);
  2587.                                     final T dlns = lnsCoef.getdLnsdGamma(n, jms);

  2588.                                     final T ijs        = ghijCoef.getIjs(s, jms);
  2589.                                     final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2590.                                     final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2591.                                     final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2592.                                     final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2593.                                     final T jjs        = ghijCoef.getJjs(s, jms);
  2594.                                     final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2595.                                     final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2596.                                     final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2597.                                     final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2598.                                     // J<sub>n</sub>
  2599.                                     final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2600.                                     // K₀<sup>-n-1,s</sup>

  2601.                                     final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2602.                                     final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2603.                                     final T coef0 = jn.multiply(d0smj);
  2604.                                     final T coef1 = coef0.multiply(lns);
  2605.                                     final T coef2 = coef1.multiply(kns);

  2606.                                     final T coef3 = coef2.multiply(ijs);
  2607.                                     final T coef4 = coef2.multiply(jjs);

  2608.                                     // Add the term to the coefficients
  2609.                                     cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2610.                                     cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2611.                                     cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2612.                                     cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2613.                                     cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2614.                                     cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2615.                                     cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2616.                                     sCoef[0][j] = sCoef[0][j].add(coef4);
  2617.                                     sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2618.                                     sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2619.                                     sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2620.                                     sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2621.                                     sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2622.                                     sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2623.                                 }
  2624.                             }
  2625.                         }

  2626.                         //the second double sum is added only if N >= 4 and j between 5 and 2*N-3
  2627.                         if (nMax >= 4 && isBetween(j, 5, 2 * nMax - 3)) {
  2628.                             //compute second double sum where s: j-min(j-1,N) -> (j-3)/2 and n: j-s -> min(j-1,N)
  2629.                             for (int s = j - minjm1on; s <= FastMath.min((j - 3) / 2, sMax); s++) {
  2630.                                 // j - s
  2631.                                 final int jms = j - s;
  2632.                                 // Kronecker symbols (2 - delta(0,s-j)) and (2 - delta(0,j-s))
  2633.                                 final int d0smj = (s == j) ? 1 : 2;

  2634.                                 for (int n = j - s; n <= minjm1on; n++) {
  2635.                                     // if n + (j-s) is odd, then the term is equal to zero due to the factor Vn,s-j
  2636.                                     if ((n + jms) % 2 == 0) {
  2637.                                         // (2 - delta(0,j-s)) * J<sub>n</sub> * K₀<sup>-n-1,s</sup> * L<sub>n</sub><sup>j-s</sup>
  2638.                                         final T lns  = lnsCoef.getLns(n, jms);
  2639.                                         final T dlns = lnsCoef.getdLnsdGamma(n, jms);

  2640.                                         final T ijs        = ghijCoef.getIjs(s, jms);
  2641.                                         final T dIjsdh     = ghijCoef.getdIjsdh(s, jms);
  2642.                                         final T dIjsdk     = ghijCoef.getdIjsdk(s, jms);
  2643.                                         final T dIjsdAlpha = ghijCoef.getdIjsdAlpha(s, jms);
  2644.                                         final T dIjsdBeta  = ghijCoef.getdIjsdBeta(s, jms);

  2645.                                         final T jjs        = ghijCoef.getJjs(s, jms);
  2646.                                         final T dJjsdh     = ghijCoef.getdJjsdh(s, jms);
  2647.                                         final T dJjsdk     = ghijCoef.getdJjsdk(s, jms);
  2648.                                         final T dJjsdAlpha = ghijCoef.getdJjsdAlpha(s, jms);
  2649.                                         final T dJjsdBeta  = ghijCoef.getdJjsdBeta(s, jms);

  2650.                                         // J<sub>n</sub>
  2651.                                         final T jn = zero.subtract(harmonics.getUnnormalizedCnm(n, 0));

  2652.                                         // K₀<sup>-n-1,s</sup>
  2653.                                         final T kns   = (T) hansenObjects.getHansenObjects()[s].getValue(-n - 1, context.getX());
  2654.                                         final T dkns  = (T) hansenObjects.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  2655.                                         final T coef0 = jn.multiply(d0smj);
  2656.                                         final T coef1 = coef0.multiply(lns);
  2657.                                         final T coef2 = coef1.multiply(kns);

  2658.                                         final T coef3 = coef2.multiply(ijs);
  2659.                                         final T coef4 = coef2.multiply(jjs);

  2660.                                         // Add the term to the coefficients
  2661.                                         cCoef[0][j] = cCoef[0][j].subtract(coef3);
  2662.                                         cCoef[1][j] = cCoef[1][j].subtract(coef3.multiply(n + 1));
  2663.                                         cCoef[2][j] = cCoef[2][j].subtract(coef1.multiply(kns.multiply(dIjsdh).add(ijs.multiply(hXXX).multiply(dkns))));
  2664.                                         cCoef[3][j] = cCoef[3][j].subtract(coef1.multiply(kns.multiply(dIjsdk).add(ijs.multiply(kXXX).multiply(dkns))));
  2665.                                         cCoef[4][j] = cCoef[4][j].subtract(coef2.multiply(dIjsdAlpha));
  2666.                                         cCoef[5][j] = cCoef[5][j].subtract(coef2.multiply(dIjsdBeta));
  2667.                                         cCoef[6][j] = cCoef[6][j].subtract(coef0.multiply(dlns).multiply(kns).multiply(ijs));

  2668.                                         sCoef[0][j] = sCoef[0][j].add(coef4);
  2669.                                         sCoef[1][j] = sCoef[1][j].add(coef4.multiply(n + 1));
  2670.                                         sCoef[2][j] = sCoef[2][j].add(coef1.multiply(kns.multiply(dJjsdh).add(jjs.multiply(hXXX).multiply(dkns))));
  2671.                                         sCoef[3][j] = sCoef[3][j].add(coef1.multiply(kns.multiply(dJjsdk).add(jjs.multiply(kXXX).multiply(dkns))));
  2672.                                         sCoef[4][j] = sCoef[4][j].add(coef2.multiply(dJjsdAlpha));
  2673.                                         sCoef[5][j] = sCoef[5][j].add(coef2.multiply(dJjsdBeta));
  2674.                                         sCoef[6][j] = sCoef[6][j].add(coef0.multiply(dlns).multiply(kns).multiply(jjs));
  2675.                                     }
  2676.                                 }
  2677.                             }
  2678.                         }
  2679.                     }
  2680.                 }

  2681.                 cCoef[0][j] = cCoef[0][j].multiply(context.getMuoa().divide(j).negate());
  2682.                 cCoef[1][j] = cCoef[1][j].multiply(context.getMuoa().divide(auxiliaryElements.getSma().multiply(j)));
  2683.                 cCoef[2][j] = cCoef[2][j].multiply(context.getMuoa().divide(j).negate());
  2684.                 cCoef[3][j] = cCoef[3][j].multiply(context.getMuoa().divide(j).negate());
  2685.                 cCoef[4][j] = cCoef[4][j].multiply(context.getMuoa().divide(j).negate());
  2686.                 cCoef[5][j] = cCoef[5][j].multiply(context.getMuoa().divide(j).negate());
  2687.                 cCoef[6][j] = cCoef[6][j].multiply(context.getMuoa().divide(j).negate());

  2688.                 sCoef[0][j] = sCoef[0][j].multiply(context.getMuoa().divide(j).negate());
  2689.                 sCoef[1][j] = sCoef[1][j].multiply(context.getMuoa().divide(auxiliaryElements.getSma().multiply(j)));
  2690.                 sCoef[2][j] = sCoef[2][j].multiply(context.getMuoa().divide(j).negate());
  2691.                 sCoef[3][j] = sCoef[3][j].multiply(context.getMuoa().divide(j).negate());
  2692.                 sCoef[4][j] = sCoef[4][j].multiply(context.getMuoa().divide(j).negate());
  2693.                 sCoef[5][j] = sCoef[5][j].multiply(context.getMuoa().divide(j).negate());
  2694.                 sCoef[6][j] = sCoef[6][j].multiply(context.getMuoa().divide(j).negate());

  2695.             }
  2696.         }

  2697.         /** Check if an index is within the accepted interval.
  2698.          *
  2699.          * @param index the index to check
  2700.          * @param lowerBound the lower bound of the interval
  2701.          * @param upperBound the upper bound of the interval
  2702.          * @return true if the index is between the lower and upper bounds, false otherwise
  2703.          */
  2704.         private boolean isBetween(final int index, final int lowerBound, final int upperBound) {
  2705.             return index >= lowerBound && index <= upperBound;
  2706.         }

  2707.         /**Get the value of C<sup>j</sup>.
  2708.          *
  2709.          * @param j j index
  2710.          * @return C<sup>j</sup>
  2711.          */
  2712.         public T getCj(final int j) {
  2713.             return cCoef[0][j];
  2714.         }

  2715.         /**Get the value of dC<sup>j</sup> / da.
  2716.          *
  2717.          * @param j j index
  2718.          * @return dC<sup>j</sup> / da
  2719.          */
  2720.         public T getdCjdA(final int j) {
  2721.             return cCoef[1][j];
  2722.         }

  2723.         /**Get the value of dC<sup>j</sup> / dh.
  2724.          *
  2725.          * @param j j index
  2726.          * @return dC<sup>j</sup> / dh
  2727.          */
  2728.         public T getdCjdH(final int j) {
  2729.             return cCoef[2][j];
  2730.         }

  2731.         /**Get the value of dC<sup>j</sup> / dk.
  2732.          *
  2733.          * @param j j index
  2734.          * @return dC<sup>j</sup> / dk
  2735.          */
  2736.         public T getdCjdK(final int j) {
  2737.             return cCoef[3][j];
  2738.         }

  2739.         /**Get the value of dC<sup>j</sup> / dα.
  2740.          *
  2741.          * @param j j index
  2742.          * @return dC<sup>j</sup> / dα
  2743.          */
  2744.         public T getdCjdAlpha(final int j) {
  2745.             return cCoef[4][j];
  2746.         }

  2747.         /**Get the value of dC<sup>j</sup> / dβ.
  2748.          *
  2749.          * @param j j index
  2750.          * @return dC<sup>j</sup> / dβ
  2751.          */
  2752.         public T getdCjdBeta(final int j) {
  2753.             return cCoef[5][j];
  2754.         }

  2755.         /**Get the value of dC<sup>j</sup> / dγ.
  2756.          *
  2757.          * @param j j index
  2758.          * @return dC<sup>j</sup> / dγ
  2759.          */
  2760.         public T getdCjdGamma(final int j) {
  2761.             return cCoef[6][j];
  2762.         }

  2763.         /**Get the value of S<sup>j</sup>.
  2764.          *
  2765.          * @param j j index
  2766.          * @return S<sup>j</sup>
  2767.          */
  2768.         public T getSj(final int j) {
  2769.             return sCoef[0][j];
  2770.         }

  2771.         /**Get the value of dS<sup>j</sup> / da.
  2772.          *
  2773.          * @param j j index
  2774.          * @return dS<sup>j</sup> / da
  2775.          */
  2776.         public T getdSjdA(final int j) {
  2777.             return sCoef[1][j];
  2778.         }

  2779.         /**Get the value of dS<sup>j</sup> / dh.
  2780.          *
  2781.          * @param j j index
  2782.          * @return dS<sup>j</sup> / dh
  2783.          */
  2784.         public T getdSjdH(final int j) {
  2785.             return sCoef[2][j];
  2786.         }

  2787.         /**Get the value of dS<sup>j</sup> / dk.
  2788.          *
  2789.          * @param j j index
  2790.          * @return dS<sup>j</sup> / dk
  2791.          */
  2792.         public T getdSjdK(final int j) {
  2793.             return sCoef[3][j];
  2794.         }

  2795.         /**Get the value of dS<sup>j</sup> / dα.
  2796.          *
  2797.          * @param j j index
  2798.          * @return dS<sup>j</sup> / dα
  2799.          */
  2800.         public T getdSjdAlpha(final int j) {
  2801.             return sCoef[4][j];
  2802.         }

  2803.         /**Get the value of dS<sup>j</sup> / dβ.
  2804.          *
  2805.          * @param j j index
  2806.          * @return dS<sup>j</sup> / dβ
  2807.          */
  2808.         public T getdSjdBeta(final int j) {
  2809.             return sCoef[5][j];
  2810.         }

  2811.         /**Get the value of dS<sup>j</sup> /  dγ.
  2812.          *
  2813.          * @param j j index
  2814.          * @return dS<sup>j</sup> /  dγ
  2815.          */
  2816.         public T getdSjdGamma(final int j) {
  2817.             return sCoef[6][j];
  2818.         }
  2819.     }

  2820.     /** Coefficients valid for one time slot. */
  2821.     private static class Slot {

  2822.         /**The coefficients D<sub>i</sub>.
  2823.          * <p>
  2824.          * i corresponds to the equinoctial element, as follows:
  2825.          * - i=0 for a <br/>
  2826.          * - i=1 for k <br/>
  2827.          * - i=2 for h <br/>
  2828.          * - i=3 for q <br/>
  2829.          * - i=4 for p <br/>
  2830.          * - i=5 for λ <br/>
  2831.          * </p>
  2832.          */
  2833.         private final ShortPeriodicsInterpolatedCoefficient di;

  2834.         /** The coefficients C<sub>i</sub><sup>j</sup>.
  2835.          * <p>
  2836.          * The constant term C<sub>i</sub>⁰ is also stored in this variable at index j = 0 <br>
  2837.          * The index order is cij[j][i] <br/>
  2838.          * i corresponds to the equinoctial element, as follows: <br/>
  2839.          * - i=0 for a <br/>
  2840.          * - i=1 for k <br/>
  2841.          * - i=2 for h <br/>
  2842.          * - i=3 for q <br/>
  2843.          * - i=4 for p <br/>
  2844.          * - i=5 for λ <br/>
  2845.          * </p>
  2846.          */
  2847.         private final ShortPeriodicsInterpolatedCoefficient[] cij;

  2848.         /** The coefficients S<sub>i</sub><sup>j</sup>.
  2849.          * <p>
  2850.          * The index order is sij[j][i] <br/>
  2851.          * i corresponds to the equinoctial element, as follows: <br/>
  2852.          * - i=0 for a <br/>
  2853.          * - i=1 for k <br/>
  2854.          * - i=2 for h <br/>
  2855.          * - i=3 for q <br/>
  2856.          * - i=4 for p <br/>
  2857.          * - i=5 for λ <br/>
  2858.          * </p>
  2859.          */
  2860.         private final ShortPeriodicsInterpolatedCoefficient[] sij;

  2861.         /** Simple constructor.
  2862.          *  @param maxFrequencyShortPeriodics maximum value for j index
  2863.          *  @param interpolationPoints number of points used in the interpolation process
  2864.          */
  2865.         Slot(final int maxFrequencyShortPeriodics, final int interpolationPoints) {

  2866.             final int rows = maxFrequencyShortPeriodics + 1;
  2867.             di  = new ShortPeriodicsInterpolatedCoefficient(interpolationPoints);
  2868.             cij = new ShortPeriodicsInterpolatedCoefficient[rows];
  2869.             sij = new ShortPeriodicsInterpolatedCoefficient[rows];

  2870.             //Initialize the arrays
  2871.             for (int j = 0; j <= maxFrequencyShortPeriodics; j++) {
  2872.                 cij[j] = new ShortPeriodicsInterpolatedCoefficient(interpolationPoints);
  2873.                 sij[j] = new ShortPeriodicsInterpolatedCoefficient(interpolationPoints);
  2874.             }

  2875.         }

  2876.     }

  2877.     /** Coefficients valid for one time slot. */
  2878.     private static class FieldSlot <T extends RealFieldElement<T>> {

  2879.         /**The coefficients D<sub>i</sub>.
  2880.          * <p>
  2881.          * i corresponds to the equinoctial element, as follows:
  2882.          * - i=0 for a <br/>
  2883.          * - i=1 for k <br/>
  2884.          * - i=2 for h <br/>
  2885.          * - i=3 for q <br/>
  2886.          * - i=4 for p <br/>
  2887.          * - i=5 for λ <br/>
  2888.          * </p>
  2889.          */
  2890.         private final FieldShortPeriodicsInterpolatedCoefficient<T> di;

  2891.         /** The coefficients C<sub>i</sub><sup>j</sup>.
  2892.          * <p>
  2893.          * The constant term C<sub>i</sub>⁰ is also stored in this variable at index j = 0 <br>
  2894.          * The index order is cij[j][i] <br/>
  2895.          * i corresponds to the equinoctial element, as follows: <br/>
  2896.          * - i=0 for a <br/>
  2897.          * - i=1 for k <br/>
  2898.          * - i=2 for h <br/>
  2899.          * - i=3 for q <br/>
  2900.          * - i=4 for p <br/>
  2901.          * - i=5 for λ <br/>
  2902.          * </p>
  2903.          */
  2904.         private final FieldShortPeriodicsInterpolatedCoefficient<T>[] cij;

  2905.         /** The coefficients S<sub>i</sub><sup>j</sup>.
  2906.          * <p>
  2907.          * The index order is sij[j][i] <br/>
  2908.          * i corresponds to the equinoctial element, as follows: <br/>
  2909.          * - i=0 for a <br/>
  2910.          * - i=1 for k <br/>
  2911.          * - i=2 for h <br/>
  2912.          * - i=3 for q <br/>
  2913.          * - i=4 for p <br/>
  2914.          * - i=5 for λ <br/>
  2915.          * </p>
  2916.          */
  2917.         private final FieldShortPeriodicsInterpolatedCoefficient<T>[] sij;

  2918.         /** Simple constructor.
  2919.          *  @param maxFrequencyShortPeriodics maximum value for j index
  2920.          *  @param interpolationPoints number of points used in the interpolation process
  2921.          */
  2922.         @SuppressWarnings("unchecked")
  2923.         FieldSlot(final int maxFrequencyShortPeriodics, final int interpolationPoints) {

  2924.             final int rows = maxFrequencyShortPeriodics + 1;
  2925.             di  = new FieldShortPeriodicsInterpolatedCoefficient<>(interpolationPoints);
  2926.             cij = (FieldShortPeriodicsInterpolatedCoefficient<T>[]) Array.newInstance(FieldShortPeriodicsInterpolatedCoefficient.class, rows);
  2927.             sij = (FieldShortPeriodicsInterpolatedCoefficient<T>[]) Array.newInstance(FieldShortPeriodicsInterpolatedCoefficient.class, rows);

  2928.             //Initialize the arrays
  2929.             for (int j = 0; j <= maxFrequencyShortPeriodics; j++) {
  2930.                 cij[j] = new FieldShortPeriodicsInterpolatedCoefficient<>(interpolationPoints);
  2931.                 sij[j] = new FieldShortPeriodicsInterpolatedCoefficient<>(interpolationPoints);
  2932.             }

  2933.         }

  2934.     }

  2935.     /** Compute potential and potential derivatives with respect to orbital parameters. */
  2936.     private class UAnddU {

  2937.         /** The current value of the U function. <br/>
  2938.          * Needed for the short periodic contribution */
  2939.         private double U;

  2940.         /** dU / da. */
  2941.         private double dUda;

  2942.         /** dU / dk. */
  2943.         private double dUdk;

  2944.         /** dU / dh. */
  2945.         private double dUdh;

  2946.         /** dU / dAlpha. */
  2947.         private double dUdAl;

  2948.         /** dU / dBeta. */
  2949.         private double dUdBe;

  2950.         /** dU / dGamma. */
  2951.         private double dUdGa;

  2952.         /** Simple constuctor.
  2953.          *  @param date current date
  2954.          *  @param context container for attributes
  2955.          *  @param auxiliaryElements auxiliary elements related to the current orbit
  2956.          *  @param hansen initialization of hansen objects
  2957.          */
  2958.         UAnddU(final AbsoluteDate date,
  2959.                final DSSTZonalContext context,
  2960.                final AuxiliaryElements auxiliaryElements,
  2961.                final HansenObjects hansen) {

  2962.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(date);

  2963.             //Reset U
  2964.             U = 0.;

  2965.             // Gs and Hs coefficients
  2966.             final double[][] GsHs = CoefficientsFactory.computeGsHs(auxiliaryElements.getK(), auxiliaryElements.getH(), auxiliaryElements.getAlpha(), auxiliaryElements.getBeta(), maxEccPowMeanElements);
  2967.             // Qns coefficients
  2968.             final double[][] Qns  = CoefficientsFactory.computeQns(auxiliaryElements.getGamma(), maxDegree, maxEccPowMeanElements);

  2969.             final double[] roaPow = new double[maxDegree + 1];
  2970.             roaPow[0] = 1.;
  2971.             for (int i = 1; i <= maxDegree; i++) {
  2972.                 roaPow[i] = context.getRoa() * roaPow[i - 1];
  2973.             }

  2974.             // Potential derivatives
  2975.             dUda  = 0.;
  2976.             dUdk  = 0.;
  2977.             dUdh  = 0.;
  2978.             dUdAl = 0.;
  2979.             dUdBe = 0.;
  2980.             dUdGa = 0.;

  2981.             for (int s = 0; s <= maxEccPowMeanElements; s++) {
  2982.                 //Initialize the Hansen roots
  2983.                 hansen.computeHansenObjectsInitValues(context, s);

  2984.                 // Get the current Gs coefficient
  2985.                 final double gs = GsHs[0][s];

  2986.                 // Compute Gs partial derivatives from 3.1-(9)
  2987.                 double dGsdh  = 0.;
  2988.                 double dGsdk  = 0.;
  2989.                 double dGsdAl = 0.;
  2990.                 double dGsdBe = 0.;
  2991.                 if (s > 0) {
  2992.                     // First get the G(s-1) and the H(s-1) coefficients
  2993.                     final double sxgsm1 = s * GsHs[0][s - 1];
  2994.                     final double sxhsm1 = s * GsHs[1][s - 1];
  2995.                     // Then compute derivatives
  2996.                     dGsdh  = auxiliaryElements.getBeta()  * sxgsm1 - auxiliaryElements.getAlpha() * sxhsm1;
  2997.                     dGsdk  = auxiliaryElements.getAlpha() * sxgsm1 + auxiliaryElements.getBeta()  * sxhsm1;
  2998.                     dGsdAl = auxiliaryElements.getK() * sxgsm1 - auxiliaryElements.getH() * sxhsm1;
  2999.                     dGsdBe = auxiliaryElements.getH() * sxgsm1 + auxiliaryElements.getK() * sxhsm1;
  3000.                 }

  3001.                 // Kronecker symbol (2 - delta(0,s))
  3002.                 final double d0s = (s == 0) ? 1 : 2;

  3003.                 for (int n = s + 2; n <= maxDegree; n++) {
  3004.                     // (n - s) must be even
  3005.                     if ((n - s) % 2 == 0) {

  3006.                         //Extract data from previous computation :
  3007.                         final double kns   = hansen.getHansenObjects()[s].getValue(-n - 1, context.getX());
  3008.                         final double dkns  = hansen.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  3009.                         final double vns   = Vns.get(new NSKey(n, s));
  3010.                         final double coef0 = d0s * roaPow[n] * vns * -harmonics.getUnnormalizedCnm(n, 0);
  3011.                         final double coef1 = coef0 * Qns[n][s];
  3012.                         final double coef2 = coef1 * kns;
  3013.                         final double coef3 = coef2 * gs;
  3014.                         // dQns/dGamma = Q(n, s + 1) from Equation 3.1-(8)
  3015.                         final double dqns  = Qns[n][s + 1];

  3016.                         // Compute U
  3017.                         U += coef3;
  3018.                         // Compute dU / da :
  3019.                         dUda += coef3 * (n + 1);
  3020.                         // Compute dU / dEx
  3021.                         dUdk += coef1 * (kns * dGsdk + auxiliaryElements.getK() * context.getXXX() * gs * dkns);
  3022.                         // Compute dU / dEy
  3023.                         dUdh += coef1 * (kns * dGsdh + auxiliaryElements.getH() * context.getXXX() * gs * dkns);
  3024.                         // Compute dU / dAlpha
  3025.                         dUdAl += coef2 * dGsdAl;
  3026.                         // Compute dU / dBeta
  3027.                         dUdBe += coef2 * dGsdBe;
  3028.                         // Compute dU / dGamma
  3029.                         dUdGa += coef0 * kns * dqns * gs;

  3030.                     }
  3031.                 }
  3032.             }

  3033.             // Multiply by -(μ / a)
  3034.             this.U = -context.getMuoa() * U;

  3035.             this.dUda = dUda *  context.getMuoa() / auxiliaryElements.getSma();
  3036.             this.dUdk = dUdk * -context.getMuoa();
  3037.             this.dUdh = dUdh * -context.getMuoa();
  3038.             this.dUdAl = dUdAl * -context.getMuoa();
  3039.             this.dUdBe = dUdBe * -context.getMuoa();
  3040.             this.dUdGa = dUdGa * -context.getMuoa();

  3041.         }

  3042.         /** Return value of U.
  3043.          * @return U
  3044.          */
  3045.         public double getU() {
  3046.             return U;
  3047.         }

  3048.         /** Return value of dU / da.
  3049.          * @return dUda
  3050.          */
  3051.         public double getdUda() {
  3052.             return dUda;
  3053.         }

  3054.         /** Return value of dU / dk.
  3055.          * @return dUdk
  3056.          */
  3057.         public double getdUdk() {
  3058.             return dUdk;
  3059.         }

  3060.         /** Return value of dU / dh.
  3061.          * @return dUdh
  3062.          */
  3063.         public double getdUdh() {
  3064.             return dUdh;
  3065.         }

  3066.         /** Return value of dU / dAlpha.
  3067.          * @return dUdAl
  3068.          */
  3069.         public double getdUdAl() {
  3070.             return dUdAl;
  3071.         }

  3072.         /** Return value of dU / dBeta.
  3073.          * @return dUdBe
  3074.          */
  3075.         public double getdUdBe() {
  3076.             return dUdBe;
  3077.         }

  3078.         /** Return value of dU / dGamma.
  3079.          * @return dUdGa
  3080.          */
  3081.         public double getdUdGa() {
  3082.             return dUdGa;
  3083.         }

  3084.     }

  3085.     /** Compute the derivatives of the gravitational potential U [Eq. 3.1-(6)].
  3086.      *  <p>
  3087.      *  The result is the array
  3088.      *  [dU/da, dU/dk, dU/dh, dU/dα, dU/dβ, dU/dγ]
  3089.      *  </p>
  3090.      */
  3091.     private class FieldUAnddU <T extends RealFieldElement<T>> {

  3092.          /** The current value of the U function. <br/>
  3093.           * Needed for the short periodic contribution */
  3094.         private T U;

  3095.          /** dU / da. */
  3096.         private T dUda;

  3097.          /** dU / dk. */
  3098.         private T dUdk;

  3099.          /** dU / dh. */
  3100.         private T dUdh;

  3101.          /** dU / dAlpha. */
  3102.         private T dUdAl;

  3103.          /** dU / dBeta. */
  3104.         private T dUdBe;

  3105.          /** dU / dGamma. */
  3106.         private T dUdGa;

  3107.          /** Simple constuctor.
  3108.           *  @param date current date
  3109.           *  @param context container for attributes
  3110.           *  @param auxiliaryElements auxiliary elements related to the current orbit
  3111.           *  @param hansen initialization of hansen objects
  3112.           */
  3113.         FieldUAnddU(final FieldAbsoluteDate<T> date,
  3114.                     final FieldDSSTZonalContext<T> context,
  3115.                     final FieldAuxiliaryElements<T> auxiliaryElements,
  3116.                     final FieldHansenObjects<T> hansen) {

  3117.             // Zero for initialization
  3118.             final Field<T> field = date.getField();
  3119.             final T zero = field.getZero();

  3120.             //spherical harmonics
  3121.             final UnnormalizedSphericalHarmonics harmonics = provider.onDate(date.toAbsoluteDate());

  3122.             //Reset U
  3123.             U = zero;

  3124.             // Gs and Hs coefficients
  3125.             final T[][] GsHs = CoefficientsFactory.computeGsHs(auxiliaryElements.getK(), auxiliaryElements.getH(), auxiliaryElements.getAlpha(), auxiliaryElements.getBeta(), maxEccPowMeanElements, field);
  3126.             // Qns coefficients
  3127.             final T[][] Qns  = CoefficientsFactory.computeQns(auxiliaryElements.getGamma(), maxDegree, maxEccPowMeanElements);

  3128.             final T[] roaPow = MathArrays.buildArray(field, maxDegree + 1);
  3129.             roaPow[0] = zero.add(1.);
  3130.             for (int i = 1; i <= maxDegree; i++) {
  3131.                 roaPow[i] = roaPow[i - 1].multiply(context.getRoa());
  3132.             }

  3133.             // Potential derivatives
  3134.             dUda  = zero;
  3135.             dUdk  = zero;
  3136.             dUdh  = zero;
  3137.             dUdAl = zero;
  3138.             dUdBe = zero;
  3139.             dUdGa = zero;

  3140.             for (int s = 0; s <= maxEccPowMeanElements; s++) {
  3141.                 //Initialize the Hansen roots
  3142.                 hansen.computeHansenObjectsInitValues(context, s);

  3143.                 // Get the current Gs coefficient
  3144.                 final T gs = GsHs[0][s];

  3145.                 // Compute Gs partial derivatives from 3.1-(9)
  3146.                 T dGsdh  = zero;
  3147.                 T dGsdk  = zero;
  3148.                 T dGsdAl = zero;
  3149.                 T dGsdBe = zero;
  3150.                 if (s > 0) {
  3151.                     // First get the G(s-1) and the H(s-1) coefficients
  3152.                     final T sxgsm1 = GsHs[0][s - 1].multiply(s);
  3153.                     final T sxhsm1 = GsHs[1][s - 1].multiply(s);
  3154.                     // Then compute derivatives
  3155.                     dGsdh  = sxgsm1.multiply(auxiliaryElements.getBeta()).subtract(sxhsm1.multiply(auxiliaryElements.getAlpha()));
  3156.                     dGsdk  = sxgsm1.multiply(auxiliaryElements.getAlpha()).add(sxhsm1.multiply(auxiliaryElements.getBeta()));
  3157.                     dGsdAl = sxgsm1.multiply(auxiliaryElements.getK()).subtract(sxhsm1.multiply(auxiliaryElements.getH()));
  3158.                     dGsdBe = sxgsm1.multiply(auxiliaryElements.getH()).add(sxhsm1.multiply(auxiliaryElements.getK()));
  3159.                 }

  3160.                 // Kronecker symbol (2 - delta(0,s))
  3161.                 final T d0s = zero.add((s == 0) ? 1 : 2);

  3162.                 for (int n = s + 2; n <= maxDegree; n++) {
  3163.                     // (n - s) must be even
  3164.                     if ((n - s) % 2 == 0) {

  3165.                         //Extract data from previous computation :
  3166.                         final T kns   = (T) hansen.getHansenObjects()[s].getValue(-n - 1, context.getX());
  3167.                         final T dkns  = (T) hansen.getHansenObjects()[s].getDerivative(-n - 1, context.getX());

  3168.                         final double vns   = Vns.get(new NSKey(n, s));
  3169.                         final T coef0 = d0s.multiply(roaPow[n]).multiply(vns).multiply(-harmonics.getUnnormalizedCnm(n, 0));
  3170.                         final T coef1 = coef0.multiply(Qns[n][s]);
  3171.                         final T coef2 = coef1.multiply(kns);
  3172.                         final T coef3 = coef2.multiply(gs);
  3173.                         // dQns/dGamma = Q(n, s + 1) from Equation 3.1-(8)
  3174.                         final T dqns  = Qns[n][s + 1];

  3175.                         // Compute U
  3176.                         U = U.add(coef3);
  3177.                         // Compute dU / da :
  3178.                         dUda  = dUda.add(coef3.multiply(n + 1));
  3179.                         // Compute dU / dEx
  3180.                         dUdk  = dUdk.add(coef1.multiply(dGsdk.multiply(kns).add(auxiliaryElements.getK().multiply(context.getXXX()).multiply(dkns).multiply(gs))));
  3181.                         // Compute dU / dEy
  3182.                         dUdh  = dUdh.add(coef1.multiply(dGsdh.multiply(kns).add(auxiliaryElements.getH().multiply(context.getXXX()).multiply(dkns).multiply(gs))));
  3183.                         // Compute dU / dAlpha
  3184.                         dUdAl = dUdAl.add(coef2.multiply(dGsdAl));
  3185.                         // Compute dU / dBeta
  3186.                         dUdBe = dUdBe.add(coef2.multiply(dGsdBe));
  3187.                         // Compute dU / dGamma
  3188.                         dUdGa = dUdGa.add(coef0.multiply(kns).multiply(dqns).multiply(gs));
  3189.                     }
  3190.                 }
  3191.             }

  3192.             // Multiply by -(μ / a)
  3193.             U = U.multiply(context.getMuoa().negate());

  3194.             dUda  = dUda.multiply(context.getMuoa().divide(auxiliaryElements.getSma()));
  3195.             dUdk  = dUdk.multiply(context.getMuoa()).negate();
  3196.             dUdh  = dUdh.multiply(context.getMuoa()).negate();
  3197.             dUdAl = dUdAl.multiply(context.getMuoa()).negate();
  3198.             dUdBe = dUdBe.multiply(context.getMuoa()).negate();
  3199.             dUdGa = dUdGa.multiply(context.getMuoa()).negate();

  3200.         }

  3201.         /** Return value of U.
  3202.          * @return U
  3203.          */
  3204.         public T getU() {
  3205.             return U;
  3206.         }

  3207.          /** Return value of dU / da.
  3208.           * @return dUda
  3209.           */
  3210.         public T getdUda() {
  3211.             return dUda;
  3212.         }

  3213.          /** Return value of dU / dk.
  3214.           * @return dUdk
  3215.           */
  3216.         public T getdUdk() {
  3217.             return dUdk;
  3218.         }

  3219.          /** Return value of dU / dh.
  3220.           * @return dUdh
  3221.           */
  3222.         public T getdUdh() {
  3223.             return dUdh;
  3224.         }

  3225.          /** Return value of dU / dAlpha.
  3226.           * @return dUdAl
  3227.           */
  3228.         public T getdUdAl() {
  3229.             return dUdAl;
  3230.         }

  3231.          /** Return value of dU / dBeta.
  3232.           * @return dUdBe
  3233.           */
  3234.         public T getdUdBe() {
  3235.             return dUdBe;
  3236.         }

  3237.          /** Return value of dU / dGamma.
  3238.           * @return dUdGa
  3239.           */
  3240.         public T getdUdGa() {
  3241.             return dUdGa;
  3242.         }

  3243.     }

  3244.     /** Computes init values of the Hansen Objects. */
  3245.     private class HansenObjects {

  3246.         /** An array that contains the objects needed to build the Hansen coefficients. <br/>
  3247.          * The index is s*/
  3248.         private HansenZonalLinear[] hansenObjects;

  3249.         /** Simple constructor. */
  3250.         HansenObjects() {
  3251.             this.hansenObjects = new HansenZonalLinear[maxEccPow + 1];
  3252.             for (int s = 0; s <= maxEccPow; s++) {
  3253.                 this.hansenObjects[s] = new HansenZonalLinear(maxDegree, s);
  3254.             }
  3255.         }

  3256.         /** Compute init values for hansen objects.
  3257.          * @param context container for attributes
  3258.          * @param element element of the array to compute the init values
  3259.          */
  3260.         public void computeHansenObjectsInitValues(final DSSTZonalContext context, final int element) {
  3261.             hansenObjects[element].computeInitValues(context.getX());
  3262.         }

  3263.         /** Get the Hansen Objects.
  3264.          * @return hansenObjects
  3265.          */
  3266.         public HansenZonalLinear[] getHansenObjects() {
  3267.             return hansenObjects;
  3268.         }

  3269.     }

  3270.     /** Computes init values of the Hansen Objects.
  3271.      * @param <T> type of the elements
  3272.      */
  3273.     private class FieldHansenObjects<T extends RealFieldElement<T>> {

  3274.         /** An array that contains the objects needed to build the Hansen coefficients. <br/>
  3275.          * The index is s*/
  3276.         private FieldHansenZonalLinear<T>[] hansenObjects;

  3277.         /** Simple constructor.
  3278.          * @param field field used by default
  3279.          */
  3280.         @SuppressWarnings("unchecked")
  3281.         FieldHansenObjects(final Field<T> field) {
  3282.             this.hansenObjects = (FieldHansenZonalLinear<T>[]) Array.newInstance(FieldHansenZonalLinear.class, maxEccPow + 1);
  3283.             for (int s = 0; s <= maxEccPow; s++) {
  3284.                 this.hansenObjects[s] = new FieldHansenZonalLinear<>(maxDegree, s, field);
  3285.             }
  3286.         }

  3287.         /** Compute init values for hansen objects.
  3288.          * @param context container for attributes
  3289.          * @param element element of the array to compute the init values
  3290.          */
  3291.         public void computeHansenObjectsInitValues(final FieldDSSTZonalContext<T> context, final int element) {
  3292.             hansenObjects[element].computeInitValues(context.getX());
  3293.         }

  3294.         /** Get the Hansen Objects.
  3295.          * @return hansenObjects
  3296.          */
  3297.         public FieldHansenZonalLinear<T>[] getHansenObjects() {
  3298.             return hansenObjects;
  3299.         }

  3300.     }

  3301. }