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  package org.hipparchus.analysis.differentiation;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.CalculusFieldElementAbstractTest;
21  import org.hipparchus.UnitTestUtils;
22  import org.hipparchus.analysis.FieldUnivariateFunction;
23  import org.hipparchus.exception.LocalizedCoreFormats;
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.util.FastMath;
26  import org.hipparchus.util.FieldSinCos;
27  import org.hipparchus.util.MathArrays;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertNotSame;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  import static org.junit.jupiter.api.Assertions.fail;
34  
35  /**
36   * Test for class {@link UnivariateDerivative}.
37   */
38  public abstract class UnivariateDerivativeAbstractTest<T extends UnivariateDerivative<T>>
39      extends CalculusFieldElementAbstractTest<T> {
40  
41      protected abstract int getMaxOrder();
42  
43      @Override
44      protected abstract T build(final double x);
45  
46      @Test
47      public void testOrder() {
48          assertEquals(getMaxOrder(), build(0).getOrder());
49      }
50  
51      @Test
52      public void testNewInstance() {
53          T ud = build(5.25);
54          assertEquals(5.25, ud.getValue(), 1.0e-15);
55          assertEquals(1.0,  ud.getDerivative(1), 1.0e-15);
56          T newInstance = ud.newInstance(7.5);
57          assertEquals(7.5, newInstance.getValue(), 1.0e-15);
58          assertEquals(0.0, newInstance.getDerivative(1), 1.0e-15);
59      }
60  
61      @Test
62      public void testGetPartialDerivative() {
63          try {
64              build(3.0).getPartialDerivative(0, 1);
65              fail("an exception should have been thrown");
66          } catch( MathIllegalArgumentException miae) {
67              assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
68              assertEquals(2, ((Integer) miae.getParts()[0]).intValue());
69              assertEquals(1, ((Integer) miae.getParts()[1]).intValue());
70          }
71      }
72  
73      @Test
74      public void testNorm() {
75          final T x = build(3.0);
76          assertTrue(x.norm() >= FastMath.abs(x.getValue()));
77      }
78  
79      @Test
80      public void testGetDerivative() {
81          T x  = build(3.0);
82          T ud = x.square();
83          try {
84              ud.getDerivative(-1);
85              fail("an exception should have been thrown");
86          } catch (MathIllegalArgumentException miae) {
87              assertEquals(LocalizedCoreFormats.DERIVATION_ORDER_NOT_ALLOWED, miae.getSpecifier());
88          }
89          assertEquals(9.0, ud.getValue(), 1.0e-15);
90          assertEquals(9.0, ud.getDerivative(0), 1.0e-15);
91          assertEquals(6.0, ud.getDerivative(1), 1.0e-15);
92          for (int n = 2; n <= getMaxOrder(); ++n) {
93              assertEquals(n == 2 ? 2.0 : 0.0, ud.getDerivative(n), 1.0e-15);
94          }
95          try {
96              ud.getDerivative(getMaxOrder() + 1);
97              fail("an exception should have been thrown");
98          } catch (MathIllegalArgumentException miae) {
99              assertEquals(LocalizedCoreFormats.DERIVATION_ORDER_NOT_ALLOWED, miae.getSpecifier());
100         }
101     }
102 
103     @Test
104     public void testGetFreeParameters() {
105         assertEquals(1, build(3.0).getFreeParameters());
106     }
107 
108     protected void checkAgainstDS(final double x, final FieldUnivariateFunction f) {
109         final T xUD = build(x);
110         final T yUD = f.value(xUD);
111         final DerivativeStructure yDS = f.value(xUD.toDerivativeStructure());
112         for (int i = 0; i <= yUD.getOrder(); ++i) {
113             if  (Double.isNaN(yDS.getPartialDerivative(i))) {
114                 assertEquals(yDS.getPartialDerivative(i),
115                                         yUD.getDerivative(i));
116             }else {
117                 assertEquals(yDS.getPartialDerivative(i),
118                                         yUD.getDerivative(i),
119                                         4.0e-14* FastMath.abs(yDS.getPartialDerivative(i)));
120             }
121         }
122     }
123 
124     @Test
125     public void testArithmeticVsDS() {
126         for (double x = -1.25; x < 1.25; x+= 0.5) {
127             checkAgainstDS(x,
128                            new FieldUnivariateFunction() {
129                                public <S extends CalculusFieldElement<S>> S value(S x) {
130                                    final S y = x.add(3).multiply(x).subtract(5).multiply(0.5);
131                                    return y.negate().divide(4).divide(x).add(y).subtract(x).twice().reciprocal();
132                                }
133                            });
134         }
135     }
136 
137     @Test
138     public void testRemainderDoubleVsDS() {
139         for (double x = -1.25; x < 1.25; x+= 0.5) {
140             checkAgainstDS(x,
141                            new FieldUnivariateFunction() {
142                                public <S extends CalculusFieldElement<S>> S value(S x) {
143                                    return x.remainder(0.5);
144                                }
145                            });
146         }
147     }
148 
149     @Test
150     public void testRemainderUdVsDS() {
151         for (double x = -1.25; x < 1.25; x+= 0.5) {
152             checkAgainstDS(x,
153                            new FieldUnivariateFunction() {
154                               public <S extends CalculusFieldElement<S>> S value(S x) {
155                                   return x.remainder(x.divide(0.7));
156                               }
157                            });
158         }
159     }
160 
161     @Test
162     public void testAbsVsDS() {
163         for (double x = -1.25; x < 1.25; x+= 0.5) {
164             checkAgainstDS(x,
165                            new FieldUnivariateFunction() {
166                                public <S extends CalculusFieldElement<S>> S value(S x) {
167                                    return x.abs();
168                                }
169                            });
170         }
171     }
172 
173     @Test
174     public void testScalbVsDS() {
175         for (int n = -4; n < 4; ++n) {
176             final int theN = n;
177             for (double x = -1.25; x < 1.25; x+= 0.5) {
178                 checkAgainstDS(x,
179                                new FieldUnivariateFunction() {
180                                    public <S extends CalculusFieldElement<S>> S value(S x) {
181                                        return x.scalb(theN);
182                                    }
183                                });
184             }
185         }
186     }
187 
188     @Test
189     public void testUlpVsDS() {
190         for (double x = -1.25; x < 1.25; x+= 0.0001) {
191             checkAgainstDS(x,
192                            new FieldUnivariateFunction() {
193                                public <S extends CalculusFieldElement<S>> S value(S x) {
194                                    return x.ulp();
195                            }
196             });
197         }
198     }
199 
200     @Test
201     public void testHypotVsDS() {
202         for (double x = -3.25; x < 3.25; x+= 0.5) {
203             checkAgainstDS(x,
204                            new FieldUnivariateFunction() {
205                                public <S extends CalculusFieldElement<S>> S value(S x) {
206                                    return x.cos().multiply(5).hypot(x.sin().twice());
207                                }
208                            });
209         }
210     }
211 
212     @Test
213     public void testAtan2VsDS() {
214         for (double x = -3.25; x < 3.25; x+= 0.5) {
215             checkAgainstDS(x,
216                            new FieldUnivariateFunction() {
217                                public <S extends CalculusFieldElement<S>> S value(S x) {
218                                    return x.cos().multiply(5).atan2(x.sin().twice());
219                                }
220                            });
221         }
222     }
223 
224     @Test
225     public void testPowersVsDS() {
226         for (double x = -3.25; x < 3.25; x+= 0.5) {
227             checkAgainstDS(x,
228                            new FieldUnivariateFunction() {
229                                public <S extends CalculusFieldElement<S>> S value(S x) {
230                                    final FieldSinCos<S> sc = x.sinCos();
231                                    return x.pow(3.2).add(x.pow(2)).subtract(sc.cos().abs().pow(sc.sin()));
232                                }
233                            });
234         }
235     }
236 
237     @Test
238     public void testSqrtVsDS() {
239         for (double x = 0.001; x < 3.25; x+= 0.5) {
240             checkAgainstDS(x,
241                     new FieldUnivariateFunction() {
242                         public <S extends CalculusFieldElement<S>> S value(S x) {
243                             return x.sqrt();
244                         }
245                     });
246         }
247     }
248 
249     @Test
250     public void testCbrtVsDS() {
251         for (double x = 0.001; x < 3.25; x+= 0.5) {
252             checkAgainstDS(x,
253                     new FieldUnivariateFunction() {
254                         public <S extends CalculusFieldElement<S>> S value(S x) {
255                             return x.cbrt();
256                         }
257                     });
258         }
259     }
260 
261     @Test
262     public void testRootsVsDS() {
263         for (double x = 0.001; x < 3.25; x+= 0.5) {
264             checkAgainstDS(x,
265                            new FieldUnivariateFunction() {
266                                public <S extends CalculusFieldElement<S>> S value(S x) {
267                                    return x.rootN(5);//x.sqrt().add(x.cbrt()).subtract(x.rootN(5));
268                                }
269                            });
270         }
271     }
272 
273     @Test
274     public void testExpsLogsVsDS() {
275         for (double x = 2.5; x < 3.25; x+= 0.125) {
276             checkAgainstDS(x,
277                            new FieldUnivariateFunction() {
278                                public <S extends CalculusFieldElement<S>> S value(S x) {
279                                    return x.exp().add(x.multiply(0.5).expm1()).log().log10().log1p();
280                                }
281                            });
282         }
283     }
284 
285     @Test
286     public void testTrigonometryVsDS() {
287         for (double x = -3.25; x < 3.25; x+= 0.5) {
288             checkAgainstDS(x,
289                            new FieldUnivariateFunction() {
290                                public <S extends CalculusFieldElement<S>> S value(S x) {
291                                    return x.cos().multiply(x.sin()).atan().divide(12).asin().multiply(0.1).acos().tan();
292                                }
293                            });
294         }
295     }
296 
297     @Test
298     public void testHyperbolicVsDS() {
299         for (double x = -1.25; x < 1.25; x+= 0.5) {
300             checkAgainstDS(x,
301                            new FieldUnivariateFunction() {
302                                public <S extends CalculusFieldElement<S>> S value(S x) {
303                                    return x.cosh().multiply(x.sinh()).multiply(12).abs().acosh().asinh().divide(7).tanh().multiply(0.1).atanh();
304                                }
305                            });
306         }
307     }
308 
309     @Test
310     public void testConvertersVsDS() {
311         for (double x = -1.25; x < 1.25; x+= 0.5) {
312             checkAgainstDS(x,
313                            new FieldUnivariateFunction() {
314                                public <S extends CalculusFieldElement<S>> S value(S x) {
315                                    return x.multiply(5).toDegrees().subtract(x).toRadians();
316                                }
317                            });
318         }
319     }
320 
321     @Test
322     public void testLinearCombination2D2FVsDS() {
323         for (double x = -1.25; x < 1.25; x+= 0.5) {
324             checkAgainstDS(x,
325                            new FieldUnivariateFunction() {
326                                public <S extends CalculusFieldElement<S>> S value(S x) {
327                                    return x.linearCombination(1.0, x.multiply(0.9),
328                                                               2.0, x.multiply(0.8));
329                                }
330                            });
331         }
332     }
333 
334     @Test
335     public void testLinearCombination2F2FVsDS() {
336         for (double x = -1.25; x < 1.25; x+= 0.5) {
337             checkAgainstDS(x,
338                            new FieldUnivariateFunction() {
339                                public <S extends CalculusFieldElement<S>> S value(S x) {
340                                    return x.linearCombination(x.add(1), x.multiply(0.9),
341                                                               x.add(2), x.multiply(0.8));
342                                }
343                            });
344         }
345     }
346 
347     @Test
348     public void testLinearCombination3D3FVsDS() {
349         for (double x = -1.25; x < 1.25; x+= 0.5) {
350             checkAgainstDS(x,
351                            new FieldUnivariateFunction() {
352                                public <S extends CalculusFieldElement<S>> S value(S x) {
353                                    return x.linearCombination(1.0, x.multiply(0.9),
354                                                               2.0, x.multiply(0.8),
355                                                               3.0, x.multiply(0.7));
356                                }
357                            });
358         }
359     }
360 
361     @Test
362     public void testLinearCombination3F3FVsDS() {
363         for (double x = -1.25; x < 1.25; x+= 0.5) {
364             checkAgainstDS(x,
365                            new FieldUnivariateFunction() {
366                                public <S extends CalculusFieldElement<S>> S value(S x) {
367                                    return x.linearCombination(x.add(1), x.multiply(0.9),
368                                                               x.add(2), x.multiply(0.8),
369                                                               x.add(3), x.multiply(0.7));
370                                }
371                            });
372         }
373     }
374 
375     @Test
376     public void testLinearCombination4D4FVsDS() {
377         for (double x = -1.25; x < 1.25; x+= 0.5) {
378             checkAgainstDS(x,
379                            new FieldUnivariateFunction() {
380                                public <S extends CalculusFieldElement<S>> S value(S x) {
381                                    return x.linearCombination(1.0, x.multiply(0.9),
382                                                               2.0, x.multiply(0.8),
383                                                               3.0, x.multiply(0.7),
384                                                               4.0, x.multiply(0.6));
385                                }
386                            });
387         }
388     }
389 
390     @Test
391     public void testLinearCombination4F4FVsDS() {
392         for (double x = -1.25; x < 1.25; x+= 0.5) {
393             checkAgainstDS(x,
394                            new FieldUnivariateFunction() {
395                                public <S extends CalculusFieldElement<S>> S value(S x) {
396                                    return x.linearCombination(x.add(1), x.multiply(0.9),
397                                                               x.add(2), x.multiply(0.8),
398                                                               x.add(3), x.multiply(0.7),
399                                                               x.add(4), x.multiply(0.6));
400                                }
401                            });
402         }
403     }
404 
405     @Test
406     public void testLinearCombinationnDnFVsDS() {
407         for (double x = -1.25; x < 1.25; x+= 0.5) {
408             checkAgainstDS(x,
409                            new FieldUnivariateFunction() {
410                                public <S extends CalculusFieldElement<S>> S value(S x) {
411                                    final S[] b = MathArrays.buildArray(x.getField(), 4);
412                                    b[0] = x.add(0.9);
413                                    b[1] = x.add(0.8);
414                                    b[2] = x.add(0.7);
415                                    b[3] = x.add(0.6);
416                                    return x.linearCombination(new double[] { 1, 2, 3, 4 }, b);
417                                }
418                            });
419         }
420     }
421 
422     @Test
423     public void testLinearCombinationnFnFVsDS() {
424         for (double x = -1.25; x < 1.25; x+= 0.5) {
425             checkAgainstDS(x,
426                            new FieldUnivariateFunction() {
427                                public <S extends CalculusFieldElement<S>> S value(S x) {
428                                    final S[] a = MathArrays.buildArray(x.getField(), 4);
429                                    a[0] = x.add(1);
430                                    a[1] = x.add(2);
431                                    a[2] = x.add(3);
432                                    a[3] = x.add(4);
433                                    final S[] b = MathArrays.buildArray(x.getField(), 4);
434                                    b[0] = x.add(0.9);
435                                    b[1] = x.add(0.8);
436                                    b[2] = x.add(0.7);
437                                    b[3] = x.add(0.6);
438                                    return x.linearCombination(a, b);
439                                }
440                            });
441         }
442     }
443 
444     @Test
445     public void testSerialization() {
446         T a = build(1.3);
447         @SuppressWarnings("unchecked")
448         T b = (T) UnitTestUtils.serializeAndRecover(a);
449         assertEquals(a, b);
450         assertNotSame(a, b);
451     }
452 
453     @Test
454     public void testZero() {
455         T zero = build(17.0).getField().getZero();
456         for (int i = 0; i <= zero.getOrder(); ++i) {
457             assertEquals(0.0, zero.getDerivative(i), 1.0e-15);
458         }
459     }
460 
461     @Test
462     public void testOne() {
463         T one = build(17.0).getField().getOne();
464         for (int i = 0; i <= one.getOrder(); ++i) {
465             assertEquals(i == 0 ? 1.0 : 0.0, one.getDerivative(i), 1.0e-15);
466         }
467     }
468 
469 }