1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.linear;
23
24 import java.io.Serializable;
25 import java.util.Arrays;
26
27 import org.hipparchus.Field;
28 import org.hipparchus.FieldElement;
29 import org.hipparchus.exception.LocalizedCoreFormats;
30 import org.hipparchus.exception.MathIllegalArgumentException;
31 import org.hipparchus.exception.MathRuntimeException;
32 import org.hipparchus.exception.NullArgumentException;
33 import org.hipparchus.util.MathArrays;
34 import org.hipparchus.util.MathUtils;
35
36
37
38
39
40 public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<T>, Serializable {
41
42 private static final long serialVersionUID = 7648186910365927050L;
43
44
45 private T[] data;
46
47
48 private final Field<T> field;
49
50
51
52
53
54
55
56
57
58
59
60 public ArrayFieldVector(final Field<T> field) {
61 this(field, 0);
62 }
63
64
65
66
67
68
69
70 public ArrayFieldVector(Field<T> field, int size) {
71 this.field = field;
72 this.data = MathArrays.buildArray(field, size);
73 }
74
75
76
77
78
79
80
81 public ArrayFieldVector(int size, T preset) {
82 this(preset.getField(), size);
83 Arrays.fill(data, preset);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public ArrayFieldVector(T[] d)
99 throws MathIllegalArgumentException, NullArgumentException {
100 MathUtils.checkNotNull(d);
101 try {
102 field = d[0].getField();
103 data = d.clone();
104 } catch (ArrayIndexOutOfBoundsException e) {
105 throw new MathIllegalArgumentException(e, LocalizedCoreFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
106 }
107 }
108
109
110
111
112
113
114
115
116
117 public ArrayFieldVector(Field<T> field, T[] d)
118 throws NullArgumentException {
119 MathUtils.checkNotNull(d);
120 this.field = field;
121 data = d.clone();
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public ArrayFieldVector(T[] d, boolean copyArray)
146 throws MathIllegalArgumentException, NullArgumentException {
147 MathUtils.checkNotNull(d);
148 if (d.length == 0) {
149 throw new MathIllegalArgumentException(LocalizedCoreFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
150 }
151 field = d[0].getField();
152 data = copyArray ? d.clone() : d;
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public ArrayFieldVector(Field<T> field, T[] d, boolean copyArray)
171 throws NullArgumentException {
172 MathUtils.checkNotNull(d);
173 this.field = field;
174 data = copyArray ? d.clone() : d;
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public ArrayFieldVector(T[] d, int pos, int size)
188 throws MathIllegalArgumentException, NullArgumentException {
189 MathUtils.checkNotNull(d);
190 if (d.length < pos + size) {
191 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
192 pos + size, d.length);
193 }
194 field = d[0].getField();
195 data = MathArrays.buildArray(field, size);
196 System.arraycopy(d, pos, data, 0, size);
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 public ArrayFieldVector(Field<T> field, T[] d, int pos, int size)
211 throws MathIllegalArgumentException, NullArgumentException {
212 MathUtils.checkNotNull(d);
213 if (d.length < pos + size) {
214 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
215 pos + size, d.length);
216 }
217 this.field = field;
218 data = MathArrays.buildArray(field, size);
219 System.arraycopy(d, pos, data, 0, size);
220 }
221
222
223
224
225
226
227
228 public ArrayFieldVector(FieldVector<T> v)
229 throws NullArgumentException {
230 MathUtils.checkNotNull(v);
231 field = v.getField();
232 data = MathArrays.buildArray(field, v.getDimension());
233 for (int i = 0; i < data.length; ++i) {
234 data[i] = v.getEntry(i);
235 }
236 }
237
238
239
240
241
242
243
244 public ArrayFieldVector(ArrayFieldVector<T> v)
245 throws NullArgumentException {
246 MathUtils.checkNotNull(v);
247 field = v.getField();
248 data = v.data.clone();
249 }
250
251
252
253
254
255
256
257
258
259 public ArrayFieldVector(ArrayFieldVector<T> v, boolean deep)
260 throws NullArgumentException {
261 MathUtils.checkNotNull(v);
262 field = v.getField();
263 data = deep ? v.data.clone() : v.data;
264 }
265
266
267
268
269
270
271
272
273
274 public ArrayFieldVector(FieldVector<T> v1, FieldVector<T> v2)
275 throws NullArgumentException {
276 MathUtils.checkNotNull(v1);
277 MathUtils.checkNotNull(v2);
278 field = v1.getField();
279 final T[] v1Data =
280 (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector<T>) v1).data : v1.toArray();
281 final T[] v2Data =
282 (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector<T>) v2).data : v2.toArray();
283 data = MathArrays.buildArray(field, v1Data.length + v2Data.length);
284 System.arraycopy(v1Data, 0, data, 0, v1Data.length);
285 System.arraycopy(v2Data, 0, data, v1Data.length, v2Data.length);
286 }
287
288
289
290
291
292
293
294
295
296 public ArrayFieldVector(FieldVector<T> v1, T[] v2)
297 throws NullArgumentException {
298 MathUtils.checkNotNull(v1);
299 MathUtils.checkNotNull(v2);
300 field = v1.getField();
301 final T[] v1Data =
302 (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector<T>) v1).data : v1.toArray();
303 data = MathArrays.buildArray(field, v1Data.length + v2.length);
304 System.arraycopy(v1Data, 0, data, 0, v1Data.length);
305 System.arraycopy(v2, 0, data, v1Data.length, v2.length);
306 }
307
308
309
310
311
312
313
314
315
316 public ArrayFieldVector(T[] v1, FieldVector<T> v2)
317 throws NullArgumentException {
318 MathUtils.checkNotNull(v1);
319 MathUtils.checkNotNull(v2);
320 field = v2.getField();
321 final T[] v2Data =
322 (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector<T>) v2).data : v2.toArray();
323 data = MathArrays.buildArray(field, v1.length + v2Data.length);
324 System.arraycopy(v1, 0, data, 0, v1.length);
325 System.arraycopy(v2Data, 0, data, v1.length, v2Data.length);
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 public ArrayFieldVector(T[] v1, T[] v2)
344 throws MathIllegalArgumentException, NullArgumentException {
345 MathUtils.checkNotNull(v1);
346 MathUtils.checkNotNull(v2);
347 if (v1.length + v2.length == 0) {
348 throw new MathIllegalArgumentException(LocalizedCoreFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
349 }
350 data = MathArrays.buildArray(v1[0].getField(), v1.length + v2.length);
351 System.arraycopy(v1, 0, data, 0, v1.length);
352 System.arraycopy(v2, 0, data, v1.length, v2.length);
353 field = data[0].getField();
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367 public ArrayFieldVector(Field<T> field, T[] v1, T[] v2)
368 throws MathIllegalArgumentException, NullArgumentException {
369 MathUtils.checkNotNull(v1);
370 MathUtils.checkNotNull(v2);
371 if (v1.length + v2.length == 0) {
372 throw new MathIllegalArgumentException(LocalizedCoreFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
373 }
374 data = MathArrays.buildArray(field, v1.length + v2.length);
375 System.arraycopy(v1, 0, data, 0, v1.length);
376 System.arraycopy(v2, 0, data, v1.length, v2.length);
377 this.field = field;
378 }
379
380
381 @Override
382 public Field<T> getField() {
383 return field;
384 }
385
386
387 @Override
388 public FieldVector<T> copy() {
389 return new ArrayFieldVector<T>(this, true);
390 }
391
392
393 @Override
394 public FieldVector<T> add(FieldVector<T> v)
395 throws MathIllegalArgumentException {
396 if (v instanceof ArrayFieldVector) {
397 return add((ArrayFieldVector<T>) v);
398 } else {
399 checkVectorDimensions(v);
400 T[] out = MathArrays.buildArray(field, data.length);
401 for (int i = 0; i < data.length; i++) {
402 out[i] = data[i].add(v.getEntry(i));
403 }
404 return new ArrayFieldVector<T>(field, out, false);
405 }
406 }
407
408
409
410
411
412
413
414
415 public ArrayFieldVector<T> add(ArrayFieldVector<T> v)
416 throws MathIllegalArgumentException {
417 checkVectorDimensions(v.data.length);
418 T[] out = MathArrays.buildArray(field, data.length);
419 for (int i = 0; i < data.length; i++) {
420 out[i] = data[i].add(v.data[i]);
421 }
422 return new ArrayFieldVector<T>(field, out, false);
423 }
424
425
426 @Override
427 public FieldVector<T> subtract(FieldVector<T> v)
428 throws MathIllegalArgumentException {
429 if (v instanceof ArrayFieldVector) {
430 return subtract((ArrayFieldVector<T>) v);
431 } else {
432 checkVectorDimensions(v);
433 T[] out = MathArrays.buildArray(field, data.length);
434 for (int i = 0; i < data.length; i++) {
435 out[i] = data[i].subtract(v.getEntry(i));
436 }
437 return new ArrayFieldVector<T>(field, out, false);
438 }
439 }
440
441
442
443
444
445
446
447
448 public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v)
449 throws MathIllegalArgumentException {
450 checkVectorDimensions(v.data.length);
451 T[] out = MathArrays.buildArray(field, data.length);
452 for (int i = 0; i < data.length; i++) {
453 out[i] = data[i].subtract(v.data[i]);
454 }
455 return new ArrayFieldVector<T>(field, out, false);
456 }
457
458
459 @Override
460 public FieldVector<T> mapAdd(T d) throws NullArgumentException {
461 T[] out = MathArrays.buildArray(field, data.length);
462 for (int i = 0; i < data.length; i++) {
463 out[i] = data[i].add(d);
464 }
465 return new ArrayFieldVector<T>(field, out, false);
466 }
467
468
469 @Override
470 public FieldVector<T> mapAddToSelf(T d) throws NullArgumentException {
471 for (int i = 0; i < data.length; i++) {
472 data[i] = data[i].add(d);
473 }
474 return this;
475 }
476
477
478 @Override
479 public FieldVector<T> mapSubtract(T d) throws NullArgumentException {
480 T[] out = MathArrays.buildArray(field, data.length);
481 for (int i = 0; i < data.length; i++) {
482 out[i] = data[i].subtract(d);
483 }
484 return new ArrayFieldVector<T>(field, out, false);
485 }
486
487
488 @Override
489 public FieldVector<T> mapSubtractToSelf(T d) throws NullArgumentException {
490 for (int i = 0; i < data.length; i++) {
491 data[i] = data[i].subtract(d);
492 }
493 return this;
494 }
495
496
497 @Override
498 public FieldVector<T> mapMultiply(T d) throws NullArgumentException {
499 T[] out = MathArrays.buildArray(field, data.length);
500 for (int i = 0; i < data.length; i++) {
501 out[i] = data[i].multiply(d);
502 }
503 return new ArrayFieldVector<T>(field, out, false);
504 }
505
506
507 @Override
508 public FieldVector<T> mapMultiplyToSelf(T d) throws NullArgumentException {
509 for (int i = 0; i < data.length; i++) {
510 data[i] = data[i].multiply(d);
511 }
512 return this;
513 }
514
515
516 @Override
517 public FieldVector<T> mapDivide(T d)
518 throws NullArgumentException, MathRuntimeException {
519 MathUtils.checkNotNull(d);
520 T[] out = MathArrays.buildArray(field, data.length);
521 for (int i = 0; i < data.length; i++) {
522 out[i] = data[i].divide(d);
523 }
524 return new ArrayFieldVector<T>(field, out, false);
525 }
526
527
528 @Override
529 public FieldVector<T> mapDivideToSelf(T d)
530 throws NullArgumentException, MathRuntimeException {
531 MathUtils.checkNotNull(d);
532 for (int i = 0; i < data.length; i++) {
533 data[i] = data[i].divide(d);
534 }
535 return this;
536 }
537
538
539 @Override
540 public FieldVector<T> mapInv() throws MathRuntimeException {
541 T[] out = MathArrays.buildArray(field, data.length);
542 final T one = field.getOne();
543 for (int i = 0; i < data.length; i++) {
544 try {
545 out[i] = one.divide(data[i]);
546 } catch (final MathRuntimeException e) {
547 throw new MathRuntimeException(e, LocalizedCoreFormats.INDEX, i);
548 }
549 }
550 return new ArrayFieldVector<T>(field, out, false);
551 }
552
553
554 @Override
555 public FieldVector<T> mapInvToSelf() throws MathRuntimeException {
556 final T one = field.getOne();
557 for (int i = 0; i < data.length; i++) {
558 try {
559 data[i] = one.divide(data[i]);
560 } catch (final MathRuntimeException e) {
561 throw new MathRuntimeException(e, LocalizedCoreFormats.INDEX, i);
562 }
563 }
564 return this;
565 }
566
567
568 @Override
569 public FieldVector<T> ebeMultiply(FieldVector<T> v)
570 throws MathIllegalArgumentException {
571 if (v instanceof ArrayFieldVector) {
572 return ebeMultiply((ArrayFieldVector<T>) v);
573 } else {
574 checkVectorDimensions(v);
575 T[] out = MathArrays.buildArray(field, data.length);
576 for (int i = 0; i < data.length; i++) {
577 out[i] = data[i].multiply(v.getEntry(i));
578 }
579 return new ArrayFieldVector<T>(field, out, false);
580 }
581 }
582
583
584
585
586
587
588
589
590 public ArrayFieldVector<T> ebeMultiply(ArrayFieldVector<T> v)
591 throws MathIllegalArgumentException {
592 checkVectorDimensions(v.data.length);
593 T[] out = MathArrays.buildArray(field, data.length);
594 for (int i = 0; i < data.length; i++) {
595 out[i] = data[i].multiply(v.data[i]);
596 }
597 return new ArrayFieldVector<T>(field, out, false);
598 }
599
600
601 @Override
602 public FieldVector<T> ebeDivide(FieldVector<T> v)
603 throws MathIllegalArgumentException, MathRuntimeException {
604 if (v instanceof ArrayFieldVector) {
605 return ebeDivide((ArrayFieldVector<T>) v);
606 } else {
607 checkVectorDimensions(v);
608 T[] out = MathArrays.buildArray(field, data.length);
609 for (int i = 0; i < data.length; i++) {
610 try {
611 out[i] = data[i].divide(v.getEntry(i));
612 } catch (final MathRuntimeException e) {
613 throw new MathRuntimeException(e, LocalizedCoreFormats.INDEX, i);
614 }
615 }
616 return new ArrayFieldVector<T>(field, out, false);
617 }
618 }
619
620
621
622
623
624
625
626
627
628 public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v)
629 throws MathIllegalArgumentException, MathRuntimeException {
630 checkVectorDimensions(v.data.length);
631 T[] out = MathArrays.buildArray(field, data.length);
632 for (int i = 0; i < data.length; i++) {
633 try {
634 out[i] = data[i].divide(v.data[i]);
635 } catch (final MathRuntimeException e) {
636 throw new MathRuntimeException(e, LocalizedCoreFormats.INDEX, i);
637 }
638 }
639 return new ArrayFieldVector<T>(field, out, false);
640 }
641
642
643
644
645
646
647 public T[] getDataRef() {
648 return data;
649 }
650
651
652 @Override
653 public T dotProduct(FieldVector<T> v)
654 throws MathIllegalArgumentException {
655 if (v instanceof ArrayFieldVector) {
656 return dotProduct((ArrayFieldVector<T>) v);
657 } else {
658 checkVectorDimensions(v);
659 T dot = field.getZero();
660 for (int i = 0; i < data.length; i++) {
661 dot = dot.add(data[i].multiply(v.getEntry(i)));
662 }
663 return dot;
664 }
665 }
666
667
668
669
670
671
672
673
674 public T dotProduct(ArrayFieldVector<T> v)
675 throws MathIllegalArgumentException {
676 checkVectorDimensions(v.data.length);
677 T dot = field.getZero();
678 for (int i = 0; i < data.length; i++) {
679 dot = dot.add(data[i].multiply(v.data[i]));
680 }
681 return dot;
682 }
683
684
685 @Override
686 public FieldVector<T> projection(FieldVector<T> v)
687 throws MathIllegalArgumentException, MathRuntimeException {
688 return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
689 }
690
691
692
693
694
695
696
697
698 public ArrayFieldVector<T> projection(ArrayFieldVector<T> v)
699 throws MathIllegalArgumentException, MathRuntimeException {
700 return (ArrayFieldVector<T>) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v)));
701 }
702
703
704 @Override
705 public FieldMatrix<T> outerProduct(FieldVector<T> v) {
706 if (v instanceof ArrayFieldVector) {
707 return outerProduct((ArrayFieldVector<T>) v);
708 } else {
709 final int m = data.length;
710 final int n = v.getDimension();
711 final FieldMatrix<T> out = new Array2DRowFieldMatrix<>(field, m, n);
712 for (int i = 0; i < m; i++) {
713 for (int j = 0; j < n; j++) {
714 out.setEntry(i, j, data[i].multiply(v.getEntry(j)));
715 }
716 }
717 return out;
718 }
719 }
720
721
722
723
724
725
726 public FieldMatrix<T> outerProduct(ArrayFieldVector<T> v) {
727 final int m = data.length;
728 final int n = v.data.length;
729 final FieldMatrix<T> out = new Array2DRowFieldMatrix<>(field, m, n);
730 for (int i = 0; i < m; i++) {
731 for (int j = 0; j < n; j++) {
732 out.setEntry(i, j, data[i].multiply(v.data[j]));
733 }
734 }
735 return out;
736 }
737
738
739 @Override
740 public T getEntry(int index) {
741 return data[index];
742 }
743
744
745 @Override
746 public int getDimension() {
747 return data.length;
748 }
749
750
751 @Override
752 public FieldVector<T> append(FieldVector<T> v) {
753 if (v instanceof ArrayFieldVector) {
754 return append((ArrayFieldVector<T>) v);
755 } else {
756 return new ArrayFieldVector<T>(this,new ArrayFieldVector<T>(v));
757 }
758 }
759
760
761
762
763
764
765 public ArrayFieldVector<T> append(ArrayFieldVector<T> v) {
766 return new ArrayFieldVector<T>(this, v);
767 }
768
769
770 @Override
771 public FieldVector<T> append(T in) {
772 final T[] out = MathArrays.buildArray(field, data.length + 1);
773 System.arraycopy(data, 0, out, 0, data.length);
774 out[data.length] = in;
775 return new ArrayFieldVector<T>(field, out, false);
776 }
777
778
779 @Override
780 public FieldVector<T> getSubVector(int index, int n)
781 throws MathIllegalArgumentException {
782 if (n < 0) {
783 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
784 }
785 ArrayFieldVector<T> out = new ArrayFieldVector<>(field, n);
786 try {
787 System.arraycopy(data, index, out.data, 0, n);
788 } catch (IndexOutOfBoundsException e) {
789 checkIndex(index);
790 checkIndex(index + n - 1);
791 }
792 return out;
793 }
794
795
796 @Override
797 public void setEntry(int index, T value) {
798 try {
799 data[index] = value;
800 } catch (IndexOutOfBoundsException e) {
801 checkIndex(index);
802 }
803 }
804
805
806 @Override
807 public void setSubVector(int index, FieldVector<T> v) throws MathIllegalArgumentException {
808 try {
809 if (v instanceof ArrayFieldVector) {
810 set(index, (ArrayFieldVector<T>) v);
811 } else {
812 for (int i = index; i < index + v.getDimension(); ++i) {
813 data[i] = v.getEntry(i-index);
814 }
815 }
816 } catch (IndexOutOfBoundsException e) {
817 checkIndex(index);
818 checkIndex(index + v.getDimension() - 1);
819 }
820 }
821
822
823
824
825
826
827
828
829 public void set(int index, ArrayFieldVector<T> v) throws MathIllegalArgumentException {
830 try {
831 System.arraycopy(v.data, 0, data, index, v.data.length);
832 } catch (IndexOutOfBoundsException e) {
833 checkIndex(index);
834 checkIndex(index + v.data.length - 1);
835 }
836 }
837
838
839 @Override
840 public void set(T value) {
841 Arrays.fill(data, value);
842 }
843
844
845 @Override
846 public T[] toArray(){
847 return data.clone();
848 }
849
850
851
852
853
854
855
856 protected void checkVectorDimensions(FieldVector<T> v)
857 throws MathIllegalArgumentException {
858 checkVectorDimensions(v.getDimension());
859 }
860
861
862
863
864
865
866
867
868 protected void checkVectorDimensions(int n)
869 throws MathIllegalArgumentException {
870 if (data.length != n) {
871 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
872 data.length, n);
873 }
874 }
875
876
877
878
879
880
881
882
883
884
885 public T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor) {
886 final int dim = getDimension();
887 visitor.start(dim, 0, dim - 1);
888 for (int i = 0; i < dim; i++) {
889 visitor.visit(i, getEntry(i));
890 }
891 return visitor.end();
892 }
893
894
895
896
897
898
899
900
901
902
903
904
905
906 public T walkInDefaultOrder(final FieldVectorPreservingVisitor<T> visitor,
907 final int start, final int end)
908 throws MathIllegalArgumentException {
909 checkIndices(start, end);
910 visitor.start(getDimension(), start, end);
911 for (int i = start; i <= end; i++) {
912 visitor.visit(i, getEntry(i));
913 }
914 return visitor.end();
915 }
916
917
918
919
920
921
922
923
924
925
926
927
928 public T walkInOptimizedOrder(final FieldVectorPreservingVisitor<T> visitor) {
929 return walkInDefaultOrder(visitor);
930 }
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946 public T walkInOptimizedOrder(final FieldVectorPreservingVisitor<T> visitor,
947 final int start, final int end)
948 throws MathIllegalArgumentException {
949 return walkInDefaultOrder(visitor, start, end);
950 }
951
952
953
954
955
956
957
958
959
960
961 public T walkInDefaultOrder(final FieldVectorChangingVisitor<T> visitor) {
962 final int dim = getDimension();
963 visitor.start(dim, 0, dim - 1);
964 for (int i = 0; i < dim; i++) {
965 setEntry(i, visitor.visit(i, getEntry(i)));
966 }
967 return visitor.end();
968 }
969
970
971
972
973
974
975
976
977
978
979
980
981
982 public T walkInDefaultOrder(final FieldVectorChangingVisitor<T> visitor,
983 final int start, final int end)
984 throws MathIllegalArgumentException {
985 checkIndices(start, end);
986 visitor.start(getDimension(), start, end);
987 for (int i = start; i <= end; i++) {
988 setEntry(i, visitor.visit(i, getEntry(i)));
989 }
990 return visitor.end();
991 }
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004 public T walkInOptimizedOrder(final FieldVectorChangingVisitor<T> visitor) {
1005 return walkInDefaultOrder(visitor);
1006 }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022 public T walkInOptimizedOrder(final FieldVectorChangingVisitor<T> visitor,
1023 final int start, final int end)
1024 throws MathIllegalArgumentException {
1025 return walkInDefaultOrder(visitor, start, end);
1026 }
1027
1028
1029
1030 @Override
1031 public String toString() {
1032 final StringBuilder builder = new StringBuilder();
1033 builder.append('{');
1034 for (int i = 0; i < data.length; ++i) {
1035 if (i > 0) {
1036 builder.append("; ");
1037 }
1038 builder.append(data[i].toString());
1039 }
1040 builder.append('}');
1041 return builder.toString();
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051 @Override
1052 public boolean equals(Object other) {
1053 if (this == other) {
1054 return true;
1055 }
1056 if (other == null) {
1057 return false;
1058 }
1059
1060 try {
1061 @SuppressWarnings("unchecked")
1062 FieldVector<T> rhs = (FieldVector<T>) other;
1063 if (data.length != rhs.getDimension()) {
1064 return false;
1065 }
1066
1067 for (int i = 0; i < data.length; ++i) {
1068 if (!data[i].equals(rhs.getEntry(i))) {
1069 return false;
1070 }
1071 }
1072 return true;
1073 } catch (ClassCastException ex) {
1074
1075 return false;
1076 }
1077 }
1078
1079
1080
1081
1082
1083
1084 @Override
1085 public int hashCode() {
1086 int h = 3542;
1087 for (final T a : data) {
1088 h ^= a.hashCode();
1089 }
1090 return h;
1091 }
1092
1093
1094
1095
1096
1097
1098
1099 private void checkIndex(final int index) throws MathIllegalArgumentException {
1100 if (index < 0 || index >= getDimension()) {
1101 throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX,
1102 index, 0, getDimension() - 1);
1103 }
1104 }
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114 private void checkIndices(final int start, final int end)
1115 throws MathIllegalArgumentException {
1116 final int dim = getDimension();
1117 if ((start < 0) || (start >= dim)) {
1118 throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX, start, 0,
1119 dim - 1);
1120 }
1121 if ((end < 0) || (end >= dim)) {
1122 throw new MathIllegalArgumentException(LocalizedCoreFormats.INDEX, end, 0,
1123 dim - 1);
1124 }
1125 if (end < start) {
1126 throw new MathIllegalArgumentException(LocalizedCoreFormats.INITIAL_ROW_AFTER_FINAL_ROW,
1127 end, start, false);
1128 }
1129 }
1130
1131 }