~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/linear/ArrayRealVector.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import java.io.Serializable;
20
20
import java.util.Arrays;
 
21
import java.util.Iterator;
21
22
 
22
23
import org.apache.commons.math.MathRuntimeException;
23
24
import org.apache.commons.math.util.MathUtils;
24
25
 
25
26
/**
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) $
28
29
 * @since 2.0
29
30
 */
30
 
public class ArrayRealVector implements RealVector, Serializable {
 
31
public class ArrayRealVector extends AbstractRealVector implements Serializable {
 
32
 
 
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}";
31
36
 
32
37
    /** Serializable version identifier. */
33
38
    private static final long serialVersionUID = -1097961340710804027L;
96
101
        throws NullPointerException, IllegalArgumentException {
97
102
        if (d == null) {
98
103
            throw new NullPointerException();
99
 
        }   
 
104
        }
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");
102
107
        }
103
108
        data = copyArray ? d.clone() :  d;
104
109
    }
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);
117
121
        }
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);
144
147
        }
145
148
        data = new double[size];
146
149
        for (int i = pos; i < pos + size; i++) {
164
167
     * @param v vector to copy
165
168
     */
166
169
    public ArrayRealVector(ArrayRealVector v) {
167
 
        data = v.data.clone();
 
170
        this(v, true);
168
171
    }
169
172
 
170
173
    /**
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)
194
197
     */
 
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);
 
205
        }
 
206
    }
 
207
 
 
208
    /**
 
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)
 
212
     */
 
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);
 
219
        }
 
220
        System.arraycopy(v2.data, 0, data, l1, l2);
 
221
    }
 
222
 
 
223
    /**
 
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)
 
227
     */
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);
199
234
    }
200
235
 
201
236
    /**
204
239
     * @param v2 second vector (will be put at back of the new vector)
205
240
     */
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);
210
247
    }
211
248
 
212
249
    /**
215
252
     * @param v2 second vector (will be put at back of the new vector)
216
253
     */
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);
221
260
    }
222
261
 
223
262
    /** {@inheritDoc} */
224
 
    public RealVector copy() {
 
263
    @Override
 
264
    public AbstractRealVector copy() {
225
265
        return new ArrayRealVector(this, true);
226
266
    }
227
267
 
228
268
    /** {@inheritDoc} */
 
269
    @Override
229
270
    public RealVector add(RealVector v)
230
 
    throws IllegalArgumentException {
231
 
        try {
 
271
        throws IllegalArgumentException {
 
272
        if (v instanceof ArrayRealVector) {
232
273
            return add((ArrayRealVector) v);
233
 
        } catch (ClassCastException cce) {
 
274
        } else {
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();
 
278
            Entry e;
 
279
            while (it.hasNext() && (e = it.next()) != null) {
 
280
                out[e.getIndex()] += e.getValue();
238
281
            }
239
 
            return new ArrayRealVector(out);
 
282
            return new ArrayRealVector(out, false);
240
283
        }
241
284
    }
242
285
 
243
286
    /** {@inheritDoc} */
 
287
    @Override
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];
 
293
            out[i] += v[i];
250
294
        }
251
 
        return new ArrayRealVector(out);
 
295
        return new ArrayRealVector(out, false);
252
296
    }
253
297
 
254
298
    /**
263
307
    }
264
308
 
265
309
    /** {@inheritDoc} */
 
310
    @Override
266
311
    public RealVector subtract(RealVector v)
267
 
    throws IllegalArgumentException {
268
 
        try {
 
312
        throws IllegalArgumentException {
 
313
        if (v instanceof ArrayRealVector) {
269
314
            return subtract((ArrayRealVector) v);
270
 
        } catch (ClassCastException cce) {
 
315
        } else {
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();
 
319
            Entry e;
 
320
            while(it.hasNext() && (e = it.next()) != null) {
 
321
                out[e.getIndex()] -= e.getValue();
275
322
            }
276
 
            return new ArrayRealVector(out);
 
323
            return new ArrayRealVector(out, false);
277
324
        }
278
325
    }
279
326
 
280
327
    /** {@inheritDoc} */
 
328
    @Override
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];
 
334
            out[i] -= v[i];
287
335
        }
