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  
18  package org.hipparchus;
19  
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 CalculusFieldElementTest {
28  
29      @Test
30      public void testMultiplyInt() {
31          // GIVEN
32          final double value = 3.;
33          final int factor = 2;
34          final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
35          // WHEN
36          final TestCalculusFieldElement actualOperation = testElement.multiply(factor);
37          // THEN
38          final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value * factor);
39          Assert.assertEquals(expectedOperation, actualOperation);
40      }
41  
42      @Test
43      public void testAddDouble() {
44          // GIVEN
45          final double value1 = 1.;
46          final double value2 = 2.;
47          final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
48          // WHEN
49          final TestCalculusFieldElement actualOperation = testElement1.add(value2);
50          // THEN
51          final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 + value2);
52          Assert.assertEquals(expectedOperation, actualOperation);
53      }
54  
55      @Test
56      public void testSubtractDouble() {
57          // GIVEN
58          final double value1 = 1.;
59          final double value2 = 2.;
60          final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
61          // WHEN
62          final TestCalculusFieldElement actualOperation = testElement1.subtract(value2);
63          // THEN
64          final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 - value2);
65          Assert.assertEquals(expectedOperation, actualOperation);
66      }
67  
68      @Test
69      public void testMultiplyDouble() {
70          // GIVEN
71          final double value1 = 3.;
72          final double value2 = 2.;
73          final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
74          // WHEN
75          final TestCalculusFieldElement actualOperation = testElement1.multiply(value2);
76          // THEN
77          final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 * value2);
78          Assert.assertEquals(expectedOperation, actualOperation);
79      }
80  
81      @Test
82      public void testDivideDouble() {
83          // GIVEN
84          final double value1 = 3.;
85          final double value2 = 2.;
86          final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
87          // WHEN
88          final TestCalculusFieldElement actualOperation = testElement1.divide(value2);
89          // THEN
90          final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 / value2);
91          Assert.assertEquals(expectedOperation, actualOperation);
92      }
93  
94      @Test
95      public void testSubtract() {
96          // GIVEN
97          final double value1 = 1.;
98          final double value2 = 2.;
99          final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
100         final TestCalculusFieldElement testElement2 = new TestCalculusFieldElement(value2);
101         // WHEN
102         final TestCalculusFieldElement actualOperation = testElement1.subtract(testElement2);
103         // THEN
104         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 - value2);
105         Assert.assertEquals(expectedOperation, actualOperation);
106     }
107 
108     @Test
109     public void testDivide() {
110         // GIVEN
111         final double value1 = 3.;
112         final double value2 = 2.;
113         final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
114         final TestCalculusFieldElement testElement2 = new TestCalculusFieldElement(value2);
115         // WHEN
116         final TestCalculusFieldElement actualOperation = testElement1.divide(testElement2);
117         // THEN
118         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 / value2);
119         Assert.assertEquals(expectedOperation, actualOperation);
120     }
121 
122     @Test
123     public void testSquare() {
124         // GIVEN
125         final double value = 3.;
126         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
127         // WHEN
128         final TestCalculusFieldElement actualOperation = testElement.square();
129         // THEN
130         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value * value);
131         Assert.assertEquals(expectedOperation, actualOperation);
132     }
133 
134     @Test
135     public void testSqrt() {
136         // GIVEN
137         final double value = 3.;
138         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
139         // WHEN
140         final TestCalculusFieldElement actualOperation = testElement.sqrt();
141         // THEN
142         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sqrt(value));
143         Assert.assertEquals(expectedOperation, actualOperation);
144     }
145 
146     @Test
147     public void testCbrt() {
148         // GIVEN
149         final double value = 3.;
150         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
151         // WHEN
152         final TestCalculusFieldElement actualOperation = testElement.cbrt();
153         // THEN
154         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cbrt(value));
155         Assert.assertEquals(expectedOperation, actualOperation);
156     }
157 
158     @Test
159     public void testRootN() {
160         // GIVEN
161         final int n = 4;
162         final double value = 3.;
163         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
164         // WHEN
165         final TestCalculusFieldElement actualOperation = testElement.rootN(n);
166         // THEN
167         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.pow(value, 1. / n));
168         Assert.assertEquals(expectedOperation, actualOperation);
169     }
170 
171     @Test
172     public void testPowInt() {
173         // GIVEN
174         final int exponent = 4;
175         final double value = 3.;
176         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
177         // WHEN
178         final TestCalculusFieldElement actualOperation = testElement.pow(exponent);
179         // THEN
180         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.pow(value, exponent));
181         Assert.assertEquals(expectedOperation, actualOperation);
182     }
183 
184     @Test
185     public void testPowDouble() {
186         // GIVEN
187         final double exponent = 4.5;
188         final double value = 3.;
189         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
190         // WHEN
191         final TestCalculusFieldElement actualOperation = testElement.pow(exponent);
192         // THEN
193         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.pow(value, exponent));
194         Assert.assertEquals(expectedOperation, actualOperation);
195     }
196 
197     @Test
198     public void testSin() {
199         // GIVEN
200         final double value = 3.;
201         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
202         // WHEN
203         final TestCalculusFieldElement actualOperation = testElement.sin();
204         // THEN
205         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sin(value));
206         Assert.assertEquals(expectedOperation, actualOperation);
207     }
208 
209     @Test
210     public void testCos() {
211         // GIVEN
212         final double value = 3.;
213         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
214         // WHEN
215         final TestCalculusFieldElement actualOperation = testElement.cos();
216         // THEN
217         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cos(value));
218         Assert.assertEquals(expectedOperation, actualOperation);
219     }
220 
221     @Test
222     public void testTan() {
223         // GIVEN
224         final double value = 3.;
225         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
226         // WHEN
227         final TestCalculusFieldElement actualOperation = testElement.tan();
228         // THEN
229         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.tan(value));
230         Assert.assertEquals(expectedOperation, actualOperation);
231     }
232 
233     @Test
234     public void testSinh() {
235         // GIVEN
236         final double value = 3.;
237         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
238         // WHEN
239         final TestCalculusFieldElement actualOperation = testElement.sinh();
240         // THEN
241         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sinh(value));
242         Assert.assertEquals(expectedOperation, actualOperation);
243     }
244 
245     @Test
246     public void testCosh() {
247         // GIVEN
248         final double value = 3.;
249         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
250         // WHEN
251         final TestCalculusFieldElement actualOperation = testElement.cosh();
252         // THEN
253         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cosh(value));
254         Assert.assertEquals(expectedOperation, actualOperation);
255     }
256 
257     @Test
258     public void testTanh() {
259         // GIVEN
260         final double value = 3.;
261         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
262         // WHEN
263         final TestCalculusFieldElement actualOperation = testElement.tanh();
264         // THEN
265         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.tanh(value));
266         Assert.assertEquals(expectedOperation, actualOperation);
267     }
268 
269     @Test
270     public void testSign() {
271         // GIVEN
272         final double value = 3.6;
273         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
274         // WHEN
275         final TestCalculusFieldElement actualOperation = testElement.sign();
276         // THEN
277         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.signum(value));
278         Assert.assertEquals(expectedOperation, actualOperation);
279     }
280 
281     @Test
282     public void testUlp() {
283         // GIVEN
284         final double value = 3.6;
285         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
286         // WHEN
287         final TestCalculusFieldElement actualOperation = testElement.ulp();
288         // THEN
289         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.ulp(value));
290         Assert.assertEquals(expectedOperation, actualOperation);
291     }
292 
293     @Test
294     public void testFloor() {
295         // GIVEN
296         final double value = 3.6;
297         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
298         // WHEN
299         final TestCalculusFieldElement actualOperation = testElement.floor();
300         // THEN
301         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.floor(value));
302         Assert.assertEquals(expectedOperation, actualOperation);
303     }
304 
305     @Test
306     public void testCeil() {
307         // GIVEN
308         final double value = 3.6;
309         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
310         // WHEN
311         final TestCalculusFieldElement actualOperation = testElement.ceil();
312         // THEN
313         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.ceil(value));
314         Assert.assertEquals(expectedOperation, actualOperation);
315     }
316 
317     @Test
318     public void testRint() {
319         // GIVEN
320         final double value = 3.6;
321         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
322         // WHEN
323         final TestCalculusFieldElement actualOperation = testElement.rint();
324         // THEN
325         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.rint(value));
326         Assert.assertEquals(expectedOperation, actualOperation);
327     }
328 
329     @Test
330     public void testToDegrees() {
331         // GIVEN
332         final double value = 3.;
333         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
334         // WHEN
335         final TestCalculusFieldElement actualOperation = testElement.toDegrees();
336         // THEN
337         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.toDegrees(value));
338         Assert.assertEquals(expectedOperation, actualOperation);
339     }
340 
341     @Test
342     public void testToRadians() {
343         // GIVEN
344         final double value = 3.;
345         final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
346         // WHEN
347         final TestCalculusFieldElement actualOperation = testElement.toRadians();
348         // THEN
349         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.toRadians(value));
350         Assert.assertEquals(expectedOperation, actualOperation);
351     }
352 
353     @Test
354     public void testLinearCombinationDouble2() {
355         // GIVEN
356         final double value1 = 3.;
357         final double value2 = 2.;
358         final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
359         final TestCalculusFieldElement testElement2 = new TestCalculusFieldElement(value2);
360         final double coeff1 = -5.;
361         final double coeff2 = 4.;
362         // WHEN
363         final TestCalculusFieldElement actualOperation = testElement1.linearCombination(coeff1, testElement1,
364                 coeff2, testElement2);
365         // THEN
366         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(
367                 coeff1 * value1 + coeff2 * value2);
368         Assert.assertEquals(expectedOperation, actualOperation);
369     }
370 
371     @Test
372     public void testLinearCombinationDouble3() {
373         // GIVEN
374         final double value1 = 3.;
375         final double value2 = 2.;
376         final double value3 = -1;
377         final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
378         final TestCalculusFieldElement testElement2 = new TestCalculusFieldElement(value2);
379         final TestCalculusFieldElement testElement3 = new TestCalculusFieldElement(value3);
380         final double coeff1 = -5.;
381         final double coeff2 = 4.;
382         final double coeff3 = 6.;
383         // WHEN
384         final TestCalculusFieldElement actualOperation = testElement1.linearCombination(coeff1, testElement1,
385                 coeff2, testElement2, coeff3, testElement3);
386         // THEN
387         final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(
388                 coeff1 * value1 + coeff2 * value2 + coeff3 * value3);
389         Assert.assertEquals(expectedOperation, actualOperation);
390     }
391 
392     @Test
393     public void testGetExponent() {
394         // GIVEN
395         final double value = 3.5;
396         final  TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
397         // WHEN
398         final int actualOperation = testElement.getExponent();
399         // THEN
400         final int expectedOperation = FastMath.getExponent(value);
401         Assert.assertEquals(expectedOperation, actualOperation);
402     }
403 
404     @Test
405     public void testIsFinite() {
406         Assert.assertTrue(new TestCalculusFieldElement(1.).isFinite());
407         Assert.assertFalse(new TestCalculusFieldElement(Double.NaN).isFinite());
408         Assert.assertFalse(new TestCalculusFieldElement(Double.POSITIVE_INFINITY).isFinite());
409     }
410 
411     @Test
412     public void testIsInfinite() {
413         Assert.assertFalse(new TestCalculusFieldElement(1.).isInfinite());
414         Assert.assertTrue(new TestCalculusFieldElement(Double.POSITIVE_INFINITY).isInfinite());
415     }
416 
417     @Test
418     public void testIsNan() {
419         Assert.assertFalse(new TestCalculusFieldElement(1.).isNaN());
420         Assert.assertTrue(new TestCalculusFieldElement(Double.NaN).isNaN());
421     }
422 
423     @Test
424     public void testNorm() {
425         Assert.assertEquals(0., new TestCalculusFieldElement(0.).norm(), 0.0);
426     }
427 
428     private static class TestCalculusFieldElement implements CalculusFieldElement<TestCalculusFieldElement> {
429 
430         private final double value;
431 
432         TestCalculusFieldElement (double value) {
433             this.value = value;
434         }
435 
436         @Override
437         public TestCalculusFieldElement newInstance(double value) {
438             return new TestCalculusFieldElement(value);
439         }
440 
441         @Override
442         public TestCalculusFieldElement scalb(int n) {
443             return null;
444         }
445 
446         @Override
447         public TestCalculusFieldElement ulp() {
448             return new TestCalculusFieldElement(FastMath.ulp(value));
449         }
450 
451         @Override
452         public TestCalculusFieldElement hypot(TestCalculusFieldElement y) throws MathIllegalArgumentException {
453             return null;
454         }
455 
456         @Override
457         public TestCalculusFieldElement pow(TestCalculusFieldElement e) throws MathIllegalArgumentException {
458             return new TestCalculusFieldElement(FastMath.pow(value, e.value));
459         }
460 
461         @Override
462         public TestCalculusFieldElement exp() {
463             return null;
464         }
465 
466         @Override
467         public TestCalculusFieldElement expm1() {
468             return null;
469         }
470 
471         @Override
472         public TestCalculusFieldElement log() {
473             return null;
474         }
475 
476         @Override
477         public TestCalculusFieldElement log1p() {
478             return null;
479         }
480 
481         @Override
482         public TestCalculusFieldElement log10() {
483             return null;
484         }
485 
486         @Override
487         public TestCalculusFieldElement sin() {
488             return new TestCalculusFieldElement(FastMath.sin(value));
489         }
490 
491         @Override
492         public TestCalculusFieldElement cos() {
493             return new TestCalculusFieldElement(FastMath.cos(value));
494         }
495 
496         @Override
497         public TestCalculusFieldElement acos() {
498             return null;
499         }
500 
501         @Override
502         public TestCalculusFieldElement asin() {
503             return null;
504         }
505 
506         @Override
507         public TestCalculusFieldElement atan() {
508             return null;
509         }
510 
511         @Override
512         public TestCalculusFieldElement atan2(TestCalculusFieldElement x) throws MathIllegalArgumentException {
513             return null;
514         }
515 
516         @Override
517         public TestCalculusFieldElement sinh() {
518             return new TestCalculusFieldElement(FastMath.sinh(value));
519         }
520 
521         @Override
522         public TestCalculusFieldElement cosh() {
523             return new TestCalculusFieldElement(FastMath.cosh(value));
524         }
525 
526         @Override
527         public TestCalculusFieldElement acosh() {
528             return null;
529         }
530 
531         @Override
532         public TestCalculusFieldElement asinh() {
533             return null;
534         }
535 
536         @Override
537         public TestCalculusFieldElement atanh() {
538             return null;
539         }
540 
541         @Override
542         public TestCalculusFieldElement linearCombination(TestCalculusFieldElement[] a, TestCalculusFieldElement[] b) throws MathIllegalArgumentException {
543             return null;
544         }
545 
546         @Override
547         public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2) {
548             return newInstance(a1.value * b1.value + a2.value * b2.value);
549         }
550 
551         @Override
552         public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2, TestCalculusFieldElement a3, TestCalculusFieldElement b3) {
553             return newInstance(a1.value * b1.value + a2.value * b2.value + a3.value * b3.value);
554         }
555 
556         @Override
557         public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2, TestCalculusFieldElement a3, TestCalculusFieldElement b3, TestCalculusFieldElement a4, TestCalculusFieldElement b4) {
558             return newInstance(a1.value * b1.value + a2.value * b2.value + a3.value * b3.value +
559                     a4.value * b4.value);
560         }
561 
562         @Override
563         public TestCalculusFieldElement ceil() {
564             return new TestCalculusFieldElement(FastMath.ceil(value));
565         }
566 
567         @Override
568         public TestCalculusFieldElement floor() {
569             return new TestCalculusFieldElement(FastMath.floor(value));
570         }
571 
572         @Override
573         public TestCalculusFieldElement rint() {
574             return new TestCalculusFieldElement(FastMath.rint(value));
575         }
576 
577         @Override
578         public TestCalculusFieldElement remainder(double a) {
579             return null;
580         }
581 
582         @Override
583         public TestCalculusFieldElement remainder(TestCalculusFieldElement a) {
584             return null;
585         }
586 
587         @Override
588         public TestCalculusFieldElement sign() {
589             return new TestCalculusFieldElement(FastMath.signum(value));
590         }
591 
592         @Override
593         public TestCalculusFieldElement copySign(TestCalculusFieldElement sign) {
594             return null;
595         }
596 
597         @Override
598         public TestCalculusFieldElement abs() {
599             return new TestCalculusFieldElement(FastMath.abs(value));
600         }
601 
602         @Override
603         public double getReal() {
604             return value;
605         }
606 
607         @Override
608         public TestCalculusFieldElement add(TestCalculusFieldElement a) throws NullArgumentException {
609             return new TestCalculusFieldElement(value + a.value);
610         }
611 
612         @Override
613         public TestCalculusFieldElement negate() {
614             return new TestCalculusFieldElement(-value);
615         }
616 
617         @Override
618         public TestCalculusFieldElement multiply(TestCalculusFieldElement a) throws NullArgumentException {
619             return new TestCalculusFieldElement(value * a.value);
620         }
621 
622         @Override
623         public TestCalculusFieldElement reciprocal() throws MathRuntimeException {
624             return new TestCalculusFieldElement(1. / value);
625         }
626 
627         @Override
628         public Field<TestCalculusFieldElement> getField() {
629             return null;
630         }
631 
632         @Override
633         public boolean equals(Object obj) {
634             if (obj instanceof TestCalculusFieldElement) {
635                 return Double.compare(value, ((TestCalculusFieldElement) obj).value) == 0;
636             } else {
637                 return false;
638             }
639         }
640     }
641 
642 }