KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* AUTO-GENERATED */
2 package JSci.maths.vectors;
3
4 import JSci.GlobalSettings;
5 import JSci.maths.Complex;
6 import JSci.maths.ComplexMapping;
7 import JSci.maths.MathDouble;
8 import JSci.maths.MathInteger;
9 import JSci.maths.groups.AbelianGroup;
10 import JSci.maths.algebras.Module;
11 import JSci.maths.algebras.VectorSpace;
12 import JSci.maths.algebras.HilbertSpace;
13 import JSci.maths.fields.Ring;
14 import JSci.maths.fields.Field;
15
16 /**
17 * An optimised implementation of a 2D complex vector.
18 * @version 2.2
19 * @author Mark Hale
20 */

21 public final class Complex2Vector extends AbstractComplexVector {
22         protected double xre, xim;
23         protected double yre, yim;
24         /**
25         * Constructs an empty 2-vector.
26         */

27         public Complex2Vector() {
28                 super(2);
29         }
30         /**
31         * Constructs a 2-vector.
32         * @param x x coordinate.
33         * @param y y coordinate.
34         */

35         public Complex2Vector(final Complex x, final Complex y) {
36                 this();
37                 xre = x.real();
38                 xim = x.imag();
39                 yre = y.real();
40                 yim = y.imag();
41         }
42         public Complex2Vector(double xRe, double xIm, double yRe, double yIm) {
43                 this();
44                 xre = xRe;
45                 xim = xIm;
46                 yre = yRe;
47                 yim = yIm;
48         }
49         /**
50         * Compares two complex vectors for equality.
51         * @param obj a complex 2-vector
52         */

53     public boolean equals(Object JavaDoc obj, double tol) {
54                 if(obj != null && (obj instanceof Complex2Vector)) {
55                         final Complex2Vector vec = (Complex2Vector) obj;
56                         double dxRe = xre - vec.xre;
57                         double dxIm = xim - vec.xim;
58                         double dyRe = yre - vec.yre;
59                         double dyIm = yim - vec.yim;
60                         return (dxRe*dxRe + dxIm*dxIm
61                          + dyRe*dyRe + dyIm*dyIm <= 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(Complex.toString(xre, xim)).append(',').append(Complex.toString(yre, yim));
71                 return buf.toString();
72         }
73         /**
74         * Returns the real part of this complex 2-vector.
75         */

76         public AbstractDoubleVector real() {
77                 return new Double2Vector(
78                         xre,
79                         yre
80                 );
81         }
82         /**
83         * Returns the imaginary part of this complex 2-vector.
84         */

85         public AbstractDoubleVector imag() {
86                 return new Double2Vector(
87                         xim,
88                         yim
89                 );
90         }
91         /**
92         * Returns a component of this vector.
93         * @param n index of the vector component
94         * @exception VectorDimensionException If attempting to access an invalid component.
95         */

96         public Complex getComponent(final int n) {
97                 switch(n) {
98                         case 0 : return new Complex(xre, xim);
99                         case 1 : return new Complex(yre, yim);
100                         default : throw new VectorDimensionException("Invalid component.");
101                 }
102         }
103         public double getRealComponent(final int n) {
104                 switch(n) {
105                         case 0 : return xre;
106                         case 1 : return yre;
107                         default : throw new VectorDimensionException("Invalid component.");
108                 }
109         }
110         public double getImagComponent(final int n) {
111                 switch(n) {
112                         case 0 : return xim;
113                         case 1 : return yim;
114                         default : throw new VectorDimensionException("Invalid component.");
115                 }
116         }
117         /**
118         * Sets the value of a component of this vector.
119         * Should only be used to initialise this vector.
120         * @param n index of the vector component
121         * @param z a complex number
122         * @exception VectorDimensionException If attempting to access an invalid component.
123         */

124         public void setComponent(final int n, final Complex z) {
125                 switch(n) {
126                         case 0 : xre = z.real(); xim = z.imag(); break;
127                         case 1 : yre = z.real(); yim = z.imag(); break;
128                         default : throw new VectorDimensionException("Invalid component.");
129                 }
130         }
131         /**
132         * Sets the value of a component of this vector.
133         * Should only be used to initialise this vector.
134         * @param n index of the vector component
135         * @param x the real part of a complex number
136         * @param y the imaginary part of a complex number
137         * @exception VectorDimensionException If attempting to access an invalid component.
138         */

139         public void setComponent(final int n, final double x, final double y) {
140                 switch(n) {
141                         case 0 : xre = x; xim = y; break;
142                         case 1 : yre = x; yim = y; break;
143                         default : throw new VectorDimensionException("Invalid component.");
144                 }
145         }
146         /**
147         * Returns the l<sup>2</sup>-norm (magnitude).
148         */

149         public double norm() {
150                 return Math.sqrt(
151                         xre*xre + xim*xim
152                         +yre*yre + yim*yim
153                 );
154         }
155         /**
156         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
157         */

158         public double infNorm() {
159                 double infNormSq = 0;
160                 double modSq;
161                 modSq = xre*xre + xim*xim;
162                 if(modSq > infNormSq)
163                         infNormSq = modSq;
164                 modSq = yre*yre + yim*yim;
165                 if(modSq > infNormSq)
166                         infNormSq = modSq;
167                 return Math.sqrt(infNormSq);
168         }
169
170 //============
171
// OPERATIONS
172
//============
173

174         /**
175         * Returns the negative of this vector.
176         */

177         public AbelianGroup.Member negate() {
178                 return new Complex2Vector(
179                         -xre, -xim,
180                         -yre, -yim
181                 );
182         }
183
184 // COMPLEX CONJUGATE
185

186         /**
187         * Returns the complex conjugate of this vector.
188         * @return a complex 2-vector
189         */

190         public AbstractComplexVector conjugate() {
191                 return new Complex2Vector(
192                         xre, -xim,
193                         yre, -yim
194                 );
195         }
196
197 // ADDITION
198

199         /**
200         * Returns the addition of this vector and another.
201         */

202         public AbelianGroup.Member add(final AbelianGroup.Member vec) {
203                 if(vec instanceof AbstractComplexVector)
204                         return add((AbstractComplexVector)vec);
205                 else if(vec instanceof AbstractDoubleVector)
206                         return add((AbstractDoubleVector)vec);
207                 else if(vec instanceof AbstractIntegerVector)
208                         return add((AbstractIntegerVector)vec);
209                 else
210                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
211         }
212         /**
213         * Returns the addition of this vector and another.
214         * @param vec a complex 2-vector
215         */

216         public AbstractComplexVector add(final AbstractComplexVector vec) {
217                 if(vec.N == 2) {
218                         return new Complex2Vector(
219                                 xre+vec.getComponent(0).real(), xim+vec.getComponent(0).imag(),
220                                 yre+vec.getComponent(1).real(), yim+vec.getComponent(1).imag()
221                         );
222                 } else
223                         throw new VectorDimensionException("Vectors are different sizes.");
224         }
225         /**
226         * Returns the addition of this vector and another.
227         * @param vec a double 2-vector
228         */

229         public AbstractComplexVector add(final AbstractDoubleVector vec) {
230                 if(vec.N == 2) {
231                         return new Complex2Vector(
232                                 xre+vec.getComponent(0), xim,
233                                 yre+vec.getComponent(1), yim
234                         );
235                 } else
236                         throw new VectorDimensionException("Vectors are different sizes.");
237         }
238         /**
239         * Returns the addition of this vector and another.
240         * @param vec an integer 2-vector
241         */

242         public AbstractComplexVector add(final AbstractIntegerVector vec) {
243                 if(vec.N == 2) {
244                         return new Complex2Vector(
245                                 xre+vec.getComponent(0), xim,
246                                 yre+vec.getComponent(1), yim
247                         );
248                 } else
249                         throw new VectorDimensionException("Vectors are different sizes.");
250         }
251
252 // SUBTRACTION
253

254         /**
255         * Returns the subtraction of this vector by another.
256         */

257         public AbelianGroup.Member subtract(final AbelianGroup.Member vec) {
258                 if(vec instanceof AbstractComplexVector)
259                         return subtract((AbstractComplexVector)vec);
260                 else if(vec instanceof AbstractDoubleVector)
261                         return subtract((AbstractDoubleVector)vec);
262                 else if(vec instanceof AbstractIntegerVector)
263                         return subtract((AbstractIntegerVector)vec);
264                 else
265                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
266         }
267         /**
268         * Returns the subtraction of this vector by another.
269         * @param vec a complex 2-vector
270         */

271         public AbstractComplexVector subtract(final AbstractComplexVector vec) {
272                 if(vec.N == 2) {
273                         return new Complex2Vector(
274                                 xre-vec.getComponent(0).real(), xim-vec.getComponent(0).imag(),
275                                 yre-vec.getComponent(1).real(), yim-vec.getComponent(1).imag()
276                         );
277                 } else
278                         throw new VectorDimensionException("Vectors are different sizes.");
279         }
280         /**
281         * Returns the subtraction of this vector by another.
282         * @param vec a double 2-vector
283         */

284         public AbstractComplexVector subtract(final AbstractDoubleVector vec) {
285                 if(vec.N == 2) {
286                         return new Complex2Vector(
287                                 xre-vec.getComponent(0), xim,
288                                 yre-vec.getComponent(1), yim
289                         );
290                 } else
291                         throw new VectorDimensionException("Vectors are different sizes.");
292         }
293         /**
294         * Returns the subtraction of this vector by another.
295         * @param vec an integer 2-vector
296         */

297         public AbstractComplexVector subtract(final AbstractIntegerVector vec) {
298                 if(vec.N == 2) {
299                         return new Complex2Vector(
300                                 xre-vec.getComponent(0), xim,
301                                 yre-vec.getComponent(1), yim
302                         );
303                 } else
304                         throw new VectorDimensionException("Vectors are different sizes.");
305         }
306
307 // SCALAR MULTIPLICATION
308

309         /**
310         * Returns the multiplication of this vector by a scalar.
311         */

312         public Module.Member scalarMultiply(Ring.Member x) {
313                 if(x instanceof Complex)
314                         return scalarMultiply((Complex)x);
315                 else if(x instanceof MathDouble)
316                         return scalarMultiply(((MathDouble)x).value());
317                 else if(x instanceof MathInteger)
318                         return scalarMultiply(((MathInteger)x).value());
319                 else
320                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
321         }
322         /**
323         * Returns the multiplication of this vector by a scalar.
324         * @param z a complex number
325         * @return a complex 2-vector
326         */

327         public AbstractComplexVector scalarMultiply(final Complex z) {
328                 final double real=z.real();
329                 final double imag=z.imag();
330                 return new Complex2Vector(
331                         xre*real-xim*imag, xre*imag+xim*real,
332                         yre*real-yim*imag, yre*imag+yim*real
333                 );
334         }
335         /**
336         * Returns the multiplication of this vector by a scalar.
337         * @param k a double
338         * @return a complex 2-vector
339         */

340         public AbstractComplexVector scalarMultiply(final double k) {
341                 return new Complex2Vector(
342                         k*xre, k*xim,
343                         k*yre, k*yim
344                 );
345         }
346
347 // SCALAR DIVISION
348

349         /**
350         * Returns the division of this vector by a scalar.
351         */

352         public VectorSpace.Member scalarDivide(Field.Member x) {
353                 if(x instanceof Complex)
354                         return scalarDivide((Complex)x);
355                 else if(x instanceof MathDouble)
356                         return scalarDivide(((MathDouble)x).value());
357                 else
358                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
359         }
360         /**
361         * Returns the division of this vector by a scalar.
362         * @param z a complex number
363         * @return a complex 2-vector
364         * @exception ArithmeticException If divide by zero.
365         */

366         public AbstractComplexVector scalarDivide(final Complex z) {
367                 final double real=z.real();
368                 final double imag=z.imag();
369                 final double a,denom;
370                 if(Math.abs(real)<Math.abs(imag)) {
371                         a=real/imag;
372                         denom=real*a+imag;
373                         return new Complex2Vector(
374                                 (xre*a+xim)/denom, (xim*a-xre)/denom,
375                                 (yre*a+yim)/denom, (yim*a-yre)/denom
376                         );
377                 } else {
378                         a=imag/real;
379                         denom=real+imag*a;
380                         return new Complex2Vector(
381                                 (xre+xim*a)/denom, (xim-xre*a)/denom,
382                                 (yre+yim*a)/denom, (yim-yre*a)/denom
383                         );
384                 }
385         }
386         /**
387         * Returns the division of this vector by a scalar.
388         * @param k a double
389         * @return a complex 2-vector
390         * @exception ArithmeticException If divide by zero.
391         */

392         public AbstractComplexVector scalarDivide(final double k) {
393                 return new Complex2Vector(
394                         xre/k, xim/k,
395                         yre/k, yim/k
396                 );
397         }
398
399 // SCALAR PRODUCT
400

401         /**
402         * Returns the scalar product of this vector and another.
403         */

404         public Complex scalarProduct(HilbertSpace.Member vec) {
405                 if(vec instanceof AbstractComplexVector)
406                         return scalarProduct((AbstractComplexVector)vec);
407                 else
408                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
409         }
410         /**
411         * Returns the scalar product of this vector and another.
412         * @param vec a complex vector
413         * @exception VectorDimensionException If the vectors are different sizes.
414         */

415         public Complex scalarProduct(final AbstractComplexVector vec) {
416                 if(vec instanceof Complex2Vector)
417                         return scalarProduct((Complex2Vector)vec);
418                 else {
419                         if(vec.N == 2) {
420                                 return new Complex(
421                                         xre*vec.getComponent(0).real()+xim*vec.getComponent(0).imag()+
422                                         yre*vec.getComponent(1).real()+yim*vec.getComponent(1).imag(),
423                                         xim*vec.getComponent(0).real()-xre*vec.getComponent(0).imag()+
424                                         yim*vec.getComponent(1).real()-yre*vec.getComponent(1).imag()
425                                 );
426                         } else
427                                 throw new VectorDimensionException("Vectors are different sizes.");
428                 }
429         }
430         /**
431         * Returns the scalar product of this vector and another.
432         * @param vec a complex 2-vector
433         */

434         public Complex scalarProduct(final Complex2Vector vec) {
435                 return new Complex(
436                         xre*vec.xre+xim*vec.xim+
437                         yre*vec.yre+yim*vec.yim,
438                         xim*vec.xre-xre*vec.xim+
439                         yim*vec.yre-yre*vec.yim
440                 );
441         }
442
443
444 // MAP COMPONENTS
445

446         /**
447         * Applies a function on all the vector components.
448         * @param mapping a user-defined function
449         * @return a complex 2-vector
450         */

451         public AbstractComplexVector mapComponents(final ComplexMapping mapping) {
452                 return new Complex2Vector(
453                         mapping.map(xre, xim),
454                         mapping.map(yre, yim)
455                 );
456         }
457 }
458
Popular Tags