288
 
        return new ArrayRealVector(out);
 
336
        return new ArrayRealVector(out, false);
289
337
    }
290
338
 
291
339
    /**
300
348
    }
301
349
 
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;
307
 
        }
308
 
        return new ArrayRealVector(out);
309
 
    }
310
 
 
311
 
    /** {@inheritDoc} */
 
351
    @Override
312
352
    public RealVector mapAddToSelf(double d) {
313
353
        for (int i = 0; i < data.length; i++) {
314
354
            data[i] = data[i] + d;
317
357
    }
318
358
 
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;
324
 
        }
325
 
        return new ArrayRealVector(out);
326
 
    }
327
 
 
328
 
    /** {@inheritDoc} */
 
360
    @Override
329
361
    public RealVector mapSubtractToSelf(double d) {
330
362
        for (int i = 0; i < data.length; i++) {
331
363
            data[i] = data[i] - d;
334
366
    }
335
367
 
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;
341
 
        }
342
 
        return new ArrayRealVector(out);
343
 
    }
344
 
 
345
 
    /** {@inheritDoc} */
 
369
    @Override
346
370
    public RealVector mapMultiplyToSelf(double d) {
347
371
        for (int i = 0; i < data.length; i++) {
348
372
            data[i] = data[i] * d;
351
375
    }
352
376
 
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;
358
 
        }
359
 
        return new ArrayRealVector(out);
360
 
    }
361
 
 
362
 
    /** {@inheritDoc} */
 
378
    @Override
363
379
    public RealVector mapDivideToSelf(double d) {
364
380
        for (int i = 0; i < data.length; i++) {
365
381
            data[i] = data[i] / d;
368
384
    }
369
385
 
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);
375
 
        }
376
 
        return new ArrayRealVector(out);
377
 
    }
378
 
 
379
 
    /** {@inheritDoc} */
 
387
    @Override
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);
385
393
    }
386
394
 
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]);
392
 
        }
393
 
        return new ArrayRealVector(out);
394
 
    }
395
 
 
396
 
    /** {@inheritDoc} */
 
396
    @Override
397
397
    public RealVector mapExpToSelf() {
398
398
        for (int i = 0; i < data.length; i++) {
399
399
            data[i] = Math.exp(data[i]);
402
402
    }
403
403
 
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]);
409
 
        }
410
 
        return new ArrayRealVector(out);
411
 
    }
412
 
 
413
 
    /** {@inheritDoc} */
 
405
    @Override
414
406
    public RealVector mapExpm1ToSelf() {
415
407
        for (int i = 0; i < data.length; i++) {
416
408
            data[i] = Math.expm1(data[i]);
419
411
    }
420
412
 
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]);
426
 
        }
427
 
        return new ArrayRealVector(out);
428
 
    }
429
 
 
430
 
    /** {@inheritDoc} */
 
414
    @Override
431
415
    public RealVector mapLogToSelf() {
432
416
        for (int i = 0; i < data.length; i++) {
433
417
            data[i] = Math.log(data[i]);
436
420
    }
437
421
 
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]);
443
 
        }
444
 
        return new ArrayRealVector(out);
445
 
    }
446
 
 
447
 
    /** {@inheritDoc} */
 
423
    @Override
448
424
    public RealVector mapLog10ToSelf() {
449
425
        for (int i = 0; i < data.length; i++) {
450
426
            data[i] = Math.log10(data[i]);
453
429
    }
454
430
 
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]);
460
 
        }
461
 
        return new ArrayRealVector(out);
462
 
    }
463
 
 
464
 
    /** {@inheritDoc} */
 
432
    @Override
