summaryrefslogtreecommitdiffstats
path: root/src/javax/vecmath/GVector.java
blob: 4718ae8fd22be44b9c727ed1aac173c62fb49cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
 * $RCSfile$
 *
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

package javax.vecmath;

import java.lang.Math;

/**
 * A double precision, general, dynamically-resizable,
 * one-dimensional vector class.  Index numbering begins with zero.
 */

public class GVector implements java.io.Serializable, Cloneable {

    private int length;
    double[] values;

    // Compatible with 1.1
    static final long serialVersionUID = 1398850036893875112L;

    /**
     * Constructs a new GVector of the specified
     * length with all vector elements initialized to 0.
     * @param length the number of elements in this GVector.
     */
    public GVector(int length)
	{
	    int i;
            
            this.length = length;	    
	    values = new double[length];
	    for(i = 0; i < length; i++) values[i] = 0.0;
	}
    
    /**
     * Constructs a new GVector from the specified array elements.
     * The length of this GVector is set to the length of the
     * specified array.  The array elements are copied into this new
     * GVector.
     * @param vector the values for the new GVector.
     */
    public GVector(double[] vector)
	{
	    int i;
	    
            length = vector.length;	    
	    values = new double[vector.length];
	    for(i = 0; i < length; i++) values[i] = vector[i];
	}
							  
    /**
     * Constructs a new GVector from the specified vector.
     * The vector elements are copied into this new GVector.
     * @param vector the source GVector for this new GVector.
     */
    public GVector(GVector vector)
	{
	    int i;
	
	    values = new double[vector.length];
	    length = vector.length;
	    for(i = 0; i < length; i++) values[i] = vector.values[i];
	}

    /**
      * Constructs a new GVector and copies the initial values
      * from the specified tuple.
      * @param tuple the source for the new GVector's initial values
      */
    public GVector(Tuple2f tuple)
        {
            values = new double[2];
            values[0] = (double)tuple.x;
            values[1] = (double)tuple.y;
	    length = 2;
        }

    /** 
      * Constructs a new GVector and copies the initial values
      * from the specified tuple.
      * @param tuple the source for the new GVector's initial values
      */ 
    public GVector(Tuple3f tuple)
	{
	    values = new double[3];
	    values[0] = (double)tuple.x;
	    values[1] = (double)tuple.y;
	    values[2] = (double)tuple.z;
	    length = 3;
	}

    /** 
      * Constructs a new GVector and copies the initial values
      * from the specified tuple.
      * @param tuple the source for the new GVector's initial values
      */ 
    public GVector(Tuple3d tuple)
	{
	    values = new double[3];
	    values[0] = tuple.x;
	    values[1] = tuple.y;
	    values[2] = tuple.z;
	    length = 3;
	}

    /** 
      * Constructs a new GVector and copies the initial values
      * from the specified tuple.
      * @param tuple the source for the new GVector's initial values
      */ 
    public GVector(Tuple4f tuple)
	{
	    values = new double[4];
	    values[0] = (double)tuple.x;
	    values[1] = (double)tuple.y;
	    values[2] = (double)tuple.z;
	    values[3] = (double)tuple.w;
	    length = 4;
	}

    /** 
      * Constructs a new GVector and copies the initial values
      * from the specified tuple.
      * @param tuple the source for the new GVector's initial values
      */ 
    public GVector(Tuple4d tuple)
	{
	    values = new double[4];
	    values[0] = tuple.x;
	    values[1] = tuple.y;
	    values[2] = tuple.z;
	    values[3] = tuple.w;
	    length = 4;
	}

    /**
     * Constructs a new GVector of the specified length and
     * initializes it by copying the specified number of elements from
     * the specified array.  The array must contain at least
     * <code>length</code> elements (i.e., <code>vector.length</code> >=
     * <code>length</code>.  The length of this new GVector is set to
     * the specified length.
     * @param  vector   The array from which the values will be copied.
     * @param  length   The number of values copied from the array.
     */
    public GVector(double vector[], int length) {
	int i;

        this.length = length;
	values = new double [length];
	for(i=0;i<length;i++) {
	    values[i] = vector[i];
        }
    }

