1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32 final double value = 3.;
33 final int factor = 2;
34 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
35
36 final TestCalculusFieldElement actualOperation = testElement.multiply(factor);
37
38 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value * factor);
39 Assert.assertEquals(expectedOperation, actualOperation);
40 }
41
42 @Test
43 public void testAddDouble() {
44
45 final double value1 = 1.;
46 final double value2 = 2.;
47 final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
48
49 final TestCalculusFieldElement actualOperation = testElement1.add(value2);
50
51 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 + value2);
52 Assert.assertEquals(expectedOperation, actualOperation);
53 }
54
55 @Test
56 public void testSubtractDouble() {
57
58 final double value1 = 1.;
59 final double value2 = 2.;
60 final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
61
62 final TestCalculusFieldElement actualOperation = testElement1.subtract(value2);
63
64 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 - value2);
65 Assert.assertEquals(expectedOperation, actualOperation);
66 }
67
68 @Test
69 public void testMultiplyDouble() {
70
71 final double value1 = 3.;
72 final double value2 = 2.;
73 final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
74
75 final TestCalculusFieldElement actualOperation = testElement1.multiply(value2);
76
77 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 * value2);
78 Assert.assertEquals(expectedOperation, actualOperation);
79 }
80
81 @Test
82 public void testDivideDouble() {
83
84 final double value1 = 3.;
85 final double value2 = 2.;
86 final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1);
87
88 final TestCalculusFieldElement actualOperation = testElement1.divide(value2);
89
90 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 / value2);
91 Assert.assertEquals(expectedOperation, actualOperation);
92 }
93
94 @Test
95 public void testSubtract() {
96
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
102 final TestCalculusFieldElement actualOperation = testElement1.subtract(testElement2);
103
104 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 - value2);
105 Assert.assertEquals(expectedOperation, actualOperation);
106 }
107
108 @Test
109 public void testDivide() {
110
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
116 final TestCalculusFieldElement actualOperation = testElement1.divide(testElement2);
117
118 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value1 / value2);
119 Assert.assertEquals(expectedOperation, actualOperation);
120 }
121
122 @Test
123 public void testSquare() {
124
125 final double value = 3.;
126 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
127
128 final TestCalculusFieldElement actualOperation = testElement.square();
129
130 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(value * value);
131 Assert.assertEquals(expectedOperation, actualOperation);
132 }
133
134 @Test
135 public void testSqrt() {
136
137 final double value = 3.;
138 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
139
140 final TestCalculusFieldElement actualOperation = testElement.sqrt();
141
142 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sqrt(value));
143 Assert.assertEquals(expectedOperation, actualOperation);
144 }
145
146 @Test
147 public void testCbrt() {
148
149 final double value = 3.;
150 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
151
152 final TestCalculusFieldElement actualOperation = testElement.cbrt();
153
154 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cbrt(value));
155 Assert.assertEquals(expectedOperation, actualOperation);
156 }
157
158 @Test
159 public void testRootN() {
160
161 final int n = 4;
162 final double value = 3.;
163 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
164
165 final TestCalculusFieldElement actualOperation = testElement.rootN(n);
166
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
174 final int exponent = 4;
175 final double value = 3.;
176 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
177
178 final TestCalculusFieldElement actualOperation = testElement.pow(exponent);
179
180 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.pow(value, exponent));
181 Assert.assertEquals(expectedOperation, actualOperation);
182 }
183
184 @Test
185 public void testPowDouble() {
186
187 final double exponent = 4.5;
188 final double value = 3.;
189 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
190
191 final TestCalculusFieldElement actualOperation = testElement.pow(exponent);
192
193 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.pow(value, exponent));
194 Assert.assertEquals(expectedOperation, actualOperation);
195 }
196
197 @Test
198 public void testSin() {
199
200 final double value = 3.;
201 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
202
203 final TestCalculusFieldElement actualOperation = testElement.sin();
204
205 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sin(value));
206 Assert.assertEquals(expectedOperation, actualOperation);
207 }
208
209 @Test
210 public void testCos() {
211
212 final double value = 3.;
213 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
214
215 final TestCalculusFieldElement actualOperation = testElement.cos();
216
217 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cos(value));
218 Assert.assertEquals(expectedOperation, actualOperation);
219 }
220
221 @Test
222 public void testTan() {
223
224 final double value = 3.;
225 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
226
227 final TestCalculusFieldElement actualOperation = testElement.tan();
228
229 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.tan(value));
230 Assert.assertEquals(expectedOperation, actualOperation);
231 }
232
233 @Test
234 public void testSinh() {
235
236 final double value = 3.;
237 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
238
239 final TestCalculusFieldElement actualOperation = testElement.sinh();
240
241 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.sinh(value));
242 Assert.assertEquals(expectedOperation, actualOperation);
243 }
244
245 @Test
246 public void testCosh() {
247
248 final double value = 3.;
249 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
250
251 final TestCalculusFieldElement actualOperation = testElement.cosh();
252
253 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.cosh(value));
254 Assert.assertEquals(expectedOperation, actualOperation);
255 }
256
257 @Test
258 public void testTanh() {
259
260 final double value = 3.;
261 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
262
263 final TestCalculusFieldElement actualOperation = testElement.tanh();
264
265 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.tanh(value));
266 Assert.assertEquals(expectedOperation, actualOperation);
267 }
268
269 @Test
270 public void testSign() {
271
272 final double value = 3.6;
273 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
274
275 final TestCalculusFieldElement actualOperation = testElement.sign();
276
277 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.signum(value));
278 Assert.assertEquals(expectedOperation, actualOperation);
279 }
280
281 @Test
282 public void testUlp() {
283
284 final double value = 3.6;
285 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
286
287 final TestCalculusFieldElement actualOperation = testElement.ulp();
288
289 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.ulp(value));
290 Assert.assertEquals(expectedOperation, actualOperation);
291 }
292
293 @Test
294 public void testFloor() {
295
296 final double value = 3.6;
297 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
298
299 final TestCalculusFieldElement actualOperation = testElement.floor();
300
301 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.floor(value));
302 Assert.assertEquals(expectedOperation, actualOperation);
303 }
304
305 @Test
306 public void testCeil() {
307
308 final double value = 3.6;
309 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
310
311 final TestCalculusFieldElement actualOperation = testElement.ceil();
312
313 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.ceil(value));
314 Assert.assertEquals(expectedOperation, actualOperation);
315 }
316
317 @Test
318 public void testRint() {
319
320 final double value = 3.6;
321 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
322
323 final TestCalculusFieldElement actualOperation = testElement.rint();
324
325 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.rint(value));
326 Assert.assertEquals(expectedOperation, actualOperation);
327 }
328
329 @Test
330 public void testToDegrees() {
331
332 final double value = 3.;
333 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
334
335 final TestCalculusFieldElement actualOperation = testElement.toDegrees();
336
337 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.toDegrees(value));
338 Assert.assertEquals(expectedOperation, actualOperation);
339 }
340
341 @Test
342 public void testToRadians() {
343
344 final double value = 3.;
345 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
346
347 final TestCalculusFieldElement actualOperation = testElement.toRadians();
348
349 final TestCalculusFieldElement expectedOperation = new TestCalculusFieldElement(FastMath.toRadians(value));
350 Assert.assertEquals(expectedOperation, actualOperation);
351 }
352
353 @Test
354 public void testLinearCombinationDouble2() {
355
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
363 final TestCalculusFieldElement actualOperation = testElement1.linearCombination(coeff1, testElement1,
364 coeff2, testElement2);
365
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
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
384 final TestCalculusFieldElement actualOperation = testElement1.linearCombination(coeff1, testElement1,
385 coeff2, testElement2, coeff3, testElement3);
386
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
395 final double value = 3.5;
396 final TestCalculusFieldElement testElement = new TestCalculusFieldElement(value);
397
398 final int actualOperation = testElement.getExponent();
399
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 }