1 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 21 public final class Complex2Vector extends AbstractComplexVector { 22 protected double xre, xim; 23 protected double yre, yim; 24 27 public Complex2Vector() { 28 super(2); 29 } 30 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 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*dxIm 61 + dyRe*dyRe + dyIm*dyIm <= tol*tol); 62 } else 63 return false; 64 } 65 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 76 public AbstractDoubleVector real() { 77 return new Double2Vector( 78 xre, 79 yre 80 ); 81 } 82 85 public AbstractDoubleVector imag() { 86 return new Double2Vector( 87 xim, 88 yim 89 ); 90 } 91 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 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 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 149 public double norm() { 150 return Math.sqrt( 151 xre*xre + xim*xim 152 +yre*yre + yim*yim 153 ); 154 } 155 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 174 177 public AbelianGroup.Member negate() { 178 return new Complex2Vector( 179 -xre, -xim, 180 -yre, -yim 181 ); 182 } 183 184 186 190 public AbstractComplexVector conjugate() { 191 return new Complex2Vector( 192 xre, -xim, 193 yre, -yim 194 ); 195 } 196 197 199 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 ("Member class not recognised by this method."); 211 } 212 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 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 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 254 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 ("Member class not recognised by this method."); 266 } 267 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 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 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 309 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 ("Member class not recognised by this method."); 321 } 322 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 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 349 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 ("Member class not recognised by this method."); 359 } 360 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 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 401 404 public Complex scalarProduct(HilbertSpace.Member vec) { 405 if(vec instanceof AbstractComplexVector) 406 return scalarProduct((AbstractComplexVector)vec); 407 else 408 throw new IllegalArgumentException ("Member class not recognised by this method."); 409 } 410 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 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 446 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 |