KickJava   Java API By Example, From Geeks To Geeks.

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


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

15 public final class Integer3Vector extends AbstractIntegerVector {
16         protected int x;
17         protected int y;
18         protected int z;
19         /**
20         * Constructs an empty 3-vector.
21         */

22         public Integer3Vector() {
23                 super(3);
24         }
25         /**
26         * Constructs a 3-vector.
27         * @param x x coordinate.
28         * @param y y coordinate.
29         * @param z z coordinate.
30         */

31         public Integer3Vector(final int x, final int y, final int z) {
32                 this();
33                 this.x = x;
34                 this.y = y;
35                 this.z = z;
36         }
37         /**
38         * Constructs a 3-vector.
39         */

40         public Integer3Vector(int[] array) {
41                 this();
42                 x = array[0];
43                 y = array[1];
44                 z = array[2];
45         }
46         /**
47         * Compares two integer vectors for equality.
48         * @param obj a integer 3-vector
49         */

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

65         public String JavaDoc toString() {
66                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(15);
67                 buf.append(x).append(',').append(y).append(',').append(z);
68                 return buf.toString();
69         }
70         /**
71         * Converts this 3-vector to a double 3-vector.
72         * @return a double 3-vector
73         */

74         public AbstractDoubleVector toDoubleVector() {
75                 return new Double3Vector(
76                         x,
77                         y,
78                         z
79                 );
80         }
81         /**
82         * Converts this 3-vector to a complex 3-vector.
83         * @return a complex 3-vector
84         */

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

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

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

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

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

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

162         /**
163         * Returns the negative of this vector.
164         */

165         public AbelianGroup.Member negate() {
166                 return new Integer3Vector(
167                         -x,
168                         -y,
169                         -z
170                 );
171         }
172
173 // ADDITION
174

175         /**
176         * Returns the addition of this vector and another.
177         */

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

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

201         /**
202         * Returns the subtraction of this vector by another.
203         */

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

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

227         /**
228         * Returns the multiplication of this vector by a scalar.
229         */

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

241         public AbstractIntegerVector scalarMultiply(final int k) {
242                 return new Integer3Vector(
243                         k*x,
244                         k*y,
245                         k*z
246                 );
247         }
248
249
250 // SCALAR PRODUCT
251

252         /**
253         * Returns the scalar product of this vector and another.
254         * @param vec a integer 3-vector
255         */

256         public int scalarProduct(final AbstractIntegerVector vec) {
257                 if(vec.N == 3) {
258                         return x*vec.getComponent(0)
259                                 +y*vec.getComponent(1)
260                                 +z*vec.getComponent(2);
261                 } else
262                         throw new VectorDimensionException("Vectors are different sizes.");
263         }
264
265 // VECTOR PRODUCT
266

267         /**
268         * Returns the vector product of this vector and another (so(3) algebra).
269         * @param vec a integer 3-vector
270         */

271         public Integer3Vector multiply(final Integer3Vector vec) {
272                 return new Integer3Vector(
273                         y*vec.z - vec.y*z,
274                         z*vec.x - vec.z*x,
275                         x*vec.y - vec.x*y
276                 );
277         }
278
279 }
280
281
Popular Tags