    /**
      * Returns the square root of the sum of the squares of this
      * vector (its length in n-dimensional space).
      * @return  length of this vector
      */ 
 
    public final double norm()
    {
      double sq = 0.0;
      int i;

	for(i=0;i<length;i++) {
	    sq += values[i]*values[i];
        }	     

	return(Math.sqrt(sq));

    }

    /** 
      * Returns the sum of the squares of this   
      * vector (its length squared in n-dimensional space). 
      * @return  length squared of this vector 
      */
    public final double normSquared() 
    { 
        double sq = 0.0;
        int i;

	for(i=0;i<length;i++) {
	    sq += values[i]*values[i];
        }	     

	return(sq);
    }

    /**
     * Sets the value of this vector to the normalization of vector v1.
     * @param v1 the un-normalized vector
     */  
    public final void normalize(GVector v1)
    {
      double sq = 0.0;
      int i;

      if( length != v1.length)
          throw new MismatchedSizeException(VecMathI18N.getString("GVector0"));

       for(i=0;i<length;i++) {
	    sq += v1.values[i]*v1.values[i];
       }	     

       double invMag;
       invMag = 1.0/Math.sqrt(sq);

       for(i=0;i<length;i++) {
          values[i] = v1.values[i]*invMag;
       }
    }

 
    /**
     * Normalizes this vector in place. 
     */  
    public final void normalize() 
    { 
      double sq = 0.0;
      int i;

        for(i=0;i<length;i++) {
            sq += values[i]*values[i];
        }        

        double invMag;
	invMag = 1.0/Math.sqrt(sq);

        for(i=0;i<length;i++) {
            values[i] = values[i]*invMag;
        }

    } 

    /**
     * Sets the value of this vector to the scalar multiplication
     * of the scale factor with the vector v1.
     * @param s the scalar value
     * @param v1 the source vector 
     */  
    public final void scale(double s, GVector v1)
    {
        int i;
	if( length != v1.length)
          throw new MismatchedSizeException(VecMathI18N.getString("GVector1"));

        for(i=0;i<length;i++) {
            values[i] = v1.values[i]*s;
        }
    }

    /** 
     * Scales this vector by the scale factor s. 
     * @param s the scalar value 
     */   
    public final void scale(double s) 
    { 
      int i;

        for(i=0;i<length;i++) {
            values[i] = values[i]*s;
        }
    } 

    /**
     * Sets the value of this vector to the scalar multiplication by s
     * of vector v1 plus vector v2 (this = s*v1 + v2).
     * @param s the scalar value
     * @param v1 the vector to be multiplied
     * @param v2 the vector to be added
     */
    public final void scaleAdd(double s, GVector v1, GVector v2)
    {

      int i;

      if( v2.length != v1.length ) 
	 throw new MismatchedSizeException(VecMathI18N.getString("GVector2"));

       if( length  != v1.length )
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector3"));

        for(i=0;i<length;i++) {
            values[i] = v1.values[i]*s + v2.values[i];
        }
    }

    /**
      * Sets the value of this vector to sum of itself and the specified
      * vector
      * @param vector the second vector
      */  
    public final void add(GVector vector)
	{
	    int i;
	    
          if( length  != vector.length )
	     throw new MismatchedSizeException(VecMathI18N.getString("GVector4"));

	   for(i = 0; i < length; i++) {
		this.values[i] += vector.values[i];
	   }
	}

    /**
      * Sets the value of this vector to the vector sum of vectors vector1
      * and vector2.
      * @param vector1 the first vector
      * @param vector2 the second vector
      */  
    public final void add(GVector vector1, GVector vector2)
	{
         int i;

         if( vector1.length != vector2.length ) 
   	   throw new MismatchedSizeException(VecMathI18N.getString("GVector5"));

       if( length  != vector1.length )
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector6"));
	    
	    for(i = 0; i < length; i++)
		this.values[i] = vector1.values[i] + vector2.values[i];
	}

