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.219 * @author Mark Hale20 */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-vector52         */53     public boolean equals(Object 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*dxIm61                          + dyRe*dyRe + dyIm*dyIm <= tol*tol);62                 } else63                         return false;64         }65         /**66         * Returns a comma delimited string representing the value of this vector.67         */68         public String toString() {69                 final StringBuffer buf = new StringBuffer (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                         yre80                 );81         }82         /**83         * Returns the imaginary part of this complex 2-vector.84         */85         public AbstractDoubleVector imag() {86                 return new Double2Vector(87                         xim,88                         yim89                 );90         }91         /**92         * Returns a component of this vector.93         * @param n index of the vector component94         * @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 component121         * @param z a complex number122         * @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 component135         * @param x the real part of a complex number136         * @param y the imaginary part of a complex number137         * @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 l2-norm (magnitude).148         */149         public double norm() {150                 return Math.sqrt(151                         xre*xre + xim*xim152                         +yre*yre + yim*yim153                 );154         }155         /**156         * Returns the l-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 // OPERATIONS172 //============173 174         /**175         * Returns the negative of this vector.176         */177         public AbelianGroup.Member negate() {178                 return new Complex2Vector(179                         -xre, -xim,180                         -yre, -yim181                 );182         }183 184 // COMPLEX CONJUGATE185 186         /**187         * Returns the complex conjugate of this vector.188         * @return a complex 2-vector189         */190         public AbstractComplexVector conjugate() {191                 return new Complex2Vector(192                         xre, -xim,193                         yre, -yim194                 );195         }196 197 // ADDITION198 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                 else210                         throw new IllegalArgumentException ("Member class not recognised by this method.");211         }212         /**213         * Returns the addition of this vector and another.214         * @param vec a complex 2-vector215         */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                 } else223                         throw new VectorDimensionException("Vectors are different sizes.");224         }225         /**226         * Returns the addition of this vector and another.227         * @param vec a double 2-vector228         */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), yim234                         );235                 } else236                         throw new VectorDimensionException("Vectors are different sizes.");237         }238         /**239         * Returns the addition of this vector and another.240         * @param vec an integer 2-vector241         */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), yim247                         );248                 } else249                         throw new VectorDimensionException("Vectors are different sizes.");250         }251 252 // SUBTRACTION253 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                 else265                         throw new IllegalArgumentException ("Member class not recognised by this method.");266         }267         /**268         * Returns the subtraction of this vector by another.269         * @param vec a complex 2-vector270         */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                 } else278                         throw new VectorDimensionException("Vectors are different sizes.");279         }280         /**281         * Returns the subtraction of this vector by another.282         * @param vec a double 2-vector283         */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), yim289                         );290                 } else291                         throw new VectorDimensionException("Vectors are different sizes.");292         }293         /**294         * Returns the subtraction of this vector by another.295         * @param vec an integer 2-vector296         */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), yim302                         );303                 } else304                         throw new VectorDimensionException("Vectors are different sizes.");305         }306 307 // SCALAR MULTIPLICATION308 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                 else320                         throw new IllegalArgumentException ("Member class not recognised by this method.");321         }322         /**323         * Returns the multiplication of this vector by a scalar.324         * @param z a complex number325         * @return a complex 2-vector326         */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*real333                 );334         }335         /**336         * Returns the multiplication of this vector by a scalar.337         * @param k a double338         * @return a complex 2-vector339         */340         public AbstractComplexVector scalarMultiply(final double k) {341                 return new Complex2Vector(342                         k*xre, k*xim,343                         k*yre, k*yim344                 );345         }346 347 // SCALAR DIVISION348 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                 else358                         throw new IllegalArgumentException ("Member class not recognised by this method.");359         }360         /**361         * Returns the division of this vector by a scalar.362         * @param z a complex number363         * @return a complex 2-vector364         * @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)