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