    /**
      * Sets the value of this vector to the vector difference of itself
      * and vector (this = this - vector).
      * @param vector the other vector
      */  
    public final void sub(GVector vector) 
	{ 
	    int i;
	    
           if( length  != vector.length )
    	        throw new MismatchedSizeException(VecMathI18N.getString("GVector7"));

	    for(i = 0; i < length; i++) {
		this.values[i] -= vector.values[i];
	    }
	} 

    /**
      * Sets the value of this vector to the vector difference
      * of vectors vector1 and vector2 (this = vector1 - vector2).
      * @param vector1 the first vector
      * @param vector2 the second vector
      */  
    public final void sub(GVector vector1, GVector vector2) 
	{
	    int i,l;
	    

	if( vector1.length != vector2.length ) 
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector8"));

       if( length  != vector1.length )
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector9"));

	    for(i = 0; i < length; i++)
		this.values[i] = vector1.values[i] - vector2.values[i];
	}

    /**
      * Multiplies matrix m1 times Vector v1 and places the result
      * into this vector (this = m1*v1).  
      * @param m1  The matrix in the multiplication
      * @param v1  The vector that is multiplied
      */
    public final void mul(GMatrix m1, GVector v1) {
       if (m1.getNumCol() != v1.length) 
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector10"));

       if (length  != m1.getNumRow())
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector11"));

       double v[];
       if (v1 != this) {
	   v = v1.values;
       } else {
	   v = (double []) values.clone();
       }

       for(int j=length-1; j>=0; j--){
	  values[j] = 0.0;
	  for(int i=v1.length-1;i>=0; i--){
	    values[j] += m1.values[j][i] * v[i];
	  }
       }
     }

    /** 
      * Multiplies the transpose of vector v1 (ie, v1 becomes a row
      * vector with respect to the multiplication) times matrix m1 
      * and places the result into this vector 
      * (this = transpose(v1)*m1).  The result is technically a 
      * row vector, but the GVector class only knows about column
      * vectors, and so the result is stored as a column vector.
      * @param m1  The matrix in the multiplication
      * @param v1  The vector that is temporarily transposed
      */ 
    public final void mul(GVector v1, GMatrix m1) {   
	if (m1.getNumRow() != v1.length) 
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector12"));

       if (length  != m1.getNumCol())
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector13"));

       double v[];
       if (v1 != this) {
	   v = v1.values;
       } else {
	   v = (double []) values.clone();
       }

       for (int j=length-1; j>=0; j--){
	  values[j] = 0.0;
	  for(int i=v1.length-1; i>=0; i--){
	     values[j] += m1.values[i][j] * v[i];
	  }
       }
     }   

    /** 
      * Negates the value of this vector: this = -this.
      */
    public final void negate() {
	for(int i=length-1; i>=0; i--) {
	    this.values[i] *= -1.0;
	}
    }

    /**
      * Sets all the values in this vector to zero.
      */
    public final void zero() {
	for (int i=0; i < this.length; i++) {
	    this.values[i] = 0.0;
	}
    }

    /**
      * Changes the size of this vector dynamically.  If the size is increased
      * no data values will be lost.  If the size is decreased, only those data
      * values whose vector positions were eliminated will be lost.
      * @param length  number of desired elements in this vector
      */
    public final void setSize(int length) {
	   double[] tmp = new double[length];
	   int i,max;

           if( this.length < length)
	      max = this.length;
           else
	       max = length;

	   for(i=0;i<max;i++) {
	      tmp[i] = values[i];
           }
	   this.length = length;

	   values = tmp;

	}

    /**
      * Sets the value of this vector to the values found in the array 
      * parameter. The array should be at least equal in length to
      * the number of elements in the vector.
      * @param vector  the source array
      */  
    public final void set(double[] vector) {
	for(int i = length-1; i >=0; i--) 
	    values[i] = vector[i];
    }

