View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.geometry.euclidean.threed;
24  
25  import java.text.DecimalFormat;
26  import java.text.DecimalFormatSymbols;
27  import java.text.NumberFormat;
28  import java.util.Locale;
29  
30  import org.hipparchus.Field;
31  import org.hipparchus.analysis.differentiation.DSFactory;
32  import org.hipparchus.analysis.differentiation.DerivativeStructure;
33  import org.hipparchus.exception.MathIllegalArgumentException;
34  import org.hipparchus.exception.MathRuntimeException;
35  import org.hipparchus.random.Well1024a;
36  import org.hipparchus.util.Binary64;
37  import org.hipparchus.util.Binary64Field;
38  import org.hipparchus.util.FastMath;
39  import org.hipparchus.util.Precision;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  public class FieldVector3DTest {
44  
45      @Test
46      public void testConstructors() throws MathIllegalArgumentException {
47          double cosAlpha = 1 / 2.0;
48          double sinAlpha = FastMath.sqrt(3) / 2.0;
49          double cosDelta = FastMath.sqrt(2) / 2.0;
50          double sinDelta = -FastMath.sqrt(2) / 2.0;
51          DSFactory factory21 = new DSFactory(2, 1);
52          DSFactory factory31 = new DSFactory(3, 1);
53          DSFactory factory41 = new DSFactory(4, 1);
54          FieldVector3D<DerivativeStructure> u = new FieldVector3D<>(2,
55                                                                     new FieldVector3D<>(factory21.variable(0,  FastMath.PI / 3),
56                                                                                         factory21.variable(1, -FastMath.PI / 4)));
57          checkVector(u, 2 * cosAlpha * cosDelta, 2 * sinAlpha * cosDelta, 2 * sinDelta);
58          Assert.assertEquals(-2 * sinAlpha * cosDelta, u.getX().getPartialDerivative(1, 0), 1.0e-12);
59          Assert.assertEquals(+2 * cosAlpha * cosDelta, u.getY().getPartialDerivative(1, 0), 1.0e-12);
60          Assert.assertEquals(0,                        u.getZ().getPartialDerivative(1, 0), 1.0e-12);
61          Assert.assertEquals(-2 * cosAlpha * sinDelta, u.getX().getPartialDerivative(0, 1), 1.0e-12);
62          Assert.assertEquals(-2 * sinAlpha * sinDelta, u.getY().getPartialDerivative(0, 1), 1.0e-12);
63          Assert.assertEquals(2 * cosDelta,             u.getZ().getPartialDerivative(0, 1), 1.0e-12);
64  
65          checkVector(new FieldVector3D<>(2, createVector(1, 0,  0, 3)),
66                      2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2);
67          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
68                                          createVector(1, 0,  0, 4)),
69                      2, 0, 0, 2, 0, 0, 1, 0, 2, 0, 0, 0, 0, 2, 0);
70          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
71                                          new Vector3D(1, 0,  0)),
72                      2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
73  
74          checkVector(new FieldVector3D<>(2, createVector(1, 0,  0, 3),
75                                         -3, createVector(0, 0, -1, 3)),
76                      2, 0, 3, -1, 0, 0, 0, -1, 0, 0, 0, -1);
77          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
78                                          createVector(1, 0,  0, 4),
79                                          factory41.variable(3, -3.0),
80                                          createVector(0, 0, -1, 4)),
81                      2, 0, 3, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, -1);
82          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
83                                          new Vector3D(1, 0,  0),
84                                          factory41.variable(3, -3.0),
85                                          new Vector3D(0, 0, -1)),
86                      2, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1);
87  
88          checkVector(new FieldVector3D<>(2, createVector(1, 0, 0, 3),
89                                          5, createVector(0, 1, 0, 3),
90                                         -3, createVector(0, 0, -1, 3)),
91                      2, 5, 3, 4, 0, 0, 0, 4, 0, 0, 0, 4);
92          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
93                                          createVector(1, 0,  0, 4),
94                                          factory41.variable(3,  5.0),
95                                          createVector(0, 1,  0, 4),
96                                          factory41.variable(3, -3.0),
97                                          createVector(0, 0, -1, 4)),
98                      2, 5, 3, 4, 0, 0, 1, 0, 4, 0, 1, 0, 0, 4, -1);
99          checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
100                                         new Vector3D(1, 0,  0),
101                                         factory41.variable(3,  5.0),
102                                         new Vector3D(0, 1,  0),
103                                         factory41.variable(3, -3.0),
104                                         new Vector3D(0, 0, -1)),
105                     2, 5, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, -1);
106 
107         checkVector(new FieldVector3D<>(2, createVector(1, 0, 0, 3),
108                                         5, createVector(0, 1, 0, 3),
109                                         5, createVector(0, -1, 0, 3),
110                                        -3, createVector(0, 0, -1, 3)),
111                     2, 0, 3, 9, 0, 0, 0, 9, 0, 0, 0, 9);
112         checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
113                                         createVector(1, 0,  0, 4),
114                                         factory41.variable(3,  5.0),
115                                         createVector(0, 1,  0, 4),
116                                         factory41.variable(3,  5.0),
117                                         createVector(0, -1,  0, 4),
118                                         factory41.variable(3, -3.0),
119                                         createVector(0, 0, -1, 4)),
120                     2, 0, 3, 9, 0, 0, 1, 0, 9, 0, 0, 0, 0, 9, -1);
121         checkVector(new FieldVector3D<>(factory41.variable(3,  2.0),
122                                         new Vector3D(1, 0,  0),
123                                         factory41.variable(3,  5.0),
124                                         new Vector3D(0, 1,  0),
125                                         factory41.variable(3,  5.0),
126                                         new Vector3D(0, -1,  0),
127                                         factory41.variable(3, -3.0),
128                                         new Vector3D(0, 0, -1)),
129                     2, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1);
130 
131         checkVector(new FieldVector3D<DerivativeStructure>(new DerivativeStructure[] {
132             factory31.variable(2,  2),
133             factory31.variable(1,  5),
134             factory31.variable(0, -3)
135         }),
136         2, 5, -3, 0, 0, 1, 0, 1, 0, 1, 0, 0);
137 
138     }
139 
140     @SuppressWarnings("unlikely-arg-type")
141     @Test
142     public void testEquals() {
143         DSFactory factory31 = new DSFactory(3, 1);
144         FieldVector3D<DerivativeStructure> u1 = createVector(1, 2, 3, 3);
145         FieldVector3D<DerivativeStructure> v  = createVector(1, 2, 3 + 10 * Precision.EPSILON, 3);
146         Assert.assertTrue(u1.equals(u1));
147         Assert.assertTrue(u1.equals(new FieldVector3D<>(factory31.variable(0, 1.0),
148                                                         factory31.variable(1, 2.0),
149                                                         factory31.variable(2, 3.0))));
150         Assert.assertFalse(u1.equals(new FieldVector3D<>(factory31.constant(1.0),
151                                                          factory31.variable(1, 2.0),
152                                                          factory31.variable(2, 3.0))));
153         Assert.assertFalse(u1.equals(new FieldVector3D<>(factory31.variable(0, 1.0),
154                                                          factory31.constant(2.0),
155                                                          factory31.variable(2, 3.0))));
156         Assert.assertFalse(u1.equals(new FieldVector3D<>(factory31.variable(0, 1.0),
157                                                          factory31.variable(1, 2.0),
158                                                          factory31.constant(3.0))));
159         Assert.assertFalse(u1.equals(v));
160         Assert.assertFalse(u1.equals(u1.toVector3D()));
161         Assert.assertTrue(createVector(0, Double.NaN, 0, 3).equals(createVector(0, 0, Double.NaN, 3)));
162     }
163 
164     @Test
165     public void testHash() {
166         Assert.assertEquals(createVector(0, Double.NaN, 0, 3).hashCode(), createVector(0, 0, Double.NaN, 3).hashCode());
167         FieldVector3D<DerivativeStructure> u = createVector(1, 2, 3, 3);
168         FieldVector3D<DerivativeStructure> v = createVector(1, 2, 3 + 10 * Precision.EPSILON, 3);
169         Assert.assertTrue(u.hashCode() != v.hashCode());
170     }
171 
172     @Test
173     public void testInfinite() {
174         Assert.assertTrue(createVector(1, 1, Double.NEGATIVE_INFINITY, 3).isInfinite());
175         Assert.assertTrue(createVector(1, Double.NEGATIVE_INFINITY, 1, 3).isInfinite());
176         Assert.assertTrue(createVector(Double.NEGATIVE_INFINITY, 1, 1, 3).isInfinite());
177         Assert.assertFalse(createVector(1, 1, 2, 3).isInfinite());
178         Assert.assertFalse(createVector(1, Double.NaN, Double.NEGATIVE_INFINITY, 3).isInfinite());
179     }
180 
181     @Test
182     public void testNaN() {
183         Assert.assertTrue(createVector(1, 1, Double.NaN, 3).isNaN());
184         Assert.assertTrue(createVector(1, Double.NaN, 1, 3).isNaN());
185         Assert.assertTrue(createVector(Double.NaN, 1, 1, 3).isNaN());
186         Assert.assertFalse(createVector(1, 1, 2, 3).isNaN());
187         Assert.assertFalse(createVector(1, 1, Double.NEGATIVE_INFINITY, 3).isNaN());
188     }
189 
190     @Test
191     public void testCanonical() {
192 
193         final Field<Binary64> field = Binary64Field.getInstance();
194 
195         Assert.assertEquals(0.0, FieldVector3D.getZero(field).getNorm().getReal(), 1.0e-20);
196         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getPlusI(field), Vector3D.PLUS_I).getReal(),   1.0e-20);
197         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getMinusI(field), Vector3D.MINUS_I).getReal(), 1.0e-20);
198         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getPlusJ(field), Vector3D.PLUS_J).getReal(),   1.0e-20);
199         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getMinusJ(field), Vector3D.MINUS_J).getReal(), 1.0e-20);
200         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getPlusK(field), Vector3D.PLUS_K).getReal(),   1.0e-20);
201         Assert.assertEquals(0.0, FieldVector3D.angle(FieldVector3D.getMinusK(field), Vector3D.MINUS_K).getReal(), 1.0e-20);
202         Assert.assertTrue(FieldVector3D.getNaN(field).isNaN());
203         Assert.assertTrue(FieldVector3D.getPositiveInfinity(field).isInfinite());
204         Assert.assertTrue(FieldVector3D.getNegativeInfinity(field).isInfinite());
205 
206         // new instances are created each time
207         Assert.assertNotSame(FieldVector3D.getZero(field),            FieldVector3D.getZero(field));
208         Assert.assertNotSame(FieldVector3D.getPlusI(field),           FieldVector3D.getPlusI(field));
209         Assert.assertNotSame(FieldVector3D.getMinusI(field),          FieldVector3D.getMinusI(field));
210         Assert.assertNotSame(FieldVector3D.getPlusJ(field),           FieldVector3D.getPlusJ(field));
211         Assert.assertNotSame(FieldVector3D.getMinusJ(field),          FieldVector3D.getMinusJ(field));
212         Assert.assertNotSame(FieldVector3D.getPlusK(field),           FieldVector3D.getPlusK(field));
213         Assert.assertNotSame(FieldVector3D.getMinusK(field),          FieldVector3D.getMinusK(field));
214         Assert.assertNotSame(FieldVector3D.getNaN(field),             FieldVector3D.getNaN(field));
215         Assert.assertNotSame(FieldVector3D.getPositiveInfinity(field),FieldVector3D.getPositiveInfinity(field));
216         Assert.assertNotSame(FieldVector3D.getNegativeInfinity(field),FieldVector3D.getNegativeInfinity(field));
217 
218     }
219 
220     @Test
221     public void testToString() {
222         Assert.assertEquals("{3; 2; 1}", createVector(3, 2, 1, 3).toString());
223         NumberFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US));
224         Assert.assertEquals("{3.000; 2.000; 1.000}", createVector(3, 2, 1, 3).toString(format));
225     }
226 
227     @Test(expected=MathIllegalArgumentException.class)
228     public void testWrongDimension() throws MathIllegalArgumentException {
229         DSFactory factory31 = new DSFactory(3, 1);
230         new FieldVector3D<DerivativeStructure>(new DerivativeStructure[] {
231             factory31.variable(0, 2),
232             factory31.variable(0, 5)
233         });
234     }
235 
236     @Test
237     public void testCoordinates() {
238         FieldVector3D<DerivativeStructure> v = createVector(1, 2, 3, 3);
239         Assert.assertTrue(FastMath.abs(v.getX().getReal() - 1) < 1.0e-12);
240         Assert.assertTrue(FastMath.abs(v.getY().getReal() - 2) < 1.0e-12);
241         Assert.assertTrue(FastMath.abs(v.getZ().getReal() - 3) < 1.0e-12);
242         DerivativeStructure[] coordinates = v.toArray();
243         Assert.assertTrue(FastMath.abs(coordinates[0].getReal() - 1) < 1.0e-12);
244         Assert.assertTrue(FastMath.abs(coordinates[1].getReal() - 2) < 1.0e-12);
245         Assert.assertTrue(FastMath.abs(coordinates[2].getReal() - 3) < 1.0e-12);
246     }
247 
248     @Test
249     public void testNorm1() {
250         Assert.assertEquals( 0.0, createVector(0, 0, 0, 3).getNorm1().getReal(), 0);
251         Assert.assertEquals( 6.0, createVector(1, -2, 3, 3).getNorm1().getReal(), 0);
252         Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(1, 0, 0), 0);
253         Assert.assertEquals(-1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(0, 1, 0), 0);
254         Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(0, 0, 1), 0);
255     }
256 
257     @Test
258     public void testNorm() {
259         double r = FastMath.sqrt(14);
260         Assert.assertEquals(0.0, createVector(0, 0, 0, 3).getNorm().getReal(), 0);
261         Assert.assertEquals(r, createVector(1, 2, 3, 3).getNorm().getReal(), 1.0e-12);
262         Assert.assertEquals( 1.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(1, 0, 0), 0);
263         Assert.assertEquals( 2.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(0, 1, 0), 0);
264         Assert.assertEquals( 3.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(0, 0, 1), 0);
265     }
266 
267     @Test
268     public void testNormSq() {
269         Assert.assertEquals(0.0, createVector(0, 0, 0, 3).getNormSq().getReal(), 0);
270         Assert.assertEquals(14, createVector(1, 2, 3, 3).getNormSq().getReal(), 1.0e-12);
271         Assert.assertEquals( 2, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(1, 0, 0), 0);
272         Assert.assertEquals( 4, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(0, 1, 0), 0);
273         Assert.assertEquals( 6, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(0, 0, 1), 0);
274     }
275 
276     @Test
277     public void testNormInf() {
278         Assert.assertEquals( 0.0, createVector(0, 0, 0, 3).getNormInf().getReal(), 0);
279         Assert.assertEquals( 3.0, createVector(1, -2, 3, 3).getNormInf().getReal(), 0);
280         Assert.assertEquals( 0.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
281         Assert.assertEquals( 0.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
282         Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
283         Assert.assertEquals( 3.0, createVector(2, -1, 3, 3).getNormInf().getReal(), 0);
284         Assert.assertEquals( 0.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
285         Assert.assertEquals( 0.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
286         Assert.assertEquals( 1.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
287         Assert.assertEquals( 3.0, createVector(1, -3, 2, 3).getNormInf().getReal(), 0);
288         Assert.assertEquals( 0.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
289         Assert.assertEquals(-1.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
290         Assert.assertEquals( 0.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
291         Assert.assertEquals( 3.0, createVector(2, -3, 1, 3).getNormInf().getReal(), 0);
292         Assert.assertEquals( 0.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
293         Assert.assertEquals(-1.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
294         Assert.assertEquals( 0.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
295         Assert.assertEquals( 3.0, createVector(3, -1, 2, 3).getNormInf().getReal(), 0);
296         Assert.assertEquals( 1.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
297         Assert.assertEquals( 0.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
298         Assert.assertEquals( 0.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
299         Assert.assertEquals( 3.0, createVector(3, -2, 1, 3).getNormInf().getReal(), 0);
300         Assert.assertEquals( 1.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(1, 0, 0), 0);
301         Assert.assertEquals( 0.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(0, 1, 0), 0);
302         Assert.assertEquals( 0.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(0, 0, 1), 0);
303     }
304 
305     @Test
306     public void testDistance1() {
307         FieldVector3D<DerivativeStructure> v1 = createVector(1, -2, 3, 3);
308         FieldVector3D<DerivativeStructure> v2 = createVector(-4, 2, 0, 3);
309         Assert.assertEquals(0.0, FieldVector3D.distance1(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0);
310         DerivativeStructure distance = FieldVector3D.distance1(v1, v2);
311         Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
312         Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
313         Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
314         Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
315         distance = FieldVector3D.distance1(v1, new Vector3D(-4, 2, 0));
316         Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
317         Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
318         Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
319         Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
320         distance = FieldVector3D.distance1(new Vector3D(-4, 2, 0), v1);
321         Assert.assertEquals(12.0, distance.getReal(), 1.0e-12);
322         Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
323         Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
324         Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
325     }
326 
327     @Test
328     public void testDistance() {
329         FieldVector3D<DerivativeStructure> v1 = createVector(1, -2, 3, 3);
330         FieldVector3D<DerivativeStructure> v2 = createVector(-4, 2, 0, 3);
331         Assert.assertEquals(0.0, FieldVector3D.distance(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0);
332         DerivativeStructure distance = FieldVector3D.distance(v1, v2);
333         Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12);
334         Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
335         Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
336         Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
337         distance = FieldVector3D.distance(v1, new Vector3D(-4, 2, 0));
338         Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12);
339         Assert.assertEquals( 5 / FastMath.sqrt(50), distance.getPartialDerivative(1, 0, 0), 1.0e-12);
340         Assert.assertEquals(-4 / FastMath.sqrt(50), distance.getPartialDerivative(0, 1, 0), 1.0e-12);
341         Assert.assertEquals( 3 / FastMath.sqrt(50), distance.getPartialDerivative(0, 0, 1), 1.0e-12);
342         distance = FieldVector3D.distance(new Vector3D(-4, 2, 0), v1);
343         Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12);
344         Assert.assertEquals( 5 / FastMath.sqrt(50), distance.getPartialDerivative(1, 0, 0), 1.0e-12);
345         Assert.assertEquals(-4 / FastMath.sqrt(50), distance.getPartialDerivative(0, 1, 0), 1.0e-12);
346         Assert.assertEquals( 3 / FastMath.sqrt(50), distance.getPartialDerivative(0, 0, 1), 1.0e-12);
347     }
348 
349     @Test
350     public void testDistanceSq() {
351         FieldVector3D<DerivativeStructure> v1 = createVector(1, -2, 3, 3);
352         FieldVector3D<DerivativeStructure> v2 = createVector(-4, 2, 0, 3);
353         Assert.assertEquals(0.0, FieldVector3D.distanceSq(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0);
354         DerivativeStructure distanceSq = FieldVector3D.distanceSq(v1, v2);
355         Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12);
356         Assert.assertEquals(0, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12);
357         Assert.assertEquals(0, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12);
358         Assert.assertEquals(0, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12);
359         distanceSq = FieldVector3D.distanceSq(v1, new Vector3D(-4, 2, 0));
360         Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12);
361         Assert.assertEquals(10, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12);
362         Assert.assertEquals(-8, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12);
363         Assert.assertEquals( 6, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12);
364         distanceSq = FieldVector3D.distanceSq(new Vector3D(-4, 2, 0), v1);
365         Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12);
366         Assert.assertEquals(10, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12);
367         Assert.assertEquals(-8, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12);
368         Assert.assertEquals( 6, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12);
369   }
370 
371     @Test
372     public void testDistanceInf() {
373         FieldVector3D<DerivativeStructure> v1 = createVector(1, -2, 3, 3);
374         FieldVector3D<DerivativeStructure> v2 = createVector(-4, 2, 0, 3);
375         Assert.assertEquals(0.0, FieldVector3D.distanceInf(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0);
376         DerivativeStructure distance = FieldVector3D.distanceInf(v1, v2);
377         Assert.assertEquals(5.0, distance.getReal(), 1.0e-12);
378         Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
379         Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
380         Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
381         distance = FieldVector3D.distanceInf(v1, new Vector3D(-4, 2, 0));
382         Assert.assertEquals(5.0, distance.getReal(), 1.0e-12);
383         Assert.assertEquals(1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
384         Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
385         Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
386         distance = FieldVector3D.distanceInf(new Vector3D(-4, 2, 0), v1);
387         Assert.assertEquals(5.0, distance.getReal(), 1.0e-12);
388         Assert.assertEquals(1, distance.getPartialDerivative(1, 0, 0), 1.0e-12);
389         Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12);
390         Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12);
391         Assert.assertEquals(v1.subtract(v2).getNormInf().getReal(), FieldVector3D.distanceInf(v1, v2).getReal(), 1.0e-12);
392 
393         Assert.assertEquals(5.0,
394                             FieldVector3D.distanceInf(createVector( 1, -2, 3, 3), createVector(-4,  2, 0, 3)).getReal(),
395                             1.0e-12);
396         Assert.assertEquals(5.0,
397                             FieldVector3D.distanceInf(createVector( 1, 3, -2, 3), createVector(-4, 0,  2, 3)).getReal(),
398                             1.0e-12);
399         Assert.assertEquals(5.0,
400                             FieldVector3D.distanceInf(createVector(-2,  1, 3, 3), createVector( 2, -4, 0, 3)).getReal(),
401                             1.0e-12);
402         Assert.assertEquals(5.0,
403                             FieldVector3D.distanceInf(createVector(-2, 3,  1, 3), createVector( 2, 0, -4, 3)).getReal(),
404                             1.0e-12);
405         Assert.assertEquals(5.0,
406                             FieldVector3D.distanceInf(createVector(3, -2,  1, 3), createVector(0,  2, -4, 3)).getReal(),
407                             1.0e-12);
408         Assert.assertEquals(5.0,
409                             FieldVector3D.distanceInf(createVector(3,  1, -2, 3), createVector(0, -4,  2, 3)).getReal(),
410                             1.0e-12);
411 
412         Assert.assertEquals(5.0,
413                             FieldVector3D.distanceInf(createVector( 1, -2, 3, 3), new Vector3D(-4,  2, 0)).getReal(),
414                             1.0e-12);
415         Assert.assertEquals(5.0,
416                             FieldVector3D.distanceInf(createVector( 1, 3, -2, 3), new Vector3D(-4, 0,  2)).getReal(),
417                             1.0e-12);
418         Assert.assertEquals(5.0,
419                             FieldVector3D.distanceInf(createVector(-2,  1, 3, 3), new Vector3D( 2, -4, 0)).getReal(),
420                             1.0e-12);
421         Assert.assertEquals(5.0,
422                             FieldVector3D.distanceInf(createVector(-2, 3,  1, 3), new Vector3D( 2, 0, -4)).getReal(),
423                             1.0e-12);
424         Assert.assertEquals(5.0,
425                             FieldVector3D.distanceInf(createVector(3, -2,  1, 3), new Vector3D(0,  2, -4)).getReal(),
426                             1.0e-12);
427         Assert.assertEquals(5.0,
428                             FieldVector3D.distanceInf(createVector(3,  1, -2, 3), new Vector3D(0, -4,  2)).getReal(),
429                             1.0e-12);
430 
431     }
432 
433     @Test
434     public void testSubtract() {
435         FieldVector3D<DerivativeStructure> v1 = createVector(1, 2, 3, 3);
436         FieldVector3D<DerivativeStructure> v2 = createVector(-3, -2, -1, 3);
437         v1 = v1.subtract(v2);
438         checkVector(v1, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0);
439 
440         checkVector(v2.subtract(v1), -7, -6, -5, 1, 0, 0, 0, 1, 0, 0, 0, 1);
441         checkVector(v2.subtract(new Vector3D(4, 4, 4)), -7, -6, -5, 1, 0, 0, 0, 1, 0, 0, 0, 1);
442         checkVector(v2.subtract(3, v1), -15, -14, -13, 1, 0, 0, 0, 1, 0, 0, 0, 1);
443         checkVector(v2.subtract(3, new Vector3D(4, 4, 4)), -15, -14, -13, 1, 0, 0, 0, 1, 0, 0, 0, 1);
444         DSFactory factory31 = new DSFactory(3, 1);
445         checkVector(v2.subtract(factory31.variable(2, 3), new Vector3D(4, 4, 4)),
446                     -15, -14, -13, 1, 0, -4, 0, 1, -4, 0, 0, -3);
447 
448         DSFactory factory41 = new DSFactory(4, 1);
449         checkVector(createVector(1, 2, 3, 4).subtract(factory41.variable(3, 5.0),
450                                                       createVector(3, -2, 1, 4)),
451                     -14, 12, -2,
452                      -4,  0,  0, -3,
453                       0, -4,  0,  2,
454                       0,  0, -4, -1);
455 
456     }
457 
458     @Test
459     public void testAdd() {
460         FieldVector3D<DerivativeStructure> v1 = createVector(1, 2, 3, 3);
461         FieldVector3D<DerivativeStructure> v2 = createVector(-3, -2, -1, 3);
462         v1 = v1.add(v2);
463         checkVector(v1, -2, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 2);
464 
465         checkVector(v2.add(v1), -5, -2, 1, 3, 0, 0, 0, 3, 0, 0, 0, 3);
466         checkVector(v2.add(new Vector3D(-2, 0, 2)), -5, -2, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1);
467         checkVector(v2.add(3, v1), -9, -2, 5, 7, 0, 0, 0, 7, 0, 0, 0, 7);
468         checkVector(v2.add(3, new Vector3D(-2, 0, 2)), -9, -2, 5, 1, 0, 0, 0, 1, 0, 0, 0, 1);
469         DSFactory factory31 = new DSFactory(3, 1);
470         checkVector(v2.add(factory31.variable(2, 3), new Vector3D(-2, 0, 2)),
471                     -9, -2, 5, 1, 0, -2, 0, 1, 0, 0, 0, 3);
472 
473         DSFactory factory41 = new DSFactory(4, 1);
474         checkVector(createVector(1, 2, 3, 4).add(factory41.variable(3, 5.0),
475                                                  createVector(3, -2, 1, 4)),
476                     16, -8,  8,
477                      6,  0,  0,  3,
478                      0,  6,  0, -2,
479                      0,  0,  6,  1);
480 
481     }
482 
483     @Test
484     public void testScalarProduct() {
485         FieldVector3D<DerivativeStructure> v = createVector(1, 2, 3, 3);
486         v = v.scalarMultiply(3);
487         checkVector(v, 3, 6, 9);
488 
489         checkVector(v.scalarMultiply(0.5), 1.5, 3, 4.5);
490     }
491 
492     @Test
493     public void testVectorialProducts() {
494         FieldVector3D<DerivativeStructure> v1 = createVector(2, 1, -4, 3);
495         FieldVector3D<DerivativeStructure> v2 = createVector(3, 1, -1, 3);
496 
497         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2).getReal() - 11) < 1.0e-12);
498         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2.toVector3D()).getReal() - 11) < 1.0e-12);
499         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1.toVector3D(), v2).getReal() - 11) < 1.0e-12);
500 
501         FieldVector3D<DerivativeStructure> v3 = FieldVector3D.crossProduct(v1, v2);
502         checkVector(v3, 3, -10, -1);
503         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
504         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);
505 
506         v3 = FieldVector3D.crossProduct(v1, v2.toVector3D());
507         checkVector(v3, 3, -10, -1);
508         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
509         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);
510 
511         v3 = FieldVector3D.crossProduct(v1.toVector3D(), v2);
512         checkVector(v3, 3, -10, -1);
513         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12);
514         Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12);
515 
516     }
517 
518     @Test
519     public void testCrossProductCancellation() {
520         FieldVector3D<DerivativeStructure> v1 = createVector(9070467121.0, 4535233560.0, 1, 3);
521         FieldVector3D<DerivativeStructure> v2 = createVector(9070467123.0, 4535233561.0, 1, 3);
522         checkVector(FieldVector3D.crossProduct(v1, v2), -1, 2, 1);
523 
524         double scale    = FastMath.scalb(1.0, 100);
525         FieldVector3D<DerivativeStructure> big1   = new FieldVector3D<>(scale, v1);
526         FieldVector3D<DerivativeStructure> small2 = new FieldVector3D<>(1 / scale, v2);
527         checkVector(FieldVector3D.crossProduct(big1, small2), -1, 2, 1);
528 
529     }
530 
531     @Test
532     public void testAngular() {
533         Assert.assertEquals(0,           createVector(1, 0, 0, 3).getAlpha().getReal(), 1.0e-10);
534         Assert.assertEquals(0,           createVector(1, 0, 0, 3).getDelta().getReal(), 1.0e-10);
535         Assert.assertEquals(FastMath.PI / 2, createVector(0, 1, 0, 3).getAlpha().getReal(), 1.0e-10);
536         Assert.assertEquals(0,           createVector(0, 1, 0, 3).getDelta().getReal(), 1.0e-10);
537         Assert.assertEquals(FastMath.PI / 2, createVector(0, 0, 1, 3).getDelta().getReal(), 1.0e-10);
538 
539         FieldVector3D<DerivativeStructure> u = createVector(-1, 1, -1, 3);
540         Assert.assertEquals(3 * FastMath.PI /4, u.getAlpha().getReal(), 1.0e-10);
541         Assert.assertEquals(-1.0 / FastMath.sqrt(3), u.getDelta().sin().getReal(), 1.0e-10);
542     }
543 
544     @Test
545     public void testAngularSeparation() throws MathRuntimeException {
546         FieldVector3D<DerivativeStructure> v1 = createVector(2, -1, 4, 3);
547 
548         FieldVector3D<DerivativeStructure>  k = v1.normalize();
549         FieldVector3D<DerivativeStructure>  i = k.orthogonal();
550         FieldVector3D<DerivativeStructure> v2 = k.scalarMultiply(FastMath.cos(1.2)).add(i.scalarMultiply(FastMath.sin(1.2)));
551 
552         Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2).getReal() - 1.2) < 1.0e-12);
553         Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2.toVector3D()).getReal() - 1.2) < 1.0e-12);
554         Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1.toVector3D(), v2).getReal() - 1.2) < 1.0e-12);
555 
556         try {
557             FieldVector3D.angle(v1, Vector3D.ZERO);
558             Assert.fail("an exception should have been thrown");
559         } catch (MathRuntimeException mae) {
560             // expected
561         }
562         Assert.assertEquals(0.0, FieldVector3D.angle(v1, v1.toVector3D()).getReal(), 1.0e-15);
563         Assert.assertEquals(FastMath.PI, FieldVector3D.angle(v1, v1.negate().toVector3D()).getReal(), 1.0e-15);
564 
565     }
566 
567     @Test
568     public void testNormalize() throws MathRuntimeException {
569         Assert.assertEquals(1.0, createVector(5, -4, 2, 3).normalize().getNorm().getReal(), 1.0e-12);
570         try {
571             createVector(0, 0, 0, 3).normalize();
572             Assert.fail("an exception should have been thrown");
573         } catch (MathRuntimeException ae) {
574             // expected behavior
575         }
576     }
577 
578     @Test
579     public void testNegate() {
580         checkVector(createVector(0.1, 2.5, 1.3, 3).negate(),
581                     -0.1, -2.5, -1.3, -1, 0, 0, 0, -1, 0, 0, 0, -1);
582     }
583 
584     @Test
585     public void testOrthogonal() throws MathRuntimeException {
586         FieldVector3D<DerivativeStructure> v1 = createVector(0.1, 2.5, 1.3, 3);
587         Assert.assertEquals(0.0, FieldVector3D.dotProduct(v1, v1.orthogonal()).getReal(), 1.0e-12);
588         FieldVector3D<DerivativeStructure> v2 = createVector(2.3, -0.003, 7.6, 3);
589         Assert.assertEquals(0.0, FieldVector3D.dotProduct(v2, v2.orthogonal()).getReal(), 1.0e-12);
590         FieldVector3D<DerivativeStructure> v3 = createVector(-1.7, 1.4, 0.2, 3);
591         Assert.assertEquals(0.0, FieldVector3D.dotProduct(v3, v3.orthogonal()).getReal(), 1.0e-12);
592         FieldVector3D<DerivativeStructure> v4 = createVector(4.2, 0.1, -1.8, 3);
593         Assert.assertEquals(0.0, FieldVector3D.dotProduct(v4, v4.orthogonal()).getReal(), 1.0e-12);
594         try {
595             createVector(0, 0, 0, 3).orthogonal();
596             Assert.fail("an exception should have been thrown");
597         } catch (MathRuntimeException ae) {
598             // expected behavior
599         }
600     }
601 
602     @Test
603     public void testAngle() throws MathRuntimeException {
604         Assert.assertEquals(0.22572612855273393616,
605                             FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(4, 5, 6, 3)).getReal(),
606                             1.0e-12);
607         Assert.assertEquals(7.98595620686106654517199e-8,
608                             FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(2, 4, 6.000001, 3)).getReal(),
609                             1.0e-12);
610         Assert.assertEquals(3.14159257373023116985197793156,
611                             FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(-2, -4, -6.000001, 3)).getReal(),
612                             1.0e-12);
613         try {
614             FieldVector3D.angle(createVector(0, 0, 0, 3), createVector(1, 0, 0, 3));
615             Assert.fail("an exception should have been thrown");
616         } catch (MathRuntimeException ae) {
617             // expected behavior
618         }
619     }
620 
621     @Test
622     public void testAccurateDotProduct() {
623         // the following two vectors are nearly but not exactly orthogonal
624         // naive dot product (i.e. computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z
625         // leads to a result of 0.0, instead of the correct -1.855129...
626         FieldVector3D<DerivativeStructure> u1 = createVector(-1321008684645961.0 /  268435456.0,
627                                    -5774608829631843.0 /  268435456.0,
628                                    -7645843051051357.0 / 8589934592.0, 3);
629         FieldVector3D<DerivativeStructure> u2 = createVector(-5712344449280879.0 /    2097152.0,
630                                    -4550117129121957.0 /    2097152.0,
631                                     8846951984510141.0 /     131072.0, 3);
632         DerivativeStructure sNaive = u1.getX().multiply(u2.getX()).add(u1.getY().multiply(u2.getY())).add(u1.getZ().multiply(u2.getZ()));
633         DerivativeStructure sAccurate = FieldVector3D.dotProduct(u1, u2);
634         Assert.assertEquals(0.0, sNaive.getReal(), 1.0e-30);
635         Assert.assertEquals(-2088690039198397.0 / 1125899906842624.0, sAccurate.getReal(), 1.0e-15);
636     }
637 
638     @Test
639     public void testDotProduct() {
640         // we compare accurate versus naive dot product implementations
641         // on regular vectors (i.e. not extreme cases like in the previous test)
642         Well1024a random = new Well1024a(553267312521321234l);
643         for (int i = 0; i < 10000; ++i) {
644             double ux = 10000 * random.nextDouble();
645             double uy = 10000 * random.nextDouble();
646             double uz = 10000 * random.nextDouble();
647             double vx = 10000 * random.nextDouble();
648             double vy = 10000 * random.nextDouble();
649             double vz = 10000 * random.nextDouble();
650             double sNaive = ux * vx + uy * vy + uz * vz;
651 
652             FieldVector3D<DerivativeStructure> uds = createVector(ux, uy, uz, 3);
653             FieldVector3D<DerivativeStructure> vds = createVector(vx, vy, vz, 3);
654             Vector3D v = new Vector3D(vx, vy, vz);
655 
656             DerivativeStructure sAccurate = FieldVector3D.dotProduct(uds, vds);
657             Assert.assertEquals(sNaive, sAccurate.getReal(), 2.5e-16 * sNaive);
658             Assert.assertEquals(ux + vx, sAccurate.getPartialDerivative(1, 0, 0), 2.5e-16 * sNaive);
659             Assert.assertEquals(uy + vy, sAccurate.getPartialDerivative(0, 1, 0), 2.5e-16 * sNaive);
660             Assert.assertEquals(uz + vz, sAccurate.getPartialDerivative(0, 0, 1), 2.5e-16 * sNaive);
661 
662             sAccurate = FieldVector3D.dotProduct(uds, v);
663             Assert.assertEquals(sNaive, sAccurate.getReal(), 2.5e-16 * sNaive);
664             Assert.assertEquals(vx, sAccurate.getPartialDerivative(1, 0, 0), 2.5e-16 * sNaive);
665             Assert.assertEquals(vy, sAccurate.getPartialDerivative(0, 1, 0), 2.5e-16 * sNaive);
666             Assert.assertEquals(vz, sAccurate.getPartialDerivative(0, 0, 1), 2.5e-16 * sNaive);
667 
668         }
669     }
670 
671     @Test
672     public void testAccurateCrossProduct() {
673         // the vectors u1 and u2 are nearly but not exactly anti-parallel
674         // (7.31e-16 degrees from 180 degrees) naive cross product (i.e.
675         // computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z
676         // leads to a result of   [0.0009765, -0.0001220, -0.0039062],
677         // instead of the correct [0.0006913, -0.0001254, -0.0007909]
678         final FieldVector3D<DerivativeStructure> u1 = createVector(-1321008684645961.0 /   268435456.0,
679                                                                    -5774608829631843.0 /   268435456.0,
680                                                                    -7645843051051357.0 /  8589934592.0, 3);
681         final FieldVector3D<DerivativeStructure> u2 = createVector( 1796571811118507.0 /  2147483648.0,
682                                                                     7853468008299307.0 /  2147483648.0,
683                                                                     2599586637357461.0 / 17179869184.0, 3);
684         final FieldVector3D<DerivativeStructure> u3 = createVector(12753243807587107.0 / 18446744073709551616.0,
685                                                                    -2313766922703915.0 / 18446744073709551616.0,
686                                                                     -227970081415313.0 /   288230376151711744.0, 3);
687         FieldVector3D<DerivativeStructure> cNaive = new FieldVector3D<DerivativeStructure>(u1.getY().multiply(u2.getZ()).subtract(u1.getZ().multiply(u2.getY())),
688                                        u1.getZ().multiply(u2.getX()).subtract(u1.getX().multiply(u2.getZ())),
689                                        u1.getX().multiply(u2.getY()).subtract(u1.getY().multiply(u2.getX())));
690         FieldVector3D<DerivativeStructure> cAccurate = FieldVector3D.crossProduct(u1, u2);
691         Assert.assertTrue(FieldVector3D.distance(u3, cNaive).getReal() > 2.9 * u3.getNorm().getReal());
692         Assert.assertEquals(0.0, FieldVector3D.distance(u3, cAccurate).getReal(), 1.0e-30 * cAccurate.getNorm().getReal());
693     }
694 
695     @Test
696     public void testCrossProduct() {
697         // we compare accurate versus naive cross product implementations
698         // on regular vectors (i.e. not extreme cases like in the previous test)
699         Well1024a random = new Well1024a(885362227452043214l);
700         for (int i = 0; i < 10000; ++i) {
701             double ux = random.nextDouble();
702             double uy = random.nextDouble();
703             double uz = random.nextDouble();
704             double vx = random.nextDouble();
705             double vy = random.nextDouble();
706             double vz = random.nextDouble();
707             Vector3D cNaive = new Vector3D(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx);
708 
709             FieldVector3D<DerivativeStructure> uds = createVector(ux, uy, uz, 3);
710             FieldVector3D<DerivativeStructure> vds = createVector(vx, vy, vz, 3);
711             Vector3D v = new Vector3D(vx, vy, vz);
712 
713             checkVector(FieldVector3D.crossProduct(uds, vds),
714                         cNaive.getX(), cNaive.getY(), cNaive.getZ(),
715                         0, vz - uz, uy - vy,
716                         uz - vz, 0, vx - ux,
717                         vy - uy, ux - vx, 0);
718 
719             checkVector(FieldVector3D.crossProduct(uds, v),
720                         cNaive.getX(), cNaive.getY(), cNaive.getZ(),
721                           0,  vz, -vy,
722                         -vz,   0,  vx,
723                          vy, -vx,   0);
724 
725         }
726     }
727 
728     @Test
729     public void testArithmeticalBlending(){
730         // Given
731         final FieldVector3D<Binary64> vector1 = new FieldVector3D<>(new Binary64(1), new Binary64(2), new Binary64(3));
732         final FieldVector3D<Binary64> vector2 = new FieldVector3D<>(new Binary64(2), new Binary64(4), new Binary64(9));
733 
734         final Binary64 blendingValue = new Binary64(0.65);
735 
736         // When
737         final FieldVector3D<Binary64> blendedVector = vector1.blendArithmeticallyWith(vector2, blendingValue);
738 
739         // Then
740         Assert.assertEquals(1.65, blendedVector.getX().getReal(), 1.0e-15);
741         Assert.assertEquals(3.3 , blendedVector.getY().getReal(), 1.0e-15);
742         Assert.assertEquals(6.9 , blendedVector.getZ().getReal(), 1.0e-15);
743     }
744 
745     private FieldVector3D<DerivativeStructure> createVector(double x, double y, double z, int params) {
746         DSFactory factory = new DSFactory(params, 1);
747         return new FieldVector3D<>(factory.variable(0, x),
748                                    factory.variable(1, y),
749                                    factory.variable(2, z));
750     }
751 
752     private void checkVector(FieldVector3D<DerivativeStructure> v, double x, double y, double z) {
753         Assert.assertEquals(x, v.getX().getReal(), 1.0e-12);
754         Assert.assertEquals(y, v.getY().getReal(), 1.0e-12);
755         Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12);
756     }
757 
758     private void checkVector(FieldVector3D<DerivativeStructure> v, double x, double y, double z,
759                              double dxdx, double dxdy, double dxdz,
760                              double dydx, double dydy, double dydz,
761                              double dzdx, double dzdy, double dzdz) {
762         Assert.assertEquals(x, v.getX().getReal(), 1.0e-12);
763         Assert.assertEquals(y, v.getY().getReal(), 1.0e-12);
764         Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12);
765         Assert.assertEquals(dxdx, v.getX().getPartialDerivative(1, 0, 0), 1.0e-12);
766         Assert.assertEquals(dxdy, v.getX().getPartialDerivative(0, 1, 0), 1.0e-12);
767         Assert.assertEquals(dxdz, v.getX().getPartialDerivative(0, 0, 1), 1.0e-12);
768         Assert.assertEquals(dydx, v.getY().getPartialDerivative(1, 0, 0), 1.0e-12);
769         Assert.assertEquals(dydy, v.getY().getPartialDerivative(0, 1, 0), 1.0e-12);
770         Assert.assertEquals(dydz, v.getY().getPartialDerivative(0, 0, 1), 1.0e-12);
771         Assert.assertEquals(dzdx, v.getZ().getPartialDerivative(1, 0, 0), 1.0e-12);
772         Assert.assertEquals(dzdy, v.getZ().getPartialDerivative(0, 1, 0), 1.0e-12);
773         Assert.assertEquals(dzdz, v.getZ().getPartialDerivative(0, 0, 1), 1.0e-12);
774     }
775 
776     private void checkVector(FieldVector3D<DerivativeStructure> v, double x, double y, double z,
777                              double dxdx, double dxdy, double dxdz, double dxdt,
778                              double dydx, double dydy, double dydz, double dydt,
779                              double dzdx, double dzdy, double dzdz, double dzdt) {
780         Assert.assertEquals(x, v.getX().getReal(), 1.0e-12);
781         Assert.assertEquals(y, v.getY().getReal(), 1.0e-12);
782         Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12);
783         Assert.assertEquals(dxdx, v.getX().getPartialDerivative(1, 0, 0, 0), 1.0e-12);
784         Assert.assertEquals(dxdy, v.getX().getPartialDerivative(0, 1, 0, 0), 1.0e-12);
785         Assert.assertEquals(dxdz, v.getX().getPartialDerivative(0, 0, 1, 0), 1.0e-12);
786         Assert.assertEquals(dxdt, v.getX().getPartialDerivative(0, 0, 0, 1), 1.0e-12);
787         Assert.assertEquals(dydx, v.getY().getPartialDerivative(1, 0, 0, 0), 1.0e-12);
788         Assert.assertEquals(dydy, v.getY().getPartialDerivative(0, 1, 0, 0), 1.0e-12);
789         Assert.assertEquals(dydz, v.getY().getPartialDerivative(0, 0, 1, 0), 1.0e-12);
790         Assert.assertEquals(dydt, v.getY().getPartialDerivative(0, 0, 0, 1), 1.0e-12);
791         Assert.assertEquals(dzdx, v.getZ().getPartialDerivative(1, 0, 0, 0), 1.0e-12);
792         Assert.assertEquals(dzdy, v.getZ().getPartialDerivative(0, 1, 0, 0), 1.0e-12);
793         Assert.assertEquals(dzdz, v.getZ().getPartialDerivative(0, 0, 1, 0), 1.0e-12);
794         Assert.assertEquals(dzdt, v.getZ().getPartialDerivative(0, 0, 0, 1), 1.0e-12);
795     }
796 
797 }