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 Complex3Vector extends AbstractComplexVector { 22 protected double xre, xim; 23 protected double yre, yim; 24 protected double zre, zim; 25 28 public Complex3Vector() { 29 super(3); 30 } 31 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 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*dxIm 69 + dyRe*dyRe + dyIm*dyIm 70 + dzRe*dzRe + dzIm*dzIm <= tol*tol); 71 } else 72 return false; 73 } 74 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 85 public AbstractDoubleVector real() { 86 return new Double3Vector( 87 xre, 88 yre, 89 zre 90 ); 91 } 92 95 public AbstractDoubleVector imag() { 96 return new Double3Vector( 97 xim, 98 yim, 99 zim 100 ); 101 } 102 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 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 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 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 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 194 197 public AbelianGroup.Member negate() { 198 return new Complex3Vector( 199 -xre, -xim, 200 -yre, -yim, 201 -zre, -zim 202 ); 203 } 204 205 207 211 public AbstractComplexVector conjugate() { 212 return new Complex3Vector( 213 xre, -xim, 214 yre, -yim, 215 zre, -zim 216 ); 217 } 218 219 221 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 ("Member class not recognised by this method."); 233 } 234 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 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 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 279 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 ("Member class not recognised by this method."); 291 } 292 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 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 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 337 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 ("Member class not recognised by this method."); 349 } 350 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 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 379 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 ("Member class not recognised by this method."); 389 } 390 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 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 434 437 public Complex scalarProduct(HilbertSpace.Member vec) { 438 if(vec instanceof AbstractComplexVector) 439 return scalarProduct((AbstractComplexVector)vec); 440 else 441 throw new IllegalArgumentException ("Member class not recognised by this method."); 442 } 443 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 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 482 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 499 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 |