KickJava   Java API By Example, From Geeks To Geeks.

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


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 3D complex vector.
18 * @version 2.2
19 * @author Mark Hale
20 */

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

28         public Complex3Vector() {
29                 super(3);
30         }
31         /**
32         * Constructs a 3-vector.
33         * @param x x coordinate.
34         * @param y y coordinate.
35         * @param z z coordinate.
36         */

37         public Complex3Vector(final Complex x, final Complex y, final Complex z) {
38                 this();
39                 xre = x.real();
40                 xim = x.imag();
41                 yre = y.real();
42                 yim = y.imag();
43                 zre = z.real();
44                 zim = z.imag();
45         }
46         public Complex3Vector(double xRe, double xIm, double yRe, double yIm, double zRe, double zIm) {
47                 this();
48                 xre = xRe;
49                 xim = xIm;
50                 yre = yRe;
51                 yim = yIm;
52                 zre = zRe;
53                 zim = zIm;
54         }
55         /**
56         * Compares two complex vectors for equality.
57         * @param obj a complex 3-vector
58         */

59     public boolean equals(Object JavaDoc obj, double tol) {
60                 if(obj != null && (obj instanceof Complex3Vector)) {
61                         final Complex3Vector vec = (Complex3Vector) obj;
62                         double dxRe = xre - vec.xre;
63                         double dxIm = xim - vec.xim;
64                         double dyRe = yre - vec.yre;
65                         double dyIm = yim - vec.yim;
66                         double dzRe = zre - vec.zre;
67                         double dzIm = zim - vec.zim;
68                         return (dxRe*dxRe + dxIm*dxIm
69                          + dyRe*dyRe + dyIm*dyIm
70                          + dzRe*dzRe + dzIm*dzIm <= tol*tol);
71                 } else
72                         return false;
73         }
74         /**
75         * Returns a comma delimited string representing the value of this vector.
76         */

77         public String JavaDoc toString() {
78                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(15);
79                 buf.append(Complex.toString(xre, xim)).append(',').append(Complex.toString(yre, yim)).append(',').append(Complex.toString(zre, zim));
80                 return buf.toString();
81         }
82         /**
83         * Returns the real part of this complex 3-vector.
84         */

85         public AbstractDoubleVector real() {
86                 return new Double3Vector(
87                         xre,
88                         yre,
89                         zre
90                 );
91         }
92         /**
93         * Returns the imaginary part of this complex 3-vector.
94         */

95         public AbstractDoubleVector imag() {
96                 return new Double3Vector(
97                         xim,
98                         yim,
99                         zim
100                 );
101         }
102         /**
103         * Returns a component of this vector.
104         * @param n index of the vector component
105         * @exception VectorDimensionException If attempting to access an invalid component.
106         */

