Ported over bamboo benchmarks for use as non-Bamboo java benchmarks.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / bh / MathVector.java
1
2 /**
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.
7  **/
8 class MathVector
9 {
10   /**
11    * The number of dimensions in the vector
12    **/
13   public final int NDIM;
14   /**
15    * An array containing the values in the vector.
16    **/
17   private double data[];
18
19   /**
20    * Construct an empty 3 dimensional vector for use in Barnes-Hut algorithm.
21    **/
22   public MathVector()
23   {
24     NDIM = 3;
25     data = new double[NDIM];
26     for (int i=0; i < NDIM; i++) {
27       data[i] = 0.0;
28     }
29   }
30
31   public MathVector(boolean x) {
32     NDIM = 3;
33     data = null;
34   }
35
36   /**
37    * Create a copy of the vector.
38    * @return a clone of the math vector
39    **/
40   public Object clone() 
41   {
42     MathVector v = new MathVector();
43     v.data = new double[NDIM];
44     for (int i = 0; i < NDIM; i++) {
45       v.data[i] = data[i];
46     }
47     return v;
48   }
49
50   /**
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.
54    **/
55   public  double value(int i)
56   {
57     return data[i];
58   }
59
60   /**
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
64    **/
65   public  void value(int i, double v)
66   {
67     data[i] = v;
68   }
69
70   /**
71    * Set one of the dimensions of the vector to 1.0
72    * param j the dimension to set.
73    **/
74   public  void unit(int j)
75   {
76     for (int i=0; i < NDIM; i++) {
77       data[i] = (i == j ? 1.0 : 0.0);
78     }
79   }
80
81   /**
82    * Add two vectors and the result is placed in this vector.
83    * @param u the other operand of the addition
84    **/
85   public  void addition(MathVector u)
86   {
87     for (int i=0; i < NDIM; i++) {
88       data[i] += u.data[i];
89     }
90   }
91
92   /**
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.
96    **/
97   public  void subtraction(MathVector u)
98   {
99     for (int i=0; i < NDIM; i++) {
100       data[i] -= u.data[i];
101     }
102   }
103
104   /**
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
108    **/
109   public  void subtraction(MathVector u, MathVector v)
110   {
111     for (int i=0; i < NDIM; i++) {
112       data[i] = u.data[i] - v.data[i];
113     }
114   }
115
116   /**
117    * Multiply the vector times a scalar.
118    * @param s the scalar value
119    **/
120   public  void multScalar(double s)
121   {
122     for (int i=0; i < NDIM; i++) {
123       data[i] *= s;
124     }
125   }
126
127   /**
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
131    **/
132   public  void multScalar(MathVector u, double s)
133   {
134     for (int i=0; i < NDIM; i++) {
135       data[i] = u.data[i] * s;
136     }
137   }
138
139   /**
140    * Divide each element of the vector by a scalar value.
141    * @param s the scalar value.
142    **/
143   public  void divScalar(double s)
144   {
145     for (int i=0; i < NDIM; i++) {
146       data[i] /= s;
147     }
148   }
149
150   /**
151    * Return the dot product of a vector.
152    * @return the dot product of a vector.
153    **/
154   public  double dotProduct()
155   {
156     double s = 0.0;
157     for (int i=0; i < NDIM; i++) {
158       s += data[i] * data[i];
159     }
160     return s;
161   }
162
163   public  double absolute()
164   {
165     double tmp = 0.0;
166     for (int i = 0; i < NDIM; i++) {
167       tmp += data[i] * data[i];
168     }
169     return Math.sqrt(tmp);
170   }
171
172   public  double distance(MathVector v)
173   {
174     double tmp = 0.0;
175     for (int i = 0; i < NDIM; i++) {
176       tmp += (data[i] - v.data[i]) * (data[i] - v.data[i]);
177     }
178     return Math.sqrt(tmp);
179   }
180
181   public  void crossProduct(MathVector u, MathVector w)
182   {
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];
186   }
187
188   public  void incrementalAdd(MathVector u)
189   {
190     for (int i = 0; i < NDIM; i++) {
191       data[i] += u.data[i];
192     }
193   }
194
195   public  void incrementalSub(MathVector u)
196   {
197     for (int i = 0; i < NDIM; i++) {
198       data[i] -= u.data[i];
199     }
200   }
201
202   public  void incrementalMultScalar(double s) 
203   {
204     for (int i=0; i < NDIM; i++) {
205       data[i] *= s;
206     }
207   }
208
209   public  void incrementalDivScalar(double s)
210   {
211     for (int i=0; i < NDIM; i++) {
212       data[i] /= s;
213     }
214   }
215
216   /**
217    * Add a scalar to each element in the vector and put the
218    * result in this vector.
219    * @param u a vector
220    * @param s the scalar
221    **/
222   public  void addScalar(MathVector u, double s) 
223   {
224     for (int i = 0; i < NDIM; i++) {
225       data[i] = u.data[i] + s;
226     }
227   }
228
229
230   /**
231    * Return the string representation of the vector
232    **/
233   /*public String toString()
234   {
235     StringBuffer s = new StringBuffer();
236     for (int i = 0; i < NDIM; i++) {
237       s.append(data[i] + " ");
238     }
239     return s.toString();
240   }*/
241 }