465
433
    public RealVector mapLog1pToSelf() {
466
434
        for (int i = 0; i < data.length; i++) {
467
435
            data[i] = Math.log1p(data[i]);
470
438
    }
471
439
 
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]);
477
 
        }
478
 
        return new ArrayRealVector(out);
479
 
    }
480
 
 
481
 
    /** {@inheritDoc} */
 
441
    @Override
482
442
    public RealVector mapCoshToSelf() {
483
443
        for (int i = 0; i < data.length; i++) {
484
444
            data[i] = Math.cosh(data[i]);
487
447
    }
488
448
 
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]);
494
 
        }
495
 
        return new ArrayRealVector(out);
496
 
    }
497
 
 
498
 
    /** {@inheritDoc} */
 
450
    @Override
499
451
    public RealVector mapSinhToSelf() {
500
452
        for (int i = 0; i < data.length; i++) {
501
453
            data[i] = Math.sinh(data[i]);
504
456
    }
505
457
 
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]);
511
 
        }
512
 
        return new ArrayRealVector(out);
513
 
    }
514
 
 
515
 
    /** {@inheritDoc} */
 
459
    @Override
516
460
    public RealVector mapTanhToSelf() {
517
461
        for (int i = 0; i < data.length; i++) {
518
462
            data[i] = Math.tanh(data[i]);
521
465
    }
522
466
 
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]);
528
 
        }
529
 
        return new ArrayRealVector(out);
530
 
    }
531
 
 
532
 
    /** {@inheritDoc} */
 
468
    @Override
533
469
    public RealVector mapCosToSelf() {
534
470
        for (int i = 0; i < data.length; i++) {
535
471
            data[i] = Math.cos(data[i]);
538
474
    }
539
475
 
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]);
545
 
        }
546
 
        return new ArrayRealVector(out);
547
 
    }
548
 
 
549
 
    /** {@inheritDoc} */
 
477
    @Override
550
478
    public RealVector mapSinToSelf() {
551
479
        for (int i = 0; i < data.length; i++) {
552
480
            data[i] = Math.sin(data[i]);
555
483
    }
556
484
 
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]);
562
 
        }
563
 
        return new ArrayRealVector(out);
564
 
    }
565
 
 
566
 
    /** {@inheritDoc} */
 
486
    @Override
567
487
    public RealVector mapTanToSelf() {
568
488
        for (int i = 0; i < data.length; i++) {
569
489
            data[i] = Math.tan(data[i]);
572
492
    }
573
493
 
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]);
579
 
        }
580
 
        return new ArrayRealVector(out);
581
 
    }
582
 
 
583
 
    /** {@inheritDoc} */
 
495
    @Override
584
496
    public RealVector mapAcosToSelf() {
585
497
        for (int i = 0; i < data.length; i++) {
586
498
            data[i] = Math.acos(data[i]);
589
501
    }
590
502
 
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]);
596
 
        }
597
 
        return new ArrayRealVector(out);
598
 
    }
599
 
 
600
 
    /** {@inheritDoc} */
 
504
    @Override
601
505
    public RealVector mapAsinToSelf() {
602
506
        for (int i = 0; i < data.length; i++) {
603
507
            data[i] = Math.asin(data[i]);
606
510
    }
607
511
 
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]);
613
 
        }
614
 
        return new ArrayRealVector(out);
615
 
    }
616
 
 
617
 
    /** {@inheritDoc} */
 
513
    @Override
618
514
    public RealVector mapAtanToSelf() {
619
515
        for (int i = 0; i < data.length; i++) {
620
516
            data[i] = Math.atan(data[i]);
623
519
    }
624
520
 
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];
630
 
        }
631
 
        return new ArrayRealVector(out);
632
 
    }
633
 
 
634
 
    /** {@inheritDoc} */
 
522
    @Override
635
523
    public RealVector mapInvToSelf() {
636
524
        for (int i = 0; i < data.length; i++) {
637
525
            data[i] = 1.0 / data[i];
640
528
    }
641
529
 
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]);
647
 
        }