    /**
      * Sets the value of this vector to the values found in vector vector.
      * @param vector  the source vector
      */
    public final void set(GVector vector) {
	int i;
	
	if (length < vector.length) {
	    length = vector.length;
	    values = new double[length];
	    for(i = 0; i < length; i++) 
		values[i] = vector.values[i];
	}else {
	    for(i = 0; i < vector.length; i++)
		values[i] = vector.values[i];
	    for(i = vector.length; i < length; i++)
		values[i] = 0.0;
	}
    }

    /**
      * Sets the value of this vector to the values in tuple
      * @param tuple the source for the new GVector's new values
      */ 
    public final void set(Tuple2f tuple)
        {
            if (length < 2) {
	       length = 2;
	       values = new double[2];
	    }
            values[0] = (double)tuple.x;
            values[1] = (double)tuple.y;
	    for(int i = 2; i < length; i++) values[i] = 0.0;

        }

    /** 
      * Sets the value of this vector to the values in tuple
      * @param tuple the source for the new GVector's new values
      */ 
    public final void set(Tuple3f tuple)
	{
            if (length < 3) {
	       length = 3;
	       values = new double[3];
	    }
            values[0] = (double)tuple.x;
            values[1] = (double)tuple.y;
	    values[2] = (double)tuple.z;
	    for(int i = 3; i < length; i++) values[i] = 0.0;
	}

    /** 
      * Sets the value of this vector to the values in tuple
      * @param tuple the source for the new GVector's new values
      */ 
    public final void set(Tuple3d tuple)
	{
            if (length < 3) {
	       length = 3;
	       values = new double[3];
	    }
            values[0] = tuple.x;
            values[1] = tuple.y;
	    values[2] = tuple.z;
	    for(int i = 3; i < length; i++) values[i] = 0.0;
	}

    /** 
      * Sets the value of this vector to the values in tuple
      * @param tuple the source for the new GVector's new values
      */ 
    public final void set(Tuple4f tuple)
	{
            if (length < 4) {
	       length = 4;
	       values = new double[4];
	    }
            values[0] = (double)tuple.x;
            values[1] = (double)tuple.y;
	    values[2] = (double)tuple.z;
	    values[3] = (double)tuple.w;
	    for(int i = 4; i < length; i++) values[i] = 0.0;
	}

    /** 
      * Sets the value of this vector to the values in tuple
      * @param tuple the source for the new GVector's new values
      */ 
    public final void set(Tuple4d tuple)
	{
            if (length < 4) {
	       length = 4;
	       values = new double[4];
	    }
            values[0] = tuple.x;
            values[1] = tuple.y;
	    values[2] = tuple.z;
	    values[3] = tuple.w;
	    for(int i = 4; i < length; i++) values[i] = 0.0;
	}

    /**
      * Returns the number of elements in this vector.
      * @return  number of elements in this vector
      */
    public final int getSize()
	{
	    return values.length;
	}

    /**
      * Retrieves the value at the specified index value of this vector.
      * @param index the index of the element to retrieve (zero indexed)
      * @return the value at the indexed element
      */  
    public final double getElement(int index)
	{
	    return values[index];
	}

 
    /**  
      * Modifies the value at the specified index of this vector.
      * @param index  the index if the element to modify (zero indexed) 
      * @param value  the new vector element value
      */   
    public final void setElement(int index, double value)
	{
	    values[index] = value;
	}

    /**
      * Returns a string that contains the values of this GVector.
      * @return the String representation
      */  
    public String toString() {
      StringBuffer buffer = new StringBuffer(length*8);

      int i;

      for(i=0;i<length;i++) {
           buffer.append(values[i]).append(" ");
      }

      return buffer.toString();

    }


    /**
     * Returns a hash code value based on the data values in this
     * object.  Two different GVector objects with identical data
     * values (i.e., GVector.equals returns true) will return the
     * same hash number.  Two GVector objects with different data
     * members may return the same hash value, although this is not
     * likely.
     * @return the integer hash code value
     */
    public int hashCode() {
	long bits = 1L;

	for (int i = 0; i < length; i++) {
	    bits = 31L * bits + VecMathUtil.doubleToLongBits(values[i]);
	}

	return (int) (bits ^ (bits >> 32));
    } 


