1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.ode.nonstiff;
18
19 import org.apache.commons.numbers.core.Sum;
20 import org.apache.commons.math4.core.jdkmath.JdkMath;
21 import org.apache.commons.math4.legacy.core.Field;
22 import org.apache.commons.math4.legacy.core.RealFieldElement;
23 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
24
25
26
27
28
29
30
31
32 public class Decimal64 extends Number
33 implements RealFieldElement<Decimal64>, Comparable<Decimal64> {
34
35
36 public static final Decimal64 ZERO;
37
38
39 public static final Decimal64 ONE;
40
41
42
43
44
45 public static final Decimal64 NEGATIVE_INFINITY;
46
47
48
49
50
51 public static final Decimal64 POSITIVE_INFINITY;
52
53
54 public static final Decimal64 NAN;
55
56
57 private static final long serialVersionUID = 20120227L;
58
59 static {
60 ZERO = new Decimal64(0d);
61 ONE = new Decimal64(1d);
62 NEGATIVE_INFINITY = new Decimal64(Double.NEGATIVE_INFINITY);
63 POSITIVE_INFINITY = new Decimal64(Double.POSITIVE_INFINITY);
64 NAN = new Decimal64(Double.NaN);
65 }
66
67
68 private final double value;
69
70
71
72
73
74
75 public Decimal64(final double x) {
76 this.value = x;
77 }
78
79
80
81
82
83
84 @Override
85 public Field<Decimal64> getField() {
86 return Decimal64Field.getInstance();
87 }
88
89
90
91
92
93
94
95
96 @Override
97 public Decimal64 add(final Decimal64 a) {
98 return new Decimal64(this.value + a.value);
99 }
100
101
102
103
104
105
106
107
108 @Override
109 public Decimal64 subtract(final Decimal64 a) {
110 return new Decimal64(this.value - a.value);
111 }
112
113
114
115
116
117
118
119 @Override
120 public Decimal64 negate() {
121 return new Decimal64(-this.value);
122 }
123
124
125
126
127
128
129
130
131 @Override
132 public Decimal64 multiply(final Decimal64 a) {
133 return new Decimal64(this.value * a.value);
134 }
135
136
137
138
139
140
141
142 @Override
143 public Decimal64 multiply(final int n) {
144 return new Decimal64(n * this.value);
145 }
146
147
148
149
150
151
152
153
154
155 @Override
156 public Decimal64 divide(final Decimal64 a) {
157 return new Decimal64(this.value / a.value);
158 }
159
160
161
162
163
164
165
166
167 @Override
168 public Decimal64 reciprocal() {
169 return new Decimal64(1.0 / this.value);
170 }
171
172
173
174
175
176
177
178
179
180
181 @Override
182 public byte byteValue() {
183 return (byte) value;
184 }
185
186
187
188
189
190
191 @Override
192 public short shortValue() {
193 return (short) value;
194 }
195
196
197
198
199
200
201 @Override
202 public int intValue() {
203 return (int) value;
204 }
205
206
207
208
209
210
211 @Override
212 public long longValue() {
213 return (long) value;
214 }
215
216
217
218
219
220
221 @Override
222 public float floatValue() {
223 return (float) value;
224 }
225
226
227 @Override
228 public double doubleValue() {
229 return value;
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 @Override
246 public int compareTo(final Decimal64 o) {
247 return Double.compare(this.value, o.value);
248 }
249
250
251
252
253
254
255 @Override
256 public boolean equals(final Object obj) {
257 if (obj instanceof Decimal64) {
258 final Decimal64 that = (Decimal64) obj;
259 return Double.doubleToLongBits(this.value) == Double
260 .doubleToLongBits(that.value);
261 }
262 return false;
263 }
264
265
266
267
268
269
270
271
272
273 @Override
274 public int hashCode() {
275 long v = Double.doubleToLongBits(value);
276 return (int) (v ^ (v >>> 32));
277 }
278
279
280
281
282
283
284
285
286
287 @Override
288 public String toString() {
289 return Double.toString(value);
290 }
291
292
293
294
295
296
297
298
299
300
301
302 public boolean isInfinite() {
303 return Double.isInfinite(value);
304 }
305
306
307
308
309
310
311
312 public boolean isNaN() {
313 return Double.isNaN(value);
314 }
315
316
317
318
319 @Override
320 public double getReal() {
321 return value;
322 }
323
324
325
326
327 @Override
328 public Decimal64 add(final double a) {
329 return new Decimal64(value + a);
330 }
331
332
333
334
335 @Override
336 public Decimal64 subtract(final double a) {
337 return new Decimal64(value - a);
338 }
339
340
341
342
343 @Override
344 public Decimal64 multiply(final double a) {
345 return new Decimal64(value * a);
346 }
347
348
349
350
351 @Override
352 public Decimal64 divide(final double a) {
353 return new Decimal64(value / a);
354 }
355
356
357
358
359 @Override
360 public Decimal64 remainder(final double a) {
361 return new Decimal64(JdkMath.IEEEremainder(value, a));
362 }
363
364
365
366
367 @Override
368 public Decimal64 remainder(final Decimal64 a) {
369 return new Decimal64(JdkMath.IEEEremainder(value, a.value));
370 }
371
372
373
374
375 @Override
376 public Decimal64 abs() {
377 return new Decimal64(JdkMath.abs(value));
378 }
379
380
381
382
383 @Override
384 public Decimal64 ceil() {
385 return new Decimal64(JdkMath.ceil(value));
386 }
387
388
389
390
391 @Override
392 public Decimal64 floor() {
393 return new Decimal64(JdkMath.floor(value));
394 }
395
396
397
398
399 @Override
400 public Decimal64 rint() {
401 return new Decimal64(JdkMath.rint(value));
402 }
403
404
405
406
407 @Override
408 public long round() {
409 return JdkMath.round(value);
410 }
411
412
413
414
415 @Override
416 public Decimal64 signum() {
417 return new Decimal64(JdkMath.signum(value));
418 }
419
420
421
422
423 @Override
424 public Decimal64 copySign(final Decimal64 sign) {
425 return new Decimal64(JdkMath.copySign(value, sign.value));
426 }
427
428
429
430
431 @Override
432 public Decimal64 copySign(final double sign) {
433 return new Decimal64(JdkMath.copySign(value, sign));
434 }
435
436
437
438
439 @Override
440 public Decimal64 scalb(final int n) {
441 return new Decimal64(JdkMath.scalb(value, n));
442 }
443
444
445
446
447 @Override
448 public Decimal64 hypot(final Decimal64 y) {
449 return new Decimal64(JdkMath.hypot(value, y.value));
450 }
451
452
453
454
455 @Override
456 public Decimal64 sqrt() {
457 return new Decimal64(JdkMath.sqrt(value));
458 }
459
460
461
462
463 @Override
464 public Decimal64 cbrt() {
465 return new Decimal64(JdkMath.cbrt(value));
466 }
467
468
469
470
471 @Override
472 public Decimal64 rootN(final int n) {
473 if (value < 0) {
474 return new Decimal64(-JdkMath.pow(-value, 1.0 / n));
475 } else {
476 return new Decimal64(JdkMath.pow(value, 1.0 / n));
477 }
478 }
479
480
481
482
483 @Override
484 public Decimal64 pow(final double p) {
485 return new Decimal64(JdkMath.pow(value, p));
486 }
487
488
489
490
491 @Override
492 public Decimal64 pow(final int n) {
493 return new Decimal64(JdkMath.pow(value, n));
494 }
495
496
497
498
499 @Override
500 public Decimal64 pow(final Decimal64 e) {
501 return new Decimal64(JdkMath.pow(value, e.value));
502 }
503
504
505
506
507 @Override
508 public Decimal64 exp() {
509 return new Decimal64(JdkMath.exp(value));
510 }
511
512
513
514
515 @Override
516 public Decimal64 expm1() {
517 return new Decimal64(JdkMath.expm1(value));
518 }
519
520
521
522
523 @Override
524 public Decimal64 log() {
525 return new Decimal64(JdkMath.log(value));
526 }
527
528
529
530
531 @Override
532 public Decimal64 log1p() {
533 return new Decimal64(JdkMath.log1p(value));
534 }
535
536
537
538
539
540 @Override
541 public Decimal64 log10() {
542 return new Decimal64(JdkMath.log10(value));
543 }
544
545
546
547
548 @Override
549 public Decimal64 cos() {
550 return new Decimal64(JdkMath.cos(value));
551 }
552
553
554
555
556 @Override
557 public Decimal64 sin() {
558 return new Decimal64(JdkMath.sin(value));
559 }
560
561
562
563
564 @Override
565 public Decimal64 tan() {
566 return new Decimal64(JdkMath.tan(value));
567 }
568
569
570
571
572 @Override
573 public Decimal64 acos() {
574 return new Decimal64(JdkMath.acos(value));
575 }
576
577
578
579
580 @Override
581 public Decimal64 asin() {
582 return new Decimal64(JdkMath.asin(value));
583 }
584
585
586
587
588 @Override
589 public Decimal64 atan() {
590 return new Decimal64(JdkMath.atan(value));
591 }
592
593
594
595
596 @Override
597 public Decimal64 atan2(final Decimal64 x) {
598 return new Decimal64(JdkMath.atan2(value, x.value));
599 }
600
601
602
603
604 @Override
605 public Decimal64 cosh() {
606 return new Decimal64(JdkMath.cosh(value));
607 }
608
609
610
611
612 @Override
613 public Decimal64 sinh() {
614 return new Decimal64(JdkMath.sinh(value));
615 }
616
617
618
619
620 @Override
621 public Decimal64 tanh() {
622 return new Decimal64(JdkMath.tanh(value));
623 }
624
625
626
627
628 @Override
629 public Decimal64 acosh() {
630 return new Decimal64(JdkMath.acosh(value));
631 }
632
633
634
635
636 @Override
637 public Decimal64 asinh() {
638 return new Decimal64(JdkMath.asinh(value));
639 }
640
641
642
643
644 @Override
645 public Decimal64 atanh() {
646 return new Decimal64(JdkMath.atanh(value));
647 }
648
649
650
651
652 @Override
653 public Decimal64 linearCombination(final Decimal64[] a, final Decimal64[] b)
654 throws DimensionMismatchException {
655 if (a.length != b.length) {
656 throw new DimensionMismatchException(a.length, b.length);
657 }
658 final double[] aDouble = new double[a.length];
659 final double[] bDouble = new double[b.length];
660 for (int i = 0; i < a.length; ++i) {
661 aDouble[i] = a[i].value;
662 bDouble[i] = b[i].value;
663 }
664 return new Decimal64(Sum.ofProducts(aDouble, bDouble).getAsDouble());
665 }
666
667
668
669
670 @Override
671 public Decimal64 linearCombination(final double[] a, final Decimal64[] b)
672 throws DimensionMismatchException {
673 if (a.length != b.length) {
674 throw new DimensionMismatchException(a.length, b.length);
675 }
676 final double[] bDouble = new double[b.length];
677 for (int i = 0; i < a.length; ++i) {
678 bDouble[i] = b[i].value;
679 }
680 return new Decimal64(Sum.ofProducts(a, bDouble).getAsDouble());
681 }
682
683
684
685
686 @Override
687 public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
688 final Decimal64 a2, final Decimal64 b2) {
689 return new Decimal64(Sum.create()
690 .addProduct(a1.value, b1.value)
691 .addProduct(a2.value, b2.value).getAsDouble());
692 }
693
694
695
696
697 @Override
698 public Decimal64 linearCombination(final double a1, final Decimal64 b1,
699 final double a2, final Decimal64 b2) {
700 return new Decimal64(Sum.create()
701 .addProduct(a1, b1.value)
702 .addProduct(a2, b2.value).getAsDouble());
703 }
704
705
706
707
708 @Override
709 public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
710 final Decimal64 a2, final Decimal64 b2,
711 final Decimal64 a3, final Decimal64 b3) {
712 return new Decimal64(Sum.create()
713 .addProduct(a1.value, b1.value)
714 .addProduct(a2.value, b2.value)
715 .addProduct(a3.value, b3.value).getAsDouble());
716 }
717
718
719
720
721 @Override
722 public Decimal64 linearCombination(final double a1, final Decimal64 b1,
723 final double a2, final Decimal64 b2,
724 final double a3, final Decimal64 b3) {
725 return new Decimal64(Sum.create()
726 .addProduct(a1, b1.value)
727 .addProduct(a2, b2.value)
728 .addProduct(a3, b3.value).getAsDouble());
729 }
730
731
732
733
734 @Override
735 public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1,
736 final Decimal64 a2, final Decimal64 b2,
737 final Decimal64 a3, final Decimal64 b3,
738 final Decimal64 a4, final Decimal64 b4) {
739 return new Decimal64(Sum.create()
740 .addProduct(a1.value, b1.value)
741 .addProduct(a2.value, b2.value)
742 .addProduct(a3.value, b3.value)
743 .addProduct(a4.value, b4.value).getAsDouble());
744 }
745
746
747
748
749 @Override
750 public Decimal64 linearCombination(final double a1, final Decimal64 b1,
751 final double a2, final Decimal64 b2,
752 final double a3, final Decimal64 b3,
753 final double a4, final Decimal64 b4) {
754 return new Decimal64(Sum.create()
755 .addProduct(a1, b1.value)
756 .addProduct(a2, b2.value)
757 .addProduct(a3, b3.value)
758 .addProduct(a4, b4.value).getAsDouble());
759 }
760 }