648
 
        return new ArrayRealVector(out);
649
 
    }
650
 
 
651
 
    /** {@inheritDoc} */
 
531
    @Override
652
532
    public RealVector mapAbsToSelf() {
653
533
        for (int i = 0; i < data.length; i++) {
654
534
            data[i] = Math.abs(data[i]);
657
537
    }
658
538
 
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]);
664
 
        }
665
 
        return new ArrayRealVector(out);
666
 
    }
667
 
 
668
 
    /** {@inheritDoc} */
 
540
    @Override
669
541
    public RealVector mapSqrtToSelf() {
670
542
        for (int i = 0; i < data.length; i++) {
671
543
            data[i] = Math.sqrt(data[i]);
674
546
    }
675
547
 
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]);
681
 
        }
682
 
        return new ArrayRealVector(out);
683
 
    }
684
 
 
685
 
    /** {@inheritDoc} */
 
549
    @Override
686
550
    public RealVector mapCbrtToSelf() {
687
551
        for (int i = 0; i < data.length; i++) {
688
552
            data[i] = Math.cbrt(data[i]);
691
555
    }
692
556
 
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]);
698
 
        }
699
 
        return new ArrayRealVector(out);
700
 
    }
701
 
 
702
 
    /** {@inheritDoc} */
 
558
    @Override
703
559
    public RealVector mapCeilToSelf() {
704
560
        for (int i = 0; i < data.length; i++) {
705
561
            data[i] = Math.ceil(data[i]);
708
564
    }
709
565
 
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]);
715
 
        }
716
 
        return new ArrayRealVector(out);
717
 
    }
718
 
 
719
 
    /** {@inheritDoc} */
 
567
    @Override
720
568
    public RealVector mapFloorToSelf() {
721
569
        for (int i = 0; i < data.length; i++) {
722
570
            data[i] = Math.floor(data[i]);
725
573
    }
726
574
 
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]);
732
 
        }
733
 
        return new ArrayRealVector(out);
734
 
    }
735
 
 
736
 
    /** {@inheritDoc} */
 
576
    @Override
737
577
    public RealVector mapRintToSelf() {
738
578
        for (int i = 0; i < data.length; i++) {
739
579
            data[i] = Math.rint(data[i]);
742
582
    }
743
583
 
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]);
749
 
        }
750
 
        return new ArrayRealVector(out);
751
 
    }
752
 
 
753
 
    /** {@inheritDoc} */
 
585
    @Override
754
586
    public RealVector mapSignumToSelf() {
755
587
        for (int i = 0; i < data.length; i++) {
756
588
            data[i] = Math.signum(data[i]);
759
591
    }
760
592
 
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]);
766
 
        }
767
 
        return new ArrayRealVector(out);
768
 
    }
769
 
 
770
 
    /** {@inheritDoc} */
 
594
    @Override
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 {
781
 
        try {
 
605
        if (v instanceof ArrayRealVector) {
782
606
            return ebeMultiply((ArrayRealVector) v);
783
 
        } catch (ClassCastException cce) {
 
607
        } else {
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);
788
612
            }
789
 
            return new ArrayRealVector(out);
 
613
            return new ArrayRealVector(out, false);
790
614
        }
791
615
    }
792
616
 
793
617
    /** {@inheritDoc} */
 
618
    @Override
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];
 
624
            out[i] *= v[i];
800
625
        }
801
 
        return new ArrayRealVector(out);
 
626
        return new ArrayRealVector(out, false);
802
627
    }
803
628
 
804
629
    /**
815
640
    /** {@inheritDoc} */
816
641
    public RealVector ebeDivide(RealVector v)
817
642
        throws IllegalArgumentException {
818
 
        try {
 
643
        if (v instanceof ArrayRealVector) {
819
644
            return ebeDivide((ArrayRealVector) v);
820
 
        } catch (ClassCastException cce) {
 
645
        } else {
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);
825
650
            }
826
 
            return new ArrayRealVector(out);
 
651
            return new ArrayRealVector(out, false);
827
652
        }