    /**
      * Returns true if all of the data members of GVector vector1 are
      * equal to the corresponding data members in this GVector.
      * @param vector1  The vector with which the comparison is made.
      * @return  true or false
      */  
    public boolean equals(GVector vector1)
	{
        try { 
	    if( length != vector1.length)   return false;

            for(int i = 0;i<length;i++) {
	      if( values[i] != vector1.values[i]) return false;
            }

	    return true;
        }
        catch (NullPointerException e2) { return false; }

         }
    /**
     * Returns true if the Object o1 is of type GMatrix and all of the
     * data members of o1 are equal to the corresponding data members in
     * this GMatrix.
     * @param o1  The object with which the comparison is made.
     * @return  true or false
     */  
    public boolean equals(Object o1)
    {
        try {   
            GVector v2 = (GVector) o1;

	    if( length != v2.length)   return false;

            for(int i = 0;i<length;i++) {
	      if( values[i] != v2.values[i]) return false;
            }
	    return true;
        }
        catch (ClassCastException   e1) { return false; }
        catch (NullPointerException e2) { return false; }

    }

   /**
     * Returns true if the L-infinite distance between this vector
     * and vector v1 is less than or equal to the epsilon parameter, 
     * otherwise returns false.  The L-infinite
     * distance is equal to 
     * MAX[abs(x1-x2), abs(y1-y2), . . .  ].
     * @param v1  The vector to be compared to this vector
     * @param epsilon  the threshold value  
     */
    public boolean epsilonEquals(GVector v1, double epsilon)
    {
       double diff;

	    if( length != v1.length)   return false;

            for(int i = 0;i<length;i++) {
	        diff = values[i] - v1.values[i];
                if( (diff<0?-diff:diff) > epsilon) return false;
            }
	    return true;
    }

  /**
    * Returns the dot product of this vector and vector v1.
    * @param v1 the other vector
    * @return the dot product of this and v1
    */
   public final double dot(GVector v1)
     {
       if( length != v1.length)  
	       throw new MismatchedSizeException(VecMathI18N.getString("GVector14"));

       double result = 0.0;
       for(int i = 0;i<length;i++) {
	  result += values[i] * v1.values[i];
       }
       return result;
     }
 
 
  /**
    *  Solves for x in Ax = b, where x is this vector (nx1), A is mxn,
    *  b is mx1, and A = U*W*transpose(V); U,W,V must
    *  be precomputed and can be found by taking the singular value 
    *  decomposition (SVD) of A using the method SVD found in the 
    *  GMatrix class.
    *  @param U  The U matrix produced by the GMatrix method SVD
    *  @param W  The W matrix produced by the GMatrix method SVD
    *  @param V  The V matrix produced by the GMatrix method SVD
    *  @param b  The b vector in the linear equation Ax = b
    */
   public final void SVDBackSolve(GMatrix U, GMatrix W, GMatrix V, GVector b)
    {
       if( !(U.nRow == b.getSize() &&
	     U.nRow == U.nCol      &&
	     U.nRow == W.nRow  ) ) {
            throw new MismatchedSizeException(VecMathI18N.getString("GVector15"));
       }

       if( !(W.nCol == values.length &&
	     W.nCol == V.nCol        &&
	     W.nCol == V.nRow  ) ) {
            throw new MismatchedSizeException(VecMathI18N.getString("GVector23"));
       }

       GMatrix tmp = new GMatrix( U.nRow, W.nCol);
       tmp.mul( U, V);
       tmp.mulTransposeRight( U, W);
       tmp.invert();
       mul(tmp, b);

    }

