~ubuntu-branches/ubuntu/trusty/weka/trusty-proposed

« back to all changes in this revision

Viewing changes to weka/core/matrix/DoubleVector.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or (at
 
5
 *    your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful, but
 
8
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
10
 *    General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
15
 
 
16
/*
 
17
 *    DoubleVector.java
 
18
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
19
 *
 
20
 */
 
21
 
 
22
package weka.core.matrix;
 
23
 
 
24
import java.lang.reflect.Method;
 
25
import java.util.Arrays;
 
26
 
 
27
/**
 
28
 * A vector specialized on doubles.
 
29
 * 
 
30
 * @author Yong Wang
 
31
 * @version $Revision: 1.3 $
 
32
 */
 
33
public class  DoubleVector implements Cloneable {
 
34
 
 
35
  double[] V; // array for internal storage of elements.
 
36
 
 
37
  private int  sizeOfVector;      // size of the vector
 
38
  
 
39
  /* ------------------------
 
40
     Constructors
 
41
     * ------------------------ */
 
42
 
 
43
  /** Constructs a null vector.
 
44
   */
 
45
  public DoubleVector() {
 
46
    this( 0 );
 
47
  }
 
48
    
 
49
  /** Constructs an n-vector of zeros. 
 
50
      @param n    length.
 
51
  */
 
52
  public DoubleVector( int n ){
 
53
    V = new double[ n ];
 
54
    setSize( n );
 
55
  }
 
56
    
 
57
  /** Constructs a constant n-vector.
 
58
      @param n    length.
 
59
      @param s    the scalar value used to fill the vector
 
60
  */
 
61
  public DoubleVector( int n, double s ){
 
62
    this( n );
 
63
    set( s );
 
64
  }
 
65
    
 
66
  /** Constructs a vector directly from a double array
 
67
   *  @param v   the array
 
68
   */
 
69
  public DoubleVector( double v[] ){
 
70
    if( v == null ) {
 
71
      V = new double[0];
 
72
      setSize( 0 );
 
73
    }
 
74
    else {
 
75
      V = v;
 
76
      setSize( v.length );
 
77
    }
 
78
  }
 
79
    
 
80
  /* ------------------------
 
81
   *  Public Methods
 
82
   * ------------------------ */
 
83
    
 
84
  /** Set a single element.
 
85
   *  @param i    Index.
 
86
   *  @param s    a[i].
 
87
   */
 
88
  public void  set( int i, double s ) {
 
89
    
 
90
    V[i] = s;
 
91
  }
 
92
    
 
93
  /** Set all elements to a value
 
94
   *  @param s    the value
 
95
   */
 
96
  public void  set( double s ) {
 
97
    set(0, size()-1, s);
 
98
  }
 
99
 
 
100
  /** Set some elements to a value
 
101
   *  @param i0 the index of the first element
 
102
   *  @param i1 the index of the second element
 
103
   *  @param s the value 
 
104
   */
 
105
  public void set( int i0, int i1, double s ) {
 
106
 
 
107
    for(int i = i0; i <= i1; i++ )
 
108
      V[i] = s;
 
109
  }
 
110
 
 
111
  /** Set some elements using a 2-D array
 
112
   *  @param i0 the index of the first element
 
113
   *  @param i1 the index of the second element
 
114
   *  @param j0 the index of the starting element in the 2-D array
 
115
   *  @param v the values
 
116
   */
 
117
  public void  set( int i0, int i1, double [] v, int j0){
 
118
    for(int i = i0; i<= i1; i++)
 
119
      V[i] = v[j0 + i - i0];
 
120
  }
 
121
    
 
122
  /** Set the elements using a DoubleVector
 
123
   *  @param v the DoubleVector
 
124
   */
 
125
  public void  set( DoubleVector v ){
 
126
    set( 0, v.size() - 1, v, 0);
 
127
  }
 
128
  
 
129
  /** Set some elements using a DoubleVector.
 
130
   *  @param i0 the index of the first element
 
131
   *  @param i1 the index of the second element
 
132
   *  @param v the DoubleVector
 
133
   *  @param j0 the index of the starting element in the DoubleVector
 
134
   */
 
135
  public void  set( int i0, int i1, DoubleVector v, int j0){
 
136
    for(int i = i0; i<= i1; i++)
 
137
      V[i] = v.V[j0 + i - i0];
 
138
  }
 
139
  
 
140
  /** Access the internal one-dimensional array.
 
141
      @return     Pointer to the one-dimensional array of vector elements.
 
142
  */
 
143
  public double []  getArray() {
 
144
    return V;
 
145
  }
 
146
    
 
147
  void  setArray( double [] a ) {
 
148
    V = a;
 
149
  }
 
150
 
 
151
  /** Returns a copy of the DoubleVector usng a double array.
 
152
      @return the one-dimensional array.  */
 
153
  public double[] getArrayCopy() {
 
154
    double v[] = new double[size()];
 
155
    
 
156
    for(int i= 0; i < size(); i++ ) 
 
157
      v[i] = V[i];
 
158
    
 
159
    return v;
 
160
  }
 
161
    
 
162
  /** Sorts the array in place */
 
163
  public void  sort() {
 
164
    Arrays.sort( V, 0, size() );
 
165
  }
 
166
 
 
167
  /** Sorts the array in place with index returned */
 
168
  public IntVector  sortWithIndex() {
 
169
    IntVector index = IntVector.seq( 0, size()-1 );
 
170
    sortWithIndex( 0, size()-1, index );
 
171
    return index;
 
172
  }
 
173
  
 
174
  /** Sorts the array in place with index changed 
 
175
   *  @param xi   first index
 
176
   *  @param xj   last index
 
177
   *  @param index array that stores all indices
 
178
   */
 
179
  public void  sortWithIndex( int xi, int xj, IntVector index ) {
 
180
    if( xi < xj ) { 
 
181
      double x, f, k;
 
182
      int xm = (int) (xi + xj) / 2; // median index
 
183
      x = Math.min( V[xi],             // median of three
 
184
                    Math.max( V[xm], V[xj])); 
 
185
      int i = xi;
 
186
      int j = xj;
 
187
      while( i < j ) {
 
188
        while( V[i] < x && i < xj ) i++;
 
189
        while( V[j] > x && j > xi ) j--;
 
190
        if( i <= j ){
 
191
          swap(i, j);
 
192
          index.swap(i, j);
 
193
          i++;
 
194
          j--;
 
195
        }
 
196
      }
 
197
      sortWithIndex(xi, j, index);
 
198
      sortWithIndex(i, xj, index);
 
199
    }
 
200
  }
 
201
  
 
202
  /** Gets the size of the vector.
 
203
      @return     the size
 
204
  */
 
205
  public int  size(){
 
206
    return sizeOfVector;
 
207
  }
 
208
    
 
209
  /** 
 
210
   *  Sets the size of the vector
 
211
   *  @param m the size
 
212
   */ 
 
213
  public void  setSize( int m ){
 
214
    if( m > capacity() ) 
 
215
      throw new IllegalArgumentException("insufficient capacity");
 
216
    sizeOfVector = m;
 
217
  }
 
218
    
 
219
  /** Gets the capacity of the vector.
 
220
   *  @return     the capacity.
 
221
   */
 
222
  public int  capacity() {
 
223
    if( V == null ) return 0;
 
224
    return V.length;
 
225
  } 
 
226
 
 
227
  /** Sets the capacity of the vector
 
228
   *  @param n the capacity.  
 
229
   */
 
230
  public void  setCapacity ( int n ) {
 
231
    if( n == capacity() ) return;
 
232
    double [] oldV = V;
 
233
    int m = Math.min( n, size() );
 
234
    V = new double[ n ];
 
235
    setSize( m );
 
236
    set(0, m-1, oldV, 0);
 
237
  }
 
238
 
 
239
  /** Gets a single element.
 
240
   *  @param i    Index.
 
241
   *  @return     the value of the i-th element
 
242
   */
 
243
  public double  get( int i ) {
 
244
    return V[i];
 
245
  }
 
246
    
 
247
  /** 
 
248
   *  Adds a value to an element 
 
249
   *  @param i  the index of the element 
 
250
   *  @param s the value
 
251
   */
 
252
  public void  setPlus( int i, double s ) {
 
253
    V[i] += s;
 
254
  }
 
255
    
 
256
  /** 
 
257
   *  Multiplies a value to an element 
 
258
   *  @param i  the index of the element 
 
259
   *  @param s the value
 
260
   */
 
261
  public void  setTimes( int i, double s ) {
 
262
    V[i] *= s;
 
263
  }
 
264
    
 
265
  /**
 
266
   *  Adds an element into the vector
 
267
   *  @param x  the value of the new element
 
268
   */
 
269
  public void addElement( double x ) {
 
270
    if( capacity() == 0 ) setCapacity( 10 );
 
271
    if( size() == capacity() ) setCapacity( 2 * capacity() );
 
272
    V[size()] = x;
 
273
    setSize( size() + 1 );
 
274
  }
 
275
  
 
276
  /**
 
277
   *  Returns the squared vector 
 
278
   */
 
279
  public DoubleVector square() {
 
280
    DoubleVector v = new DoubleVector( size() ); 
 
281
    for(int i = 0; i < size(); i++ ) v.V[i] = V[i] * V[i];
 
282
    return v;
 
283
  }
 
284
 
 
285
  /**
 
286
   *  Returns the square-root of all the elements in the vector 
 
287
   */
 
288
  public DoubleVector sqrt() {
 
289
    DoubleVector v = new DoubleVector( size() ); 
 
290
    for(int i = 0; i < size(); i++ ) v.V[i] = Math.sqrt(V[i]);
 
291
    return v;
 
292
  }
 
293
 
 
294
  /** Makes a deep copy of the vector
 
295
   */
 
296
  public DoubleVector  copy() { 
 
297
    return (DoubleVector) clone();
 
298
  }
 
299
    
 
300
  /** Clones the DoubleVector object.
 
301
   */
 
302
  public Object  clone() { 
 
303
    int n = size();
 
304
    DoubleVector u = new DoubleVector( n );
 
305
    for( int i = 0; i < n; i++) 
 
306
      u.V[i] = V[i];
 
307
    return u;
 
308
  }
 
309
    
 
310
  /** 
 
311
   * Returns the inner product of two DoubleVectors
 
312
   * @param v the second DoubleVector
 
313
   * @return the product
 
314
   */
 
315
  public double  innerProduct(DoubleVector v) {
 
316
    if(size() != v.size()) 
 
317
      throw new IllegalArgumentException("sizes unmatch");
 
318
    double p = 0;
 
319
    for (int i = 0; i < size(); i++) {
 
320
      p += V[i] * v.V[i];
 
321
    }
 
322
    return p;
 
323
  }
 
324
    
 
325
  /** 
 
326
   * Returns the signs of all elements in terms of -1, 0 and +1.
 
327
   */
 
328
  public DoubleVector sign() 
 
329
  {
 
330
    DoubleVector s = new DoubleVector( size() );
 
331
    for( int i = 0; i < size(); i++ ) {
 
332
      if( V[i] > 0 ) s.V[i] = 1;
 
333
      else if( V[i] < 0 ) s.V[i] = -1;
 
334
      else s.V[i] = 0;
 
335
    }
 
336
    return s;
 
337
  } 
 
338
 
 
339
  /** Returns the sum of all elements in the vector.
 
340
   */
 
341
  public double  sum() 
 
342
  {
 
343
    double s = 0;
 
344
    for( int i=0; i< size(); i++) s += V[i];
 
345
    return s;
 
346
  }
 
347
    
 
348
  /** Returns the squared sum of all elements in the vector.
 
349
   */
 
350
  public double  sum2()
 
351
  {
 
352
    double s2 = 0;
 
353
    for( int i=0; i< size(); i++) s2 += V[i] * V[i];
 
354
    return s2;
 
355
  }
 
356
  
 
357
  /** Returns the L1-norm of the vector
 
358
   */
 
359
  public double norm1()
 
360
  {
 
361
    double s = 0;
 
362
    for( int i=0; i< size(); i++) s += Math.abs(V[i]);
 
363
    return s;
 
364
  }
 
365
  
 
366
  /** Returns the L2-norm of the vector
 
367
   */
 
368
  public double norm2()
 
369
  {
 
370
    return Math.sqrt( sum2() );
 
371
  }
 
372
 
 
373
  /** Returns ||u-v||^2
 
374
   *  @param v the second vector
 
375
   */
 
376
  public double sum2( DoubleVector v ) 
 
377
  {
 
378
    return minus( v ).sum2();
 
379
  }
 
380
  
 
381
  /** Returns a subvector.
 
382
   *  @param i0   the index of the first element
 
383
   *  @param i1   the index of the last element
 
384
   *  @return     v[i0:i1]
 
385
   */
 
386
  public DoubleVector  subvector( int i0, int i1 ) 
 
387
  {
 
388
    DoubleVector v = new DoubleVector( i1-i0+1 );
 
389
    v.set(0, i1 - i0, this, i0);
 
390
    return v;
 
391
  }
 
392
  
 
393
  /** Returns a subvector.
 
394
   *  @param index stores the indices of the needed elements
 
395
   *  @return     v[index]
 
396
   */
 
397
  public DoubleVector  subvector( IntVector index ) {
 
398
    DoubleVector v = new DoubleVector( index.size() );
 
399
    for( int i = 0; i < index.size(); i++ )
 
400
      v.V[i] = V[index.V[i]];
 
401
    return v;
 
402
  }
 
403
 
 
404
  /** Returns a vector from the pivoting indices. Elements not indexed are
 
405
   *  set to zero.
 
406
   *  @param index stores the pivoting indices
 
407
   *  @param length the total number of the potential elements
 
408
   *  @return the subvector */
 
409
  public DoubleVector  unpivoting( IntVector index, int length ) {
 
410
    if( index.size() > length ) 
 
411
      throw new IllegalArgumentException("index.size() > length ");
 
412
    DoubleVector u = new DoubleVector( length );
 
413
    for( int i = 0; i < index.size(); i++ ) {
 
414
      u.V[index.V[i]] =  V[i];
 
415
    }
 
416
    return u;
 
417
  }
 
418
  
 
419
  /** Adds a value to all the elements 
 
420
   *  @param x the value
 
421
   */
 
422
  public DoubleVector  plus ( double x ) {
 
423
    return copy().plusEquals( x );      
 
424
  }
 
425
  
 
426
  /** Adds a value to all the elements in place
 
427
   *  @param x the value
 
428
   */
 
429
  public DoubleVector plusEquals ( double x ) {
 
430
    for( int i = 0; i < size(); i++ )
 
431
      V[i] += x;
 
432
    return this;
 
433
  }
 
434
  
 
435
  /** 
 
436
   *  Adds another vector element by element 
 
437
   *  @param v the second vector
 
438
   */
 
439
  public DoubleVector  plus( DoubleVector v ) {
 
440
    return copy().plusEquals( v );
 
441
  }
 
442
  
 
443
  /** 
 
444
   *  Adds another vector in place element by element 
 
445
   *  @param v the second vector
 
446
   */
 
447
  public DoubleVector  plusEquals( DoubleVector v ) {
 
448
    for(int i = 0; i < size(); i++ )
 
449
      V[i] += v.V[i];
 
450
    return this;
 
451
  }
 
452
  
 
453
  /** 
 
454
   *  Subtracts a value
 
455
   *  @param x the value
 
456
   */
 
457
  public DoubleVector  minus( double x ) {
 
458
    return plus( -x );
 
459
  }
 
460
  
 
461
  /** 
 
462
   *  Subtracts a value in place
 
463
   *  @param x the value
 
464
   */
 
465
  public DoubleVector  minusEquals( double x ) {
 
466
    plusEquals( -x );
 
467
    return this;
 
468
  }
 
469
  
 
470
  /** 
 
471
   *  Subtracts another DoubleVector element by element 
 
472
   *  @param v the second DoubleVector
 
473
   */
 
474
  public DoubleVector  minus( DoubleVector v ) {
 
475
    return copy().minusEquals( v );
 
476
  }
 
477
  
 
478
  /** 
 
479
   *  Subtracts another DoubleVector element by element in place
 
480
   *  @param v the second DoubleVector
 
481
   */
 
482
  public DoubleVector  minusEquals( DoubleVector v ) {
 
483
    for(int i = 0; i < size(); i++ )
 
484
      V[i] -=  v.V[i];
 
485
    return this;
 
486
  }
 
487
    
 
488
  /** Multiplies a scalar
 
489
      @param s    scalar
 
490
      @return     s * v
 
491
  */
 
492
  public DoubleVector  times( double s ) {
 
493
    return copy().timesEquals( s );
 
494
  }
 
495
    
 
496
  /** Multiply a vector by a scalar in place, u = s * u
 
497
      @param s    scalar
 
498
      @return     replace u by s * u
 
499
  */
 
500
  public DoubleVector  timesEquals( double s ) {
 
501
    for (int i = 0; i < size(); i++) {
 
502
      V[i] *= s;
 
503
    }
 
504
    return this;
 
505
  }
 
506
    
 
507
  /** 
 
508
   *  Multiplies another DoubleVector element by element
 
509
   *  @param v the second DoubleVector
 
510
   */
 
511
  public DoubleVector  times( DoubleVector v ) {
 
512
    return copy().timesEquals( v ); 
 
513
    
 
514
  }
 
515
    
 
516
  /** 
 
517
   *  Multiplies another DoubleVector element by element in place 
 
518
   *  @param v the second DoubleVector
 
519
   */
 
520
  public DoubleVector  timesEquals( DoubleVector v ) {
 
521
    for(int i = 0; i < size(); i++ )
 
522
      V[i] *= v.V[i];
 
523
    return this;
 
524
  }
 
525
  
 
526
  /** 
 
527
   *  Divided by another DoubleVector element by element
 
528
   *  @param v the second DoubleVector
 
529
   */
 
530
  public DoubleVector  dividedBy ( DoubleVector v ) {
 
531
    return copy().dividedByEquals( v );
 
532
  }
 
533
  
 
534
  /** 
 
535
   *  Divided by another DoubleVector element by element in place 
 
536
   *  @param v the second DoubleVector
 
537
   */
 
538
  public DoubleVector  dividedByEquals ( DoubleVector v ) {
 
539
    for( int i = 0; i < size(); i++ ) {
 
540
      V[i] /= v.V[i];
 
541
    }
 
542
    return this;
 
543
  }
 
544
  
 
545
  /** 
 
546
   *  Checks if it is an empty vector
 
547
   */
 
548
  public boolean  isEmpty() {
 
549
    if( size() == 0 ) return true;
 
550
    return false;
 
551
  }
 
552
  
 
553
  /** 
 
554
   * Returns a vector that stores the cumulated values of the original
 
555
   * vector */
 
556
  public DoubleVector cumulate() 
 
557
  {
 
558
    return copy().cumulateInPlace();
 
559
  }
 
560
        
 
561
  /** 
 
562
   * Cumulates the original vector in place 
 
563
   */
 
564
  public DoubleVector cumulateInPlace() 
 
565
  {
 
566
    for (int i = 1; i < size(); i++) {
 
567
      V[i] += V[i-1];
 
568
    }
 
569
    return this;
 
570
  }
 
571
        
 
572
  /** 
 
573
   * Returns the index of the maximum. <p>
 
574
   * If multiple maximums exist, the index of the first is returned.
 
575
   */
 
576
  public int  indexOfMax()
 
577
  {
 
578
    int index = 0;
 
579
    double ma = V[0];
 
580
 
 
581
    for( int i = 1; i < size(); i++ ){
 
582
      if( ma < V[i] ) {
 
583
        ma = V[i];
 
584
        index = i;
 
585
      }
 
586
    }
 
587
    return index;
 
588
  }
 
589
  
 
590
 
 
591
  /** 
 
592
   * Returns true if vector not sorted
 
593
   */
 
594
  public boolean unsorted () {
 
595
    if( size() < 2 ) return false;
 
596
    for( int i = 1; i < size(); i++ ) {
 
597
      if( V[i-1] > V[i] )
 
598
        return true;
 
599
    }
 
600
    return false;
 
601
  }
 
602
  
 
603
  /**
 
604
   *  Combine two vectors together
 
605
   *  @param v the second vector
 
606
   */
 
607
  public DoubleVector  cat( DoubleVector v ) {
 
608
    DoubleVector w = new DoubleVector( size() + v.size() );
 
609
    w.set(0, size() - 1, this, 0);
 
610
    w.set(size(), size() + v.size()-1, v, 0);
 
611
    return w;
 
612
  }
 
613
    
 
614
  /**
 
615
   *  Swaps the values stored at i and j
 
616
   *  @param i the index i
 
617
   *  @param j the index j
 
618
   */
 
619
  public void  swap( int i, int j ){
 
620
    if( i == j ) return;
 
621
    double t = V[i];
 
622
    V[i] = V[j];
 
623
    V[j] = t;
 
624
  }
 
625
 
 
626
  /**
 
627
   *  Returns the maximum value of all elements
 
628
   */
 
629
  public double max () {
 
630
    if( size() < 1 ) throw new IllegalArgumentException("zero size");
 
631
    double ma = V[0];
 
632
    if( size() < 2 ) return ma;
 
633
    for( int i = 1; i < size(); i++ ) {
 
634
      if( V[i] > ma ) ma = V[i];
 
635
    }
 
636
    return ma;
 
637
  }
 
638
  
 
639
 
 
640
  /**
 
641
   *  Applies a method to the vector
 
642
   *  @param className the class name
 
643
   *  @param method the method
 
644
   */
 
645
  public DoubleVector map( String className, String method ) {
 
646
    try {
 
647
      Class c = Class.forName( className );
 
648
      Class [] cs = new Class[1]; 
 
649
      cs[ 0 ] = Double.TYPE;
 
650
      Method m = c.getMethod( method, cs );
 
651
      
 
652
      DoubleVector w = new DoubleVector( size() );
 
653
      Object [] obj = new Object[1];
 
654
      for( int i = 0; i < size(); i++ ) {
 
655
        obj[0] = new Double( V[i] );
 
656
        w.set( i, Double.parseDouble(m.invoke( null, obj ).toString()) ); 
 
657
      }
 
658
      return w;
 
659
    }
 
660
    catch ( Exception e ) {
 
661
      e.printStackTrace();
 
662
      System.exit(1);
 
663
    }
 
664
    return null;
 
665
  }
 
666
 
 
667
  /**
 
668
   * Returns the reverse vector
 
669
   */ 
 
670
  public DoubleVector  rev() {
 
671
    int n = size();
 
672
    DoubleVector w = new DoubleVector( n );
 
673
    for(int i = 0; i < n; i++ )
 
674
      w.V[i] = V[n-i-1];
 
675
    return w;
 
676
  }
 
677
  
 
678
  /**
 
679
   * Returns a random vector of uniform distribution
 
680
   * @param n the size of the vector
 
681
   */ 
 
682
  public static DoubleVector  random( int n ) {
 
683
    DoubleVector v = new DoubleVector( n );
 
684
    for (int i = 0; i < n; i++) {
 
685
      v.V[i] = Math.random();
 
686
    }
 
687
    return v;
 
688
  }
 
689
 
 
690
  /** Convert the DoubleVecor to a string
 
691
   */ 
 
692
  public String  toString() {
 
693
    return toString( 5, false );
 
694
  }
 
695
    
 
696
  /** Convert the DoubleVecor to a string
 
697
   *  @param digits the number of digits after decimal point
 
698
   *  @param trailing true if trailing zeros are to be shown
 
699
   */ 
 
700
  public String  toString( int digits, boolean trailing ) {
 
701
    if( isEmpty() ) return "null vector";
 
702
 
 
703
    StringBuffer text = new StringBuffer();
 
704
    FlexibleDecimalFormat nf = new FlexibleDecimalFormat( digits, 
 
705
                                                          trailing );
 
706
    nf.grouping( true );
 
707
    for( int i = 0; i < size(); i ++ ) nf.update( V[i] );
 
708
    int count = 0;
 
709
    int width = 80;
 
710
    String number;
 
711
    for( int i = 0; i < size(); i++ ) {
 
712
      number = nf.format(V[i]);
 
713
      count += 1 + number.length();
 
714
      if( count > width-1 ) { 
 
715
        text.append('\n'); 
 
716
        count = 1 + number.length();
 
717
      }
 
718
      text.append( " " + number );
 
719
    }
 
720
        
 
721
    return text.toString();
 
722
  }
 
723
 
 
724
  public static void  main( String args[] ) {
 
725
 
 
726
    
 
727
    DoubleVector u = random(10);
 
728
    DoubleVector v = random(10);
 
729
    DoubleVector a = random(10);
 
730
    DoubleVector w = a; 
 
731
 
 
732
    System.out.println( random(10).plus(v).plus(w) );
 
733
 
 
734
  }
 
735
}
 
736