KickJava   Java API By Example, From Geeks To Geeks.

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


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 2D double vector.
15 * @version 2.0
16 * @author Mark Hale
17 */

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

24         public Double2Vector() {
25                 super(2);
26         }
27         /**
28         * Constructs a 2-vector.
29         * @param x x coordinate.
30         * @param y y coordinate.
31         */

32         public Double2Vector(final double x, final double y) {
33                 this();
34                 this.x = x;
35                 this.y = y;
36         }
37         /**
38         * Constructs a 2-vector.
39         */

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

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

62         public String JavaDoc toString() {
63                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(15);
64                 buf.append(x).append(',').append(y);
65                 return buf.toString();
66         }
67         /**
68         * Converts this 2-vector to an integer 2-vector.
69         * @return an integer 2-vector
70         */

71         public AbstractIntegerVector toIntegerVector() {
72                 return new Integer2Vector(
73                         Math.round((float)x),
74                         Math.round((float)y)
75                 );
76         }
77         /**
78         * Converts this 2-vector to a complex 2-vector.
79         * @return a complex 2-vector
80         */

81         public AbstractComplexVector toComplexVector() {
82                 return new Complex2Vector(
83                         x, 0.0,
84                         y, 0.0
85                 );
86         }
87         /**
88         * Returns a component of this vector.
89         * @param n index of the vector component
90         * @exception VectorDimensionException If attempting to access an invalid component.
91         */

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

106         public void setComponent(final int n, final double value) {
107                 switch(n) {
108                         case 0 : x = value; break;
109                         case 1 : y = value; break;
110                         default : throw new VectorDimensionException("Invalid component.");
111                 }
112         }
113         /**
114         * Returns the l<sup>n</sup>-norm.
115         */

116         public double norm(final int n) {
117                 final double answer = Math.pow(Math.abs(x), n)
118                         +Math.pow(Math.abs(y), n);
119                 return Math.pow(answer, 1.0/n);
120         }
121         /**
122         * Returns the l<sup>2</sup>-norm (magnitude).
123         */

124         public double norm() {
125                 return Math.sqrt(
126                         x*x
127                         +y*y
128                 );
129         }
130         /**
131         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
132         * @author Taber Smith
133         */

134         public double infNorm() {
135                 double infNorm = 0;
136                 double abs;
137                 abs = Math.abs(x);
138                 if(abs > infNorm)
139                         infNorm = abs;
140                 abs = Math.abs(y);
141                 if(abs > infNorm)
142                         infNorm = abs;
143                 return infNorm;
144         }
145
146 //============
147
// OPERATIONS
148
//============
149

150         /**
151         * Returns the negative of this vector.
152         */

153         public AbelianGroup.Member negate() {
154                 return new Double2Vector(
155                         -x,
156                         -y
157                 );
158         }
159
160 // ADDITION
161

162         /**
163         * Returns the addition of this vector and another.
164         */

165         public AbelianGroup.Member add(final AbelianGroup.Member vec) {
166                 if(vec instanceof AbstractDoubleVector)
167                         return add((AbstractDoubleVector)vec);
168                 else
169                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
170         }
171         /**
172         * Returns the addition of this vector and another.
173         * @param vec a double 2-vector
174         */

175         public AbstractDoubleVector add(final AbstractDoubleVector vec) {
176                 if(vec.N == 2) {
177                         return new Double2Vector(
178                                 x+vec.getComponent(0),
179                                 y+vec.getComponent(1)
180                         );
181                 } else
182                         throw new VectorDimensionException("Vectors are different sizes.");
183         }
184
185 // SUBTRACTION
186

187         /**
188         * Returns the subtraction of this vector by another.
189         */

190         public AbelianGroup.Member subtract(final AbelianGroup.Member vec) {
191                 if(vec instanceof AbstractDoubleVector)
192                         return subtract((AbstractDoubleVector)vec);
193                 else
194                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
195         }
196         /**
197         * Returns the subtraction of this vector by another.
198         * @param vec a double 2-vector
199         */

200         public AbstractDoubleVector subtract(final AbstractDoubleVector vec) {
201                 if(vec.N == 2) {
202                         return new Double2Vector(
203                                 x-vec.getComponent(0),
204                                 y-vec.getComponent(1)
205                         );
206                 } else
207                         throw new VectorDimensionException("Vectors are different sizes.");
208         }
209
210 // SCALAR MULTIPLICATION
211

212         /**
213         * Returns the multiplication of this vector by a scalar.
214         */

215         public Module.Member scalarMultiply(Ring.Member x) {
216                 if(x instanceof MathInteger)
217                         return scalarMultiply(((MathInteger)x).value());
218                 else if(x instanceof MathDouble)
219                         return scalarMultiply(((MathDouble)x).value());
220                 else
221                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
222         }
223         /**
224         * Returns the multiplication of this vector by a scalar.
225         * @param k a double
226         * @return a double 2-vector
227         */

228         public AbstractDoubleVector scalarMultiply(final double k) {
229                 return new Double2Vector(
230                         k*x,
231                         k*y
232                 );
233         }
234
235 // SCALAR DIVISION
236

237         /**
238         * Returns the division of this vector by a scalar.
239         */

240         public VectorSpace.Member scalarDivide(Field.Member x) {
241                 if(x instanceof MathDouble)
242                         return scalarDivide(((MathDouble)x).value());
243                 else
244                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
245         }
246         /**
247         * Returns the division of this vector by a scalar.
248         * @param k a double
249         * @return a double 2-vector
250         * @exception ArithmeticException If divide by zero.
251         */

252         public AbstractDoubleVector scalarDivide(final double k) {
253                 return new Double2Vector(
254                         x/k,
255                         y/k
256                 );
257         }
258
259 // SCALAR PRODUCT
260

261         /**
262         * Returns the scalar product of this vector and another.
263         * @param vec a double 2-vector
264         */

265         public double scalarProduct(final AbstractDoubleVector vec) {
266                 if(vec.N == 2) {
267                         return x*vec.getComponent(0)
268                                 +y*vec.getComponent(1);
269                 } else
270                         throw new VectorDimensionException("Vectors are different sizes.");
271         }
272
273
274 // MAP COMPONENTS
275

276         /**
277         * Applies a function on all the vector components.
278         * @param mapping a user-defined function
279         * @return a double 2-vector
280         */

281         public AbstractDoubleVector mapComponents(final Mapping mapping) {
282                 return new Double2Vector(
283                         mapping.map(x),
284                         mapping.map(y)
285                 );
286         }
287 }
288
289
Popular Tags