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