1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.differentiation;
18
19 import org.hipparchus.CalculusFieldElement;
20 import org.hipparchus.CalculusFieldElementAbstractTest;
21 import org.hipparchus.Field;
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.assertNotEquals;
32 import static org.junit.jupiter.api.Assertions.assertNotSame;
33 import static org.junit.jupiter.api.Assertions.assertSame;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35 import static org.junit.jupiter.api.Assertions.fail;
36
37
38
39
40 public abstract class FieldUnivariateDerivative1AbstractTest<T extends CalculusFieldElement<T>>
41 extends CalculusFieldElementAbstractTest<FieldUnivariateDerivative1<T>> {
42
43 protected abstract Field<T> getValueField();
44
45 protected FieldUnivariateDerivative1<T> build(double value) {
46 final Field<T> valueField = getValueField();
47 return new FieldUnivariateDerivative1<>(valueField.getZero().newInstance(value),
48 valueField.getOne());
49 }
50
51 protected T buildScalar(double value) {
52 return getValueField().getZero().newInstance(value);
53 }
54
55 private int getMaxOrder() {
56 return 1;
57 }
58
59 protected FieldUnivariateDerivative1<T> build(final double f0, final double f1) {
60 T prototype = build(0.0).getValue();
61 return new FieldUnivariateDerivative1<>(prototype.newInstance(f0),
62 prototype.newInstance(f1));
63 }
64
65 @Test
66 public void testNorm() {
67 T prototype = build(2.0).getValue();
68 assertTrue(prototype.norm() >= FastMath.abs(prototype.getReal()));
69 }
70
71 @Test
72 public void testOrder() {
73 assertEquals(getMaxOrder(), build(0).getOrder());
74 }
75
76 @Test
77 public void testNewInstance() {
78 FieldUnivariateDerivative1<T> ud = build(5.25);
79 assertEquals(5.25, ud.getValue().getReal(), 1.0e-15);
80 assertEquals(1.0, ud.getDerivative(1).getReal(), 1.0e-15);
81 FieldUnivariateDerivative1<T> newInstance = ud.newInstance(7.5);
82 assertEquals(7.5, newInstance.getValue().getReal(), 1.0e-15);
83 assertEquals(0.0, newInstance.getDerivative(1).getReal(), 1.0e-15);
84 }
85
86 @Test
87 public void testGetPartialDerivative() {
88 try {
89 build(3.0).getPartialDerivative(0, 1);
90 fail("an exception should have been thrown");
91 } catch( MathIllegalArgumentException miae) {
92 assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
93 assertEquals(2, ((Integer) miae.getParts()[0]).intValue());
94 assertEquals(1, ((Integer) miae.getParts()[1]).intValue());
95 }
96 }
97
98 @Test
99 public void testGetDerivative() {
100 FieldUnivariateDerivative1<T> x = build(3.0);
101 FieldUnivariateDerivative1<T> ud = x.square();
102 try {
103 ud.getDerivative(-1);
104 fail("an exception should have been thrown");
105 } catch (MathIllegalArgumentException miae) {
106 assertEquals(LocalizedCoreFormats.DERIVATION_ORDER_NOT_ALLOWED, miae.getSpecifier());
107 }
108 assertEquals(9.0, ud.getValue().getReal(), 1.0e-15);
109 assertEquals(9.0, ud.getDerivative(0).getReal(), 1.0e-15);
110 assertEquals(6.0, ud.getDerivative(1).getReal(), 1.0e-15);
111 for (int n = 2; n <= getMaxOrder(); ++n) {
112 assertEquals(n == 2 ? 2.0 : 0.0, ud.getDerivative(n).getReal(), 1.0e-15);
113 }
114 try {
115 ud.getDerivative(getMaxOrder() + 1);
116 fail("an exception should have been thrown");
117 } catch (MathIllegalArgumentException miae) {
118 assertEquals(LocalizedCoreFormats.DERIVATION_ORDER_NOT_ALLOWED, miae.getSpecifier());
119 }
120 }
121
122 @Test
123 public void testGetFreeParameters() {
124 assertEquals(1, build(3.0).getFreeParameters());
125 }
126
127 protected void checkAgainstDS(final double x,
128 final FieldUnivariateFunction f) {
129 final FieldUnivariateDerivative1<T> xUD = build(x);
130 final FieldUnivariateDerivative1<T> yUD = f.value(xUD);
131 final FieldDerivativeStructure<T> yDS = f.value(
132 xUD.toDerivativeStructure());
133 for (int i = 0; i <= yUD.getOrder(); ++i) {
134 if (Double.isNaN(yDS.getPartialDerivative(i).getReal())) {
135 assertEquals(yDS.getPartialDerivative(i).getReal(),
136 yUD.getDerivative(i).getReal());
137 } else {
138 assertEquals(yDS.getPartialDerivative(i).getReal(),
139 yUD.getDerivative(i).getReal(),
140 4.0e-14 * FastMath.abs(yDS.getPartialDerivative(i).getReal()));
141 }
142 }
143 }
144
145 @Test
146 public void testFieldAdd() {
147 check(build(1.0, 2.0).add(buildScalar(5.0)), 6.0, 2.0);
148 }
149
150 @Test
151 public void testFieldSubtract() {
152 check(build(1.0, 2.0).subtract(buildScalar(5.0)), -4.0, 2.0);
153 }
154
155 @Test
156 public void testFieldMultiply() {
157 check(build(1.0, 2.0).multiply(buildScalar(5.0)), 5.0, 10.0);
158 }
159
160 @Test
161 public void testFieldDivide() {
162 check(build(1.0, 5.0).divide(buildScalar(5.0)), 0.2, 1.0);
163 }
164
165 @Test
166 public void testCopySignField() {
167
168 FieldUnivariateDerivative1<T> minusOne = build(-1.0);
169 assertEquals(+1.0, minusOne.copySign(buildScalar(+1.0)).getReal(), 1.0e-15);
170 assertEquals(-1.0, minusOne.copySign(buildScalar(-1.0)).getReal(), 1.0e-15);
171 assertEquals(+1.0, minusOne.copySign(buildScalar(+0.0)).getReal(), 1.0e-15);
172 assertEquals(-1.0, minusOne.copySign(buildScalar(-0.0)).getReal(), 1.0e-15);
173 assertEquals(+1.0, minusOne.copySign(buildScalar(Double.NaN)).getReal(), 1.0e-15);
174
175 FieldUnivariateDerivative1<T> plusOne = build(1.0);
176 assertEquals(+1.0, plusOne.copySign(buildScalar(+1.0)).getReal(), 1.0e-15);
177 assertEquals(-1.0, plusOne.copySign(buildScalar(-1.0)).getReal(), 1.0e-15);
178 assertEquals(+1.0, plusOne.copySign(buildScalar(+0.0)).getReal(), 1.0e-15);
179 assertEquals(-1.0, plusOne.copySign(buildScalar(-0.0)).getReal(), 1.0e-15);
180 assertEquals(+1.0, plusOne.copySign(buildScalar(Double.NaN)).getReal(), 1.0e-15);
181
182 }
183
184 @Test
185 public void testRemainderField() {
186 double epsilon = 2.0e-15;
187 for (double x = -1.7; x < 2; x += 0.2) {
188 FieldUnivariateDerivative1<T> dsX = build(x);
189 for (double y = -1.7; y < 2; y += 0.2) {
190 FieldUnivariateDerivative1<T> remainder = dsX.remainder(buildScalar(y));
191 FieldUnivariateDerivative1<T> ref = dsX.subtract(x - FastMath.IEEEremainder(x, y));
192 FieldUnivariateDerivative1<T> zero = remainder.subtract(ref);
193 assertEquals(0, zero.getFirstDerivative().getReal(), epsilon);
194 }
195 }
196 }
197
198 @Test
199 public void testArithmeticVsDS() {
200 for (double x = -1.25; x < 1.25; x+= 0.5) {
201 checkAgainstDS(x,
202 new FieldUnivariateFunction() {
203 public <S extends CalculusFieldElement<S>> S value(S x) {
204 final S y = x.add(3).multiply(x).subtract(5).multiply(0.5);
205 return y.negate().divide(4).divide(x).add(y).subtract(x).twice().reciprocal();
206 }
207 });
208 }
209 }
210
211 @Test
212 public void testRemainderDoubleVsDS() {
213 for (double x = -1.25; x < 1.25; x+= 0.5) {
214 checkAgainstDS(x,
215 new FieldUnivariateFunction() {
216 public <S extends CalculusFieldElement<S>> S value(S x) {
217 return x.remainder(0.5);
218 }
219 });
220 }
221 }
222
223 @Test
224 public void testRemainderUdVsDS() {
225 for (double x = -1.25; x < 1.25; x+= 0.5) {
226 checkAgainstDS(x,
227 new FieldUnivariateFunction() {
228 public <S extends CalculusFieldElement<S>> S value(S x) {
229 return x.remainder(x.divide(0.7));
230 }
231 });
232 }
233 }
234
235 @Test
236 public void testAbsVsDS() {
237 for (double x = -1.25; x < 1.25; x+= 0.5) {
238 checkAgainstDS(x,
239 new FieldUnivariateFunction() {
240 public <S extends CalculusFieldElement<S>> S value(S x) {
241 return x.abs();
242 }
243 });
244 }
245 }
246
247 @Test
248 public void testScalbVsDS() {
249 for (int n = -4; n < 4; ++n) {
250 final int theN = n;
251 for (double x = -1.25; x < 1.25; x+= 0.5) {
252 checkAgainstDS(x,
253 new FieldUnivariateFunction() {
254 public <S extends CalculusFieldElement<S>> S value(S x) {
255 return x.scalb(theN);
256 }
257 });
258 }
259 }
260 }
261
262 @Test
263 public void testUlpVsDS() {
264 for (double x = -1.25; x < 1.25; x+= 0.0001) {
265 checkAgainstDS(x,
266 new FieldUnivariateFunction() {
267 public <S extends CalculusFieldElement<S>> S value(S x) {
268 return x.ulp();
269 }
270 });
271 }
272 }
273
274 @Test
275 public void testHypotVsDS() {
276 for (double x = -3.25; x < 3.25; x+= 0.5) {
277 checkAgainstDS(x,
278 new FieldUnivariateFunction() {
279 public <S extends CalculusFieldElement<S>> S value(S x) {
280 return x.cos().multiply(5).hypot(x.sin().twice());
281 }
282 });
283 }
284 }
285
286 @Test
287 public void testAtan2VsDS() {
288 for (double x = -3.25; x < 3.25; x+= 0.5) {
289 checkAgainstDS(x,
290 new FieldUnivariateFunction() {
291 public <S extends CalculusFieldElement<S>> S value(S x) {
292 return x.cos().multiply(5).atan2(x.sin().twice());
293 }
294 });
295 }
296 }
297
298 @Test
299 public void testPowersVsDS() {
300 for (double x = -3.25; x < 3.25; x+= 0.5) {
301 checkAgainstDS(x,
302 new FieldUnivariateFunction() {
303 public <S extends CalculusFieldElement<S>> S value(S x) {
304 final FieldSinCos<S> sc = x.sinCos();
305 return x.pow(3.2).add(x.pow(2)).subtract(sc.cos().abs().pow(sc.sin()));
306 }
307 });
308 }
309 }
310
311 @Test
312 public void testRootsVsDS() {
313 for (double x = 0.001; x < 3.25; x+= 0.5) {
314 checkAgainstDS(x,
315 new FieldUnivariateFunction() {
316 public <S extends CalculusFieldElement<S>> S value(S x) {
317 return x.rootN(5);
318 }
319 });
320 }
321 }
322
323 @Test
324 public void testExpsLogsVsDS() {
325 for (double x = 2.5; x < 3.25; x+= 0.125) {
326 checkAgainstDS(x,
327 new FieldUnivariateFunction() {
328 public <S extends CalculusFieldElement<S>> S value(S x) {
329 return x.exp().add(x.multiply(0.5).expm1()).log().log10().log1p();
330 }
331 });
332 }
333 }
334
335 @Test
336 public void testTrigonometryVsDS() {
337 for (double x = -3.25; x < 3.25; x+= 0.5) {
338 checkAgainstDS(x,
339 new FieldUnivariateFunction() {
340 public <S extends CalculusFieldElement<S>> S value(S x) {
341 return x.cos().multiply(x.sin()).atan().divide(12).asin().multiply(0.1).acos().tan();
342 }
343 });
344 }
345 }
346
347 @Test
348 public void testHyperbolicVsDS() {
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.cosh().multiply(x.sinh()).multiply(12).abs().acosh().asinh().divide(7).tanh().multiply(0.1).atanh();
354 }
355 });
356 }
357 }
358
359 @Test
360 public void testConvertersVsDS() {
361 for (double x = -1.25; x < 1.25; x+= 0.5) {
362 checkAgainstDS(x,
363 new FieldUnivariateFunction() {
364 public <S extends CalculusFieldElement<S>> S value(S x) {
365 return x.multiply(5).toDegrees().subtract(x).toRadians();
366 }
367 });
368 }
369 }
370
371 @Test
372 public void testLinearCombination2D2FVsDS() {
373 for (double x = -1.25; x < 1.25; x+= 0.5) {
374 checkAgainstDS(x,
375 new FieldUnivariateFunction() {
376 public <S extends CalculusFieldElement<S>> S value(S x) {
377 return x.linearCombination(1.0, x.multiply(0.9),
378 2.0, x.multiply(0.8));
379 }
380 });
381 }
382 }
383
384 @Test
385 public void testLinearCombination2F2FVsDS() {
386 for (double x = -1.25; x < 1.25; x+= 0.5) {
387 checkAgainstDS(x,
388 new FieldUnivariateFunction() {
389 public <S extends CalculusFieldElement<S>> S value(S x) {
390 return x.linearCombination(x.add(1), x.multiply(0.9),
391 x.add(2), x.multiply(0.8));
392 }
393 });
394 }
395 }
396
397 @Test
398 public void testLinearCombination3D3FVsDS() {
399 for (double x = -1.25; x < 1.25; x+= 0.5) {
400 checkAgainstDS(x,
401 new FieldUnivariateFunction() {
402 public <S extends CalculusFieldElement<S>> S value(S x) {
403 return x.linearCombination(1.0, x.multiply(0.9),
404 2.0, x.multiply(0.8),
405 3.0, x.multiply(0.7));
406 }
407 });
408 }
409 }
410
411 @Test
412 public void testLinearCombination3F3FVsDS() {
413 for (double x = -1.25; x < 1.25; x+= 0.5) {
414 checkAgainstDS(x,
415 new FieldUnivariateFunction() {
416 public <S extends CalculusFieldElement<S>> S value(S x) {
417 return x.linearCombination(x.add(1), x.multiply(0.9),
418 x.add(2), x.multiply(0.8),
419 x.add(3), x.multiply(0.7));
420 }
421 });
422 }
423 }
424
425 @Test
426 public void testLinearCombination4D4FVsDS() {
427 for (double x = -1.25; x < 1.25; x+= 0.5) {
428 checkAgainstDS(x,
429 new FieldUnivariateFunction() {
430 public <S extends CalculusFieldElement<S>> S value(S x) {
431 return x.linearCombination(1.0, x.multiply(0.9),
432 2.0, x.multiply(0.8),
433 3.0, x.multiply(0.7),
434 4.0, x.multiply(0.6));
435 }
436 });
437 }
438 }
439
440 @Test
441 public void testLinearCombination4F4FVsDS() {
442 for (double x = -1.25; x < 1.25; x+= 0.5) {
443 checkAgainstDS(x,
444 new FieldUnivariateFunction() {
445 public <S extends CalculusFieldElement<S>> S value(S x) {
446 return x.linearCombination(x.add(1), x.multiply(0.9),
447 x.add(2), x.multiply(0.8),
448 x.add(3), x.multiply(0.7),
449 x.add(4), x.multiply(0.6));
450 }
451 });
452 }
453 }
454
455 @Test
456 public void testLinearCombinationnDnFVsDS() {
457 for (double x = -1.25; x < 1.25; x+= 0.5) {
458 checkAgainstDS(x,
459 new FieldUnivariateFunction() {
460 public <S extends CalculusFieldElement<S>> S value(S x) {
461 final S[] b = MathArrays.buildArray(x.getField(), 4);
462 b[0] = x.add(0.9);
463 b[1] = x.add(0.8);
464 b[2] = x.add(0.7);
465 b[3] = x.add(0.6);
466 return x.linearCombination(new double[] { 1, 2, 3, 4 }, b);
467 }
468 });
469 }
470 }
471
472 @Test
473 public void testLinearCombinationnFnFVsDS() {
474 for (double x = -1.25; x < 1.25; x+= 0.5) {
475 checkAgainstDS(x,
476 new FieldUnivariateFunction() {
477 public <S extends CalculusFieldElement<S>> S value(S x) {
478 final S[] a = MathArrays.buildArray(x.getField(), 4);
479 a[0] = x.add(1);
480 a[1] = x.add(2);
481 a[2] = x.add(3);
482 a[3] = x.add(4);
483 final S[] b = MathArrays.buildArray(x.getField(), 4);
484 b[0] = x.add(0.9);
485 b[1] = x.add(0.8);
486 b[2] = x.add(0.7);
487 b[3] = x.add(0.6);
488 return x.linearCombination(a, b);
489 }
490 });
491 }
492 }
493
494 @Test
495 public void testLinearCombinationField() {
496 final T[] a = MathArrays.buildArray(getValueField(), 3);
497 a[0] = buildScalar(-1321008684645961.0 / 268435456.0);
498 a[1] = buildScalar(-5774608829631843.0 / 268435456.0);
499 a[2] = buildScalar(-7645843051051357.0 / 8589934592.0);
500 final FieldUnivariateDerivative1<T>[] b = MathArrays.buildArray(FieldUnivariateDerivative1Field.getUnivariateDerivative1Field(getValueField()), 3);
501 b[0] = build(-5712344449280879.0 / 2097152.0);
502 b[1] = build(-4550117129121957.0 / 2097152.0);
503 b[2] = build(8846951984510141.0 / 131072.0);
504
505 final FieldUnivariateDerivative1<T> abSumInline = b[0].linearCombination(a[0], b[0],
506 a[1], b[1],
507 a[2], b[2]);
508 final FieldUnivariateDerivative1<T> abSumArray = b[0].linearCombination(a, b);
509 assertEquals(abSumInline.getReal(), abSumArray.getReal(), 3.0e-8);
510 assertEquals(-1.8551294182586248737720779899, abSumInline.getReal(), 5.0e-8);
511 assertEquals(abSumInline.getFirstDerivative().getReal(), abSumArray.getFirstDerivative().getReal(), 3.0e-8);
512 }
513
514 @Test
515 public void testZero() {
516 FieldUnivariateDerivative1<T> zero = build(17.0).getField().getZero();
517 for (int i = 0; i <= zero.getOrder(); ++i) {
518 assertEquals(0.0, zero.getDerivative(i).getReal(), 1.0e-15);
519 }
520 }
521
522 @Test
523 public void testOne() {
524 FieldUnivariateDerivative1<T> one = build(17.0).getField().getOne();
525 for (int i = 0; i <= one.getOrder(); ++i) {
526 assertEquals(i == 0 ? 1.0 : 0.0, one.getDerivative(i).getReal(), 1.0e-15);
527 }
528 }
529
530 @Test
531 public void testGetFirstDerivative() {
532 FieldUnivariateDerivative1<T> ud1 = build(-0.5, 2.5);
533 assertEquals(-0.5, ud1.getReal(), 1.0e-15);
534 assertEquals(-0.5, ud1.getValue().getReal(), 1.0e-15);
535 assertEquals(+2.5, ud1.getFirstDerivative().getReal(), 1.0e-15);
536 }
537
538 @Test
539 public void testConversion() {
540 FieldUnivariateDerivative1<T> udA = build(-0.5, 2.5);
541 FieldDerivativeStructure<T> ds = udA.toDerivativeStructure();
542 assertEquals(1, ds.getFreeParameters());
543 assertEquals(1, ds.getOrder());
544 assertEquals(-0.5, ds.getValue().getReal(), 1.0e-15);
545 assertEquals(-0.5, ds.getPartialDerivative(0).getReal(), 1.0e-15);
546 assertEquals( 2.5, ds.getPartialDerivative(1).getReal(), 1.0e-15);
547 FieldUnivariateDerivative1<T> udB = new FieldUnivariateDerivative1<>(ds);
548 assertNotSame(udA, udB);
549 assertEquals(udA, udB);
550 try {
551 new FieldUnivariateDerivative1<>(new FDSFactory<>(getValueField(), 2, 2).variable(0, 1.0));
552 fail("an exception should have been thrown");
553 } catch (MathIllegalArgumentException miae) {
554 assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
555 }
556 try {
557 new FieldUnivariateDerivative1<>(new FDSFactory<>(getValueField(), 1, 2).variable(0, 1.0));
558 fail("an exception should have been thrown");
559 } catch (MathIllegalArgumentException miae) {
560 assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
561 }
562 }
563
564 @Test
565 public void testDoublePow() {
566 assertSame(build(3).getField().getZero(), FieldUnivariateDerivative1.pow(0.0, build(1.5)));
567 FieldUnivariateDerivative1<T> ud = FieldUnivariateDerivative1.pow(2.0, build(1.5));
568 DSFactory factory = new DSFactory(1, 1);
569 DerivativeStructure ds = factory.constant(2.0).pow(factory.variable(0, 1.5));
570 assertEquals(ds.getValue(), ud.getValue().getReal(), 1.0e-15);
571 assertEquals(ds.getPartialDerivative(1), ud.getFirstDerivative().getReal(), 1.0e-15);
572 }
573
574 @Test
575 public void testTaylor() {
576 assertEquals(2.5, build(2, 1).taylor(0.5).getReal(), 1.0e-15);
577 assertEquals(2.5, build(2, 1).taylor(getValueField().getZero().newInstance(0.5)).getReal(), 1.0e-15);
578 }
579
580 @Test
581 public void testEquals() {
582 FieldUnivariateDerivative1<T> ud1 = build(12, -34);
583 assertEquals(ud1, ud1);
584 assertNotEquals("", ud1);
585 assertEquals(ud1, build(12, -34));
586 assertNotEquals(ud1, build(21, -34));
587 assertNotEquals(ud1, build(12, -43));
588 assertNotEquals(ud1, build(21, -43));
589 }
590
591 @Test
592 public void testRunTimeClass() {
593 Field<FieldUnivariateDerivative1<T>> field = build(0.0).getField();
594 assertEquals(FieldUnivariateDerivative1.class, field.getRuntimeClass());
595 }
596
597 private void check(FieldUnivariateDerivative1<T> ud1, double value, double derivative) {
598
599
600 assertEquals(value, ud1.getReal(), 1.0e-15);
601
602
603 assertEquals(derivative, ud1.getFirstDerivative().getReal(), 1.0e-15);
604
605 }
606
607 }