3 * A class representing a three dimensional vector that implements
4 * several math operations. To improve speed we implement the
5 * vector as an array of doubles rather than use the exising
6 * code in the java.util.Vector class.
11 * The number of dimensions in the vector
13 public final int NDIM;
15 * An array containing the values in the vector.
17 private double data[];
20 * Construct an empty 3 dimensional vector for use in Barnes-Hut algorithm.
25 data = new double[NDIM];
26 for (int i=0; i < NDIM; i++) {
31 public MathVector(boolean x) {
37 * Create a copy of the vector.
38 * @return a clone of the math vector
42 MathVector v = new MathVector();
43 v.data = new double[NDIM];
44 for (int i = 0; i < NDIM; i++) {
51 * Return the value at the i'th index of the vector.
52 * @param i the vector index
53 * @return the value at the i'th index of the vector.
55 public double value(int i)
61 * Set the value of the i'th index of the vector.
62 * @param i the vector index
63 * @param v the value to store
65 public void value(int i, double v)
71 * Set one of the dimensions of the vector to 1.0
72 * param j the dimension to set.
74 public void unit(int j)
76 for (int i=0; i < NDIM; i++) {
77 data[i] = (i == j ? 1.0 : 0.0);
82 * Add two vectors and the result is placed in this vector.
83 * @param u the other operand of the addition
85 public void addition(MathVector u)
87 for (int i=0; i < NDIM; i++) {
93 * Subtract two vectors and the result is placed in this vector.
94 * This vector contain the first operand.
95 * @param u the other operand of the subtraction.
97 public void subtraction(MathVector u)
99 for (int i=0; i < NDIM; i++) {
100 data[i] -= u.data[i];
105 * Subtract two vectors and the result is placed in this vector.
106 * @param u the first operand of the subtraction.
107 * @param v the second opernd of the subtraction
109 public void subtraction(MathVector u, MathVector v)
111 for (int i=0; i < NDIM; i++) {
112 data[i] = u.data[i] - v.data[i];
117 * Multiply the vector times a scalar.
118 * @param s the scalar value
120 public void multScalar(double s)
122 for (int i=0; i < NDIM; i++) {
128 * Multiply the vector times a scalar and place the result in this vector.
129 * @param u the vector
130 * @param s the scalar value
132 public void multScalar(MathVector u, double s)
134 for (int i=0; i < NDIM; i++) {
135 data[i] = u.data[i] * s;
140 * Divide each element of the vector by a scalar value.
141 * @param s the scalar value.
143 public void divScalar(double s)
145 for (int i=0; i < NDIM; i++) {
151 * Return the dot product of a vector.
152 * @return the dot product of a vector.
154 public double dotProduct()
157 for (int i=0; i < NDIM; i++) {
158 s += data[i] * data[i];
163 public double absolute()
166 for (int i = 0; i < NDIM; i++) {
167 tmp += data[i] * data[i];
169 return Math.sqrt(tmp);
172 public double distance(MathVector v)
175 for (int i = 0; i < NDIM; i++) {
176 tmp += (data[i] - v.data[i]) * (data[i] - v.data[i]);
178 return Math.sqrt(tmp);
181 public void crossProduct(MathVector u, MathVector w)
183 data[0] = u.data[1] * w.data[2] - u.data[2]*w.data[1];
184 data[1] = u.data[2] * w.data[0] - u.data[0]*w.data[2];
185 data[2] = u.data[0] * w.data[1] - u.data[1]*w.data[0];
188 public void incrementalAdd(MathVector u)
190 for (int i = 0; i < NDIM; i++) {
191 data[i] += u.data[i];
195 public void incrementalSub(MathVector u)
197 for (int i = 0; i < NDIM; i++) {
198 data[i] -= u.data[i];
202 public void incrementalMultScalar(double s)
204 for (int i=0; i < NDIM; i++) {
209 public void incrementalDivScalar(double s)
211 for (int i=0; i < NDIM; i++) {
217 * Add a scalar to each element in the vector and put the
218 * result in this vector.
220 * @param s the scalar
222 public void addScalar(MathVector u, double s)
224 for (int i = 0; i < NDIM; i++) {
225 data[i] = u.data[i] + s;
231 * Return the string representation of the vector
233 /*public String toString()
235 StringBuffer s = new StringBuffer();
236 for (int i = 0; i < NDIM; i++) {
237 s.append(data[i] + " ");