Source Code Cross Referenced for ComplexMatrix.java in  » Science » JSci » JSci » maths » matrices » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » JSci » JSci.maths.matrices 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* AUTO-GENERATED */
002:        package JSci.maths.matrices;
003:
004:        import JSci.maths.Complex;
005:        import JSci.maths.ComplexMapping;
006:        import JSci.maths.DimensionException;
007:        import JSci.maths.vectors.AbstractComplexVector;
008:        import JSci.maths.vectors.ComplexVector;
009:        import JSci.maths.groups.AbelianGroup;
010:        import JSci.maths.algebras.*;
011:        import JSci.maths.fields.*;
012:
013:        /**
014:         * The ComplexMatrix class provides an object for encapsulating matrices containing complex numbers.
015:         * @version 2.2
016:         * @author Mark Hale
017:         */
018:        public class ComplexMatrix extends AbstractComplexMatrix {
019:            /**
020:             * Arrays containing the elements of the matrix.
021:             */
022:            protected double matrixRe[][], matrixIm[][];
023:
024:            /**
025:             * Constructs an empty matrix.
026:             * @param rows the number of rows
027:             * @param cols the number of columns
028:             */
029:            public ComplexMatrix(final int rows, final int cols) {
030:                super (rows, cols);
031:                matrixRe = new double[rows][cols];
032:                matrixIm = new double[rows][cols];
033:            }
034:
035:            /**
036:             * Constructs a matrix by wrapping two arrays.
037:             * @param arrayRe an array of real values
038:             * @param arrayIm an array of imaginary values
039:             */
040:            public ComplexMatrix(final double arrayRe[][],
041:                    final double arrayIm[][]) {
042:                this (arrayRe.length, arrayRe[0].length);
043:                matrixRe = arrayRe;
044:                matrixIm = arrayIm;
045:            }
046:
047:            /**
048:             * Constructs a matrix from an array.
049:             * @param array an assigned value
050:             */
051:            public ComplexMatrix(final Complex array[][]) {
052:                this (array.length, array[0].length);
053:                for (int j, i = 0; i < numRows; i++) {
054:                    for (j = 0; j < numCols; j++) {
055:                        matrixRe[i][j] = array[i][j].real();
056:                        matrixIm[i][j] = array[i][j].imag();
057:                    }
058:                }
059:            }
060:
061:            /**
062:             * Constructs a matrix from an array of vectors (columns).
063:             * @param array an assigned value
064:             */
065:            public ComplexMatrix(ComplexVector array[]) {
066:                this (array[0].dimension(), array.length);
067:                for (int j, i = 0; i < numRows; i++) {
068:                    for (j = 0; j < numCols; j++) {
069:                        matrixRe[i][j] = array[j].getComponent(i).real();
070:                        matrixIm[i][j] = array[j].getComponent(i).imag();
071:                    }
072:                }
073:            }
074:
075:            /**
076:             * Compares two complex matrices for equality.
077:             * @param m a complex matrix
078:             */
079:            public boolean equals(AbstractComplexMatrix m, double tol) {
080:                if (m != null && numRows == m.rows() && numCols == m.columns()) {
081:                    double sumSqr = 0.0;
082:                    for (int i = 0; i < numRows; i++) {
083:                        for (int j = 0; j < numCols; j++) {
084:                            double deltaRe = matrixRe[i][j]
085:                                    - m.getRealElement(i, j);
086:                            double deltaIm = matrixIm[i][j]
087:                                    - m.getImagElement(i, j);
088:                            sumSqr += deltaRe * deltaRe + deltaIm * deltaIm;
089:                        }
090:                    }
091:                    return (sumSqr <= tol * tol);
092:                } else {
093:                    return false;
094:                }
095:            }
096:
097:            /**
098:             * Returns a string representing this matrix.
099:             */
100:            public String toString() {
101:                final StringBuffer buf = new StringBuffer(5 * numRows * numCols);
102:                for (int j, i = 0; i < numRows; i++) {
103:                    for (j = 0; j < numCols; j++) {
104:                        buf.append(Complex.toString(matrixRe[i][j],
105:                                matrixIm[i][j]));
106:                        buf.append(' ');
107:                    }
108:                    buf.append('\n');
109:                }
110:                return buf.toString();
111:            }
112:
113:            /**
114:             * Returns a hashcode for this matrix.
115:             */
116:            public int hashCode() {
117:                return (int) Math.exp(infNorm());
118:            }
119:
120:            /**
121:             * Returns the real part of this complex matrix.
122:             * @return a double matrix
123:             */
124:            public AbstractDoubleMatrix real() {
125:                return new DoubleMatrix(matrixRe);
126:            }
127:
128:            /**
129:             * Returns the imaginary part of this complex matrix.
130:             * @return a double matrix
131:             */
132:            public AbstractDoubleMatrix imag() {
133:                return new DoubleMatrix(matrixIm);
134:            }
135:
136:            /**
137:             * Returns an element of the matrix.
138:             * @param i row index of the element
139:             * @param j column index of the element
140:             * @exception MatrixDimensionException If attempting to access an invalid element.
141:             */
142:            public Complex getElement(final int i, final int j) {
143:                if (i >= 0 && i < numRows && j >= 0 && j < numCols)
144:                    return new Complex(matrixRe[i][j], matrixIm[i][j]);
145:                else
146:                    throw new MatrixDimensionException(getInvalidElementMsg(i,
147:                            j));
148:            }
149:
150:            public double getRealElement(final int i, final int j) {
151:                if (i >= 0 && i < numRows && j >= 0 && j < numCols)
152:                    return matrixRe[i][j];
153:                else
154:                    throw new MatrixDimensionException(getInvalidElementMsg(i,
155:                            j));
156:            }
157:
158:            public double getImagElement(final int i, final int j) {
159:                if (i >= 0 && i < numRows && j >= 0 && j < numCols)
160:                    return matrixIm[i][j];
161:                else
162:                    throw new MatrixDimensionException(getInvalidElementMsg(i,
163:                            j));
164:            }
165:
166:            /**
167:             * Sets the value of an element of the matrix.
168:             * Should only be used to initialise this matrix.
169:             * @param i row index of the element
170:             * @param j column index of the element
171:             * @param z a complex number
172:             * @exception MatrixDimensionException If attempting to access an invalid element.
173:             */
174:            public void setElement(final int i, final int j, final Complex z) {
175:                if (i >= 0 && i < numRows && j >= 0 && j < numCols) {
176:                    matrixRe[i][j] = z.real();
177:                    matrixIm[i][j] = z.imag();
178:                } else
179:                    throw new MatrixDimensionException(getInvalidElementMsg(i,
180:                            j));
181:            }
182:
183:            /**
184:             * Sets the value of an element of the matrix.
185:             * Should only be used to initialise this matrix.
186:             * @param i row index of the element
187:             * @param j column index of the element
188:             * @param x the real part of a complex number
189:             * @param y the imaginary part of a complex number
190:             * @exception MatrixDimensionException If attempting to access an invalid element.
191:             */
192:            public void setElement(final int i, final int j, final double x,
193:                    final double y) {
194:                if (i >= 0 && i < numRows && j >= 0 && j < numCols) {
195:                    matrixRe[i][j] = x;
196:                    matrixIm[i][j] = y;
197:                } else
198:                    throw new MatrixDimensionException(getInvalidElementMsg(i,
199:                            j));
200:            }
201:
202:            /**
203:             * Returns the l<sup><img border=0 alt="infinity" src="doc-files/infinity.gif"></sup>-norm.
204:             * @author Taber Smith
205:             */
206:            public double infNorm() {
207:                double result = 0.0, tmpResult;
208:                for (int i = 0; i < numRows; i++) {
209:                    tmpResult = 0.0;
210:                    for (int j = 0; j < numCols; j++)
211:                        tmpResult += Math
212:                                .sqrt((matrixRe[i][j] * matrixRe[i][j] + matrixIm[i][j]
213:                                        * matrixIm[i][j]));
214:                    if (tmpResult > result)
215:                        result = tmpResult;
216:                }
217:                return result;
218:            }
219:
220:            /**
221:             * Returns the Frobenius or Hilbert-Schmidt (l<sup>2</sup>) norm.
222:             * @jsci.planetmath FrobeniusMatrixNorm
223:             * @author Taber Smith
224:             */
225:            public double frobeniusNorm() {
226:                double result = 0.0;
227:                for (int j, i = 0; i < numRows; i++)
228:                    for (j = 0; j < numCols; j++)
229:                        result += matrixRe[i][j] * matrixRe[i][j]
230:                                + matrixIm[i][j] * matrixIm[i][j];
231:                return Math.sqrt(result);
232:            }
233:
234:            //============
235:            // OPERATIONS
236:            //============
237:
238:            /**
239:             * Returns the negative of this matrix.
240:             */
241:            public AbelianGroup.Member negate() {
242:                final double arrayRe[][] = new double[numRows][numCols];
243:                final double arrayIm[][] = new double[numRows][numCols];
244:                for (int j, i = 0; i < numRows; i++) {
245:                    arrayRe[i][0] = -matrixRe[i][0];
246:                    arrayIm[i][0] = -matrixIm[i][0];
247:                    for (j = 1; j < numCols; j++) {
248:                        arrayRe[i][j] = -matrixRe[i][j];
249:                        arrayIm[i][j] = -matrixIm[i][j];
250:                    }
251:                }
252:                return new ComplexMatrix(arrayRe, arrayIm);
253:            }
254:
255:            // ADDITION
256:
257:            /**
258:             * Returns the addition of this matrix and another.
259:             * @param m a complex matrix
260:             * @exception MatrixDimensionException If the matrices are different sizes.
261:             */
262:            public AbstractComplexMatrix add(final AbstractComplexMatrix m) {
263:                if (m instanceof  ComplexMatrix) {
264:                    return add((ComplexMatrix) m);
265:                } else {
266:                    if (numRows == m.rows() && numCols == m.columns()) {
267:                        final double arrayRe[][] = new double[numRows][numCols];
268:                        final double arrayIm[][] = new double[numRows][numCols];
269:                        for (int j, i = 0; i < numRows; i++) {
270:                            arrayRe[i][0] = matrixRe[i][0]
271:                                    + m.getElement(i, 0).real();
272:                            arrayIm[i][0] = matrixIm[i][0]
273:                                    + m.getElement(i, 0).imag();
274:                            for (j = 1; j < numCols; j++) {
275:                                arrayRe[i][j] = matrixRe[i][j]
276:                                        + m.getElement(i, j).real();
277:                                arrayIm[i][j] = matrixIm[i][j]
278:                                        + m.getElement(i, j).imag();
279:                            }
280:                        }
281:                        return new ComplexMatrix(arrayRe, arrayIm);
282:                    } else
283:                        throw new MatrixDimensionException(
284:                                "Matrices are different sizes.");
285:                }
286:            }
287:
288:            public ComplexMatrix add(final ComplexMatrix m) {
289:                if (numRows == m.numRows && numCols == m.numCols) {
290:                    final double arrayRe[][] = new double[numRows][numCols];
291:                    final double arrayIm[][] = new double[numRows][numCols];
292:                    for (int j, i = 0; i < numRows; i++) {
293:                        arrayRe[i][0] = matrixRe[i][0] + m.matrixRe[i][0];
294:                        arrayIm[i][0] = matrixIm[i][0] + m.matrixIm[i][0];
295:                        for (j = 1; j < numCols; j++) {
296:                            arrayRe[i][j] = matrixRe[i][j] + m.matrixRe[i][j];
297:                            arrayIm[i][j] = matrixIm[i][j] + m.matrixIm[i][j];
298:                        }
299:                    }
300:                    return new ComplexMatrix(arrayRe, arrayIm);
301:                } else
302:                    throw new MatrixDimensionException(
303:                            "Matrices are different sizes.");
304:            }
305:
306:            // SUBTRACTION
307:
308:            /**
309:             * Returns the subtraction of this matrix by another.
310:             * @param m a complex matrix
311:             * @exception MatrixDimensionException If the matrices are different sizes.
312:             */
313:            public AbstractComplexMatrix subtract(final AbstractComplexMatrix m) {
314:                if (m instanceof  ComplexMatrix) {
315:                    return subtract((ComplexMatrix) m);
316:                } else {
317:                    if (numRows == m.rows() && numCols == m.columns()) {
318:                        final double arrayRe[][] = new double[numRows][numCols];
319:                        final double arrayIm[][] = new double[numRows][numCols];
320:                        for (int j, i = 0; i < numRows; i++) {
321:                            arrayRe[i][0] = matrixRe[i][0]
322:                                    - m.getElement(i, 0).real();
323:                            arrayIm[i][0] = matrixIm[i][0]
324:                                    - m.getElement(i, 0).imag();
325:                            for (j = 1; j < numCols; j++) {
326:                                arrayRe[i][j] = matrixRe[i][j]
327:                                        - m.getElement(i, j).real();
328:                                arrayIm[i][j] = matrixIm[i][j]
329:                                        - m.getElement(i, j).imag();
330:                            }
331:                        }
332:                        return new ComplexMatrix(arrayRe, arrayIm);
333:                    } else
334:                        throw new MatrixDimensionException(
335:                                "Matrices are different sizes.");
336:                }
337:            }
338:
339:            public ComplexMatrix subtract(final ComplexMatrix m) {
340:                if (numRows == m.numRows && numCols == m.numCols) {
341:                    final double arrayRe[][] = new double[numRows][numCols];
342:                    final double arrayIm[][] = new double[numRows][numCols];
343:                    for (int j, i = 0; i < numRows; i++) {
344:                        arrayRe[i][0] = matrixRe[i][0] - m.matrixRe[i][0];
345:                        arrayIm[i][0] = matrixIm[i][0] - m.matrixIm[i][0];
346:                        for (j = 1; j < numCols; j++) {
347:                            arrayRe[i][j] = matrixRe[i][j] - m.matrixRe[i][j];
348:                            arrayIm[i][j] = matrixIm[i][j] - m.matrixIm[i][j];
349:                        }
350:                    }
351:                    return new ComplexMatrix(arrayRe, arrayIm);
352:                } else
353:                    throw new MatrixDimensionException(
354:                            "Matrices are different sizes.");
355:            }
356:
357:            // SCALAR MULTIPLICATION
358:
359:            /**
360:             * Returns the multiplication of this matrix by a scalar.
361:             * @param z a complex number
362:             * @return a complex matrix
363:             */
364:            public AbstractComplexMatrix scalarMultiply(final Complex z) {
365:                final double real = z.real();
366:                final double imag = z.imag();
367:                final double arrayRe[][] = new double[numRows][numCols];
368:                final double arrayIm[][] = new double[numRows][numCols];
369:                for (int j, i = 0; i < numRows; i++) {
370:                    arrayRe[i][0] = real * matrixRe[i][0] - imag
371:                            * matrixIm[i][0];
372:                    arrayIm[i][0] = imag * matrixRe[i][0] + real
373:                            * matrixIm[i][0];
374:                    for (j = 1; j < numCols; j++) {
375:                        arrayRe[i][j] = real * matrixRe[i][j] - imag
376:                                * matrixIm[i][j];
377:                        arrayIm[i][j] = imag * matrixRe[i][j] + real
378:                                * matrixIm[i][j];
379:                    }
380:                }
381:                return new ComplexMatrix(arrayRe, arrayIm);
382:            }
383:
384:            /**
385:             * Returns the multiplication of this matrix by a scalar.
386:             * @param x a double
387:             * @return a complex matrix
388:             */
389:            public AbstractComplexMatrix scalarMultiply(final double x) {
390:                final double arrayRe[][] = new double[numRows][numCols];
391:                final double arrayIm[][] = new double[numRows][numCols];
392:                for (int j, i = 0; i < numRows; i++) {
393:                    arrayRe[i][0] = x * matrixRe[i][0];
394:                    arrayIm[i][0] = x * matrixIm[i][0];
395:                    for (j = 1; j < numCols; j++) {
396:                        arrayRe[i][j] = x * matrixRe[i][j];
397:                        arrayIm[i][j] = x * matrixIm[i][j];
398:                    }
399:                }
400:                return new ComplexMatrix(arrayRe, arrayIm);
401:            }
402:
403:            // SCALAR DIVISON
404:
405:            /**
406:             * Returns the division of this matrix by a scalar.
407:             * @param z a complex number
408:             * @return a complex matrix
409:             */
410:            public AbstractComplexMatrix scalarDivide(final Complex z) {
411:                final Complex array[][] = new Complex[numRows][numCols];
412:                for (int j, i = 0; i < numRows; i++) {
413:                    array[i][0] = new Complex(matrixRe[i][0], matrixIm[i][0])
414:                            .divide(z);
415:                    for (j = 1; j < numCols; j++)
416:                        array[i][j] = new Complex(matrixRe[i][j],
417:                                matrixIm[i][j]).divide(z);
418:                }
419:                return new ComplexMatrix(array);
420:            }
421:
422:            /**
423:             * Returns the division of this matrix by a scalar.
424:             * @param x a double
425:             * @return a complex matrix
426:             */
427:            public AbstractComplexMatrix scalarDivide(final double x) {
428:                final double arrayRe[][] = new double[numRows][numCols];
429:                final double arrayIm[][] = new double[numRows][numCols];
430:                for (int j, i = 0; i < numRows; i++) {
431:                    arrayRe[i][0] = matrixRe[i][0] / x;
432:                    arrayIm[i][0] = matrixIm[i][0] / x;
433:                    for (j = 1; j < numCols; j++) {
434:                        arrayRe[i][j] = matrixRe[i][j] / x;
435:                        arrayIm[i][j] = matrixIm[i][j] / x;
436:                    }
437:                }
438:                return new ComplexMatrix(arrayRe, arrayIm);
439:            }
440:
441:            // MATRIX MULTIPLICATION
442:
443:            /**
444:             * Returns the multiplication of a vector by this matrix.
445:             * @param v a complex vector
446:             * @exception DimensionException If the matrix and vector are incompatible.
447:             */
448:            public AbstractComplexVector multiply(final AbstractComplexVector v) {
449:                if (numCols == v.dimension()) {
450:                    final double arrayRe[] = new double[numRows];
451:                    final double arrayIm[] = new double[numRows];
452:                    Complex comp;
453:                    for (int j, i = 0; i < numRows; i++) {
454:                        comp = v.getComponent(0);
455:                        arrayRe[i] = (matrixRe[i][0] * comp.real() - matrixIm[i][0]
456:                                * comp.imag());
457:                        arrayIm[i] = (matrixIm[i][0] * comp.real() + matrixRe[i][0]
458:                                * comp.imag());
459:                        for (j = 1; j < numCols; j++) {
460:                            comp = v.getComponent(j);
461:                            arrayRe[i] += (matrixRe[i][j] * comp.real() - matrixIm[i][j]
462:                                    * comp.imag());
463:                            arrayIm[i] += (matrixIm[i][j] * comp.real() + matrixRe[i][j]
464:                                    * comp.imag());
465:                        }
466:                    }
467:                    return new ComplexVector(arrayRe, arrayIm);
468:                } else
469:                    throw new DimensionException(
470:                            "Matrix and vector are incompatible.");
471:            }
472:
473:            /**
474:             * Returns the multiplication of this matrix and another.
475:             * @param m a complex matrix
476:             * @return an AbstractComplexMatrix or an AbstractComplexSquareMatrix as appropriate
477:             * @exception MatrixDimensionException If the matrices are incompatible.
478:             */
479:            public AbstractComplexMatrix multiply(final AbstractComplexMatrix m) {
480:                if (m instanceof  ComplexMatrix) {
481:                    return multiply((ComplexMatrix) m);
482:                } else {
483:                    if (numCols == m.rows()) {
484:                        final double arrayRe[][] = new double[numRows][m
485:                                .columns()];
486:                        final double arrayIm[][] = new double[numRows][m
487:                                .columns()];
488:                        int n, k;
489:                        Complex elem;
490:                        for (int j = 0; j < numRows; j++) {
491:                            for (k = 0; k < m.columns(); k++) {
492:                                elem = m.getElement(0, k);
493:                                arrayRe[j][k] = (matrixRe[j][0] * elem.real() - matrixIm[j][0]
494:                                        * elem.imag());
495:                                arrayIm[j][k] = (matrixIm[j][0] * elem.real() + matrixRe[j][0]
496:                                        * elem.imag());
497:                                for (n = 1; n < numCols; n++) {
498:                                    elem = m.getElement(n, k);
499:                                    arrayRe[j][k] += (matrixRe[j][n]
500:                                            * elem.real() - matrixIm[j][n]
501:                                            * elem.imag());
502:                                    arrayIm[j][k] += (matrixIm[j][n]
503:                                            * elem.real() + matrixRe[j][n]
504:                                            * elem.imag());
505:                                }
506:                            }
507:                        }
508:                        if (numRows == m.columns())
509:                            return new ComplexSquareMatrix(arrayRe, arrayIm);
510:                        else
511:                            return new ComplexMatrix(arrayRe, arrayIm);
512:                    } else
513:                        throw new MatrixDimensionException(
514:                                "Incompatible matrices.");
515:                }
516:            }
517:
518:            public AbstractComplexMatrix multiply(final ComplexMatrix m) {
519:                if (numCols == m.numRows) {
520:                    final double arrayRe[][] = new double[numRows][m.numCols];
521:                    final double arrayIm[][] = new double[numRows][m.numCols];
522:                    int n, k;
523:                    for (int j = 0; j < numRows; j++) {
524:                        for (k = 0; k < m.numCols; k++) {
525:                            arrayRe[j][k] = (matrixRe[j][0] * m.matrixRe[0][k] - matrixIm[j][0]
526:                                    * m.matrixIm[0][k]);
527:                            arrayIm[j][k] = (matrixIm[j][0] * m.matrixRe[0][k] + matrixRe[j][0]
528:                                    * m.matrixIm[0][k]);
529:                            for (n = 1; n < numCols; n++) {
530:                                arrayRe[j][k] += (matrixRe[j][n]
531:                                        * m.matrixRe[n][k] - matrixIm[j][n]
532:                                        * m.matrixIm[n][k]);
533:                                arrayIm[j][k] += (matrixIm[j][n]
534:                                        * m.matrixRe[n][k] + matrixRe[j][n]
535:                                        * m.matrixIm[n][k]);
536:                            }
537:                        }
538:                    }
539:                    if (numRows == m.numCols)
540:                        return new ComplexSquareMatrix(arrayRe, arrayIm);
541:                    else
542:                        return new ComplexMatrix(arrayRe, arrayIm);
543:                } else
544:                    throw new MatrixDimensionException("Incompatible matrices.");
545:            }
546:
547:            // DIRECT SUM
548:
549:            /**
550:             * Returns the direct sum of this matrix and another.
551:             */
552:            public AbstractComplexMatrix directSum(final AbstractComplexMatrix m) {
553:                final double arrayRe[][] = new double[numRows + m.numRows][numCols
554:                        + m.numCols];
555:                final double arrayIm[][] = new double[numRows + m.numRows][numCols
556:                        + m.numCols];
557:                for (int j, i = 0; i < numRows; i++) {
558:                    for (j = 0; j < numCols; j++) {
559:                        arrayRe[i][j] = matrixRe[i][j];
560:                        arrayIm[i][j] = matrixIm[i][j];
561:                    }
562:                }
563:                for (int j, i = 0; i < m.numRows; i++) {
564:                    for (j = 0; j < m.numCols; j++) {
565:                        Complex elem = m.getElement(i, j);
566:                        arrayRe[i + numRows][j + numCols] = elem.real();
567:                        arrayIm[i + numRows][j + numCols] = elem.imag();
568:                    }
569:                }
570:                return new ComplexMatrix(arrayRe, arrayIm);
571:            }
572:
573:            // TENSOR PRODUCT
574:
575:            /**
576:             * Returns the tensor product of this matrix and another.
577:             */
578:            public AbstractComplexMatrix tensor(final AbstractComplexMatrix m) {
579:                final double arrayRe[][] = new double[numRows * m.numRows][numCols
580:                        * m.numCols];
581:                final double arrayIm[][] = new double[numRows * m.numRows][numCols
582:                        * m.numCols];
583:                for (int i = 0; i < numRows; i++) {
584:                    for (int j = 0; j < numCols; j++) {
585:                        for (int k = 0; k < m.numRows; j++) {
586:                            for (int l = 0; l < m.numCols; l++) {
587:                                Complex elem = m.getElement(k, l);
588:                                arrayRe[i * m.numRows + k][j * m.numCols + l] = (matrixRe[i][j]
589:                                        * elem.real() - matrixIm[i][j]
590:                                        * elem.imag());
591:                                arrayIm[i * m.numRows + k][j * m.numCols + l] = (matrixIm[i][j]
592:                                        * elem.real() + matrixRe[i][j]
593:                                        * elem.imag());
594:                            }
595:                        }
596:                    }
597:                }
598:                return new ComplexMatrix(arrayRe, arrayIm);
599:            }
600:
601:            // HERMITIAN ADJOINT
602:
603:            /**
604:             * Returns the hermitian adjoint of this matrix.
605:             * @return a complex matrix
606:             */
607:            public AbstractComplexMatrix hermitianAdjoint() {
608:                final double arrayRe[][] = new double[numCols][numRows];
609:                final double arrayIm[][] = new double[numCols][numRows];
610:                for (int j, i = 0; i < numRows; i++) {
611:                    arrayRe[0][i] = matrixRe[i][0];
612:                    arrayIm[0][i] = -matrixIm[i][0];
613:                    for (j = 1; j < numCols; j++) {
614:                        arrayRe[j][i] = matrixRe[i][j];
615:                        arrayIm[j][i] = -matrixIm[i][j];
616:                    }
617:                }
618:                return new ComplexMatrix(arrayRe, arrayIm);
619:            }
620:
621:            // CONJUGATE
622:
623:            /**
624:             * Returns the complex conjugate of this matrix.
625:             * @return a complex matrix
626:             */
627:            public AbstractComplexMatrix conjugate() {
628:                final double arrayIm[][] = new double[numRows][numCols];
629:                for (int j, i = 0; i < numRows; i++) {
630:                    arrayIm[i][0] = -matrixIm[i][0];
631:                    for (j = 1; j < numCols; j++)
632:                        arrayIm[i][j] = -matrixIm[i][j];
633:                }
634:                return new ComplexMatrix(matrixRe, arrayIm);
635:            }
636:
637:            // TRANSPOSE
638:
639:            /**
640:             * Returns the transpose of this matrix.
641:             * @return a complex matrix
642:             */
643:            public Matrix transpose() {
644:                final double arrayRe[][] = new double[numCols][numRows];
645:                final double arrayIm[][] = new double[numCols][numRows];
646:                for (int j, i = 0; i < numRows; i++) {
647:                    arrayRe[0][i] = matrixRe[i][0];
648:                    arrayIm[0][i] = matrixIm[i][0];
649:                    for (j = 1; j < numCols; j++) {
650:                        arrayRe[j][i] = matrixRe[i][j];
651:                        arrayIm[j][i] = matrixIm[i][j];
652:                    }
653:                }
654:                return new ComplexMatrix(arrayRe, arrayIm);
655:            }
656:
657:            // MAP ELEMENTS
658:
659:            /**
660:             * Applies a function on all the matrix elements.
661:             * @param f a user-defined function
662:             * @return a complex matrix
663:             */
664:            public AbstractComplexMatrix mapElements(final ComplexMapping f) {
665:                final Complex array[][] = new Complex[numRows][numCols];
666:                for (int j, i = 0; i < numRows; i++) {
667:                    array[i][0] = f.map(matrixRe[i][0], matrixIm[i][0]);
668:                    for (j = 1; j < numCols; j++)
669:                        array[i][j] = f.map(matrixRe[i][j], matrixIm[i][j]);
670:                }
671:                return new ComplexMatrix(array);
672:            }
673:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.