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.Field;
20 import org.hipparchus.exception.LocalizedCoreFormats;
21 import org.hipparchus.exception.MathIllegalArgumentException;
22 import org.junit.Assert;
23 import org.junit.Test;
24
25
26
27
28 public class UnivariateDerivative2Test extends UnivariateDerivativeAbstractTest<UnivariateDerivative2> {
29
30 @Override
31 protected UnivariateDerivative2 build(final double x) {
32 return new UnivariateDerivative2(x, 1.0, 0.0);
33 }
34
35 @Override
36 protected int getMaxOrder() {
37 return 2;
38 }
39
40 @Test
41 public void testGetFirstAndSecondDerivative() {
42 UnivariateDerivative2 ud1 = new UnivariateDerivative2(-0.5, 2.5, 4.5);
43 Assert.assertEquals(-0.5, ud1.getReal(), 1.0e-15);
44 Assert.assertEquals(-0.5, ud1.getValue(), 1.0e-15);
45 Assert.assertEquals(+2.5, ud1.getFirstDerivative(), 1.0e-15);
46 Assert.assertEquals(+4.5, ud1.getSecondDerivative(), 1.0e-15);
47 }
48
49 @Test
50 public void testConversion() {
51 UnivariateDerivative2 udA = new UnivariateDerivative2(-0.5, 2.5, 4.5);
52 DerivativeStructure ds = udA.toDerivativeStructure();
53 Assert.assertEquals(1, ds.getFreeParameters());
54 Assert.assertEquals(2, ds.getOrder());
55 Assert.assertEquals(-0.5, ds.getValue(), 1.0e-15);
56 Assert.assertEquals(-0.5, ds.getPartialDerivative(0), 1.0e-15);
57 Assert.assertEquals( 2.5, ds.getPartialDerivative(1), 1.0e-15);
58 Assert.assertEquals( 4.5, ds.getPartialDerivative(2), 1.0e-15);
59 UnivariateDerivative2 udB = new UnivariateDerivative2(ds);
60 Assert.assertNotSame(udA, udB);
61 Assert.assertEquals(udA, udB);
62 try {
63 new UnivariateDerivative2(new DSFactory(2, 2).variable(0, 1.0));
64 Assert.fail("an exception should have been thrown");
65 } catch (MathIllegalArgumentException miae) {
66 Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
67 }
68 try {
69 new UnivariateDerivative2(new DSFactory(1, 1).variable(0, 1.0));
70 Assert.fail("an exception should have been thrown");
71 } catch (MathIllegalArgumentException miae) {
72 Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
73 }
74 }
75
76 @Test
77 public void testDoublePow() {
78 Assert.assertSame(build(3).getField().getZero(), UnivariateDerivative2.pow(0.0, build(1.5)));
79 UnivariateDerivative2 ud = UnivariateDerivative2.pow(2.0, build(1.5));
80 DSFactory factory = new DSFactory(1, 2);
81 DerivativeStructure ds = factory.constant(2.0).pow(factory.variable(0, 1.5));
82 Assert.assertEquals(ds.getValue(), ud.getValue(), 1.0e-15);
83 Assert.assertEquals(ds.getPartialDerivative(1), ud.getFirstDerivative(), 1.0e-15);
84 Assert.assertEquals(ds.getPartialDerivative(2), ud.getSecondDerivative(), 1.0e-15);
85 }
86
87 @Test
88 public void testTaylor() {
89 Assert.assertEquals(-0.125, new UnivariateDerivative2(1, -3, 4).taylor(0.75), 1.0e-15);
90 }
91
92 @Test
93 public void testHashcode() {
94 Assert.assertEquals(-1025507011, new UnivariateDerivative2(2, 1, -1).hashCode());
95 }
96
97 @Test
98 public void testEquals() {
99 UnivariateDerivative2 ud2 = new UnivariateDerivative2(12, -34, 56);
100 Assert.assertEquals(ud2, ud2);
101 Assert.assertNotEquals(ud2, "");
102 Assert.assertEquals(ud2, new UnivariateDerivative2(12, -34, 56));
103 Assert.assertNotEquals(ud2, new UnivariateDerivative2(21, -34, 56));
104 Assert.assertNotEquals(ud2, new UnivariateDerivative2(12, -43, 56));
105 Assert.assertNotEquals(ud2, new UnivariateDerivative2(12, -34, 65));
106 Assert.assertNotEquals(ud2, new UnivariateDerivative2(21, -43, 65));
107 }
108
109 @Test
110 public void testComparableFirstTerm() {
111
112 final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
113 final UnivariateDerivative2 ud2b = new UnivariateDerivative2(2, 0, 25);
114
115 final int actualComparison = ud2a.compareTo(ud2b);
116
117 final int expectedComparison = 1;
118 Assert.assertEquals(expectedComparison, actualComparison);
119 }
120
121 @Test
122 public void testComparableSecondTerm() {
123
124 final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
125 final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, 0, 25);
126
127 final int actualComparison = ud2a.compareTo(ud2b);
128
129 final int expectedComparison = -1;
130 Assert.assertEquals(expectedComparison, actualComparison);
131 }
132
133 @Test
134 public void testComparableThirdTerm() {
135
136 final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
137 final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, -34, 25);
138
139 final int actualComparison = ud2a.compareTo(ud2b);
140
141 final int expectedComparison = 0;
142 Assert.assertEquals(expectedComparison, actualComparison);
143 }
144
145 @Test
146 public void testRunTimeClass() {
147 Field<UnivariateDerivative2> field = build(0.0).getField();
148 Assert.assertEquals(UnivariateDerivative2.class, field.getRuntimeClass());
149 }
150
151 }