KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > matrices > AbstractComplexMatrix


1 /* AUTO-GENERATED */
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 /**
17 * The AbstractComplexMatrix class provides an object for encapsulating matrices containing complex numbers.
18 * @version 2.2
19 * @author Mark Hale
20 */

21 public abstract class AbstractComplexMatrix extends Matrix {
22         /**
23         * Constructs a matrix.
24         */

25         protected AbstractComplexMatrix(final int rows,final int cols) {
26                 super(rows,cols);
27         }
28         /**
29         * Compares two complex matrices for equality.
30         * @param obj a complex matrix
31         */

32         public final boolean equals(Object JavaDoc obj) {
33                 if(obj instanceof AbstractComplexMatrix) {
34                         return equals((AbstractComplexMatrix)obj);
35                 } else {
36                         return false;
37                 }
38         }
39         /**
40         * Compares two complex matrices for equality.
41         * Two matrices are considered to be equal if the Frobenius norm of their difference is within the zero tolerance.
42         * @param m a complex matrix
43         */

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         /**
63         * Returns a string representing this matrix.
64         */

65         public String JavaDoc toString() {
66                 final StringBuffer JavaDoc buf=new StringBuffer JavaDoc(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         /**
77         * Returns a hashcode for this matrix.
78         */

79         public int hashCode() {
80                 return (int)Math.exp(infNorm());
81         }
82         /**
83         * Returns the real part of this complex matrix.
84         * @return a double matrix
85         */

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         /**
95         * Returns the imaginary part of this complex matrix.
96         * @return a double matrix
97         */

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         /**
107         * Returns an element of the matrix.
108         * @param i row index of the element
109         * @param j column index of the element
110         * @exception MatrixDimensionException If attempting to access an invalid element.
111         */

112         public abstract Complex getElement(final int i, final int j);
113         /**
114         * Returns the real part of an element of the matrix.
115         * @param i row index of the element
116         * @param j column index of the element
117         * @exception MatrixDimensionException If attempting to access an invalid element.
118         */

119         public abstract double getRealElement(final int i, final int j);
120         /**
121         * Returns the imag part of an element of the matrix.
122         * @param i row index of the element
123         * @param j column index of the element
124         * @exception MatrixDimensionException If attempting to access an invalid element.
125         */

126         public abstract double getImagElement(final int i, final int j);
127         /**
128         * Sets the value of an element of the matrix.
129         * Should only be used to initialise this matrix.
130         * @param i row index of the element
131         * @param j column index of the element
132         * @param z a complex number
133         * @exception MatrixDimensionException If attempting to access an invalid element.
134         */

135         public abstract void setElement(final int i, final int j, final Complex z);
136         /**
137         * Sets the value of an element of the matrix.
138         * Should only be used to initialise this matrix.
139         * @param i row index of the element
140         * @param j column index of the element
141         * @param x the real part of a complex number
142         * @param y the imaginary part of a complex number
143         * @exception MatrixDimensionException If attempting to access an invalid element.
144         */

145         public abstract void setElement(final int i, final int j, final double x, final double y);
146     public Object JavaDoc getSet() {
147         throw new RuntimeException JavaDoc("Not implemented: file bug");
148     }
149         /**
150         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
151         * @author Taber Smith
152         */

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         /**
165         * Returns the Frobenius or Hilbert-Schmidt (l<sup>2</sup>) norm.
166         * @jsci.planetmath FrobeniusMatrixNorm
167         * @author Taber Smith
168         */

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 //============
179
// OPERATIONS
180
//============
181

182         /**
183         * Returns the negative of this matrix.
184         */

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 // ADDITION
200

201         /**
202         * Returns the addition of this matrix and another.
203         */

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 JavaDoc("Member class not recognised by this method.");
209         }
210         /**
211         * Returns the addition of this matrix and another.
212         * @param m a complex matrix
213         * @exception MatrixDimensionException If the matrices are different sizes.
214         */

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 // SUBTRACTION
233

234         /**
235         * Returns the subtraction of this matrix by another.
236         */

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 JavaDoc("Member class not recognised by this method.");
242         }
243         /**
244         * Returns the subtraction of this matrix by another.
245         * @param m a complex matrix
246         * @exception MatrixDimensionException If the matrices are different sizes.
247         */

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 // SCALAR MULTIPLICATION
266

267         /**
268         * Returns the multiplication of this matrix by a scalar.
269         */

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 JavaDoc("Member class not recognised by this method.");
279         }
280         /**
281         * Returns the multiplication of this matrix by a scalar.
282         * @param z a complex number
283         * @return a complex matrix
284         */

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         /**
301         * Returns the multiplication of this matrix by a scalar.
302         * @param x a double
303         * @return a complex matrix
304         */

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 // SCALAR DIVISON
320

321         /**
322         * Returns the division of this matrix by a scalar.
323         */

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 JavaDoc("Member class not recognised by this method.");
331         }
332         /**
333         * Returns the division of this matrix by a scalar.
334         * @param z a complex number
335         * @return a complex matrix
336         */

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         /**
347         * Returns the division of this matrix by a scalar.
348         * @param x a double
349         * @return a complex matrix
350         */

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 // SCALAR PRODUCT
366

367         /**
368         * Returns the scalar product of this matrix and another.
369         * @param m a complex matrix.
370         * @exception MatrixDimensionException If the matrices are different sizes.
371         */

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 // MATRIX MULTIPLICATION
390

391         /**
392         * Returns the multiplication of a vector by this matrix.
393         * @param v a complex vector
394         * @exception DimensionException If the matrix and vector are incompatible.
395         */

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         /**
416         * Returns the multiplication of this matrix and another.
417         */

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 JavaDoc("Matrix class not recognised by this method.");
423         }
424         /**
425         * Returns the multiplication of this matrix and another.
426         * @param m a complex matrix
427         * @return an AbstractComplexMatrix or an AbstractComplexSquareMatrix as appropriate
428         * @exception MatrixDimensionException If the matrices are incompatible.
429         */

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 // DIRECT SUM
457

458         /**
459         * Returns the direct sum of this matrix and another.
460         */

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 // TENSOR PRODUCT
480

481         /**
482         * Returns the tensor product of this matrix and another.
483         */

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 // HERMITIAN ADJOINT
502

503         /**
504         * Returns the hermitian adjoint of this matrix.
505         * @return a complex matrix
506         */

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 // CONJUGATE
522

523         /**
524         * Returns the complex conjugate of this matrix.
525         * @return a complex matrix
526         */

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 // TRANSPOSE
542

543         /**
544         * Returns the transpose of this matrix.
545         * @return a complex matrix
546         */

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 // MAP ELEMENTS
562

563         /**
564         * Applies a function on all the matrix elements.
565         * @param f a user-defined function
566         * @return a complex matrix
567         */

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