1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.util;
18
19 import java.util.Arrays;
20
21 import org.hipparchus.CalculusFieldElement;
22 import org.hipparchus.Field;
23 import org.hipparchus.exception.LocalizedCoreFormats;
24 import org.hipparchus.exception.MathIllegalArgumentException;
25
26
27
28
29
30 public class Tuple implements CalculusFieldElement<Tuple> {
31
32
33 private final double[] values;
34
35
36 private final transient TupleField field;
37
38
39
40
41 public Tuple(final double... x) {
42 this(new TupleField(x.length), x.clone());
43 }
44
45
46
47
48
49 private Tuple(final TupleField field, final double[] x) {
50 this.values = x;
51 this.field = field;
52 }
53
54
55 @Override
56 public Tuple newInstance(final double value) {
57 final Tuple t = new Tuple(field, new double[values.length]);
58 Arrays.fill(t.values, value);
59 return t;
60 }
61
62
63
64
65 public int getDimension() {
66 return values.length;
67 }
68
69
70
71
72
73 public double getComponent(final int index) {
74 return values[index];
75 }
76
77
78
79
80 public double[] getComponents() {
81 return values.clone();
82 }
83
84
85 @Override
86 public Field<Tuple> getField() {
87 return field;
88 }
89
90
91 @Override
92 public Tuple add(final Tuple a) {
93 final Tuple result = new Tuple(field, new double[values.length]);
94 for (int i = 0; i < values.length; ++i) {
95 result.values[i] = values[i] + a.values[i];
96 }
97 return result;
98 }
99
100
101 @Override
102 public Tuple subtract(final Tuple a) {
103 final Tuple result = new Tuple(field, new double[values.length]);
104 for (int i = 0; i < values.length; ++i) {
105 result.values[i] = values[i] - a.values[i];
106 }
107 return result;
108 }
109
110
111 @Override
112 public Tuple negate() {
113 final Tuple result = new Tuple(field, new double[values.length]);
114 for (int i = 0; i < values.length; ++i) {
115 result.values[i] = -values[i];
116 }
117 return result;
118 }
119
120
121 @Override
122 public Tuple multiply(final Tuple a) {
123 final Tuple result = new Tuple(field, new double[values.length]);
124 for (int i = 0; i < values.length; ++i) {
125 result.values[i] = values[i] * a.values[i];
126 }
127 return result;
128 }
129
130
131 @Override
132 public Tuple multiply(final int n) {
133 final Tuple result = new Tuple(field, new double[values.length]);
134 for (int i = 0; i < values.length; ++i) {
135 result.values[i] = values[i] * n;
136 }
137 return result;
138 }
139
140
141 @Override
142 public Tuple square() {
143 return multiply(this);
144 }
145
146
147 @Override
148 public Tuple divide(final Tuple a) {
149 final Tuple result = new Tuple(field, new double[values.length]);
150 for (int i = 0; i < values.length; ++i) {
151 result.values[i] = values[i] / a.values[i];
152 }
153 return result;
154 }
155
156
157 @Override
158 public Tuple reciprocal() {
159 final Tuple result = new Tuple(field, new double[values.length]);
160 for (int i = 0; i < values.length; ++i) {
161 result.values[i] = 1.0 / values[i];
162 }
163 return result;
164 }
165
166
167 @Override
168 public boolean equals(final Object obj) {
169 if (obj instanceof Tuple) {
170 final Tuple that = (Tuple) obj;
171 if (getDimension() == that.getDimension()) {
172 boolean equals = true;
173 for (int i = 0; i < values.length; ++i) {
174 equals &= Double.doubleToRawLongBits(values[i]) == Double.doubleToRawLongBits(that.values[i]);
175 }
176 return equals;
177 }
178 }
179 return false;
180 }
181
182
183 @Override
184 public int hashCode() {
185 return 0x34b1a444 + Arrays.hashCode(values);
186 }
187
188
189 @Override
190 public double getReal() {
191 return values[0];
192 }
193
194
195 @Override
196 public Tuple getAddendum() {
197 final double[] addendum = values.clone();
198 addendum[0] = 0;
199 for (int i = 1; i < addendum.length; ++i) {
200 addendum[i] -= values[0];
201 }
202 return new Tuple(field, addendum);
203 }
204
205
206 @Override
207 public Tuple add(final double a) {
208 final Tuple result = new Tuple(field, new double[values.length]);
209 for (int i = 0; i < values.length; ++i) {
210 result.values[i] = values[i] + a;
211 }
212 return result;
213 }
214
215
216 @Override
217 public Tuple subtract(final double a) {
218 final Tuple result = new Tuple(field, new double[values.length]);
219 for (int i = 0; i < values.length; ++i) {
220 result.values[i] = values[i] - a;
221 }
222 return result;
223 }
224
225
226 @Override
227 public Tuple multiply(final double a) {
228 final Tuple result = new Tuple(field, new double[values.length]);
229 for (int i = 0; i < values.length; ++i) {
230 result.values[i] = values[i] * a;
231 }
232 return result;
233 }
234
235
236 @Override
237 public Tuple divide(final double a) {
238 final Tuple result = new Tuple(field, new double[values.length]);
239 for (int i = 0; i < values.length; ++i) {
240 result.values[i] = values[i] / a;
241 }
242 return result;
243 }
244
245
246 @Override
247 public Tuple remainder(final double a) {
248 final Tuple result = new Tuple(field, new double[values.length]);
249 for (int i = 0; i < values.length; ++i) {
250 result.values[i] = FastMath.IEEEremainder(values[i], a);
251 }
252 return result;
253 }
254
255
256 @Override
257 public Tuple remainder(final Tuple a) {
258 final Tuple result = new Tuple(field, new double[values.length]);
259 for (int i = 0; i < values.length; ++i) {
260 result.values[i] = FastMath.IEEEremainder(values[i], a.values[i]);
261 }
262 return result;
263 }
264
265
266 @Override
267 public Tuple abs() {
268 final Tuple result = new Tuple(field, new double[values.length]);
269 for (int i = 0; i < values.length; ++i) {
270 result.values[i] = FastMath.abs(values[i]);
271 }
272 return result;
273 }
274
275
276 @Override
277 public Tuple ceil() {
278 final Tuple result = new Tuple(field, new double[values.length]);
279 for (int i = 0; i < values.length; ++i) {
280 result.values[i] = FastMath.ceil(values[i]);
281 }
282 return result;
283 }
284
285
286 @Override
287 public Tuple floor() {
288 final Tuple result = new Tuple(field, new double[values.length]);
289 for (int i = 0; i < values.length; ++i) {
290 result.values[i] = FastMath.floor(values[i]);
291 }
292 return result;
293 }
294
295
296 @Override
297 public Tuple rint() {
298 final Tuple result = new Tuple(field, new double[values.length]);
299 for (int i = 0; i < values.length; ++i) {
300 result.values[i] = FastMath.rint(values[i]);
301 }
302 return result;
303 }
304
305
306 @Override
307 public Tuple sign() {
308 final Tuple result = new Tuple(field, new double[values.length]);
309 for (int i = 0; i < values.length; ++i) {
310 result.values[i] = FastMath.signum(values[i]);
311 }
312 return result;
313 }
314
315
316 @Override
317 public Tuple copySign(final Tuple sign) {
318 final Tuple result = new Tuple(field, new double[values.length]);
319 for (int i = 0; i < values.length; ++i) {
320 result.values[i] = FastMath.copySign(values[i], sign.values[i]);
321 }
322 return result;
323 }
324
325
326 @Override
327 public Tuple copySign(final double sign) {
328 final Tuple result = new Tuple(field, new double[values.length]);
329 for (int i = 0; i < values.length; ++i) {
330 result.values[i] = FastMath.copySign(values[i], sign);
331 }
332 return result;
333 }
334
335
336 @Override
337 public Tuple scalb(final int n) {
338 final Tuple result = new Tuple(field, new double[values.length]);
339 for (int i = 0; i < values.length; ++i) {
340 result.values[i] = FastMath.scalb(values[i], n);
341 }
342 return result;
343 }
344
345
346 @Override
347 public Tuple ulp() {
348 final Tuple result = new Tuple(field, new double[values.length]);
349 for (int i = 0; i < values.length; ++i) {
350 result.values[i] = FastMath.ulp(values[i]);
351 }
352 return result;
353 }
354
355
356 @Override
357 public Tuple hypot(final Tuple y) {
358 final Tuple result = new Tuple(field, new double[values.length]);
359 for (int i = 0; i < values.length; ++i) {
360 result.values[i] = FastMath.hypot(values[i], y.values[i]);
361 }
362 return result;
363 }
364
365
366 @Override
367 public Tuple sqrt() {
368 final Tuple result = new Tuple(field, new double[values.length]);
369 for (int i = 0; i < values.length; ++i) {
370 result.values[i] = FastMath.sqrt(values[i]);
371 }
372 return result;
373 }
374
375
376 @Override
377 public Tuple cbrt() {
378 final Tuple result = new Tuple(field, new double[values.length]);
379 for (int i = 0; i < values.length; ++i) {
380 result.values[i] = FastMath.cbrt(values[i]);
381 }
382 return result;
383 }
384
385
386 @Override
387 public Tuple rootN(final int n) {
388 final Tuple result = new Tuple(field, new double[values.length]);
389 for (int i = 0; i < values.length; ++i) {
390 if (values[i] < 0) {
391 result.values[i] = -FastMath.pow(-values[i], 1.0 / n);
392 } else {
393 result.values[i] = FastMath.pow(values[i], 1.0 / n);
394 }
395 }
396 return result;
397 }
398
399
400 @Override
401 public Tuple pow(final double p) {
402 final Tuple result = new Tuple(field, new double[values.length]);
403 for (int i = 0; i < values.length; ++i) {
404 result.values[i] = FastMath.pow(values[i], p);
405 }
406 return result;
407 }
408
409
410 @Override
411 public Tuple pow(final int n) {
412 final Tuple result = new Tuple(field, new double[values.length]);
413 for (int i = 0; i < values.length; ++i) {
414 result.values[i] = FastMath.pow(values[i], n);
415 }
416 return result;
417 }
418
419
420 @Override
421 public Tuple pow(final Tuple e) {
422 final Tuple result = new Tuple(field, new double[values.length]);
423 for (int i = 0; i < values.length; ++i) {
424 result.values[i] = FastMath.pow(values[i], e.values[i]);
425 }
426 return result;
427 }
428
429
430 @Override
431 public Tuple exp() {
432 final Tuple result = new Tuple(field, new double[values.length]);
433 for (int i = 0; i < values.length; ++i) {
434 result.values[i] = FastMath.exp(values[i]);
435 }
436 return result;
437 }
438
439
440 @Override
441 public Tuple expm1() {
442 final Tuple result = new Tuple(field, new double[values.length]);
443 for (int i = 0; i < values.length; ++i) {
444 result.values[i] = FastMath.expm1(values[i]);
445 }
446 return result;
447 }
448
449
450 @Override
451 public Tuple log() {
452 final Tuple result = new Tuple(field, new double[values.length]);
453 for (int i = 0; i < values.length; ++i) {
454 result.values[i] = FastMath.log(values[i]);
455 }
456 return result;
457 }
458
459
460 @Override
461 public Tuple log1p() {
462 final Tuple result = new Tuple(field, new double[values.length]);
463 for (int i = 0; i < values.length; ++i) {
464 result.values[i] = FastMath.log1p(values[i]);
465 }
466 return result;
467 }
468
469
470 @Override
471 public Tuple log10() {
472 final Tuple result = new Tuple(field, new double[values.length]);
473 for (int i = 0; i < values.length; ++i) {
474 result.values[i] = FastMath.log10(values[i]);
475 }
476 return result;
477 }
478
479
480 @Override
481 public Tuple cos() {
482 final Tuple result = new Tuple(field, new double[values.length]);
483 for (int i = 0; i < values.length; ++i) {
484 result.values[i] = FastMath.cos(values[i]);
485 }
486 return result;
487 }
488
489
490 @Override
491 public Tuple sin() {
492 final Tuple result = new Tuple(field, new double[values.length]);
493 for (int i = 0; i < values.length; ++i) {
494 result.values[i] = FastMath.sin(values[i]);
495 }
496 return result;
497 }
498
499
500 @Override
501 public FieldSinCos<Tuple> sinCos() {
502 final Tuple sin = new Tuple(field, new double[values.length]);
503 final Tuple cos = new Tuple(field, new double[values.length]);
504 for (int i = 0; i < values.length; ++i) {
505 final SinCos sc = FastMath.sinCos(values[i]);
506 sin.values[i] = sc.sin();
507 cos.values[i] = sc.cos();
508 }
509 return new FieldSinCos<>(sin, cos);
510 }
511
512
513 @Override
514 public Tuple tan() {
515 final Tuple result = new Tuple(field, new double[values.length]);
516 for (int i = 0; i < values.length; ++i) {
517 result.values[i] = FastMath.tan(values[i]);
518 }
519 return result;
520 }
521
522
523 @Override
524 public Tuple acos() {
525 final Tuple result = new Tuple(field, new double[values.length]);
526 for (int i = 0; i < values.length; ++i) {
527 result.values[i] = FastMath.acos(values[i]);
528 }
529 return result;
530 }
531
532
533 @Override
534 public Tuple asin() {
535 final Tuple result = new Tuple(field, new double[values.length]);
536 for (int i = 0; i < values.length; ++i) {
537 result.values[i] = FastMath.asin(values[i]);
538 }
539 return result;
540 }
541
542
543 @Override
544 public Tuple atan() {
545 final Tuple result = new Tuple(field, new double[values.length]);
546 for (int i = 0; i < values.length; ++i) {
547 result.values[i] = FastMath.atan(values[i]);
548 }
549 return result;
550 }
551
552
553 @Override
554 public Tuple atan2(final Tuple x) {
555 final Tuple result = new Tuple(field, new double[values.length]);
556 for (int i = 0; i < values.length; ++i) {
557 result.values[i] = FastMath.atan2(values[i], x.values[i]);
558 }
559 return result;
560 }
561
562
563 @Override
564 public Tuple cosh() {
565 final Tuple result = new Tuple(field, new double[values.length]);
566 for (int i = 0; i < values.length; ++i) {
567 result.values[i] = FastMath.cosh(values[i]);
568 }
569 return result;
570 }
571
572
573 @Override
574 public Tuple sinh() {
575 final Tuple result = new Tuple(field, new double[values.length]);
576 for (int i = 0; i < values.length; ++i) {
577 result.values[i] = FastMath.sinh(values[i]);
578 }
579 return result;
580 }
581
582
583 @Override
584 public FieldSinhCosh<Tuple> sinhCosh() {
585 final Tuple sinh = new Tuple(field, new double[values.length]);
586 final Tuple cosh = new Tuple(field, new double[values.length]);
587 for (int i = 0; i < values.length; ++i) {
588 final SinhCosh sch = FastMath.sinhCosh(values[i]);
589 sinh.values[i] = sch.sinh();
590 cosh.values[i] = sch.cosh();
591 }
592 return new FieldSinhCosh<>(sinh, cosh);
593 }
594
595
596 @Override
597 public Tuple tanh() {
598 final Tuple result = new Tuple(field, new double[values.length]);
599 for (int i = 0; i < values.length; ++i) {
600 result.values[i] = FastMath.tanh(values[i]);
601 }
602 return result;
603 }
604
605
606 @Override
607 public Tuple acosh() {
608 final Tuple result = new Tuple(field, new double[values.length]);
609 for (int i = 0; i < values.length; ++i) {
610 result.values[i] = FastMath.acosh(values[i]);
611 }
612 return result;
613 }
614
615
616 @Override
617 public Tuple asinh() {
618 final Tuple result = new Tuple(field, new double[values.length]);
619 for (int i = 0; i < values.length; ++i) {
620 result.values[i] = FastMath.asinh(values[i]);
621 }
622 return result;
623 }
624
625
626 @Override
627 public Tuple atanh() {
628 final Tuple result = new Tuple(field, new double[values.length]);
629 for (int i = 0; i < values.length; ++i) {
630 result.values[i] = FastMath.atanh(values[i]);
631 }
632 return result;
633 }
634
635
636 @Override
637 public Tuple toDegrees() {
638 final Tuple result = new Tuple(field, new double[values.length]);
639 for (int i = 0; i < values.length; ++i) {
640 result.values[i] = FastMath.toDegrees(values[i]);
641 }
642 return result;
643 }
644
645
646 @Override
647 public Tuple toRadians() {
648 final Tuple result = new Tuple(field, new double[values.length]);
649 for (int i = 0; i < values.length; ++i) {
650 result.values[i] = FastMath.toRadians(values[i]);
651 }
652 return result;
653 }
654
655
656 @Override
657 public Tuple linearCombination(final Tuple[] a, final Tuple[] b)
658 throws MathIllegalArgumentException {
659 final Tuple result = new Tuple(field, new double[values.length]);
660 MathUtils.checkDimension(a.length, b.length);
661 final double[] aDouble = new double[a.length];
662 final double[] bDouble = new double[b.length];
663 for (int i = 0; i < values.length; ++i) {
664 for (int j = 0; j < a.length; ++j) {
665 aDouble[j] = a[j].values[i];
666 bDouble[j] = b[j].values[i];
667 }
668 result.values[i] = MathArrays.linearCombination(aDouble, bDouble);
669 }
670 return result;
671 }
672
673
674 @Override
675 public Tuple linearCombination(final double[] a, final Tuple[] b)
676 throws MathIllegalArgumentException {
677 final Tuple result = new Tuple(field, new double[values.length]);
678 MathUtils.checkDimension(a.length, b.length);
679 final double[] bDouble = new double[b.length];
680 for (int i = 0; i < values.length; ++i) {
681 for (int j = 0; j < a.length; ++j) {
682 bDouble[j] = b[j].values[i];
683 }
684 result.values[i] = MathArrays.linearCombination(a, bDouble);
685 }
686 return result;
687 }
688
689
690 @Override
691 public Tuple linearCombination(final Tuple a1, final Tuple b1,
692 final Tuple a2, final Tuple b2) {
693 final Tuple result = new Tuple(field, new double[values.length]);
694 for (int i = 0; i < values.length; ++i) {
695 result.values[i] = MathArrays.linearCombination(a1.values[i], b1.values[i],
696 a2.values[i], b2.values[i]);
697 }
698 return result;
699 }
700
701
702 @Override
703 public Tuple linearCombination(final double a1, final Tuple b1,
704 final double a2, final Tuple b2) {
705 final Tuple result = new Tuple(field, new double[values.length]);
706 for (int i = 0; i < values.length; ++i) {
707 result.values[i] = MathArrays.linearCombination(a1, b1.values[i],
708 a2, b2.values[i]);
709 }
710 return result;
711 }
712
713
714 @Override
715 public Tuple linearCombination(final Tuple a1, final Tuple b1,
716 final Tuple a2, final Tuple b2,
717 final Tuple a3, final Tuple b3) {
718 final Tuple result = new Tuple(field, new double[values.length]);
719 for (int i = 0; i < values.length; ++i) {
720 result.values[i] = MathArrays.linearCombination(a1.values[i], b1.values[i],
721 a2.values[i], b2.values[i],
722 a3.values[i], b3.values[i]);
723 }
724 return result;
725 }
726
727
728 @Override
729 public Tuple linearCombination(final double a1, final Tuple b1,
730 final double a2, final Tuple b2,
731 final double a3, final Tuple b3) {
732 final Tuple result = new Tuple(field, new double[values.length]);
733 for (int i = 0; i < values.length; ++i) {
734 result.values[i] = MathArrays.linearCombination(a1, b1.values[i],
735 a2, b2.values[i],
736 a3, b3.values[i]);
737 }
738 return result;
739 }
740
741
742 @Override
743 public Tuple linearCombination(final Tuple a1, final Tuple b1,
744 final Tuple a2, final Tuple b2,
745 final Tuple a3, final Tuple b3,
746 final Tuple a4, final Tuple b4) {
747 final Tuple result = new Tuple(field, new double[values.length]);
748 for (int i = 0; i < values.length; ++i) {
749 result.values[i] = MathArrays.linearCombination(a1.values[i], b1.values[i],
750 a2.values[i], b2.values[i],
751 a3.values[i], b3.values[i],
752 a4.values[i], b4.values[i]);
753 }
754 return result;
755 }
756
757
758 @Override
759 public Tuple linearCombination(final double a1, final Tuple b1,
760 final double a2, final Tuple b2,
761 final double a3, final Tuple b3,
762 final double a4, final Tuple b4) {
763 final Tuple result = new Tuple(field, new double[values.length]);
764 for (int i = 0; i < values.length; ++i) {
765 result.values[i] = MathArrays.linearCombination(a1, b1.values[i],
766 a2, b2.values[i],
767 a3, b3.values[i],
768 a4, b4.values[i]);
769 }
770 return result;
771 }
772
773
774 @Override
775 public double norm() {
776 double sum = 0;
777 for (final double value : values) {
778 sum += FastMath.abs(value);
779 }
780 return sum;
781 }
782
783
784 @Override
785 public Tuple getPi() {
786 final Tuple result = new Tuple(field, new double[values.length]);
787 Arrays.fill(result.values, FastMath.PI);
788 return result;
789 }
790
791
792 @Override
793 public boolean isSmall(final Tuple base, final double relativeThreshold) {
794 if (values.length != base.values.length) {
795 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
796 values.length, base.values.length);
797 }
798 boolean ok = true;
799 for (int i = 0; i < values.length && ok; ++i) {
800 ok = FastMath.abs(values[i]) <= FastMath.abs(base.values[i] * relativeThreshold);
801 }
802 return ok;
803 }
804
805
806
807 private static class TupleField implements Field<Tuple> {
808
809
810 private final Tuple zero;
811
812
813 private final Tuple one;
814
815
816
817
818 TupleField(final int dimension) {
819 final double[] zeroData = new double[dimension];
820 final double[] oneData = new double[dimension];
821 Arrays.fill(oneData, 1.0);
822 this.zero = new Tuple(this, zeroData);
823 this.one = new Tuple(this, oneData);
824 }
825
826
827 @Override
828 public Tuple getZero() {
829 return zero;
830 }
831
832
833 @Override
834 public Tuple getOne() {
835 return one;
836 }
837
838
839 @Override
840 public Class<Tuple> getRuntimeClass() {
841 return Tuple.class;
842 }
843
844
845 @Override
846 public boolean equals(final Object other) {
847 if (other instanceof TupleField) {
848 return zero.getDimension() == ((TupleField) other).zero.getDimension();
849 } else {
850 return false;
851 }
852 }
853
854
855 @Override
856 public int hashCode() {
857 return 0x6672493d ^ zero.getDimension();
858 }
859
860 }
861
862 }