FieldKeplerianOrbit.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.orbits;


  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. import java.util.stream.Stream;

  21. import org.hipparchus.CalculusFieldElement;
  22. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
  23. import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.FieldSinCos;
  28. import org.hipparchus.util.MathArrays;
  29. import org.hipparchus.util.MathUtils;
  30. import org.hipparchus.util.Precision;
  31. import org.orekit.errors.OrekitException;
  32. import org.orekit.errors.OrekitIllegalArgumentException;
  33. import org.orekit.errors.OrekitInternalError;
  34. import org.orekit.errors.OrekitMessages;
  35. import org.orekit.frames.Frame;
  36. import org.orekit.time.FieldAbsoluteDate;
  37. import org.orekit.utils.FieldPVCoordinates;
  38. import org.orekit.utils.TimeStampedFieldPVCoordinates;


  39. /**
  40.  * This class handles traditional Keplerian orbital parameters.

  41.  * <p>
  42.  * The parameters used internally are the classical Keplerian elements:
  43.  *   <pre>
  44.  *     a
  45.  *     e
  46.  *     i
  47.  *     ω
  48.  *     Ω
  49.  *     v
  50.  *   </pre>
  51.  * where ω stands for the Perigee Argument, Ω stands for the
  52.  * Right Ascension of the Ascending Node and v stands for the true anomaly.
  53.  * <p>
  54.  * This class supports hyperbolic orbits, using the convention that semi major
  55.  * axis is negative for such orbits (and of course eccentricity is greater than 1).
  56.  * </p>
  57.  * <p>
  58.  * When orbit is either equatorial or circular, some Keplerian elements
  59.  * (more precisely ω and Ω) become ambiguous so this class should not
  60.  * be used for such orbits. For this reason, {@link EquinoctialOrbit equinoctial
  61.  * orbits} is the recommended way to represent orbits.
  62.  * </p>

  63.  * <p>
  64.  * The instance <code>KeplerianOrbit</code> is guaranteed to be immutable.
  65.  * </p>
  66.  * @see     Orbit
  67.  * @see    CircularOrbit
  68.  * @see    CartesianOrbit
  69.  * @see    EquinoctialOrbit
  70.  * @author Luc Maisonobe
  71.  * @author Guylaine Prat
  72.  * @author Fabien Maussion
  73.  * @author V&eacute;ronique Pommier-Maurussane
  74.  * @author Andrea Antolino
  75.  * @since 9.0
  76.  */
  77. public class FieldKeplerianOrbit<T extends CalculusFieldElement<T>> extends FieldOrbit<T> {

  78.     /** Name of the eccentricity parameter. */
  79.     private static final String ECCENTRICITY = "eccentricity";

  80.     /** First coefficient to compute Kepler equation solver starter. */
  81.     private static final double A;

  82.     /** Second coefficient to compute Kepler equation solver starter. */
  83.     private static final double B;

  84.     static {
  85.         final double k1 = 3 * FastMath.PI + 2;
  86.         final double k2 = FastMath.PI - 1;
  87.         final double k3 = 6 * FastMath.PI - 1;
  88.         A  = 3 * k2 * k2 / k1;
  89.         B  = k3 * k3 / (6 * k1);
  90.     }

  91.     /** Semi-major axis (m). */
  92.     private final T a;

  93.     /** Eccentricity. */
  94.     private final T e;

  95.     /** Inclination (rad). */
  96.     private final T i;

  97.     /** Perigee Argument (rad). */
  98.     private final T pa;

  99.     /** Right Ascension of Ascending Node (rad). */
  100.     private final T raan;

  101.     /** True anomaly (rad). */
  102.     private final T v;

  103.     /** Semi-major axis derivative (m/s). */
  104.     private final T aDot;

  105.     /** Eccentricity derivative. */
  106.     private final T eDot;

  107.     /** Inclination derivative (rad/s). */
  108.     private final T iDot;

  109.     /** Perigee Argument derivative (rad/s). */
  110.     private final T paDot;

  111.     /** Right Ascension of Ascending Node derivative (rad/s). */
  112.     private final T raanDot;

  113.     /** True anomaly derivative (rad/s). */
  114.     private final T vDot;

  115.     /** Partial Cartesian coordinates (position and velocity are valid, acceleration may be missing). */
  116.     private FieldPVCoordinates<T> partialPV;

  117.     /** Identity element. */
  118.     private final T one;

  119.     /** Zero element. */
  120.     private final T zero;

  121.     /** Third Canonical Vector. */
  122.     private final FieldVector3D<T> PLUS_K;

  123.     /** Creates a new instance.
  124.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  125.      * @param e eccentricity (positive or equal to 0)
  126.      * @param i inclination (rad)
  127.      * @param pa perigee argument (ω, rad)
  128.      * @param raan right ascension of ascending node (Ω, rad)
  129.      * @param anomaly mean, eccentric or true anomaly (rad)
  130.      * @param type type of anomaly
  131.      * @param frame the frame in which the parameters are defined
  132.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  133.      * @param date date of the orbital parameters
  134.      * @param mu central attraction coefficient (m³/s²)
  135.      * @exception IllegalArgumentException if frame is not a {@link
  136.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  137.      * or v is out of range for hyperbolic orbits
  138.      */
  139.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  140.                                final T pa, final T raan,
  141.                                final T anomaly, final PositionAngle type,
  142.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  143.         throws IllegalArgumentException {
  144.         this(a, e, i, pa, raan, anomaly,
  145.              null, null, null, null, null, null,
  146.              type, frame, date, mu);
  147.     }

  148.     /** Creates a new instance.
  149.      * @param a  semi-major axis (m), negative for hyperbolic orbits
  150.      * @param e eccentricity (positive or equal to 0)
  151.      * @param i inclination (rad)
  152.      * @param pa perigee argument (ω, rad)
  153.      * @param raan right ascension of ascending node (Ω, rad)
  154.      * @param anomaly mean, eccentric or true anomaly (rad)
  155.      * @param aDot  semi-major axis derivative, null if unknown (m/s)
  156.      * @param eDot eccentricity derivative, null if unknown
  157.      * @param iDot inclination derivative, null if unknown (rad/s)
  158.      * @param paDot perigee argument derivative, null if unknown (rad/s)
  159.      * @param raanDot right ascension of ascending node derivative, null if unknown (rad/s)
  160.      * @param anomalyDot mean, eccentric or true anomaly derivative, null if unknown (rad/s)
  161.      * @param type type of anomaly
  162.      * @param frame the frame in which the parameters are defined
  163.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  164.      * @param date date of the orbital parameters
  165.      * @param mu central attraction coefficient (m³/s²)
  166.      * @exception IllegalArgumentException if frame is not a {@link
  167.      * Frame#isPseudoInertial pseudo-inertial frame} or a and e don't match for hyperbolic orbits,
  168.      * or v is out of range for hyperbolic orbits
  169.      */
  170.     public FieldKeplerianOrbit(final T a, final T e, final T i,
  171.                                final T pa, final T raan, final T anomaly,
  172.                                final T aDot, final T eDot, final T iDot,
  173.                                final T paDot, final T raanDot, final T anomalyDot,
  174.                                final PositionAngle type,
  175.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  176.         throws IllegalArgumentException {
  177.         super(frame, date, mu);
  178.         if (a.multiply(e.negate().add(1)).getReal() < 0) {
  179.             throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_A_E_MISMATCH_WITH_CONIC_TYPE, a.getReal(), e.getReal());
  180.         }

  181.         // Checking eccentricity range
  182.         checkParameterRangeInclusive(ECCENTRICITY, e.getReal(), 0.0, Double.POSITIVE_INFINITY);

  183.         this.a       =    a;
  184.         this.aDot    =    aDot;
  185.         this.e       =    e;
  186.         this.eDot    =    eDot;
  187.         this.i       =    i;
  188.         this.iDot    =    iDot;
  189.         this.pa      =    pa;
  190.         this.paDot   =    paDot;
  191.         this.raan    =    raan;
  192.         this.raanDot =    raanDot;

  193.         /** Identity element. */
  194.         this.one = a.getField().getOne();

  195.         /** Zero element. */
  196.         this.zero = a.getField().getZero();

  197.         /**Third canonical vector. */
  198.         this.PLUS_K = FieldVector3D.getPlusK(a.getField());

  199.         if (hasDerivatives()) {
  200.             final FieldUnivariateDerivative1<T> eUD        = new FieldUnivariateDerivative1<>(e, eDot);
  201.             final FieldUnivariateDerivative1<T> anomalyUD  = new FieldUnivariateDerivative1<>(anomaly,  anomalyDot);
  202.             final FieldUnivariateDerivative1<T> vUD;
  203.             switch (type) {
  204.                 case MEAN :
  205.                     vUD = (a.getReal() < 0) ?
  206.                           hyperbolicEccentricToTrue(meanToHyperbolicEccentric(anomalyUD, eUD), eUD) :
  207.                           ellipticEccentricToTrue(meanToEllipticEccentric(anomalyUD, eUD), eUD);
  208.                     break;
  209.                 case ECCENTRIC :
  210.                     vUD = (a.getReal() < 0) ?
  211.                           hyperbolicEccentricToTrue(anomalyUD, eUD) :
  212.                           ellipticEccentricToTrue(anomalyUD, eUD);
  213.                     break;
  214.                 case TRUE :
  215.                     vUD = anomalyUD;
  216.                     break;
  217.                 default : // this should never happen
  218.                     throw new OrekitInternalError(null);
  219.             }
  220.             this.v    = vUD.getValue();
  221.             this.vDot = vUD.getDerivative(1);
  222.         } else {
  223.             switch (type) {
  224.                 case MEAN :

  225.                     this.v = (a.getReal() < 0) ?
  226.                              hyperbolicEccentricToTrue(meanToHyperbolicEccentric(anomaly, e), e) :
  227.                              ellipticEccentricToTrue(meanToEllipticEccentric(anomaly, e), e);

  228.                     break;
  229.                 case ECCENTRIC :
  230.                     this.v = (a.getReal() < 0) ?
  231.                              hyperbolicEccentricToTrue(anomaly, e) :
  232.                              ellipticEccentricToTrue(anomaly, e);

  233.                     break;
  234.                 case TRUE :
  235.                     this.v = anomaly;
  236.                     break;
  237.                 default : // this should never happen
  238.                     throw new OrekitInternalError(null);
  239.             }
  240.             this.vDot = null;
  241.         }

  242.         // check true anomaly range
  243.         if (e.multiply(v.cos()).add(1).getReal() <= 0) {
  244.             final double vMax = e.reciprocal().negate().acos().getReal();
  245.             throw new OrekitIllegalArgumentException(OrekitMessages.ORBIT_ANOMALY_OUT_OF_HYPERBOLIC_RANGE,
  246.                                                      v.getReal(), e.getReal(), -vMax, vMax);
  247.         }

  248.         this.partialPV = null;

  249.     }

  250.     /** Constructor from Cartesian parameters.
  251.      *
  252.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  253.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  254.      * use {@code mu} and the position to compute the acceleration, including
  255.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  256.      *
  257.      * @param pvCoordinates the PVCoordinates of the satellite
  258.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  259.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  260.      * @param mu central attraction coefficient (m³/s²)
  261.      * @exception IllegalArgumentException if frame is not a {@link
  262.      * Frame#isPseudoInertial pseudo-inertial frame}
  263.      */
  264.     public FieldKeplerianOrbit(final TimeStampedFieldPVCoordinates<T> pvCoordinates,
  265.                                final Frame frame, final T mu)
  266.         throws IllegalArgumentException {
  267.         this(pvCoordinates, frame, mu, hasNonKeplerianAcceleration(pvCoordinates, mu));
  268.     }

  269.     /** Constructor from Cartesian parameters.
  270.      *
  271.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  272.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  273.      * use {@code mu} and the position to compute the acceleration, including
  274.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  275.      *
  276.      * @param pvCoordinates the PVCoordinates of the satellite
  277.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  278.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  279.      * @param mu central attraction coefficient (m³/s²)
  280.      * @param reliableAcceleration if true, the acceleration is considered to be reliable
  281.      * @exception IllegalArgumentException if frame is not a {@link
  282.      * Frame#isPseudoInertial pseudo-inertial frame}
  283.      */
  284.     private FieldKeplerianOrbit(final TimeStampedFieldPVCoordinates<T> pvCoordinates,
  285.                                 final Frame frame, final T mu,
  286.                                 final boolean reliableAcceleration)
  287.         throws IllegalArgumentException {

  288.         super(pvCoordinates, frame, mu);

  289.         // identity element
  290.         this.one = pvCoordinates.getPosition().getX().getField().getOne();

  291.         // zero element
  292.         this.zero = one.getField().getZero();

  293.         // third canonical vector
  294.         this.PLUS_K = FieldVector3D.getPlusK(one.getField());

  295.         // compute inclination
  296.         final FieldVector3D<T> momentum = pvCoordinates.getMomentum();
  297.         final T m2 = momentum.getNormSq();

  298.         i = FieldVector3D.angle(momentum, PLUS_K);
  299.         // compute right ascension of ascending node
  300.         raan = FieldVector3D.crossProduct(PLUS_K, momentum).getAlpha();
  301.         // preliminary computations for parameters depending on orbit shape (elliptic or hyperbolic)
  302.         final FieldVector3D<T> pvP     = pvCoordinates.getPosition();
  303.         final FieldVector3D<T> pvV     = pvCoordinates.getVelocity();
  304.         final FieldVector3D<T> pvA     = pvCoordinates.getAcceleration();

  305.         final T   r2      = pvP.getNormSq();
  306.         final T   r       = r2.sqrt();
  307.         final T   V2      = pvV.getNormSq();
  308.         final T   rV2OnMu = r.multiply(V2).divide(mu);

  309.         // compute semi-major axis (will be negative for hyperbolic orbits)
  310.         a = r.divide(rV2OnMu.negate().add(2.0));
  311.         final T muA = a.multiply(mu);

  312.         // compute true anomaly
  313.         if (a.getReal() > 0) {
  314.             // elliptic or circular orbit
  315.             final T eSE = FieldVector3D.dotProduct(pvP, pvV).divide(muA.sqrt());
  316.             final T eCE = rV2OnMu.subtract(1);
  317.             e = (eSE.multiply(eSE).add(eCE.multiply(eCE))).sqrt();
  318.             v = ellipticEccentricToTrue(eSE.atan2(eCE), e);
  319.         } else {
  320.             // hyperbolic orbit
  321.             final T eSH = FieldVector3D.dotProduct(pvP, pvV).divide(muA.negate().sqrt());
  322.             final T eCH = rV2OnMu.subtract(1);
  323.             e = (m2.negate().divide(muA).add(1)).sqrt();
  324.             v = hyperbolicEccentricToTrue((eCH.add(eSH)).divide(eCH.subtract(eSH)).log().divide(2), e);
  325.         }

  326.         // Checking eccentricity range
  327.         checkParameterRangeInclusive(ECCENTRICITY, e.getReal(), 0.0, Double.POSITIVE_INFINITY);

  328.         // compute perigee argument
  329.         final FieldVector3D<T> node = new FieldVector3D<>(raan, zero);
  330.         final T px = FieldVector3D.dotProduct(pvP, node);
  331.         final T py = FieldVector3D.dotProduct(pvP, FieldVector3D.crossProduct(momentum, node)).divide(m2.sqrt());
  332.         pa = py.atan2(px).subtract(v);

  333.         partialPV = pvCoordinates;

  334.         if (reliableAcceleration) {
  335.             // we have a relevant acceleration, we can compute derivatives

  336.             final T[][] jacobian = MathArrays.buildArray(a.getField(), 6, 6);
  337.             getJacobianWrtCartesian(PositionAngle.MEAN, jacobian);

  338.             final FieldVector3D<T> keplerianAcceleration    = new FieldVector3D<>(r.multiply(r2).reciprocal().multiply(mu.negate()), pvP);
  339.             final FieldVector3D<T> nonKeplerianAcceleration = pvA.subtract(keplerianAcceleration);
  340.             final T   aX                       = nonKeplerianAcceleration.getX();
  341.             final T   aY                       = nonKeplerianAcceleration.getY();
  342.             final T   aZ                       = nonKeplerianAcceleration.getZ();
  343.             aDot    = jacobian[0][3].multiply(aX).add(jacobian[0][4].multiply(aY)).add(jacobian[0][5].multiply(aZ));
  344.             eDot    = jacobian[1][3].multiply(aX).add(jacobian[1][4].multiply(aY)).add(jacobian[1][5].multiply(aZ));
  345.             iDot    = jacobian[2][3].multiply(aX).add(jacobian[2][4].multiply(aY)).add(jacobian[2][5].multiply(aZ));
  346.             paDot   = jacobian[3][3].multiply(aX).add(jacobian[3][4].multiply(aY)).add(jacobian[3][5].multiply(aZ));
  347.             raanDot = jacobian[4][3].multiply(aX).add(jacobian[4][4].multiply(aY)).add(jacobian[4][5].multiply(aZ));

  348.             // in order to compute true anomaly derivative, we must compute
  349.             // mean anomaly derivative including Keplerian motion and convert to true anomaly
  350.             final T MDot = getKeplerianMeanMotion().
  351.                            add(jacobian[5][3].multiply(aX)).add(jacobian[5][4].multiply(aY)).add(jacobian[5][5].multiply(aZ));
  352.             final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  353.             final FieldUnivariateDerivative1<T> MUD = new FieldUnivariateDerivative1<>(getMeanAnomaly(), MDot);
  354.             final FieldUnivariateDerivative1<T> vUD = (a.getReal() < 0) ?
  355.                                             FieldKeplerianOrbit.hyperbolicEccentricToTrue(FieldKeplerianOrbit.meanToHyperbolicEccentric(MUD, eUD), eUD) :
  356.                                             FieldKeplerianOrbit.ellipticEccentricToTrue(FieldKeplerianOrbit.meanToEllipticEccentric(MUD, eUD), eUD);
  357.             vDot = vUD.getDerivative(1);

  358.         } else {
  359.             // acceleration is either almost zero or NaN,
  360.             // we assume acceleration was not known
  361.             // we don't set up derivatives
  362.             aDot    = null;
  363.             eDot    = null;
  364.             iDot    = null;
  365.             paDot   = null;
  366.             raanDot = null;
  367.             vDot    = null;
  368.         }

  369.     }

  370.     /** Constructor from Cartesian parameters.
  371.      *
  372.      * <p> The acceleration provided in {@code FieldPVCoordinates} is accessible using
  373.      * {@link #getPVCoordinates()} and {@link #getPVCoordinates(Frame)}. All other methods
  374.      * use {@code mu} and the position to compute the acceleration, including
  375.      * {@link #shiftedBy(CalculusFieldElement)} and {@link #getPVCoordinates(FieldAbsoluteDate, Frame)}.
  376.      *
  377.      * @param FieldPVCoordinates the PVCoordinates of the satellite
  378.      * @param frame the frame in which are defined the {@link FieldPVCoordinates}
  379.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  380.      * @param date date of the orbital parameters
  381.      * @param mu central attraction coefficient (m³/s²)
  382.      * @exception IllegalArgumentException if frame is not a {@link
  383.      * Frame#isPseudoInertial pseudo-inertial frame}
  384.      */
  385.     public FieldKeplerianOrbit(final FieldPVCoordinates<T> FieldPVCoordinates,
  386.                                final Frame frame, final FieldAbsoluteDate<T> date, final T mu)
  387.         throws IllegalArgumentException {
  388.         this(new TimeStampedFieldPVCoordinates<>(date, FieldPVCoordinates), frame, mu);
  389.     }

  390.     /** Constructor from any kind of orbital parameters.
  391.      * @param op orbital parameters to copy
  392.      */
  393.     public FieldKeplerianOrbit(final FieldOrbit<T> op) {
  394.         this(op.getPVCoordinates(), op.getFrame(), op.getMu(), op.hasDerivatives());
  395.     }

  396.     /** {@inheritDoc} */
  397.     public OrbitType getType() {
  398.         return OrbitType.KEPLERIAN;
  399.     }

  400.     /** {@inheritDoc} */
  401.     public T getA() {
  402.         return a;
  403.     }

  404.     /** {@inheritDoc} */
  405.     public T getADot() {
  406.         return aDot;
  407.     }

  408.     /** {@inheritDoc} */
  409.     public T getE() {
  410.         return e;
  411.     }

  412.     /** {@inheritDoc} */
  413.     public T getEDot() {
  414.         return eDot;
  415.     }

  416.     /** {@inheritDoc} */
  417.     public T getI() {
  418.         return i;
  419.     }

  420.     /** {@inheritDoc} */
  421.     public T getIDot() {
  422.         return iDot;
  423.     }

  424.     /** Get the perigee argument.
  425.      * @return perigee argument (rad)
  426.      */
  427.     public T getPerigeeArgument() {
  428.         return pa;
  429.     }

  430.     /** Get the perigee argument derivative.
  431.      * <p>
  432.      * If the orbit was created without derivatives, the value returned is null.
  433.      * </p>
  434.      * @return perigee argument derivative (rad/s)
  435.      */
  436.     public T getPerigeeArgumentDot() {
  437.         return paDot;
  438.     }

  439.     /** Get the right ascension of the ascending node.
  440.      * @return right ascension of the ascending node (rad)
  441.      */
  442.     public T getRightAscensionOfAscendingNode() {
  443.         return raan;
  444.     }

  445.     /** Get the right ascension of the ascending node derivative.
  446.      * <p>
  447.      * If the orbit was created without derivatives, the value returned is null.
  448.      * </p>
  449.      * @return right ascension of the ascending node derivative (rad/s)
  450.      */
  451.     public T getRightAscensionOfAscendingNodeDot() {
  452.         return raanDot;
  453.     }

  454.     /** Get the true anomaly.
  455.      * @return true anomaly (rad)
  456.      */
  457.     public T getTrueAnomaly() {
  458.         return v;
  459.     }

  460.     /** Get the true anomaly derivative.
  461.      * <p>
  462.      * If the orbit was created without derivatives, the value returned is null.
  463.      * </p>
  464.      * @return true anomaly derivative (rad/s)
  465.      */
  466.     public T getTrueAnomalyDot() {
  467.         return vDot;
  468.     }

  469.     /** Get the eccentric anomaly.
  470.      * @return eccentric anomaly (rad)
  471.      */
  472.     public T getEccentricAnomaly() {
  473.         return (a.getReal() < 0) ? trueToHyperbolicEccentric(v, e) : trueToEllipticEccentric(v, e);
  474.     }

  475.     /** Get the eccentric anomaly derivative.
  476.      * <p>
  477.      * If the orbit was created without derivatives, the value returned is null.
  478.      * </p>
  479.      * @return eccentric anomaly derivative (rad/s)
  480.      */
  481.     public T getEccentricAnomalyDot() {

  482.         if (!hasDerivatives()) {
  483.             return null;
  484.         }

  485.         final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  486.         final FieldUnivariateDerivative1<T> vUD = new FieldUnivariateDerivative1<>(v, vDot);
  487.         final FieldUnivariateDerivative1<T> EUD = (a.getReal() < 0) ?
  488.                                                 trueToHyperbolicEccentric(vUD, eUD) :
  489.                                                 trueToEllipticEccentric(vUD, eUD);
  490.         return EUD.getDerivative(1);

  491.     }

  492.     /** Get the mean anomaly.
  493.      * @return mean anomaly (rad)
  494.      */
  495.     public T getMeanAnomaly() {
  496.         return (a.getReal() < 0) ?
  497.                hyperbolicEccentricToMean(trueToHyperbolicEccentric(v, e), e) :
  498.                ellipticEccentricToMean(trueToEllipticEccentric(v, e), e);
  499.     }

  500.     /** Get the mean anomaly derivative.
  501.      * <p>
  502.      * If the orbit was created without derivatives, the value returned is null.
  503.      * </p>
  504.      * @return mean anomaly derivative (rad/s)
  505.      */
  506.     public T getMeanAnomalyDot() {

  507.         if (!hasDerivatives()) {
  508.             return null;
  509.         }

  510.         final FieldUnivariateDerivative1<T> eUD = new FieldUnivariateDerivative1<>(e, eDot);
  511.         final FieldUnivariateDerivative1<T> vUD = new FieldUnivariateDerivative1<>(v, vDot);
  512.         final FieldUnivariateDerivative1<T> MUD = (a.getReal() < 0) ?
  513.                                                 hyperbolicEccentricToMean(trueToHyperbolicEccentric(vUD, eUD), eUD) :
  514.                                                 ellipticEccentricToMean(trueToEllipticEccentric(vUD, eUD), eUD);
  515.         return MUD.getDerivative(1);

  516.     }

  517.     /** Get the anomaly.
  518.      * @param type type of the angle
  519.      * @return anomaly (rad)
  520.      */
  521.     public T getAnomaly(final PositionAngle type) {
  522.         return (type == PositionAngle.MEAN) ? getMeanAnomaly() :
  523.                                               ((type == PositionAngle.ECCENTRIC) ? getEccentricAnomaly() :
  524.                                                                                    getTrueAnomaly());
  525.     }

  526.     /** Get the anomaly derivative.
  527.      * <p>
  528.      * If the orbit was created without derivatives, the value returned is null.
  529.      * </p>
  530.      * @param type type of the angle
  531.      * @return anomaly derivative (rad/s)
  532.      */
  533.     public T getAnomalyDot(final PositionAngle type) {
  534.         return (type == PositionAngle.MEAN) ? getMeanAnomalyDot() :
  535.                                               ((type == PositionAngle.ECCENTRIC) ? getEccentricAnomalyDot() :
  536.                                                                                    getTrueAnomalyDot());
  537.     }

  538.     /** {@inheritDoc} */
  539.     @Override
  540.     public boolean hasDerivatives() {
  541.         return aDot != null;
  542.     }

  543.     /** Computes the true anomaly from the elliptic eccentric anomaly.
  544.      * @param E eccentric anomaly (rad)
  545.      * @param e eccentricity
  546.      * @param <T> type of the field elements
  547.      * @return v the true anomaly
  548.      */
  549.     public static <T extends CalculusFieldElement<T>> T ellipticEccentricToTrue(final T E, final T e) {
  550.         final T beta = e.divide(e.multiply(e).negate().add(1).sqrt().add(1));
  551.         final FieldSinCos<T> scE = FastMath.sinCos(E);
  552.         return E.add(beta.multiply(scE.sin()).divide(beta.multiply(scE.cos()).subtract(1).negate()).atan().multiply(2));
  553.     }

  554.     /** Computes the elliptic eccentric anomaly from the true anomaly.
  555.      * @param v true anomaly (rad)
  556.      * @param e eccentricity
  557.      * @param <T> type of the field elements
  558.      * @return E the elliptic eccentric anomaly
  559.      */
  560.     public static <T extends CalculusFieldElement<T>> T trueToEllipticEccentric(final T v, final T e) {
  561.         final T beta = e.divide(e.multiply(e).negate().add(1).sqrt().add(1));
  562.         final FieldSinCos<T> scv = FastMath.sinCos(v);
  563.         return v.subtract((beta.multiply(scv.sin()).divide(beta.multiply(scv.cos()).add(1))).atan().multiply(2));
  564.     }

  565.     /** Computes the true anomaly from the hyperbolic eccentric anomaly.
  566.      * @param H hyperbolic eccentric anomaly (rad)
  567.      * @param e eccentricity
  568.      * @param <T> type of the field elements
  569.      * @return v the true anomaly
  570.      */
  571.     public static <T extends CalculusFieldElement<T>> T hyperbolicEccentricToTrue(final T H, final T e) {
  572.         final T s    = e.add(1).divide(e.subtract(1)).sqrt();
  573.         final T tanH = H.multiply(0.5).tanh();
  574.         return s.multiply(tanH).atan().multiply(2);
  575.     }

  576.     /** Computes the hyperbolic eccentric anomaly from the true anomaly.
  577.      * @param v true anomaly (rad)
  578.      * @param e eccentricity
  579.      * @param <T> type of the field elements
  580.      * @return H the hyperbolic eccentric anomaly
  581.      */
  582.     public static <T extends CalculusFieldElement<T>> T trueToHyperbolicEccentric(final T v, final T e) {
  583.         final FieldSinCos<T> scv = FastMath.sinCos(v);
  584.         final T sinhH = e.multiply(e).subtract(1).sqrt().multiply(scv.sin()).divide(e.multiply(scv.cos()).add(1));
  585.         return sinhH.asinh();
  586.     }

  587.     /** Computes the mean anomaly from the hyperbolic eccentric anomaly.
  588.      * @param H hyperbolic eccentric anomaly (rad)
  589.      * @param e eccentricity
  590.      * @param <T> type of the field elements
  591.      * @return M the mean anomaly
  592.      */
  593.     public static <T extends CalculusFieldElement<T>> T hyperbolicEccentricToMean(final T H, final T e) {
  594.         return e.multiply(H.sinh()).subtract(H);
  595.     }

  596.     /** Computes the elliptic eccentric anomaly from the mean anomaly.
  597.      * <p>
  598.      * The algorithm used here for solving Kepler equation has been published
  599.      * in: "Procedures for  solving Kepler's Equation", A. W. Odell and
  600.      * R. H. Gooding, Celestial Mechanics 38 (1986) 307-334
  601.      * </p>
  602.      * @param M mean anomaly (rad)
  603.      * @param e eccentricity
  604.      * @param <T> type of the field elements
  605.      * @return E the eccentric anomaly
  606.      */
  607.     public static <T extends CalculusFieldElement<T>> T meanToEllipticEccentric(final T M, final T e) {
  608.         // reduce M to [-PI PI) interval
  609.         final T reducedM = MathUtils.normalizeAngle(M, M.getField().getZero());

  610.         // compute start value according to A. W. Odell and R. H. Gooding S12 starter
  611.         T E;
  612.         if (reducedM.abs().getReal() < 1.0 / 6.0) {
  613.             if (FastMath.abs(reducedM.getReal()) < Precision.SAFE_MIN) {
  614.                 // this is an Orekit change to the S12 starter, mainly used when T is some kind of derivative structure.
  615.                 // If reducedM is 0.0, the derivative of cbrt is infinite which induces NaN appearing later in
  616.                 // the computation. As in this case E and M are almost equal, we initialize E with reducedM
  617.                 E = reducedM;
  618.             } else {
  619.                 // this is the standard S12 starter
  620.                 E = reducedM.add(e.multiply( (reducedM.multiply(6).cbrt()).subtract(reducedM)));
  621.             }
  622.         } else {
  623.             final T pi = e.getPi();
  624.             if (reducedM.getReal() < 0) {
  625.                 final T w = reducedM.add(pi);
  626.                 E = reducedM.add(e.multiply(w.multiply(A).divide(w.negate().add(B)).subtract(pi).subtract(reducedM)));
  627.             } else {
  628.                 final T w = reducedM.negate().add(pi);
  629.                 E = reducedM.add(e.multiply(w.multiply(A).divide(w.negate().add(B)).negate().subtract(reducedM).add(pi)));
  630.             }
  631.         }

  632.         final T e1 = e.negate().add(1);
  633.         final boolean noCancellationRisk = (e1.getReal() + E.getReal() * E.getReal() / 6) >= 0.1;

  634.         // perform two iterations, each consisting of one Halley step and one Newton-Raphson step
  635.         for (int j = 0; j < 2; ++j) {

  636.             final T f;
  637.             T fd;
  638.             final FieldSinCos<T> scE = FastMath.sinCos(E);
  639.             final T fdd  = e.multiply(scE.sin());
  640.             final T fddd = e.multiply(scE.cos());

  641.             if (noCancellationRisk) {

  642.                 f  = (E.subtract(fdd)).subtract(reducedM);
  643.                 fd = fddd.negate().add(1);
  644.             } else {


  645.                 f  = eMeSinE(E, e).subtract(reducedM);
  646.                 final T s = E.multiply(0.5).sin();
  647.                 fd = e1.add(e.multiply(s).multiply(s).multiply(2));
  648.             }
  649.             final T dee = f.multiply(fd).divide(f.multiply(fdd).multiply(0.5).subtract(fd.multiply(fd)));

  650.             // update eccentric anomaly, using expressions that limit underflow problems
  651.             final T w = fd.add(dee.multiply(fdd.add(dee.multiply(fddd.divide(3)))).multiply(0.5));
  652.             fd = fd.add(dee.multiply(fdd.add(dee.multiply(fddd).multiply(0.5))));
  653.             E = E.subtract(f.subtract(dee.multiply(fd.subtract(w))).divide(fd));

  654.         }

  655.         // expand the result back to original range
  656.         E = E.add(M).subtract(reducedM);
  657.         return E;
  658.     }

  659.     /** Accurate computation of E - e sin(E).
  660.      * <p>
  661.      * This method is used when E is close to 0 and e close to 1,
  662.      * i.e. near the perigee of almost parabolic orbits
  663.      * </p>
  664.      * @param E eccentric anomaly
  665.      * @param e eccentricity
  666.      * @param <T> Type of the field elements
  667.      * @return E - e sin(E)
  668.      */
  669.     private static <T extends CalculusFieldElement<T>> T eMeSinE(final T E, final T e) {

  670.         T x = (e.negate().add(1)).multiply(E.sin());
  671.         final T mE2 = E.negate().multiply(E);
  672.         T term = E;
  673.         double d    = 0;
  674.         // the inequality test below IS intentional and should NOT be replaced by a check with a small tolerance
  675.         for (T x0 = E.getField().getZero().add(Double.NaN); !Double.valueOf(x.getReal()).equals(Double.valueOf(x0.getReal()));) {
  676.             d += 2;
  677.             term = term.multiply(mE2.divide(d * (d + 1)));
  678.             x0 = x;
  679.             x = x.subtract(term);
  680.         }
  681.         return x;
  682.     }

  683.     /** Computes the hyperbolic eccentric anomaly from the mean anomaly.
  684.      * <p>
  685.      * The algorithm used here for solving hyperbolic Kepler equation is
  686.      * Danby's iterative method (3rd order) with Vallado's initial guess.
  687.      * </p>
  688.      * @param M mean anomaly (rad)
  689.      * @param e eccentricity
  690.      * @param <T> Type of the field elements
  691.      * @return H the hyperbolic eccentric anomaly
  692.      */
  693.     public static <T extends CalculusFieldElement<T>> T meanToHyperbolicEccentric(final T M, final T e) {

  694.         // Resolution of hyperbolic Kepler equation for Keplerian parameters

  695.         // Field value of pi
  696.         final T pi = e.getPi();

  697.         // Initial guess
  698.         T H;
  699.         if (e.getReal() < 1.6) {
  700.             if (-pi.getReal() < M.getReal() && M.getReal() < 0. || M.getReal() > pi.getReal()) {
  701.                 H = M.subtract(e);
  702.             } else {
  703.                 H = M.add(e);
  704.             }
  705.         } else {
  706.             if (e.getReal() < 3.6 && M.abs().getReal() > pi.getReal()) {
  707.                 H = M.subtract(e.copySign(M));
  708.             } else {
  709.                 H = M.divide(e.subtract(1));
  710.             }
  711.         }

  712.         // Iterative computation
  713.         int iter = 0;
  714.         do {
  715.             final T f3  = e.multiply(H.cosh());
  716.             final T f2  = e.multiply(H.sinh());
  717.             final T f1  = f3.subtract(1);
  718.             final T f0  = f2.subtract(H).subtract(M);
  719.             final T f12 = f1.multiply(2);
  720.             final T d   = f0.divide(f12);
  721.             final T fdf = f1.subtract(d.multiply(f2));
  722.             final T ds  = f0.divide(fdf);

  723.             final T shift = f0.divide(fdf.add(ds.multiply(ds.multiply(f3.divide(6)))));

  724.             H = H.subtract(shift);

  725.             if ((shift.abs().getReal()) <= 1.0e-12) {
  726.                 return H;
  727.             }

  728.         } while (++iter < 50);

  729.         throw new MathIllegalArgumentException(OrekitMessages.UNABLE_TO_COMPUTE_HYPERBOLIC_ECCENTRIC_ANOMALY,
  730.                                                iter);
  731.     }

  732.     /** Computes the mean anomaly from the elliptic eccentric anomaly.
  733.      * @param E eccentric anomaly (rad)
  734.      * @param e eccentricity
  735.      * @param <T> type of the field elements
  736.      * @return M the mean anomaly
  737.      */
  738.     public static <T extends CalculusFieldElement<T>> T ellipticEccentricToMean(final T E, final T e) {
  739.         return E.subtract(e.multiply(E.sin()));
  740.     }

  741.     /** {@inheritDoc} */
  742.     public T getEquinoctialEx() {
  743.         return e.multiply(pa.add(raan).cos());
  744.     }

  745.     /** {@inheritDoc} */
  746.     public T getEquinoctialExDot() {

  747.         if (!hasDerivatives()) {
  748.             return null;
  749.         }

  750.         final FieldUnivariateDerivative1<T> eUD    = new FieldUnivariateDerivative1<>(e,    eDot);
  751.         final FieldUnivariateDerivative1<T> paUD   = new FieldUnivariateDerivative1<>(pa,   paDot);
  752.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  753.         return eUD.multiply(paUD.add(raanUD).cos()).getDerivative(1);

  754.     }

  755.     /** {@inheritDoc} */
  756.     public T getEquinoctialEy() {
  757.         return  e.multiply((pa.add(raan)).sin());
  758.     }

  759.     /** {@inheritDoc} */
  760.     public T getEquinoctialEyDot() {

  761.         if (!hasDerivatives()) {
  762.             return null;
  763.         }

  764.         final FieldUnivariateDerivative1<T> eUD    = new FieldUnivariateDerivative1<>(e,    eDot);
  765.         final FieldUnivariateDerivative1<T> paUD   = new FieldUnivariateDerivative1<>(pa,   paDot);
  766.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  767.         return eUD.multiply(paUD.add(raanUD).sin()).getDerivative(1);

  768.     }

  769.     /** {@inheritDoc} */
  770.     public T getHx() {
  771.         // Check for equatorial retrograde orbit
  772.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  773.             return this.zero.add(Double.NaN);
  774.         }
  775.         return  raan.cos().multiply(i.divide(2).tan());
  776.     }

  777.     /** {@inheritDoc} */
  778.     public T getHxDot() {

  779.         if (!hasDerivatives()) {
  780.             return null;
  781.         }

  782.         // Check for equatorial retrograde orbit
  783.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  784.             return this.zero.add(Double.NaN);
  785.         }

  786.         final FieldUnivariateDerivative1<T> iUD    = new FieldUnivariateDerivative1<>(i,    iDot);
  787.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  788.         return raanUD.cos().multiply(iUD.multiply(0.5).tan()).getDerivative(1);

  789.     }

  790.     /** {@inheritDoc} */
  791.     public T getHy() {
  792.         // Check for equatorial retrograde orbit
  793.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  794.             return this.zero.add(Double.NaN);
  795.         }
  796.         return  raan.sin().multiply(i.divide(2).tan());
  797.     }

  798.     /** {@inheritDoc} */
  799.     public T getHyDot() {

  800.         if (!hasDerivatives()) {
  801.             return null;
  802.         }

  803.         // Check for equatorial retrograde orbit
  804.         if (FastMath.abs(i.subtract(i.getPi()).getReal()) < 1.0e-10) {
  805.             return this.zero.add(Double.NaN);
  806.         }

  807.         final FieldUnivariateDerivative1<T> iUD    = new FieldUnivariateDerivative1<>(i,    iDot);
  808.         final FieldUnivariateDerivative1<T> raanUD = new FieldUnivariateDerivative1<>(raan, raanDot);
  809.         return raanUD.sin().multiply(iUD.multiply(0.5).tan()).getDerivative(1);

  810.     }

  811.     /** {@inheritDoc} */
  812.     public T getLv() {
  813.         return pa.add(raan).add(v);
  814.     }

  815.     /** {@inheritDoc} */
  816.     public T getLvDot() {
  817.         return hasDerivatives() ?
  818.                paDot.add(raanDot).add(vDot) :
  819.                null;
  820.     }

  821.     /** {@inheritDoc} */
  822.     public T getLE() {
  823.         return pa.add(raan).add(getEccentricAnomaly());
  824.     }

  825.     /** {@inheritDoc} */
  826.     public T getLEDot() {
  827.         return hasDerivatives() ?
  828.                paDot.add(raanDot).add(getEccentricAnomalyDot()) :
  829.                null;
  830.     }

  831.     /** {@inheritDoc} */
  832.     public T getLM() {
  833.         return pa.add(raan).add(getMeanAnomaly());
  834.     }

  835.     /** {@inheritDoc} */
  836.     public T getLMDot() {
  837.         return hasDerivatives() ?
  838.                paDot.add(raanDot).add(getMeanAnomalyDot()) :
  839.                null;
  840.     }

  841.     /** Compute position and velocity but not acceleration.
  842.      */
  843.     private void computePVWithoutA() {

  844.         if (partialPV != null) {
  845.             // already computed
  846.             return;
  847.         }

  848.         // preliminary variables
  849.         final FieldSinCos<T> scRaan = FastMath.sinCos(raan);
  850.         final FieldSinCos<T> scPa   = FastMath.sinCos(pa);
  851.         final FieldSinCos<T> scI    = FastMath.sinCos(i);
  852.         final T cosRaan = scRaan.cos();
  853.         final T sinRaan = scRaan.sin();
  854.         final T cosPa   = scPa.cos();
  855.         final T sinPa   = scPa.sin();
  856.         final T cosI    = scI.cos();
  857.         final T sinI    = scI.sin();
  858.         final T crcp    = cosRaan.multiply(cosPa);
  859.         final T crsp    = cosRaan.multiply(sinPa);
  860.         final T srcp    = sinRaan.multiply(cosPa);
  861.         final T srsp    = sinRaan.multiply(sinPa);

  862.         // reference axes defining the orbital plane
  863.         final FieldVector3D<T> p = new FieldVector3D<>(crcp.subtract(cosI.multiply(srsp)),  srcp.add(cosI.multiply(crsp)), sinI.multiply(sinPa));
  864.         final FieldVector3D<T> q = new FieldVector3D<>(crsp.add(cosI.multiply(srcp)).negate(), cosI.multiply(crcp).subtract(srsp), sinI.multiply(cosPa));

  865.         if (a.getReal() > 0) {

  866.             // elliptical case

  867.             // elliptic eccentric anomaly
  868.             final T uME2             = e.negate().add(1).multiply(e.add(1));
  869.             final T s1Me2            = uME2.sqrt();
  870.             final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  871.             final T cosE             = scE.cos();
  872.             final T sinE             = scE.sin();

  873.             // coordinates of position and velocity in the orbital plane
  874.             final T x      = a.multiply(cosE.subtract(e));
  875.             final T y      = a.multiply(sinE).multiply(s1Me2);
  876.             final T factor = FastMath.sqrt(getMu().divide(a)).divide(e.negate().multiply(cosE).add(1));
  877.             final T xDot   = sinE.negate().multiply(factor);
  878.             final T yDot   = cosE.multiply(s1Me2).multiply(factor);

  879.             final FieldVector3D<T> position = new FieldVector3D<>(x, p, y, q);
  880.             final FieldVector3D<T> velocity = new FieldVector3D<>(xDot, p, yDot, q);
  881.             partialPV = new FieldPVCoordinates<>(position, velocity);

  882.         } else {

  883.             // hyperbolic case

  884.             // compute position and velocity factors
  885.             final FieldSinCos<T> scV = FastMath.sinCos(v);
  886.             final T sinV             = scV.sin();
  887.             final T cosV             = scV.cos();
  888.             final T f                = a.multiply(e.multiply(e).negate().add(1));
  889.             final T posFactor        = f.divide(e.multiply(cosV).add(1));
  890.             final T velFactor        = FastMath.sqrt(getMu().divide(f));

  891.             final FieldVector3D<T> position     = new FieldVector3D<>(posFactor.multiply(cosV), p, posFactor.multiply(sinV), q);
  892.             final FieldVector3D<T> velocity     = new FieldVector3D<>(velFactor.multiply(sinV).negate(), p, velFactor.multiply(e.add(cosV)), q);
  893.             partialPV = new FieldPVCoordinates<>(position, velocity);

  894.         }

  895.     }

  896.     /** Compute non-Keplerian part of the acceleration from first time derivatives.
  897.      * <p>
  898.      * This method should be called only when {@link #hasDerivatives()} returns true.
  899.      * </p>
  900.      * @return non-Keplerian part of the acceleration
  901.      */
  902.     private FieldVector3D<T> nonKeplerianAcceleration() {

  903.         final T[][] dCdP = MathArrays.buildArray(a.getField(), 6, 6);
  904.         getJacobianWrtParameters(PositionAngle.MEAN, dCdP);

  905.         final T nonKeplerianMeanMotion = getMeanAnomalyDot().subtract(getKeplerianMeanMotion());
  906.         final T nonKeplerianAx =     dCdP[3][0].multiply(aDot).
  907.                                  add(dCdP[3][1].multiply(eDot)).
  908.                                  add(dCdP[3][2].multiply(iDot)).
  909.                                  add(dCdP[3][3].multiply(paDot)).
  910.                                  add(dCdP[3][4].multiply(raanDot)).
  911.                                  add(dCdP[3][5].multiply(nonKeplerianMeanMotion));
  912.         final T nonKeplerianAy =     dCdP[4][0].multiply(aDot).
  913.                                  add(dCdP[4][1].multiply(eDot)).
  914.                                  add(dCdP[4][2].multiply(iDot)).
  915.                                  add(dCdP[4][3].multiply(paDot)).
  916.                                  add(dCdP[4][4].multiply(raanDot)).
  917.                                  add(dCdP[4][5].multiply(nonKeplerianMeanMotion));
  918.         final T nonKeplerianAz =     dCdP[5][0].multiply(aDot).
  919.                                  add(dCdP[5][1].multiply(eDot)).
  920.                                  add(dCdP[5][2].multiply(iDot)).
  921.                                  add(dCdP[5][3].multiply(paDot)).
  922.                                  add(dCdP[5][4].multiply(raanDot)).
  923.                                  add(dCdP[5][5].multiply(nonKeplerianMeanMotion));

  924.         return new FieldVector3D<>(nonKeplerianAx, nonKeplerianAy, nonKeplerianAz);

  925.     }

  926.     /** {@inheritDoc} */
  927.     protected TimeStampedFieldPVCoordinates<T> initPVCoordinates() {

  928.         // position and velocity
  929.         computePVWithoutA();

  930.         // acceleration
  931.         final T r2 = partialPV.getPosition().getNormSq();
  932.         final FieldVector3D<T> keplerianAcceleration = new FieldVector3D<>(r2.multiply(FastMath.sqrt(r2)).reciprocal().multiply(getMu().negate()),
  933.                                                                            partialPV.getPosition());
  934.         final FieldVector3D<T> acceleration = hasDerivatives() ?
  935.                                               keplerianAcceleration.add(nonKeplerianAcceleration()) :
  936.                                               keplerianAcceleration;

  937.         return new TimeStampedFieldPVCoordinates<>(getDate(), partialPV.getPosition(), partialPV.getVelocity(), acceleration);

  938.     }

  939.     /** {@inheritDoc} */
  940.     public FieldKeplerianOrbit<T> shiftedBy(final double dt) {
  941.         return shiftedBy(getDate().getField().getZero().add(dt));
  942.     }

  943.     /** {@inheritDoc} */
  944.     public FieldKeplerianOrbit<T> shiftedBy(final T dt) {

  945.         // use Keplerian-only motion
  946.         final FieldKeplerianOrbit<T> keplerianShifted = new FieldKeplerianOrbit<>(a, e, i, pa, raan,
  947.                                                                                   getKeplerianMeanMotion().multiply(dt).add(getMeanAnomaly()),
  948.                                                                                   PositionAngle.MEAN, getFrame(), getDate().shiftedBy(dt), getMu());

  949.         if (hasDerivatives()) {

  950.             // extract non-Keplerian acceleration from first time derivatives
  951.             final FieldVector3D<T> nonKeplerianAcceleration = nonKeplerianAcceleration();

  952.             // add quadratic effect of non-Keplerian acceleration to Keplerian-only shift
  953.             keplerianShifted.computePVWithoutA();
  954.             final FieldVector3D<T> fixedP   = new FieldVector3D<>(one, keplerianShifted.partialPV.getPosition(),
  955.                                                                   dt.multiply(dt).multiply(0.5), nonKeplerianAcceleration);
  956.             final T   fixedR2 = fixedP.getNormSq();
  957.             final T   fixedR  = fixedR2.sqrt();
  958.             final FieldVector3D<T> fixedV  = new FieldVector3D<>(one, keplerianShifted.partialPV.getVelocity(),
  959.                                                                  dt, nonKeplerianAcceleration);
  960.             final FieldVector3D<T> fixedA  = new FieldVector3D<>(fixedR2.multiply(fixedR).reciprocal().multiply(getMu().negate()),
  961.                                                                  keplerianShifted.partialPV.getPosition(),
  962.                                                                  one, nonKeplerianAcceleration);

  963.             // build a new orbit, taking non-Keplerian acceleration into account
  964.             return new FieldKeplerianOrbit<>(new TimeStampedFieldPVCoordinates<>(keplerianShifted.getDate(),
  965.                                                                                  fixedP, fixedV, fixedA),
  966.                                              keplerianShifted.getFrame(), keplerianShifted.getMu());

  967.         } else {
  968.             // Keplerian-only motion is all we can do
  969.             return keplerianShifted;
  970.         }

  971.     }

  972.     /** {@inheritDoc}
  973.      * <p>
  974.      * The interpolated instance is created by polynomial Hermite interpolation
  975.      * on Keplerian elements, without derivatives (which means the interpolation
  976.      * falls back to Lagrange interpolation only).
  977.      * </p>
  978.      * <p>
  979.      * As this implementation of interpolation is polynomial, it should be used only
  980.      * with small samples (about 10-20 points) in order to avoid <a
  981.      * href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's phenomenon</a>
  982.      * and numerical problems (including NaN appearing).
  983.      * </p>
  984.      * <p>
  985.      * If orbit interpolation on large samples is needed, using the {@link
  986.      * org.orekit.propagation.analytical.Ephemeris} class is a better way than using this
  987.      * low-level interpolation. The Ephemeris class automatically handles selection of
  988.      * a neighboring sub-sample with a predefined number of point from a large global sample
  989.      * in a thread-safe way.
  990.      * </p>
  991.      */
  992.     public FieldKeplerianOrbit<T> interpolate(final FieldAbsoluteDate<T> date, final Stream<FieldOrbit<T>> sample) {

  993.         // first pass to check if derivatives are available throughout the sample
  994.         final List<FieldOrbit<T>> list = sample.collect(Collectors.toList());
  995.         boolean useDerivatives = true;
  996.         for (final FieldOrbit<T> orbit : list) {
  997.             useDerivatives = useDerivatives && orbit.hasDerivatives();
  998.         }

  999.         // set up an interpolator
  1000.         final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();

  1001.         // second pass to feed interpolator
  1002.         FieldAbsoluteDate<T> previousDate = null;
  1003.         T                    previousPA   = zero.add(Double.NaN);
  1004.         T                    previousRAAN = zero.add(Double.NaN);
  1005.         T                    previousM    = zero.add(Double.NaN);
  1006.         for (final FieldOrbit<T> orbit : list) {
  1007.             final FieldKeplerianOrbit<T> kep = (FieldKeplerianOrbit<T>) OrbitType.KEPLERIAN.convertType(orbit);
  1008.             final T continuousPA;
  1009.             final T continuousRAAN;
  1010.             final T continuousM;
  1011.             if (previousDate == null) {
  1012.                 continuousPA   = kep.getPerigeeArgument();
  1013.                 continuousRAAN = kep.getRightAscensionOfAscendingNode();
  1014.                 continuousM    = kep.getMeanAnomaly();
  1015.             } else {
  1016.                 final T dt      = kep.getDate().durationFrom(previousDate);
  1017.                 final T keplerM = previousM.add(kep.getKeplerianMeanMotion().multiply(dt));
  1018.                 continuousPA   = MathUtils.normalizeAngle(kep.getPerigeeArgument(), previousPA);
  1019.                 continuousRAAN = MathUtils.normalizeAngle(kep.getRightAscensionOfAscendingNode(), previousRAAN);
  1020.                 continuousM    = MathUtils.normalizeAngle(kep.getMeanAnomaly(), keplerM);
  1021.             }
  1022.             previousDate = kep.getDate();
  1023.             previousPA   = continuousPA;
  1024.             previousRAAN = continuousRAAN;
  1025.             previousM    = continuousM;
  1026.             final T[] toAdd = MathArrays.buildArray(getA().getField(), 6);
  1027.             toAdd[0] = kep.getA();
  1028.             toAdd[1] = kep.getE();
  1029.             toAdd[2] = kep.getI();
  1030.             toAdd[3] = continuousPA;
  1031.             toAdd[4] = continuousRAAN;
  1032.             toAdd[5] = continuousM;
  1033.             if (useDerivatives) {
  1034.                 final T[] toAddDot = MathArrays.buildArray(one.getField(), 6);
  1035.                 toAddDot[0] = kep.getADot();
  1036.                 toAddDot[1] = kep.getEDot();
  1037.                 toAddDot[2] = kep.getIDot();
  1038.                 toAddDot[3] = kep.getPerigeeArgumentDot();
  1039.                 toAddDot[4] = kep.getRightAscensionOfAscendingNodeDot();
  1040.                 toAddDot[5] = kep.getMeanAnomalyDot();
  1041.                 interpolator.addSamplePoint(kep.getDate().durationFrom(date),
  1042.                                             toAdd, toAddDot);
  1043.             } else {
  1044.                 interpolator.addSamplePoint(this.zero.add(kep.getDate().durationFrom(date)),
  1045.                                             toAdd);
  1046.             }
  1047.         }

  1048.         // interpolate
  1049.         final T[][] interpolated = interpolator.derivatives(zero, 1);

  1050.         // build a new interpolated instance
  1051.         return new FieldKeplerianOrbit<>(interpolated[0][0], interpolated[0][1], interpolated[0][2],
  1052.                                          interpolated[0][3], interpolated[0][4], interpolated[0][5],
  1053.                                          interpolated[1][0], interpolated[1][1], interpolated[1][2],
  1054.                                          interpolated[1][3], interpolated[1][4], interpolated[1][5],
  1055.                                          PositionAngle.MEAN, getFrame(), date, getMu());

  1056.     }

  1057.     /** {@inheritDoc} */
  1058.     protected T[][] computeJacobianMeanWrtCartesian() {
  1059.         if (a.getReal() > 0) {
  1060.             return computeJacobianMeanWrtCartesianElliptical();
  1061.         } else {
  1062.             return computeJacobianMeanWrtCartesianHyperbolic();
  1063.         }
  1064.     }

  1065.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1066.      * <p>
  1067.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1068.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1069.      * yDot for j=4, zDot for j=5).
  1070.      * </p>
  1071.      * @return 6x6 Jacobian matrix
  1072.      */
  1073.     private T[][] computeJacobianMeanWrtCartesianElliptical() {

  1074.         final T[][] jacobian = MathArrays.buildArray(getA().getField(), 6, 6);

  1075.         // compute various intermediate parameters
  1076.         computePVWithoutA();
  1077.         final FieldVector3D<T> position = partialPV.getPosition();
  1078.         final FieldVector3D<T> velocity = partialPV.getVelocity();
  1079.         final FieldVector3D<T> momentum = partialPV.getMomentum();
  1080.         final T v2         = velocity.getNormSq();
  1081.         final T r2         = position.getNormSq();
  1082.         final T r          = r2.sqrt();
  1083.         final T r3         = r.multiply(r2);

  1084.         final T px         = position.getX();
  1085.         final T py         = position.getY();
  1086.         final T pz         = position.getZ();
  1087.         final T vx         = velocity.getX();
  1088.         final T vy         = velocity.getY();
  1089.         final T vz         = velocity.getZ();
  1090.         final T mx         = momentum.getX();
  1091.         final T my         = momentum.getY();
  1092.         final T mz         = momentum.getZ();

  1093.         final T mu         = getMu();
  1094.         final T sqrtMuA    = FastMath.sqrt(a.multiply(mu));
  1095.         final T sqrtAoMu   = FastMath.sqrt(a.divide(mu));
  1096.         final T a2         = a.multiply(a);
  1097.         final T twoA       = a.multiply(2);
  1098.         final T rOnA       = r.divide(a);

  1099.         final T oMe2       = e.multiply(e).negate().add(1);
  1100.         final T epsilon    = oMe2.sqrt();
  1101.         final T sqrtRec    = epsilon.reciprocal();

  1102.         final FieldSinCos<T> scI  = FastMath.sinCos(i);
  1103.         final FieldSinCos<T> scPA = FastMath.sinCos(pa);
  1104.         final T cosI       = scI.cos();
  1105.         final T sinI       = scI.sin();
  1106.         final T cosPA      = scPA.cos();
  1107.         final T sinPA      = scPA.sin();

  1108.         final T pv         = FieldVector3D.dotProduct(position, velocity);
  1109.         final T cosE       = a.subtract(r).divide(a.multiply(e));
  1110.         final T sinE       = pv.divide(e.multiply(sqrtMuA));

  1111.         // da
  1112.         final FieldVector3D<T> vectorAR = new FieldVector3D<>(a2.multiply(2).divide(r3), position);
  1113.         final FieldVector3D<T> vectorARDot = velocity.scalarMultiply(a2.multiply(mu.divide(2.).reciprocal()));
  1114.         fillHalfRow(this.one, vectorAR,    jacobian[0], 0);
  1115.         fillHalfRow(this.one, vectorARDot, jacobian[0], 3);

  1116.         // de
  1117.         final T factorER3 = pv.divide(twoA);
  1118.         final FieldVector3D<T> vectorER   = new FieldVector3D<>(cosE.multiply(v2).divide(r.multiply(mu)), position,
  1119.                                                                 sinE.divide(sqrtMuA), velocity,
  1120.                                                                 factorER3.negate().multiply(sinE).divide(sqrtMuA), vectorAR);
  1121.         final FieldVector3D<T> vectorERDot = new FieldVector3D<>(sinE.divide(sqrtMuA), position,
  1122.                                                                  cosE.multiply(mu.divide(2.).reciprocal()).multiply(r), velocity,
  1123.                                                                  factorER3.negate().multiply(sinE).divide(sqrtMuA), vectorARDot);
  1124.         fillHalfRow(this.one, vectorER,    jacobian[1], 0);
  1125.         fillHalfRow(this.one, vectorERDot, jacobian[1], 3);

  1126.         // dE / dr (Eccentric anomaly)
  1127.         final T coefE = cosE.divide(e.multiply(sqrtMuA));
  1128.         final FieldVector3D<T>  vectorEAnR =
  1129.             new FieldVector3D<>(sinE.negate().multiply(v2).divide(e.multiply(r).multiply(mu)), position, coefE, velocity,
  1130.                                 factorER3.negate().multiply(coefE), vectorAR);

  1131.         // dE / drDot
  1132.         final FieldVector3D<T>  vectorEAnRDot =
  1133.             new FieldVector3D<>(sinE.multiply(-2).multiply(r).divide(e.multiply(mu)), velocity, coefE, position,
  1134.                                 factorER3.negate().multiply(coefE), vectorARDot);

  1135.         // precomputing some more factors
  1136.         final T s1 = sinE.negate().multiply(pz).divide(r).subtract(cosE.multiply(vz).multiply(sqrtAoMu));
  1137.         final T s2 = cosE.negate().multiply(pz).divide(r3);
  1138.         final T s3 = sinE.multiply(vz).divide(sqrtMuA.multiply(-2));
  1139.         final T t1 = sqrtRec.multiply(cosE.multiply(pz).divide(r).subtract(sinE.multiply(vz).multiply(sqrtAoMu)));
  1140.         final T t2 = sqrtRec.multiply(sinE.negate().multiply(pz).divide(r3));
  1141.         final T t3 = sqrtRec.multiply(cosE.subtract(e)).multiply(vz).divide(sqrtMuA.multiply(2));
  1142.         final T t4 = sqrtRec.multiply(e.multiply(sinI).multiply(cosPA).multiply(sqrtRec).subtract(vz.multiply(sqrtAoMu)));
  1143.         final FieldVector3D<T> s = new FieldVector3D<>(cosE.divide(r), this.PLUS_K,
  1144.                                                        s1,       vectorEAnR,
  1145.                                                        s2,       position,
  1146.                                                        s3,       vectorAR);
  1147.         final FieldVector3D<T> sDot = new FieldVector3D<>(sinE.negate().multiply(sqrtAoMu), this.PLUS_K,
  1148.                                                           s1,               vectorEAnRDot,
  1149.                                                           s3,               vectorARDot);
  1150.         final FieldVector3D<T> t =
  1151.             new FieldVector3D<>(sqrtRec.multiply(sinE).divide(r), this.PLUS_K).add(new FieldVector3D<>(t1, vectorEAnR,
  1152.                                                                                                        t2, position,
  1153.                                                                                                        t3, vectorAR,
  1154.                                                                                                        t4, vectorER));
  1155.         final FieldVector3D<T> tDot = new FieldVector3D<>(sqrtRec.multiply(cosE.subtract(e)).multiply(sqrtAoMu), this.PLUS_K,
  1156.                                                           t1,                                                    vectorEAnRDot,
  1157.                                                           t3,                                                    vectorARDot,
  1158.                                                           t4,                                                    vectorERDot);

  1159.         // di
  1160.         final T factorI1 = sinI.negate().multiply(sqrtRec).divide(sqrtMuA);
  1161.         final T i1 =  factorI1;
  1162.         final T i2 =  factorI1.negate().multiply(mz).divide(twoA);
  1163.         final T i3 =  factorI1.multiply(mz).multiply(e).divide(oMe2);
  1164.         final T i4 = cosI.multiply(sinPA);
  1165.         final T i5 = cosI.multiply(cosPA);
  1166.         fillHalfRow(i1, new FieldVector3D<>(vy, vx.negate(), this.zero), i2, vectorAR, i3, vectorER, i4, s, i5, t,
  1167.                     jacobian[2], 0);
  1168.         fillHalfRow(i1, new FieldVector3D<>(py.negate(), px, this.zero), i2, vectorARDot, i3, vectorERDot, i4, sDot, i5, tDot,
  1169.                     jacobian[2], 3);

  1170.         // dpa
  1171.         fillHalfRow(cosPA.divide(sinI), s,    sinPA.negate().divide(sinI), t,    jacobian[3], 0);
  1172.         fillHalfRow(cosPA.divide(sinI), sDot, sinPA.negate().divide(sinI), tDot, jacobian[3], 3);

  1173.         // dRaan
  1174.         final T factorRaanR = (a.multiply(mu).multiply(oMe2).multiply(sinI).multiply(sinI)).reciprocal();
  1175.         fillHalfRow( factorRaanR.negate().multiply(my), new FieldVector3D<>(zero, vz, vy.negate()),
  1176.                      factorRaanR.multiply(mx), new FieldVector3D<>(vz.negate(), zero,  vx),
  1177.                      jacobian[4], 0);
  1178.         fillHalfRow(factorRaanR.negate().multiply(my), new FieldVector3D<>(zero, pz.negate(),  py),
  1179.                      factorRaanR.multiply(mx), new FieldVector3D<>(pz, zero, px.negate()),
  1180.                      jacobian[4], 3);

  1181.         // dM
  1182.         fillHalfRow(rOnA, vectorEAnR,    sinE.negate(), vectorER,    jacobian[5], 0);
  1183.         fillHalfRow(rOnA, vectorEAnRDot, sinE.negate(), vectorERDot, jacobian[5], 3);

  1184.         return jacobian;

  1185.     }

  1186.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1187.      * <p>
  1188.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1189.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1190.      * yDot for j=4, zDot for j=5).
  1191.      * </p>
  1192.      * @return 6x6 Jacobian matrix
  1193.      */
  1194.     private T[][] computeJacobianMeanWrtCartesianHyperbolic() {

  1195.         final T[][] jacobian = MathArrays.buildArray(getA().getField(), 6, 6);

  1196.         // compute various intermediate parameters
  1197.         computePVWithoutA();
  1198.         final FieldVector3D<T> position = partialPV.getPosition();
  1199.         final FieldVector3D<T> velocity = partialPV.getVelocity();
  1200.         final FieldVector3D<T> momentum = partialPV.getMomentum();
  1201.         final T r2         = position.getNormSq();
  1202.         final T r          = r2.sqrt();
  1203.         final T r3         = r.multiply(r2);

  1204.         final T x          = position.getX();
  1205.         final T y          = position.getY();
  1206.         final T z          = position.getZ();
  1207.         final T vx         = velocity.getX();
  1208.         final T vy         = velocity.getY();
  1209.         final T vz         = velocity.getZ();
  1210.         final T mx         = momentum.getX();
  1211.         final T my         = momentum.getY();
  1212.         final T mz         = momentum.getZ();

  1213.         final T mu         = getMu();
  1214.         final T absA       = a.negate();
  1215.         final T sqrtMuA    = absA.multiply(mu).sqrt();
  1216.         final T a2         = a.multiply(a);
  1217.         final T rOa        = r.divide(absA);

  1218.         final FieldSinCos<T> scI = FastMath.sinCos(i);
  1219.         final T cosI       = scI.cos();
  1220.         final T sinI       = scI.sin();

  1221.         final T pv         = FieldVector3D.dotProduct(position, velocity);

  1222.         // da
  1223.         final FieldVector3D<T> vectorAR = new FieldVector3D<>(a2.multiply(-2).divide(r3), position);
  1224.         final FieldVector3D<T> vectorARDot = velocity.scalarMultiply(a2.multiply(-2).divide(mu));
  1225.         fillHalfRow(this.one.negate(), vectorAR,    jacobian[0], 0);
  1226.         fillHalfRow(this.one.negate(), vectorARDot, jacobian[0], 3);

  1227.         // differentials of the momentum
  1228.         final T m      = momentum.getNorm();
  1229.         final T oOm    = m.reciprocal();
  1230.         final FieldVector3D<T> dcXP = new FieldVector3D<>(this.zero, vz, vy.negate());
  1231.         final FieldVector3D<T> dcYP = new FieldVector3D<>(vz.negate(),   this.zero,  vx);
  1232.         final FieldVector3D<T> dcZP = new FieldVector3D<>( vy, vx.negate(),   this.zero);
  1233.         final FieldVector3D<T> dcXV = new FieldVector3D<>(  this.zero,  z.negate(),   y);
  1234.         final FieldVector3D<T> dcYV = new FieldVector3D<>(  z,   this.zero,  x.negate());
  1235.         final FieldVector3D<T> dcZV = new FieldVector3D<>( y.negate(),   x,   this.zero);
  1236.         final FieldVector3D<T> dCP  = new FieldVector3D<>(mx.multiply(oOm), dcXP, my.multiply(oOm), dcYP, mz.multiply(oOm), dcZP);
  1237.         final FieldVector3D<T> dCV  = new FieldVector3D<>(mx.multiply(oOm), dcXV, my.multiply(oOm), dcYV, mz.multiply(oOm), dcZV);

  1238.         // dp
  1239.         final T mOMu   = m.divide(mu);
  1240.         final FieldVector3D<T> dpP  = new FieldVector3D<>(mOMu.multiply(2), dCP);
  1241.         final FieldVector3D<T> dpV  = new FieldVector3D<>(mOMu.multiply(2), dCV);

  1242.         // de
  1243.         final T p      = m.multiply(mOMu);
  1244.         final T moO2ae = absA.multiply(2).multiply(e).reciprocal();
  1245.         final T m2OaMu = p.negate().divide(absA);
  1246.         fillHalfRow(moO2ae, dpP, m2OaMu.multiply(moO2ae), vectorAR,    jacobian[1], 0);
  1247.         fillHalfRow(moO2ae, dpV, m2OaMu.multiply(moO2ae), vectorARDot, jacobian[1], 3);

  1248.         // di
  1249.         final T cI1 = m.multiply(sinI).reciprocal();
  1250.         final T cI2 = cosI.multiply(cI1);
  1251.         fillHalfRow(cI2, dCP, cI1.negate(), dcZP, jacobian[2], 0);
  1252.         fillHalfRow(cI2, dCV, cI1.negate(), dcZV, jacobian[2], 3);


  1253.         // dPA
  1254.         final T cP1     =  y.multiply(oOm);
  1255.         final T cP2     =  x.negate().multiply(oOm);
  1256.         final T cP3     =  mx.multiply(cP1).add(my.multiply(cP2)).negate();
  1257.         final T cP4     =  cP3.multiply(oOm);
  1258.         final T cP5     =  r2.multiply(sinI).multiply(sinI).negate().reciprocal();
  1259.         final T cP6     = z.multiply(cP5);
  1260.         final T cP7     = cP3.multiply(cP5);
  1261.         final FieldVector3D<T> dacP  = new FieldVector3D<>(cP1, dcXP, cP2, dcYP, cP4, dCP, oOm, new FieldVector3D<>(my.negate(), mx, this.zero));
  1262.         final FieldVector3D<T> dacV  = new FieldVector3D<>(cP1, dcXV, cP2, dcYV, cP4, dCV);
  1263.         final FieldVector3D<T> dpoP  = new FieldVector3D<>(cP6, dacP, cP7, this.PLUS_K);
  1264.         final FieldVector3D<T> dpoV  = new FieldVector3D<>(cP6, dacV);

  1265.         final T re2     = r2.multiply(e).multiply(e);
  1266.         final T recOre2 = p.subtract(r).divide(re2);
  1267.         final T resOre2 = pv.multiply(mOMu).divide(re2);
  1268.         final FieldVector3D<T> dreP  = new FieldVector3D<>(mOMu, velocity, pv.divide(mu), dCP);
  1269.         final FieldVector3D<T> dreV  = new FieldVector3D<>(mOMu, position, pv.divide(mu), dCV);
  1270.         final FieldVector3D<T> davP  = new FieldVector3D<>(resOre2.negate(), dpP, recOre2, dreP, resOre2.divide(r), position);
  1271.         final FieldVector3D<T> davV  = new FieldVector3D<>(resOre2.negate(), dpV, recOre2, dreV);
  1272.         fillHalfRow(this.one, dpoP, this.one.negate(), davP, jacobian[3], 0);
  1273.         fillHalfRow(this.one, dpoV, this.one.negate(), davV, jacobian[3], 3);

  1274.         // dRAAN
  1275.         final T cO0 = cI1.multiply(cI1);
  1276.         final T cO1 =  mx.multiply(cO0);
  1277.         final T cO2 =  my.negate().multiply(cO0);
  1278.         fillHalfRow(cO1, dcYP, cO2, dcXP, jacobian[4], 0);
  1279.         fillHalfRow(cO1, dcYV, cO2, dcXV, jacobian[4], 3);

  1280.         // dM
  1281.         final T s2a    = pv.divide(absA.multiply(2));
  1282.         final T oObux  = m.multiply(m).add(absA.multiply(mu)).sqrt().reciprocal();
  1283.         final T scasbu = pv.multiply(oObux);
  1284.         final FieldVector3D<T> dauP = new FieldVector3D<>(sqrtMuA.reciprocal(), velocity, s2a.negate().divide(sqrtMuA), vectorAR);
  1285.         final FieldVector3D<T> dauV = new FieldVector3D<>(sqrtMuA.reciprocal(), position, s2a.negate().divide(sqrtMuA), vectorARDot);
  1286.         final FieldVector3D<T> dbuP = new FieldVector3D<>(oObux.multiply(mu.divide(2.)), vectorAR,    m.multiply(oObux), dCP);
  1287.         final FieldVector3D<T> dbuV = new FieldVector3D<>(oObux.multiply(mu.divide(2.)), vectorARDot, m.multiply(oObux), dCV);
  1288.         final FieldVector3D<T> dcuP = new FieldVector3D<>(oObux, velocity, scasbu.negate().multiply(oObux), dbuP);
  1289.         final FieldVector3D<T> dcuV = new FieldVector3D<>(oObux, position, scasbu.negate().multiply(oObux), dbuV);
  1290.         fillHalfRow(this.one, dauP, e.negate().divide(rOa.add(1)), dcuP, jacobian[5], 0);
  1291.         fillHalfRow(this.one, dauV, e.negate().divide(rOa.add(1)), dcuV, jacobian[5], 3);

  1292.         return jacobian;

  1293.     }

  1294.     /** {@inheritDoc} */
  1295.     protected T[][] computeJacobianEccentricWrtCartesian() {
  1296.         if (a.getReal() > 0) {
  1297.             return computeJacobianEccentricWrtCartesianElliptical();
  1298.         } else {
  1299.             return computeJacobianEccentricWrtCartesianHyperbolic();
  1300.         }
  1301.     }

  1302.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1303.      * <p>
  1304.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1305.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1306.      * yDot for j=4, zDot for j=5).
  1307.      * </p>
  1308.      * @return 6x6 Jacobian matrix
  1309.      */
  1310.     private T[][] computeJacobianEccentricWrtCartesianElliptical() {

  1311.         // start by computing the Jacobian with mean angle
  1312.         final T[][] jacobian = computeJacobianMeanWrtCartesianElliptical();

  1313.         // Differentiating the Kepler equation M = E - e sin E leads to:
  1314.         // dM = (1 - e cos E) dE - sin E de
  1315.         // which is inverted and rewritten as:
  1316.         // dE = a/r dM + sin E a/r de
  1317.         final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  1318.         final T aOr              = e.negate().multiply(scE.cos()).add(1).reciprocal();

  1319.         // update anomaly row
  1320.         final T[] eRow           = jacobian[1];
  1321.         final T[] anomalyRow     = jacobian[5];
  1322.         for (int j = 0; j < anomalyRow.length; ++j) {
  1323.             anomalyRow[j] = aOr.multiply(anomalyRow[j].add(scE.sin().multiply(eRow[j])));
  1324.         }

  1325.         return jacobian;

  1326.     }

  1327.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1328.      * <p>
  1329.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1330.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1331.      * yDot for j=4, zDot for j=5).
  1332.      * </p>
  1333.      * @return 6x6 Jacobian matrix
  1334.      */
  1335.     private T[][] computeJacobianEccentricWrtCartesianHyperbolic() {

  1336.         // start by computing the Jacobian with mean angle
  1337.         final T[][] jacobian = computeJacobianMeanWrtCartesianHyperbolic();

  1338.         // Differentiating the Kepler equation M = e sinh H - H leads to:
  1339.         // dM = (e cosh H - 1) dH + sinh H de
  1340.         // which is inverted and rewritten as:
  1341.         // dH = 1 / (e cosh H - 1) dM - sinh H / (e cosh H - 1) de
  1342.         final T H      = getEccentricAnomaly();
  1343.         final T coshH  = H.cosh();
  1344.         final T sinhH  = H.sinh();
  1345.         final T absaOr = e.multiply(coshH).subtract(1).reciprocal();
  1346.         // update anomaly row
  1347.         final T[] eRow       = jacobian[1];
  1348.         final T[] anomalyRow = jacobian[5];

  1349.         for (int j = 0; j < anomalyRow.length; ++j) {
  1350.             anomalyRow[j] = absaOr.multiply(anomalyRow[j].subtract(sinhH.multiply(eRow[j])));

  1351.         }

  1352.         return jacobian;

  1353.     }

  1354.     /** {@inheritDoc} */
  1355.     protected T[][] computeJacobianTrueWrtCartesian() {
  1356.         if (a.getReal() > 0) {
  1357.             return computeJacobianTrueWrtCartesianElliptical();
  1358.         } else {
  1359.             return computeJacobianTrueWrtCartesianHyperbolic();
  1360.         }
  1361.     }

  1362.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1363.      * <p>
  1364.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1365.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1366.      * yDot for j=4, zDot for j=5).
  1367.      * </p>
  1368.      * @return 6x6 Jacobian matrix
  1369.      */
  1370.     private T[][] computeJacobianTrueWrtCartesianElliptical() {

  1371.         // start by computing the Jacobian with eccentric angle
  1372.         final T[][] jacobian = computeJacobianEccentricWrtCartesianElliptical();
  1373.         // Differentiating the eccentric anomaly equation sin E = sqrt(1-e^2) sin v / (1 + e cos v)
  1374.         // and using cos E = (e + cos v) / (1 + e cos v) to get rid of cos E leads to:
  1375.         // dE = [sqrt (1 - e^2) / (1 + e cos v)] dv - [sin E / (1 - e^2)] de
  1376.         // which is inverted and rewritten as:
  1377.         // dv = sqrt (1 - e^2) a/r dE + [sin E / sqrt (1 - e^2)] a/r de
  1378.         final T e2               = e.multiply(e);
  1379.         final T oMe2             = e2.negate().add(1);
  1380.         final T epsilon          = oMe2.sqrt();
  1381.         final FieldSinCos<T> scE = FastMath.sinCos(getEccentricAnomaly());
  1382.         final T aOr              = e.multiply(scE.cos()).negate().add(1).reciprocal();
  1383.         final T aFactor          = epsilon.multiply(aOr);
  1384.         final T eFactor          = scE.sin().multiply(aOr).divide(epsilon);

  1385.         // update anomaly row
  1386.         final T[] eRow           = jacobian[1];
  1387.         final T[] anomalyRow     = jacobian[5];
  1388.         for (int j = 0; j < anomalyRow.length; ++j) {
  1389.             anomalyRow[j] = aFactor.multiply(anomalyRow[j]).add(eFactor.multiply(eRow[j]));
  1390.         }
  1391.         return jacobian;

  1392.     }

  1393.     /** Compute the Jacobian of the orbital parameters with respect to the Cartesian parameters.
  1394.      * <p>
  1395.      * Element {@code jacobian[i][j]} is the derivative of parameter i of the orbit with
  1396.      * respect to Cartesian coordinate j (x for j=0, y for j=1, z for j=2, xDot for j=3,
  1397.      * yDot for j=4, zDot for j=5).
  1398.      * </p>
  1399.      * @return 6x6 Jacobian matrix
  1400.      */
  1401.     private T[][] computeJacobianTrueWrtCartesianHyperbolic() {

  1402.         // start by computing the Jacobian with eccentric angle
  1403.         final T[][] jacobian = computeJacobianEccentricWrtCartesianHyperbolic();

  1404.         // Differentiating the eccentric anomaly equation sinh H = sqrt(e^2-1) sin v / (1 + e cos v)
  1405.         // and using cosh H = (e + cos v) / (1 + e cos v) to get rid of cosh H leads to:
  1406.         // dH = [sqrt (e^2 - 1) / (1 + e cos v)] dv + [sinh H / (e^2 - 1)] de
  1407.         // which is inverted and rewritten as:
  1408.         // dv = sqrt (1 - e^2) a/r dH - [sinh H / sqrt (e^2 - 1)] a/r de
  1409.         final T e2       = e.multiply(e);
  1410.         final T e2Mo     = e2.subtract(1);
  1411.         final T epsilon  = e2Mo.sqrt();
  1412.         final T H        = getEccentricAnomaly();
  1413.         final T coshH    = H.cosh();
  1414.         final T sinhH    = H.sinh();
  1415.         final T aOr      = e.multiply(coshH).subtract(1).reciprocal();
  1416.         final T aFactor  = epsilon.multiply(aOr);
  1417.         final T eFactor  = sinhH .multiply(aOr).divide(epsilon);

  1418.         // update anomaly row
  1419.         final T[] eRow           = jacobian[1];
  1420.         final T[] anomalyRow     = jacobian[5];
  1421.         for (int j = 0; j < anomalyRow.length; ++j) {
  1422.             anomalyRow[j] = aFactor.multiply(anomalyRow[j]).subtract(eFactor.multiply(eRow[j]));
  1423.         }

  1424.         return jacobian;

  1425.     }

  1426.     /** {@inheritDoc} */
  1427.     public void addKeplerContribution(final PositionAngle type, final T gm,
  1428.                                       final T[] pDot) {
  1429.         final T oMe2;
  1430.         final T ksi;
  1431.         final T absA = a.abs();
  1432.         final T n    = absA.reciprocal().multiply(gm).sqrt().divide(absA);
  1433.         switch (type) {
  1434.             case MEAN :
  1435.                 pDot[5] = pDot[5].add(n);
  1436.                 break;
  1437.             case ECCENTRIC :
  1438.                 oMe2 = e.multiply(e).negate().add(1).abs();
  1439.                 ksi  = e.multiply(v.cos()).add(1);
  1440.                 pDot[5] = pDot[5].add( n.multiply(ksi).divide(oMe2));
  1441.                 break;
  1442.             case TRUE :
  1443.                 oMe2 = e.multiply(e).negate().add(1).abs();
  1444.                 ksi  = e.multiply(v.cos()).add(1);
  1445.                 pDot[5] = pDot[5].add(n.multiply(ksi).multiply(ksi).divide(oMe2.multiply(oMe2.sqrt())));
  1446.                 break;
  1447.             default :
  1448.                 throw new OrekitInternalError(null);
  1449.         }
  1450.     }

  1451.     /**  Returns a string representation of this Keplerian parameters object.
  1452.      * @return a string representation of this object
  1453.      */
  1454.     public String toString() {
  1455.         return new StringBuilder().append("Keplerian parameters: ").append('{').
  1456.                                   append("a: ").append(a.getReal()).
  1457.                                   append("; e: ").append(e.getReal()).
  1458.                                   append("; i: ").append(FastMath.toDegrees(i.getReal())).
  1459.                                   append("; pa: ").append(FastMath.toDegrees(pa.getReal())).
  1460.                                   append("; raan: ").append(FastMath.toDegrees(raan.getReal())).
  1461.                                   append("; v: ").append(FastMath.toDegrees(v.getReal())).
  1462.                                   append(";}").toString();
  1463.     }

  1464.     /** Check if the given parameter is within an acceptable range.
  1465.      * The bounds are inclusive: an exception is raised when either of those conditions are met:
  1466.      * <ul>
  1467.      *     <li>The parameter is strictly greater than upperBound</li>
  1468.      *     <li>The parameter is strictly lower than lowerBound</li>
  1469.      * </ul>
  1470.      * <p>
  1471.      * In either of these cases, an OrekitException is raised.
  1472.      * </p>
  1473.      * @param parameterName name of the parameter
  1474.      * @param parameter value of the parameter
  1475.      * @param lowerBound lower bound of the acceptable range (inclusive)
  1476.      * @param upperBound upper bound of the acceptable range (inclusive)
  1477.      */
  1478.     private void checkParameterRangeInclusive(final String parameterName, final double parameter,
  1479.                                               final double lowerBound, final double upperBound) {
  1480.         if (parameter < lowerBound || parameter > upperBound) {
  1481.             throw new OrekitException(OrekitMessages.INVALID_PARAMETER_RANGE, parameterName,
  1482.                                       parameter, lowerBound, upperBound);
  1483.         }
  1484.     }

  1485.     /** {@inheritDoc} */
  1486.     @Override
  1487.     public KeplerianOrbit toOrbit() {
  1488.         if (hasDerivatives()) {
  1489.             return new KeplerianOrbit(a.getReal(), e.getReal(), i.getReal(),
  1490.                                       pa.getReal(), raan.getReal(), v.getReal(),
  1491.                                       aDot.getReal(), eDot.getReal(), iDot.getReal(),
  1492.                                       paDot.getReal(), raanDot.getReal(), vDot.getReal(),
  1493.                                       PositionAngle.TRUE,
  1494.                                       getFrame(), getDate().toAbsoluteDate(), getMu().getReal());
  1495.         } else {
  1496.             return new KeplerianOrbit(a.getReal(), e.getReal(), i.getReal(),
  1497.                                       pa.getReal(), raan.getReal(), v.getReal(),
  1498.                                       PositionAngle.TRUE,
  1499.                                       getFrame(), getDate().toAbsoluteDate(), getMu().getReal());
  1500.         }
  1501.     }


  1502. }