/* * $RCSfile$ * * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. * * $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 * length elements (i.e., vector.length >= * length. 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=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=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> 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 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