View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.analysis.differentiation;
19  
20  import java.lang.reflect.Array;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.hipparchus.CalculusFieldElement;
28  import org.hipparchus.CalculusFieldElementAbstractTest;
29  import org.hipparchus.Field;
30  import org.hipparchus.analysis.CalculusFieldMultivariateFunction;
31  import org.hipparchus.analysis.CalculusFieldMultivariateVectorFunction;
32  import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
33  import org.hipparchus.analysis.polynomials.PolynomialFunction;
34  import org.hipparchus.dfp.Dfp;
35  import org.hipparchus.dfp.DfpField;
36  import org.hipparchus.exception.LocalizedCoreFormats;
37  import org.hipparchus.exception.MathIllegalArgumentException;
38  import org.hipparchus.random.RandomGenerator;
39  import org.hipparchus.random.Well1024a;
40  import org.hipparchus.random.Well19937a;
41  import org.hipparchus.util.ArithmeticUtils;
42  import org.hipparchus.util.CombinatoricsUtils;
43  import org.hipparchus.util.FastMath;
44  import org.hipparchus.util.FieldSinCos;
45  import org.hipparchus.util.FieldSinhCosh;
46  import org.hipparchus.util.MathArrays;
47  import org.hipparchus.util.Precision;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  
51  import static org.junit.jupiter.api.Assertions.assertEquals;
52  import static org.junit.jupiter.api.Assertions.assertNotEquals;
53  import static org.junit.jupiter.api.Assertions.assertSame;
54  import static org.junit.jupiter.api.Assertions.assertThrows;
55  import static org.junit.jupiter.api.Assertions.assertTrue;
56  import static org.junit.jupiter.api.Assertions.fail;
57  
58  /**
59   * Abstract test for class {@link FieldDerivativeStructure}.
60   */
61  public abstract class FieldDerivativeStructureAbstractTest<T extends CalculusFieldElement<T>>
62      extends CalculusFieldElementAbstractTest<FieldDerivativeStructure<T>> {
63  
64      protected abstract Field<T> getField();
65  
66      protected T buildScalar(double value) {
67          return getField().getZero().newInstance(value);
68      }
69  
70      protected FDSFactory<T> buildFactory(int parameters, int order) {
71          return new FDSFactory<>(getField(), parameters, order);
72      }
73  
74      @Override
75      protected FieldDerivativeStructure<T> build(final double x) {
76          return buildFactory(2, 1).variable(0, x);
77      }
78  
79      @Test
80      public void testWrongFieldVariableIndex() {
81          assertThrows(MathIllegalArgumentException.class, () -> buildFactory(3, 1).variable(3, buildScalar(1.0)));
82      }
83  
84      @Test
85      public void testWrongPrimitiveVariableIndex() {
86          assertThrows(MathIllegalArgumentException.class, () -> {
87              final FDSFactory<T> factory = buildFactory(3, 1);
88              factory.variable(3, 1.0);
89          });
90      }
91  
92      @Test
93      public void testMissingOrders() {
94          assertThrows(MathIllegalArgumentException.class, () -> {
95              final FDSFactory<T> factory = buildFactory(3, 1);
96              factory.variable(0, 1.0).getPartialDerivative(0, 1);
97          });
98      }
99  
100     @Test
101     public void testWrongDimensionField() {
102         assertThrows(MathIllegalArgumentException.class, () -> {
103             final FDSFactory<T> factory = buildFactory(3, 1);
104             factory.build(buildScalar(1.0), buildScalar(1.0), buildScalar(1.0), buildScalar(1.0), buildScalar(1.0));
105         });
106     }
107 
108     @Test
109     public void testWrongDimensionPrimitive() {
110         assertThrows(MathIllegalArgumentException.class, () -> {
111             final FDSFactory<T> factory = buildFactory(3, 1);
112             factory.build(1.0, 1.0, 1.0, 1.0, 1.0);
113         });
114     }
115 
116     @Test
117     public void testTooLargeOrder() {
118         assertThrows(MathIllegalArgumentException.class, () -> {
119             final FDSFactory<T> factory = buildFactory(3, 1);
120             factory.variable(0, 1.0).getPartialDerivative(1, 1, 2);
121         });
122     }
123 
124     @Test
125     public void testVariableWithoutDerivativeField() {
126         final FDSFactory<T> factory = buildFactory(1, 0);
127         FieldDerivativeStructure<T> v = factory.variable(0, buildScalar(1.0));
128         assertEquals(1.0, v.getReal(), 1.0e-15);
129     }
130 
131     @Test
132     public void testVariableWithoutDerivativePrimitive() {
133         final FDSFactory<T> factory = buildFactory(1, 0);
134         FieldDerivativeStructure<T> v = factory.variable(0, 1.0);
135         assertEquals(1.0, v.getReal(), 1.0e-15);
136     }
137 
138     @Test
139     public void testVariableWithoutDerivative1() {
140         assertThrows(MathIllegalArgumentException.class, () -> {
141             final FDSFactory<T> factory = buildFactory(1, 0);
142             FieldDerivativeStructure<T> v = factory.variable(0, 1.0);
143             assertEquals(1.0, v.getPartialDerivative(1).getReal(), 1.0e-15);
144         });
145     }
146 
147     @Test
148     public void testVariable() {
149         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
150             final FDSFactory<T> factory = buildFactory(3, maxOrder);
151             checkF0F1(factory.variable(0, 1.0),
152                       1.0, 1.0, 0.0, 0.0);
153             checkF0F1(factory.variable(1, 2.0),
154                       2.0, 0.0, 1.0, 0.0);
155             checkF0F1(factory.variable(2, 3.0),
156                       3.0, 0.0, 0.0, 1.0);
157         }
158     }
159 
160     @Test
161     public void testConstant() {
162         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
163             final FDSFactory<T> factory = buildFactory(3, maxOrder);
164             checkF0F1(factory.constant(FastMath.PI),
165                       FastMath.PI, 0.0, 0.0, 0.0);
166         }
167     }
168 
169     @Test
170     public void testFieldAdd() {
171         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
172             final FDSFactory<T> factory = buildFactory(3, maxOrder);
173             checkF0F1(factory.variable(0, 1.0).add(buildScalar(5)), 6.0, 1.0, 0.0, 0.0);
174             checkF0F1(factory.variable(1, 2.0).add(buildScalar(5)), 7.0, 0.0, 1.0, 0.0);
175             checkF0F1(factory.variable(2, 3.0).add(buildScalar(5)), 8.0, 0.0, 0.0, 1.0);
176         }
177     }
178 
179     @Test
180     public void testPrimitiveAdd() {
181         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
182             final FDSFactory<T> factory = buildFactory(3, maxOrder);
183             checkF0F1(factory.variable(0, 1.0).add(5), 6.0, 1.0, 0.0, 0.0);
184             checkF0F1(factory.variable(1, 2.0).add(5), 7.0, 0.0, 1.0, 0.0);
185             checkF0F1(factory.variable(2, 3.0).add(5), 8.0, 0.0, 0.0, 1.0);
186         }
187     }
188 
189     @Test
190     public void testAdd() {
191         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
192             final FDSFactory<T> factory = buildFactory(3, maxOrder);
193             FieldDerivativeStructure<T> x = factory.variable(0, 1.0);
194             FieldDerivativeStructure<T> y = factory.variable(1, 2.0);
195             FieldDerivativeStructure<T> z = factory.variable(2, 3.0);
196             FieldDerivativeStructure<T> xyz = x.add(y.add(z));
197             checkF0F1(xyz, x.getValue().getReal() + y.getValue().getReal() + z.getValue().getReal(), 1.0, 1.0, 1.0);
198         }
199     }
200 
201     @Test
202     public void testFieldSubtract() {
203         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
204             final FDSFactory<T> factory = buildFactory(3, maxOrder);
205             checkF0F1(factory.variable(0, 1.0).subtract(buildScalar(5)), -4.0, 1.0, 0.0, 0.0);
206             checkF0F1(factory.variable(1, 2.0).subtract(buildScalar(5)), -3.0, 0.0, 1.0, 0.0);
207             checkF0F1(factory.variable(2, 3.0).subtract(buildScalar(5)), -2.0, 0.0, 0.0, 1.0);
208         }
209     }
210 
211     @Test
212     public void testPrimitiveSubtract() {
213         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
214             final FDSFactory<T> factory = buildFactory(3, maxOrder);
215             checkF0F1(factory.variable(0, 1.0).subtract(5), -4.0, 1.0, 0.0, 0.0);
216             checkF0F1(factory.variable(1, 2.0).subtract(5), -3.0, 0.0, 1.0, 0.0);
217             checkF0F1(factory.variable(2, 3.0).subtract(5), -2.0, 0.0, 0.0, 1.0);
218         }
219     }
220 
221     @Test
222     public void testSubtract() {
223         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
224             final FDSFactory<T> factory = buildFactory(3, maxOrder);
225             FieldDerivativeStructure<T> x = factory.variable(0, 1.0);
226             FieldDerivativeStructure<T> y = factory.variable(1, 2.0);
227             FieldDerivativeStructure<T> z = factory.variable(2, 3.0);
228             FieldDerivativeStructure<T> xyz = x.subtract(y.subtract(z));
229             checkF0F1(xyz, x.getReal() - (y.getReal() - z.getReal()), 1.0, -1.0, 1.0);
230         }
231     }
232 
233     @Test
234     public void testFieldMultiply() {
235         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
236             final FDSFactory<T> factory = buildFactory(3, maxOrder);
237             checkF0F1(factory.variable(0, 1.0).multiply(buildScalar(5)),  5.0, 5.0, 0.0, 0.0);
238             checkF0F1(factory.variable(1, 2.0).multiply(buildScalar(5)), 10.0, 0.0, 5.0, 0.0);
239             checkF0F1(factory.variable(2, 3.0).multiply(buildScalar(5)), 15.0, 0.0, 0.0, 5.0);
240         }
241     }
242 
243     @Test
244     public void testPrimitiveMultiply() {
245         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
246             final FDSFactory<T> factory = buildFactory(3, maxOrder);
247             checkF0F1(factory.variable(0, 1.0).multiply(5),  5.0, 5.0, 0.0, 0.0);
248             checkF0F1(factory.variable(1, 2.0).multiply(5), 10.0, 0.0, 5.0, 0.0);
249             checkF0F1(factory.variable(2, 3.0).multiply(5), 15.0, 0.0, 0.0, 5.0);
250         }
251     }
252 
253     @Test
254     public void testMultiply() {
255         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
256             final FDSFactory<T> factory = buildFactory(3, maxOrder);
257             FieldDerivativeStructure<T> x = factory.variable(0, 1.0);
258             FieldDerivativeStructure<T> y = factory.variable(1, 2.0);
259             FieldDerivativeStructure<T> z = factory.variable(2, 3.0);
260             FieldDerivativeStructure<T> xyz = x.multiply(y.multiply(z));
261             for (int i = 0; i <= maxOrder; ++i) {
262                 for (int j = 0; j <= maxOrder; ++j) {
263                     for (int k = 0; k <= maxOrder; ++k) {
264                         if (i + j + k <= maxOrder) {
265                             assertEquals((i == 0 ? x.getReal() : (i == 1 ? 1.0 : 0.0)) *
266                                                 (j == 0 ? y.getReal() : (j == 1 ? 1.0 : 0.0)) *
267                                                 (k == 0 ? z.getReal() : (k == 1 ? 1.0 : 0.0)),
268                                                 xyz.getPartialDerivative(i, j, k).getReal(),
269                                                 1.0e-15);
270                         }
271                     }
272                 }
273             }
274         }
275     }
276 
277     @Test
278     public void testFieldDivide() {
279         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
280             final FDSFactory<T> factory = buildFactory(3, maxOrder);
281             checkF0F1(factory.variable(0, 1.0).divide(buildScalar(2)),  0.5, 0.5, 0.0, 0.0);
282             checkF0F1(factory.variable(1, 2.0).divide(buildScalar(2)),  1.0, 0.0, 0.5, 0.0);
283             checkF0F1(factory.variable(2, 3.0).divide(buildScalar(2)),  1.5, 0.0, 0.0, 0.5);
284         }
285     }
286 
287     @Test
288     public void testPrimitiveDivide() {
289         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
290             final FDSFactory<T> factory = buildFactory(3, maxOrder);
291             checkF0F1(factory.variable(0, 1.0).half(),  0.5, 0.5, 0.0, 0.0);
292             checkF0F1(factory.variable(1, 2.0).half(),  1.0, 0.0, 0.5, 0.0);
293             checkF0F1(factory.variable(2, 3.0).half(),  1.5, 0.0, 0.0, 0.5);
294         }
295     }
296 
297     @Test
298     public void testNegate() {
299         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
300             final FDSFactory<T> factory = buildFactory(3, maxOrder);
301             checkF0F1(factory.variable(0, 1.0).negate(), -1.0, -1.0, 0.0, 0.0);
302             checkF0F1(factory.variable(1, 2.0).negate(), -2.0, 0.0, -1.0, 0.0);
303             checkF0F1(factory.variable(2, 3.0).negate(), -3.0, 0.0, 0.0, -1.0);
304         }
305     }
306 
307     @Test
308     public void testReciprocal() {
309         final FDSFactory<T> factory = buildFactory(1, 6);
310         for (double x = 0.1; x < 1.2; x += 0.1) {
311             FieldDerivativeStructure<T> r = factory.variable(0, x).reciprocal();
312             assertEquals(1 / x, r.getReal(), 1.0e-15);
313             for (int i = 1; i < r.getOrder(); ++i) {
314                 double expected = ArithmeticUtils.pow(-1, i) * CombinatoricsUtils.factorial(i) /
315                                   FastMath.pow(x, i + 1);
316                 assertEquals(expected, r.getPartialDerivative(i).getReal(), 1.0e-15 * FastMath.abs(expected));
317             }
318         }
319     }
320 
321     @Test
322     public void testPow() {
323         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
324             final FDSFactory<T> factory = buildFactory(3, maxOrder);
325             for (int n = 0; n < 10; ++n) {
326 
327                 FieldDerivativeStructure<T> x = factory.variable(0, 1.0);
328                 FieldDerivativeStructure<T> y = factory.variable(1, 2.0);
329                 FieldDerivativeStructure<T> z = factory.variable(2, 3.0);
330                 List<FieldDerivativeStructure<T>> list = Arrays.asList(x, y, z,
331                                                                                x.add(y).add(z),
332                                                                                x.multiply(y).multiply(z));
333 
334                 if (n == 0) {
335                     for (FieldDerivativeStructure<T> ds : list) {
336                         checkEquals(ds.getField().getOne(), ds.pow(n), 1.0e-15);
337                     }
338                 } else if (n == 1) {
339                     for (FieldDerivativeStructure<T> ds : list) {
340                         checkEquals(ds, ds.pow(n), 1.0e-15);
341                     }
342                 } else {
343                     for (FieldDerivativeStructure<T> ds : list) {
344                         FieldDerivativeStructure<T> p = ds.getField().getOne();
345                         for (int i = 0; i < n; ++i) {
346                             p = p.multiply(ds);
347                         }
348                         checkEquals(p, ds.pow(n), 1.0e-15);
349                     }
350                 }
351             }
352         }
353     }
354 
355     @Test
356     public void testPowDoubleDS() {
357         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
358 
359             final FDSFactory<T> factory = buildFactory(3, maxOrder);
360             FieldDerivativeStructure<T> x = factory.variable(0, 0.1);
361             FieldDerivativeStructure<T> y = factory.variable(1, 0.2);
362             FieldDerivativeStructure<T> z = factory.variable(2, 0.3);
363             List<FieldDerivativeStructure<T>> list = Arrays.asList(x, y, z,
364                                                                            x.add(y).add(z),
365                                                                            x.multiply(y).multiply(z));
366 
367             for (FieldDerivativeStructure<T> ds : list) {
368                 // the special case a = 0 is included here
369                 for (double a : new double[] { 0.0, 0.1, 1.0, 2.0, 5.0 }) {
370                     FieldDerivativeStructure<T> reference = (a == 0) ?
371                                                     x.getField().getZero() :
372                                                     factory.constant(a).pow(ds);
373                     FieldDerivativeStructure<T> result = FieldDerivativeStructure.pow(a, ds);
374                     checkEquals(reference, result, 2.0e-14 * FastMath.abs(reference.getReal()));
375                 }
376 
377             }
378 
379             // negative base: -1^x can be evaluated for integers only, so value is sometimes OK, derivatives are always NaN
380             FieldDerivativeStructure<T> negEvenInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 2.0));
381             assertEquals(4.0, negEvenInteger.getReal(), 1.0e-15);
382             assertTrue(Double.isNaN(negEvenInteger.getPartialDerivative(1, 0, 0).getReal()));
383             FieldDerivativeStructure<T> negOddInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 3.0));
384             assertEquals(-8.0, negOddInteger.getReal(), 1.0e-15);
385             assertTrue(Double.isNaN(negOddInteger.getPartialDerivative(1, 0, 0).getReal()));
386             FieldDerivativeStructure<T> negNonInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 2.001));
387             assertTrue(Double.isNaN(negNonInteger.getReal()));
388             assertTrue(Double.isNaN(negNonInteger.getPartialDerivative(1, 0, 0).getReal()));
389 
390             FieldDerivativeStructure<T> zeroNeg = FieldDerivativeStructure.pow(0.0, factory.variable(0, -1.0));
391             assertTrue(Double.isNaN(zeroNeg.getReal()));
392             assertTrue(Double.isNaN(zeroNeg.getPartialDerivative(1, 0, 0).getReal()));
393             FieldDerivativeStructure<T> posNeg = FieldDerivativeStructure.pow(2.0, factory.variable(0, -2.0));
394             assertEquals(1.0 / 4.0, posNeg.getReal(), 1.0e-15);
395             assertEquals(FastMath.log(2.0) / 4.0, posNeg.getPartialDerivative(1, 0, 0).getReal(), 1.0e-15);
396 
397             // very special case: a = 0 and power = 0
398             FieldDerivativeStructure<T> zeroZero = FieldDerivativeStructure.pow(0.0, factory.variable(0, 0.0));
399 
400             // this should be OK for simple first derivative with one variable only ...
401             assertEquals(1.0, zeroZero.getReal(), 1.0e-15);
402             assertEquals(Double.NEGATIVE_INFINITY, zeroZero.getPartialDerivative(1, 0, 0).getReal(), 1.0e-15);
403 
404             // the following checks show a LIMITATION of the current implementation
405             // we have no way to tell x is a pure linear variable x = 0
406             // we only say: "x is a structure with value = 0.0,
407             // first derivative with respect to x = 1.0, and all other derivatives
408             // (first order with respect to y and z and higher derivatives) all 0.0.
409             // We have function f(x) = a^x and x = 0 so we compute:
410             // f(0) = 1, f'(0) = ln(a), f''(0) = ln(a)^2. The limit of these values
411             // when a converges to 0 implies all derivatives keep switching between
412             // +infinity and -infinity.
413             //
414             // Function composition rule for first derivatives is:
415             // d[f(g(x,y,z))]/dy = f'(g(x,y,z)) * dg(x,y,z)/dy
416             // so given that in our case x represents g and does not depend
417             // on y or z, we have dg(x,y,z)/dy = 0
418             // applying the composition rules gives:
419             // d[f(g(x,y,z))]/dy = f'(g(x,y,z)) * dg(x,y,z)/dy
420             //                 = -infinity * 0
421             //                 = NaN
422             // if we knew x is really the x variable and not the identity
423             // function applied to x, we would not have computed f'(g(x,y,z)) * dg(x,y,z)/dy
424             // and we would have found that the result was 0 and not NaN
425             assertTrue(Double.isNaN(zeroZero.getPartialDerivative(0, 1, 0).getReal()));
426             assertTrue(Double.isNaN(zeroZero.getPartialDerivative(0, 0, 1).getReal()));
427 
428             // Function composition rule for second derivatives is:
429             // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x)
430             // when function f is the a^x root and x = 0 we have:
431             // f(0) = 1, f'(0) = ln(a), f''(0) = ln(a)^2 which for a = 0 implies
432             // all derivatives keep switching between +infinity and -infinity
433             // so given that in our case x represents g, we have g(x) = 0,
434             // g'(x) = 1 and g''(x) = 0
435             // applying the composition rules gives:
436             // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x)
437             //                 = +infinity * 1^2 + -infinity * 0
438             //                 = +infinity + NaN
439             //                 = NaN
440             // if we knew x is really the x variable and not the identity
441             // function applied to x, we would not have computed f'(g(x)) * g''(x)
442             // and we would have found that the result was +infinity and not NaN
443             if (maxOrder > 1) {
444                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(2, 0, 0).getReal()));
445                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(0, 2, 0).getReal()));
446                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(0, 0, 2).getReal()));
447                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(1, 1, 0).getReal()));
448                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(0, 1, 1).getReal()));
449                 assertTrue(Double.isNaN(zeroZero.getPartialDerivative(1, 1, 0).getReal()));
450             }
451 
452             // very special case: 0^0 where the power is a primitive
453             FieldDerivativeStructure<T> zeroDsZeroDouble = factory.variable(0, 0.0).pow(0.0);
454             boolean first = true;
455             for (final T d : zeroDsZeroDouble.getAllDerivatives()) {
456                 if (first) {
457                     assertEquals(1.0, d.getReal(), Precision.EPSILON);
458                     first = false;
459                 } else {
460                     assertEquals(0.0, d.getReal(), Precision.SAFE_MIN);
461                 }
462             }
463             FieldDerivativeStructure<T> zeroDsZeroInt = factory.variable(0, 0.0).pow(0);
464             first = true;
465             for (final T d : zeroDsZeroInt.getAllDerivatives()) {
466                 if (first) {
467                     assertEquals(1.0, d.getReal(), Precision.EPSILON);
468                     first = false;
469                 } else {
470                     assertEquals(0.0, d.getReal(), Precision.SAFE_MIN);
471                 }
472             }
473 
474             // 0^p with p smaller than 1.0
475             FieldDerivativeStructure<T> u = factory.variable(1, -0.0).pow(0.25);
476             for (int i0 = 0; i0 <= maxOrder; ++i0) {
477                 for (int i1 = 0; i1 <= maxOrder; ++i1) {
478                     for (int i2 = 0; i2 <= maxOrder; ++i2) {
479                         if (i0 + i1 + i2 <= maxOrder) {
480                             assertEquals(0.0, u.getPartialDerivative(i0, i1, i2).getReal(), 1.0e-10);
481                         }
482                     }
483                 }
484             }
485         }
486 
487     }
488 
489     @Test
490     public void testExpression() {
491         final FDSFactory<T> factory = buildFactory(3, 5);
492         double epsilon = 2.5e-13;
493         for (double x = 0; x < 2; x += 0.2) {
494             FieldDerivativeStructure<T> dsX = factory.variable(0, x);
495             for (double y = 0; y < 2; y += 0.2) {
496                 FieldDerivativeStructure<T> dsY = factory.variable(1, y);
497                 for (double z = 0; z >- 2; z -= 0.2) {
498                     FieldDerivativeStructure<T> dsZ = factory.variable(2, z);
499 
500                     // f(x, y, z) = x + 5 x y - 2 z + (8 z x - y)^3
501                     FieldDerivativeStructure<T> ds =
502                             dsX.linearCombination(1, dsX,
503                                                     5, dsX.multiply(dsY),
504                                                     -2, dsZ,
505                                                     1, dsX.linearCombination(8, dsZ.multiply(dsX),
506                                                                                -1, dsY).pow(3));
507                     FieldDerivativeStructure<T> dsOther =
508                                     dsX.linearCombination(1, dsX,
509                                                     5, dsX.multiply(dsY),
510                                                     -2, dsZ).add(dsX.linearCombination   (8, dsZ.multiply(dsX),
511                                                                                          -1, dsY).pow(3));
512                     double f = x + 5 * x * y - 2 * z + FastMath.pow(8 * z * x - y, 3);
513                     assertEquals(f, ds.getReal(),
514                                         FastMath.abs(epsilon * f));
515                     assertEquals(f, dsOther.getReal(),
516                                         FastMath.abs(epsilon * f));
517 
518                     // df/dx = 1 + 5 y + 24 (8 z x - y)^2 z
519                     double dfdx = 1 + 5 * y + 24 * z * FastMath.pow(8 * z * x - y, 2);
520                     assertEquals(dfdx, ds.getPartialDerivative(1, 0, 0).getReal(),
521                                         FastMath.abs(epsilon * dfdx));
522                     assertEquals(dfdx, dsOther.getPartialDerivative(1, 0, 0).getReal(),
523                                         FastMath.abs(epsilon * dfdx));
524 
525                     // df/dxdy = 5 + 48 z*(y - 8 z x)
526                     double dfdxdy = 5 + 48 * z * (y - 8 * z * x);
527                     assertEquals(dfdxdy, ds.getPartialDerivative(1, 1, 0).getReal(),
528                                         FastMath.abs(epsilon * dfdxdy));
529                     assertEquals(dfdxdy, dsOther.getPartialDerivative(1, 1, 0).getReal(),
530                                         FastMath.abs(epsilon * dfdxdy));
531 
532                     // df/dxdydz = 48 (y - 16 z x)
533                     double dfdxdydz = 48 * (y - 16 * z * x);
534                     assertEquals(dfdxdydz, ds.getPartialDerivative(1, 1, 1).getReal(),
535                                         FastMath.abs(epsilon * dfdxdydz));
536                     assertEquals(dfdxdydz, dsOther.getPartialDerivative(1, 1, 1).getReal(),
537                                         FastMath.abs(epsilon * dfdxdydz));
538 
539                 }
540 
541             }
542         }
543     }
544 
545     @Test
546     public void testCompositionOneVariableX() {
547         double epsilon = 1.0e-13;
548         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
549             final FDSFactory<T> factory = buildFactory(1, maxOrder);
550             for (double x = 0.1; x < 1.2; x += 0.1) {
551                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
552                 for (double y = 0.1; y < 1.2; y += 0.1) {
553                     FieldDerivativeStructure<T> dsY = factory.constant(y);
554                     FieldDerivativeStructure<T> f = dsX.divide(dsY).sqrt();
555                     double f0 = FastMath.sqrt(x / y);
556                     assertEquals(f0, f.getReal(), FastMath.abs(epsilon * f0));
557                     if (f.getOrder() > 0) {
558                         double f1 = 1 / (2 * FastMath.sqrt(x * y));
559                         assertEquals(f1, f.getPartialDerivative(1).getReal(), FastMath.abs(epsilon * f1));
560                         if (f.getOrder() > 1) {
561                             double f2 = -f1 / (2 * x);
562                             assertEquals(f2, f.getPartialDerivative(2).getReal(), FastMath.abs(epsilon * f2));
563                             if (f.getOrder() > 2) {
564                                 double f3 = (f0 + x / (2 * y * f0)) / (4 * x * x * x);
565                                 assertEquals(f3, f.getPartialDerivative(3).getReal(), FastMath.abs(epsilon * f3));
566                             }
567                         }
568                     }
569                 }
570             }
571         }
572     }
573 
574     @Test
575     public void testTrigo() {
576         double epsilon = 2.0e-12;
577         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
578             final FDSFactory<T> factory = buildFactory(3, maxOrder);
579             for (double x = 0.1; x < 1.2; x += 0.1) {
580                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
581                 for (double y = 0.1; y < 1.2; y += 0.1) {
582                     FieldDerivativeStructure<T> dsY = factory.variable(1, y);
583                     for (double z = 0.1; z < 1.2; z += 0.1) {
584                         FieldDerivativeStructure<T> dsZ = factory.variable(2, z);
585                         FieldDerivativeStructure<T> f = dsX.divide(dsY.cos().add(dsZ.tan())).sin();
586                         double a = FastMath.cos(y) + FastMath.tan(z);
587                         double f0 = FastMath.sin(x / a);
588                         assertEquals(f0, f.getReal(), FastMath.abs(epsilon * f0));
589                         if (f.getOrder() > 0) {
590                             double dfdx = FastMath.cos(x / a) / a;
591                             assertEquals(dfdx, f.getPartialDerivative(1, 0, 0).getReal(), FastMath.abs(epsilon * dfdx));
592                             double dfdy =  x * FastMath.sin(y) * dfdx / a;
593                             assertEquals(dfdy, f.getPartialDerivative(0, 1, 0).getReal(), FastMath.abs(epsilon * dfdy));
594                             double cz = FastMath.cos(z);
595                             double cz2 = cz * cz;
596                             double dfdz = -x * dfdx / (a * cz2);
597                             assertEquals(dfdz, f.getPartialDerivative(0, 0, 1).getReal(), FastMath.abs(epsilon * dfdz));
598                             if (f.getOrder() > 1) {
599                                 double df2dx2 = -(f0 / (a * a));
600                                 assertEquals(df2dx2, f.getPartialDerivative(2, 0, 0).getReal(), FastMath.abs(epsilon * df2dx2));
601                                 double df2dy2 = x * FastMath.cos(y) * dfdx / a -
602                                                 x * x * FastMath.sin(y) * FastMath.sin(y) * f0 / (a * a * a * a) +
603                                                 2 * FastMath.sin(y) * dfdy / a;
604                                 assertEquals(df2dy2, f.getPartialDerivative(0, 2, 0).getReal(), FastMath.abs(epsilon * df2dy2));
605                                 double c4 = cz2 * cz2;
606                                 double df2dz2 = x * (2 * a * (1 - a * cz * FastMath.sin(z)) * dfdx - x * f0 / a ) / (a * a * a * c4);
607                                 assertEquals(df2dz2, f.getPartialDerivative(0, 0, 2).getReal(), FastMath.abs(epsilon * df2dz2));
608                                 double df2dxdy = dfdy / x  - x * FastMath.sin(y) * f0 / (a * a * a);
609                                 assertEquals(df2dxdy, f.getPartialDerivative(1, 1, 0).getReal(), FastMath.abs(epsilon * df2dxdy));
610                             }
611                         }
612                     }
613                 }
614             }
615         }
616     }
617 
618     @Test
619     public void testSqrtDefinition() {
620         double[] epsilon = new double[] { 5.0e-16, 5.0e-16, 2.7e-15, 5.7e-14, 2.0e-12 };
621         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
622             final FDSFactory<T> factory = buildFactory(1, maxOrder);
623             for (double x = 0.1; x < 1.2; x += 0.001) {
624                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
625                 FieldDerivativeStructure<T> sqrt1 = dsX.pow(0.5);
626                 FieldDerivativeStructure<T> sqrt2 = dsX.sqrt();
627                 FieldDerivativeStructure<T> zero = sqrt1.subtract(sqrt2);
628                 for (int n = 0; n <= maxOrder; ++n) {
629                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
630                 }
631             }
632         }
633     }
634 
635     @Test
636     public void testRootNSingularity() {
637         doTestRootNSingularity(true);
638     }
639 
640     protected void doTestRootNSingularity(final boolean signedInfinities) {
641         for (int n = 2; n < 10; ++n) {
642             for (int maxOrder = 0; maxOrder < 12; ++maxOrder) {
643                 final FDSFactory<T> factory = buildFactory(1, maxOrder);
644                 FieldDerivativeStructure<T> dsZero = factory.variable(0, 0.0);
645                 FieldDerivativeStructure<T> rootN  = dsZero.rootN(n);
646                 assertEquals(0.0, rootN.getReal(), 1.0e-20);
647                 if (maxOrder > 0) {
648                     assertTrue(Double.isInfinite(rootN.getPartialDerivative(1).getReal()));
649                     assertTrue(rootN.getPartialDerivative(1).getReal() > 0);
650                     for (int order = 2; order <= maxOrder; ++order) {
651                         // the following checks shows a LIMITATION of the current implementation
652                         // we have no way to tell dsZero is a pure linear variable x = 0
653                         // we only say: "dsZero is a structure with value = 0.0,
654                         // first derivative = 1.0, second and higher derivatives = 0.0".
655                         // Function composition rule for second derivatives is:
656                         // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x)
657                         // when function f is the nth root and x = 0 we have:
658                         // f(0) = 0, f'(0) = +infinity, f''(0) = -infinity (and higher
659                         // derivatives keep switching between +infinity and -infinity)
660                         // so given that in our case dsZero represents g, we have g(x) = 0,
661                         // g'(x) = 1 and g''(x) = 0
662                         // applying the composition rules gives:
663                         // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x)
664                         //                 = -infinity * 1^2 + +infinity * 0
665                         //                 = -infinity + NaN
666                         //                 = NaN
667                         // if we knew dsZero is really the x variable and not the identity
668                         // function applied to x, we would not have computed f'(g(x)) * g''(x)
669                         // and we would have found that the result was -infinity and not NaN
670                         final double d = rootN.getPartialDerivative(order).getReal();
671                         assertTrue(Double.isNaN(d) || Double.isInfinite(d));
672                     }
673                 }
674 
675                 // the following shows that the limitation explained above is NOT a bug...
676                 // if we set up the higher order derivatives for g appropriately, we do
677                 // compute the higher order derivatives of the composition correctly
678                 double[] gDerivatives = new double[ 1 + maxOrder];
679                 gDerivatives[0] = 0.0;
680                 for (int k = 1; k <= maxOrder; ++k) {
681                     gDerivatives[k] = FastMath.pow(-1.0, k + 1);
682                 }
683                 FieldDerivativeStructure<T> correctRoot = factory.build(gDerivatives).rootN(n);
684                 assertEquals(0.0, correctRoot.getReal(), 1.0e-20);
685                 if (maxOrder > 0) {
686                     assertTrue(Double.isInfinite(correctRoot.getPartialDerivative(1).getReal()));
687                     assertTrue(correctRoot.getPartialDerivative(1).getReal() > 0);
688                     for (int order = 2; order <= maxOrder; ++order) {
689                         assertTrue(Double.isInfinite(correctRoot.getPartialDerivative(order).getReal()));
690                         if (signedInfinities) {
691                             if ((order % 2) == 0) {
692                                 assertTrue(correctRoot.getPartialDerivative(order).getReal() < 0);
693                             } else {
694                                 assertTrue(correctRoot.getPartialDerivative(order).getReal() > 0);
695                             }
696                         }
697                     }
698                 }
699 
700             }
701 
702         }
703 
704     }
705 
706     @Test
707     public void testSqrtPow2() {
708         double[] epsilon = new double[] { 1.0e-16, 3.0e-16, 2.0e-15, 6.0e-14, 6.0e-12 };
709         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
710             final FDSFactory<T> factory = buildFactory(1, maxOrder);
711             for (double x = 0.1; x < 1.2; x += 0.001) {
712                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
713                 FieldDerivativeStructure<T> rebuiltX = dsX.multiply(dsX).sqrt();
714                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
715                 for (int n = 0; n <= maxOrder; ++n) {
716                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
717                 }
718             }
719         }
720     }
721 
722     @Test
723     public void testCbrtDefinition() {
724         double[] epsilon = new double[] { 4.0e-16, 9.0e-16, 6.0e-15, 2.0e-13, 4.0e-12 };
725         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
726             final FDSFactory<T> factory = buildFactory(1, maxOrder);
727             for (double x = 0.1; x < 1.2; x += 0.001) {
728                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
729                 FieldDerivativeStructure<T> cbrt1 = dsX.pow(1.0 / 3.0);
730                 FieldDerivativeStructure<T> cbrt2 = dsX.cbrt();
731                 FieldDerivativeStructure<T> zero = cbrt1.subtract(cbrt2);
732                 for (int n = 0; n <= maxOrder; ++n) {
733                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
734                 }
735             }
736         }
737     }
738 
739     @Test
740     public void testCbrtPow3() {
741         double[] epsilon = new double[] { 1.0e-16, 5.0e-16, 8.0e-15, 4.0e-13, 3.0e-11 };
742         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
743             final FDSFactory<T> factory = buildFactory(1, maxOrder);
744             for (double x = 0.1; x < 1.2; x += 0.001) {
745                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
746                 FieldDerivativeStructure<T> rebuiltX = dsX.multiply(dsX.multiply(dsX)).cbrt();
747                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
748                 for (int n = 0; n <= maxOrder; ++n) {
749                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
750                 }
751             }
752         }
753     }
754 
755     @Test
756     public void testPowReciprocalPow() {
757         double[] epsilon = new double[] { 2.0e-15, 2.0e-14, 3.0e-13, 8.0e-12, 3.0e-10 };
758         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
759             final FDSFactory<T> factory = buildFactory(2, maxOrder);
760             for (double x = 0.1; x < 1.2; x += 0.01) {
761                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
762                 for (double y = 0.1; y < 1.2; y += 0.01) {
763                     FieldDerivativeStructure<T> dsY = factory.variable(1, y);
764                     FieldDerivativeStructure<T> rebuiltX = dsX.pow(dsY).pow(dsY.reciprocal());
765                     FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
766                     for (int n = 0; n <= maxOrder; ++n) {
767                         for (int m = 0; m <= maxOrder; ++m) {
768                             if (n + m <= maxOrder) {
769                                 assertEquals(0.0, zero.getPartialDerivative(n, m).getReal(), epsilon[n + m]);
770                             }
771                         }
772                     }
773                 }
774             }
775         }
776     }
777 
778     @Test
779     public void testHypotDefinition() {
780         double epsilon = 1.0e-20;
781         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
782             final FDSFactory<T> factory = buildFactory(2, maxOrder);
783             for (double x = -1.7; x < 2; x += 0.2) {
784                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
785                 for (double y = -1.7; y < 2; y += 0.2) {
786                     FieldDerivativeStructure<T> dsY = factory.variable(1, y);
787                     FieldDerivativeStructure<T> hypot = FieldDerivativeStructure.hypot(dsY, dsX);
788                     FieldDerivativeStructure<T> ref = dsX.multiply(dsX).add(dsY.multiply(dsY)).sqrt();
789                     FieldDerivativeStructure<T> zero = hypot.subtract(ref);
790                     for (int n = 0; n <= maxOrder; ++n) {
791                         for (int m = 0; m <= maxOrder; ++m) {
792                             if (n + m <= maxOrder) {
793                                 assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon);
794                             }
795                         }
796                     }
797                 }
798             }
799         }
800     }
801 
802     @Test
803     public abstract void testHypotNoOverflow();
804 
805     protected void doTestHypotNoOverflow(int tenPower) {
806 
807         final FDSFactory<T> factory = buildFactory(2, 5);
808         FieldDerivativeStructure<T> dsX = factory.variable(0, +3.0);
809         FieldDerivativeStructure<T> dsY = factory.variable(1, -4.0);
810         T scaling = factory.getValueField().getOne();
811         for (int i = 0; i < tenPower; ++i) {
812             scaling = scaling.multiply(10);
813         }
814         dsX = dsX.multiply(scaling);
815         dsY = dsY.multiply(scaling);
816         FieldDerivativeStructure<T> hypot = FieldDerivativeStructure.hypot(dsX, dsY);
817         FieldDerivativeStructure<T> scaledDownHypot = hypot;
818         scaledDownHypot = scaledDownHypot.divide(scaling);
819         assertEquals(5.0, scaledDownHypot.getReal(), 5.0e-15);
820         assertEquals(dsX.divide(hypot).getReal(), scaledDownHypot.getPartialDerivative(1, 0).getReal(), 1.0e-10);
821         assertEquals(dsY.divide(hypot).getReal(), scaledDownHypot.getPartialDerivative(0, 1).getReal(), 1.0e-10);
822 
823         FieldDerivativeStructure<T> sqrt  = dsX.multiply(dsX).add(dsY.multiply(dsY)).sqrt();
824         assertTrue(sqrt.getValue().isInfinite() || sqrt.getValue().isNaN());
825 
826     }
827 
828     @Test
829     public void testHypotNeglectible() {
830 
831         final FDSFactory<T> factory = buildFactory(2, 5);
832         FieldDerivativeStructure<T> dsSmall = factory.variable(0, +3.0e-10);
833         FieldDerivativeStructure<T> dsLarge = factory.variable(1, -4.0e25);
834 
835         assertEquals(dsLarge.getValue().norm(),
836                             FieldDerivativeStructure.hypot(dsSmall, dsLarge).getReal(),
837                             1.0e-10);
838         assertEquals(0,
839                             FieldDerivativeStructure.hypot(dsSmall, dsLarge).getPartialDerivative(1, 0).getReal(),
840                             1.0e-10);
841         assertEquals(-1,
842                             FieldDerivativeStructure.hypot(dsSmall, dsLarge).getPartialDerivative(0, 1).getReal(),
843                             1.0e-10);
844 
845         assertEquals(dsLarge.getValue().norm(),
846                             FieldDerivativeStructure.hypot(dsLarge, dsSmall).getReal(),
847                             1.0e-10);
848         assertEquals(0,
849                             FieldDerivativeStructure.hypot(dsLarge, dsSmall).getPartialDerivative(1, 0).getReal(),
850                             1.0e-10);
851         assertEquals(-1,
852                             FieldDerivativeStructure.hypot(dsLarge, dsSmall).getPartialDerivative(0, 1).getReal(),
853                             1.0e-10);
854 
855     }
856 
857     @Test
858     public void testHypotSpecial() {
859         final FDSFactory<T> factory = buildFactory(2, 5);
860         assertTrue(Double.isNaN(FieldDerivativeStructure.hypot(factory.variable(0, Double.NaN),
861                                                                  factory.variable(0, +3.0e250)).getReal()));
862         assertTrue(Double.isNaN(FieldDerivativeStructure.hypot(factory.variable(0, +3.0e250),
863                                                                  factory.variable(0, Double.NaN)).getReal()));
864         assertTrue(Double.isInfinite(FieldDerivativeStructure.hypot(factory.variable(0, Double.POSITIVE_INFINITY),
865                                                                       factory.variable(0, +3.0e250)).getReal()));
866         assertTrue(Double.isInfinite(FieldDerivativeStructure.hypot(factory.variable(0, +3.0e250),
867                                                                       factory.variable(0, Double.POSITIVE_INFINITY)).getReal()));
868     }
869 
870     @Test
871     public void testFieldRemainder() {
872         double epsilon = 1.0e-15;
873         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
874             final FDSFactory<T> factory = buildFactory(2, maxOrder);
875             for (double x = -1.7; x < 2; x += 0.2) {
876                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
877                 for (double y = -1.7; y < 2; y += 0.2) {
878                     FieldDerivativeStructure<T> remainder = dsX.remainder(buildScalar(y));
879                     FieldDerivativeStructure<T> ref = dsX.subtract(x - FastMath.IEEEremainder(x, y));
880                     FieldDerivativeStructure<T> zero = remainder.subtract(ref);
881                     for (int n = 0; n <= maxOrder; ++n) {
882                         for (int m = 0; m <= maxOrder; ++m) {
883                             if (n + m <= maxOrder) {
884                                 assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon);
885                             }
886                         }
887                     }
888                 }
889             }
890         }
891     }
892 
893     @Test
894     public void testPrimitiveRemainder() {
895         double epsilon = 1.0e-15;
896         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
897             final FDSFactory<T> factory = buildFactory(2, maxOrder);
898             for (double x = -1.7; x < 2; x += 0.2) {
899                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
900                 for (double y = -1.7; y < 2; y += 0.2) {
901                     FieldDerivativeStructure<T> remainder = dsX.remainder(y);
902                     FieldDerivativeStructure<T> ref = dsX.subtract(x - FastMath.IEEEremainder(x, y));
903                     FieldDerivativeStructure<T> zero = remainder.subtract(ref);
904                     for (int n = 0; n <= maxOrder; ++n) {
905                         for (int m = 0; m <= maxOrder; ++m) {
906                             if (n + m <= maxOrder) {
907                                 assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon);
908                             }
909                         }
910                     }
911                 }
912             }
913         }
914     }
915 
916     @Test
917     public void testRemainder() {
918         double epsilon = 2.0e-15;
919         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
920             final FDSFactory<T> factory = buildFactory(2, maxOrder);
921             for (double x = -1.7; x < 2; x += 0.2) {
922                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
923                 for (double y = -1.7; y < 2; y += 0.2) {
924                     FieldDerivativeStructure<T> dsY = factory.variable(1, y);
925                     FieldDerivativeStructure<T> remainder = dsX.remainder(dsY);
926                     FieldDerivativeStructure<T> ref = dsX.subtract(dsY.multiply((x - FastMath.IEEEremainder(x, y)) / y));
927                     FieldDerivativeStructure<T> zero = remainder.subtract(ref);
928                     for (int n = 0; n <= maxOrder; ++n) {
929                         for (int m = 0; m <= maxOrder; ++m) {
930                             if (n + m <= maxOrder) {
931                                 assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon);
932                             }
933                         }
934                     }
935                 }
936             }
937         }
938     }
939 
940     @Override
941     @Test
942     public void testExp() {
943         double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16 };
944         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
945             final FDSFactory<T> factory = buildFactory(1, maxOrder);
946             for (double x = 0.1; x < 1.2; x += 0.001) {
947                 double refExp = FastMath.exp(x);
948                 FieldDerivativeStructure<T> exp = factory.variable(0, x).exp();
949                 for (int n = 0; n <= maxOrder; ++n) {
950                     assertEquals(refExp, exp.getPartialDerivative(n).getReal(), epsilon[n]);
951                 }
952             }
953         }
954     }
955 
956     @Test
957     public void testExpm1Definition() {
958         double epsilon = 3.0e-16;
959         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
960             final FDSFactory<T> factory = buildFactory(1, maxOrder);
961             for (double x = 0.1; x < 1.2; x += 0.001) {
962                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
963                 FieldDerivativeStructure<T> expm11 = dsX.expm1();
964                 FieldDerivativeStructure<T> expm12 = dsX.exp().subtract(dsX.getField().getOne());
965                 FieldDerivativeStructure<T> zero = expm11.subtract(expm12);
966                 for (int n = 0; n <= maxOrder; ++n) {
967                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon);
968                 }
969             }
970         }
971     }
972 
973     @Override
974     @Test
975     public void testLog() {
976         double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 };
977         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
978             final FDSFactory<T> factory = buildFactory(1, maxOrder);
979             for (double x = 0.1; x < 1.2; x += 0.001) {
980                 FieldDerivativeStructure<T> log = factory.variable(0, x).log();
981                 assertEquals(FastMath.log(x), log.getReal(), epsilon[0]);
982                 for (int n = 1; n <= maxOrder; ++n) {
983                     double refDer = -CombinatoricsUtils.factorial(n - 1) / FastMath.pow(-x, n);
984                     assertEquals(refDer, log.getPartialDerivative(n).getReal(), epsilon[n]);
985                 }
986             }
987         }
988     }
989 
990     @Test
991     public void testLog1pDefinition() {
992         double epsilon = 3.0e-16;
993         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
994             final FDSFactory<T> factory = buildFactory(1, maxOrder);
995             for (double x = 0.1; x < 1.2; x += 0.001) {
996                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
997                 FieldDerivativeStructure<T> log1p1 = dsX.log1p();
998                 FieldDerivativeStructure<T> log1p2 = dsX.add(dsX.getField().getOne()).log();
999                 FieldDerivativeStructure<T> zero = log1p1.subtract(log1p2);
1000                 for (int n = 0; n <= maxOrder; ++n) {
1001                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon);
1002                 }
1003             }
1004         }
1005     }
1006 
1007     @Test
1008     public void testLog10Definition() {
1009         double[] epsilon = new double[] { 3.0e-16, 9.0e-16, 8.0e-15, 3.0e-13, 8.0e-12 };
1010         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1011             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1012             for (double x = 0.1; x < 1.2; x += 0.001) {
1013                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1014                 FieldDerivativeStructure<T> log101 = dsX.log10();
1015                 FieldDerivativeStructure<T> log102 = dsX.log().divide(FastMath.log(10.0));
1016                 FieldDerivativeStructure<T> zero = log101.subtract(log102);
1017                 for (int n = 0; n <= maxOrder; ++n) {
1018                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1019                 }
1020             }
1021         }
1022     }
1023 
1024     @Test
1025     public void testLogExp() {
1026         double[] epsilon = new double[] { 2.0e-16, 2.0e-16, 3.0e-16, 2.0e-15, 6.0e-15 };
1027         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1028             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1029             for (double x = 0.1; x < 1.2; x += 0.001) {
1030                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1031                 FieldDerivativeStructure<T> rebuiltX = dsX.exp().log();
1032                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1033                 for (int n = 0; n <= maxOrder; ++n) {
1034                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1035                 }
1036             }
1037         }
1038     }
1039 
1040     @Test
1041     public void testLog1pExpm1() {
1042         double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 5.0e-16, 9.0e-16, 6.0e-15 };
1043         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1044             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1045             for (double x = 0.1; x < 1.2; x += 0.001) {
1046                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1047                 FieldDerivativeStructure<T> rebuiltX = dsX.expm1().log1p();
1048                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1049                 for (int n = 0; n <= maxOrder; ++n) {
1050                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1051                 }
1052             }
1053         }
1054     }
1055 
1056     @Test
1057     public void testLog10Power() {
1058         double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 9.0e-16, 6.0e-15, 7.0e-14 };
1059         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1060             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1061             for (double x = 0.1; x < 1.2; x += 0.001) {
1062                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1063                 FieldDerivativeStructure<T> rebuiltX = factory.constant(10.0).pow(dsX).log10();
1064                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1065                 for (int n = 0; n <= maxOrder; ++n) {
1066                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1067                 }
1068             }
1069         }
1070     }
1071 
1072     @Test
1073     public void testSinCosSeparated() {
1074         double epsilon = 5.0e-16;
1075         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1076             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1077             for (double x = 0.1; x < 1.2; x += 0.001) {
1078                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1079                 FieldDerivativeStructure<T> sin = dsX.sin();
1080                 FieldDerivativeStructure<T> cos = dsX.cos();
1081                 double s = FastMath.sin(x);
1082                 double c = FastMath.cos(x);
1083                 for (int n = 0; n <= maxOrder; ++n) {
1084                     switch (n % 4) {
1085                     case 0 :
1086                         assertEquals( s, sin.getPartialDerivative(n).getReal(), epsilon);
1087                         assertEquals( c, cos.getPartialDerivative(n).getReal(), epsilon);
1088                         break;
1089                     case 1 :
1090                         assertEquals( c, sin.getPartialDerivative(n).getReal(), epsilon);
1091                         assertEquals(-s, cos.getPartialDerivative(n).getReal(), epsilon);
1092                         break;
1093                     case 2 :
1094                         assertEquals(-s, sin.getPartialDerivative(n).getReal(), epsilon);
1095                         assertEquals(-c, cos.getPartialDerivative(n).getReal(), epsilon);
1096                         break;
1097                     default :
1098                         assertEquals(-c, sin.getPartialDerivative(n).getReal(), epsilon);
1099                         assertEquals( s, cos.getPartialDerivative(n).getReal(), epsilon);
1100                         break;
1101                     }
1102                 }
1103             }
1104         }
1105     }
1106 
1107     @Test
1108     public void testSinCosCombined() {
1109         double epsilon = 5.0e-16;
1110         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1111             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1112             for (double x = 0.1; x < 1.2; x += 0.001) {
1113                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1114                 FieldSinCos<FieldDerivativeStructure<T>> sinCos = dsX.sinCos();
1115                 double s = FastMath.sin(x);
1116                 double c = FastMath.cos(x);
1117                 for (int n = 0; n <= maxOrder; ++n) {
1118                     switch (n % 4) {
1119                     case 0 :
1120                         assertEquals( s, sinCos.sin().getPartialDerivative(n).getReal(), epsilon);
1121                         assertEquals( c, sinCos.cos().getPartialDerivative(n).getReal(), epsilon);
1122                         break;
1123                     case 1 :
1124                         assertEquals( c, sinCos.sin().getPartialDerivative(n).getReal(), epsilon);
1125                         assertEquals(-s, sinCos.cos().getPartialDerivative(n).getReal(), epsilon);
1126                         break;
1127                     case 2 :
1128                         assertEquals(-s, sinCos.sin().getPartialDerivative(n).getReal(), epsilon);
1129                         assertEquals(-c, sinCos.cos().getPartialDerivative(n).getReal(), epsilon);
1130                         break;
1131                     default :
1132                         assertEquals(-c, sinCos.sin().getPartialDerivative(n).getReal(), epsilon);
1133                         assertEquals( s, sinCos.cos().getPartialDerivative(n).getReal(), epsilon);
1134                         break;
1135                     }
1136                 }
1137             }
1138         }
1139     }
1140 
1141     @Test
1142     public void testSinAsin() {
1143         double[] epsilon = new double[] { 3.0e-16, 5.0e-16, 3.0e-15, 3.0e-14, 4.0e-13 };
1144         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1145             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1146             for (double x = 0.1; x < 1.2; x += 0.001) {
1147                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1148                 FieldDerivativeStructure<T> rebuiltX = dsX.sin().asin();
1149                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1150                 for (int n = 0; n <= maxOrder; ++n) {
1151                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1152                 }
1153             }
1154         }
1155     }
1156 
1157     @Test
1158     public void testCosAcos() {
1159         double[] epsilon = new double[] { 7.0e-16, 6.0e-15, 2.0e-13, 4.0e-12, 2.0e-10 };
1160         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1161             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1162             for (double x = 0.1; x < 1.2; x += 0.001) {
1163                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1164                 FieldDerivativeStructure<T> rebuiltX = dsX.cos().acos();
1165                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1166                 for (int n = 0; n <= maxOrder; ++n) {
1167                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1168                 }
1169             }
1170         }
1171     }
1172 
1173     @Test
1174     public void testTanAtan() {
1175         double[] epsilon = new double[] { 3.0e-16, 2.0e-16, 2.0e-15, 4.0e-14, 2.0e-12 };
1176         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1177             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1178             for (double x = 0.1; x < 1.2; x += 0.001) {
1179                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1180                 FieldDerivativeStructure<T> rebuiltX = dsX.tan().atan();
1181                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1182                 for (int n = 0; n <= maxOrder; ++n) {
1183                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1184                 }
1185             }
1186         }
1187     }
1188 
1189     @Test
1190     public void testTangentDefinition() {
1191         double[] epsilon = new double[] { 9.0e-16, 4.5e-15, 4.0e-14, 5.0e-13, 2.0e-11 };
1192         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1193             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1194             for (double x = 0.1; x < 1.2; x += 0.001) {
1195                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1196                 FieldDerivativeStructure<T> tan1 = dsX.sin().divide(dsX.cos());
1197                 FieldDerivativeStructure<T> tan2 = dsX.tan();
1198                 FieldDerivativeStructure<T> zero = tan1.subtract(tan2);
1199                 for (int n = 0; n <= maxOrder; ++n) {
1200                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1201                 }
1202             }
1203         }
1204     }
1205 
1206     @Override
1207     @Test
1208     public void testAtan2() {
1209         double[] epsilon = new double[] { 5.0e-16, 3.0e-15, 2.9e-14, 1.0e-12, 8.0e-11 };
1210         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1211             final FDSFactory<T> factory = buildFactory(2, maxOrder);
1212             for (double x = -1.7; x < 2; x += 0.2) {
1213                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1214                 for (double y = -1.7; y < 2; y += 0.2) {
1215                     FieldDerivativeStructure<T> dsY = factory.variable(1, y);
1216                     FieldDerivativeStructure<T> atan2 = FieldDerivativeStructure.atan2(dsY, dsX);
1217                     FieldDerivativeStructure<T> ref = dsY.divide(dsX).atan();
1218                     if (x < 0) {
1219                         ref = (y < 0) ? ref.subtract(FastMath.PI) : ref.add(FastMath.PI);
1220                     }
1221                     FieldDerivativeStructure<T> zero = atan2.subtract(ref);
1222                     for (int n = 0; n <= maxOrder; ++n) {
1223                         for (int m = 0; m <= maxOrder; ++m) {
1224                             if (n + m <= maxOrder) {
1225                                 assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon[n + m]);
1226                             }
1227                         }
1228                     }
1229                 }
1230             }
1231         }
1232     }
1233 
1234     @Test
1235     public void testAtan2SpecialCasesDerivatives() {
1236 
1237         final FDSFactory<T> factory = buildFactory(2, 2);
1238         FieldDerivativeStructure<T> pp =
1239                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(+0.0)), factory.variable(1, buildScalar(+0.0)));
1240         assertEquals(0, pp.getReal(), 1.0e-15);
1241         assertEquals(+1, FastMath.copySign(1, pp.getReal()), 1.0e-15);
1242 
1243         FieldDerivativeStructure<T> pn =
1244                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(+0.0)), factory.variable(1, buildScalar(-0.0)));
1245         assertEquals(FastMath.PI, pn.getReal(), 1.0e-15);
1246 
1247         FieldDerivativeStructure<T> np =
1248                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(-0.0)), factory.variable(1, buildScalar(+0.0)));
1249         assertEquals(0, np.getReal(), 1.0e-15);
1250         assertEquals(-1, FastMath.copySign(1, np.getReal()), 1.0e-15);
1251 
1252         FieldDerivativeStructure<T> nn =
1253                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(-0.0)), factory.variable(1, buildScalar(-0.0)));
1254         assertEquals(-FastMath.PI, nn.getReal(), 1.0e-15);
1255 
1256     }
1257 
1258     @Test
1259     public void testSinhCoshCombined() {
1260         double epsilon = 5.0e-16;
1261         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1262             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1263             for (double x = 0.1; x < 1.2; x += 0.001) {
1264                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1265                 FieldSinhCosh<FieldDerivativeStructure<T>> sinhCosh = dsX.sinhCosh();
1266                 double sh = FastMath.sinh(x);
1267                 double ch = FastMath.cosh(x);
1268                 for (int n = 0; n <= maxOrder; ++n) {
1269                     if (n % 2 == 0) {
1270                         assertEquals(sh, sinhCosh.sinh().getPartialDerivative(n).getReal(), epsilon);
1271                         assertEquals(ch, sinhCosh.cosh().getPartialDerivative(n).getReal(), epsilon);
1272                     } else {
1273                         assertEquals(ch, sinhCosh.sinh().getPartialDerivative(n).getReal(), epsilon);
1274                         assertEquals(sh, sinhCosh.cosh().getPartialDerivative(n).getReal(), epsilon);
1275                     }
1276                 }
1277             }
1278         }
1279     }
1280 
1281     @Test
1282     public void testSinhDefinition() {
1283         double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 5.0e-16, 2.0e-15, 6.0e-15 };
1284         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1285             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1286             for (double x = 0.1; x < 1.2; x += 0.001) {
1287                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1288                 FieldDerivativeStructure<T> sinh1 = dsX.exp().subtract(dsX.exp().reciprocal()).multiply(0.5);
1289                 FieldDerivativeStructure<T> sinh2 = dsX.sinh();
1290                 FieldDerivativeStructure<T> zero = sinh1.subtract(sinh2);
1291                 for (int n = 0; n <= maxOrder; ++n) {
1292                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1293                 }
1294             }
1295         }
1296     }
1297 
1298     @Test
1299     public void testCoshDefinition() {
1300         double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 5.0e-16, 2.0e-15, 6.0e-15 };
1301         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1302             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1303             for (double x = 0.1; x < 1.2; x += 0.001) {
1304                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1305                 FieldDerivativeStructure<T> cosh1 = dsX.exp().add(dsX.exp().reciprocal()).multiply(0.5);
1306                 FieldDerivativeStructure<T> cosh2 = dsX.cosh();
1307                 FieldDerivativeStructure<T> zero = cosh1.subtract(cosh2);
1308                 for (int n = 0; n <= maxOrder; ++n) {
1309                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1310                 }
1311             }
1312         }
1313     }
1314 
1315     @Test
1316     public void testTanhDefinition() {
1317         double[] epsilon = new double[] { 3.0e-16, 5.0e-16, 7.0e-16, 3.0e-15, 2.0e-14 };
1318         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1319             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1320             for (double x = 0.1; x < 1.2; x += 0.001) {
1321                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1322                 FieldDerivativeStructure<T> tanh1 = dsX.exp().subtract(dsX.exp().reciprocal()).divide(dsX.exp().add(dsX.exp().reciprocal()));
1323                 FieldDerivativeStructure<T> tanh2 = dsX.tanh();
1324                 FieldDerivativeStructure<T> zero = tanh1.subtract(tanh2);
1325                 for (int n = 0; n <= maxOrder; ++n) {
1326                     assertEquals(0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1327                 }
1328             }
1329         }
1330     }
1331 
1332     @Test
1333     public void testSinhAsinh() {
1334         double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 4.0e-16, 7.0e-16, 3.0e-15, 8.0e-15 };
1335         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1336             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1337             for (double x = 0.1; x < 1.2; x += 0.001) {
1338                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1339                 FieldDerivativeStructure<T> rebuiltX = dsX.sinh().asinh();
1340                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1341                 for (int n = 0; n <= maxOrder; ++n) {
1342                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1343                 }
1344             }
1345         }
1346     }
1347 
1348     @Test
1349     public void testCoshAcosh() {
1350         double[] epsilon = new double[] { 2.0e-15, 1.0e-14, 2.0e-13, 6.0e-12, 3.0e-10, 2.0e-8 };
1351         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1352             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1353             for (double x = 0.1; x < 1.2; x += 0.001) {
1354                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1355                 FieldDerivativeStructure<T> rebuiltX = dsX.cosh().acosh();
1356                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1357                 for (int n = 0; n <= maxOrder; ++n) {
1358                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1359                 }
1360             }
1361         }
1362     }
1363 
1364     @Test
1365     public void testTanhAtanh() {
1366         double[] epsilon = new double[] { 5.0e-16, 2.0e-16, 7.0e-16, 4.0e-15, 3.0e-14, 4.0e-13 };
1367         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1368             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1369             for (double x = 0.1; x < 1.2; x += 0.001) {
1370                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1371                 FieldDerivativeStructure<T> rebuiltX = dsX.tanh().atanh();
1372                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1373                 for (int n = 0; n <= maxOrder; ++n) {
1374                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon[n]);
1375                 }
1376             }
1377         }
1378     }
1379 
1380     @Test
1381     public void testCompositionOneVariableY() {
1382         double epsilon = 1.0e-13;
1383         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1384             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1385             for (double x = 0.1; x < 1.2; x += 0.1) {
1386                 FieldDerivativeStructure<T> dsX = factory.constant(x);
1387                 for (double y = 0.1; y < 1.2; y += 0.1) {
1388                     FieldDerivativeStructure<T> dsY = factory.variable(0, y);
1389                     FieldDerivativeStructure<T> f = dsX.divide(dsY).sqrt();
1390                     double f0 = FastMath.sqrt(x / y);
1391                     assertEquals(f0, f.getReal(), FastMath.abs(epsilon * f0));
1392                     if (f.getOrder() > 0) {
1393                         double f1 = -x / (2 * y * y * f0);
1394                         assertEquals(f1, f.getPartialDerivative(1).getReal(), FastMath.abs(epsilon * f1));
1395                         if (f.getOrder() > 1) {
1396                             double f2 = (f0 - x / (4 * y * f0)) / (y * y);
1397                             assertEquals(f2, f.getPartialDerivative(2).getReal(), FastMath.abs(epsilon * f2));
1398                             if (f.getOrder() > 2) {
1399                                 double f3 = (x / (8 * y * f0) - 2 * f0) / (y * y * y);
1400                                 assertEquals(f3, f.getPartialDerivative(3).getReal(), FastMath.abs(epsilon * f3));
1401                             }
1402                         }
1403                     }
1404                 }
1405             }
1406         }
1407     }
1408 
1409     @Test
1410     public void testTaylorPrimitivePolynomial() {
1411         final FDSFactory<T> factory = buildFactory(3, 4);
1412         for (double x = 0; x < 1.2; x += 0.1) {
1413             FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1414             for (double y = 0; y < 1.2; y += 0.2) {
1415                 FieldDerivativeStructure<T> dsY = factory.variable(1, y);
1416                 for (double z = 0; z < 1.2; z += 0.2) {
1417                     FieldDerivativeStructure<T> dsZ = factory.variable(2, z);
1418                     FieldDerivativeStructure<T> f = dsX.multiply(dsY).add(dsZ).multiply(dsX).multiply(dsY);
1419                     for (double dx = -0.2; dx < 0.2; dx += 0.2) {
1420                         for (double dy = -0.2; dy < 0.2; dy += 0.1) {
1421                             for (double dz = -0.2; dz < 0.2; dz += 0.1) {
1422                                 double ref = (x + dx) * (y + dy) * ((x + dx) * (y + dy) + (z + dz));
1423                                 assertEquals(ref, f.taylor(dx, dy, dz).getReal(), 2.0e-15);
1424                             }
1425                         }
1426                     }
1427                 }
1428             }
1429         }
1430     }
1431 
1432     @Test
1433     public void testTaylorFieldPolynomial() {
1434         final FDSFactory<T> factory = buildFactory(3, 4);
1435         for (double x = 0; x < 1.2; x += 0.1) {
1436             FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1437             for (double y = 0; y < 1.2; y += 0.2) {
1438                 FieldDerivativeStructure<T> dsY = factory.variable(1, y);
1439                 for (double z = 0; z < 1.2; z += 0.2) {
1440                     FieldDerivativeStructure<T> dsZ = factory.variable(2, z);
1441                     FieldDerivativeStructure<T> f = dsX.multiply(dsY).add(dsZ).multiply(dsX).multiply(dsY);
1442                     for (double dx = -0.2; dx < 0.2; dx += 0.2) {
1443                         T dxF = buildScalar(dx);
1444                         for (double dy = -0.2; dy < 0.2; dy += 0.1) {
1445                             T dyF = buildScalar(dy);
1446                             for (double dz = -0.2; dz < 0.2; dz += 0.1) {
1447                                 T dzF = buildScalar(dz);
1448                                 double ref = (x + dx) * (y + dy) * ((x + dx) * (y + dy) + (z + dz));
1449                                 assertEquals(ref, f.taylor(dxF, dyF, dzF).getReal(), 2.0e-15);
1450                             }
1451                         }
1452                     }
1453                 }
1454             }
1455         }
1456     }
1457 
1458     @Test
1459     public void testTaylorAtan2() {
1460         double[] expected = new double[] { 0.214, 0.0241, 0.00422, 6.48e-4, 8.04e-5 };
1461         double x0 =  0.1;
1462         double y0 = -0.3;
1463         for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
1464             final FDSFactory<T> factory = buildFactory(2, maxOrder);
1465             FieldDerivativeStructure<T> dsX   = factory.variable(0, x0);
1466             FieldDerivativeStructure<T> dsY   = factory.variable(1, y0);
1467             FieldDerivativeStructure<T> atan2 = FieldDerivativeStructure.atan2(dsY, dsX);
1468             double maxError = 0;
1469             for (double dx = -0.05; dx < 0.05; dx += 0.001) {
1470                 for (double dy = -0.05; dy < 0.05; dy += 0.001) {
1471                     double ref = FastMath.atan2(y0 + dy, x0 + dx);
1472                     maxError = FastMath.max(maxError, FastMath.abs(ref - atan2.taylor(dx, dy).getReal()));
1473                 }
1474             }
1475             assertEquals(0.0, expected[maxOrder] - maxError, 0.01 * expected[maxOrder]);
1476         }
1477     }
1478 
1479     @Test
1480     public void test2Abs() {
1481 
1482         final FDSFactory<T> factory = buildFactory(1, 1);
1483         FieldDerivativeStructure<T> minusOne = factory.variable(0, -1.0);
1484         assertEquals(+1.0, minusOne.abs().getPartialDerivative(0).getReal(), 1.0e-15);
1485         assertEquals(-1.0, minusOne.abs().getPartialDerivative(1).getReal(), 1.0e-15);
1486 
1487         FieldDerivativeStructure<T> plusOne = factory.variable(0, +1.0);
1488         assertEquals(+1.0, plusOne.abs().getPartialDerivative(0).getReal(), 1.0e-15);
1489         assertEquals(+1.0, plusOne.abs().getPartialDerivative(1).getReal(), 1.0e-15);
1490 
1491         FieldDerivativeStructure<T> minusZero = factory.variable(0, buildScalar(-0.0));
1492         assertEquals(+0.0, minusZero.abs().getPartialDerivative(0).getReal(), 1.0e-15);
1493         assertEquals(-1.0, minusZero.abs().getPartialDerivative(1).getReal(), 1.0e-15);
1494 
1495         FieldDerivativeStructure<T> plusZero = factory.variable(0, buildScalar(+0.0));
1496         assertEquals(+0.0, plusZero.abs().getPartialDerivative(0).getReal(), 1.0e-15);
1497         assertEquals(+1.0, plusZero.abs().getPartialDerivative(1).getReal(), 1.0e-15);
1498 
1499     }
1500 
1501     @Override
1502     @Test
1503     public void testSign() {
1504 
1505         final FDSFactory<T> factory = buildFactory(1, 1);
1506         FieldDerivativeStructure<T> minusOne = factory.variable(0, -1.0);
1507         assertEquals(-1.0, minusOne.sign().getPartialDerivative(0).getReal(), 1.0e-15);
1508         assertEquals( 0.0, minusOne.sign().getPartialDerivative(1).getReal(), 1.0e-15);
1509 
1510         FieldDerivativeStructure<T> plusOne = factory.variable(0, +1.0);
1511         assertEquals(+1.0, plusOne.sign().getPartialDerivative(0).getReal(), 1.0e-15);
1512         assertEquals( 0.0, plusOne.sign().getPartialDerivative(1).getReal(), 1.0e-15);
1513 
1514         FieldDerivativeStructure<T> minusZero = factory.variable(0, buildScalar(-0.0));
1515         assertEquals(-0.0, minusZero.sign().getPartialDerivative(0).getReal(), 1.0e-15);
1516         assertTrue(Double.doubleToLongBits(minusZero.sign().getReal()) < 0);
1517         assertEquals( 0.0, minusZero.sign().getPartialDerivative(1).getReal(), 1.0e-15);
1518 
1519         FieldDerivativeStructure<T> plusZero = factory.variable(0, buildScalar(+0.0));
1520         assertEquals(+0.0, plusZero.sign().getPartialDerivative(0).getReal(), 1.0e-15);
1521         assertEquals(0, Double.doubleToLongBits(plusZero.sign().getReal()));
1522         assertEquals( 0.0, plusZero.sign().getPartialDerivative(1).getReal(), 1.0e-15);
1523 
1524     }
1525 
1526     @Test
1527     public void testCeilFloorRintLong() {
1528 
1529         final FDSFactory<T> factory = buildFactory(1, 1);
1530         FieldDerivativeStructure<T> x = factory.variable(0, -1.5);
1531         assertEquals(-1.5, x.getPartialDerivative(0).getReal(), 1.0e-15);
1532         assertEquals(+1.0, x.getPartialDerivative(1).getReal(), 1.0e-15);
1533         assertEquals(-1.0, x.ceil().getPartialDerivative(0).getReal(), 1.0e-15);
1534         assertEquals(+0.0, x.ceil().getPartialDerivative(1).getReal(), 1.0e-15);
1535         assertEquals(-2.0, x.floor().getPartialDerivative(0).getReal(), 1.0e-15);
1536         assertEquals(+0.0, x.floor().getPartialDerivative(1).getReal(), 1.0e-15);
1537         assertEquals(-2.0, x.rint().getPartialDerivative(0).getReal(), 1.0e-15);
1538         assertEquals(+0.0, x.rint().getPartialDerivative(1).getReal(), 1.0e-15);
1539         assertEquals(-2.0, x.subtract(x.getField().getOne()).rint().getPartialDerivative(0).getReal(), 1.0e-15);
1540 
1541     }
1542 
1543     @Test
1544     public void testCopySign() {
1545 
1546         final FDSFactory<T> factory = buildFactory(1, 1);
1547         FieldDerivativeStructure<T> minusOne = factory.variable(0, -1.0);
1548         assertEquals(+1.0, minusOne.copySign(+1.0).getPartialDerivative(0).getReal(), 1.0e-15);
1549         assertEquals(-1.0, minusOne.copySign(+1.0).getPartialDerivative(1).getReal(), 1.0e-15);
1550         assertEquals(-1.0, minusOne.copySign(-1.0).getPartialDerivative(0).getReal(), 1.0e-15);
1551         assertEquals(+1.0, minusOne.copySign(-1.0).getPartialDerivative(1).getReal(), 1.0e-15);
1552         assertEquals(+1.0, minusOne.copySign(+0.0).getPartialDerivative(0).getReal(), 1.0e-15);
1553         assertEquals(-1.0, minusOne.copySign(+0.0).getPartialDerivative(1).getReal(), 1.0e-15);
1554         assertEquals(-1.0, minusOne.copySign(-0.0).getPartialDerivative(0).getReal(), 1.0e-15);
1555         assertEquals(+1.0, minusOne.copySign(-0.0).getPartialDerivative(1).getReal(), 1.0e-15);
1556         assertEquals(+1.0, minusOne.copySign(Double.NaN).getPartialDerivative(0).getReal(), 1.0e-15);
1557         assertEquals(-1.0, minusOne.copySign(Double.NaN).getPartialDerivative(1).getReal(), 1.0e-15);
1558         assertEquals(+1.0, minusOne.copySign(buildScalar(+1.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1559         assertEquals(-1.0, minusOne.copySign(buildScalar(+1.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1560         assertEquals(-1.0, minusOne.copySign(buildScalar(-1.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1561         assertEquals(+1.0, minusOne.copySign(buildScalar(-1.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1562         assertEquals(+1.0, minusOne.copySign(buildScalar(+0.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1563         assertEquals(-1.0, minusOne.copySign(buildScalar(+0.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1564         assertEquals(-1.0, minusOne.copySign(buildScalar(-0.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1565         assertEquals(+1.0, minusOne.copySign(buildScalar(-0.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1566         assertEquals(+1.0, minusOne.copySign(buildScalar(Double.NaN)).getPartialDerivative(0).getReal(), 1.0e-15);
1567         assertEquals(-1.0, minusOne.copySign(buildScalar(Double.NaN)).getPartialDerivative(1).getReal(), 1.0e-15);
1568 
1569         FieldDerivativeStructure<T> plusOne = factory.variable(0, +1.0);
1570         assertEquals(+1.0, plusOne.copySign(+1.0).getPartialDerivative(0).getReal(), 1.0e-15);
1571         assertEquals(+1.0, plusOne.copySign(+1.0).getPartialDerivative(1).getReal(), 1.0e-15);
1572         assertEquals(-1.0, plusOne.copySign(-1.0).getPartialDerivative(0).getReal(), 1.0e-15);
1573         assertEquals(-1.0, plusOne.copySign(-1.0).getPartialDerivative(1).getReal(), 1.0e-15);
1574         assertEquals(+1.0, plusOne.copySign(+0.0).getPartialDerivative(0).getReal(), 1.0e-15);
1575         assertEquals(+1.0, plusOne.copySign(+0.0).getPartialDerivative(1).getReal(), 1.0e-15);
1576         assertEquals(-1.0, plusOne.copySign(-0.0).getPartialDerivative(0).getReal(), 1.0e-15);
1577         assertEquals(-1.0, plusOne.copySign(-0.0).getPartialDerivative(1).getReal(), 1.0e-15);
1578         assertEquals(+1.0, plusOne.copySign(Double.NaN).getPartialDerivative(0).getReal(), 1.0e-15);
1579         assertEquals(+1.0, plusOne.copySign(Double.NaN).getPartialDerivative(1).getReal(), 1.0e-15);
1580         assertEquals(+1.0, plusOne.copySign(buildScalar(+1.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1581         assertEquals(+1.0, plusOne.copySign(buildScalar(+1.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1582         assertEquals(-1.0, plusOne.copySign(buildScalar(-1.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1583         assertEquals(-1.0, plusOne.copySign(buildScalar(-1.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1584         assertEquals(+1.0, plusOne.copySign(buildScalar(+0.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1585         assertEquals(+1.0, plusOne.copySign(buildScalar(+0.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1586         assertEquals(-1.0, plusOne.copySign(buildScalar(-0.0)).getPartialDerivative(0).getReal(), 1.0e-15);
1587         assertEquals(-1.0, plusOne.copySign(buildScalar(-0.0)).getPartialDerivative(1).getReal(), 1.0e-15);
1588         assertEquals(+1.0, plusOne.copySign(buildScalar(Double.NaN)).getPartialDerivative(0).getReal(), 1.0e-15);
1589         assertEquals(+1.0, plusOne.copySign(buildScalar(Double.NaN)).getPartialDerivative(1).getReal(), 1.0e-15);
1590 
1591     }
1592 
1593     @Test
1594     public void testToDegreesDefinition() {
1595         double epsilon = 3.0e-16;
1596         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1597             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1598             for (double x = 0.1; x < 1.2; x += 0.001) {
1599                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1600                 assertEquals(FastMath.toDegrees(x), dsX.toDegrees().getReal(), epsilon * FastMath.toDegrees(x));
1601                 for (int n = 1; n <= maxOrder; ++n) {
1602                     if (n == 1) {
1603                         assertEquals(180 / FastMath.PI, dsX.toDegrees().getPartialDerivative(1).getReal(), epsilon);
1604                     } else {
1605                         assertEquals(0.0, dsX.toDegrees().getPartialDerivative(n).getReal(), epsilon);
1606                     }
1607                 }
1608             }
1609         }
1610     }
1611 
1612     @Test
1613     public void testToRadiansDefinition() {
1614         double epsilon = 3.0e-16;
1615         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1616             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1617             for (double x = 0.1; x < 1.2; x += 0.001) {
1618                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1619                 assertEquals(FastMath.toRadians(x), dsX.toRadians().getReal(), epsilon);
1620                 for (int n = 1; n <= maxOrder; ++n) {
1621                     if (n == 1) {
1622                         assertEquals(FastMath.PI / 180, dsX.toRadians().getPartialDerivative(1).getReal(), epsilon);
1623                     } else {
1624                         assertEquals(0.0, dsX.toRadians().getPartialDerivative(n).getReal(), epsilon);
1625                     }
1626                 }
1627             }
1628         }
1629     }
1630 
1631     @Test
1632     public void testDegRad() {
1633         double epsilon = 3.0e-16;
1634         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1635             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1636             for (double x = 0.1; x < 1.2; x += 0.001) {
1637                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1638                 FieldDerivativeStructure<T> rebuiltX = dsX.toDegrees().toRadians();
1639                 FieldDerivativeStructure<T> zero = rebuiltX.subtract(dsX);
1640                 for (int n = 0; n <= maxOrder; ++n) {
1641                     assertEquals(0.0, zero.getPartialDerivative(n).getReal(), epsilon);
1642                 }
1643             }
1644         }
1645     }
1646 
1647     @Test
1648     public void testComposeMismatchedDimensions() {
1649         assertThrows(MathIllegalArgumentException.class, () -> {
1650             final FDSFactory<T> factory = buildFactory(1, 3);
1651             factory.variable(0, 1.2).compose(new double[3]);
1652         });
1653     }
1654 
1655     @Test
1656     public abstract void testComposeField();
1657 
1658     protected void doTestComposeField(final double[] epsilon) {
1659         double[] maxError = new double[epsilon.length];
1660         for (int maxOrder = 0; maxOrder < epsilon.length; ++maxOrder) {
1661             @SuppressWarnings("unchecked")
1662             FieldPolynomialFunction<T>[] p = (FieldPolynomialFunction<T>[]) Array.newInstance(FieldPolynomialFunction.class,
1663                                                                                               maxOrder + 1);
1664             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1665             T[] coefficients = MathArrays.buildArray(factory.getValueField(), epsilon.length);
1666             for (int i = 0; i < coefficients.length; ++i) {
1667                 coefficients[i] = factory.getValueField().getZero().newInstance(i + 1);
1668             }
1669             p[0] = new FieldPolynomialFunction<>(coefficients);
1670             for (int i = 1; i <= maxOrder; ++i) {
1671                 p[i] = p[i - 1].polynomialDerivative();
1672             }
1673             for (double x = 0.1; x < 1.2; x += 0.001) {
1674                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1675                 FieldDerivativeStructure<T> dsY1 = dsX.getField().getZero();
1676                 for (int i = p[0].degree(); i >= 0; --i) {
1677                     dsY1 = dsY1.multiply(dsX).add(p[0].getCoefficients()[i]);
1678                 }
1679                 T[] f = MathArrays.buildArray(getField(), maxOrder + 1);
1680                 for (int i = 0; i < f.length; ++i) {
1681                     f[i] = p[i].value(x);
1682                 }
1683                 FieldDerivativeStructure<T> dsY2 = dsX.compose(f);
1684                 FieldDerivativeStructure<T> zero = dsY1.subtract(dsY2);
1685                 for (int n = 0; n <= maxOrder; ++n) {
1686                     maxError[n] = FastMath.max(maxError[n], FastMath.abs(zero.getPartialDerivative(n).getReal()));
1687                 }
1688             }
1689         }
1690         for (int n = 0; n < maxError.length; ++n) {
1691             assertEquals(0.0, maxError[n], epsilon[n]);
1692         }
1693     }
1694 
1695     @Test
1696     public abstract void testComposePrimitive();
1697 
1698     protected void doTestComposePrimitive(final double[] epsilon) {
1699         PolynomialFunction poly =
1700                 new PolynomialFunction(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 });
1701         double[] maxError = new double[epsilon.length];
1702         for (int maxOrder = 0; maxOrder < epsilon.length; ++maxOrder) {
1703             final FDSFactory<T> factory = buildFactory(1, maxOrder);
1704             PolynomialFunction[] p = new PolynomialFunction[maxOrder + 1];
1705             p[0] = poly;
1706             for (int i = 1; i <= maxOrder; ++i) {
1707                 p[i] = p[i - 1].polynomialDerivative();
1708             }
1709             for (double x = 0.1; x < 1.2; x += 0.001) {
1710                 FieldDerivativeStructure<T> dsX = factory.variable(0, x);
1711                 FieldDerivativeStructure<T> dsY1 = dsX.getField().getZero();
1712                 for (int i = poly.degree(); i >= 0; --i) {
1713                     dsY1 = dsY1.multiply(dsX).add(poly.getCoefficients()[i]);
1714                 }
1715                 double[] f = new double[maxOrder + 1];
1716                 for (int i = 0; i < f.length; ++i) {
1717                     f[i] = p[i].value(x);
1718                 }
1719                 FieldDerivativeStructure<T> dsY2 = dsX.compose(f);
1720                 FieldDerivativeStructure<T> zero = dsY1.subtract(dsY2);
1721                 for (int n = 0; n <= maxOrder; ++n) {
1722                     maxError[n] = FastMath.max(maxError[n], FastMath.abs(zero.getPartialDerivative(n).getReal()));
1723                 }
1724             }
1725         }
1726         for (int n = 0; n < maxError.length; ++n) {
1727             assertEquals(0.0, maxError[n], epsilon[n]);
1728         }
1729     }
1730 
1731     @Test
1732     public void testIntegration() {
1733         // check that first-order integration on two variables does not depend on sequence of operations
1734         final RandomGenerator random = new Well19937a(0x87bb96d6e11557bdl);
1735         final FDSFactory<T> factory = buildFactory(3, 7);
1736         final int size = factory.getCompiler().getSize();
1737         for (int count = 0; count < 100; ++count) {
1738             final double[] data = new double[size];
1739             for (int i = 0; i < size; i++) {
1740                 data[i] = random.nextDouble();
1741             }
1742             final FieldDerivativeStructure<T> f       = factory.build(data);
1743             final FieldDerivativeStructure<T> i2fIxIy = f.integrate(0, 1).integrate(1, 1);
1744             final FieldDerivativeStructure<T> i2fIyIx = f.integrate(1, 1).integrate(0, 1);
1745             checkEquals(i2fIxIy, i2fIyIx, 0.);
1746         }
1747     }
1748 
1749     @Test
1750     public void testIntegrationGreaterThanOrder() {
1751         // check that integration to a too high order generates zero
1752         // as integration constants are set to zero
1753         final RandomGenerator random = new Well19937a(0x4744a847b11e4c6fl);
1754         final FDSFactory<T> factory = buildFactory(3, 7);
1755         final int size = factory.getCompiler().getSize();
1756         for (int count = 0; count < 100; ++count) {
1757             final double[] data = new double[size];
1758             for (int i = 0; i < size; i++) {
1759                 data[i] = random.nextDouble();
1760             }
1761             final FieldDerivativeStructure<T> f = factory.build(data);
1762             for (int index = 0; index < factory.getCompiler().getFreeParameters(); ++index) {
1763                 final FieldDerivativeStructure<T> integ = f.integrate(index, factory.getCompiler().getOrder() + 1);
1764                 factory.constant(0).equals(integ);
1765             }
1766         }
1767     }
1768 
1769     @Test
1770     public void testIntegrationNoOp() {
1771         // check that integration of order 0 is no-op
1772         final RandomGenerator random = new Well19937a(0x75a35152f30f644bl);
1773         final FDSFactory<T> factory = buildFactory(3, 7);
1774         final int size = factory.getCompiler().getSize();
1775         for (int count = 0; count < 100; ++count) {
1776             final double[] data = new double[size];
1777             for (int i = 0; i < size; i++) {
1778                 data[i] = random.nextDouble();
1779             }
1780             final FieldDerivativeStructure<T> f = factory.build(data);
1781             for (int index = 0; index < factory.getCompiler().getFreeParameters(); ++index) {
1782                 final FieldDerivativeStructure<T> integ = f.integrate(index, 0);
1783                 checkEquals(f, integ, 0.);
1784             }
1785         }
1786     }
1787 
1788     @Test
1789     public void testDifferentiationNoOp() {
1790         // check that differentiation of order 0 is no-op
1791         final RandomGenerator random = new Well19937a(0x3b6ae4c2f1282949l);
1792         final FDSFactory<T> factory = buildFactory(3, 7);
1793         final int size = factory.getCompiler().getSize();
1794         for (int count = 0; count < 100; ++count) {
1795             final double[] data = new double[size];
1796             for (int i = 0; i < size; i++) {
1797                 data[i] = random.nextDouble();
1798             }
1799             final FieldDerivativeStructure<T> f = factory.build(data);
1800             for (int index = 0; index < factory.getCompiler().getFreeParameters(); ++index) {
1801                 final FieldDerivativeStructure<T> integ = f.differentiate(index, 0);
1802                 f.equals(integ);
1803             }
1804         }
1805     }
1806 
1807     @Test
1808     public void testIntegrationDifferentiation() {
1809         // check that integration and differentiation for univariate functions are each other inverse except for constant
1810         // term and highest order one
1811         final RandomGenerator random = new Well19937a(0x67fe66c05e5ee222l);
1812         final FDSFactory<T> factory = buildFactory(1, 25);
1813         final int size = factory.getCompiler().getSize();
1814         for (int count = 0; count < 100; ++count) {
1815             final double[] data = new double[size];
1816             for (int i = 1; i < size - 1; i++) {
1817                 data[i] = random.nextDouble();
1818             }
1819             final int indexVar = 0;
1820             final FieldDerivativeStructure<T> f = factory.build(data);
1821             final FieldDerivativeStructure<T> f2 = f.integrate(indexVar, 1).differentiate(indexVar, 1);
1822             final FieldDerivativeStructure<T> f3 = f.differentiate(indexVar, 1).integrate(indexVar, 1);
1823             checkEquals(f2, f, 0.);
1824             checkEquals(f2, f3, 0.);
1825             // check special case when non-positive integration order actually returns differentiation
1826             final FieldDerivativeStructure<T> df = f.integrate(indexVar, -1);
1827             final FieldDerivativeStructure<T> df2 = f.differentiate(indexVar, 1);
1828             checkEquals(df, df2, 0.);
1829             // check special case when non-positive differentiation order actually returns integration
1830             final FieldDerivativeStructure<T> fi  = f.differentiate(indexVar, -1);
1831             final FieldDerivativeStructure<T> fi2 = f.integrate(indexVar, 1);
1832             checkEquals(fi, fi2, 0.);
1833         }
1834     }
1835 
1836     @Test
1837     public void testDifferentiation1() {
1838         // check differentiation operator with result obtained manually
1839         final int freeParam = 3;
1840         final int order = 5;
1841         final FDSFactory<T> factory = buildFactory(freeParam, order);
1842         final FieldDerivativeStructure<T> f = factory.variable(0, 1.0);
1843         final int[] orders = new int[freeParam];
1844         orders[0] = 2;
1845         orders[1] = 1;
1846         orders[2] = 1;
1847         final T value = factory.getValueField().getZero().newInstance(10.);
1848         f.setDerivativeComponent(factory.getCompiler().getPartialDerivativeIndex(orders), value);
1849         final FieldDerivativeStructure<T> dfDx = f.differentiate(0, 1);
1850         orders[0] -= 1;
1851         assertEquals(1., dfDx.getPartialDerivative(new int[freeParam]).getReal(), 0.);
1852         assertEquals(value.getReal(), dfDx.getPartialDerivative(orders).getReal(), 0.);
1853         factory.constant(0.0).equals(f.differentiate(0, order + 1));
1854     }
1855 
1856     @Test
1857     public void testDifferentiation2() {
1858         // check that first-order differentiation twice is same as second-order differentiation
1859         final RandomGenerator random = new Well19937a(0xec293aaee352de94l);
1860         final FDSFactory<T> factory = buildFactory(5, 4);
1861         final int size = factory.getCompiler().getSize();
1862         for (int count = 0; count < 100; ++count) {
1863             final double[] data = new double[size];
1864             for (int i = 0; i < size; i++) {
1865                 data[i] = random.nextDouble();
1866             }
1867             final FieldDerivativeStructure<T> f = factory.build(data);
1868             final FieldDerivativeStructure<T> d2fDx2 = f.differentiate(0, 1).differentiate(0, 1);
1869             final FieldDerivativeStructure<T> d2fDx2Bis = f.differentiate(0, 2);
1870             checkEquals(d2fDx2, d2fDx2Bis, 0.);
1871         }
1872     }
1873 
1874     @Test
1875     public void testDifferentiation3() {
1876         // check that first-order differentiation on two variables does not depend on sequence of operations
1877         final RandomGenerator random = new Well19937a(0x35409ecc1348e46cl);
1878         final FDSFactory<T> factory = buildFactory(3, 7);
1879         final int size = factory.getCompiler().getSize();
1880         for (int count = 0; count < 100; ++count) {
1881             final double[] data = new double[size];
1882             for (int i = 0; i < size; i++) {
1883                 data[i] = random.nextDouble();
1884             }
1885             final FieldDerivativeStructure<T> f = factory.build(data);
1886             final FieldDerivativeStructure<T> d2fDxDy = f.differentiate(0, 1).differentiate(1, 1);
1887             final FieldDerivativeStructure<T> d2fDyDx = f.differentiate(1, 1).differentiate(0, 1);
1888             checkEquals(d2fDxDy, d2fDyDx, 0.);
1889         }
1890     }
1891 
1892     @Test
1893     public void testField() {
1894         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
1895             final FDSFactory<T> factory = buildFactory(3, maxOrder);
1896             FieldDerivativeStructure<T> x = factory.variable(0, 1.0);
1897             checkF0F1(x.getField().getZero(), 0.0, 0.0, 0.0, 0.0);
1898             checkF0F1(x.getField().getOne(), 1.0, 0.0, 0.0, 0.0);
1899             assertEquals(maxOrder, x.getField().getZero().getOrder());
1900             assertEquals(3, x.getField().getZero().getFreeParameters());
1901             assertEquals(FieldDerivativeStructure.class, x.getField().getRuntimeClass());
1902         }
1903     }
1904 
1905     @Test
1906     public void testEquals() {
1907         final FDSFactory<T> factory33 = buildFactory(3, 3);
1908         final FDSFactory<T> factory35 = buildFactory(3, 5);
1909         final FDSFactory<T> factory55 = buildFactory(5, 5);
1910         assertNotEquals(factory33.variable(0, 1.0), factory35.variable(0, 1.0));
1911         assertNotEquals(factory33.variable(0, 1.0), factory55.variable(0, 1.0));
1912         assertEquals(factory33.variable(0, 1.0), factory33.variable(0, 1.0));
1913         assertNotEquals(factory33.variable(0, 1.0), new UnivariateDerivative1(0, 0));
1914         assertNotEquals(factory33.variable(0, 1.0), factory33.variable(1, 1.0));
1915         final FieldDerivativeStructure<T> x = factory33.variable(0, 1.0);
1916         Assertions.assertEquals(x, x);
1917         Assertions.assertNotEquals(0, x.hashCode());
1918     }
1919 
1920     @Test
1921     public void testOneParameterConstructor() {
1922         double x = 1.2;
1923         double cos = FastMath.cos(x);
1924         double sin = FastMath.sin(x);
1925         final FDSFactory<T> factory = buildFactory(1, 4);
1926         FieldDerivativeStructure<T> yRef = factory.variable(0, x).cos();
1927         try {
1928             factory.build(0.0, 0.0);
1929             fail("an exception should have been thrown");
1930         } catch (MathIllegalArgumentException dme) {
1931             // expected
1932         } catch (Exception e) {
1933             fail("wrong exceptionc caught " + e.getClass().getName());
1934         }
1935         double[] derivatives = new double[] { cos, -sin, -cos, sin, cos };
1936         FieldDerivativeStructure<T> y = factory.build(derivatives);
1937         checkEquals(yRef, y, 1.0e-15);
1938         T[] all = y.getAllDerivatives();
1939         assertEquals(derivatives.length, all.length);
1940         for (int i = 0; i < all.length; ++i) {
1941             assertEquals(derivatives[i], all[i].getReal(), 1.0e-15);
1942         }
1943     }
1944 
1945     @Test
1946     public void testOneOrderConstructor() {
1947         double x =  1.2;
1948         double y =  2.4;
1949         double z = 12.5;
1950         final FDSFactory<T> factory = buildFactory(3, 1);
1951         FieldDerivativeStructure<T> xRef = factory.variable(0, x);
1952         FieldDerivativeStructure<T> yRef = factory.variable(1, y);
1953         FieldDerivativeStructure<T> zRef = factory.variable(2, z);
1954         try {
1955             factory.build(x + y - z, 1.0, 1.0);
1956             fail("an exception should have been thrown");
1957         } catch (MathIllegalArgumentException dme) {
1958             // expected
1959         } catch (Exception e) {
1960             fail("wrong exceptionc caught " + e.getClass().getName());
1961         }
1962         double[] derivatives = new double[] { x + y - z, 1.0, 1.0, -1.0 };
1963         FieldDerivativeStructure<T> t = factory.build(derivatives);
1964         checkEquals(xRef.add(yRef.subtract(zRef)), t, 1.0e-15);
1965         T[] all = xRef.add(yRef.subtract(zRef)).getAllDerivatives();
1966         assertEquals(derivatives.length, all.length);
1967         for (int i = 0; i < all.length; ++i) {
1968             assertEquals(derivatives[i], all[i].getReal(), 1.0e-15);
1969         }
1970     }
1971 
1972     @Test
1973     public void testLinearCombination1DSDS() {
1974         doTestLinearCombination1DSDS(1.0e-15);
1975     }
1976 
1977     protected void doTestLinearCombination1DSDS(final double tol) {
1978         final FDSFactory<T> factory = buildFactory(6, 1);
1979         final FieldDerivativeStructure<T>[] a = MathArrays.buildArray(factory.getDerivativeField(), 3);
1980         a[0] = factory.variable(0, -1321008684645961.0 / 268435456.0);
1981         a[1] = factory.variable(1, -5774608829631843.0 / 268435456.0);
1982         a[2] = factory.variable(2, -7645843051051357.0 / 8589934592.0);
1983         final FieldDerivativeStructure<T>[] b = MathArrays.buildArray(factory.getDerivativeField(), 3);
1984         b[0] = factory.variable(3, -5712344449280879.0 / 2097152.0);
1985         b[1] = factory.variable(4, -4550117129121957.0 / 2097152.0);
1986         b[2] = factory.variable(5, 8846951984510141.0 / 131072.0);
1987 
1988         final FieldDerivativeStructure<T> abSumInline = a[0].linearCombination(a[0], b[0], a[1], b[1], a[2], b[2]);
1989         final FieldDerivativeStructure<T> abSumArray = a[0].linearCombination(a, b);
1990 
1991         assertEquals(abSumInline.getReal(), abSumArray.getReal(), 0);
1992         assertEquals(-1.8551294182586248737720779899, abSumInline.getReal(), tol);
1993         assertEquals(b[0].getReal(), abSumInline.getPartialDerivative(1, 0, 0, 0, 0, 0).getReal(), 1.0e-15);
1994         assertEquals(b[1].getReal(), abSumInline.getPartialDerivative(0, 1, 0, 0, 0, 0).getReal(), 1.0e-15);
1995         assertEquals(b[2].getReal(), abSumInline.getPartialDerivative(0, 0, 1, 0, 0, 0).getReal(), 1.0e-15);
1996         assertEquals(a[0].getReal(), abSumInline.getPartialDerivative(0, 0, 0, 1, 0, 0).getReal(), 1.0e-15);
1997         assertEquals(a[1].getReal(), abSumInline.getPartialDerivative(0, 0, 0, 0, 1, 0).getReal(), 1.0e-15);
1998         assertEquals(a[2].getReal(), abSumInline.getPartialDerivative(0, 0, 0, 0, 0, 1).getReal(), 1.0e-15);
1999 
2000     }
2001 
2002     @Test
2003     public void testLinearCombination1FieldDS() {
2004         doTestLinearCombination1FieldDS(1.0e-15);
2005     }
2006 
2007     protected void doTestLinearCombination1FieldDS(final double tol) {
2008         final FDSFactory<T> factory = buildFactory(3, 1);
2009         final T[] a = MathArrays.buildArray(getField(), 3);
2010         a[0] = buildScalar(-1321008684645961.0 / 268435456.0);
2011         a[1] = buildScalar(-5774608829631843.0 / 268435456.0);
2012         a[2] = buildScalar(-7645843051051357.0 / 8589934592.0);
2013         final FieldDerivativeStructure<T>[] b = MathArrays.buildArray(factory.getDerivativeField(), 3);
2014         b[0] = factory.variable(0, -5712344449280879.0 / 2097152.0);
2015         b[1] = factory.variable(1, -4550117129121957.0 / 2097152.0);
2016         b[2] = factory.variable(2, 8846951984510141.0 / 131072.0);
2017 
2018         final FieldDerivativeStructure<T> abSumInline = b[0].linearCombination(a[0], b[0],
2019                                                                                a[1], b[1],
2020                                                                                a[2], b[2]);
2021         final FieldDerivativeStructure<T> abSumArray = b[0].linearCombination(a, b);
2022 
2023         assertEquals(abSumInline.getReal(), abSumArray.getReal(), 0);
2024         assertEquals(-1.8551294182586248737720779899, abSumInline.getReal(), tol);
2025         assertEquals(a[0].getReal(), abSumInline.getPartialDerivative(1, 0, 0).getReal(), 1.0e-15);
2026         assertEquals(a[1].getReal(), abSumInline.getPartialDerivative(0, 1, 0).getReal(), 1.0e-15);
2027         assertEquals(a[2].getReal(), abSumInline.getPartialDerivative(0, 0, 1).getReal(), 1.0e-15);
2028 
2029     }
2030 
2031     @Test
2032     public void testLinearCombination1DoubleDS() {
2033         doTestLinearCombination1DoubleDS(1.0e-15);
2034     }
2035 
2036     protected void doTestLinearCombination1DoubleDS(final double tol) {
2037         final FDSFactory<T> factory = buildFactory(3, 1);
2038         final double[] a = new double[] {
2039             -1321008684645961.0 / 268435456.0,
2040             -5774608829631843.0 / 268435456.0,
2041             -7645843051051357.0 / 8589934592.0
2042         };
2043         final FieldDerivativeStructure<T>[] b = MathArrays.buildArray(factory.getDerivativeField(), 3);
2044         b[0] = factory.variable(0, -5712344449280879.0 / 2097152.0);
2045         b[1] = factory.variable(1, -4550117129121957.0 / 2097152.0);
2046         b[2] = factory.variable(2, 8846951984510141.0 / 131072.0);
2047 
2048         final FieldDerivativeStructure<T> abSumInline = b[0].linearCombination(a[0], b[0],
2049                                                                        a[1], b[1],
2050                                                                        a[2], b[2]);
2051         final FieldDerivativeStructure<T> abSumArray = b[0].linearCombination(a, b);
2052 
2053         assertEquals(abSumInline.getReal(), abSumArray.getReal(), 0);
2054         assertEquals(-1.8551294182586248737720779899, abSumInline.getReal(), tol);
2055         assertEquals(a[0], abSumInline.getPartialDerivative(1, 0, 0).getReal(), 1.0e-15);
2056         assertEquals(a[1], abSumInline.getPartialDerivative(0, 1, 0).getReal(), 1.0e-15);
2057         assertEquals(a[2], abSumInline.getPartialDerivative(0, 0, 1).getReal(), 1.0e-15);
2058 
2059     }
2060 
2061     @Test
2062     public void testLinearCombination2DSDS() {
2063 
2064         final FDSFactory<T> factory = buildFactory(4, 1);
2065         final FieldDerivativeStructure<T>[] u = MathArrays.buildArray(factory.getDerivativeField(), 4);
2066         final FieldDerivativeStructure<T>[] v = MathArrays.buildArray(factory.getDerivativeField(), 4);
2067 
2068         // we compare accurate versus naive dot product implementations
2069         // on regular vectors (i.e. not extreme cases like in the previous test)
2070         Well1024a random = new Well1024a(0xc6af886975069f11l);
2071 
2072         for (int i = 0; i < 10000; ++i) {
2073             for (int j = 0; j < u.length; ++j) {
2074                 u[j] = factory.variable(j, 1e17 * random.nextDouble());
2075                 v[j] = factory.constant(1e17 * random.nextDouble());
2076             }
2077 
2078             FieldDerivativeStructure<T> lin = u[0].linearCombination(u[0], v[0], u[1], v[1]);
2079             double ref = u[0].getReal() * v[0].getReal() +
2080                          u[1].getReal() * v[1].getReal();
2081             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2082             assertEquals(v[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2083             assertEquals(v[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2084 
2085             lin = u[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2]);
2086             ref = u[0].getReal() * v[0].getReal() +
2087                   u[1].getReal() * v[1].getReal() +
2088                   u[2].getReal() * v[2].getReal();
2089             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2090             assertEquals(v[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2091             assertEquals(v[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2092             assertEquals(v[2].getReal(), lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2093 
2094             lin = u[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]);
2095             ref = u[0].getReal() * v[0].getReal() +
2096                   u[1].getReal() * v[1].getReal() +
2097                   u[2].getReal() * v[2].getReal() +
2098                   u[3].getReal() * v[3].getReal();
2099             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2100             assertEquals(v[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2101             assertEquals(v[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2102             assertEquals(v[2].getReal(), lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2103             assertEquals(v[3].getReal(), lin.getPartialDerivative(0, 0, 0, 1).getReal(), 1.0e-15 * FastMath.abs(v[3].getReal()));
2104 
2105         }
2106     }
2107 
2108     @Test
2109     public void testLinearCombination2DoubleDS() {
2110         final FDSFactory<T> factory = buildFactory(4, 1);
2111         final double[] u = new double[4];
2112         final FieldDerivativeStructure<T>[] v = MathArrays.buildArray(factory.getDerivativeField(), 4);
2113         // we compare accurate versus naive dot product implementations
2114         // on regular vectors (i.e. not extreme cases like in the previous test)
2115         Well1024a random = new Well1024a(0xc6af886975069f11l);
2116 
2117         for (int i = 0; i < 10000; ++i) {
2118             for (int j = 0; j < u.length; ++j) {
2119                 u[j] = 1e17 * random.nextDouble();
2120                 v[j] = factory.variable(j, 1e17 * random.nextDouble());
2121             }
2122 
2123             FieldDerivativeStructure<T> lin = v[0].linearCombination(u[0], v[0], u[1], v[1]);
2124             double ref = u[0] * v[0].getReal() +
2125                          u[1] * v[1].getReal();
2126             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2127             assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2128             assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2129 
2130             lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2]);
2131             ref = u[0] * v[0].getReal() +
2132                   u[1] * v[1].getReal() +
2133                   u[2] * v[2].getReal();
2134             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2135             assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2136             assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2137             assertEquals(u[2], lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2138 
2139             lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]);
2140             ref = u[0] * v[0].getReal() +
2141                   u[1] * v[1].getReal() +
2142                   u[2] * v[2].getReal() +
2143                   u[3] * v[3].getReal();
2144             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2145             assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2146             assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2147             assertEquals(u[2], lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2148             assertEquals(u[3], lin.getPartialDerivative(0, 0, 0, 1).getReal(), 1.0e-15 * FastMath.abs(v[3].getReal()));
2149 
2150         }
2151     }
2152 
2153     @Test
2154     public void testLinearCombination2FieldDS() {
2155         final FDSFactory<T> factory = buildFactory(4, 1);
2156         final T[] u = MathArrays.buildArray(getField(), 4);
2157         final FieldDerivativeStructure<T>[] v = MathArrays.buildArray(factory.getDerivativeField(), 4);
2158         // we compare accurate versus naive dot product implementations
2159         // on regular vectors (i.e. not extreme cases like in the previous test)
2160         Well1024a random = new Well1024a(0xc6af886975069f11l);
2161 
2162         for (int i = 0; i < 10000; ++i) {
2163             for (int j = 0; j < u.length; ++j) {
2164                 u[j] = buildScalar(1e17 * random.nextDouble());
2165                 v[j] = factory.variable(j, 1e17 * random.nextDouble());
2166             }
2167 
2168             FieldDerivativeStructure<T> lin = v[0].linearCombination(u[0], v[0], u[1], v[1]);
2169             double ref = u[0].getReal() * v[0].getReal() +
2170                          u[1].getReal() * v[1].getReal();
2171             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2172             assertEquals(u[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2173             assertEquals(u[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2174 
2175             lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2]);
2176             ref = u[0].getReal() * v[0].getReal() +
2177                   u[1].getReal() * v[1].getReal() +
2178                   u[2].getReal() * v[2].getReal();
2179             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2180             assertEquals(u[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2181             assertEquals(u[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2182             assertEquals(u[2].getReal(), lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2183 
2184             lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]);
2185             ref = u[0].getReal() * v[0].getReal() +
2186                   u[1].getReal() * v[1].getReal() +
2187                   u[2].getReal() * v[2].getReal() +
2188                   u[3].getReal() * v[3].getReal();
2189             assertEquals(ref, lin.getReal(), 1.0e-15 * FastMath.abs(ref));
2190             assertEquals(u[0].getReal(), lin.getPartialDerivative(1, 0, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[0].getReal()));
2191             assertEquals(u[1].getReal(), lin.getPartialDerivative(0, 1, 0, 0).getReal(), 1.0e-15 * FastMath.abs(v[1].getReal()));
2192             assertEquals(u[2].getReal(), lin.getPartialDerivative(0, 0, 1, 0).getReal(), 1.0e-15 * FastMath.abs(v[2].getReal()));
2193             assertEquals(u[3].getReal(), lin.getPartialDerivative(0, 0, 0, 1).getReal(), 1.0e-15 * FastMath.abs(v[3].getReal()));
2194 
2195         }
2196     }
2197 
2198     @Test
2199     public void testZero() {
2200         FDSFactory<T> factory = buildFactory(3, 2);
2201         FieldDerivativeStructure<T> zero = factory.constant(17).getField().getZero();
2202         T[] a = zero.getAllDerivatives();
2203         assertEquals(10, a.length);
2204         for (int i = 0; i < a.length; ++i) {
2205             assertEquals(buildScalar(0.0), a[i]);
2206         }
2207     }
2208 
2209     @Test
2210     public void testOne() {
2211         FDSFactory<T> factory = buildFactory(3, 2);
2212         FieldDerivativeStructure<T> one = factory.constant(17).getField().getOne();
2213         T[] a = one.getAllDerivatives();
2214         assertEquals(10, a.length);
2215         for (int i = 0; i < a.length; ++i) {
2216             assertEquals(i == 0 ? buildScalar(1.0) : buildScalar(0.0), a[i]);
2217         }
2218     }
2219 
2220     @Test
2221     public void testMap() {
2222         List<int[]> pairs = new ArrayList<>();
2223         for (int parameters = 1; parameters < 5; ++parameters) {
2224             for (int order = 0; order < 3; ++order) {
2225                 pairs.add(new int[] { parameters, order });
2226             }
2227         }
2228         Map<Field<?>, Integer> map = new HashMap<>();
2229         for (int i = 0; i < 1000; ++i) {
2230             // create a brand new factory for each derivative
2231             int parameters = pairs.get(i % pairs.size())[0];
2232             int order      = pairs.get(i % pairs.size())[1];
2233             FDSFactory<T> factory = buildFactory(parameters, order);
2234             map.put(factory.constant(buildScalar(17)).getField(), 0);
2235         }
2236 
2237         // despite we have created numerous factories,
2238         // there should be only one field for each pair parameters/order
2239         assertEquals(pairs.size(), map.size());
2240         @SuppressWarnings("unchecked")
2241         Field<FieldDerivativeStructure<T>> first = (Field<FieldDerivativeStructure<T>>) map.entrySet().iterator().next().getKey();
2242         assertEquals(first, first);
2243         assertNotEquals(first, getField());
2244 
2245         // even at same parameters and differentiation orders, different values generate different fields
2246         FieldDerivativeStructure<T> zero64 = buildFactory(3, 2).build();
2247         FieldDerivativeStructure<Dfp> zeroDFP = new FDSFactory<Dfp>(new DfpField(15), 3, 2).build();
2248         assertEquals(zero64.getFreeParameters(), zeroDFP.getFreeParameters());
2249         assertEquals(zero64.getOrder(), zeroDFP.getOrder());
2250         assertNotEquals(zero64.getField(), zeroDFP.getField());
2251     }
2252 
2253     @SuppressWarnings("unchecked")
2254     @Test
2255     public void testRebaseConditions() {
2256         final FDSFactory<T> f32 = buildFactory(3, 2);
2257         final FDSFactory<T> f22 = buildFactory(2, 2);
2258         final FDSFactory<T> f31 = buildFactory(3, 1);
2259         try {
2260             f32.variable(0, 0).rebase(f22.variable(0, 0), f22.variable(1, 1.0));
2261         } catch (MathIllegalArgumentException miae) {
2262             assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
2263             assertEquals(3, ((Integer) miae.getParts()[0]).intValue());
2264             assertEquals(2, ((Integer) miae.getParts()[1]).intValue());
2265         }
2266         try {
2267             f32.variable(0, 0).rebase(f31.variable(0, 0), f31.variable(1, 1.0), f31.variable(2, 2.0));
2268         } catch (MathIllegalArgumentException miae) {
2269             assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
2270             assertEquals(2, ((Integer) miae.getParts()[0]).intValue());
2271             assertEquals(1, ((Integer) miae.getParts()[1]).intValue());
2272         }
2273     }
2274 
2275     @SuppressWarnings("unchecked")
2276     @Test
2277     public void testRebaseNoVariables() {
2278         final FieldDerivativeStructure<T> x = buildFactory(0, 2).constant(1.0);
2279         assertSame(x, x.rebase());
2280     }
2281 
2282     @Test
2283     public void testRebaseValueMoreIntermediateThanBase() {
2284         doTestRebaseValue(createBaseVariables(buildFactory(2, 4), 1.5, -2.0),
2285                           q -> {
2286                               final FieldDerivativeStructure<T>[] a = MathArrays.buildArray(q[0].getFactory().getDerivativeField(), 3);
2287                               a[0] = q[0].add(q[1].multiply(3));
2288                               a[1] = q[0].log();
2289                               a[2] = q[1].divide(q[0].sin());
2290                               return a;
2291                           },
2292                           buildFactory(3, 4),
2293                           p -> p[0].add(p[1].divide(p[2])),
2294                           1.0e-15);
2295     }
2296 
2297     @Test
2298     public void testRebaseValueLessIntermediateThanBase() {
2299         doTestRebaseValue(createBaseVariables(buildFactory(3, 4), 1.5, -2.0, 0.5),
2300                           q -> {
2301                               final FieldDerivativeStructure<T>[] a = MathArrays.buildArray(q[0].getFactory().getDerivativeField(), 2);
2302                               a[0] = q[0].add(q[1].multiply(3));
2303                               a[1] = q[0].add(q[1]).subtract(q[2]);
2304                               return a;
2305                           },
2306                           buildFactory(2, 4),
2307                           p -> p[0].multiply(p[1]),
2308                           1.0e-15);
2309     }
2310 
2311     @Test
2312     public void testRebaseValueEqualIntermediateAndBase() {
2313         doTestRebaseValue(createBaseVariables(buildFactory(2, 4), 1.5, -2.0),
2314                           q -> {
2315                               final FieldDerivativeStructure<T>[] a = MathArrays.buildArray(q[0].getFactory().getDerivativeField(), 2);
2316                               a[0] = q[0].add(q[1].multiply(3));
2317                               a[1] = q[0].add(q[1]);
2318                               return a;
2319                           },
2320                           buildFactory(2, 4),
2321                           p -> p[0].multiply(p[1]),
2322                           1.0e-15);
2323     }
2324 
2325     private void doTestRebaseValue(final FieldDerivativeStructure<T>[] q,
2326                                    final CalculusFieldMultivariateVectorFunction<FieldDerivativeStructure<T>> qToP,
2327                                    final FDSFactory<T> factoryP,
2328                                    final CalculusFieldMultivariateFunction<FieldDerivativeStructure<T>> f,
2329                                    final double tol) {
2330 
2331         // intermediate variables as functions of base variables
2332         final FieldDerivativeStructure<T>[] pBase = qToP.value(q);
2333         
2334         // reference function
2335         final FieldDerivativeStructure<T> ref = f.value(pBase);
2336 
2337         // intermediate variables as independent variables
2338         final FieldDerivativeStructure<T>[] pIntermediate = createIntermediateVariables(factoryP, pBase);
2339 
2340         // function of the intermediate variables
2341         final FieldDerivativeStructure<T> fI = f.value(pIntermediate);
2342 
2343         // function rebased to base variables
2344         final FieldDerivativeStructure<T> rebased = fI.rebase(pBase);
2345 
2346         assertEquals(q[0].getFreeParameters(),                   ref.getFreeParameters());
2347         assertEquals(q[0].getOrder(),                            ref.getOrder());
2348         assertEquals(factoryP.getCompiler().getFreeParameters(), fI.getFreeParameters());
2349         assertEquals(factoryP.getCompiler().getOrder(),          fI.getOrder());
2350         assertEquals(ref.getFreeParameters(),                    rebased.getFreeParameters());
2351         assertEquals(ref.getOrder(),                             rebased.getOrder());
2352 
2353         checkEquals(ref, rebased, tol);
2354 
2355     }
2356 
2357     final FieldDerivativeStructure<T>[] createBaseVariables(final FDSFactory<T> factory, double... q) {
2358         final FieldDerivativeStructure<T>[] qDS = MathArrays.buildArray(factory.getDerivativeField(), q.length);
2359         for (int i = 0; i < q.length; ++i) {
2360             qDS[i] = factory.variable(i, q[i]);
2361         }
2362         return qDS;
2363     }
2364 
2365     @SuppressWarnings("unchecked")
2366     final FieldDerivativeStructure<T>[] createIntermediateVariables(final FDSFactory<T> factory,
2367                                                                     final FieldDerivativeStructure<T>... pBase) {
2368         final FieldDerivativeStructure<T>[] pIntermediate = MathArrays.buildArray(factory.getDerivativeField(), pBase.length);
2369         for (int i = 0; i < pBase.length; ++i) {
2370             pIntermediate[i] = factory.variable(i, pBase[i].getValue());
2371         }
2372         return pIntermediate;
2373     }
2374 
2375     @Test
2376     public void testRunTimeClass() {
2377         FDSFactory<T> factory = buildFactory(3, 2);
2378         Field<FieldDerivativeStructure<T>> field = factory.getDerivativeField();
2379         assertEquals(FieldDerivativeStructure.class, field.getRuntimeClass());
2380         assertEquals(getField(), factory.getValueField());
2381         assertEquals("org.hipparchus.analysis.differentiation.FDSFactory$DerivativeField",
2382                             factory.getDerivativeField().getClass().getName());
2383     }
2384 
2385     private void checkF0F1(FieldDerivativeStructure<T> ds, double value, double...derivatives) {
2386 
2387         // check dimension
2388         assertEquals(derivatives.length, ds.getFreeParameters());
2389 
2390         // check value, directly and also as 0th order derivative
2391         assertEquals(value, ds.getReal(), 1.0e-15);
2392         assertEquals(value, ds.getPartialDerivative(new int[ds.getFreeParameters()]).getReal(), 1.0e-15);
2393 
2394         // check first order derivatives
2395         for (int i = 0; i < derivatives.length; ++i) {
2396             int[] orders = new int[derivatives.length];
2397             orders[i] = 1;
2398             assertEquals(derivatives[i], ds.getPartialDerivative(orders).getReal(), 1.0e-15);
2399         }
2400 
2401     }
2402 
2403     public static <T extends CalculusFieldElement<T>> void checkEquals(FieldDerivativeStructure<T> ds1,
2404                                                                        FieldDerivativeStructure<T> ds2,
2405                                                                        double epsilon) {
2406 
2407         // check dimension
2408         assertEquals(ds1.getFreeParameters(), ds2.getFreeParameters());
2409         assertEquals(ds1.getOrder(), ds2.getOrder());
2410 
2411         int[] derivatives = new int[ds1.getFreeParameters()];
2412         int sum = 0;
2413         while (true) {
2414 
2415             if (sum <= ds1.getOrder()) {
2416                 assertEquals(ds1.getPartialDerivative(derivatives).getReal(),
2417                                     ds2.getPartialDerivative(derivatives).getReal(),
2418                                     epsilon);
2419             }
2420 
2421             boolean increment = true;
2422             sum = 0;
2423             for (int i = derivatives.length - 1; i >= 0; --i) {
2424                 if (increment) {
2425                     if (derivatives[i] == ds1.getOrder()) {
2426                         derivatives[i] = 0;
2427                     } else {
2428                         derivatives[i]++;
2429                         increment = false;
2430                     }
2431                 }
2432                 sum += derivatives[i];
2433             }
2434             if (increment) {
2435                 return;
2436             }
2437 
2438         }
2439 
2440     }
2441 
2442 }