KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > vectors > Double3Vector


1 package JSci.maths.vectors;
2
3 import JSci.GlobalSettings;
4 import JSci.maths.Mapping;
5 import JSci.maths.MathInteger;
6 import JSci.maths.MathDouble;
7 import JSci.maths.groups.AbelianGroup;
8 import JSci.maths.algebras.Module;
9 import JSci.maths.fields.Ring;
10 import JSci.maths.algebras.VectorSpace;
11 import JSci.maths.fields.Field;
12
13 /**
14 * An optimised implementation of a 3D double vector.
15 * @version 2.0
16 * @author Mark Hale
17 */

18 public final class Double3Vector extends AbstractDoubleVector {
19         protected double x;
20         protected double y;
21         protected double z;
22         /**
23         * Constructs an empty 3-vector.
24         */

25         public Double3Vector() {
26                 super(3);
27         }
28         /**
29         * Constructs a 3-vector.
30         * @param x x coordinate.
31         * @param y y coordinate.
32         * @param z z coordinate.
33         */

34         public Double3Vector(final double x, final double y, final double z) {
35                 this();
36                 this.x = x;
37                 this.y = y;
38                 this.z = z;
39         }
40         /**
41         * Constructs a 3-vector.
42         */

43         public Double3Vector(double[] array) {
44                 this();
45                 x = array[0];
46                 y = array[1];
47                 z = array[2];
48         }
49         /**
50         * Compares two double vectors for equality.
51         * @param obj a double 3-vector
52         */

53     public boolean equals(Object JavaDoc obj, double tol) {
54                 if(obj != null && (obj instanceof Double3Vector)) {
55                         final Double3Vector vec = (Double3Vector) obj;
56                         double dx = x - vec.x;
57                         double dy = y - vec.y;
58                         double dz = z - vec.z;
59                         return (dx*dx
60                          + dy*dy
61                          + dz*dz <= tol*tol);
62                 } else
63                         return false;
64         }
65         /**
66         * Returns a comma delimited string representing the value of this vector.
67         */

68         public String JavaDoc toString() {
69                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(15);
70                 buf.append(x).append(',').append(y).append(',').append(z);
71                 return buf.toString();
72         }
73         /**
74         * Converts this 3-vector to an integer 3-vector.
75         * @return an integer 3-vector
76         */

77         public AbstractIntegerVector toIntegerVector() {
78                 return new Integer3Vector(
79                         Math.round((float)x),
80                         Math.round((float)y),
81                         Math.round((float)z)
82                 );
83         }
84         /**
85         * Converts this 3-vector to a complex 3-vector.
86         * @return a complex 3-vector
87         */

88         public AbstractComplexVector toComplexVector() {
89                 return new Complex3Vector(
90                         x, 0.0,
91                         y, 0.0,
92                         z, 0.0
93                 );
94         }
95         /**
96         * Returns a component of this vector.
97         * @param n index of the vector component
98         * @exception VectorDimensionException If attempting to access an invalid component.
99         */

100         public double getComponent(final int n) {
101                 switch(n) {
102                         case 0 : return x;
103                         case 1 : return y;
104                         case 2 : return z;
105                         default : throw new VectorDimensionException("Invalid component.");
106                 }
107         }
108         /**
109         * Sets the value of a component of this vector.
110         * Should only be used to initialise this vector.
111         * @param n index of the vector component
112         * @param value a number
113         * @exception VectorDimensionException If attempting to access an invalid component.
114         */

115         public void setComponent(final int n, final double value) {
116                 switch(n) {
117                         case 0 : x = value; break;
118                         case 1 : y = value; break;
119                         case 2 : z = value; break;
120                         default : throw new VectorDimensionException("Invalid component.");
121                 }
122         }
123         /**
124         * Returns the l<sup>n</sup>-norm.
125         */

126         public double norm(final int n) {
127                 final double answer = Math.pow(Math.abs(x), n)
128                         +Math.pow(Math.abs(y), n)
129                         +Math.pow(Math.abs(z), n);
130                 return Math.pow(answer, 1.0/n);
131         }
132         /**
133         * Returns the l<sup>2</sup>-norm (magnitude).
134         */

135         public double norm() {
136                 return Math.sqrt(
137                         x*x
138                         +y*y
139                         +z*z
140                 );
141         }
142         /**
143         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
144         * @author Taber Smith
145         */

146         public double infNorm() {
147                 double infNorm = 0;
148                 double abs;
149                 abs = Math.abs(x);
150                 if(abs > infNorm)
151                         infNorm = abs;
152                 abs = Math.abs(y);
153                 if(abs > infNorm)
154                         infNorm = abs;
155                 abs = Math.abs(z);
156                 if(abs > infNorm)
157                         infNorm = abs;
158                 return infNorm;
159         }
160
161 //============
162
// OPERATIONS
163
//============
164

165         /**
166         * Returns the negative of this vector.
167         */

168         public AbelianGroup.Member negate() {
169                 return new Double3Vector(
170                         -x,
171                         -y,
172                         -z
173                 );
174         }
175
176 // ADDITION
177

178         /**
179         * Returns the addition of this vector and another.
180         */

181         public AbelianGroup.Member add(final AbelianGroup.Member vec) {
182                 if(vec instanceof AbstractDoubleVector)
183                         return add((AbstractDoubleVector)vec);
184                 else
185                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
186         }
187         /**
188         * Returns the addition of this vector and another.
189         * @param vec a double 3-vector
190         */

191         public AbstractDoubleVector add(final AbstractDoubleVector vec) {
192                 if(vec.N == 3) {
193                         return new Double3Vector(
194                                 x+vec.getComponent(0),
195                                 y+vec.getComponent(1),
196                                 z+vec.getComponent(2)
197                         );
198                 } else
199                         throw new VectorDimensionException("Vectors are different sizes.");
200         }
201
202 // SUBTRACTION
203

204         /**
205         * Returns the subtraction of this vector by another.
206         */

207         public AbelianGroup.Member subtract(final AbelianGroup.Member vec) {
208                 if(vec instanceof AbstractDoubleVector)
209                         return subtract((AbstractDoubleVector)vec);
210                 else
211                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
212         }
213         /**
214         * Returns the subtraction of this vector by another.
215         * @param vec a double 3-vector
216         */

217         public AbstractDoubleVector subtract(final AbstractDoubleVector vec) {
218                 if(vec.N == 3) {
219                         return new Double3Vector(
220                                 x-vec.getComponent(0),
221                                 y-vec.getComponent(1),
222                                 z-vec.getComponent(2)
223                         );
224                 } else
225                         throw new VectorDimensionException("Vectors are different sizes.");
226         }
227
228 // SCALAR MULTIPLICATION
229

230         /**
231         * Returns the multiplication of this vector by a scalar.
232         */

233         public Module.Member scalarMultiply(Ring.Member x) {
234                 if(x instanceof MathInteger)
235                         return scalarMultiply(((MathInteger)x).value());
236                 else if(x instanceof MathDouble)
237                         return scalarMultiply(((MathDouble)x).value());
238                 else
239                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
240         }
241         /**
242         * Returns the multiplication of this vector by a scalar.
243         * @param k a double
244         * @return a double 3-vector
245         */

246         public AbstractDoubleVector scalarMultiply(final double k) {
247                 return new Double3Vector(
248                         k*x,
249                         k*y,
250                         k*z
251                 );
252         }
253
254 // SCALAR DIVISION
255

256         /**
257         * Returns the division of this vector by a scalar.
258         */

259         public VectorSpace.Member scalarDivide(Field.Member x) {
260                 if(x instanceof MathDouble)
261                         return scalarDivide(((MathDouble)x).value());
262                 else
263                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
264         }
265         /**
266         * Returns the division of this vector by a scalar.
267         * @param k a double
268         * @return a double 3-vector
269         * @exception ArithmeticException If divide by zero.
270         */

271         public AbstractDoubleVector scalarDivide(final double k) {
272                 return new Double3Vector(
273                         x/k,
274                         y/k,
275                         z/k
276                 );
277         }
278
279 // SCALAR PRODUCT
280

281         /**
282         * Returns the scalar product of this vector and another.
283         * @param vec a double 3-vector
284         */

285         public double scalarProduct(final AbstractDoubleVector vec) {
286                 if(vec.N == 3) {
287                         return x*vec.getComponent(0)
288                                 +y*vec.getComponent(1)
289                                 +z*vec.getComponent(2);
290                 } else
291                         throw new VectorDimensionException("Vectors are different sizes.");
292         }
293
294 // VECTOR PRODUCT
295

296         /**
297         * Returns the vector product of this vector and another (so(3) algebra).
298         * @param vec a double 3-vector
299         */

300         public Double3Vector multiply(final Double3Vector vec) {
301                 return new Double3Vector(
302                         y*vec.z - vec.y*z,
303                         z*vec.x - vec.z*x,
304                         x*vec.y - vec.x*y
305                 );
306         }
307
308 // MAP COMPONENTS
309

310         /**
311         * Applies a function on all the vector components.
312         * @param mapping a user-defined function
313         * @return a double 3-vector
314         */

315         public AbstractDoubleVector mapComponents(final Mapping mapping) {
316                 return new Double3Vector(
317                         mapping.map(x),
318                         mapping.map(y),
319                         mapping.map(z)
320                 );
321         }
322 }
323
324
Popular Tags