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.MathIllegalArgumentException;
21 import org.hipparchus.exception.MathRuntimeException;
22 import org.hipparchus.exception.NullArgumentException;
23 import org.hipparchus.util.FastMath;
24 import org.junit.Assert;
25 import org.junit.Test;
26
27 public class DerivativeTest {
28
29 private static final double TOLERANCE = 1e-10;
30
31 @Test
32 public void testGetReal() {
33
34 final double expectedOperation = 0.5;
35 final TestDerivative testDerivative = new TestDerivative(expectedOperation);
36
37 final double actualOperation = testDerivative.getReal();
38
39 Assert.assertEquals(expectedOperation, actualOperation, 0.);
40 }
41
42 @Test
43 public void testAddDouble() {
44
45 final double value = 0.5;
46 final TestDerivative testDerivative = new TestDerivative(value);
47 final double scalar = 2.;
48
49 final TestDerivative actualOperation = testDerivative.add(scalar);
50
51 final TestDerivative expectedOperation = new TestDerivative(value + scalar);
52 Assert.assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
53 }
54
55 @Test
56 public void testSubtractDouble() {
57
58 final double value = 0.5;
59 final TestDerivative testDerivative = new TestDerivative(value);
60 final double scalar = 2.;
61
62 final TestDerivative actualOperation = testDerivative.subtract(scalar);
63
64 final TestDerivative expectedOperation = new TestDerivative(value - scalar);
65 Assert.assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
66 }
67
68 @Test
69 public void testLog10() {
70
71 final double value = 0.5;
72 final TestDerivative testDerivative = new TestDerivative(value);
73
74 final TestDerivative actualOperation = testDerivative.log10();
75
76 final TestDerivative expectedOperation = new TestDerivative(FastMath.log10(value));
77 Assert.assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
78 }
79
80 @Test
81 public void testPow() {
82
83 final double value1 = 2.;
84 final double value2 = 3.;
85 final TestDerivative testDerivative1 = new TestDerivative(value1);
86 final TestDerivative testDerivative2 = new TestDerivative(value2);
87
88 final TestDerivative actualOperation = testDerivative1.pow(testDerivative2);
89
90 final TestDerivative expectedOperation = new TestDerivative(FastMath.pow(value1, value2));
91 Assert.assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
92 }
93
94 @Test
95 public void testCosh() {
96
97 final double value = 0.5;
98 final TestDerivative testDerivative = new TestDerivative(value);
99
100 final TestDerivative actualOperation = testDerivative.cosh();
101
102 final TestDerivative expectedOperation = new TestDerivative(FastMath.cosh(value));
103 Assert.assertEquals(expectedOperation, actualOperation);
104 }
105
106 @Test
107 public void testSinh() {
108
109 final double value = 0.5;
110 final TestDerivative testDerivative = new TestDerivative(value);
111
112 final TestDerivative actualOperation = testDerivative.sinh();
113
114 final TestDerivative expectedOperation = new TestDerivative(FastMath.sinh(value));
115 Assert.assertEquals(expectedOperation, actualOperation);
116 }
117
118 @Test
119 public void testAcos() {
120
121 final double value = 0.5;
122 final TestDerivative testDerivative = new TestDerivative(value);
123
124 final TestDerivative actualOperation = testDerivative.acos();
125
126 final TestDerivative expectedOperation = new TestDerivative(FastMath.acos(value));
127 Assert.assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
128 }
129
130 static class TestDerivative implements Derivative<TestDerivative> {
131
132 private final double value;
133
134 TestDerivative (final double value) {
135 this.value = value;
136 }
137
138 @Override
139 public TestDerivative newInstance(double value) {
140 return new TestDerivative(value);
141 }
142
143 @Override
144 public TestDerivative scalb(int n) {
145 return null;
146 }
147
148 @Override
149 public TestDerivative hypot(TestDerivative y) throws MathIllegalArgumentException {
150 return null;
151 }
152
153 @Override
154 public TestDerivative exp() {
155 return new TestDerivative(FastMath.exp(value));
156 }
157
158 @Override
159 public TestDerivative expm1() {
160 return null;
161 }
162
163 @Override
164 public TestDerivative log() {
165 return new TestDerivative(FastMath.log(value));
166 }
167
168 @Override
169 public TestDerivative log1p() {
170 return null;
171 }
172
173 @Override
174 public TestDerivative cos() {
175 return null;
176 }
177
178 @Override
179 public TestDerivative sin() {
180 return null;
181 }
182
183 @Override
184 public TestDerivative asin() {
185 return new TestDerivative(FastMath.asin(value));
186 }
187
188 @Override
189 public TestDerivative atan() {
190 return null;
191 }
192
193 @Override
194 public TestDerivative atan2(TestDerivative x) throws MathIllegalArgumentException {
195 return null;
196 }
197
198 @Override
199 public TestDerivative acosh() {
200 return null;
201 }
202
203 @Override
204 public TestDerivative asinh() {
205 return null;
206 }
207
208 @Override
209 public TestDerivative atanh() {
210 return null;
211 }
212
213 @Override
214 public TestDerivative linearCombination(TestDerivative[] a, TestDerivative[] b) throws MathIllegalArgumentException {
215 return null;
216 }
217
218 @Override
219 public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2) {
220 return null;
221 }
222
223 @Override
224 public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2,
225 TestDerivative a3, TestDerivative b3) {
226 return null;
227 }
228
229 @Override
230 public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2,
231 TestDerivative a3, TestDerivative b3, TestDerivative a4, TestDerivative b4) {
232 return null;
233 }
234
235 @Override
236 public TestDerivative remainder(TestDerivative a) {
237 return null;
238 }
239
240 @Override
241 public TestDerivative copySign(TestDerivative sign) {
242 return null;
243 }
244
245 @Override
246 public TestDerivative abs() {
247 return null;
248 }
249
250 @Override
251 public TestDerivative add(TestDerivative a) throws NullArgumentException {
252 return new TestDerivative(value + a.value);
253 }
254
255 @Override
256 public TestDerivative negate() {
257 return new TestDerivative(-value);
258 }
259
260 @Override
261 public TestDerivative multiply(TestDerivative a) throws NullArgumentException {
262 return new TestDerivative(value * a.value);
263 }
264
265 @Override
266 public TestDerivative reciprocal() throws MathRuntimeException {
267 return new TestDerivative(1. / value);
268 }
269
270 @Override
271 public Field<TestDerivative> getField() {
272 return null;
273 }
274
275 @Override
276 public int getFreeParameters() {
277 return 0;
278 }
279
280 @Override
281 public int getOrder() {
282 return 0;
283 }
284
285 @Override
286 public double getValue() {
287 return value;
288 }
289
290 @Override
291 public double getPartialDerivative(int... orders) throws MathIllegalArgumentException {
292 return 0;
293 }
294
295 @Override
296 public TestDerivative compose(double... f) throws MathIllegalArgumentException {
297 return null;
298 }
299
300 @Override
301 public boolean equals(Object obj) {
302 if (obj instanceof DerivativeTest.TestDerivative) {
303 return Double.compare(value, ((DerivativeTest.TestDerivative) obj).value) == 0;
304 } else {
305 return false;
306 }
307 }
308 }
309
310 }