View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.analysis.differentiation;
18  
19  import org.hipparchus.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.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  
28  class DerivativeTest {
29  
30      private static final double TOLERANCE = 1e-10;
31  
32      @Test
33      void testGetReal() {
34          // GIVEN
35          final double expectedOperation = 0.5;
36          final TestDerivative testDerivative = new TestDerivative(expectedOperation);
37          // WHEN
38          final double actualOperation = testDerivative.getReal();
39          // THEN
40          assertEquals(expectedOperation, actualOperation, 0.);
41      }
42  
43      @Test
44      void testAddDouble() {
45          // GIVEN
46          final double value = 0.5;
47          final TestDerivative testDerivative = new TestDerivative(value);
48          final double scalar = 2.;
49          // WHEN
50          final TestDerivative actualOperation = testDerivative.add(scalar);
51          // THEN
52          final TestDerivative expectedOperation = new TestDerivative(value + scalar);
53          assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
54      }
55  
56      @Test
57      void testSubtractDouble() {
58          // GIVEN
59          final double value = 0.5;
60          final TestDerivative testDerivative = new TestDerivative(value);
61          final double scalar = 2.;
62          // WHEN
63          final TestDerivative actualOperation = testDerivative.subtract(scalar);
64          // THEN
65          final TestDerivative expectedOperation = new TestDerivative(value - scalar);
66          assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
67      }
68  
69      @Test
70      void testLog10() {
71          // GIVEN
72          final double value = 0.5;
73          final TestDerivative testDerivative = new TestDerivative(value);
74          // WHEN
75          final TestDerivative actualOperation = testDerivative.log10();
76          // THEN
77          final TestDerivative expectedOperation = new TestDerivative(FastMath.log10(value));
78          assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
79      }
80  
81      @Test
82      void testPow() {
83          // GIVEN
84          final double value1 = 2.;
85          final double value2 = 3.;
86          final TestDerivative testDerivative1 = new TestDerivative(value1);
87          final TestDerivative testDerivative2 = new TestDerivative(value2);
88          // WHEN
89          final TestDerivative actualOperation = testDerivative1.pow(testDerivative2);
90          // THEN
91          final TestDerivative expectedOperation = new TestDerivative(FastMath.pow(value1, value2));
92          assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
93      }
94  
95      @Test
96      void testCosh() {
97          // GIVEN
98          final double value = 0.5;
99          final TestDerivative testDerivative = new TestDerivative(value);
100         // WHEN
101         final TestDerivative actualOperation = testDerivative.cosh();
102         // THEN
103         final TestDerivative expectedOperation = new TestDerivative(FastMath.cosh(value));
104         assertEquals(expectedOperation, actualOperation);
105     }
106 
107     @Test
108     void testSinh() {
109         // GIVEN
110         final double value = 0.5;
111         final TestDerivative testDerivative = new TestDerivative(value);
112         // WHEN
113         final TestDerivative actualOperation = testDerivative.sinh();
114         // THEN
115         final TestDerivative expectedOperation = new TestDerivative(FastMath.sinh(value));
116         assertEquals(expectedOperation, actualOperation);
117     }
118 
119     @Test
120     void testAcos() {
121         // GIVEN
122         final double value = 0.5;
123         final TestDerivative testDerivative = new TestDerivative(value);
124         // WHEN
125         final TestDerivative actualOperation = testDerivative.acos();
126         // THEN
127         final TestDerivative expectedOperation = new TestDerivative(FastMath.acos(value));
128         assertEquals(expectedOperation.getValue(), actualOperation.getValue(), TOLERANCE);
129     }
130 
131     static class TestDerivative implements Derivative<TestDerivative> {
132 
133         private final double value;
134 
135         TestDerivative (final double value) {
136             this.value = value;
137         }
138 
139         @Override
140         public TestDerivative getAddendum() {
141             return subtract(getReal());
142         }
143 
144         @Override
145         public TestDerivative newInstance(double value) {
146             return new TestDerivative(value);
147         }
148 
149         @Override
150         public TestDerivative scalb(int n) {
151             return null;
152         }
153 
154         @Override
155         public TestDerivative hypot(TestDerivative y) throws MathIllegalArgumentException {
156             return null;
157         }
158 
159         @Override
160         public TestDerivative exp() {
161             return new TestDerivative(FastMath.exp(value));
162         }
163 
164         @Override
165         public TestDerivative expm1() {
166             return null;
167         }
168 
169         @Override
170         public TestDerivative log() {
171             return new TestDerivative(FastMath.log(value));
172         }
173 
174         @Override
175         public TestDerivative log1p() {
176             return null;
177         }
178 
179         @Override
180         public TestDerivative cos() {
181             return null;
182         }
183 
184         @Override
185         public TestDerivative sin() {
186             return null;
187         }
188 
189         @Override
190         public TestDerivative asin() {
191             return new TestDerivative(FastMath.asin(value));
192         }
193 
194         @Override
195         public TestDerivative atan() {
196             return null;
197         }
198 
199         @Override
200         public TestDerivative atan2(TestDerivative x) throws MathIllegalArgumentException {
201             return null;
202         }
203 
204         @Override
205         public TestDerivative acosh() {
206             return null;
207         }
208 
209         @Override
210         public TestDerivative asinh() {
211             return null;
212         }
213 
214         @Override
215         public TestDerivative atanh() {
216             return null;
217         }
218 
219         @Override
220         public TestDerivative linearCombination(TestDerivative[] a, TestDerivative[] b) throws MathIllegalArgumentException {
221             return null;
222         }
223 
224         @Override
225         public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2) {
226             return null;
227         }
228 
229         @Override
230         public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2,
231                                                 TestDerivative a3, TestDerivative b3) {
232             return null;
233         }
234 
235         @Override
236         public TestDerivative linearCombination(TestDerivative a1, TestDerivative b1, TestDerivative a2, TestDerivative b2,
237                                                 TestDerivative a3, TestDerivative b3, TestDerivative a4, TestDerivative b4) {
238             return null;
239         }
240 
241         @Override
242         public TestDerivative remainder(TestDerivative a) {
243             return null;
244         }
245 
246         @Override
247         public TestDerivative copySign(TestDerivative sign) {
248             return null;
249         }
250 
251         @Override
252         public TestDerivative abs() {
253             return null;
254         }
255 
256         @Override
257         public TestDerivative add(TestDerivative a) throws NullArgumentException {
258             return new TestDerivative(value + a.value);
259         }
260 
261         @Override
262         public TestDerivative negate() {
263             return new TestDerivative(-value);
264         }
265 
266         @Override
267         public TestDerivative multiply(TestDerivative a) throws NullArgumentException {
268             return new TestDerivative(value * a.value);
269         }
270 
271         @Override
272         public TestDerivative reciprocal() throws MathRuntimeException {
273             return new TestDerivative(1. / value);
274         }
275 
276         @Override
277         public Field<TestDerivative> getField() {
278             return null;
279         }
280 
281         @Override
282         public int getFreeParameters() {
283             return 0;
284         }
285 
286         @Override
287         public int getOrder() {
288             return 0;
289         }
290 
291         @Override
292         public double getValue() {
293             return value;
294         }
295 
296         @Override
297         public double getPartialDerivative(int... orders) throws MathIllegalArgumentException {
298             return 0;
299         }
300 
301         @Override
302         public TestDerivative compose(double... f) throws MathIllegalArgumentException {
303             return null;
304         }
305 
306         @Override
307         public boolean equals(Object obj) {
308             if (obj instanceof DerivativeTest.TestDerivative) {
309                 return Double.compare(value, ((DerivativeTest.TestDerivative) obj).value) == 0;
310             } else {
311                 return false;
312             }
313         }
314     }
315 
316 }