   /**
     * LU Decomposition Back Solve; this method takes the LU matrix
     * and the permutation vector produced by the GMatrix method LUD
     * and solves the equation (LU)*x = b by placing the solution vector
     * x into this vector.  This vector should be the same length or 
     * longer than b.  
     * @param LU  The matrix into which the lower and upper decompostions
     * have been placed
     * @param b  The b vector in the equation (LU)*x = b
     * @param permutation  The row permuations that were necessary to 
     * produce the LU matrix parameter
     */ 
   public final void LUDBackSolve(GMatrix LU, GVector b, GVector permutation)
   {
       int size = LU.nRow*LU.nCol;

       double[] temp = new double[size];
       double[] result = new double[size];
       int[] row_perm = new int[b.getSize()]; 
       int i,j;

       if( LU.nRow != b.getSize() ) {
            throw new MismatchedSizeException(VecMathI18N.getString("GVector16"));
       }

       if( LU.nRow != permutation.getSize() ) {
            throw new MismatchedSizeException(VecMathI18N.getString("GVector24"));
       }

       if (LU.nRow != LU.nCol) {
            throw new MismatchedSizeException(VecMathI18N.getString("GVector25"));
       }

        for(i=0;i<LU.nRow;i++) {
           for(j=0;j<LU.nCol;j++) {
               temp[i*LU.nCol+j] = LU.values[i][j];
           }
        }

       for(i=0;i<size;i++) result[i] = 0.0; 
       for(i=0;i<LU.nRow;i++) result[i*LU.nCol] = b.values[i]; 
       for(i=0;i<LU.nCol;i++) row_perm[i] = (int)permutation.values[i]; 

       GMatrix.luBacksubstitution(LU.nRow, temp, row_perm, result);

       for(i=0;i<LU.nRow;i++) this.values[i] = result[i*LU.nCol]; 
   }

  /**
    *   Returns the (n-space) angle in radians between this vector and
    *   the vector parameter; the return value is constrained to the
    *   range [0,PI].
    *   @param v1    The other vector
    *   @return   The angle in radians in the range [0,PI]
    */
   public final double angle(GVector v1)
   {
   return( Math.acos( this.dot(v1) / ( this.norm()*v1.norm() ) ) );
   }


    /**
     * @deprecated Use interpolate(GVector, GVector, double) instead
     */
    public final void interpolate(GVector v1, GVector v2, float alpha) {
	interpolate(v1, v2, (double)alpha);
    }


    /**
     * @deprecated Use interpolate(GVector, double) instead
     */
    public final void interpolate(GVector v1, float alpha) {
	interpolate(v1, (double)alpha);
    }


  /**
    *  Linearly interpolates between vectors v1 and v2 and places the
    *  result into this tuple:  this = (1-alpha)*v1 + alpha*v2.
    *  @param v1  the first vector
    *  @param v2  the second vector
    *  @param alpha  the alpha interpolation parameter   
    */   
  public final void interpolate(GVector v1, GVector v2, double alpha)
  {
	if( v2.length != v1.length ) 
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector20"));

       if( length  != v1.length )
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector21"));

       for(int i=0;i<length;i++) {
	  values[i] = (1-alpha)*v1.values[i] + alpha*v2.values[i];
       }
  }

  /**    
    *  Linearly interpolates between this vector and vector v1 and 
    *  places the result into this tuple:  this = (1-alpha)*this + alpha*v1.
    *  @param v1  the first vector 
    *  @param alpha  the alpha interpolation parameter   
    */
  public final void interpolate(GVector v1, double alpha)
  {
       if( v1.length != length )
	  throw new MismatchedSizeException(VecMathI18N.getString("GVector22"));

       for(int i=0;i<length;i++) {
	  values[i] = (1-alpha)*values[i] + alpha*v1.values[i];
       }
  }
 
    /**
     * Creates a new object of the same class as this object.
     *
     * @return a clone of this instance.
     * @exception OutOfMemoryError if there is not enough memory.
     * @see java.lang.Cloneable
     * @since Java 3D 1.3
     */
    public Object clone() {
	GVector v1 = null;
	try {
	    v1 = (GVector)super.clone();
	} catch (CloneNotSupportedException e) {
	    // this shouldn't happen, since we are Cloneable
	    throw new InternalError();
	}

	// Also need to clone array of values
	v1.values = new double[length];
	for (int i = 0; i < length; i++) {
	    v1.values[i] = values[i];
	}

	return v1;
    }

}