FieldAngularCoordinates.java

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

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

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


  56.     /** rotation. */
  57.     private final FieldRotation<T> rotation;

  58.     /** rotation rate. */
  59.     private final FieldVector3D<T> rotationRate;

  60.     /** rotation acceleration. */
  61.     private final FieldVector3D<T> rotationAcceleration;

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

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

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

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

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

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


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


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

  131.         } catch (MathIllegalArgumentException miae) {
  132.             throw new OrekitException(miae);
  133.         }

  134.     }

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

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

  153.         final T q0       = r.getQ0().getValue();
  154.         final T q1       = r.getQ1().getValue();
  155.         final T q2       = r.getQ2().getValue();
  156.         final T q3       = r.getQ3().getValue();

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

  189.     }

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

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

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

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

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

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

  260.         } catch (MathIllegalArgumentException miae) {
  261.             if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {

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

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

  298.         return omega;

  299.     }

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

  310.         // quaternion components
  311.         final T q0 = rotation.getQ0();
  312.         final T q1 = rotation.getQ1();
  313.         final T q2 = rotation.getQ2();
  314.         final T q3 = rotation.getQ3();

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

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

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

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

  367.     }

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

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

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

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

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

  394.     }

  395.     /** Build an arry of 6 elements.
  396.      * @param e1 first element
  397.      * @param e2 second element
  398.      * @param e3 third element
  399.      * @param e4 fourth element
  400.      * @param e5 fifth element
  401.      * @param e6 sixth element
  402.      * @return a new array
  403.      * @since 9.2
  404.      */
  405.     private T[] array6(final T e1, final T e2, final T e3, final T e4, final T e5, final T e6) {
  406.         final T[] array = MathArrays.buildArray(e1.getField(), 6);
  407.         array[0] = e1;
  408.         array[1] = e2;
  409.         array[2] = e3;
  410.         array[3] = e4;
  411.         array[4] = e5;
  412.         array[5] = e6;
  413.         return array;
  414.     }

  415.     /** Estimate rotation rate between two orientations.
  416.      * <p>Estimation is based on a simple fixed rate rotation
  417.      * during the time interval between the two orientations.</p>
  418.      * @param start start orientation
  419.      * @param end end orientation
  420.      * @param dt time elapsed between the dates of the two orientations
  421.      * @param <T> the type of the field elements
  422.      * @return rotation rate allowing to go from start to end orientations
  423.      */
  424.     public static <T extends RealFieldElement<T>>
  425.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  426.                                       final FieldRotation<T> end,
  427.                                       final double dt) {
  428.         return estimateRate(start, end, start.getQ0().getField().getZero().add(dt));
  429.     }

  430.     /** Estimate rotation rate between two orientations.
  431.      * <p>Estimation is based on a simple fixed rate rotation
  432.      * during the time interval between the two orientations.</p>
  433.      * @param start start orientation
  434.      * @param end end orientation
  435.      * @param dt time elapsed between the dates of the two orientations
  436.      * @param <T> the type of the field elements
  437.      * @return rotation rate allowing to go from start to end orientations
  438.      */
  439.     public static <T extends RealFieldElement<T>>
  440.         FieldVector3D<T> estimateRate(final FieldRotation<T> start,
  441.                                       final FieldRotation<T> end,
  442.                                       final T dt) {
  443.         final FieldRotation<T> evolution = start.compose(end.revert(), RotationConvention.VECTOR_OPERATOR);
  444.         return new FieldVector3D<>(evolution.getAngle().divide(dt),
  445.                                    evolution.getAxis(RotationConvention.VECTOR_OPERATOR));
  446.     }

  447.     /**
  448.      * Revert a rotation / rotation rate / rotation acceleration triplet.
  449.      *
  450.      * <p> Build a triplet which reverse the effect of another triplet.
  451.      *
  452.      * @return a new triplet whose effect is the reverse of the effect
  453.      * of the instance
  454.      */
  455.     public FieldAngularCoordinates<T> revert() {
  456.         return new FieldAngularCoordinates<>(rotation.revert(),
  457.                                              rotation.applyInverseTo(rotationRate.negate()),
  458.                                              rotation.applyInverseTo(rotationAcceleration.negate()));
  459.     }

  460.     /** Get a time-shifted state.
  461.      * <p>
  462.      * The state can be slightly shifted to close dates. This shift is based on
  463.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  464.      * proper attitude propagation but should be sufficient for either small
  465.      * time shifts or coarse accuracy.
  466.      * </p>
  467.      * @param dt time shift in seconds
  468.      * @return a new state, shifted with respect to the instance (which is immutable)
  469.      */
  470.     public FieldAngularCoordinates<T> shiftedBy(final double dt) {
  471.         return shiftedBy(rotation.getQ0().getField().getZero().add(dt));
  472.     }

  473.     /** Get a time-shifted state.
  474.      * <p>
  475.      * The state can be slightly shifted to close dates. This shift is based on
  476.      * a simple quadratic model. It is <em>not</em> intended as a replacement for
  477.      * proper attitude propagation but should be sufficient for either small
  478.      * time shifts or coarse accuracy.
  479.      * </p>
  480.      * @param dt time shift in seconds
  481.      * @return a new state, shifted with respect to the instance (which is immutable)
  482.      */
  483.     public FieldAngularCoordinates<T> shiftedBy(final T dt) {

  484.         // the shiftedBy method is based on a local approximation.
  485.         // It considers separately the contribution of the constant
  486.         // rotation, the linear contribution or the rate and the
  487.         // quadratic contribution of the acceleration. The rate
  488.         // and acceleration contributions are small rotations as long
  489.         // as the time shift is small, which is the crux of the algorithm.
  490.         // Small rotations are almost commutative, so we append these small
  491.         // contributions one after the other, as if they really occurred
  492.         // successively, despite this is not what really happens.

  493.         // compute the linear contribution first, ignoring acceleration
  494.         // BEWARE: there is really a minus sign here, because if
  495.         // the target frame rotates in one direction, the vectors in the origin
  496.         // frame seem to rotate in the opposite direction
  497.         final T rate = rotationRate.getNorm();
  498.         final T zero = rate.getField().getZero();
  499.         final T one  = rate.getField().getOne();
  500.         final FieldRotation<T> rateContribution = (rate.getReal() == 0.0) ?
  501.                                                   new FieldRotation<>(one, zero, zero, zero, false) :
  502.                                                   new FieldRotation<>(rotationRate,
  503.                                                                       rate.multiply(dt),
  504.                                                                       RotationConvention.FRAME_TRANSFORM);

  505.         // append rotation and rate contribution
  506.         final FieldAngularCoordinates<T> linearPart =
  507.                 new FieldAngularCoordinates<>(rateContribution.compose(rotation, RotationConvention.VECTOR_OPERATOR),
  508.                                               rotationRate);

  509.         final T acc  = rotationAcceleration.getNorm();
  510.         if (acc.getReal() == 0.0) {
  511.             // no acceleration, the linear part is sufficient
  512.             return linearPart;
  513.         }

  514.         // compute the quadratic contribution, ignoring initial rotation and rotation rate
  515.         // BEWARE: there is really a minus sign here, because if
  516.         // the target frame rotates in one direction, the vectors in the origin
  517.         // frame seem to rotate in the opposite direction
  518.         final FieldAngularCoordinates<T> quadraticContribution =
  519.                 new FieldAngularCoordinates<>(new FieldRotation<>(rotationAcceleration,
  520.                                                                   acc.multiply(dt.multiply(0.5).multiply(dt)),
  521.                                                                   RotationConvention.FRAME_TRANSFORM),
  522.                                               new FieldVector3D<>(dt, rotationAcceleration),
  523.                                               rotationAcceleration);

  524.         // the quadratic contribution is a small rotation:
  525.         // its initial angle and angular rate are both zero.
  526.         // small rotations are almost commutative, so we append the small
  527.         // quadratic part after the linear part as a simple offset
  528.         return quadraticContribution.addOffset(linearPart);

  529.     }

  530.     /** Get the rotation.
  531.      * @return the rotation.
  532.      */
  533.     public FieldRotation<T> getRotation() {
  534.         return rotation;
  535.     }

  536.     /** Get the rotation rate.
  537.      * @return the rotation rate vector (rad/s).
  538.      */
  539.     public FieldVector3D<T> getRotationRate() {
  540.         return rotationRate;
  541.     }

  542.     /** Get the rotation acceleration.
  543.      * @return the rotation acceleration vector dΩ/dt (rad²/s²).
  544.      */
  545.     public FieldVector3D<T> getRotationAcceleration() {
  546.         return rotationAcceleration;
  547.     }

  548.     /** Add an offset from the instance.
  549.      * <p>
  550.      * We consider here that the offset rotation is applied first and the
  551.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  552.      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
  553.      * b.addOffset(a)} lead to <em>different</em> results in most cases.
  554.      * </p>
  555.      * <p>
  556.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  557.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  558.      * so that round trip applications are possible. This means that both {@code
  559.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  560.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  561.      * </p>
  562.      * @param offset offset to subtract
  563.      * @return new instance, with offset subtracted
  564.      * @see #subtractOffset(FieldAngularCoordinates)
  565.      */
  566.     public FieldAngularCoordinates<T> addOffset(final FieldAngularCoordinates<T> offset) {
  567.         final FieldVector3D<T> rOmega    = rotation.applyTo(offset.rotationRate);
  568.         final FieldVector3D<T> rOmegaDot = rotation.applyTo(offset.rotationAcceleration);
  569.         return new FieldAngularCoordinates<>(rotation.compose(offset.rotation, RotationConvention.VECTOR_OPERATOR),
  570.                                              rotationRate.add(rOmega),
  571.                                              new FieldVector3D<>( 1.0, rotationAcceleration,
  572.                                                                   1.0, rOmegaDot,
  573.                                                                  -1.0, FieldVector3D.crossProduct(rotationRate, rOmega)));
  574.     }

  575.     /** Subtract an offset from the instance.
  576.      * <p>
  577.      * We consider here that the offset Rotation is applied first and the
  578.      * instance is applied afterward. Note that angular coordinates do <em>not</em>
  579.      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
  580.      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
  581.      * </p>
  582.      * <p>
  583.      * The two methods {@link #addOffset(FieldAngularCoordinates) addOffset} and
  584.      * {@link #subtractOffset(FieldAngularCoordinates) subtractOffset} are designed
  585.      * so that round trip applications are possible. This means that both {@code
  586.      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
  587.      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
  588.      * </p>
  589.      * @param offset offset to subtract
  590.      * @return new instance, with offset subtracted
  591.      * @see #addOffset(FieldAngularCoordinates)
  592.      */
  593.     public FieldAngularCoordinates<T> subtractOffset(final FieldAngularCoordinates<T> offset) {
  594.         return addOffset(offset.revert());
  595.     }

  596.     /** Convert to a regular angular coordinates.
  597.      * @return a regular angular coordinates
  598.      */
  599.     public AngularCoordinates toAngularCoordinates() {
  600.         return new AngularCoordinates(rotation.toRotation(), rotationRate.toVector3D(),
  601.                                       rotationAcceleration.toVector3D());
  602.     }

  603.     /** Apply the rotation to a pv coordinates.
  604.      * @param pv vector to apply the rotation to
  605.      * @return a new pv coordinates which is the image of u by the rotation
  606.      */
  607.     public FieldPVCoordinates<T> applyTo(final PVCoordinates pv) {

  608.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  609.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  610.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  611.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  612.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  613.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  614.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  615.                                                                   -2, crossV,
  616.                                                                   -1, crossCrossP,
  617.                                                                   -1, crossDotP);

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

  619.     }

  620.     /** Apply the rotation to a pv coordinates.
  621.      * @param pv vector to apply the rotation to
  622.      * @return a new pv coordinates which is the image of u by the rotation
  623.      */
  624.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedPVCoordinates pv) {

  625.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  626.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  627.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  628.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  629.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  630.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  631.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  632.                                                                   -2, crossV,
  633.                                                                   -1, crossCrossP,
  634.                                                                   -1, crossDotP);

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

  636.     }

  637.     /** Apply the rotation to a pv coordinates.
  638.      * @param pv vector to apply the rotation to
  639.      * @return a new pv coordinates which is the image of u by the rotation
  640.      * @since 9.0
  641.      */
  642.     public FieldPVCoordinates<T> applyTo(final FieldPVCoordinates<T> pv) {

  643.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  644.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  645.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  646.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  647.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  648.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  649.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  650.                                                                   -2, crossV,
  651.                                                                   -1, crossCrossP,
  652.                                                                   -1, crossDotP);

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

  654.     }

  655.     /** Apply the rotation to a pv coordinates.
  656.      * @param pv vector to apply the rotation to
  657.      * @return a new pv coordinates which is the image of u by the rotation
  658.      * @since 9.0
  659.      */
  660.     public TimeStampedFieldPVCoordinates<T> applyTo(final TimeStampedFieldPVCoordinates<T> pv) {

  661.         final FieldVector3D<T> transformedP = rotation.applyTo(pv.getPosition());
  662.         final FieldVector3D<T> crossP       = FieldVector3D.crossProduct(rotationRate, transformedP);
  663.         final FieldVector3D<T> transformedV = rotation.applyTo(pv.getVelocity()).subtract(crossP);
  664.         final FieldVector3D<T> crossV       = FieldVector3D.crossProduct(rotationRate, transformedV);
  665.         final FieldVector3D<T> crossCrossP  = FieldVector3D.crossProduct(rotationRate, crossP);
  666.         final FieldVector3D<T> crossDotP    = FieldVector3D.crossProduct(rotationAcceleration, transformedP);
  667.         final FieldVector3D<T> transformedA = new FieldVector3D<>( 1, rotation.applyTo(pv.getAcceleration()),
  668.                                                                   -2, crossV,
  669.                                                                   -1, crossCrossP,
  670.                                                                   -1, crossDotP);

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

  672.     }

  673.     /** Convert rotation, rate and acceleration to modified Rodrigues vector and derivatives.
  674.      * <p>
  675.      * The modified Rodrigues vector is tan(θ/4) u where θ and u are the
  676.      * rotation angle and axis respectively.
  677.      * </p>
  678.      * @param sign multiplicative sign for quaternion components
  679.      * @return modified Rodrigues vector and derivatives (vector on row 0, first derivative
  680.      * on row 1, second derivative on row 2)
  681.      * @see #createFromModifiedRodrigues(RealFieldElement[][])
  682.      * @since 9.0
  683.      */
  684.     public T[][] getModifiedRodrigues(final double sign) {

  685.         final T q0    = getRotation().getQ0().multiply(sign);
  686.         final T q1    = getRotation().getQ1().multiply(sign);
  687.         final T q2    = getRotation().getQ2().multiply(sign);
  688.         final T q3    = getRotation().getQ3().multiply(sign);
  689.         final T oX    = getRotationRate().getX();
  690.         final T oY    = getRotationRate().getY();
  691.         final T oZ    = getRotationRate().getZ();
  692.         final T oXDot = getRotationAcceleration().getX();
  693.         final T oYDot = getRotationAcceleration().getY();
  694.         final T oZDot = getRotationAcceleration().getZ();

  695.         // first time-derivatives of the quaternion
  696.         final T q0Dot = q0.linearCombination(q1.negate(), oX, q2.negate(), oY, q3.negate(), oZ).multiply(0.5);
  697.         final T q1Dot = q0.linearCombination( q0, oX, q3.negate(), oY,  q2, oZ).multiply(0.5);
  698.         final T q2Dot = q0.linearCombination( q3, oX,  q0, oY, q1.negate(), oZ).multiply(0.5);
  699.         final T q3Dot = q0.linearCombination(q2.negate(), oX,  q1, oY,  q0, oZ).multiply(0.5);

  700.         // second time-derivatives of the quaternion
  701.         final T q0DotDot = linearCombination(q1, oXDot, q2, oYDot, q3, oZDot,
  702.                                              q1Dot, oX, q2Dot, oY, q3Dot, oZ).
  703.                            multiply(-0.5);
  704.         final T q1DotDot = linearCombination(q0, oXDot, q2, oZDot, q3.negate(), oYDot,
  705.                                              q0Dot, oX, q2Dot, oZ, q3Dot.negate(), oY).
  706.                            multiply(0.5);
  707.         final T q2DotDot = linearCombination(q0, oYDot, q3, oXDot, q1.negate(), oZDot,
  708.                                              q0Dot, oY, q3Dot, oX, q1Dot.negate(), oZ).
  709.                            multiply(0.5);
  710.         final T q3DotDot = linearCombination(q0, oZDot, q1, oYDot, q2.negate(), oXDot,
  711.                                              q0Dot, oZ, q1Dot, oY, q2Dot.negate(), oX).
  712.                            multiply(0.5);

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

  719.         final T r1       = inv.multiply(q1);
  720.         final T r2       = inv.multiply(q2);
  721.         final T r3       = inv.multiply(q3);

  722.         final T mInvR1   = inv.multiply(r1).negate();
  723.         final T mInvR2   = inv.multiply(r2).negate();
  724.         final T mInvR3   = inv.multiply(r3).negate();

  725.         final T r1Dot    = q0.linearCombination(inv, q1Dot, mInvR1, q0Dot);
  726.         final T r2Dot    = q0.linearCombination(inv, q2Dot, mInvR2, q0Dot);
  727.         final T r3Dot    = q0.linearCombination(inv, q3Dot, mInvR3, q0Dot);

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

  731.         final T[][] rodrigues = MathArrays.buildArray(q0.getField(), 3, 3);
  732.         rodrigues[0][0] = r1;
  733.         rodrigues[0][1] = r2;
  734.         rodrigues[0][2] = r3;
  735.         rodrigues[1][0] = r1Dot;
  736.         rodrigues[1][1] = r2Dot;
  737.         rodrigues[1][2] = r3Dot;
  738.         rodrigues[2][0] = r1DotDot;
  739.         rodrigues[2][1] = r2DotDot;
  740.         rodrigues[2][2] = r3DotDot;
  741.         return rodrigues;

  742.     }

  743.     /**
  744.      * Compute a linear combination.
  745.      * @param a1 first factor of the first term
  746.      * @param b1 second factor of the first term
  747.      * @param a2 first factor of the second term
  748.      * @param b2 second factor of the second term
  749.      * @param a3 first factor of the third term
  750.      * @param b3 second factor of the third term
  751.      * @param a4 first factor of the fourth term
  752.      * @param b4 second factor of the fourth term
  753.      * @param a5 first factor of the fifth term
  754.      * @param b5 second factor of the fifth term
  755.      * @param a6 first factor of the sixth term
  756.      * @param b6 second factor of the sicth term
  757.      * @return a<sub>1</sub>&times;b<sub>1</sub> + a<sub>2</sub>&times;b<sub>2</sub> +
  758.      * a<sub>3</sub>&times;b<sub>3</sub> + a<sub>4</sub>&times;b<sub>4</sub> +
  759.      * a<sub>5</sub>&times;b<sub>5</sub> + a<sub>6</sub>&times;b<sub>6</sub>
  760.      */
  761.     private T linearCombination(final T a1, final T b1, final T a2, final T b2, final T a3, final T b3,
  762.                                 final T a4, final T b4, final T a5, final T b5, final T a6, final T b6) {

  763.         final T[] a = MathArrays.buildArray(a1.getField(), 6);
  764.         a[0] = a1;
  765.         a[1] = a2;
  766.         a[2] = a3;
  767.         a[3] = a4;
  768.         a[4] = a5;
  769.         a[5] = a6;

  770.         final T[] b = MathArrays.buildArray(b1.getField(), 6);
  771.         b[0] = b1;
  772.         b[1] = b2;
  773.         b[2] = b3;
  774.         b[3] = b4;
  775.         b[4] = b5;
  776.         b[5] = b6;

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

  778.     }

  779.     /** Convert a modified Rodrigues vector and derivatives to angular coordinates.
  780.      * @param r modified Rodrigues vector (with first and second times derivatives)
  781.      * @param <T> the type of the field elements
  782.      * @return angular coordinates
  783.      * @see #getModifiedRodrigues(double)
  784.      * @since 9.0
  785.      */
  786.     public static <T extends RealFieldElement<T>>  FieldAngularCoordinates<T> createFromModifiedRodrigues(final T[][] r) {

  787.         // rotation
  788.         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]));
  789.         final T oPQ0     = rSquared.add(1).reciprocal().multiply(2);
  790.         final T q0       = oPQ0.subtract(1);
  791.         final T q1       = oPQ0.multiply(r[0][0]);
  792.         final T q2       = oPQ0.multiply(r[0][1]);
  793.         final T q3       = oPQ0.multiply(r[0][2]);

  794.         // rotation rate
  795.         final T oPQ02    = oPQ0.multiply(oPQ0);
  796.         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();
  797.         final T q1Dot    = oPQ0.multiply(r[1][0]).add(r[0][0].multiply(q0Dot));
  798.         final T q2Dot    = oPQ0.multiply(r[1][1]).add(r[0][1].multiply(q0Dot));
  799.         final T q3Dot    = oPQ0.multiply(r[1][2]).add(r[0][2].multiply(q0Dot));
  800.         final T oX       = q0.linearCombination(q1.negate(), q0Dot,  q0, q1Dot,  q3, q2Dot, q2.negate(), q3Dot).multiply(2);
  801.         final T oY       = q0.linearCombination(q2.negate(), q0Dot, q3.negate(), q1Dot,  q0, q2Dot,  q1, q3Dot).multiply(2);
  802.         final T oZ       = q0.linearCombination(q3.negate(), q0Dot,  q2, q1Dot, q1.negate(), q2Dot,  q0, q3Dot).multiply(2);

  803.         // rotation acceleration
  804.         final T q0DotDot = q0.subtract(1).negate().divide(oPQ0).multiply(q0Dot).multiply(q0Dot).
  805.                            subtract(oPQ02.multiply(q0.linearCombination(r[0][0], r[2][0], r[0][1], r[2][1], r[0][2], r[2][2]))).
  806.                            subtract(q1Dot.multiply(q1Dot).add(q2Dot.multiply(q2Dot)).add(q3Dot.multiply(q3Dot)));
  807.         final T q1DotDot = q0.linearCombination(oPQ0, r[2][0], r[1][0].add(r[1][0]), q0Dot, r[0][0], q0DotDot);
  808.         final T q2DotDot = q0.linearCombination(oPQ0, r[2][1], r[1][1].add(r[1][1]), q0Dot, r[0][1], q0DotDot);
  809.         final T q3DotDot = q0.linearCombination(oPQ0, r[2][2], r[1][2].add(r[1][2]), q0Dot, r[0][2], q0DotDot);
  810.         final T oXDot    = q0.linearCombination(q1.negate(), q0DotDot,  q0, q1DotDot,  q3, q2DotDot, q2.negate(), q3DotDot).multiply(2);
  811.         final T oYDot    = q0.linearCombination(q2.negate(), q0DotDot, q3.negate(), q1DotDot,  q0, q2DotDot,  q1, q3DotDot).multiply(2);
  812.         final T oZDot    = q0.linearCombination(q3.negate(), q0DotDot,  q2, q1DotDot, q1.negate(), q2DotDot,  q0, q3DotDot).multiply(2);

  813.         return new FieldAngularCoordinates<>(new FieldRotation<>(q0, q1, q2, q3, false),
  814.                                              new FieldVector3D<>(oX, oY, oZ),
  815.                                              new FieldVector3D<>(oXDot, oYDot, oZDot));

  816.     }

  817. }