107         public Complex getComponent(final int n) {
108                 switch(n) {
109                         case 0 : return new Complex(xre, xim);
110                         case 1 : return new Complex(yre, yim);
111                         case 2 : return new Complex(zre, zim);
112                         default : throw new VectorDimensionException("Invalid component.");
113                 }
114         }
115         public double getRealComponent(final int n) {
116                 switch(n) {
117                         case 0 : return xre;
118                         case 1 : return yre;
119                         case 2 : return zre;
120                         default : throw new VectorDimensionException("Invalid component.");
121                 }
122         }
123         public double getImagComponent(final int n) {
124                 switch(n) {
125                         case 0 : return xim;
126                         case 1 : return yim;
127                         case 2 : return zim;
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 z a complex number
136         * @exception VectorDimensionException If attempting to access an invalid component.
137         */

138         public void setComponent(final int n, final Complex z) {
139                 switch(n) {
140                         case 0 : xre = z.real(); xim = z.imag(); break;
141                         case 1 : yre = z.real(); yim = z.imag(); break;
142                         case 2 : zre = z.real(); zim = z.imag(); break;
143                         default : throw new VectorDimensionException("Invalid component.");
144                 }
145         }
146         /**
147         * Sets the value of a component of this vector.
148         * Should only be used to initialise this vector.
149         * @param n index of the vector component
150         * @param x the real part of a complex number
151         * @param y the imaginary part of a complex number
152         * @exception VectorDimensionException If attempting to access an invalid component.
153         */

154         public void setComponent(final int n, final double x, final double y) {
155                 switch(n) {
156                         case 0 : xre = x; xim = y; break;
157                         case 1 : yre = x; yim = y; break;
158                         case 2 : zre = x; zim = y; break;
159                         default : throw new VectorDimensionException("Invalid component.");
160                 }
161         }
162         /**
163         * Returns the l<sup>2</sup>-norm (magnitude).
164         */

165         public double norm() {
166                 return Math.sqrt(
167                         xre*xre + xim*xim
168                         +yre*yre + yim*yim
169                         +zre*zre + zim*zim
170                 );
171         }
172         /**
173         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
174         */

175         public double infNorm() {
176                 double infNormSq = 0;
177                 double modSq;
178                 modSq = xre*xre + xim*xim;
179                 if(modSq > infNormSq)
180                         infNormSq = modSq;
181                 modSq = yre*yre + yim*yim;
182                 if(modSq > infNormSq)
183                         infNormSq = modSq;
184                 modSq = zre*zre + zim*zim;
185                 if(modSq > infNormSq)
186                         infNormSq = modSq;
187                 return Math.sqrt(infNormSq);
188         }
189
190 //============
191
// OPERATIONS
192
//============
193

194         /**
195         * Returns the negative of this vector.
196         */

197         public AbelianGroup.Member negate() {
198                 return new Complex3Vector(
199                         -xre, -xim,
200                         -yre, -yim,
201                         -zre, -zim
202                 );
203         }
204
205 // COMPLEX CONJUGATE
206

207         /**
208         * Returns the complex conjugate of this vector.
209         * @return a complex 3-vector
210         */

211         public AbstractComplexVector conjugate() {
212                 return new Complex3Vector(
213                         xre, -xim,
214                         yre, -yim,
215                         zre, -zim
216                 );
217         }
218
219 // ADDITION
220

221         /**
222         * Returns the addition of this vector and another.
223         */

224         public AbelianGroup.Member add(final AbelianGroup.Member vec) {
225                 if(vec instanceof AbstractComplexVector)
226                         return add((AbstractComplexVector)vec);
227                 else if(vec instanceof AbstractDoubleVector)
228                         return add((AbstractDoubleVector)vec);
229                 else if(vec instanceof AbstractIntegerVector)
230                         return add((AbstractIntegerVector)vec);
231                 else
232                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
233         }
234         /**
235         * Returns the addition of this vector and another.
236         * @param vec a complex 3-vector
237         */

238         public AbstractComplexVector add(final AbstractComplexVector vec) {
239                 if(vec.N == 3) {
240                         return new Complex3Vector(
241                                 xre+vec.getComponent(0).real(), xim+vec.getComponent(0).imag(),
242                                 yre+vec.getComponent(1).real(), yim+vec.getComponent(1).imag(),
243                                 zre+vec.getComponent(2).real(), zim+vec.getComponent(2).imag()
244                         );
245                 } else
246                         throw new VectorDimensionException("Vectors are different sizes.");
247         }
248         /**
249         * Returns the addition of this vector and another.
250         * @param vec a double 3-vector
251         */

252         public AbstractComplexVector add(final AbstractDoubleVector vec) {
253                 if(vec.N == 3) {
254                         return new Complex3Vector(
255                                 xre+vec.getComponent(0), xim,
256                                 yre+vec.getComponent(1), yim,
257                                 zre+vec.getComponent(2), zim
258                         );
259                 } else
260                         throw new VectorDimensionException("Vectors are different sizes.");
261         }
262         /**
263         * Returns the addition of this vector and another.
264         * @param vec an integer 3-vector
265         */

266         public AbstractComplexVector add(final AbstractIntegerVector vec) {
267                 if(vec.N == 3) {
268                         return new Complex3Vector(
269                                 xre+vec.getComponent(0), xim,
270                                 yre+vec.getComponent(1), yim,
271                                 zre+vec.getComponent(2), zim
272                         );
273                 } else
274                         throw new VectorDimensionException("Vectors are different sizes.");
275         }
276
277 // SUBTRACTION
278

279         /**
280         * Returns the subtraction of this vector by another.
281         */

282         public AbelianGroup.Member subtract(final AbelianGroup.Member vec) {
283                 if(vec instanceof AbstractComplexVector)
284                         return subtract((AbstractComplexVector)vec);
285                 else if(vec instanceof AbstractDoubleVector)
286                         return subtract((AbstractDoubleVector)vec);
287                 else if(vec instanceof AbstractIntegerVector)
288                         return subtract((AbstractIntegerVector)vec);
289                 else
290                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
291         }
292         /**
293         * Returns the subtraction of this vector by another.
294         * @param vec a complex 3-vector
295         */

296         public AbstractComplexVector subtract(final AbstractComplexVector vec) {
297                 if(vec.N == 3) {
298                         return new Complex3Vector(
299                                 xre-vec.getComponent(0).real(), xim-vec.getComponent(0).imag(),
300                                 yre-vec.getComponent(1).real(), yim-vec.getComponent(1).imag(),
301                                 zre-vec.getComponent(2).real(), zim-vec.getComponent(2).imag()
302                         );
303                 } else
304                         throw new VectorDimensionException("Vectors are different sizes.");
305         }
306         /**
307         * Returns the subtraction of this vector by another.
308         * @param vec a double 3-vector
309         */

310         public AbstractComplexVector subtract(final AbstractDoubleVector vec) {
311                 if(vec.N == 3) {
312                         return new Complex3Vector(
313                                 xre-vec.getComponent(0), xim,
314                                 yre-vec.getComponent(1), yim,
315                                 zre-vec.getComponent(2), zim
316                         );
317                 } else
318                         throw new VectorDimensionException("Vectors are different sizes.");
319         }
320         /**
321         * Returns the subtraction of this vector by another.
322         * @param vec an integer 3-vector
323         */

324         public AbstractComplexVector subtract(final AbstractIntegerVector vec) {
325                 if(vec.N == 3) {
326                         return new Complex3Vector(
327                                 xre-vec.getComponent(0), xim,
328                                 yre-vec.getComponent(1), yim,
329                                 zre-vec.getComponent(2), zim
330                         );
331                 } else
332                         throw new VectorDimensionException("Vectors are different sizes.");
333         }
334
335 // SCALAR MULTIPLICATION
336

337         /**
338         * Returns the multiplication of this vector by a scalar.
339         */

340         public Module.Member scalarMultiply(Ring.Member x) {
341                 if(x instanceof Complex)
342                         return scalarMultiply((Complex)x);
343                 else if(x instanceof MathDouble)
344                         return scalarMultiply(((MathDouble)x).value());
345                 else if(x instanceof MathInteger)
346                         return scalarMultiply(((MathInteger)x).value());
347                 else
348                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
349         }
350         /**
351         * Returns the multiplication of this vector by a scalar.
352         * @param z a complex number
353         * @return a complex 3-vector
354         */

355         public AbstractComplexVector scalarMultiply(final Complex z) {
356                 final double real=z.real();
357                 final double imag=z.imag();
358                 return new Complex3Vector(
359                         xre*real-xim*imag, xre*imag+xim*real,
360                         yre*real-yim*imag, yre*imag+yim*real,
361                         zre*real-zim*imag, zre*imag+zim*real
362                 );
363         }
364         /**
365         * Returns the multiplication of this vector by a scalar.
366         * @param k a double
367         * @return a complex 3-vector
368         */

369         public AbstractComplexVector scalarMultiply(final double k) {
370                 return new Complex3Vector(
371                         k*xre, k*xim,
372                         k*yre, k*yim,
373                         k*zre, k*zim
374                 );
375         }
376
377 // SCALAR DIVISION
378

379         /**
380         * Returns the division of this vector by a scalar.
381         */

382         public VectorSpace.Member scalarDivide(Field.Member x) {
383                 if(x instanceof Complex)
384                         return scalarDivide((Complex)x);
385                 else if(x instanceof MathDouble)
386                         return scalarDivide(((MathDouble)x).value());
387                 else
388                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
389         }
390         /**
391         * Returns the division of this vector by a scalar.
392         * @param z a complex number
393         * @return a complex 3-vector
394         * @exception ArithmeticException If divide by zero.
395         */

396         public AbstractComplexVector scalarDivide(final Complex z) {
397                 final double real=z.real();
398                 final double imag=z.imag();
399                 final double a,denom;
400                 if(Math.abs(real)<Math.abs(imag)) {
401                         a=real/imag;
402                         denom=real*a+imag;
403                         return new Complex3Vector(
404                                 (xre*a+xim)/denom, (xim*a-xre)/denom,
405                                 (yre*a+yim)/denom, (yim*a-yre)/denom,
406                                 (zre*a+zim)/denom, (zim*a-zre)/denom
407                         );
408                 } else {
409                         a=imag/real;
410                         denom=real+imag*a;
411                         return new Complex3Vector(
412                                 (xre+xim*a)/denom, (xim-xre*a)/denom,
413                                 (yre+yim*a)/denom, (yim-yre*a)/denom,
414                                 (zre+zim*a)/denom, (zim-zre*a)/denom
415                         );
416                 }
417         }
418         /**
419         * Returns the division of this vector by a scalar.
420         * @param k a double
421         * @return a complex 3-vector
422         * @exception ArithmeticException If divide by zero.
423         */

424         public AbstractComplexVector scalarDivide(final double k) {
425                 return new Complex3Vector(
426                         xre/k, xim/k,
427                         yre/k, yim/k,
428                         zre/k, zim/k
429                 );
430         }
431
432 // SCALAR PRODUCT
433

434         /**
435         * Returns the scalar product of this vector and another.
436         */

437         public Complex scalarProduct(HilbertSpace.Member vec) {
438                 if(vec instanceof AbstractComplexVector)
439                         return scalarProduct((AbstractComplexVector)vec);
440                 else
441                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
442         }
443         /**
444         * Returns the scalar product of this vector and another.
445         * @param vec a complex vector
446         * @exception VectorDimensionException If the vectors are different sizes.
447         */

448         public Complex scalarProduct(final AbstractComplexVector vec) {
449                 if(vec instanceof Complex3Vector)
450                         return scalarProduct((Complex3Vector)vec);
451                 else {
452                         if(vec.N == 3) {
453                                 return new Complex(
454                                         xre*vec.getComponent(0).real()+xim*vec.getComponent(0).imag()+
455                                         yre*vec.getComponent(1).real()+yim*vec.getComponent(1).imag()+
456                                         zre*vec.getComponent(2).real()+zim*vec.getComponent(2).imag(),
457                                         xim*vec.getComponent(0).real()-xre*vec.getComponent(0).imag()+
458                                         yim*vec.getComponent(1).real()-yre*vec.getComponent(1).imag()+
459                                         zim*vec.getComponent(2).real()-zre*vec.getComponent(2).imag()
460                                 );
461                         } else
462                                 throw new VectorDimensionException("Vectors are different sizes.");
463                 }
464         }
465         /**
466         * Returns the scalar product of this vector and another.
467         * @param vec a complex 3-vector
468         */

469         public Complex scalarProduct(final Complex3Vector vec) {
470                 return new Complex(
471                         xre*vec.xre+xim*vec.xim+
472                         yre*vec.yre+yim*vec.yim+
473                         zre*vec.zre+zim*vec.zim,
474                         xim*vec.xre-xre*vec.xim+
475                         yim*vec.yre-yre*vec.yim+
476                         zim*vec.zre-zre*vec.zim
477                 );
478         }
479
480 // VECTOR PRODUCT
481

482         /**
483         * Returns the vector product of this vector and another.
484         * @param vec a complex 3-vector
485         */

486         public Complex3Vector multiply(final Complex3Vector vec) {
487                 return new Complex3Vector(
488                         yre*vec.zre-yim*vec.zim-zre*vec.yre+zim*vec.yim,
489                         yre*vec.zim+yim*vec.zre-zre*vec.yim-zim*vec.yre,
490                         zre*vec.xre-zim*vec.xim-xre*vec.zre+xim*vec.zim,
491                         zre*vec.xim+zim*vec.xre-xre*vec.zim-xim*vec.zre,
492                         xre*vec.yre-xim*vec.yim-yre*vec.xre+yim*vec.xim,
493                         xre*vec.yim+xim*vec.yre-yre*vec.xim-yim*vec.xre
494                 );
495         }
496
497 // MAP COMPONENTS
498

499         /**
500         * Applies a function on all the vector components.
501         * @param mapping a user-defined function
502         * @return a complex 3-vector
503         */

504         public AbstractComplexVector mapComponents(final ComplexMapping mapping) {
505                 return new Complex3Vector(
506                         mapping.map(xre, xim),
507                         mapping.map(yre, yim),
508                         mapping.map(zre, zim)
509                 );
510         }
511 }
512
Popular Tags