19
19
import java.io.Serializable;
20
20
import java.util.Arrays;
21
import java.util.Iterator;
22
23
import org.apache.commons.math.MathRuntimeException;
23
24
import org.apache.commons.math.util.MathUtils;
26
27
* This class implements the {@link RealVector} interface with a double array.
27
* @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
28
* @version $Revision: 902203 $ $Date: 2010-01-22 13:27:41 -0500 (Fri, 22 Jan 2010) $
30
public class ArrayRealVector implements RealVector, Serializable {
31
public class ArrayRealVector extends AbstractRealVector implements Serializable {
33
/** Message for non fitting position and size. */
34
private static final String NON_FITTING_POSITION_AND_SIZE_MESSAGE =
35
"position {0} and size {1} don't fit to the size of the input array {2}";
32
37
/** Serializable version identifier. */
33
38
private static final long serialVersionUID = -1097961340710804027L;
96
101
throws NullPointerException, IllegalArgumentException {
98
103
throw new NullPointerException();
100
105
if (d.length == 0) {
101
throw MathRuntimeException.createIllegalArgumentException("vector must have at least one element");
106
throw MathRuntimeException.createIllegalArgumentException("vector must have at least one element");
103
108
data = copyArray ? d.clone() : d;
112
117
public ArrayRealVector(double[] d, int pos, int size) {
113
118
if (d.length < pos + size) {
114
119
throw MathRuntimeException.createIllegalArgumentException(
115
"position {0} and size {1} don't fit to the size of the input array {2}",
116
pos, size, d.length);
120
NON_FITTING_POSITION_AND_SIZE_MESSAGE, pos, size, d.length);
118
122
data = new double[size];
119
123
System.arraycopy(d, pos, data, 0, size);
139
143
public ArrayRealVector(Double[] d, int pos, int size) {
140
144
if (d.length < pos + size) {
141
145
throw MathRuntimeException.createIllegalArgumentException(
142
"position {0} and size {1} don't fit to the size of the input array {2}",
143
pos, size, d.length);
146
NON_FITTING_POSITION_AND_SIZE_MESSAGE, pos, size, d.length);
145
148
data = new double[size];
146
149
for (int i = pos; i < pos + size; i++) {
192
195
* @param v1 first vector (will be put in front of the new vector)
193
196
* @param v2 second vector (will be put at back of the new vector)
198
public ArrayRealVector(ArrayRealVector v1, RealVector v2) {
199
final int l1 = v1.data.length;
200
final int l2 = v2.getDimension();
201
data = new double[l1 + l2];
202
System.arraycopy(v1.data, 0, data, 0, l1);
203
for (int i = 0; i < l2; ++i) {
204
data[l1 + i] = v2.getEntry(i);
209
* Construct a vector by appending one vector to another vector.
210
* @param v1 first vector (will be put in front of the new vector)
211
* @param v2 second vector (will be put at back of the new vector)
213
public ArrayRealVector(RealVector v1, ArrayRealVector v2) {
214
final int l1 = v1.getDimension();
215
final int l2 = v2.data.length;
216
data = new double[l1 + l2];
217
for (int i = 0; i < l1; ++i) {
218
data[i] = v1.getEntry(i);
220
System.arraycopy(v2.data, 0, data, l1, l2);
224
* Construct a vector by appending one vector to another vector.
225
* @param v1 first vector (will be put in front of the new vector)
226
* @param v2 second vector (will be put at back of the new vector)
195
228
public ArrayRealVector(ArrayRealVector v1, double[] v2) {
196
data = new double[v1.data.length + v2.length];
197
System.arraycopy(v1.data, 0, data, 0, v1.data.length);
198
System.arraycopy(v2, 0, data, v1.data.length, v2.length);
229
final int l1 = v1.getDimension();
230
final int l2 = v2.length;
231
data = new double[l1 + l2];
232
System.arraycopy(v1.data, 0, data, 0, l1);
233
System.arraycopy(v2, 0, data, l1, l2);
204
239
* @param v2 second vector (will be put at back of the new vector)
206
241
public ArrayRealVector(double[] v1, ArrayRealVector v2) {
207
data = new double[v1.length + v2.data.length];
208
System.arraycopy(v1, 0, data, 0, v1.length);
209
System.arraycopy(v2.data, 0, data, v1.length, v2.data.length);
242
final int l1 = v1.length;
243
final int l2 = v2.getDimension();
244
data = new double[l1 + l2];
245
System.arraycopy(v1, 0, data, 0, l1);
246
System.arraycopy(v2.data, 0, data, l1, l2);
215
252
* @param v2 second vector (will be put at back of the new vector)
217
254
public ArrayRealVector(double[] v1, double[] v2) {
218
data = new double[v1.length + v2.length];
219
System.arraycopy(v1, 0, data, 0, v1.length);
220
System.arraycopy(v2, 0, data, v1.length, v2.length);
255
final int l1 = v1.length;
256
final int l2 = v2.length;
257
data = new double[l1 + l2];
258
System.arraycopy(v1, 0, data, 0, l1);
259
System.arraycopy(v2, 0, data, l1, l2);
223
262
/** {@inheritDoc} */
224
public RealVector copy() {
264
public AbstractRealVector copy() {
225
265
return new ArrayRealVector(this, true);
228
268
/** {@inheritDoc} */
229
270
public RealVector add(RealVector v)
230
throws IllegalArgumentException {
271
throws IllegalArgumentException {
272
if (v instanceof ArrayRealVector) {
232
273
return add((ArrayRealVector) v);
233
} catch (ClassCastException cce) {
234
275
checkVectorDimensions(v);
235
double[] out = new double[data.length];
236
for (int i = 0; i < data.length; i++) {
237
out[i] = data[i] + v.getEntry(i);
276
double[] out = data.clone();
277
Iterator<Entry> it = v.sparseIterator();
279
while (it.hasNext() && (e = it.next()) != null) {
280
out[e.getIndex()] += e.getValue();
239
return new ArrayRealVector(out);
282
return new ArrayRealVector(out, false);
243
286
/** {@inheritDoc} */
244
288
public RealVector add(double[] v)
245
throws IllegalArgumentException {
289
throws IllegalArgumentException {
246
290
checkVectorDimensions(v.length);
247
double[] out = new double[data.length];
291
double[] out = data.clone();
248
292
for (int i = 0; i < data.length; i++) {
249
out[i] = data[i] + v[i];
251
return new ArrayRealVector(out);
295
return new ArrayRealVector(out, false);
265
309
/** {@inheritDoc} */
266
311
public RealVector subtract(RealVector v)
267
throws IllegalArgumentException {
312
throws IllegalArgumentException {
313
if (v instanceof ArrayRealVector) {
269
314
return subtract((ArrayRealVector) v);
270
} catch (ClassCastException cce) {
271
316
checkVectorDimensions(v);
272
double[] out = new double[data.length];
273
for (int i = 0; i < data.length; i++) {
274
out[i] = data[i] - v.getEntry(i);
317
double[] out = data.clone();
318
Iterator<Entry> it = v.sparseIterator();
320
while(it.hasNext() && (e = it.next()) != null) {
321
out[e.getIndex()] -= e.getValue();
276
return new ArrayRealVector(out);
323
return new ArrayRealVector(out, false);
280
327
/** {@inheritDoc} */
281
329
public RealVector subtract(double[] v)
282
throws IllegalArgumentException {
330
throws IllegalArgumentException {
283
331
checkVectorDimensions(v.length);
284
double[] out = new double[data.length];
332
double[] out = data.clone();
285
333
for (int i = 0; i < data.length; i++) {
286
out[i] = data[i] - v[i];
288
return new ArrayRealVector(out);
336
return new ArrayRealVector(out, false);
302
350
/** {@inheritDoc} */
303
public RealVector mapAdd(double d) {
304
double[] out = new double[data.length];
305
for (int i = 0; i < data.length; i++) {
306
out[i] = data[i] + d;
308
return new ArrayRealVector(out);
312
352
public RealVector mapAddToSelf(double d) {
313
353
for (int i = 0; i < data.length; i++) {
314
354
data[i] = data[i] + d;
319
359
/** {@inheritDoc} */
320
public RealVector mapSubtract(double d) {
321
double[] out = new double[data.length];
322
for (int i = 0; i < data.length; i++) {
323
out[i] = data[i] - d;
325
return new ArrayRealVector(out);
329
361
public RealVector mapSubtractToSelf(double d) {
330
362
for (int i = 0; i < data.length; i++) {
331
363
data[i] = data[i] - d;
336
368
/** {@inheritDoc} */
337
public RealVector mapMultiply(double d) {
338
double[] out = new double[data.length];
339
for (int i = 0; i < data.length; i++) {
340
out[i] = data[i] * d;
342
return new ArrayRealVector(out);
346
370
public RealVector mapMultiplyToSelf(double d) {
347
371
for (int i = 0; i < data.length; i++) {
348
372
data[i] = data[i] * d;
353
377
/** {@inheritDoc} */
354
public RealVector mapDivide(double d) {
355
double[] out = new double[data.length];
356
for (int i = 0; i < data.length; i++) {
357
out[i] = data[i] / d;
359
return new ArrayRealVector(out);
363
379
public RealVector mapDivideToSelf(double d) {
364
380
for (int i = 0; i < data.length; i++) {
365
381
data[i] = data[i] / d;
370
386
/** {@inheritDoc} */
371
public RealVector mapPow(double d) {
372
double[] out = new double[data.length];
373
for (int i = 0; i < data.length; i++) {
374
out[i] = Math.pow(data[i], d);
376
return new ArrayRealVector(out);
380
388
public RealVector mapPowToSelf(double d) {
381
389
for (int i = 0; i < data.length; i++) {
382
390
data[i] = Math.pow(data[i], d);
387
395
/** {@inheritDoc} */
388
public RealVector mapExp() {
389
double[] out = new double[data.length];
390
for (int i = 0; i < data.length; i++) {
391
out[i] = Math.exp(data[i]);
393
return new ArrayRealVector(out);
397
397
public RealVector mapExpToSelf() {
398
398
for (int i = 0; i < data.length; i++) {
399
399
data[i] = Math.exp(data[i]);
404
404
/** {@inheritDoc} */
405
public RealVector mapExpm1() {
406
double[] out = new double[data.length];
407
for (int i = 0; i < data.length; i++) {
408
out[i] = Math.expm1(data[i]);
410
return new ArrayRealVector(out);
414
406
public RealVector mapExpm1ToSelf() {
415
407
for (int i = 0; i < data.length; i++) {
416
408
data[i] = Math.expm1(data[i]);
421
413
/** {@inheritDoc} */
422
public RealVector mapLog() {
423
double[] out = new double[data.length];
424
for (int i = 0; i < data.length; i++) {
425
out[i] = Math.log(data[i]);
427
return new ArrayRealVector(out);
431
415
public RealVector mapLogToSelf() {
432
416
for (int i = 0; i < data.length; i++) {
433
417
data[i] = Math.log(data[i]);
438
422
/** {@inheritDoc} */
439
public RealVector mapLog10() {
440
double[] out = new double[data.length];
441
for (int i = 0; i < data.length; i++) {
442
out[i] = Math.log10(data[i]);
444
return new ArrayRealVector(out);
448
424
public RealVector mapLog10ToSelf() {
449
425
for (int i = 0; i < data.length; i++) {
450
426
data[i] = Math.log10(data[i]);
455
431
/** {@inheritDoc} */
456
public RealVector mapLog1p() {
457
double[] out = new double[data.length];
458
for (int i = 0; i < data.length; i++) {
459
out[i] = Math.log1p(data[i]);
461
return new ArrayRealVector(out);
465
433
public RealVector mapLog1pToSelf() {
466
434
for (int i = 0; i < data.length; i++) {
467
435
data[i] = Math.log1p(data[i]);
472
440
/** {@inheritDoc} */
473
public RealVector mapCosh() {
474
double[] out = new double[data.length];
475
for (int i = 0; i < data.length; i++) {
476
out[i] = Math.cosh(data[i]);
478
return new ArrayRealVector(out);
482
442
public RealVector mapCoshToSelf() {
483
443
for (int i = 0; i < data.length; i++) {
484
444
data[i] = Math.cosh(data[i]);
489
449
/** {@inheritDoc} */
490
public RealVector mapSinh() {
491
double[] out = new double[data.length];
492
for (int i = 0; i < data.length; i++) {
493
out[i] = Math.sinh(data[i]);
495
return new ArrayRealVector(out);
499
451
public RealVector mapSinhToSelf() {
500
452
for (int i = 0; i < data.length; i++) {
501
453
data[i] = Math.sinh(data[i]);
506
458
/** {@inheritDoc} */
507
public RealVector mapTanh() {
508
double[] out = new double[data.length];
509
for (int i = 0; i < data.length; i++) {
510
out[i] = Math.tanh(data[i]);
512
return new ArrayRealVector(out);
516
460
public RealVector mapTanhToSelf() {
517
461
for (int i = 0; i < data.length; i++) {
518
462
data[i] = Math.tanh(data[i]);
523
467
/** {@inheritDoc} */
524
public RealVector mapCos() {
525
double[] out = new double[data.length];
526
for (int i = 0; i < data.length; i++) {
527
out[i] = Math.cos(data[i]);
529
return new ArrayRealVector(out);
533
469
public RealVector mapCosToSelf() {
534
470
for (int i = 0; i < data.length; i++) {
535
471
data[i] = Math.cos(data[i]);
540
476
/** {@inheritDoc} */
541
public RealVector mapSin() {
542
double[] out = new double[data.length];
543
for (int i = 0; i < data.length; i++) {
544
out[i] = Math.sin(data[i]);
546
return new ArrayRealVector(out);
550
478
public RealVector mapSinToSelf() {
551
479
for (int i = 0; i < data.length; i++) {
552
480
data[i] = Math.sin(data[i]);
557
485
/** {@inheritDoc} */
558
public RealVector mapTan() {
559
double[] out = new double[data.length];
560
for (int i = 0; i < data.length; i++) {
561
out[i] = Math.tan(data[i]);
563
return new ArrayRealVector(out);
567
487
public RealVector mapTanToSelf() {
568
488
for (int i = 0; i < data.length; i++) {
569
489
data[i] = Math.tan(data[i]);
574
494
/** {@inheritDoc} */
575
public RealVector mapAcos() {
576
double[] out = new double[data.length];
577
for (int i = 0; i < data.length; i++) {
578
out[i] = Math.acos(data[i]);
580
return new ArrayRealVector(out);
584
496
public RealVector mapAcosToSelf() {
585
497
for (int i = 0; i < data.length; i++) {
586
498
data[i] = Math.acos(data[i]);
591
503
/** {@inheritDoc} */
592
public RealVector mapAsin() {
593
double[] out = new double[data.length];
594
for (int i = 0; i < data.length; i++) {
595
out[i] = Math.asin(data[i]);
597
return new ArrayRealVector(out);
601
505
public RealVector mapAsinToSelf() {
602
506
for (int i = 0; i < data.length; i++) {
603
507
data[i] = Math.asin(data[i]);
608
512
/** {@inheritDoc} */
609
public RealVector mapAtan() {
610
double[] out = new double[data.length];
611
for (int i = 0; i < data.length; i++) {
612
out[i] = Math.atan(data[i]);
614
return new ArrayRealVector(out);
618
514
public RealVector mapAtanToSelf() {
619
515
for (int i = 0; i < data.length; i++) {
620
516
data[i] = Math.atan(data[i]);
625
521
/** {@inheritDoc} */
626
public RealVector mapInv() {
627
double[] out = new double[data.length];
628
for (int i = 0; i < data.length; i++) {
629
out[i] = 1.0 / data[i];
631
return new ArrayRealVector(out);
635
523
public RealVector mapInvToSelf() {
636
524
for (int i = 0; i < data.length; i++) {
637
525
data[i] = 1.0 / data[i];
642
530
/** {@inheritDoc} */
643
public RealVector mapAbs() {
644
double[] out = new double[data.length];
645
for (int i = 0; i < data.length; i++) {
646
out[i] = Math.abs(data[i]);
648
return new ArrayRealVector(out);
652
532
public RealVector mapAbsToSelf() {
653
533
for (int i = 0; i < data.length; i++) {
654
534
data[i] = Math.abs(data[i]);
659
539
/** {@inheritDoc} */
660
public RealVector mapSqrt() {
661
double[] out = new double[data.length];
662
for (int i = 0; i < data.length; i++) {
663
out[i] = Math.sqrt(data[i]);
665
return new ArrayRealVector(out);
669
541
public RealVector mapSqrtToSelf() {
670
542
for (int i = 0; i < data.length; i++) {
671
543
data[i] = Math.sqrt(data[i]);
676
548
/** {@inheritDoc} */
677
public RealVector mapCbrt() {
678
double[] out = new double[data.length];
679
for (int i = 0; i < data.length; i++) {
680
out[i] = Math.cbrt(data[i]);
682
return new ArrayRealVector(out);
686
550
public RealVector mapCbrtToSelf() {
687
551
for (int i = 0; i < data.length; i++) {
688
552
data[i] = Math.cbrt(data[i]);
693
557
/** {@inheritDoc} */
694
public RealVector mapCeil() {
695
double[] out = new double[data.length];
696
for (int i = 0; i < data.length; i++) {
697
out[i] = Math.ceil(data[i]);
699
return new ArrayRealVector(out);
703
559
public RealVector mapCeilToSelf() {
704
560
for (int i = 0; i < data.length; i++) {
705
561
data[i] = Math.ceil(data[i]);
710
566
/** {@inheritDoc} */
711
public RealVector mapFloor() {
712
double[] out = new double[data.length];
713
for (int i = 0; i < data.length; i++) {
714
out[i] = Math.floor(data[i]);
716
return new ArrayRealVector(out);
720
568
public RealVector mapFloorToSelf() {
721
569
for (int i = 0; i < data.length; i++) {
722
570
data[i] = Math.floor(data[i]);
727
575
/** {@inheritDoc} */
728
public RealVector mapRint() {
729
double[] out = new double[data.length];
730
for (int i = 0; i < data.length; i++) {
731
out[i] = Math.rint(data[i]);
733
return new ArrayRealVector(out);
737
577
public RealVector mapRintToSelf() {
738
578
for (int i = 0; i < data.length; i++) {
739
579
data[i] = Math.rint(data[i]);
744
584
/** {@inheritDoc} */
745
public RealVector mapSignum() {
746
double[] out = new double[data.length];
747
for (int i = 0; i < data.length; i++) {
748
out[i] = Math.signum(data[i]);
750
return new ArrayRealVector(out);
754
586
public RealVector mapSignumToSelf() {
755
587
for (int i = 0; i < data.length; i++) {
756
588
data[i] = Math.signum(data[i]);
761
593
/** {@inheritDoc} */
762
public RealVector mapUlp() {
763
double[] out = new double[data.length];
764
for (int i = 0; i < data.length; i++) {
765
out[i] = Math.ulp(data[i]);
767
return new ArrayRealVector(out);
771
595
public RealVector mapUlpToSelf() {
772
596
for (int i = 0; i < data.length; i++) {
773
597
data[i] = Math.ulp(data[i]);
778
602
/** {@inheritDoc} */
779
603
public RealVector ebeMultiply(RealVector v)
780
604
throws IllegalArgumentException {
605
if (v instanceof ArrayRealVector) {
782
606
return ebeMultiply((ArrayRealVector) v);
783
} catch (ClassCastException cce) {
784
608
checkVectorDimensions(v);
785
double[] out = new double[data.length];
609
double[] out = data.clone();
786
610
for (int i = 0; i < data.length; i++) {
787
out[i] = data[i] * v.getEntry(i);
611
out[i] *= v.getEntry(i);
789
return new ArrayRealVector(out);
613
return new ArrayRealVector(out, false);
793
617
/** {@inheritDoc} */
794
619
public RealVector ebeMultiply(double[] v)
795
620
throws IllegalArgumentException {
796
621
checkVectorDimensions(v.length);
797
double[] out = new double[data.length];
622
double[] out = data.clone();
798
623
for (int i = 0; i < data.length; i++) {
799
out[i] = data[i] * v[i];
801
return new ArrayRealVector(out);
626
return new ArrayRealVector(out, false);
815
640
/** {@inheritDoc} */
816
641
public RealVector ebeDivide(RealVector v)
817
642
throws IllegalArgumentException {
643
if (v instanceof ArrayRealVector) {
819
644
return ebeDivide((ArrayRealVector) v);
820
} catch (ClassCastException cce) {
821
646
checkVectorDimensions(v);
822
double[] out = new double[data.length];
647
double[] out = data.clone();
823
648
for (int i = 0; i < data.length; i++) {
824
out[i] = data[i] / v.getEntry(i);
649
out[i] /= v.getEntry(i);
826
return new ArrayRealVector(out);
651
return new ArrayRealVector(out, false);
830
655
/** {@inheritDoc} */
831
657
public RealVector ebeDivide(double[] v)
832
658
throws IllegalArgumentException {
833
659
checkVectorDimensions(v.length);
834
double[] out = new double[data.length];
660
double[] out = data.clone();
835
661
for (int i = 0; i < data.length; i++) {
836
out[i] = data[i] / v[i];
838
return new ArrayRealVector(out);
664
return new ArrayRealVector(out, false);
866
693
/** {@inheritDoc} */
867
695
public double dotProduct(RealVector v)
868
696
throws IllegalArgumentException {
697
if (v instanceof ArrayRealVector) {
870
698
return dotProduct((ArrayRealVector) v);
871
} catch (ClassCastException cce) {
872
700
checkVectorDimensions(v);
874
for (int i = 0; i < data.length; i++) {
875
dot += data[i] * v.getEntry(i);
702
Iterator<Entry> it = v.sparseIterator();
704
while(it.hasNext() && (e = it.next()) != null) {
705
dot += data[e.getIndex()] * e.getValue();
881
711
/** {@inheritDoc} */
882
713
public double dotProduct(double[] v)
883
714
throws IllegalArgumentException {
884
715
checkVectorDimensions(v.length);
921
754
/** {@inheritDoc} */
922
756
public double getLInfNorm() {
924
758
for (double a : data) {
925
max += Math.max(max, Math.abs(a));
759
max = Math.max(max, Math.abs(a));
930
764
/** {@inheritDoc} */
931
766
public double getDistance(RealVector v)
932
767
throws IllegalArgumentException {
768
if (v instanceof ArrayRealVector) {
934
769
return getDistance((ArrayRealVector) v);
935
} catch (ClassCastException cce) {
936
771
checkVectorDimensions(v);
938
773
for (int i = 0; i < data.length; ++i) {
939
final double delta = data[i] - v.getEntry(i);
774
final double delta = data[i] - v.getEntry(i);
940
775
sum += delta * delta;
942
777
return Math.sqrt(sum);
976
812
/** {@inheritDoc} */
977
814
public double getL1Distance(RealVector v)
978
815
throws IllegalArgumentException {
816
if (v instanceof ArrayRealVector) {
980
817
return getL1Distance((ArrayRealVector) v);
981
} catch (ClassCastException cce) {
982
819
checkVectorDimensions(v);
984
821
for (int i = 0; i < data.length; ++i) {
985
final double delta = data[i] - v.getEntry(i);
822
final double delta = data[i] - v.getEntry(i);
986
823
sum += Math.abs(delta);
1022
860
/** {@inheritDoc} */
1023
862
public double getLInfDistance(RealVector v)
1024
863
throws IllegalArgumentException {
864
if (v instanceof ArrayRealVector) {
1026
865
return getLInfDistance((ArrayRealVector) v);
1027
} catch (ClassCastException cce) {
1028
867
checkVectorDimensions(v);
1030
869
for (int i = 0; i < data.length; ++i) {
1031
final double delta = data[i] - v.getEntry(i);
870
final double delta = data[i] - v.getEntry(i);
1032
871
max = Math.max(max, Math.abs(delta));
1068
908
/** {@inheritDoc} */
1069
910
public RealVector unitVector() throws ArithmeticException {
1070
911
final double norm = getNorm();
1071
912
if (norm == 0) {
1072
913
throw MathRuntimeException.createArithmeticException("zero norm");
1074
return mapDivide(getNorm());
915
return mapDivide(norm);
1077
918
/** {@inheritDoc} */
1078
920
public void unitize() throws ArithmeticException {
1079
921
final double norm = getNorm();
1080
922
if (norm == 0) {
1081
923
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector");
1083
for (int i = 0; i < data.length; i++) {
925
mapDivideToSelf(norm);
1088
928
/** {@inheritDoc} */
1107
948
/** {@inheritDoc} */
1108
950
public RealMatrix outerProduct(RealVector v)
1109
951
throws IllegalArgumentException {
952
if (v instanceof ArrayRealVector) {
1111
953
return outerProduct((ArrayRealVector) v);
1112
} catch (ClassCastException cce) {
1113
955
checkVectorDimensions(v);
1114
956
final int m = data.length;
1115
957
final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
1160
1003
/** {@inheritDoc} */
1161
1004
public RealVector append(RealVector v) {
1163
return append((ArrayRealVector) v);
1006
return new ArrayRealVector(this, (ArrayRealVector) v);
1164
1007
} catch (ClassCastException cce) {
1165
return new ArrayRealVector(this,new ArrayRealVector(v));
1008
return new ArrayRealVector(this, v);
1180
1023
final double[] out = new double[data.length + 1];
1181
1024
System.arraycopy(data, 0, out, 0, data.length);
1182
1025
out[data.length] = in;
1183
return new ArrayRealVector(out);
1026
return new ArrayRealVector(out, false);
1186
1029
/** {@inheritDoc} */
1279
1127
* Check if instance dimension is equal to some expected value.
1281
1129
* @param n expected dimension.
1282
1130
* @exception IllegalArgumentException if the dimension is
1283
1131
* inconsistent with vector size
1285
1134
protected void checkVectorDimensions(int n)
1286
1135
throws IllegalArgumentException {
1287
1136
if (data.length != n) {
1343
1192
* @return true if two vector objects are equal, false if
1344
1193
* object is null, not an instance of RealVector, or
1345
1194
* not equal to this RealVector instance
1349
1198
public boolean equals(Object other) {
1351
if (this == other) {
1200
if (this == other) {
1355
if (other == null) {
1361
RealVector rhs = (RealVector) other;
1362
if (data.length != rhs.getDimension()) {
1367
return this.isNaN();
1370
for (int i = 0; i < data.length; ++i) {
1371
if (data[i] != rhs.getEntry(i)) {
1377
} catch (ClassCastException ex) {
1204
if (other == null || !(other instanceof RealVector)) {
1209
RealVector rhs = (RealVector) other;
1210
if (data.length != rhs.getDimension()) {
1215
return this.isNaN();
1218
for (int i = 0; i < data.length; ++i) {
1219
if (data[i] != rhs.getEntry(i)) {
1385
1227
* Get a hashCode for the real vector.
1386
1228
* <p>All NaN values have the same hash code.</p>
1394
1236
return MathUtils.hash(data);
1398
* Check if an index is valid.
1399
* @param index index to check
1400
* @exception MatrixIndexException if index is not valid
1402
private void checkIndex(final int index)
1403
throws MatrixIndexException {
1404
if (index < 0 || index >= getDimension()) {
1405
throw new MatrixIndexException(
1406
"index {0} out of allowed range [{1}, {2}]",
1407
index, 0, getDimension() - 1);