1 2 package JSci.maths.matrices; 3 4 import JSci.GlobalSettings; 5 import JSci.maths.MathDouble; 6 import JSci.maths.MathInteger; 7 import JSci.maths.Complex; 8 import JSci.maths.ComplexMapping; 9 import JSci.maths.DimensionException; 10 import JSci.maths.vectors.AbstractComplexVector; 11 import JSci.maths.vectors.ComplexVector; 12 import JSci.maths.groups.AbelianGroup; 13 import JSci.maths.algebras.*; 14 import JSci.maths.fields.*; 15 16 21 public abstract class AbstractComplexMatrix extends Matrix { 22 25 protected AbstractComplexMatrix(final int rows,final int cols) { 26 super(rows,cols); 27 } 28 32 public final boolean equals(Object obj) { 33 if(obj instanceof AbstractComplexMatrix) { 34 return equals((AbstractComplexMatrix)obj); 35 } else { 36 return false; 37 } 38 } 39 44 public final boolean equals(AbstractComplexMatrix m) { 45 return equals(m, GlobalSettings.ZERO_TOL); 46 } 47 public boolean equals(AbstractComplexMatrix m, double tol) { 48 if(m != null && numRows == m.rows() && numCols == m.columns()) { 49 double sumSqr = 0.0; 50 for(int i=0;i<numRows;i++) { 51 for(int j=0;j<numCols;j++) { 52 double deltaRe = getRealElement(i,j)-m.getRealElement(i,j); 53 double deltaIm = getImagElement(i,j)-m.getImagElement(i,j); 54 sumSqr += deltaRe*deltaRe + deltaIm*deltaIm; 55 } 56 } 57 return (sumSqr <= tol*tol); 58 } else { 59 return false; 60 } 61 } 62 65 public String toString() { 66 final StringBuffer buf=new StringBuffer (5*numRows*numCols); 67 for(int i=0;i<numRows;i++) { 68 for(int j=0;j<numCols;j++) { 69 buf.append(getElement(i,j)); 70 buf.append(' '); 71 } 72 buf.append('\n'); 73 } 74 return buf.toString(); 75 } 76 79 public int hashCode() { 80 return (int)Math.exp(infNorm()); 81 } 82 86 public AbstractDoubleMatrix real() { 87 final double ans[][]=new double[numRows][numCols]; 88 for(int i=0;i<numRows;i++) { 89 for(int j=0;j<numCols;j++) 90 ans[i][j]=getElement(i,j).real(); 91 } 92 return new DoubleMatrix(ans); 93 } 94 98 public AbstractDoubleMatrix imag() { 99 final double ans[][]=new double[numRows][numCols]; 100 for(int i=0;i<numRows;i++) { 101 for(int j=0;j<numCols;j++) 102 ans[i][j]=getElement(i,j).imag(); 103 } 104 return new DoubleMatrix(ans); 105 } 106 112 public abstract Complex getElement(final int i, final int j); 113 119 public abstract double getRealElement(final int i, final int j); 120 126 public abstract double getImagElement(final int i, final int j); 127 135 public abstract void setElement(final int i, final int j, final Complex z); 136 145 public abstract void setElement(final int i, final int j, final double x, final double y); 146 public Object getSet() { 147 throw new RuntimeException ("Not implemented: file bug"); 148 } 149 153 public double infNorm() { 154 double result=0.0,tmpResult; 155 for(int i=0;i<numRows;i++) { 156 tmpResult=0.0; 157 for(int j=0;j<numCols;j++) 158 tmpResult+=getElement(i,j).norm(); 159 if(tmpResult>result) 160 result=tmpResult; 161 } 162 return result; 163 } 164 169 public double frobeniusNorm() { 170 double result=0.0; 171 for(int i=0;i<numRows;i++) { 172 for(int j=0;j<numCols;j++) 173 result += getRealElement(i,j)*getRealElement(i,j) + getImagElement(i,j)*getImagElement(i,j); 174 } 175 return Math.sqrt(result); 176 } 177 178 182 185 public AbelianGroup.Member negate() { 186 final double arrayRe[][]=new double[numRows][numCols]; 187 final double arrayIm[][]=new double[numRows][numCols]; 188 for(int i=0;i<numRows;i++) { 189 arrayRe[i][0]=-getRealElement(i,0); 190 arrayIm[i][0]=-getImagElement(i,0); 191 for(int j=1;j<numCols;j++) { 192 arrayRe[i][j]=-getRealElement(i,j); 193 arrayIm[i][j]=-getImagElement(i,j); 194 } 195 } 196 return new ComplexMatrix(arrayRe,arrayIm); 197 } 198 199 201 204 public final AbelianGroup.Member add(final AbelianGroup.Member m) { 205 if(m instanceof AbstractComplexMatrix) 206 return add((AbstractComplexMatrix)m); 207 else 208 throw new IllegalArgumentException ("Member class not recognised by this method."); 209 } 210 215 public AbstractComplexMatrix add(final AbstractComplexMatrix m) { 216 if(numRows==m.rows() && numCols==m.columns()) { 217 final double arrayRe[][]=new double[numRows][numCols]; 218 final double arrayIm[][]=new double[numRows][numCols]; 219 for(int i=0;i<numRows;i++) { 220 arrayRe[i][0] = getRealElement(i,0)+m.getRealElement(i,0); 221 arrayIm[i][0] = getImagElement(i,0)+m.getImagElement(i,0); 222 for(int j=1;j<numCols;j++) { 223 arrayRe[i][j] = getRealElement(i,j)+m.getRealElement(i,j); 224 arrayIm[i][j] = getImagElement(i,j)+m.getImagElement(i,j); 225 } 226 } 227 return new ComplexMatrix(arrayRe,arrayIm); 228 } else 229 throw new MatrixDimensionException("Matrices are different sizes."); 230 } 231 232 234 237 public final AbelianGroup.Member subtract(final AbelianGroup.Member m) { 238 if(m instanceof AbstractComplexMatrix) 239 return subtract((AbstractComplexMatrix)m); 240 else 241 throw new IllegalArgumentException ("Member class not recognised by this method."); 242 } 243 248 public AbstractComplexMatrix subtract(final AbstractComplexMatrix m) { 249 if(numRows==m.rows() && numCols==m.columns()) { 250 final double arrayRe[][]=new double[numRows][numCols]; 251 final double arrayIm[][]=new double[numRows][numCols]; 252 for(int i=0;i<numRows;i++) { 253 arrayRe[i][0] = getRealElement(i,0)-m.getRealElement(i,0); 254 arrayIm[i][0] = getImagElement(i,0)-m.getImagElement(i,0); 255 for(int j=1;j<numCols;j++) { 256 arrayRe[i][j] = getRealElement(i,j)-m.getRealElement(i,j); 257 arrayIm[i][j] = getImagElement(i,j)-m.getImagElement(i,j); 258 } 259 } 260 return new ComplexMatrix(arrayRe,arrayIm); 261 } else 262 throw new MatrixDimensionException("Matrices are different sizes."); 263 } 264 265 267 270 public final Module.Member scalarMultiply(Ring.Member x) { 271 if(x instanceof Complex) 272 return scalarMultiply((Complex)x); 273 else if(x instanceof MathDouble) 274 return scalarMultiply(((MathDouble)x).value()); 275 else if(x instanceof MathInteger) 276 return scalarMultiply(((MathInteger)x).value()); 277 else 278 throw new IllegalArgumentException ("Member class not recognised by this method."); 279 } 280 285 public AbstractComplexMatrix scalarMultiply(final Complex z) { 286 final double real=z.real(); 287 final double imag=z.imag(); 288 final double arrayRe[][]=new double[numRows][numCols]; 289 final double arrayIm[][]=new double[numRows][numCols]; 290 for(int i=0;i<numRows;i++) { 291 arrayRe[i][0] = real*getRealElement(i,0)-imag*getImagElement(i,0); 292 arrayIm[i][0] = imag*getRealElement(i,0)+real*getImagElement(i,0); 293 for(int j=1;j<numCols;j++) { 294 arrayRe[i][j] = real*getRealElement(i,j)-imag*getImagElement(i,j); 295 arrayIm[i][j] = imag*getRealElement(i,j)+real*getImagElement(i,j); 296 } 297 } 298 return new ComplexMatrix(arrayRe,arrayIm); 299 } 300 305 public AbstractComplexMatrix scalarMultiply(final double x) { 306 final double arrayRe[][]=new double[numRows][numCols]; 307 final double arrayIm[][]=new double[numRows][numCols]; 308 for(int i=0;i<numRows;i++) { 309 arrayRe[i][0] = x*getRealElement(i,0); 310 arrayIm[i][0] = x*getImagElement(i,0); 311 for(int j=1;j<numCols;j++) { 312 arrayRe[i][j] = x*getRealElement(i,j); 313 arrayIm[i][j] = x*getImagElement(i,j); 314 } 315 } 316 return new ComplexMatrix(arrayRe,arrayIm); 317 } 318 319 321 324 public final VectorSpace.Member scalarDivide(Field.Member x) { 325 if(x instanceof Complex) 326 return scalarDivide((Complex)x); 327 if(x instanceof MathDouble) 328 return scalarDivide(((MathDouble)x).value()); 329 else 330 throw new IllegalArgumentException ("Member class not recognised by this method."); 331 } 332 337 public AbstractComplexMatrix scalarDivide(final Complex z) { 338 final Complex array[][]=new Complex[numRows][numCols]; 339 for(int i=0;i<numRows;i++) { 340 array[i][0] = getElement(i,0).divide(z); 341 for(int j=1;j<numCols;j++) 342 array[i][j] = getElement(i,j).divide(z); 343 } 344 return new ComplexMatrix(array); 345 } 346 351 public AbstractComplexMatrix scalarDivide(final double x) { 352 final double arrayRe[][]=new double[numRows][numCols]; 353 final double arrayIm[][]=new double[numRows][numCols]; 354 for(int i=0;i<numRows;i++) { 355 arrayRe[i][0]=getRealElement(i,0)/x; 356 arrayIm[i][0]=getImagElement(i,0)/x; 357 for(int j=1;j<numCols;j++) { 358 arrayRe[i][j]=getRealElement(i,j)/x; 359 arrayIm[i][j]=getImagElement(i,j)/x; 360 } 361 } 362 return new ComplexMatrix(arrayRe,arrayIm); 363 } 364 365 367 372 public Complex scalarProduct(final AbstractComplexMatrix m) { 373 if(numRows==m.rows() && numCols==m.columns()) { 374 double real = 0.0, imag = 0.0; 375 for(int i=0; i<numRows; i++) { 376 real += getRealElement(i,0)*m.getRealElement(i,0) + getImagElement(i,0)*m.getImagElement(i,0); 377 imag += getImagElement(i,0)*m.getRealElement(i,0) - getRealElement(i,0)*m.getImagElement(i,0); 378 for(int j=1; j<numCols; j++) { 379 real += getRealElement(i,j)*m.getRealElement(i,j) + getImagElement(i,j)*m.getImagElement(i,j); 380 imag += getImagElement(i,j)*m.getRealElement(i,j) - getRealElement(i,j)*m.getImagElement(i,j); 381 } 382 } 383 return new Complex(real, imag); 384 } else { 385 throw new MatrixDimensionException("Matrices are different sizes."); 386 } 387 } 388 389 391 396 public AbstractComplexVector multiply(final AbstractComplexVector v) { 397 if(numCols==v.dimension()) { 398 final double arrayRe[]=new double[numRows]; 399 final double arrayIm[]=new double[numRows]; 400 Complex tmp; 401 for(int i=0;i<numRows;i++) { 402 tmp = getElement(i,0).multiply(v.getComponent(0)); 403 arrayRe[i]=tmp.real(); 404 arrayIm[i]=tmp.imag(); 405 for(int j=1;j<numCols;j++) { 406 tmp = getElement(i,j).multiply(v.getComponent(j)); 407 arrayRe[i]+=tmp.real(); 408 arrayIm[i]+=tmp.imag(); 409 } 410 } 411 return new ComplexVector(arrayRe,arrayIm); 412 } else 413 throw new DimensionException("Matrix and vector are incompatible."); 414 } 415 418 public final Ring.Member multiply(final Ring.Member m) { 419 if(m instanceof AbstractComplexMatrix) 420 return multiply((AbstractComplexMatrix)m); 421 else 422 throw new IllegalArgumentException ("Matrix class not recognised by this method."); 423 } 424 430 public AbstractComplexMatrix multiply(final AbstractComplexMatrix m) { 431 if(numCols==m.rows()) { 432 final double arrayRe[][]=new double[numRows][m.columns()]; 433 final double arrayIm[][]=new double[numRows][m.columns()]; 434 Complex tmp; 435 for(int j=0;j<numRows;j++) { 436 for(int k=0;k<m.columns();k++) { 437 tmp=getElement(j,0).multiply(m.getElement(0,k)); 438 arrayRe[j][k]=tmp.real(); 439 arrayIm[j][k]=tmp.imag(); 440 for(int n=1;n<numCols;n++) { 441 tmp=getElement(j,n).multiply(m.getElement(n,k)); 442 arrayRe[j][k]+=tmp.real(); 443 arrayIm[j][k]+=tmp.imag(); 444 } 445 } 446 } 447 if(numRows==m.columns()) 448 return new ComplexSquareMatrix(arrayRe,arrayIm); 449 else 450 return new ComplexMatrix(arrayRe,arrayIm); 451 } else { 452 throw new MatrixDimensionException("Incompatible matrices."); 453 } 454 } 455 456 458 461 public AbstractComplexMatrix directSum(final AbstractComplexMatrix m) { 462 final double arrayRe[][]=new double[numRows+m.numRows][numCols+m.numCols]; 463 final double arrayIm[][]=new double[numRows+m.numRows][numCols+m.numCols]; 464 for(int j,i=0;i<numRows;i++) { 465 for(j=0;j<numCols;j++) { 466 arrayRe[i][j]=getRealElement(i,j); 467 arrayIm[i][j]=getImagElement(i,j); 468 } 469 } 470 for(int j,i=0;i<m.numRows;i++) { 471 for(j=0;j<m.numCols;j++) { 472 arrayRe[i+numRows][j+numCols]=m.getRealElement(i,j); 473 arrayIm[i+numRows][j+numCols]=m.getImagElement(i,j); 474 } 475 } 476 return new ComplexMatrix(arrayRe,arrayIm); 477 } 478 479 481 484 public AbstractComplexMatrix tensor(final AbstractComplexMatrix m) { 485 final double arrayRe[][]=new double[numRows*m.numRows][numCols*m.numCols]; 486 final double arrayIm[][]=new double[numRows*m.numRows][numCols*m.numCols]; 487 for(int i=0;i<numRows;i++) { 488 for(int j=0;j<numCols;j++) { 489 for(int k=0;k<m.numRows;j++) { 490 for(int l=0;l<m.numCols;l++) { 491 Complex tmp=getElement(i,j).multiply(m.getElement(k,l)); 492 arrayRe[i*m.numRows+k][j*m.numCols+l]=tmp.real(); 493 arrayIm[i*m.numRows+k][j*m.numCols+l]=tmp.imag(); 494 } 495 } 496 } 497 } 498 return new ComplexMatrix(arrayRe,arrayIm); 499 } 500 501 503 507 public AbstractComplexMatrix hermitianAdjoint() { 508 final double arrayRe[][]=new double[numCols][numRows]; 509 final double arrayIm[][]=new double[numCols][numRows]; 510 for(int i=0;i<numRows;i++) { 511 arrayRe[0][i]=getRealElement(i,0); 512 arrayIm[0][i]=-getImagElement(i,0); 513 for(int j=1;j<numCols;j++) { 514 arrayRe[j][i]=getRealElement(i,j); 515 arrayIm[j][i]=-getImagElement(i,j); 516 } 517 } 518 return new ComplexMatrix(arrayRe,arrayIm); 519 } 520 521 523 527 public AbstractComplexMatrix conjugate() { 528 final double arrayRe[][]=new double[numCols][numRows]; 529 final double arrayIm[][]=new double[numCols][numRows]; 530 for(int i=0;i<numRows;i++) { 531 arrayRe[i][0]=getRealElement(i,0); 532 arrayIm[i][0]=-getImagElement(i,0); 533 for(int j=1;j<numCols;j++) { 534 arrayRe[i][j]=getRealElement(i,j); 535 arrayIm[i][j]=-getImagElement(i,j); 536 } 537 } 538 return new ComplexMatrix(arrayRe,arrayIm); 539 } 540 541 543 547 public Matrix transpose() { 548 final double arrayRe[][]=new double[numCols][numRows]; 549 final double arrayIm[][]=new double[numCols][numRows]; 550 for(int i=0;i<numRows;i++) { 551 arrayRe[0][i]=getRealElement(i,0); 552 arrayIm[0][i]=getImagElement(i,0); 553 for(int j=1;j<numCols;j++) { 554 arrayRe[j][i]=getRealElement(i,j); 555 arrayIm[j][i]=getImagElement(i,j); 556 } 557 } 558 return new ComplexMatrix(arrayRe,arrayIm); 559 } 560 561 563 568 public AbstractComplexMatrix mapElements(final ComplexMapping f) { 569 final Complex array[][]=new Complex[numRows][numCols]; 570 for(int i=0;i<numRows;i++) { 571 array[i][0]=f.map(getElement(i,0)); 572 for(int j=1;j<numCols;j++) 573 array[i][j]=f.map(getElement(i,j)); 574 } 575 return new ComplexMatrix(array); 576 } 577 } 578 579 | Popular Tags |