Source Code Cross Referenced for RealMatrixImplTest.java in  » Science » Apache-commons-math-1.1 » org » apache » commons » math » linear » 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 » Apache commons math 1.1 » org.apache.commons.math.linear 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-2005 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.math.linear;
017:
018:        import junit.framework.Test;
019:        import junit.framework.TestCase;
020:        import junit.framework.TestSuite;
021:
022:        /**
023:         * Test cases for the {@link RealMatrixImpl} class.
024:         *
025:         * @version $Revision: 240244 $ $Date: 2005-08-26 06:40:32 -0700 (Fri, 26 Aug 2005) $
026:         */
027:
028:        public final class RealMatrixImplTest extends TestCase {
029:
030:            // 3 x 3 identity matrix
031:            protected double[][] id = { { 1d, 0d, 0d }, { 0d, 1d, 0d },
032:                    { 0d, 0d, 1d } };
033:
034:            // Test data for group operations
035:            protected double[][] testData = { { 1d, 2d, 3d }, { 2d, 5d, 3d },
036:                    { 1d, 0d, 8d } };
037:            protected double[][] testDataLU = { { 2d, 5d, 3d },
038:                    { .5d, -2.5d, 6.5d }, { 0.5d, 0.2d, .2d } };
039:            protected double[][] testDataPlus2 = { { 3d, 4d, 5d },
040:                    { 4d, 7d, 5d }, { 3d, 2d, 10d } };
041:            protected double[][] testDataMinus = { { -1d, -2d, -3d },
042:                    { -2d, -5d, -3d }, { -1d, 0d, -8d } };
043:            protected double[] testDataRow1 = { 1d, 2d, 3d };
044:            protected double[] testDataCol3 = { 3d, 3d, 8d };
045:            protected double[][] testDataInv = { { -40d, 16d, 9d },
046:                    { 13d, -5d, -3d }, { 5d, -2d, -1d } };
047:            protected double[] preMultTest = { 8, 12, 33 };
048:            protected double[][] testData2 = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
049:            protected double[][] testData2T = { { 1d, 2d }, { 2d, 5d },
050:                    { 3d, 3d } };
051:            protected double[][] testDataPlusInv = { { -39d, 18d, 12d },
052:                    { 15d, 0d, 0d }, { 6d, -2d, 7d } };
053:
054:            // lu decomposition tests
055:            protected double[][] luData = { { 2d, 3d, 3d }, { 0d, 5d, 7d },
056:                    { 6d, 9d, 8d } };
057:            protected double[][] luDataLUDecomposition = { { 6d, 9d, 8d },
058:                    { 0d, 5d, 7d }, { 0.33333333333333, 0d, 0.33333333333333 } };
059:
060:            // singular matrices
061:            protected double[][] singular = { { 2d, 3d }, { 2d, 3d } };
062:            protected double[][] bigSingular = { { 1d, 2d, 3d, 4d },
063:                    { 2d, 5d, 3d, 4d }, { 7d, 3d, 256d, 1930d },
064:                    { 3d, 7d, 6d, 8d } }; // 4th row = 1st + 2nd
065:            protected double[][] detData = { { 1d, 2d, 3d }, { 4d, 5d, 6d },
066:                    { 7d, 8d, 10d } };
067:            protected double[][] detData2 = { { 1d, 3d }, { 2d, 4d } };
068:
069:            // vectors
070:            protected double[] testVector = { 1, 2, 3 };
071:            protected double[] testVector2 = { 1, 2, 3, 4 };
072:
073:            // submatrix accessor tests
074:            protected double[][] subTestData = { { 1, 2, 3, 4 },
075:                    { 1.5, 2.5, 3.5, 4.5 }, { 2, 4, 6, 8 }, { 4, 5, 6, 7 } };
076:            // array selections
077:            protected double[][] subRows02Cols13 = { { 2, 4 }, { 4, 8 } };
078:            protected double[][] subRows03Cols12 = { { 2, 3 }, { 5, 6 } };
079:            protected double[][] subRows03Cols123 = { { 2, 3, 4 }, { 5, 6, 7 } };
080:            // effective permutations
081:            protected double[][] subRows20Cols123 = { { 4, 6, 8 }, { 2, 3, 4 } };
082:            protected double[][] subRows31Cols31 = { { 7, 5 }, { 4.5, 2.5 } };
083:            // contiguous ranges
084:            protected double[][] subRows01Cols23 = { { 3, 4 }, { 3.5, 4.5 } };
085:            protected double[][] subRows23Cols00 = { { 2 }, { 4 } };
086:            protected double[][] subRows00Cols33 = { { 4 } };
087:            // row matrices
088:            protected double[][] subRow0 = { { 1, 2, 3, 4 } };
089:            protected double[][] subRow3 = { { 4, 5, 6, 7 } };
090:            // column matrices
091:            protected double[][] subColumn1 = { { 2 }, { 2.5 }, { 4 }, { 5 } };
092:            protected double[][] subColumn3 = { { 4 }, { 4.5 }, { 8 }, { 7 } };
093:
094:            // tolerances
095:            protected double entryTolerance = 10E-16;
096:            protected double normTolerance = 10E-14;
097:
098:            public RealMatrixImplTest(String name) {
099:                super (name);
100:            }
101:
102:            public void setUp() {
103:
104:            }
105:
106:            public static Test suite() {
107:                TestSuite suite = new TestSuite(RealMatrixImplTest.class);
108:                suite.setName("RealMatrixImpl Tests");
109:                return suite;
110:            }
111:
112:            /** test dimensions */
113:            public void testDimensions() {
114:                RealMatrixImpl m = new RealMatrixImpl(testData);
115:                RealMatrixImpl m2 = new RealMatrixImpl(testData2);
116:                assertEquals("testData row dimension", 3, m.getRowDimension());
117:                assertEquals("testData column dimension", 3, m
118:                        .getColumnDimension());
119:                assertTrue("testData is square", m.isSquare());
120:                assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
121:                assertEquals("testData2 column dimension", m2
122:                        .getColumnDimension(), 3);
123:                assertTrue("testData2 is not square", !m2.isSquare());
124:            }
125:
126:            /** test copy functions */
127:            public void testCopyFunctions() {
128:                RealMatrixImpl m = new RealMatrixImpl(testData);
129:                RealMatrixImpl m2 = new RealMatrixImpl(m.getData());
130:                assertEquals(m2, m);
131:            }
132:
133:            /** test add */
134:            public void testAdd() {
135:                RealMatrixImpl m = new RealMatrixImpl(testData);
136:                RealMatrixImpl mInv = new RealMatrixImpl(testDataInv);
137:                RealMatrixImpl mPlusMInv = (RealMatrixImpl) m.add(mInv);
138:                double[][] sumEntries = mPlusMInv.getData();
139:                for (int row = 0; row < m.getRowDimension(); row++) {
140:                    for (int col = 0; col < m.getColumnDimension(); col++) {
141:                        assertEquals("sum entry entry",
142:                                testDataPlusInv[row][col],
143:                                sumEntries[row][col], entryTolerance);
144:                    }
145:                }
146:            }
147:
148:            /** test add failure */
149:            public void testAddFail() {
150:                RealMatrixImpl m = new RealMatrixImpl(testData);
151:                RealMatrixImpl m2 = new RealMatrixImpl(testData2);
152:                try {
153:                    RealMatrixImpl mPlusMInv = (RealMatrixImpl) m.add(m2);
154:                    fail("IllegalArgumentException expected");
155:                } catch (IllegalArgumentException ex) {
156:                    ;
157:                }
158:            }
159:
160:            /** test norm */
161:            public void testNorm() {
162:                RealMatrixImpl m = new RealMatrixImpl(testData);
163:                RealMatrixImpl m2 = new RealMatrixImpl(testData2);
164:                assertEquals("testData norm", 14d, m.getNorm(), entryTolerance);
165:                assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance);
166:            }
167:
168:            /** test m-n = m + -n */
169:            public void testPlusMinus() {
170:                RealMatrixImpl m = new RealMatrixImpl(testData);
171:                RealMatrixImpl m2 = new RealMatrixImpl(testDataInv);
172:                assertClose("m-n = m + -n", m.subtract(m2), m2.scalarMultiply(
173:                        -1d).add(m), entryTolerance);
174:                try {
175:                    RealMatrix a = m.subtract(new RealMatrixImpl(testData2));
176:                    fail("Expecting illegalArgumentException");
177:                } catch (IllegalArgumentException ex) {
178:                    ;
179:                }
180:            }
181:
182:            /** test multiply */
183:            public void testMultiply() {
184:                RealMatrixImpl m = new RealMatrixImpl(testData);
185:                RealMatrixImpl mInv = new RealMatrixImpl(testDataInv);
186:                RealMatrixImpl identity = new RealMatrixImpl(id);
187:                RealMatrixImpl m2 = new RealMatrixImpl(testData2);
188:                assertClose("inverse multiply", m.multiply(mInv), identity,
189:                        entryTolerance);
190:                assertClose("inverse multiply", mInv.multiply(m), identity,
191:                        entryTolerance);
192:                assertClose("identity multiply", m.multiply(identity), m,
193:                        entryTolerance);
194:                assertClose("identity multiply", identity.multiply(mInv), mInv,
195:                        entryTolerance);
196:                assertClose("identity multiply", m2.multiply(identity), m2,
197:                        entryTolerance);
198:                try {
199:                    RealMatrix a = m.multiply(new RealMatrixImpl(bigSingular));
200:                    fail("Expecting illegalArgumentException");
201:                } catch (IllegalArgumentException ex) {
202:                    ;
203:                }
204:            }
205:
206:            //Additional Test for RealMatrixImplTest.testMultiply
207:
208:            private double[][] d3 = new double[][] { { 1, 2, 3, 4 },
209:                    { 5, 6, 7, 8 } };
210:            private double[][] d4 = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
211:            private double[][] d5 = new double[][] { { 30 }, { 70 } };
212:
213:            public void testMultiply2() {
214:                RealMatrix m3 = new RealMatrixImpl(d3);
215:                RealMatrix m4 = new RealMatrixImpl(d4);
216:                RealMatrix m5 = new RealMatrixImpl(d5);
217:                assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
218:            }
219:
220:            /** test isSingular */
221:            public void testIsSingular() {
222:                RealMatrixImpl m = new RealMatrixImpl(singular);
223:                assertTrue("singular", m.isSingular());
224:                m = new RealMatrixImpl(bigSingular);
225:                assertTrue("big singular", m.isSingular());
226:                m = new RealMatrixImpl(id);
227:                assertTrue("identity nonsingular", !m.isSingular());
228:                m = new RealMatrixImpl(testData);
229:                assertTrue("testData nonsingular", !m.isSingular());
230:            }
231:
232:            /** test inverse */
233:            public void testInverse() {
234:                RealMatrixImpl m = new RealMatrixImpl(testData);
235:                RealMatrix mInv = new RealMatrixImpl(testDataInv);
236:                assertClose("inverse", mInv, m.inverse(), normTolerance);
237:                assertClose("inverse^2", m, m.inverse().inverse(), 10E-12);
238:
239:                // Not square
240:                m = new RealMatrixImpl(testData2);
241:                try {
242:                    m.inverse();
243:                    fail("Expecting InvalidMatrixException");
244:                } catch (InvalidMatrixException ex) {
245:                    // expected
246:                }
247:
248:                // Singular
249:                m = new RealMatrixImpl(singular);
250:                try {
251:                    m.inverse();
252:                    fail("Expecting InvalidMatrixException");
253:                } catch (InvalidMatrixException ex) {
254:                    // expected
255:                }
256:            }
257:
258:            /** test solve */
259:            public void testSolve() {
260:                RealMatrixImpl m = new RealMatrixImpl(testData);
261:                RealMatrix mInv = new RealMatrixImpl(testDataInv);
262:                // being a bit slothful here -- actually testing that X = A^-1 * B
263:                assertClose("inverse-operate", mInv.operate(testVector), m
264:                        .solve(testVector), normTolerance);
265:                try {
266:                    double[] x = m.solve(testVector2);
267:                    fail("expecting IllegalArgumentException");
268:                } catch (IllegalArgumentException ex) {
269:                    ;
270:                }
271:                RealMatrix bs = new RealMatrixImpl(bigSingular);
272:                try {
273:                    RealMatrix a = bs.solve(bs);
274:                    fail("Expecting InvalidMatrixException");
275:                } catch (InvalidMatrixException ex) {
276:                    ;
277:                }
278:                try {
279:                    RealMatrix a = m.solve(bs);
280:                    fail("Expecting IllegalArgumentException");
281:                } catch (IllegalArgumentException ex) {
282:                    ;
283:                }
284:                try {
285:                    RealMatrix a = (new RealMatrixImpl(testData2)).solve(bs);
286:                    fail("Expecting illegalArgumentException");
287:                } catch (IllegalArgumentException ex) {
288:                    ;
289:                }
290:                try {
291:                    (new RealMatrixImpl(testData2)).luDecompose();
292:                    fail("Expecting InvalidMatrixException");
293:                } catch (InvalidMatrixException ex) {
294:                    ;
295:                }
296:            }
297:
298:            /** test determinant */
299:            public void testDeterminant() {
300:                RealMatrix m = new RealMatrixImpl(bigSingular);
301:                assertEquals("singular determinant", 0, m.getDeterminant(), 0);
302:                m = new RealMatrixImpl(detData);
303:                assertEquals("nonsingular test", -3d, m.getDeterminant(),
304:                        normTolerance);
305:
306:                // Examples verified against R (version 1.8.1, Red Hat Linux 9)
307:                m = new RealMatrixImpl(detData2);
308:                assertEquals("nonsingular R test 1", -2d, m.getDeterminant(),
309:                        normTolerance);
310:                m = new RealMatrixImpl(testData);
311:                assertEquals("nonsingular  R test 2", -1d, m.getDeterminant(),
312:                        normTolerance);
313:
314:                try {
315:                    double a = new RealMatrixImpl(testData2).getDeterminant();
316:                    fail("Expecting InvalidMatrixException");
317:                } catch (InvalidMatrixException ex) {
318:                    ;
319:                }
320:            }
321:
322:            /** test trace */
323:            public void testTrace() {
324:                RealMatrix m = new RealMatrixImpl(id);
325:                assertEquals("identity trace", 3d, m.getTrace(), entryTolerance);
326:                m = new RealMatrixImpl(testData2);
327:                try {
328:                    double x = m.getTrace();
329:                    fail("Expecting illegalArgumentException");
330:                } catch (IllegalArgumentException ex) {
331:                    ;
332:                }
333:            }
334:
335:            /** test sclarAdd */
336:            public void testScalarAdd() {
337:                RealMatrix m = new RealMatrixImpl(testData);
338:                assertClose("scalar add", new RealMatrixImpl(testDataPlus2), m
339:                        .scalarAdd(2d), entryTolerance);
340:            }
341:
342:            /** test operate */
343:            public void testOperate() {
344:                RealMatrix m = new RealMatrixImpl(id);
345:                double[] x = m.operate(testVector);
346:                assertClose("identity operate", testVector, x, entryTolerance);
347:                m = new RealMatrixImpl(bigSingular);
348:                try {
349:                    x = m.operate(testVector);
350:                    fail("Expecting illegalArgumentException");
351:                } catch (IllegalArgumentException ex) {
352:                    ;
353:                }
354:            }
355:
356:            /** test transpose */
357:            public void testTranspose() {
358:                RealMatrix m = new RealMatrixImpl(testData);
359:                assertClose("inverse-transpose", m.inverse().transpose(), m
360:                        .transpose().inverse(), normTolerance);
361:                m = new RealMatrixImpl(testData2);
362:                RealMatrix mt = new RealMatrixImpl(testData2T);
363:                assertClose("transpose", mt, m.transpose(), normTolerance);
364:            }
365:
366:            /** test preMultiply by vector */
367:            public void testPremultiplyVector() {
368:                RealMatrix m = new RealMatrixImpl(testData);
369:                assertClose("premultiply", m.preMultiply(testVector),
370:                        preMultTest, normTolerance);
371:                m = new RealMatrixImpl(bigSingular);
372:                try {
373:                    m.preMultiply(testVector);
374:                    fail("expecting IllegalArgumentException");
375:                } catch (IllegalArgumentException ex) {
376:                    ;
377:                }
378:            }
379:
380:            public void testPremultiply() {
381:                RealMatrix m3 = new RealMatrixImpl(d3);
382:                RealMatrix m4 = new RealMatrixImpl(d4);
383:                RealMatrix m5 = new RealMatrixImpl(d5);
384:                assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);
385:
386:                RealMatrixImpl m = new RealMatrixImpl(testData);
387:                RealMatrixImpl mInv = new RealMatrixImpl(testDataInv);
388:                RealMatrixImpl identity = new RealMatrixImpl(id);
389:                RealMatrixImpl m2 = new RealMatrixImpl(testData2);
390:                assertClose("inverse multiply", m.preMultiply(mInv), identity,
391:                        entryTolerance);
392:                assertClose("inverse multiply", mInv.preMultiply(m), identity,
393:                        entryTolerance);
394:                assertClose("identity multiply", m.preMultiply(identity), m,
395:                        entryTolerance);
396:                assertClose("identity multiply", identity.preMultiply(mInv),
397:                        mInv, entryTolerance);
398:                try {
399:                    RealMatrix a = m
400:                            .preMultiply(new RealMatrixImpl(bigSingular));
401:                    fail("Expecting illegalArgumentException");
402:                } catch (IllegalArgumentException ex) {
403:                    ;
404:                }
405:            }
406:
407:            public void testGetVectors() {
408:                RealMatrix m = new RealMatrixImpl(testData);
409:                assertClose("get row", m.getRow(0), testDataRow1,
410:                        entryTolerance);
411:                assertClose("get col", m.getColumn(2), testDataCol3,
412:                        entryTolerance);
413:                try {
414:                    double[] x = m.getRow(10);
415:                    fail("expecting MatrixIndexException");
416:                } catch (MatrixIndexException ex) {
417:                    ;
418:                }
419:                try {
420:                    double[] x = m.getColumn(-1);
421:                    fail("expecting MatrixIndexException");
422:                } catch (MatrixIndexException ex) {
423:                    ;
424:                }
425:            }
426:
427:            public void testGetEntry() {
428:                RealMatrix m = new RealMatrixImpl(testData);
429:                assertEquals("get entry", m.getEntry(0, 1), 2d, entryTolerance);
430:                try {
431:                    m.getEntry(10, 4);
432:                    fail("Expecting MatrixIndexException");
433:                } catch (MatrixIndexException ex) {
434:                    // expected
435:                }
436:            }
437:
438:            public void testLUDecomposition() throws Exception {
439:                RealMatrixImpl m = new RealMatrixImpl(testData);
440:                RealMatrix lu = m.getLUMatrix();
441:                assertClose("LU decomposition", lu,
442:                        (RealMatrix) new RealMatrixImpl(testDataLU),
443:                        normTolerance);
444:                verifyDecomposition(m, lu);
445:                // access LU decomposition on same object to verify caching.
446:                lu = m.getLUMatrix();
447:                assertClose("LU decomposition", lu,
448:                        (RealMatrix) new RealMatrixImpl(testDataLU),
449:                        normTolerance);
450:                verifyDecomposition(m, lu);
451:
452:                m = new RealMatrixImpl(luData);
453:                lu = m.getLUMatrix();
454:                assertClose("LU decomposition", lu,
455:                        (RealMatrix) new RealMatrixImpl(luDataLUDecomposition),
456:                        normTolerance);
457:                verifyDecomposition(m, lu);
458:                m = new RealMatrixImpl(testDataMinus);
459:                lu = m.getLUMatrix();
460:                verifyDecomposition(m, lu);
461:                m = new RealMatrixImpl(id);
462:                lu = m.getLUMatrix();
463:                verifyDecomposition(m, lu);
464:                try {
465:                    m = new RealMatrixImpl(bigSingular); // singular
466:                    lu = m.getLUMatrix();
467:                    fail("Expecting InvalidMatrixException");
468:                } catch (InvalidMatrixException ex) {
469:                    // expected
470:                }
471:                try {
472:                    m = new RealMatrixImpl(testData2); // not square
473:                    lu = m.getLUMatrix();
474:                    fail("Expecting InvalidMatrixException");
475:                } catch (InvalidMatrixException ex) {
476:                    // expected
477:                }
478:            }
479:
480:            /** test examples in user guide */
481:            public void testExamples() {
482:                // Create a real matrix with two rows and three columns
483:                double[][] matrixData = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
484:                RealMatrix m = new RealMatrixImpl(matrixData);
485:                // One more with three rows, two columns
486:                double[][] matrixData2 = { { 1d, 2d }, { 2d, 5d }, { 1d, 7d } };
487:                RealMatrix n = new RealMatrixImpl(matrixData2);
488:                // Now multiply m by n
489:                RealMatrix p = m.multiply(n);
490:                assertEquals(2, p.getRowDimension());
491:                assertEquals(2, p.getColumnDimension());
492:                // Invert p
493:                RealMatrix pInverse = p.inverse();
494:                assertEquals(2, pInverse.getRowDimension());
495:                assertEquals(2, pInverse.getColumnDimension());
496:
497:                // Solve example
498:                double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 },
499:                        { 4, -3, -5 } };
500:                RealMatrix coefficients = new RealMatrixImpl(coefficientsData);
501:                double[] constants = { 1, -2, 1 };
502:                double[] solution = coefficients.solve(constants);
503:                assertEquals(2 * solution[0] + 3 * solution[1] - 2
504:                        * solution[2], constants[0], 1E-12);
505:                assertEquals(-1 * solution[0] + 7 * solution[1] + 6
506:                        * solution[2], constants[1], 1E-12);
507:                assertEquals(4 * solution[0] - 3 * solution[1] - 5
508:                        * solution[2], constants[2], 1E-12);
509:
510:            }
511:
512:            // test submatrix accessors
513:            public void testSubMatrix() {
514:                RealMatrix m = new RealMatrixImpl(subTestData);
515:                RealMatrix mRows23Cols00 = new RealMatrixImpl(subRows23Cols00);
516:                RealMatrix mRows00Cols33 = new RealMatrixImpl(subRows00Cols33);
517:                RealMatrix mRows01Cols23 = new RealMatrixImpl(subRows01Cols23);
518:                RealMatrix mRows02Cols13 = new RealMatrixImpl(subRows02Cols13);
519:                RealMatrix mRows03Cols12 = new RealMatrixImpl(subRows03Cols12);
520:                RealMatrix mRows03Cols123 = new RealMatrixImpl(subRows03Cols123);
521:                RealMatrix mRows20Cols123 = new RealMatrixImpl(subRows20Cols123);
522:                RealMatrix mRows31Cols31 = new RealMatrixImpl(subRows31Cols31);
523:                assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2,
524:                        3, 0, 0));
525:                assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0,
526:                        0, 3, 3));
527:                assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0,
528:                        1, 2, 3));
529:                assertEquals("Rows02Cols13", mRows02Cols13, m.getSubMatrix(
530:                        new int[] { 0, 2 }, new int[] { 1, 3 }));
531:                assertEquals("Rows03Cols12", mRows03Cols12, m.getSubMatrix(
532:                        new int[] { 0, 3 }, new int[] { 1, 2 }));
533:                assertEquals("Rows03Cols123", mRows03Cols123, m.getSubMatrix(
534:                        new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
535:                assertEquals("Rows20Cols123", mRows20Cols123, m.getSubMatrix(
536:                        new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
537:                assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(
538:                        new int[] { 3, 1 }, new int[] { 3, 1 }));
539:                assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(
540:                        new int[] { 3, 1 }, new int[] { 3, 1 }));
541:
542:                try {
543:                    m.getSubMatrix(1, 0, 2, 4);
544:                    fail("Expecting MatrixIndexException");
545:                } catch (MatrixIndexException ex) {
546:                    // expected
547:                }
548:                try {
549:                    m.getSubMatrix(-1, 1, 2, 2);
550:                    fail("Expecting MatrixIndexException");
551:                } catch (MatrixIndexException ex) {
552:                    // expected
553:                }
554:                try {
555:                    m.getSubMatrix(1, 0, 2, 2);
556:                    fail("Expecting MatrixIndexException");
557:                } catch (MatrixIndexException ex) {
558:                    // expected
559:                }
560:                try {
561:                    m.getSubMatrix(1, 0, 2, 4);
562:                    fail("Expecting MatrixIndexException");
563:                } catch (MatrixIndexException ex) {
564:                    // expected
565:                }
566:                try {
567:                    m.getSubMatrix(new int[] {}, new int[] { 0 });
568:                    fail("Expecting MatrixIndexException");
569:                } catch (MatrixIndexException ex) {
570:                    // expected
571:                }
572:                try {
573:                    m.getSubMatrix(new int[] { 0 }, new int[] { 4 });
574:                    fail("Expecting MatrixIndexException");
575:                } catch (MatrixIndexException ex) {
576:                    // expected
577:                }
578:            }
579:
580:            public void testGetRowMatrix() {
581:                RealMatrix m = new RealMatrixImpl(subTestData);
582:                RealMatrix mRow0 = new RealMatrixImpl(subRow0);
583:                RealMatrix mRow3 = new RealMatrixImpl(subRow3);
584:                assertEquals("Row0", mRow0, m.getRowMatrix(0));
585:                assertEquals("Row3", mRow3, m.getRowMatrix(3));
586:                try {
587:                    m.getRowMatrix(-1);
588:                    fail("Expecting MatrixIndexException");
589:                } catch (MatrixIndexException ex) {
590:                    // expected
591:                }
592:                try {
593:                    m.getRowMatrix(4);
594:                    fail("Expecting MatrixIndexException");
595:                } catch (MatrixIndexException ex) {
596:                    // expected
597:                }
598:            }
599:
600:            public void testGetColumnMatrix() {
601:                RealMatrix m = new RealMatrixImpl(subTestData);
602:                RealMatrix mColumn1 = new RealMatrixImpl(subColumn1);
603:                RealMatrix mColumn3 = new RealMatrixImpl(subColumn3);
604:                assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
605:                assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
606:                try {
607:                    m.getColumnMatrix(-1);
608:                    fail("Expecting MatrixIndexException");
609:                } catch (MatrixIndexException ex) {
610:                    // expected
611:                }
612:                try {
613:                    m.getColumnMatrix(4);
614:                    fail("Expecting MatrixIndexException");
615:                } catch (MatrixIndexException ex) {
616:                    // expected
617:                }
618:            }
619:
620:            public void testEqualsAndHashCode() {
621:                RealMatrixImpl m = new RealMatrixImpl(testData);
622:                RealMatrixImpl m1 = (RealMatrixImpl) m.copy();
623:                RealMatrixImpl mt = (RealMatrixImpl) m.transpose();
624:                assertTrue(m.hashCode() != mt.hashCode());
625:                assertEquals(m.hashCode(), m1.hashCode());
626:                assertEquals(m, m);
627:                assertEquals(m, m1);
628:                assertFalse(m.equals(null));
629:                assertFalse(m.equals(mt));
630:                assertFalse(m.equals(new RealMatrixImpl(bigSingular)));
631:            }
632:
633:            public void testToString() {
634:                RealMatrixImpl m = new RealMatrixImpl(testData);
635:                assertEquals(
636:                        "RealMatrixImpl{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
637:                        m.toString());
638:                m = new RealMatrixImpl();
639:                assertEquals("RealMatrixImpl{}", m.toString());
640:            }
641:
642:            public void testSetSubMatrix() throws Exception {
643:                RealMatrixImpl m = new RealMatrixImpl(testData);
644:                m.setSubMatrix(detData2, 1, 1);
645:                RealMatrix expected = MatrixUtils
646:                        .createRealMatrix(new double[][] { { 1.0, 2.0, 3.0 },
647:                                { 2.0, 1.0, 3.0 }, { 1.0, 2.0, 4.0 } });
648:                assertEquals(expected, m);
649:
650:                m.setSubMatrix(detData2, 0, 0);
651:                expected = MatrixUtils
652:                        .createRealMatrix(new double[][] { { 1.0, 3.0, 3.0 },
653:                                { 2.0, 4.0, 3.0 }, { 1.0, 2.0, 4.0 } });
654:                assertEquals(expected, m);
655:
656:                m.setSubMatrix(testDataPlus2, 0, 0);
657:                expected = MatrixUtils.createRealMatrix(new double[][] {
658:                        { 3.0, 4.0, 5.0 }, { 4.0, 7.0, 5.0 },
659:                        { 3.0, 2.0, 10.0 } });
660:                assertEquals(expected, m);
661:
662:                // javadoc example
663:                RealMatrixImpl matrix = (RealMatrixImpl) MatrixUtils
664:                        .createRealMatrix(new double[][] { { 1, 2, 3, 4 },
665:                                { 5, 6, 7, 8 }, { 9, 0, 1, 2 } });
666:                matrix
667:                        .setSubMatrix(new double[][] { { 3, 4 }, { 5, 6 } }, 1,
668:                                1);
669:                expected = MatrixUtils.createRealMatrix(new double[][] {
670:                        { 1, 2, 3, 4 }, { 5, 3, 4, 8 }, { 9, 5, 6, 2 } });
671:                assertEquals(expected, matrix);
672:
673:                // dimension overflow
674:                try {
675:                    m.setSubMatrix(testData, 1, 1);
676:                    fail("expecting MatrixIndexException");
677:                } catch (MatrixIndexException e) {
678:                    // expected
679:                }
680:                // dimension underflow
681:                try {
682:                    m.setSubMatrix(testData, -1, 1);
683:                    fail("expecting MatrixIndexException");
684:                } catch (MatrixIndexException e) {
685:                    // expected
686:                }
687:                try {
688:                    m.setSubMatrix(testData, 1, -1);
689:                    fail("expecting MatrixIndexException");
690:                } catch (MatrixIndexException e) {
691:                    // expected
692:                }
693:
694:                // null
695:                try {
696:                    m.setSubMatrix(null, 1, 1);
697:                    fail("expecting NullPointerException");
698:                } catch (NullPointerException e) {
699:                    // expected
700:                }
701:                RealMatrixImpl m2 = new RealMatrixImpl();
702:                try {
703:                    m2.setSubMatrix(testData, 0, 1);
704:                    fail("expecting MatrixIndexException");
705:                } catch (MatrixIndexException e) {
706:                    // expected
707:                }
708:                try {
709:                    m2.setSubMatrix(testData, 1, 0);
710:                    fail("expecting MatrixIndexException");
711:                } catch (MatrixIndexException e) {
712:                    // expected
713:                }
714:
715:                // ragged
716:                try {
717:                    m.setSubMatrix(new double[][] { { 1 }, { 2, 3 } }, 0, 0);
718:                    fail("expecting IllegalArgumentException");
719:                } catch (IllegalArgumentException e) {
720:                    // expected
721:                }
722:
723:                // empty
724:                try {
725:                    m.setSubMatrix(new double[][] { {} }, 0, 0);
726:                    fail("expecting IllegalArgumentException");
727:                } catch (IllegalArgumentException e) {
728:                    // expected
729:                }
730:
731:            }
732:
733:            //--------------- -----------------Protected methods
734:
735:            /** verifies that two matrices are close (1-norm) */
736:            protected void assertClose(String msg, RealMatrix m, RealMatrix n,
737:                    double tolerance) {
738:                assertTrue(msg, m.subtract(n).getNorm() < tolerance);
739:            }
740:
741:            /** verifies that two vectors are close (sup norm) */
742:            protected void assertClose(String msg, double[] m, double[] n,
743:                    double tolerance) {
744:                if (m.length != n.length) {
745:                    fail("vectors not same length");
746:                }
747:                for (int i = 0; i < m.length; i++) {
748:                    assertEquals(msg + " " + i + " elements differ", m[i],
749:                            n[i], tolerance);
750:                }
751:            }
752:
753:            /** extracts the l  and u matrices from compact lu representation */
754:            protected void splitLU(RealMatrix lu, double[][] lowerData,
755:                    double[][] upperData) throws InvalidMatrixException {
756:                if (!lu.isSquare() || lowerData.length != lowerData[0].length
757:                        || upperData.length != upperData[0].length
758:                        || lowerData.length != upperData.length
759:                        || lowerData.length != lu.getRowDimension()) {
760:                    throw new InvalidMatrixException("incorrect dimensions");
761:                }
762:                int n = lu.getRowDimension();
763:                for (int i = 0; i < n; i++) {
764:                    for (int j = 0; j < n; j++) {
765:                        if (j < i) {
766:                            lowerData[i][j] = lu.getEntry(i, j);
767:                            upperData[i][j] = 0d;
768:                        } else if (i == j) {
769:                            lowerData[i][j] = 1d;
770:                            upperData[i][j] = lu.getEntry(i, j);
771:                        } else {
772:                            lowerData[i][j] = 0d;
773:                            upperData[i][j] = lu.getEntry(i, j);
774:                        }
775:                    }
776:                }
777:            }
778:
779:            /** Returns the result of applying the given row permutation to the matrix */
780:            protected RealMatrix permuteRows(RealMatrix matrix,
781:                    int[] permutation) {
782:                if (!matrix.isSquare()
783:                        || matrix.getRowDimension() != permutation.length) {
784:                    throw new IllegalArgumentException("dimension mismatch");
785:                }
786:                int n = matrix.getRowDimension();
787:                int m = matrix.getColumnDimension();
788:                double out[][] = new double[m][n];
789:                for (int i = 0; i < n; i++) {
790:                    for (int j = 0; j < m; j++) {
791:                        out[i][j] = matrix.getEntry(permutation[i], j);
792:                    }
793:                }
794:                return new RealMatrixImpl(out);
795:            }
796:
797:            /** Extracts l and u matrices from lu and verifies that matrix = l times u modulo permutation */
798:            protected void verifyDecomposition(RealMatrix matrix, RealMatrix lu)
799:                    throws Exception {
800:                int n = matrix.getRowDimension();
801:                double[][] lowerData = new double[n][n];
802:                double[][] upperData = new double[n][n];
803:                splitLU(lu, lowerData, upperData);
804:                RealMatrix lower = new RealMatrixImpl(lowerData);
805:                RealMatrix upper = new RealMatrixImpl(upperData);
806:                int[] permutation = ((RealMatrixImpl) matrix).getPermutation();
807:                RealMatrix permuted = permuteRows(matrix, permutation);
808:                assertClose("lu decomposition does not work", permuted, lower
809:                        .multiply(upper), normTolerance);
810:            }
811:
812:            /** Useful for debugging */
813:            private void dumpMatrix(RealMatrix m) {
814:                for (int i = 0; i < m.getRowDimension(); i++) {
815:                    String os = "";
816:                    for (int j = 0; j < m.getColumnDimension(); j++) {
817:                        os += m.getEntry(i, j) + " ";
818:                    }
819:                    System.out.println(os);
820:                }
821:            }
822:
823:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.