828
653
    }
829
654
 
830
655
    /** {@inheritDoc} */
 
656
    @Override
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];
 
662
                out[i] /= v[i];
837
663
        }
838
 
        return new ArrayRealVector(out);
 
664
        return new ArrayRealVector(out, false);
839
665
    }
840
666
 
841
667
    /**
850
676
    }
851
677
 
852
678
    /** {@inheritDoc} */
 
679
    @Override
853
680
    public double[] getData() {
854
681
        return data.clone();
855
682
    }
864
691
    }
865
692
 
866
693
    /** {@inheritDoc} */
 
694
    @Override
867
695
    public double dotProduct(RealVector v)
868
696
        throws IllegalArgumentException {
869
 
        try {
 
697
        if (v instanceof ArrayRealVector) {
870
698
            return dotProduct((ArrayRealVector) v);
871
 
        } catch (ClassCastException cce) {
 
699
        } else {
872
700
            checkVectorDimensions(v);
873
701
            double dot = 0;
874
 
            for (int i = 0; i < data.length; i++) {
875
 
                dot += data[i] * v.getEntry(i);
 
702
            Iterator<Entry> it = v.sparseIterator();
 
703
            Entry e;
 
704
            while(it.hasNext() && (e = it.next()) != null) {
 
705
                dot += data[e.getIndex()] * e.getValue();
876
706
            }
877
707
            return dot;
878
708
        }
879
709
    }
880
710
 
881
711
    /** {@inheritDoc} */
 
712
    @Override
882
713
    public double dotProduct(double[] v)
883
714
        throws IllegalArgumentException {
884
715
        checkVectorDimensions(v.length);
901
732
    }
902
733
 
903
734
    /** {@inheritDoc} */
 
735
    @Override
904
736
    public double getNorm() {
905
737
        double sum = 0;
906
738
        for (double a : data) {
910
742
    }
911
743
 
912
744
    /** {@inheritDoc} */
 
745
    @Override
913
746
    public double getL1Norm() {
914
747
        double sum = 0;
915
748
        for (double a : data) {
919
752
    }
920
753
 
921
754
    /** {@inheritDoc} */
 
755
    @Override
922
756
    public double getLInfNorm() {
923
757
        double max = 0;
924
758
        for (double a : data) {
925
 
            max += Math.max(max, Math.abs(a));
 
759
            max = Math.max(max, Math.abs(a));
926
760
        }
927
761
        return max;
928
762
    }
929
763
 
930
764
    /** {@inheritDoc} */
 
765
    @Override
931
766
    public double getDistance(RealVector v)
932
767
        throws IllegalArgumentException {
933
 
        try {
 
768
        if (v instanceof ArrayRealVector) {
934
769
            return getDistance((ArrayRealVector) v);
935
 
        } catch (ClassCastException cce) {
 
770
        } else {
936
771
            checkVectorDimensions(v);
937
772
            double sum = 0;
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;
941
776
            }
942
777
            return Math.sqrt(sum);
944
779
    }
945
780
 
946
781
    /** {@inheritDoc} */
 
782
    @Override
947
783
    public double getDistance(double[] v)
948
784
        throws IllegalArgumentException {
949
785
        checkVectorDimensions(v.length);
974
810
    }
975
811
 
976
812
    /** {@inheritDoc} */
 
813
    @Override
977
814
    public double getL1Distance(RealVector v)
978
815
        throws IllegalArgumentException {
979
 
        try {
 
816
        if (v instanceof ArrayRealVector) {
980
817
            return getL1Distance((ArrayRealVector) v);
981
 
        } catch (ClassCastException cce) {
 
818
        } else {
982
819
            checkVectorDimensions(v);
983
820
            double sum = 0;
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);
987
824
            }
988
825
            return sum;
990
827
    }
991
828
 
992
829
    /** {@inheritDoc} */
 
830
    @Override
993
831
    public double getL1Distance(double[] v)
994
832
        throws IllegalArgumentException {
995
833
        checkVectorDimensions(v.length);
1020
858
    }
1021
859
 
1022
860
    /** {@inheritDoc} */
 
861
    @Override
1023
862
    public double getLInfDistance(RealVector v)
1024
863
        throws IllegalArgumentException {
1025
 
        try {
 
864
        if (v instanceof ArrayRealVector) {
1026
865
            return getLInfDistance((ArrayRealVector) v);
1027
 
        } catch (ClassCastException cce) {
 
866
        } else {
1028
867
            checkVectorDimensions(v);
1029
868
            double max = 0;
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));
1033
872
            }
1034
873
            return max;
1036
875
    }
1037
876
 
1038
877
    /** {@inheritDoc} */
 
878
    @Override
1039
879
    public double getLInfDistance(double[] v)
1040
880
        throws IllegalArgumentException {
1041
881
        checkVectorDimensions(v.length);
1066
906
    }
1067
907
 
1068
908
    /** {@inheritDoc} */
 
909
    @Override
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");
1073
914
        }
1074
 
        return mapDivide(getNorm());
 
915
        return mapDivide(norm);
1075
916
    }
1076
917
 
1077
918
    /** {@inheritDoc} */
 
919
    @Override
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");
1082
924
        }
1083
 
        for (int i = 0; i < data.length; i++) {
1084
 
            data[i] /= norm;
1085
 
        }
 
925
        mapDivideToSelf(norm);
1086
926
    }
