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.219 * @author Mark Hale20 */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-vector58         */59     public boolean equals(Object 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*dxIm69                          + dyRe*dyRe + dyIm*dyIm70                          + dzRe*dzRe + dzIm*dzIm <= tol*tol);71                 } else72                         return false;73         }74         /**75         * Returns a comma delimited string representing the value of this vector.76         */77         public String toString() {78                 final StringBuffer buf = new StringBuffer (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                         zre90                 );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                         zim100                 );101         }102         /**103         * Returns a component of this vector.104         * @param n index of the vector component105         * @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 component135         * @param z a complex number136         * @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 component150         * @param x the real part of a complex number151         * @param y the imaginary part of a complex number152         * @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 l2-norm (magnitude).164         */165         public double norm() {166                 return Math.sqrt(167                         xre*xre + xim*xim168                         +yre*yre + yim*yim169                         +zre*zre + zim*zim170                 );171         }172         /**173         * Returns the l-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 // OPERATIONS192 //============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, -zim202                 );203         }204 205 // COMPLEX CONJUGATE206 207         /**208         * Returns the complex conjugate of this vector.209         * @return a complex 3-vector210         */211         public AbstractComplexVector conjugate() {212                 return new Complex3Vector(213                         xre, -xim,214                         yre, -yim,215                         zre, -zim216                 );217         }218 219 // ADDITION220 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                 else232                         throw new IllegalArgumentException ("Member class not recognised by this method.");233         }234         /**235         * Returns the addition of this vector and another.236         * @param vec a complex 3-vector237         */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                 } else246                         throw new VectorDimensionException("Vectors are different sizes.");247         }248         /**249         * Returns the addition of this vector and another.250         * @param vec a double 3-vector251         */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), zim258                         );259                 } else260                         throw new VectorDimensionException("Vectors are different sizes.");261         }262         /**263         * Returns the addition of this vector and another.264         * @param vec an integer 3-vector265         */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), zim272                         );273                 } else274                         throw new VectorDimensionException("Vectors are different sizes.");275         }276 277 // SUBTRACTION278 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                 else290                         throw new IllegalArgumentException ("Member class not recognised by this method.");291         }292         /**293         * Returns the subtraction of this vector by another.294         * @param vec a complex 3-vector295         */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                 } else304                         throw new VectorDimensionException("Vectors are different sizes.");305         }306         /**307         * Returns the subtraction of this vector by another.308         * @param vec a double 3-vector309         */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), zim316                         );317                 } else318                         throw new VectorDimensionException("Vectors are different sizes.");319         }320         /**321         * Returns the subtraction of this vector by another.322         * @param vec an integer 3-vector323         */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), zim330                         );331                 } else332                         throw new VectorDimensionException("Vectors are different sizes.");333         }334 335 // SCALAR MULTIPLICATION336 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                 else348                         throw new IllegalArgumentException ("Member class not recognised by this method.");349         }350         /**351         * Returns the multiplication of this vector by a scalar.352         * @param z a complex number353         * @return a complex 3-vector354         */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*real362                 );363         }364         /**365         * Returns the multiplication of this vector by a scalar.366         * @param k a double367         * @return a complex 3-vector368         */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*zim374                 );375         }376 377 // SCALAR DIVISION378 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                 else388                         throw new IllegalArgumentException ("Member class not recognised by this method.");389         }390         /**391         * Returns the division of this vector by a scalar.392         * @param z a complex number393         * @return a complex 3-vector394         * @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)