FieldAngularCoordinates.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.utils;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.analysis.differentiation.FDSFactory;
  21. import org.hipparchus.analysis.differentiation.FieldDerivative;
  22. import org.hipparchus.analysis.differentiation.FieldDerivativeStructure;
  23. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative1;
  24. import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
  25. import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
  26. import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
  27. import org.hipparchus.exception.LocalizedCoreFormats;
  28. import org.hipparchus.exception.MathIllegalArgumentException;
  29. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  30. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  31. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  32. import org.hipparchus.linear.FieldDecompositionSolver;
  33. import org.hipparchus.linear.FieldMatrix;
  34. import org.hipparchus.linear.FieldQRDecomposition;
  35. import org.hipparchus.linear.FieldVector;
  36. import org.hipparchus.linear.MatrixUtils;
  37. import org.hipparchus.util.MathArrays;
  38. import org.orekit.errors.OrekitException;
  39. import org.orekit.errors.OrekitMessages;

  40. /** Simple container for rotation / rotation rate pairs, using {@link
  41.  * CalculusFieldElement}.
  42.  * <p>
  43.  * The state can be slightly shifted to close dates. This shift is based on
  44.  * a simple quadratic model. It is <em>not</em> intended as a replacement for
  45.  * proper attitude propagation but should be sufficient for either small
  46.  * time shifts or coarse accuracy.
  47.  * </p>
  48.  * <p>
  49.  * This class is the angular counterpart to {@link FieldPVCoordinates}.
  50.  * </p>
  51.  * <p>Instances of this class are guaranteed to be immutable.</p>
  52.  * @param <T> the type of the field elements
  53.  * @author Luc Maisonobe
  54.  * @since 6.0
  55.  * @see AngularCoordinates
  56.  */
  57. public class FieldAngularCoordinates<T extends CalculusFieldElement<T>> {


  58.     /** rotation. */
  59.     private final FieldRotation<T> rotation;

  60.     /** rotation rate. */
  61.     private final FieldVector3D<T> rotationRate;

  62.     /** rotation acceleration. */
  63.     private final FieldVector3D<T> rotationAcceleration;

  64.     /** Builds a rotation/rotation rate pair.
  65.      * @param rotation rotation
  66.      * @param rotationRate rotation rate Ω (rad/s)
  67.      */
  68.     public FieldAngularCoordinates(final FieldRotation<T> rotation,
  69.                                    final FieldVector3D<T> rotationRate) {
  70.         this(rotation, rotationRate,
  71.              new FieldVector3D<>(rotation.getQ0().getField().getZero(),
  72.                                  rotation.getQ0().getField().getZero(),
  73.                                  rotation.getQ0().getField().getZero()));
  74.     }

  75.     /** Builds a rotation / rotation rate / rotation acceleration triplet.
  76.      * @param rotation i.e. the orientation of the vehicle
  77.      * @param rotationRate rotation rate rate Ω, i.e. the spin vector (rad/s)
  78.      * @param rotationAcceleration angular acceleration vector dΩ/dt (rad/s²)
  79.      */
  80.     public FieldAngularCoordinates(final FieldRotation<T> rotation,
  81.                                    final FieldVector3D<T> rotationRate,
  82.                                    final FieldVector3D<T> rotationAcceleration) {
  83.         this.rotation             = rotation;
  84.         this.rotationRate         = rotationRate;
  85.         this.rotationAcceleration = rotationAcceleration;
  86.     }

  87.     /** Build the rotation that transforms a pair of pv coordinates into another one.

  88.      * <p><em>WARNING</em>! This method requires much more stringent assumptions on
  89.      * its parameters than the similar {@link FieldRotation#FieldRotation(FieldVector3D, FieldVector3D,
  90.      * FieldVector3D, FieldVector3D) constructor} from the {@link FieldRotation FieldRotation} class.
  91.      * As far as the FieldRotation constructor is concerned, the {@code v₂} vector from
  92.      * the second pair can be slightly misaligned. The FieldRotation constructor will
  93.      * compensate for this misalignment and create a rotation that ensure {@code
  94.      * v₁ = r(u₁)} and {@code v₂ ∈ plane (r(u₁), r(u₂))}. <em>THIS IS NOT
  95.      * TRUE ANYMORE IN THIS CLASS</em>! As derivatives are involved and must be
  96.      * preserved, this constructor works <em>only</em> if the two pairs are fully
  97.      * consistent, i.e. if a rotation exists that fulfill all the requirements: {@code
  98.      * v₁ = r(u₁)}, {@code v₂ = r(u₂)}, {@code dv₁/dt = dr(u₁)/dt}, {@code dv₂/dt
  99.      * = dr(u₂)/dt}, {@code d²v₁/dt² = d²r(u₁)/dt²}, {@code d²v₂/dt² = d²r(u₂)/dt²}.</p>
  100.      * @param u1 first vector of the origin pair
  101.      * @param u2 second vector of the origin pair
  102.      * @param v1 desired image of u1 by the rotation
  103.      * @param v2 desired image of u2 by the rotation
  104.      * @param tolerance relative tolerance factor used to check singularities
  105.      */
  106.     public FieldAngularCoordinates(final FieldPVCoordinates<T> u1, final FieldPVCoordinates<T> u2,
  107.                                    final FieldPVCoordinates<T> v1, final FieldPVCoordinates<T> v2,
  108.                                    final double tolerance) {

  109.         try {
  110.             // find the initial fixed rotation
  111.             rotation = new FieldRotation<>(u1.getPosition(), u2.getPosition(),
  112.                                            v1.getPosition(), v2.getPosition());

  113.             // find rotation rate Ω such that
  114.             //  Ω ⨯ v₁ = r(dot(u₁)) - dot(v₁)
  115.             //  Ω ⨯ v₂ = r(dot(u₂)) - dot(v₂)
  116.             final FieldVector3D<T> ru1Dot = rotation.applyTo(u1.getVelocity());
  117.             final FieldVector3D<T> ru2Dot = rotation.applyTo(u2.getVelocity());


  118.             rotationRate = inverseCrossProducts(v1.getPosition(), ru1Dot.subtract(v1.getVelocity()),
  119.                                                 v2.getPosition(), ru2Dot.subtract(v2.getVelocity()),
  120.                                                 tolerance);


  121.             // find rotation acceleration dot(Ω) such that
  122.             // dot(Ω) ⨯ v₁ = r(dotdot(u₁)) - 2 Ω ⨯ dot(v₁) - Ω ⨯  (Ω ⨯ v₁) - dotdot(v₁)
  123.             // dot(Ω) ⨯ v₂ = r(dotdot(u₂)) - 2 Ω ⨯ dot(v₂) - Ω ⨯  (Ω ⨯ v₂) - dotdot(v₂)
  124.             final FieldVector3D<T> ru1DotDot = rotation.applyTo(u1.getAcceleration());
  125.             final FieldVector3D<T> oDotv1    = FieldVector3D.crossProduct(rotationRate, v1.getVelocity());
  126.             final FieldVector3D<T> oov1      = FieldVector3D.crossProduct(rotationRate, rotationRate.crossProduct(v1.getPosition()));
  127.             final FieldVector3D<T> c1        = new FieldVector3D<>(1, ru1DotDot, -2, oDotv1, -1, oov1, -1, v1.getAcceleration());
  128.             final FieldVector3D<T> ru2DotDot = rotation.applyTo(u2.getAcceleration());
  129.             final FieldVector3D<T> oDotv2    = FieldVector3D.crossProduct(rotationRate, v2.getVelocity());
  130.             final FieldVector3D<T> oov2      = FieldVector3D.crossProduct(rotationRate, rotationRate.crossProduct( v2.getPosition()));
  131.             final FieldVector3D<T> c2        = new FieldVector3D<>(1, ru2DotDot, -2, oDotv2, -1, oov2, -1, v2.getAcceleration());
  132.             rotationAcceleration     = inverseCrossProducts(v1.getPosition(), c1, v2.getPosition(), c2, tolerance);

  133.         } catch (MathIllegalArgumentException miae) {
  134.             throw new OrekitException(miae);
  135.         }

  136.     }

  137.     /** Builds a FieldAngularCoordinates from a field and a regular AngularCoordinates.
  138.      * @param field field for the components
  139.      * @param ang AngularCoordinates to convert
  140.      */
  141.     public FieldAngularCoordinates(final Field<T> field, final AngularCoordinates ang) {
  142.         this.rotation             = new FieldRotation<>(field, ang.getRotation());
  143.         this.rotationRate         = new FieldVector3D<>(field, ang.getRotationRate());
  144.         this.rotationAcceleration = new FieldVector3D<>(field, ang.getRotationAcceleration());
  145.     }

  146.     /** Builds a FieldAngularCoordinates from  a {@link FieldRotation}&lt;{@link FieldDerivativeStructure}&gt;.
  147.      * <p>
  148.      * The rotation components must have time as their only derivation parameter and
  149.      * have consistent derivation orders.
  150.      * </p>
  151.      * @param r rotation with time-derivatives embedded within the coordinates
  152.      * @param <U> type of the derivative
  153.      * @since 9.2
  154.      */
  155.     public <U extends FieldDerivative<T, U>> FieldAngularCoordinates(final FieldRotation<U> r) {

  156.         final T q0       = r.getQ0().getValue();
  157.         final T q1       = r.getQ1().getValue();
  158.         final T q2       = r.getQ2().getValue();
  159.         final T q3       = r.getQ3().getValue();

  160.         rotation     = new FieldRotation<>(q0, q1, q2, q3, false);
  161.         if (r.getQ0().getOrder() >= 1) {
  162.             final T q0Dot    = r.getQ0().getPartialDerivative(1);
  163.             final T q1Dot    = r.getQ1().getPartialDerivative(1);
  164.             final T q2Dot    = r.getQ2().getPartialDerivative(1);
  165.             final T q3Dot    = r.getQ3().getPartialDerivative(1);
  166.             rotationRate =
  167.                     new FieldVector3D<>(q0.linearCombination(q1.negate(), q0Dot, q0,          q1Dot,
  168.                                                              q3,          q2Dot, q2.negate(), q3Dot).multiply(2),
  169.                                         q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,
  170.                                                              q0,          q2Dot, q1,          q3Dot).multiply(2),
  171.                                         q0.linearCombination(q3.negate(), q0Dot, q2,          q1Dot,
  172.                                                              q1.negate(), q2Dot, q0,          q3Dot).multiply(2));
  173.             if (r.getQ0().getOrder() >= 2) {
  174.                 final T q0DotDot = r.getQ0().getPartialDerivative(2);
  175.                 final T q1DotDot = r.getQ1().getPartialDerivative(2);
  176.                 final T q2DotDot = r.getQ2().getPartialDerivative(2);
  177.                 final T q3DotDot = r.getQ3().getPartialDerivative(2);
  178.                 rotationAcceleration =
  179.                         new FieldVector3D<>(q0.linearCombination(q1.negate(), q0DotDot, q0,          q1DotDot,
  180.                                                                  q3,          q2DotDot, q2.negate(), q3DotDot).multiply(2),
  181.                                             q0.linearCombination(q2.negate(), q0DotDot, q3.negate(), q1DotDot,
  182.                                                                  q0,          q2DotDot, q1,          q3DotDot).multiply(2),
  183.                                             q0.linearCombination(q3.negate(), q0DotDot, q2,          q1DotDot,
  184.                                                                  q1.negate(), q2DotDot, q0,          q3DotDot).multiply(2));
  185.             } else {
  186.                 rotationAcceleration = FieldVector3D.getZero(q0.getField());
  187.             }
  188.         } else {
  189.             rotationRate         = FieldVector3D.getZero(q0.getField());
  190.             rotationAcceleration = FieldVector3D.getZero(q0.getField());
  191.         }

  192.     }

  193.     /** Fixed orientation parallel with reference frame
  194.      * (identity rotation, zero rotation rate and acceleration).
  195.      * @param field field for the components
  196.      * @param <T> the type of the field elements
  197.      * @return a new fixed orientation parallel with reference frame
  198.      */
  199.     public static <T extends CalculusFieldElement<T>> FieldAngularCoordinates<T> getIdentity(final Field<T> field) {
  200.         return new FieldAngularCoordinates<>(field, AngularCoordinates.IDENTITY);
  201.     }

  202.     /** Find a vector from two known cross products.
  203.      * <p>
  204.      * We want to find Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
  205.      * </p>
  206.      * <p>
  207.      * The first equation (Ω ⨯ v₁ = c₁) will always be fulfilled exactly,
  208.      * and the second one will be fulfilled if possible.
  209.      * </p>
  210.      * @param v1 vector forming the first known cross product
  211.      * @param c1 know vector for cross product Ω ⨯ v₁
  212.      * @param v2 vector forming the second known cross product
  213.      * @param c2 know vector for cross product Ω ⨯ v₂
  214.      * @param tolerance relative tolerance factor used to check singularities
  215.      * @param <T> the type of the field elements
  216.      * @return vector Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
  217.      * @exception MathIllegalArgumentException if vectors are inconsistent and
  218.      * no solution can be found
  219.      */
  220.     private static <T extends CalculusFieldElement<T>> FieldVector3D<T> inverseCrossProducts(final FieldVector3D<T> v1, final FieldVector3D<T> c1,
  221.                                                                                          final FieldVector3D<T> v2, final FieldVector3D<T> c2,
  222.                                                                                          final double tolerance)
  223.         throws MathIllegalArgumentException {

  224.         final T v12 = v1.getNormSq();
  225.         final T v1n = v12.sqrt();
  226.         final T v22 = v2.getNormSq();
  227.         final T v2n = v22.sqrt();
  228.         final T threshold;
  229.         if (v1n.getReal() >= v2n.getReal()) {
  230.             threshold = v1n.multiply(tolerance);
  231.         }
  232.         else {
  233.             threshold = v2n.multiply(tolerance);
  234.         }
  235.         FieldVector3D<T> omega = null;

  236.         try {
  237.             // create the over-determined linear system representing the two cross products
  238.             final FieldMatrix<T> m = MatrixUtils.createFieldMatrix(v12.getField(), 6, 3);
  239.             m.setEntry(0, 1, v1.getZ());
  240.             m.setEntry(0, 2, v1.getY().negate());
  241.             m.setEntry(1, 0, v1.getZ().negate());
  242.             m.setEntry(1, 2, v1.getX());
  243.             m.setEntry(2, 0, v1.getY());
  244.             m.setEntry(2, 1, v1.getX().negate());
  245.             m.setEntry(3, 1, v2.getZ());
  246.             m.setEntry(3, 2, v2.getY().negate());
  247.             m.setEntry(4, 0, v2.getZ().negate());
  248.             m.setEntry(4, 2, v2.getX());
  249.             m.setEntry(5, 0, v2.getY());
  250.             m.setEntry(5, 1, v2.getX().negate());

  251.             final T[] kk = MathArrays.buildArray(v2n.getField(), 6);
  252.             kk[0] = c1.getX();
  253.             kk[1] = c1.getY();
  254.             kk[2] = c1.getZ();
  255.             kk[3] = c2.getX();
  256.             kk[4] = c2.getY();
  257.             kk[5] = c2.getZ();
  258.             final FieldVector<T> rhs = MatrixUtils.createFieldVector(kk);

  259.             // find the best solution we can
  260.             final FieldDecompositionSolver<T> solver = new FieldQRDecomposition<>(m).getSolver();
  261.             final FieldVector<T> v = solver.solve(rhs);
  262.             omega = new FieldVector3D<>(v.getEntry(0), v.getEntry(1), v.getEntry(2));

  263.         } catch (MathIllegalArgumentException miae) {
  264.             if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {

  265.                 // handle some special cases for which we can compute a solution
  266.                 final T c12 = c1.getNormSq();
  267.                 final T c1n = c12.sqrt();
  268.                 final T c22 = c2.getNormSq();
  269.                 final T c2n = c22.sqrt();
  270.                 if (c1n.getReal() <= threshold.getReal() && c2n.getReal() <= threshold.getReal()) {
  271.                     // simple special case, velocities are cancelled
  272.                     return new FieldVector3D<>(v12.getField().getZero(), v12.getField().getZero(), v12.getField().getZero());
  273.                 } else if (v1n.getReal() <= threshold.getReal() && c1n.getReal() >= threshold.getReal()) {
  274.                     // this is inconsistent, if v₁ is zero, c₁ must be 0 too
  275.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
  276.                                                            c1n.getReal(), 0, true);
  277.                 } else if (v2n.getReal() <= threshold.getReal() && c2n.getReal() >= threshold.getReal()) {
  278.                     // this is inconsistent, if v₂ is zero, c₂ must be 0 too
  279.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
  280.                                                            c2n.getReal(), 0, true);
  281.                 } else if (v1.crossProduct(v1).getNorm().getReal() <= threshold.getReal() && v12.getReal() > threshold.getReal()) {
  282.                     // simple special case, v₂ is redundant with v₁, we just ignore it
  283.                     // use the simplest Ω: orthogonal to both v₁ and c₁
  284.                     omega = new FieldVector3D<>(v12.reciprocal(), v1.crossProduct(c1));
  285.                 } else {
  286.                     throw miae;
  287.                 }
  288.             } else {
  289.                 throw miae;
  290.             }
  291.         }
  292.         // check results
  293.         final T d1 = FieldVector3D.distance(omega.crossProduct(v1), c1);
  294.         if (d1.getReal() > threshold.getReal()) {
  295.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
  296.         }

  297.         final T d2 = FieldVector3D.distance(omega.crossProduct(v2), c2);
  298.         if (d2.getReal() > threshold.getReal()) {
  299.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
  300.         }

  301.         return omega;

  302.     }

  303.     /** Transform the instance to a {@link FieldRotation}&lt;{@link FieldDerivativeStructure}&gt;.
  304.      * <p>
  305.      * The {@link FieldDerivativeStructure} coordinates correspond to time-derivatives up
  306.      * to the user-specified order.
  307.      * </p>
  308.      * @param order derivation order for the vector components
  309.      * @return rotation with time-derivatives embedded within the coordinates
  310.           * @since 9.2
  311.      */
  312.     public FieldRotation<FieldDerivativeStructure<T>> toDerivativeStructureRotation(final int order) {

  313.         // quaternion components
  314.         final T q0 = rotation.getQ0();
  315.         final T q1 = rotation.getQ1();
  316.         final T q2 = rotation.getQ2();
  317.         final T q3 = rotation.getQ3();

  318.         // first time-derivatives of the quaternion
  319.         final T oX    = rotationRate.getX();
  320.         final T oY    = rotationRate.getY();
  321.         final T oZ    = rotationRate.getZ();
  322.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  323.         final T q1Dot = q0.linearCombination(q0,          oX, q3.negate(), oY, q2,          oZ).multiply(0.5);
  324.         final T q2Dot = q0.linearCombination(q3,          oX, q0,          oY, q1.negate(), oZ).multiply(0.5);
  325.         final T q3Dot = q0.linearCombination(q2.negate(), oX, q1,          oY, q0,          oZ).multiply(0.5);

  326.         // second time-derivatives of the quaternion
  327.         final T oXDot = rotationAcceleration.getX();
  328.         final T oYDot = rotationAcceleration.getY();
  329.         final T oZDot = rotationAcceleration.getZ();
  330.         final T q0DotDot = q0.linearCombination(array6(q1, q2,  q3, q1Dot, q2Dot,  q3Dot),
  331.                                                 array6(oXDot, oYDot, oZDot, oX, oY, oZ)).multiply(-0.5);
  332.         final T q1DotDot = q0.linearCombination(array6(q0, q2, q3.negate(), q0Dot, q2Dot, q3Dot.negate()),
  333.                                                 array6(oXDot, oZDot, oYDot, oX, oZ, oY)).multiply(0.5);
  334.         final T q2DotDot = q0.linearCombination(array6(q0, q3, q1.negate(), q0Dot, q3Dot, q1Dot.negate()),
  335.                                                 array6(oYDot, oXDot, oZDot, oY, oX, oZ)).multiply(0.5);
  336.         final T q3DotDot = q0.linearCombination(array6(q0, q1, q2.negate(), q0Dot, q1Dot, q2Dot.negate()),
  337.                                                 array6(oZDot, oYDot, oXDot, oZ, oY, oX)).multiply(0.5);

  338.         final FDSFactory<T> factory;
  339.         final FieldDerivativeStructure<T> q0DS;
  340.         final FieldDerivativeStructure<T> q1DS;
  341.         final FieldDerivativeStructure<T> q2DS;
  342.         final FieldDerivativeStructure<T> q3DS;
  343.         switch (order) {
  344.             case 0 :
  345.                 factory = new FDSFactory<>(q0.getField(), 1, order);
  346.                 q0DS = factory.build(q0);
  347.                 q1DS = factory.build(q1);
  348.                 q2DS = factory.build(q2);
  349.                 q3DS = factory.build(q3);
  350.                 break;
  351.             case 1 :
  352.                 factory = new FDSFactory<>(q0.getField(), 1, order);
  353.                 q0DS = factory.build(q0, q0Dot);
  354.                 q1DS = factory.build(q1, q1Dot);
  355.                 q2DS = factory.build(q2, q2Dot);
  356.                 q3DS = factory.build(q3, q3Dot);
  357.                 break;
  358.             case 2 :
  359.                 factory = new FDSFactory<>(q0.getField(), 1, order);
  360.                 q0DS = factory.build(q0, q0Dot, q0DotDot);
  361.                 q1DS = factory.build(q1, q1Dot, q1DotDot);
  362.                 q2DS = factory.build(q2, q2Dot, q2DotDot);
  363.                 q3DS = factory.build(q3, q3Dot, q3DotDot);
  364.                 break;
  365.             default :
  366.                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_DERIVATION_ORDER, order);
  367.         }

  368.         return new FieldRotation<>(q0DS, q1DS, q2DS, q3DS, false);

  369.     }

  370.     /** Transform the instance to a {@link FieldRotation}&lt;{@link UnivariateDerivative1}&gt;.
  371.      * <p>
  372.      * The {@link UnivariateDerivative1} coordinates correspond to time-derivatives up
  373.      * to the order 1.
  374.      * </p>
  375.      * @return rotation with time-derivatives embedded within the coordinates
  376.      */
  377.     public FieldRotation<FieldUnivariateDerivative1<T>> toUnivariateDerivative1Rotation() {

  378.         // quaternion components
  379.         final T q0 = rotation.getQ0();
  380.         final T q1 = rotation.getQ1();
  381.         final T q2 = rotation.getQ2();
  382.         final T q3 = rotation.getQ3();

  383.         // first time-derivatives of the quaternion
  384.         final T oX    = rotationRate.getX();
  385.         final T oY    = rotationRate.getY();
  386.         final T oZ    = rotationRate.getZ();
  387.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  388.         final T q1Dot = q0.linearCombination(q0,          oX, q3.negate(), oY, q2,          oZ).multiply(0.5);
  389.         final T q2Dot = q0.linearCombination(q3,          oX, q0,          oY, q1.negate(), oZ).multiply(0.5);
  390.         final T q3Dot = q0.linearCombination(q2.negate(), oX, q1,          oY, q0,          oZ).multiply(0.5);

  391.         final FieldUnivariateDerivative1<T> q0UD = new FieldUnivariateDerivative1<>(q0, q0Dot);
  392.         final FieldUnivariateDerivative1<T> q1UD = new FieldUnivariateDerivative1<>(q1, q1Dot);
  393.         final FieldUnivariateDerivative1<T> q2UD = new FieldUnivariateDerivative1<>(q2, q2Dot);
  394.         final FieldUnivariateDerivative1<T> q3UD = new FieldUnivariateDerivative1<>(q3, q3Dot);

  395.         return new FieldRotation<>(q0UD, q1UD, q2UD, q3UD, false);

  396.     }

  397.     /** Transform the instance to a {@link FieldRotation}&lt;{@link UnivariateDerivative2}&gt;.
  398.      * <p>
  399.      * The {@link UnivariateDerivative2} coordinates correspond to time-derivatives up
  400.      * to the order 2.
  401.      * </p>
  402.      * @return rotation with time-derivatives embedded within the coordinates
  403.      */
  404.     public FieldRotation<FieldUnivariateDerivative2<T>> toUnivariateDerivative2Rotation() {

  405.         // quaternion components
  406.         final T q0 = rotation.getQ0();
  407.         final T q1 = rotation.getQ1();
  408.         final T q2 = rotation.getQ2();
  409.         final T q3 = rotation.getQ3();

  410.         // first time-derivatives of the quaternion
  411.         final T oX    = rotationRate.getX();
  412.         final T oY    = rotationRate.getY();
  413.         final T oZ    = rotationRate.getZ();
  414.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  415.         final T q1Dot = q0.linearCombination(q0,          oX, q3.negate(), oY, q2,          oZ).multiply(0.5);
  416.         final T q2Dot = q0.linearCombination(q3,          oX, q0,          oY, q1.negate(), oZ).multiply(0.5);
  417.         final T q3Dot = q0.linearCombination(q2.negate(), oX, q1,          oY, q0,          oZ).multiply(0.5);

  418.         // second time-derivatives of the quaternion
  419.         final T oXDot = rotationAcceleration.getX();
  420.         final T oYDot = rotationAcceleration.getY();
  421.         final T oZDot = rotationAcceleration.getZ();
  422.         final T q0DotDot = q0.linearCombination(array6(q1, q2,  q3, q1Dot, q2Dot,  q3Dot),
  423.                                                 array6(oXDot, oYDot, oZDot, oX, oY, oZ)).multiply(-0.5);
  424.         final T q1DotDot = q0.linearCombination(array6(q0, q2, q3.negate(), q0Dot, q2Dot, q3Dot.negate()),
  425.                                                 array6(oXDot, oZDot, oYDot, oX, oZ, oY)).multiply(0.5);
  426.         final T q2DotDot = q0.linearCombination(array6(q0, q3, q1.negate(), q0Dot, q3Dot, q1Dot.negate()),
  427.                                                 array6(oYDot, oXDot, oZDot, oY, oX, oZ)).multiply(0.5);
  428.         final T q3DotDot = q0.linearCombination(array6(q0, q1, q2.negate(), q0Dot, q1Dot, q2Dot.negate()),
  429.                                                 array6(oZDot, oYDot, oXDot, oZ, oY, oX)).multiply(0.5);

  430.         final FieldUnivariateDerivative2<T> q0UD = new FieldUnivariateDerivative2<>(q0, q0Dot, q0DotDot);
  431.         final FieldUnivariateDerivative2<T> q1UD = new FieldUnivariateDerivative2<>(q1, q1Dot, q1DotDot);
  432.         final FieldUnivariateDerivative2<T> q2UD = new FieldUnivariateDerivative2<>(q2, q2Dot, q2DotDot);
  433.         final FieldUnivariateDerivative2<T> q3UD = new FieldUnivariateDerivative2<>(q3, q3Dot, q3DotDot);

  434.         return new FieldRotation<>(q0UD, q1UD, q2UD, q3UD, false);

  435.     }

  436.     /** Build an arry of 6 elements.
  437.      * @param e1 first element
  438.      * @param e2 second element
  439.      * @param e3 third element
  440.      * @param e4 fourth element
  441.      * @param e5 fifth element
  442.      * @param e6 sixth element
  443.      * @return a new array
  444.      * @since 9.2
  445.      */
  446.     private T[] array6(final T e1, final T e2, final T e3, final T e4, final T e5, final T e6) {
  447.         final T[] array = MathArrays.buildArray(e1.getField(), 6);
  448.         array[0] = e1;
  449.         array[1] = e2;
  450.         array[2] = e3;
  451.         array[3] = e4;
  452.         array[4] = e5;
  453.         array[5] = e6;
  454.         return array;
  455.     }

  456.     /** Estimate rotation rate between two orientations.
  457.      * <p>Estimation is based on a simple fixed rate rotation
  458.      * during the time interval between the two orientations.</p>
  459.      * @param start start orientation
  460.      * @param end end orientation
  461.      * @param dt time elapsed between the dates of the two orientations
  462.      * @param <T> the type of the field elements
  463.      * @return rotation rate allowing to go from start to end orientations
  464.      */
  465.     public static <T extends CalculusFieldElement<T>>
  466.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  467.                                       final FieldRotation<T> end,
  468.                                       final double dt) {
  469.         return estimateRate(start, end, start.getQ0().getField().getZero().add(dt));
  470.     }

  471.     /** Estimate rotation rate between two orientations.
  472.      * <p>Estimation is based on a simple fixed rate rotation
  473.      * during the time interval between the two orientations.</p>
  474.      * @param start start orientation
  475.      * @param end end orientation
  476.      * @param dt time elapsed between the dates of the two orientations
  477.      * @param <T> the type of the field elements
  478.      * @return rotation rate allowing to go from start to end orientations
  479.      */
  480.     public static <T extends CalculusFieldElement<T>>
  481.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  482.                                       final FieldRotation<T> end,
  483.                                       final T dt) {
  484.         final FieldRotation<T> evolution = start.compose(end.revert(), RotationConvention.VECTOR_OPERATOR);
  485.         return new FieldVector3D<>(evolution.getAngle().divide(dt),
  486.                                    evolution.getAxis(RotationConvention.VECTOR_OPERATOR));
  487.     }

  488.     /**
  489.      * Revert a rotation / rotation rate / rotation acceleration triplet.
  490.      *
  491.      * <p> Build a triplet which reverse the effect of another triplet.
  492.      *
  493.      * @return a new triplet whose effect is the reverse of the effect
  494.      * of the instance
  495.      */
  496.     public FieldAngularCoordinates<T> revert() {
  497.         return new FieldAngularCoordinates<>(rotation.revert(),
  498.                                              rotation.applyInverseTo(rotationRate.negate()),
  499.                                              rotation.applyInverseTo(rotationAcceleration.negate()));
  500.     }

  501.     /** Get a time-shifted state.
  502.      * <p>
  503.      * The state can be slightly shifted to close dates. This shift is based on
  504.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  505.      * proper attitude propagation but should be sufficient for either small
  506.      * time shifts or coarse accuracy.
  507.      * </p>
  508.      * @param dt time shift in seconds
  509.      * @return a new state, shifted with respect to the instance (which is immutable)
  510.      */
  511.     public FieldAngularCoordinates<T> shiftedBy(final double dt) {
  512.         return shiftedBy(rotation.getQ0().getField().getZero().add(dt));
  513.     }

  514.     /** Get a time-shifted state.
  515.      * <p>
  516.      * The state can be slightly shifted to close dates. This shift is based on
  517.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  518.      * proper attitude propagation but should be sufficient for either small
  519.      * time shifts or coarse accuracy.
  520.      * </p>
  521.      * @param dt time shift in seconds
  522.      * @return a new state, shifted with respect to the instance (which is immutable)
  523.      */
  524.     public FieldAngularCoordinates<T> shiftedBy(final T dt) {

  525.         // the shiftedBy method is based on a local approximation.
  526.         // It considers separately the contribution of the constant
  527.         // rotation, the linear contribution or the rate and the
  528.         // quadratic contribution of the acceleration. The rate
  529.         // and acceleration contributions are small rotations as long
  530.         // as the time shift is small, which is the crux of the algorithm.
  531.         // Small rotations are almost commutative, so we append these small
  532.         // contributions one after the other, as if they really occurred
  533.         // successively, despite this is not what really happens.

  534.         // compute the linear contribution first, ignoring acceleration
  535.         // BEWARE: there is really a minus sign here, because if
  536.         // the target frame rotates in one direction, the vectors in the origin
  537.         // frame seem to rotate in the opposite direction
  538.         final T rate = rotationRate.getNorm();
  539.         final T zero = rate.getField().getZero();
  540.         final T one  = rate.getField().getOne();
  541.         final FieldRotation<T> rateContribution = (rate.getReal() == 0.0) ?
  542.                                                   new FieldRotation<>(one, zero, zero, zero, false) :
  543.                                                   new FieldRotation<>(rotationRate,
  544.                                                                       rate.multiply(dt),
  545.                                                                       RotationConvention.FRAME_TRANSFORM);

  546.         // append rotation and rate contribution
  547.         final FieldAngularCoordinates<T> linearPart =
  548.                 new FieldAngularCoordinates<>(rateContribution.compose(rotation, RotationConvention.VECTOR_OPERATOR),
  549.                                               rotationRate);

  550.         final T acc  = rotationAcceleration.getNorm();
  551.         if (acc.getReal() == 0.0) {
  552.             // no acceleration, the linear part is sufficient
  553.             return linearPart;
  554.         }

  555.         // compute the quadratic contribution, ignoring initial rotation and rotation rate
  556.         // BEWARE: there is really a minus sign here, because if
  557.         // the target frame rotates in one direction, the vectors in the origin
  558.         // frame seem to rotate in the opposite direction
  559.         final FieldAngularCoordinates<T> quadraticContribution =
  560.                 new FieldAngularCoordinates<>(new FieldRotation<>(rotationAcceleration,
  561.                                                                   acc.multiply(dt.multiply(0.5).multiply(dt)),
  562.                                                                   RotationConvention.FRAME_TRANSFORM),
  563.                                               new FieldVector3D<>(dt, rotationAcceleration),
  564.                                               rotationAcceleration);

  565.         // the quadratic contribution is a small rotation:
  566.         // its initial angle and angular rate are both zero.
  567.         // small rotations are almost commutative, so we append the small
  568.         // quadratic part after the linear part as a simple offset
  569.         return quadraticContribution.addOffset(linearPart);

  570.     }

  571.     /** Get the rotation.
  572.      * @return the rotation.
  573.      */
  574.     public FieldRotation<T> getRotation() {
  575.         return rotation;
  576.     }

  577.     /** Get the rotation rate.
  578.      * @return the rotation rate vector (rad/s).
  579.      */
  580.     public FieldVector3D<T> getRotationRate() {
  581.         return rotationRate;
  582.     }

  583.     /** Get the rotation acceleration.
  584.      * @return the rotation acceleration vector dΩ/dt (rad/s²).
  585.      */
  586.     public FieldVector3D<T> getRotationAcceleration() {
  587.         return rotationAcceleration;
  588.     }

  589.     /** Add an offset from the instance.
  590.      * <p>
  591.      * We consider here that the offset rotation is applied first and the
  592.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  593.      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
  594.      * b.addOffset(a)} lead to <em>different</em> results in most cases.
  595.      * </p>
  596.      * <p>
  597.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  598.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  599.      * so that round trip applications are possible. This means that both {@code
  600.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  601.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  602.      * </p>
  603.      * @param offset offset to subtract
  604.      * @return new instance, with offset subtracted
  605.      * @see #subtractOffset(FieldAngularCoordinates)
  606.      */
  607.     public FieldAngularCoordinates<T> addOffset(final FieldAngularCoordinates<T> offset) {
  608.         final FieldVector3D<T> rOmega    = rotation.applyTo(offset.rotationRate);
  609.         final FieldVector3D<T> rOmegaDot = rotation.applyTo(offset.rotationAcceleration);
  610.         return new FieldAngularCoordinates<>(rotation.compose(offset.rotation, RotationConvention.VECTOR_OPERATOR),
  611.                                              rotationRate.add(rOmega),
  612.                                              new FieldVector3D<>( 1.0, rotationAcceleration,
  613.                                                                   1.0, rOmegaDot,
  614.                                                                  -1.0, FieldVector3D.crossProduct(rotationRate, rOmega)));
  615.     }

  616.     /** Subtract an offset from the instance.
  617.      * <p>
  618.      * We consider here that the offset Rotation is applied first and the
  619.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  620.      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
  621.      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
  622.      * </p>
  623.      * <p>
  624.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  625.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  626.      * so that round trip applications are possible. This means that both {@code
  627.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  628.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  629.      * </p>
  630.      * @param offset offset to subtract
  631.      * @return new instance, with offset subtracted
  632.      * @see #addOffset(FieldAngularCoordinates)
  633.      */
  634.     public FieldAngularCoordinates<T> subtractOffset(final FieldAngularCoordinates<T> offset) {
  635.         return addOffset(offset.revert());
  636.     }

  637.     /** Convert to a regular angular coordinates.
  638.      * @return a regular angular coordinates
  639.      */
  640.     public AngularCoordinates toAngularCoordinates() {
  641.         return new AngularCoordinates(rotation.toRotation(), rotationRate.toVector3D(),
  642.                                       rotationAcceleration.toVector3D());
  643.     }

  644.     /** Apply the rotation to a pv coordinates.
  645.      * @param pv vector to apply the rotation to
  646.      * @return a new pv coordinates which is the image of u by the rotation
  647.      */
  648.     public FieldPVCoordinates<T> applyTo(final PVCoordinates pv) {

  649.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  650.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  651.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  652.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  653.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  654.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  655.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  656.                                                                   -2, crossV,
  657.                                                                   -1, crossCrossP,
  658.                                                                   -1, crossDotP);

  659.         return new FieldPVCoordinates<>(transformedP, transformedV, transformedA);

  660.     }

  661.     /** Apply the rotation to a pv coordinates.
  662.      * @param pv vector to apply the rotation to
  663.      * @return a new pv coordinates which is the image of u by the rotation
  664.      */
  665.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedPVCoordinates pv) {

  666.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  667.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  668.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  669.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  670.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  671.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  672.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  673.                                                                   -2, crossV,
  674.                                                                   -1, crossCrossP,
  675.                                                                   -1, crossDotP);

  676.         return new TimeStampedFieldPVCoordinates<>(pv.getDate(), transformedP, transformedV, transformedA);

  677.     }

  678.     /** Apply the rotation to a pv coordinates.
  679.      * @param pv vector to apply the rotation to
  680.      * @return a new pv coordinates which is the image of u by the rotation
  681.      * @since 9.0
  682.      */
  683.     public FieldPVCoordinates<T> applyTo(final FieldPVCoordinates<T> pv) {

  684.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  685.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  686.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  687.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  688.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  689.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  690.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  691.                                                                   -2, crossV,
  692.                                                                   -1, crossCrossP,
  693.                                                                   -1, crossDotP);

  694.         return new FieldPVCoordinates<>(transformedP, transformedV, transformedA);

  695.     }

  696.     /** Apply the rotation to a pv coordinates.
  697.      * @param pv vector to apply the rotation to
  698.      * @return a new pv coordinates which is the image of u by the rotation
  699.      * @since 9.0
  700.      */
  701.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedFieldPVCoordinates<T> pv) {

  702.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  703.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  704.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  705.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  706.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  707.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  708.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  709.                                                                   -2, crossV,
  710.                                                                   -1, crossCrossP,
  711.                                                                   -1, crossDotP);

  712.         return new TimeStampedFieldPVCoordinates<>(pv.getDate(), transformedP, transformedV, transformedA);

  713.     }

  714.     /** Convert rotation, rate and acceleration to modified Rodrigues vector and derivatives.
  715.      * <p>
  716.      * The modified Rodrigues vector is tan(θ/4) u where θ and u are the
  717.      * rotation angle and axis respectively.
  718.      * </p>
  719.      * @param sign multiplicative sign for quaternion components
  720.      * @return modified Rodrigues vector and derivatives (vector on row 0, first derivative
  721.      * on row 1, second derivative on row 2)
  722.      * @see #createFromModifiedRodrigues(CalculusFieldElement[][])
  723.      * @since 9.0
  724.      */
  725.     public T[][] getModifiedRodrigues(final double sign) {

  726.         final T q0    = getRotation().getQ0().multiply(sign);
  727.         final T q1    = getRotation().getQ1().multiply(sign);
  728.         final T q2    = getRotation().getQ2().multiply(sign);
  729.         final T q3    = getRotation().getQ3().multiply(sign);
  730.         final T oX    = getRotationRate().getX();
  731.         final T oY    = getRotationRate().getY();
  732.         final T oZ    = getRotationRate().getZ();
  733.         final T oXDot = getRotationAcceleration().getX();
  734.         final T oYDot = getRotationAcceleration().getY();
  735.         final T oZDot = getRotationAcceleration().getZ();

  736.         // first time-derivatives of the quaternion
  737.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  738.         final T q1Dot = q0.linearCombination( q0, oX, q3.negate(), oY,  q2, oZ).multiply(0.5);
  739.         final T q2Dot = q0.linearCombination( q3, oX,  q0, oY, q1.negate(), oZ).multiply(0.5);
  740.         final T q3Dot = q0.linearCombination(q2.negate(), oX,  q1, oY,  q0, oZ).multiply(0.5);

  741.         // second time-derivatives of the quaternion
  742.         final T q0DotDot = linearCombination(q1, oXDot, q2, oYDot, q3, oZDot,
  743.                                              q1Dot, oX, q2Dot, oY, q3Dot, oZ).
  744.                            multiply(-0.5);
  745.         final T q1DotDot = linearCombination(q0, oXDot, q2, oZDot, q3.negate(), oYDot,
  746.                                              q0Dot, oX, q2Dot, oZ, q3Dot.negate(), oY).
  747.                            multiply(0.5);
  748.         final T q2DotDot = linearCombination(q0, oYDot, q3, oXDot, q1.negate(), oZDot,
  749.                                              q0Dot, oY, q3Dot, oX, q1Dot.negate(), oZ).
  750.                            multiply(0.5);
  751.         final T q3DotDot = linearCombination(q0, oZDot, q1, oYDot, q2.negate(), oXDot,
  752.                                              q0Dot, oZ, q1Dot, oY, q2Dot.negate(), oX).
  753.                            multiply(0.5);

  754.         // the modified Rodrigues is tan(θ/4) u where θ and u are the rotation angle and axis respectively
  755.         // this can be rewritten using quaternion components:
  756.         //      r (q₁ / (1+q₀), q₂ / (1+q₀), q₃ / (1+q₀))
  757.         // applying the derivation chain rule to previous expression gives rDot and rDotDot
  758.         final T inv          = q0.add(1).reciprocal();
  759.         final T mTwoInvQ0Dot = inv.multiply(q0Dot).multiply(-2);

  760.         final T r1       = inv.multiply(q1);
  761.         final T r2       = inv.multiply(q2);
  762.         final T r3       = inv.multiply(q3);

  763.         final T mInvR1   = inv.multiply(r1).negate();
  764.         final T mInvR2   = inv.multiply(r2).negate();
  765.         final T mInvR3   = inv.multiply(r3).negate();

  766.         final T r1Dot    = q0.linearCombination(inv, q1Dot, mInvR1, q0Dot);
  767.         final T r2Dot    = q0.linearCombination(inv, q2Dot, mInvR2, q0Dot);
  768.         final T r3Dot    = q0.linearCombination(inv, q3Dot, mInvR3, q0Dot);

  769.         final T r1DotDot = q0.linearCombination(inv, q1DotDot, mTwoInvQ0Dot, r1Dot, mInvR1, q0DotDot);
  770.         final T r2DotDot = q0.linearCombination(inv, q2DotDot, mTwoInvQ0Dot, r2Dot, mInvR2, q0DotDot);
  771.         final T r3DotDot = q0.linearCombination(inv, q3DotDot, mTwoInvQ0Dot, r3Dot, mInvR3, q0DotDot);

  772.         final T[][] rodrigues = MathArrays.buildArray(q0.getField(), 3, 3);
  773.         rodrigues[0][0] = r1;
  774.         rodrigues[0][1] = r2;
  775.         rodrigues[0][2] = r3;
  776.         rodrigues[1][0] = r1Dot;
  777.         rodrigues[1][1] = r2Dot;
  778.         rodrigues[1][2] = r3Dot;
  779.         rodrigues[2][0] = r1DotDot;
  780.         rodrigues[2][1] = r2DotDot;
  781.         rodrigues[2][2] = r3DotDot;
  782.         return rodrigues;

  783.     }

  784.     /**
  785.      * Compute a linear combination.
  786.      * @param a1 first factor of the first term
  787.      * @param b1 second factor of the first term
  788.      * @param a2 first factor of the second term
  789.      * @param b2 second factor of the second term
  790.      * @param a3 first factor of the third term
  791.      * @param b3 second factor of the third term
  792.      * @param a4 first factor of the fourth term
  793.      * @param b4 second factor of the fourth term
  794.      * @param a5 first factor of the fifth term
  795.      * @param b5 second factor of the fifth term
  796.      * @param a6 first factor of the sixth term
  797.      * @param b6 second factor of the sicth term
  798.      * @return a<sub>1</sub>&times;b<sub>1</sub> + a<sub>2</sub>&times;b<sub>2</sub> +
  799.      * a<sub>3</sub>&times;b<sub>3</sub> + a<sub>4</sub>&times;b<sub>4</sub> +
  800.      * a<sub>5</sub>&times;b<sub>5</sub> + a<sub>6</sub>&times;b<sub>6</sub>
  801.      */
  802.     private T linearCombination(final T a1, final T b1, final T a2, final T b2, final T a3, final T b3,
  803.                                 final T a4, final T b4, final T a5, final T b5, final T a6, final T b6) {

  804.         final T[] a = MathArrays.buildArray(a1.getField(), 6);
  805.         a[0] = a1;
  806.         a[1] = a2;
  807.         a[2] = a3;
  808.         a[3] = a4;
  809.         a[4] = a5;
  810.         a[5] = a6;

  811.         final T[] b = MathArrays.buildArray(b1.getField(), 6);
  812.         b[0] = b1;
  813.         b[1] = b2;
  814.         b[2] = b3;
  815.         b[3] = b4;
  816.         b[4] = b5;
  817.         b[5] = b6;

  818.         return a1.linearCombination(a, b);

  819.     }

  820.     /** Convert a modified Rodrigues vector and derivatives to angular coordinates.
  821.      * @param r modified Rodrigues vector (with first and second times derivatives)
  822.      * @param <T> the type of the field elements
  823.      * @return angular coordinates
  824.      * @see #getModifiedRodrigues(double)
  825.      * @since 9.0
  826.      */
  827.     public static <T extends CalculusFieldElement<T>>  FieldAngularCoordinates<T> createFromModifiedRodrigues(final T[][] r) {

  828.         // rotation
  829.         final T rSquared = r[0][0].multiply(r[0][0]).add(r[0][1].multiply(r[0][1])).add(r[0][2].multiply(r[0][2]));
  830.         final T oPQ0     = rSquared.add(1).reciprocal().multiply(2);
  831.         final T q0       = oPQ0.subtract(1);
  832.         final T q1       = oPQ0.multiply(r[0][0]);
  833.         final T q2       = oPQ0.multiply(r[0][1]);
  834.         final T q3       = oPQ0.multiply(r[0][2]);

  835.         // rotation rate
  836.         final T oPQ02    = oPQ0.multiply(oPQ0);
  837.         final T q0Dot    = oPQ02.multiply(q0.linearCombination(r[0][0], r[1][0], r[0][1], r[1][1],  r[0][2], r[1][2])).negate();
  838.         final T q1Dot    = oPQ0.multiply(r[1][0]).add(r[0][0].multiply(q0Dot));
  839.         final T q2Dot    = oPQ0.multiply(r[1][1]).add(r[0][1].multiply(q0Dot));
  840.         final T q3Dot    = oPQ0.multiply(r[1][2]).add(r[0][2].multiply(q0Dot));
  841.         final T oX       = q0.linearCombination(q1.negate(), q0Dot,  q0, q1Dot,  q3, q2Dot, q2.negate(), q3Dot).multiply(2);
  842.         final T oY       = q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,  q0, q2Dot,  q1, q3Dot).multiply(2);
  843.         final T oZ       = q0.linearCombination(q3.negate(), q0Dot,  q2, q1Dot, q1.negate(), q2Dot,  q0, q3Dot).multiply(2);

  844.         // rotation acceleration
  845.         final T q0DotDot = q0.subtract(1).negate().divide(oPQ0).multiply(q0Dot).multiply(q0Dot).
  846.                            subtract(oPQ02.multiply(q0.linearCombination(r[0][0], r[2][0], r[0][1], r[2][1], r[0][2], r[2][2]))).
  847.                            subtract(q1Dot.multiply(q1Dot).add(q2Dot.multiply(q2Dot)).add(q3Dot.multiply(q3Dot)));
  848.         final T q1DotDot = q0.linearCombination(oPQ0, r[2][0], r[1][0].add(r[1][0]), q0Dot, r[0][0], q0DotDot);
  849.         final T q2DotDot = q0.linearCombination(oPQ0, r[2][1], r[1][1].add(r[1][1]), q0Dot, r[0][1], q0DotDot);
  850.         final T q3DotDot = q0.linearCombination(oPQ0, r[2][2], r[1][2].add(r[1][2]), q0Dot, r[0][2], q0DotDot);
  851.         final T oXDot    = q0.linearCombination(q1.negate(), q0DotDot,  q0, q1DotDot,  q3, q2DotDot, q2.negate(), q3DotDot).multiply(2);
  852.         final T oYDot    = q0.linearCombination(q2.negate(), q0DotDot, q3.negate(), q1DotDot,  q0, q2DotDot,  q1, q3DotDot).multiply(2);
  853.         final T oZDot    = q0.linearCombination(q3.negate(), q0DotDot,  q2, q1DotDot, q1.negate(), q2DotDot,  q0, q3DotDot).multiply(2);

  854.         return new FieldAngularCoordinates<>(new FieldRotation<>(q0, q1, q2, q3, false),
  855.                                              new FieldVector3D<>(oX, oY, oZ),
  856.                                              new FieldVector3D<>(oXDot, oYDot, oZDot));

  857.     }

  858. }