1087
927
 
1088
928
    /** {@inheritDoc} */
1091
931
    }
1092
932
 
1093
933
    /** {@inheritDoc} */
 
934
    @Override
1094
935
    public RealVector projection(double[] v) {
1095
936
        return projection(new ArrayRealVector(v, false));
1096
937
    }
1105
946
    }
1106
947
 
1107
948
    /** {@inheritDoc} */
 
949
    @Override
1108
950
    public RealMatrix outerProduct(RealVector v)
1109
951
        throws IllegalArgumentException {
1110
 
        try {
 
952
        if (v instanceof ArrayRealVector) {
1111
953
            return outerProduct((ArrayRealVector) v);
1112
 
        } catch (ClassCastException cce) {
 
954
        } else {
1113
955
            checkVectorDimensions(v);
1114
956
            final int m = data.length;
1115
957
            final RealMatrix out = MatrixUtils.createRealMatrix(m, m);
1134
976
    }
1135
977
 
1136
978
    /** {@inheritDoc} */
 
979
    @Override
1137
980
    public RealMatrix outerProduct(double[] v)
1138
981
        throws IllegalArgumentException {
1139
982
        checkVectorDimensions(v.length);
1160
1003
    /** {@inheritDoc} */
1161
1004
    public RealVector append(RealVector v) {
1162
1005
        try {
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);
1166
1009
        }
1167
1010
    }
1168
1011
 
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);
1184
1027
    }
1185
1028
 
1186
1029
    /** {@inheritDoc} */
1210
1053
    }
1211
1054
 
1212
1055
    /** {@inheritDoc} */
 
1056
    @Override
