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