1   /* Copyright 2002-2026 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.utils;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.analysis.differentiation.FDSFactory;
22  import org.hipparchus.analysis.differentiation.FieldDerivative;
23  import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
24  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
25  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
28  import org.hipparchus.util.FastMath;
29  import org.hipparchus.util.FieldBlendable;
30  import org.orekit.errors.OrekitException;
31  import org.orekit.errors.OrekitMessages;
32  import org.orekit.time.FieldTimeShiftable;
33  
34  /** Simple container for Position/Velocity pairs, using {@link CalculusFieldElement}.
35   * <p>
36   * The state can be slightly shifted to close dates. This shift is based on
37   * a simple linear model. It is <em>not</em> intended as a replacement for
38   * proper orbit propagation (it is not even Keplerian!) but should be sufficient
39   * for either small time shifts or coarse accuracy.
40   * </p>
41   * <p>
42   * This class is the angular counterpart to {@link FieldAngularCoordinates}.
43   * </p>
44   * <p>Instances of this class are guaranteed to be immutable.</p>
45   * @param <T> the type of the field elements
46   * @author Luc Maisonobe
47   * @since 6.0
48   * @see PVCoordinates
49   */
50  public class FieldPVCoordinates<T extends CalculusFieldElement<T>>
51      implements FieldTimeShiftable<FieldPVCoordinates<T>, T>, FieldBlendable<FieldPVCoordinates<T>, T> {
52  
53      /** The position. */
54      private final FieldVector3D<T> position;
55  
56      /** The velocity. */
57      private final FieldVector3D<T> velocity;
58  
59      /** The acceleration. */
60      private final FieldVector3D<T> acceleration;
61  
62      /** Builds a FieldPVCoordinates triplet with zero acceleration.
63       * @param position the position vector (m)
64       * @param velocity the velocity vector (m/s)
65       */
66      public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity) {
67          this(position, velocity, FieldVector3D.getZero(position.getX().getField()));
68      }
69  
70      /** Builds a FieldPVCoordinates triplet.
71       * @param position the position vector (m)
72       * @param velocity the velocity vector (m/s)
73       * @param acceleration the acceleration vector (m/s²)
74       */
75      public FieldPVCoordinates(final FieldVector3D<T> position, final FieldVector3D<T> velocity,
76                                final FieldVector3D<T> acceleration) {
77          this.position     = position;
78          this.velocity     = velocity;
79          this.acceleration = acceleration;
80      }
81  
82      /** Builds a FieldPVCoordinates from a field and a regular PVCoordinates.
83       * @param field field for the components
84       * @param pv PVCoordinates triplet to convert
85       */
86      public FieldPVCoordinates(final Field<T> field, final PVCoordinates pv) {
87          this.position     = new FieldVector3D<>(field, pv.getPosition());
88          this.velocity     = new FieldVector3D<>(field, pv.getVelocity());
89          this.acceleration = new FieldVector3D<>(field, pv.getAcceleration());
90      }
91  
92      /** Multiplicative constructor.
93       * <p>Build a PVCoordinates from another one and a scale factor.</p>
94       * <p>The PVCoordinates built will be a * pv</p>
95       * @param a scale factor
96       * @param pv base (unscaled) PVCoordinates
97       */
98      public FieldPVCoordinates(final double a, final FieldPVCoordinates<T> pv) {
99          position     = new FieldVector3D<>(a, pv.position);
100         velocity     = new FieldVector3D<>(a, pv.velocity);
101         acceleration = new FieldVector3D<>(a, pv.acceleration);
102     }
103 
104     /** Multiplicative constructor.
105      * <p>Build a PVCoordinates from another one and a scale factor.</p>
106      * <p>The PVCoordinates built will be a * pv</p>
107      * @param a scale factor
108      * @param pv base (unscaled) PVCoordinates
109      */
110     public FieldPVCoordinates(final T a, final FieldPVCoordinates<T> pv) {
111         position     = new FieldVector3D<>(a, pv.position);
112         velocity     = new FieldVector3D<>(a, pv.velocity);
113         acceleration = new FieldVector3D<>(a, pv.acceleration);
114     }
115 
116     /** Multiplicative constructor.
117      * <p>Build a PVCoordinates from another one and a scale factor.</p>
118      * <p>The PVCoordinates built will be a * pv</p>
119      * @param a scale factor
120      * @param pv base (unscaled) PVCoordinates
121      */
122     public FieldPVCoordinates(final T a, final PVCoordinates pv) {
123         position     = new FieldVector3D<>(a, pv.getPosition());
124         velocity     = new FieldVector3D<>(a, pv.getVelocity());
125         acceleration = new FieldVector3D<>(a, pv.getAcceleration());
126     }
127 
128     /** Subtractive constructor.
129      * <p>Build a relative PVCoordinates from a start and an end position.</p>
130      * <p>The PVCoordinates built will be end - start.</p>
131      * @param start Starting PVCoordinates
132      * @param end ending PVCoordinates
133      */
134     public FieldPVCoordinates(final FieldPVCoordinates<T> start, final FieldPVCoordinates<T> end) {
135         this.position     = end.position.subtract(start.position);
136         this.velocity     = end.velocity.subtract(start.velocity);
137         this.acceleration = end.acceleration.subtract(start.acceleration);
138     }
139 
140     /** Linear constructor.
141      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
142      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
143      * @param a1 first scale factor
144      * @param pv1 first base (unscaled) PVCoordinates
145      * @param a2 second scale factor
146      * @param pv2 second base (unscaled) PVCoordinates
147      */
148     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
149                               final double a2, final FieldPVCoordinates<T> pv2) {
150         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
151         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
152         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
153     }
154 
155     /** Linear constructor.
156      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
157      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
158      * @param a1 first scale factor
159      * @param pv1 first base (unscaled) PVCoordinates
160      * @param a2 second scale factor
161      * @param pv2 second base (unscaled) PVCoordinates
162      */
163     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
164                               final T a2, final FieldPVCoordinates<T> pv2) {
165         position     = new FieldVector3D<>(a1, pv1.position, a2, pv2.position);
166         velocity     = new FieldVector3D<>(a1, pv1.velocity, a2, pv2.velocity);
167         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration);
168     }
169 
170     /** Linear constructor.
171      * <p>Build a PVCoordinates from two other ones and corresponding scale factors.</p>
172      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2</p>
173      * @param a1 first scale factor
174      * @param pv1 first base (unscaled) PVCoordinates
175      * @param a2 second scale factor
176      * @param pv2 second base (unscaled) PVCoordinates
177      */
178     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
179                               final T a2, final PVCoordinates pv2) {
180         position     = new FieldVector3D<>(a1, pv1.getPosition(), a2, pv2.getPosition());
181         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(), a2, pv2.getVelocity());
182         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration());
183     }
184 
185     /** Linear constructor.
186      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
187      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
188      * @param a1 first scale factor
189      * @param pv1 first base (unscaled) PVCoordinates
190      * @param a2 second scale factor
191      * @param pv2 second base (unscaled) PVCoordinates
192      * @param a3 third scale factor
193      * @param pv3 third base (unscaled) PVCoordinates
194      */
195     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
196                               final double a2, final FieldPVCoordinates<T> pv2,
197                               final double a3, final FieldPVCoordinates<T> pv3) {
198         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
199         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
200         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
201     }
202 
203     /** Linear constructor.
204      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
205      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
206      * @param a1 first scale factor
207      * @param pv1 first base (unscaled) PVCoordinates
208      * @param a2 second scale factor
209      * @param pv2 second base (unscaled) PVCoordinates
210      * @param a3 third scale factor
211      * @param pv3 third base (unscaled) PVCoordinates
212      */
213     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
214                               final T a2, final FieldPVCoordinates<T> pv2,
215                               final T a3, final FieldPVCoordinates<T> pv3) {
216         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position);
217         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity);
218         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration);
219     }
220 
221     /** Linear constructor.
222      * <p>Build a PVCoordinates from three other ones and corresponding scale factors.</p>
223      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3</p>
224      * @param a1 first scale factor
225      * @param pv1 first base (unscaled) PVCoordinates
226      * @param a2 second scale factor
227      * @param pv2 second base (unscaled) PVCoordinates
228      * @param a3 third scale factor
229      * @param pv3 third base (unscaled) PVCoordinates
230      */
231     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
232                               final T a2, final PVCoordinates pv2,
233                               final T a3, final PVCoordinates pv3) {
234         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),     a3, pv3.getPosition());
235         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),     a3, pv3.getVelocity());
236         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(), a3, pv3.getAcceleration());
237     }
238 
239     /** Linear constructor.
240      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
241      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
242      * @param a1 first scale factor
243      * @param pv1 first base (unscaled) PVCoordinates
244      * @param a2 second scale factor
245      * @param pv2 second base (unscaled) PVCoordinates
246      * @param a3 third scale factor
247      * @param pv3 third base (unscaled) PVCoordinates
248      * @param a4 fourth scale factor
249      * @param pv4 fourth base (unscaled) PVCoordinates
250      */
251     public FieldPVCoordinates(final double a1, final FieldPVCoordinates<T> pv1,
252                               final double a2, final FieldPVCoordinates<T> pv2,
253                               final double a3, final FieldPVCoordinates<T> pv3,
254                               final double a4, final FieldPVCoordinates<T> pv4) {
255         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
256         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
257         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
258     }
259 
260     /** Linear constructor.
261      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
262      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
263      * @param a1 first scale factor
264      * @param pv1 first base (unscaled) PVCoordinates
265      * @param a2 second scale factor
266      * @param pv2 second base (unscaled) PVCoordinates
267      * @param a3 third scale factor
268      * @param pv3 third base (unscaled) PVCoordinates
269      * @param a4 fourth scale factor
270      * @param pv4 fourth base (unscaled) PVCoordinates
271      */
272     public FieldPVCoordinates(final T a1, final FieldPVCoordinates<T> pv1,
273                               final T a2, final FieldPVCoordinates<T> pv2,
274                               final T a3, final FieldPVCoordinates<T> pv3,
275                               final T a4, final FieldPVCoordinates<T> pv4) {
276         position     = new FieldVector3D<>(a1, pv1.position,     a2, pv2.position,     a3, pv3.position,     a4, pv4.position);
277         velocity     = new FieldVector3D<>(a1, pv1.velocity,     a2, pv2.velocity,     a3, pv3.velocity,     a4, pv4.velocity);
278         acceleration = new FieldVector3D<>(a1, pv1.acceleration, a2, pv2.acceleration, a3, pv3.acceleration, a4, pv4.acceleration);
279     }
280 
281     /** Linear constructor.
282      * <p>Build a PVCoordinates from four other ones and corresponding scale factors.</p>
283      * <p>The PVCoordinates built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4</p>
284      * @param a1 first scale factor
285      * @param pv1 first base (unscaled) PVCoordinates
286      * @param a2 second scale factor
287      * @param pv2 second base (unscaled) PVCoordinates
288      * @param a3 third scale factor
289      * @param pv3 third base (unscaled) PVCoordinates
290      * @param a4 fourth scale factor
291      * @param pv4 fourth base (unscaled) PVCoordinates
292      */
293     public FieldPVCoordinates(final T a1, final PVCoordinates pv1,
294                               final T a2, final PVCoordinates pv2,
295                               final T a3, final PVCoordinates pv3,
296                               final T a4, final PVCoordinates pv4) {
297         position     = new FieldVector3D<>(a1, pv1.getPosition(),     a2, pv2.getPosition(),
298                                            a3, pv3.getPosition(),     a4, pv4.getPosition());
299         velocity     = new FieldVector3D<>(a1, pv1.getVelocity(),     a2, pv2.getVelocity(),
300                                            a3, pv3.getVelocity(),     a4, pv4.getVelocity());
301         acceleration = new FieldVector3D<>(a1, pv1.getAcceleration(), a2, pv2.getAcceleration(),
302                                            a3, pv3.getAcceleration(), a4, pv4.getAcceleration());
303     }
304 
305     /** Builds a FieldPVCoordinates triplet from  a {@link FieldVector3D}&lt;{@link FieldDerivative}&gt;.
306      * <p>
307      * The vector components must have time as their only derivation parameter and
308      * have consistent derivation orders.
309      * </p>
310      * @param p vector with time-derivatives embedded within the coordinates
311      * @param <U> type of the derivative
312      * @since 9.2
313      */
314     public <U extends FieldDerivative<T, U>> FieldPVCoordinates(final FieldVector3D<U> p) {
315         position = new FieldVector3D<>(p.getX().getValue(), p.getY().getValue(), p.getZ().getValue());
316         if (p.getX().getOrder() >= 1) {
317             velocity = new FieldVector3D<>(p.getX().getPartialDerivative(1),
318                                            p.getY().getPartialDerivative(1),
319                                            p.getZ().getPartialDerivative(1));
320             if (p.getX().getOrder() >= 2) {
321                 acceleration = new FieldVector3D<>(p.getX().getPartialDerivative(2),
322                                                    p.getY().getPartialDerivative(2),
323                                                    p.getZ().getPartialDerivative(2));
324             } else {
325                 acceleration = FieldVector3D.getZero(position.getX().getField());
326             }
327         } else {
328             final FieldVector3D<T> zero = FieldVector3D.getZero(position.getX().getField());
329             velocity     = zero;
330             acceleration = zero;
331         }
332     }
333 
334     /** Get fixed position/velocity at origin (both p, v and a are zero vectors).
335      * @param field field for the components
336      * @param <T> the type of the field elements
337      * @return a new fixed position/velocity at origin
338      */
339     public static <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getZero(final Field<T> field) {
340         return new FieldPVCoordinates<>(field, PVCoordinates.ZERO);
341     }
342 
343     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldDerivativeStructure}&gt;.
344      * <p>
345      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
346      * to the user-specified order.
347      * </p>
348      * @param order derivation order for the vector components (must be either 0, 1 or 2)
349      * @return vector with time-derivatives embedded within the coordinates
350           * @since 9.2
351      */
352     public FieldVector3D<FieldDerivativeStructure<T>> toDerivativeStructureVector(final int order) {
353 
354         final FDSFactory<T> factory;
355         final FieldDerivativeStructure<T> x;
356         final FieldDerivativeStructure<T> y;
357         final FieldDerivativeStructure<T> z;
358         switch (order) {
359             case 0 :
360                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
361                 x = factory.build(position.getX());
362                 y = factory.build(position.getY());
363                 z = factory.build(position.getZ());
364                 break;
365             case 1 :
366                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
367                 x = factory.build(position.getX(), velocity.getX());
368                 y = factory.build(position.getY(), velocity.getY());
369                 z = factory.build(position.getZ(), velocity.getZ());
370                 break;
371             case 2 :
372                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
373                 x = factory.build(position.getX(), velocity.getX(), acceleration.getX());
374                 y = factory.build(position.getY(), velocity.getY(), acceleration.getY());
375                 z = factory.build(position.getZ(), velocity.getZ(), acceleration.getZ());
376                 break;
377             default :
378                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
379         }
380 
381         return new FieldVector3D<>(x, y, z);
382 
383     }
384 
385     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative1}&gt;.
386      * <p>
387      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
388      * to the order 1.
389      * </p>
390      * @return vector with time-derivatives embedded within the coordinates
391      * @see #toUnivariateDerivative2Vector()
392      * @since 10.2
393      */
394     public FieldVector3D<FieldUnivariateDerivative1<T>> toUnivariateDerivative1Vector() {
395 
396         final FieldUnivariateDerivative1<T> x = new FieldUnivariateDerivative1<>(position.getX(), velocity.getX());
397         final FieldUnivariateDerivative1<T> y = new FieldUnivariateDerivative1<>(position.getY(), velocity.getY());
398         final FieldUnivariateDerivative1<T> z = new FieldUnivariateDerivative1<>(position.getZ(), velocity.getZ());
399 
400         return new FieldVector3D<>(x, y, z);
401     }
402 
403     /** Transform the instance to a {@link FieldVector3D}&lt;{@link FieldUnivariateDerivative2}&gt;.
404      * <p>
405      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
406      * to the order 2.
407      * </p>
408      * @return vector with time-derivatives embedded within the coordinates
409      * @see #toUnivariateDerivative1Vector()
410      * @since 10.2
411      */
412     public FieldVector3D<FieldUnivariateDerivative2<T>> toUnivariateDerivative2Vector() {
413 
414         final FieldUnivariateDerivative2<T> x = new FieldUnivariateDerivative2<>(position.getX(), velocity.getX(), acceleration.getX());
415         final FieldUnivariateDerivative2<T> y = new FieldUnivariateDerivative2<>(position.getY(), velocity.getY(), acceleration.getY());
416         final FieldUnivariateDerivative2<T> z = new FieldUnivariateDerivative2<>(position.getZ(), velocity.getZ(), acceleration.getZ());
417 
418         return new FieldVector3D<>(x, y, z);
419     }
420 
421     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldDerivativeStructure}&gt;.
422      * <p>
423      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
424      * to the user-specified order. As both the instance components {@link #getPosition() position},
425      * {@link #getVelocity() velocity} and {@link #getAcceleration() acceleration} and the
426      * {@link FieldDerivativeStructure#getPartialDerivative(int...) derivatives} of the components
427      * holds time-derivatives, there are several ways to retrieve these derivatives. If for example
428      * the {@code order} is set to 2, then both {@code pv.getPosition().getX().getPartialDerivative(2)},
429      * {@code pv.getVelocity().getX().getPartialDerivative(1)} and
430      * {@code pv.getAcceleration().getX().getValue()} return the exact same value.
431      * </p>
432      * <p>
433      * If derivation order is 1, the first derivative of acceleration will be computed as a
434      * Keplerian-only jerk. If derivation order is 2, the second derivative of velocity (which
435      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
436      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
437      * </p>
438      * @param order derivation order for the vector components (must be either 0, 1 or 2)
439      * @return pv coordinates with time-derivatives embedded within the coordinates
440           * @since 9.2
441      */
442     public FieldPVCoordinates<FieldDerivativeStructure<T>> toDerivativeStructurePV(final int order) {
443 
444         final FDSFactory<T> factory;
445         final FieldDerivativeStructure<T> x0;
446         final FieldDerivativeStructure<T> y0;
447         final FieldDerivativeStructure<T> z0;
448         final FieldDerivativeStructure<T> x1;
449         final FieldDerivativeStructure<T> y1;
450         final FieldDerivativeStructure<T> z1;
451         final FieldDerivativeStructure<T> x2;
452         final FieldDerivativeStructure<T> y2;
453         final FieldDerivativeStructure<T> z2;
454         switch (order) {
455             case 0 :
456                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
457                 x0 = factory.build(position.getX());
458                 y0 = factory.build(position.getY());
459                 z0 = factory.build(position.getZ());
460                 x1 = factory.build(velocity.getX());
461                 y1 = factory.build(velocity.getY());
462                 z1 = factory.build(velocity.getZ());
463                 x2 = factory.build(acceleration.getX());
464                 y2 = factory.build(acceleration.getY());
465                 z2 = factory.build(acceleration.getZ());
466                 break;
467             case 1 : {
468                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
469                 final T                r2            = position.getNorm2Sq();
470                 final T                r             = r2.sqrt();
471                 final T                pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
472                 final T                a             = acceleration.getNorm();
473                 final T                aOr           = a.divide(r);
474                 final FieldVector3D<T> keplerianJerk = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
475                                                                            aOr.negate(), velocity);
476                 x0 = factory.build(position.getX(),     velocity.getX());
477                 y0 = factory.build(position.getY(),     velocity.getY());
478                 z0 = factory.build(position.getZ(),     velocity.getZ());
479                 x1 = factory.build(velocity.getX(),     acceleration.getX());
480                 y1 = factory.build(velocity.getY(),     acceleration.getY());
481                 z1 = factory.build(velocity.getZ(),     acceleration.getZ());
482                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX());
483                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY());
484                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ());
485                 break;
486             }
487             case 2 : {
488                 factory = new FDSFactory<>(getPosition().getX().getField(), 1, order);
489                 final T                r2              = position.getNorm2Sq();
490                 final T                r               = r2.sqrt();
491                 final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
492                 final T                a               = acceleration.getNorm();
493                 final T                aOr             = a.divide(r);
494                 final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
495                                                                              aOr.negate(), velocity);
496                 final T                v2              = velocity.getNorm2Sq();
497                 final T                pa              = FieldVector3D.dotProduct(position, acceleration);
498                 final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
499                 final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
500                                                                              aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
501                 x0 = factory.build(position.getX(),     velocity.getX(),      acceleration.getX());
502                 y0 = factory.build(position.getY(),     velocity.getY(),      acceleration.getY());
503                 z0 = factory.build(position.getZ(),     velocity.getZ(),      acceleration.getZ());
504                 x1 = factory.build(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
505                 y1 = factory.build(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
506                 z1 = factory.build(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
507                 x2 = factory.build(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
508                 y2 = factory.build(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
509                 z2 = factory.build(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
510                 break;
511             }
512             default :
513                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
514         }
515 
516         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
517                                         new FieldVector3D<>(x1, y1, z1),
518                                         new FieldVector3D<>(x2, y2, z2));
519 
520     }
521 
522 
523     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative1}&gt;.
524      * <p>
525      * The {@link FieldUnivariateDerivative1} coordinates correspond to time-derivatives up
526      * to the order 1.
527      * The first derivative of acceleration will be computed as a Keplerian-only jerk.
528      * </p>
529      * @return pv coordinates with time-derivatives embedded within the coordinates
530      * @since 10.2
531      */
532     public FieldPVCoordinates<FieldUnivariateDerivative1<T>> toUnivariateDerivative1PV() {
533 
534         final T   r2            = position.getNorm2Sq();
535         final T   r             = FastMath.sqrt(r2);
536         final T   pvOr2         = FieldVector3D.dotProduct(position, velocity).divide(r2);
537         final T   a             = acceleration.getNorm();
538         final T   aOr           = a.divide(r);
539         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
540                                                                      aOr.negate(), velocity);
541 
542         final FieldUnivariateDerivative1<T> x0 = new FieldUnivariateDerivative1<>(position.getX(),     velocity.getX());
543         final FieldUnivariateDerivative1<T> y0 = new FieldUnivariateDerivative1<>(position.getY(),     velocity.getY());
544         final FieldUnivariateDerivative1<T> z0 = new FieldUnivariateDerivative1<>(position.getZ(),     velocity.getZ());
545         final FieldUnivariateDerivative1<T> x1 = new FieldUnivariateDerivative1<>(velocity.getX(),     acceleration.getX());
546         final FieldUnivariateDerivative1<T> y1 = new FieldUnivariateDerivative1<>(velocity.getY(),     acceleration.getY());
547         final FieldUnivariateDerivative1<T> z1 = new FieldUnivariateDerivative1<>(velocity.getZ(),     acceleration.getZ());
548         final FieldUnivariateDerivative1<T> x2 = new FieldUnivariateDerivative1<>(acceleration.getX(), keplerianJerk.getX());
549         final FieldUnivariateDerivative1<T> y2 = new FieldUnivariateDerivative1<>(acceleration.getY(), keplerianJerk.getY());
550         final FieldUnivariateDerivative1<T> z2 = new FieldUnivariateDerivative1<>(acceleration.getZ(), keplerianJerk.getZ());
551 
552         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
553                                         new FieldVector3D<>(x1, y1, z1),
554                                         new FieldVector3D<>(x2, y2, z2));
555 
556     }
557 
558     /** Transform the instance to a {@link FieldPVCoordinates}&lt;{@link FieldUnivariateDerivative2}&gt;.
559      * <p>
560      * The {@link FieldUnivariateDerivative2} coordinates correspond to time-derivatives up
561      * to the order 2.
562      * As derivation order is 2, the second derivative of velocity (which
563      * is also the first derivative of acceleration) will be computed as a Keplerian-only jerk,
564      * and the second derivative of acceleration will be computed as a Keplerian-only jounce.
565      * </p>
566      * @return pv coordinates with time-derivatives embedded within the coordinates
567      * @since 10.2
568      */
569     public FieldPVCoordinates<FieldUnivariateDerivative2<T>> toUnivariateDerivative2PV() {
570 
571         final T                r2              = position.getNorm2Sq();
572         final T                r               = r2.sqrt();
573         final T                pvOr2           = FieldVector3D.dotProduct(position, velocity).divide(r2);
574         final T                a               = acceleration.getNorm();
575         final T                aOr             = a.divide(r);
576         final FieldVector3D<T> keplerianJerk   = new FieldVector3D<>(pvOr2.multiply(-3), acceleration,
577                                                                      aOr.negate(), velocity);
578         final T                v2              = velocity.getNorm2Sq();
579         final T                pa              = FieldVector3D.dotProduct(position, acceleration);
580         final T                aj              = FieldVector3D.dotProduct(acceleration, keplerianJerk);
581         final FieldVector3D<T> keplerianJounce = new FieldVector3D<>(v2.add(pa).multiply(-3).divide(r2).add(pvOr2.multiply(pvOr2).multiply(15)).subtract(aOr), acceleration,
582                                                                      aOr.multiply(4).multiply(pvOr2).subtract(aj.divide(a.multiply(r))), velocity);
583 
584         final FieldUnivariateDerivative2<T> x0 = new FieldUnivariateDerivative2<>(position.getX(),     velocity.getX(),      acceleration.getX());
585         final FieldUnivariateDerivative2<T> y0 = new FieldUnivariateDerivative2<>(position.getY(),     velocity.getY(),      acceleration.getY());
586         final FieldUnivariateDerivative2<T> z0 = new FieldUnivariateDerivative2<>(position.getZ(),     velocity.getZ(),      acceleration.getZ());
587         final FieldUnivariateDerivative2<T> x1 = new FieldUnivariateDerivative2<>(velocity.getX(),     acceleration.getX(),  keplerianJerk.getX());
588         final FieldUnivariateDerivative2<T> y1 = new FieldUnivariateDerivative2<>(velocity.getY(),     acceleration.getY(),  keplerianJerk.getY());
589         final FieldUnivariateDerivative2<T> z1 = new FieldUnivariateDerivative2<>(velocity.getZ(),     acceleration.getZ(),  keplerianJerk.getZ());
590         final FieldUnivariateDerivative2<T> x2 = new FieldUnivariateDerivative2<>(acceleration.getX(), keplerianJerk.getX(), keplerianJounce.getX());
591         final FieldUnivariateDerivative2<T> y2 = new FieldUnivariateDerivative2<>(acceleration.getY(), keplerianJerk.getY(), keplerianJounce.getY());
592         final FieldUnivariateDerivative2<T> z2 = new FieldUnivariateDerivative2<>(acceleration.getZ(), keplerianJerk.getZ(), keplerianJounce.getZ());
593 
594         return new FieldPVCoordinates<>(new FieldVector3D<>(x0, y0, z0),
595                                         new FieldVector3D<>(x1, y1, z1),
596                                         new FieldVector3D<>(x2, y2, z2));
597 
598     }
599 
600     /** Estimate velocity between two positions.
601      * <p>Estimation is based on a simple fixed velocity translation
602      * during the time interval between the two positions.</p>
603      * @param start start position
604      * @param end end position
605      * @param dt time elapsed between the dates of the two positions
606      * @param <T> the type of the field elements
607      * @return velocity allowing to go from start to end positions
608      */
609     public static <T extends CalculusFieldElement<T>> FieldVector3D<T> estimateVelocity(final FieldVector3D<T> start,
610                                                                                     final FieldVector3D<T> end,
611                                                                                     final double dt) {
612         final double scale = 1.0 / dt;
613         return new FieldVector3D<>(scale, end, -scale, start);
614     }
615 
616     /** Get a time-shifted state.
617      * <p>
618      * The state can be slightly shifted to close dates. This shift is based on
619      * a simple quadratic model. It is <em>not</em> intended as a replacement for
620      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
621      * for either small time shifts or coarse accuracy.
622      * </p>
623      * @param dt time shift in seconds
624      * @return a new state, shifted with respect to the instance (which is immutable)
625      */
626     @Override
627     public FieldPVCoordinates<T> shiftedBy(final double dt) {
628         return new FieldPVCoordinates<>(new FieldVector3D<>(1, position, dt, velocity, 0.5 * dt * dt, acceleration),
629                                         new FieldVector3D<>(1, velocity, dt, acceleration),
630                                         acceleration);
631     }
632 
633     /** Get a time-shifted state.
634      * <p>
635      * The state can be slightly shifted to close dates. This shift is based on
636      * a simple quadratic model. It is <em>not</em> intended as a replacement for
637      * proper orbit propagation (it is not even Keplerian!) but should be sufficient
638      * for either small time shifts or coarse accuracy.
639      * </p>
640      * @param dt time shift in seconds
641      * @return a new state, shifted with respect to the instance (which is immutable)
642      */
643     @Override
644     public FieldPVCoordinates<T> shiftedBy(final T dt) {
645         final T one = dt.getField().getOne();
646         return new FieldPVCoordinates<>(positionShiftedBy(dt),
647                                         new FieldVector3D<>(one, velocity, dt, acceleration),
648                                         acceleration);
649     }
650 
651     /**
652      * Get a time-shifted position. Same as {@link #shiftedBy(CalculusFieldElement)} except
653      * that only the sifted position is returned.
654      * <p>
655      * The state can be slightly shifted to close dates. This shift is based on
656      * a simple Taylor expansion. It is <em>not</em> intended as a replacement
657      * for proper orbit propagation (it is not even Keplerian!) but should be
658      * sufficient for either small time shifts or coarse accuracy.
659      * </p>
660      *
661      * @param dt time shift in seconds
662      * @return a new state, shifted with respect to the instance (which is
663      * immutable)
664      * @since 11.2
665      */
666     public FieldVector3D<T> positionShiftedBy(final T dt) {
667         final T one = dt.getField().getOne();
668         return new FieldVector3D<>(one, position, dt, velocity, dt.square().multiply(0.5), acceleration);
669     }
670 
671     /** Gets the position.
672      * @return the position vector (m).
673      */
674     public FieldVector3D<T> getPosition() {
675         return position;
676     }
677 
678     /** Gets the velocity.
679      * @return the velocity vector (m/s).
680      */
681     public FieldVector3D<T> getVelocity() {
682         return velocity;
683     }
684 
685     /** Gets the acceleration.
686      * @return the acceleration vector (m/s²).
687      */
688     public FieldVector3D<T> getAcceleration() {
689         return acceleration;
690     }
691 
692     /** Gets the momentum.
693      * <p>This vector is the p &otimes; v where p is position, v is velocity
694      * and &otimes; is cross product. To get the real physical angular momentum
695      * you need to multiply this vector by the mass.</p>
696      * <p>The returned vector is recomputed each time this method is called, it
697      * is not cached.</p>
698      * @return a new instance of the momentum vector (m²/s).
699      */
700     public FieldVector3D<T> getMomentum() {
701         return FieldVector3D.crossProduct(position, velocity);
702     }
703 
704     /**
705      * Get the angular velocity (spin) of this point as seen from the origin.
706      *
707      * <p> The angular velocity vector is parallel to the {@link #getMomentum()
708      * angular * momentum} and is computed by ω = p &times; v / ||p||²
709      *
710      * @return the angular velocity vector
711      * @see <a href="https://en.wikipedia.org/wiki/Angular_velocity">Angular Velocity on
712      *      Wikipedia</a>
713      */
714     public FieldVector3D<T> getAngularVelocity() {
715         return this.getMomentum().scalarMultiply(
716                 this.getPosition().getNorm2Sq().reciprocal());
717     }
718 
719     /** Get the opposite of the instance.
720      * @return a new position-velocity which is opposite to the instance
721      */
722     public FieldPVCoordinates<T> negate() {
723         return new FieldPVCoordinates<>(position.negate(), velocity.negate(), acceleration.negate());
724     }
725 
726     /** Normalize the position part of the instance.
727      * <p>
728      * The computed coordinates first component (position) will be a
729      * normalized vector, the second component (velocity) will be the
730      * derivative of the first component (hence it will generally not
731      * be normalized), and the third component (acceleration) will be the
732      * derivative of the second component (hence it will generally not
733      * be normalized).
734      * </p>
735      * @return a new instance, with first component normalized and
736      * remaining component computed to have consistent derivatives
737      */
738     public FieldPVCoordinates<T> normalize() {
739         final T   inv     = position.getNorm().reciprocal();
740         final FieldVector3D<T> u       = new FieldVector3D<>(inv, position);
741         final FieldVector3D<T> v       = new FieldVector3D<>(inv, velocity);
742         final FieldVector3D<T> w       = new FieldVector3D<>(inv, acceleration);
743         final T   uv      = FieldVector3D.dotProduct(u, v);
744         final T   v2      = FieldVector3D.dotProduct(v, v);
745         final T   uw      = FieldVector3D.dotProduct(u, w);
746         final FieldVector3D<T> uDot    = new FieldVector3D<>(inv.getField().getOne(), v,
747                                                              uv.multiply(-1), u);
748         final FieldVector3D<T> uDotDot = new FieldVector3D<>(inv.getField().getOne(), w,
749                                                              uv.multiply(-2), v,
750                                                              uv.multiply(uv).multiply(3).subtract(v2).subtract(uw), u);
751         return new FieldPVCoordinates<>(u, uDot, uDotDot);
752     }
753 
754     /** Compute the cross-product of two instances.
755      * @param pv2 second instances
756      * @return the cross product v1 ^ v2 as a new instance
757      */
758     public FieldPVCoordinates<T> crossProduct(final FieldPVCoordinates<T> pv2) {
759         final FieldVector3D<T> p1 = position;
760         final FieldVector3D<T> v1 = velocity;
761         final FieldVector3D<T> a1 = acceleration;
762         final FieldVector3D<T> p2 = pv2.position;
763         final FieldVector3D<T> v2 = pv2.velocity;
764         final FieldVector3D<T> a2 = pv2.acceleration;
765         return new FieldPVCoordinates<>(FieldVector3D.crossProduct(p1, p2),
766                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, v2),
767                                                             1, FieldVector3D.crossProduct(v1, p2)),
768                                         new FieldVector3D<>(1, FieldVector3D.crossProduct(p1, a2),
769                                                             2, FieldVector3D.crossProduct(v1, v2),
770                                                             1, FieldVector3D.crossProduct(a1, p2)));
771     }
772 
773     /** Convert to a constant position-velocity.
774      * @return a constant position-velocity
775      */
776     public PVCoordinates toPVCoordinates() {
777         return new PVCoordinates(position.toVector3D(), velocity.toVector3D(), acceleration.toVector3D());
778     }
779 
780     /** Return a string representation of this position/velocity pair.
781      * @return string representation of this position/velocity pair
782      */
783     public String toString() {
784         final String comma = ", ";
785         return new StringBuilder().append('{').append("P(").
786                                   append(position.getX().getReal()).append(comma).
787                                   append(position.getY().getReal()).append(comma).
788                                   append(position.getZ().getReal()).append("), V(").
789                                   append(velocity.getX().getReal()).append(comma).
790                                   append(velocity.getY().getReal()).append(comma).
791                                   append(velocity.getZ().getReal()).append("), A(").
792                                   append(acceleration.getX().getReal()).append(comma).
793                                   append(acceleration.getY().getReal()).append(comma).
794                                   append(acceleration.getZ().getReal()).append(")}").toString();
795     }
796 
797     /** {@inheritDoc} */
798     @Override
799     public FieldPVCoordinates<T> blendArithmeticallyWith(final FieldPVCoordinates<T> other,
800                                                          final T blendingValue)
801             throws MathIllegalArgumentException {
802         final FieldVector3D<T> blendedPosition     = position.blendArithmeticallyWith(other.getPosition(), blendingValue);
803         final FieldVector3D<T> blendedVelocity     = velocity.blendArithmeticallyWith(other.getVelocity(), blendingValue);
804         final FieldVector3D<T> blendedAcceleration = acceleration.blendArithmeticallyWith(other.getAcceleration(), blendingValue);
805 
806         return new FieldPVCoordinates<>(blendedPosition, blendedVelocity, blendedAcceleration);
807     }
808 }