1213
1057
    public void setSubVector(int index, RealVector v) {
1214
1058
        try {
1215
1059
            try {
1226
1070
    }
1227
1071
 
1228
1072
    /** {@inheritDoc} */
 
1073
    @Override
1229
1074
    public void setSubVector(int index, double[] v) {
1230
1075
        try {
1231
1076
            System.arraycopy(v, 0, data, index, v.length);
1237
1082
 
1238
1083
    /**
1239
1084
     * Set a set of consecutive elements.
1240
 
     * 
 
1085
     *
1241
1086
     * @param index index of first element to be set.
1242
1087
     * @param v vector containing the values to set.
1243
1088
     * @exception MatrixIndexException if the index is
1249
1094
    }
1250
1095
 
1251
1096
    /** {@inheritDoc} */
 
1097
    @Override
1252
1098
    public void set(double value) {
1253
1099
        Arrays.fill(data, value);
1254
1100
    }
1255
1101
 
1256
1102
    /** {@inheritDoc} */
 
1103
    @Override
1257
1104
    public double[] toArray(){
1258
1105
        return data.clone();
1259
1106
    }
1270
1117
     * @exception IllegalArgumentException if the vectors do not
1271
1118
     * have the same dimension
1272
1119
     */
 
1120
    @Override
1273
1121
    protected void checkVectorDimensions(RealVector v)
1274
1122
        throws IllegalArgumentException {
1275
1123
        checkVectorDimensions(v.getDimension());
1277
1125
 
1278
1126
    /**
1279
1127
     * Check if instance dimension is equal to some expected value.
1280
 
     * 
 
1128
     *
1281
1129
     * @param n expected dimension.
1282
1130
     * @exception IllegalArgumentException if the dimension is
1283
1131
     * inconsistent with vector size
1284
1132
     */
 
1133
    @Override
1285
1134
    protected void checkVectorDimensions(int n)
1286
1135
        throws IllegalArgumentException {
1287
1136
        if (data.length != n) {
1303
1152
        }
1304
1153
        return false;
1305
1154
    }
1306
 
    
 
1155
 
1307
1156
    /**
1308
1157
     * Returns true if any coordinate of this vector is infinite and none are NaN;
1309
1158
     * false otherwise
1325
1174
        return false;
1326
1175
 
1327
1176
    }
1328
 
    
 
1177
 
1329
1178
    /**
1330
1179
     * Test for the equality of two real vectors.
1331
1180
     * <p>
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
1346
 
     * 
 
1195
     *
1347
1196
     */
1348
1197
    @Override
1349
1198
    public boolean equals(Object other) {
1350
1199
 
1351
 
      if (this == other) { 
 
1200
      if (this == other) {
1352
1201
        return true;
1353
1202
      }
1354
1203
 
1355
 
      if (other == null) {
1356
 
        return false;
1357
 
      }
1358
 
 
1359
 
      try {
1360
 
 
1361
 
          RealVector rhs = (RealVector) other;
1362
 
          if (data.length != rhs.getDimension()) {
1363
 
              return false;
1364
 
          }
1365
 
 
1366
 
          if (rhs.isNaN()) {
1367
 
              return this.isNaN();
1368
 
          }
1369
 
 
1370
 
          for (int i = 0; i < data.length; ++i) {
1371
 
              if (data[i] != rhs.getEntry(i)) {
1372
 
                  return false;
1373
 
              }
1374
 
          }
1375
 
          return true;
1376
 
 
1377
 
      } catch (ClassCastException ex) {
1378
 
          // ignore exception
 
1204
      if (other == null || !(other instanceof RealVector)) {
 
1205
        return false;
 
1206
      }
 
1207
 
 
1208
 
 
1209
      RealVector rhs = (RealVector) other;
 
1210
      if (data.length != rhs.getDimension()) {
 
1211
        return false;
 
1212
      }
 
1213
 
 
1214
      if (rhs.isNaN()) {
 
1215
        return this.isNaN();
 
1216
      }
 
1217
 
 
1218
      for (int i = 0; i < data.length; ++i) {
 
1219
        if (data[i] != rhs.getEntry(i)) {
1379
1220
          return false;
 
1221
        }
1380
1222
      }
 
1223
      return true;
 
1224
    }
1381
1225
 
1382
 
    }
1383
 
    
1384
1226
    /**
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);
1395
1237
    }
1396
1238
 
1397
 
    /**
1398
 
     * Check if an index is valid.
1399
 
     * @param index index to check
1400
 
     * @exception MatrixIndexException if index is not valid
1401
 
     */
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);
1408
 
        }
1409
 
    }
1410
 
 
1411
1239
}