1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.linear;
23
24 import org.hipparchus.UnitTestUtils;
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.exception.NullArgumentException;
28 import org.hipparchus.random.RandomGenerator;
29 import org.hipparchus.random.Well1024a;
30 import org.hipparchus.util.FastMath;
31 import org.junit.Assert;
32 import org.junit.Test;
33
34 import java.util.Arrays;
35 import java.util.Random;
36
37
38
39
40
41
42 public final class BlockRealMatrixTest {
43
44
45 protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
46
47
48 protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
49 protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
50 protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
51 protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
52 {-1d,0d,-8d} };
53 protected double[] testDataRow1 = {1d,2d,3d};
54 protected double[] testDataCol3 = {3d,3d,8d};
55 protected double[][] testDataInv =
56 { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
57 protected double[] preMultTest = {8,12,33};
58 protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
59 protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
60 protected double[][] testDataPlusInv =
61 { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
62
63
64 protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
65 protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d},
66 {0.33333333333333,0d,0.33333333333333} };
67
68
69 protected double[][] singular = { {2d,3d}, {2d,3d} };
70 protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
71 {7d,3d,256d,1930d}, {3d,7d,6d,8d}};
72 protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
73 protected double[][] detData2 = { {1d, 3d}, {2d, 4d}};
74
75
76 protected double[] testVector = {1,2,3};
77 protected double[] testVector2 = {1,2,3,4};
78
79
80 protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5},
81 {2, 4, 6, 8}, {4, 5, 6, 7}};
82
83 protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}};
84 protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}};
85 protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}};
86
87 protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}};
88 protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}};
89
90 protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}};
91 protected double[][] subRows23Cols00 = {{2} , {4}};
92 protected double[][] subRows00Cols33 = {{4}};
93
94 protected double[][] subRow0 = {{1,2,3,4}};
95 protected double[][] subRow3 = {{4,5,6,7}};
96
97 protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}};
98 protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}};
99
100
101 protected double entryTolerance = 10E-16;
102 protected double normTolerance = 10E-14;
103
104
105 @Test
106 public void testDimensions() {
107 BlockRealMatrix m = new BlockRealMatrix(testData);
108 BlockRealMatrix m2 = new BlockRealMatrix(testData2);
109 Assert.assertEquals("testData row dimension",3,m.getRowDimension());
110 Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
111 Assert.assertTrue("testData is square",m.isSquare());
112 Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2);
113 Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
114 Assert.assertTrue("testData2 is not square",!m2.isSquare());
115 }
116
117
118 @Test
119 public void testCopyFunctions() {
120 Random r = new Random(66636328996002l);
121 BlockRealMatrix m1 = createRandomMatrix(r, 47, 83);
122 BlockRealMatrix m2 = new BlockRealMatrix(m1.getData());
123 Assert.assertEquals(m1, m2);
124 BlockRealMatrix m3 = new BlockRealMatrix(testData);
125 BlockRealMatrix m4 = new BlockRealMatrix(m3.getData());
126 Assert.assertEquals(m3, m4);
127 }
128
129
130 @Test
131 public void testAdd() {
132 BlockRealMatrix m = new BlockRealMatrix(testData);
133 BlockRealMatrix mInv = new BlockRealMatrix(testDataInv);
134 RealMatrix mPlusMInv = m.add(mInv);
135 double[][] sumEntries = mPlusMInv.getData();
136 for (int row = 0; row < m.getRowDimension(); row++) {
137 for (int col = 0; col < m.getColumnDimension(); col++) {
138 Assert.assertEquals("sum entry entry",
139 testDataPlusInv[row][col],sumEntries[row][col],
140 entryTolerance);
141 }
142 }
143 }
144
145
146 @Test
147 public void testAddFail() {
148 BlockRealMatrix m = new BlockRealMatrix(testData);
149 BlockRealMatrix m2 = new BlockRealMatrix(testData2);
150 try {
151 m.add(m2);
152 Assert.fail("MathIllegalArgumentException expected");
153 } catch (MathIllegalArgumentException ex) {
154
155 }
156 }
157
158
159 @Test
160 public void testNorm() {
161 BlockRealMatrix m = new BlockRealMatrix(testData);
162 BlockRealMatrix m2 = new BlockRealMatrix(testData2);
163 Assert.assertEquals("testData norm",14d,m.getNorm1(),entryTolerance);
164 Assert.assertEquals("testData2 norm",7d,m2.getNorm1(),entryTolerance);
165 Assert.assertEquals("testData norm",10d,m.getNormInfty(),entryTolerance);
166 Assert.assertEquals("testData2 norm",10d,m2.getNormInfty(),entryTolerance);
167 }
168
169
170 @Test
171 public void testFrobeniusNorm() {
172 BlockRealMatrix m = new BlockRealMatrix(testData);
173 BlockRealMatrix m2 = new BlockRealMatrix(testData2);
174 Assert.assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
175 Assert.assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
176 }
177
178
179 @Test
180 public void testPlusMinus() {
181 BlockRealMatrix m = new BlockRealMatrix(testData);
182 BlockRealMatrix m2 = new BlockRealMatrix(testDataInv);
183 assertClose(m.subtract(m2), m2.scalarMultiply(-1d).add(m), entryTolerance);
184 try {
185 m.subtract(new BlockRealMatrix(testData2));
186 Assert.fail("Expecting illegalArgumentException");
187 } catch (MathIllegalArgumentException ex) {
188
189 }
190 }
191
192
193 @Test
194 public void testMultiply() {
195 BlockRealMatrix m = new BlockRealMatrix(testData);
196 BlockRealMatrix mInv = new BlockRealMatrix(testDataInv);
197 BlockRealMatrix identity = new BlockRealMatrix(id);
198 BlockRealMatrix m2 = new BlockRealMatrix(testData2);
199 assertClose(m.multiply(mInv), identity, entryTolerance);
200 assertClose(mInv.multiply(m), identity, entryTolerance);
201 assertClose(m.multiply(identity), m, entryTolerance);
202 assertClose(identity.multiply(mInv), mInv, entryTolerance);
203 assertClose(m2.multiply(identity), m2, entryTolerance);
204 try {
205 m.multiply(new BlockRealMatrix(bigSingular));
206 Assert.fail("Expecting illegalArgumentException");
207 } catch (MathIllegalArgumentException ex) {
208
209 }
210 }
211
212 @Test
213 public void testSeveralBlocks() {
214 RealMatrix m = new BlockRealMatrix(35, 71);
215 for (int i = 0; i < m.getRowDimension(); ++i) {
216 for (int j = 0; j < m.getColumnDimension(); ++j) {
217 m.setEntry(i, j, i + j / 1024.0);
218 }
219 }
220
221 RealMatrix mT = m.transpose();
222 Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension());
223 Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension());
224 for (int i = 0; i < mT.getRowDimension(); ++i) {
225 for (int j = 0; j < mT.getColumnDimension(); ++j) {
226 Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0);
227 }
228 }
229
230 RealMatrix mPm = m.add(m);
231 for (int i = 0; i < mPm.getRowDimension(); ++i) {
232 for (int j = 0; j < mPm.getColumnDimension(); ++j) {
233 Assert.assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0);
234 }
235 }
236
237 RealMatrix mPmMm = mPm.subtract(m);
238 for (int i = 0; i < mPmMm.getRowDimension(); ++i) {
239 for (int j = 0; j < mPmMm.getColumnDimension(); ++j) {
240 Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0);
241 }
242 }
243
244 RealMatrix mTm = mT.multiply(m);
245 for (int i = 0; i < mTm.getRowDimension(); ++i) {
246 for (int j = 0; j < mTm.getColumnDimension(); ++j) {
247 double sum = 0;
248 for (int k = 0; k < mT.getColumnDimension(); ++k) {
249 sum += (k + i / 1024.0) * (k + j / 1024.0);
250 }
251 Assert.assertEquals(sum, mTm.getEntry(i, j), 0);
252 }
253 }
254
255 RealMatrix mmT = m.multiply(mT);
256 for (int i = 0; i < mmT.getRowDimension(); ++i) {
257 for (int j = 0; j < mmT.getColumnDimension(); ++j) {
258 double sum = 0;
259 for (int k = 0; k < m.getColumnDimension(); ++k) {
260 sum += (i + k / 1024.0) * (j + k / 1024.0);
261 }
262 Assert.assertEquals(sum, mmT.getEntry(i, j), 0);
263 }
264 }
265
266 RealMatrix sub1 = m.getSubMatrix(2, 9, 5, 20);
267 for (int i = 0; i < sub1.getRowDimension(); ++i) {
268 for (int j = 0; j < sub1.getColumnDimension(); ++j) {
269 Assert.assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0);
270 }
271 }
272
273 RealMatrix sub2 = m.getSubMatrix(10, 12, 3, 70);
274 for (int i = 0; i < sub2.getRowDimension(); ++i) {
275 for (int j = 0; j < sub2.getColumnDimension(); ++j) {
276 Assert.assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0);
277 }
278 }
279
280 RealMatrix sub3 = m.getSubMatrix(30, 34, 0, 5);
281 for (int i = 0; i < sub3.getRowDimension(); ++i) {
282 for (int j = 0; j < sub3.getColumnDimension(); ++j) {
283 Assert.assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0);
284 }
285 }
286
287 RealMatrix sub4 = m.getSubMatrix(30, 32, 62, 65);
288 for (int i = 0; i < sub4.getRowDimension(); ++i) {
289 for (int j = 0; j < sub4.getColumnDimension(); ++j) {
290 Assert.assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0);
291 }
292 }
293
294 }
295
296
297
298 private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}};
299 private double[][] d4 = new double[][] {{1},{2},{3},{4}};
300 private double[][] d5 = new double[][] {{30},{70}};
301
302 @Test
303 public void testMultiply2() {
304 RealMatrix m3 = new BlockRealMatrix(d3);
305 RealMatrix m4 = new BlockRealMatrix(d4);
306 RealMatrix m5 = new BlockRealMatrix(d5);
307 assertClose(m3.multiply(m4), m5, entryTolerance);
308 }
309
310 @Test
311 public void testMultiplyTransposedBlockRealMatrix() {
312 RandomGenerator randomGenerator = new Well1024a(0xfaa1594a49a1359el);
313 for (int rows = 1; rows <= 64; rows += 7) {
314 for (int cols = 1; cols <= 64; cols += 7) {
315 final BlockRealMatrix a = new BlockRealMatrix(rows, cols);
316 a.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
317 public double visit(final int row, final int column, final double value) {
318 return randomGenerator.nextDouble();
319 }
320 });
321 for (int interm = 1; interm <= 64; interm += 7) {
322 final BlockRealMatrix b = new BlockRealMatrix(interm, cols);
323 b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
324 public double visit(final int row, final int column, final double value) {
325 return randomGenerator.nextDouble();
326 }
327 });
328 Assert.assertEquals(0.0,
329 a.multiplyTransposed(b).subtract(a.multiply(b.transpose())).getNorm1(),
330 1.0e-15);
331 }
332 }
333 }
334 }
335
336 @Test
337 public void testMultiplyTransposedArray2DRowRealMatrix() {
338 RandomGenerator randomGenerator = new Well1024a(0xac2d0185fc69670bl);
339 final RealMatrixChangingVisitor randomSetter = new DefaultRealMatrixChangingVisitor() {
340 public double visit(final int row, final int column, final double value) {
341 return randomGenerator.nextDouble();
342 }
343 };
344 for (int rows = 1; rows <= 64; rows += 7) {
345 for (int cols = 1; cols <= 64; cols += 7) {
346 final BlockRealMatrix a = new BlockRealMatrix(rows, cols);
347 a.walkInOptimizedOrder(randomSetter);
348 for (int interm = 1; interm <= 64; interm += 7) {
349 final Array2DRowRealMatrix b = new Array2DRowRealMatrix(interm, cols);
350 b.walkInOptimizedOrder(randomSetter);
351 Assert.assertEquals(0.0,
352 a.multiplyTransposed(b).subtract(a.multiply(b.transpose())).getNorm1(),
353 1.0e-15);
354 }
355 }
356 }
357 }
358
359 @Test
360 public void testMultiplyTransposedWrongDimensions() {
361 try {
362 new BlockRealMatrix(2, 3).multiplyTransposed(new BlockRealMatrix(3, 2));
363 Assert.fail("an exception should have been thrown");
364 } catch (MathIllegalArgumentException miae) {
365 Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
366 Assert.assertEquals(3, ((Integer) miae.getParts()[0]).intValue());
367 Assert.assertEquals(2, ((Integer) miae.getParts()[1]).intValue());
368 }
369 }
370
371 @Test
372 public void testTransposeMultiplyBlockRealMatrix() {
373 RandomGenerator randomGenerator = new Well1024a(0xfaa1594a49a1359el);
374 for (int rows = 1; rows <= 64; rows += 7) {
375 for (int cols = 1; cols <= 64; cols += 7) {
376 final BlockRealMatrix a = new BlockRealMatrix(rows, cols);
377 a.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
378 public double visit(final int row, final int column, final double value) {
379 return randomGenerator.nextDouble();
380 }
381 });
382 for (int interm = 1; interm <= 64; interm += 7) {
383 final BlockRealMatrix b = new BlockRealMatrix(rows, interm);
384 b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
385 public double visit(final int row, final int column, final double value) {
386 return randomGenerator.nextDouble();
387 }
388 });
389 Assert.assertEquals(0.0,
390 a.transposeMultiply(b).subtract(a.transpose().multiply(b)).getNorm1(),
391 1.0e-15);
392 }
393 }
394 }
395 }
396
397 @Test
398 public void testTransposeMultiplyArray2DRowRealMatrix() {
399 RandomGenerator randomGenerator = new Well1024a(0xac2d0185fc69670bl);
400 final RealMatrixChangingVisitor randomSetter = new DefaultRealMatrixChangingVisitor() {
401 public double visit(final int row, final int column, final double value) {
402 return randomGenerator.nextDouble();
403 }
404 };
405 for (int rows = 1; rows <= 64; rows += 7) {
406 for (int cols = 1; cols <= 64; cols += 7) {
407 final BlockRealMatrix a = new BlockRealMatrix(rows, cols);
408 a.walkInOptimizedOrder(randomSetter);
409 for (int interm = 1; interm <= 64; interm += 7) {
410 final Array2DRowRealMatrix b = new Array2DRowRealMatrix(rows, interm);
411 b.walkInOptimizedOrder(randomSetter);
412 Assert.assertEquals(0.0,
413 a.transposeMultiply(b).subtract(a.transpose().multiply(b)).getNorm1(),
414 1.0e-15);
415 }
416 }
417 }
418 }
419
420 @Test
421 public void testTransposeMultiplyWrongDimensions() {
422 try {
423 new BlockRealMatrix(2, 3).transposeMultiply(new BlockRealMatrix(3, 2));
424 Assert.fail("an exception should have been thrown");
425 } catch (MathIllegalArgumentException miae) {
426 Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, miae.getSpecifier());
427 Assert.assertEquals(2, ((Integer) miae.getParts()[0]).intValue());
428 Assert.assertEquals(3, ((Integer) miae.getParts()[1]).intValue());
429 }
430 }
431
432
433 @Test
434 public void testTrace() {
435 RealMatrix m = new BlockRealMatrix(id);
436 Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
437 m = new BlockRealMatrix(testData2);
438 try {
439 m.getTrace();
440 Assert.fail("Expecting MathIllegalArgumentException");
441 } catch (MathIllegalArgumentException ex) {
442
443 }
444 }
445
446
447 @Test
448 public void testScalarAdd() {
449 RealMatrix m = new BlockRealMatrix(testData);
450 assertClose(new BlockRealMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance);
451 }
452
453
454 @Test
455 public void testOperate() {
456 RealMatrix m = new BlockRealMatrix(id);
457 assertClose(testVector, m.operate(testVector), entryTolerance);
458 assertClose(testVector, m.operate(new ArrayRealVector(testVector)).toArray(), entryTolerance);
459 m = new BlockRealMatrix(bigSingular);
460 try {
461 m.operate(testVector);
462 Assert.fail("Expecting illegalArgumentException");
463 } catch (MathIllegalArgumentException ex) {
464
465 }
466 }
467
468 @Test
469 public void testOperateLarge() {
470 int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2;
471 int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2;
472 int r = 3 * BlockRealMatrix.BLOCK_SIZE;
473 Random random = new Random(111007463902334l);
474 RealMatrix m1 = createRandomMatrix(random, p, q);
475 RealMatrix m2 = createRandomMatrix(random, q, r);
476 RealMatrix m1m2 = m1.multiply(m2);
477 for (int i = 0; i < r; ++i) {
478 checkArrays(m1m2.getColumn(i), m1.operate(m2.getColumn(i)));
479 }
480 }
481
482 @Test
483 public void testOperatePremultiplyLarge() {
484 int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2;
485 int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2;
486 int r = 3 * BlockRealMatrix.BLOCK_SIZE;
487 Random random = new Random(111007463902334l);
488 RealMatrix m1 = createRandomMatrix(random, p, q);
489 RealMatrix m2 = createRandomMatrix(random, q, r);
490 RealMatrix m1m2 = m1.multiply(m2);
491 for (int i = 0; i < p; ++i) {
492 checkArrays(m1m2.getRow(i), m2.preMultiply(m1.getRow(i)));
493 }
494 }
495
496
497 @Test
498 public void testMath209() {
499 RealMatrix a = new BlockRealMatrix(new double[][] {
500 { 1, 2 }, { 3, 4 }, { 5, 6 }
501 });
502 double[] b = a.operate(new double[] { 1, 1 });
503 Assert.assertEquals(a.getRowDimension(), b.length);
504 Assert.assertEquals( 3.0, b[0], 1.0e-12);
505 Assert.assertEquals( 7.0, b[1], 1.0e-12);
506 Assert.assertEquals(11.0, b[2], 1.0e-12);
507 }
508
509
510 @Test
511 public void testTranspose() {
512 RealMatrix m = new BlockRealMatrix(testData);
513 RealMatrix mIT = new LUDecomposition(m).getSolver().getInverse().transpose();
514 RealMatrix mTI = new LUDecomposition(m.transpose()).getSolver().getInverse();
515 assertClose(mIT, mTI, normTolerance);
516 m = new BlockRealMatrix(testData2);
517 RealMatrix mt = new BlockRealMatrix(testData2T);
518 assertClose(mt, m.transpose(), normTolerance);
519 }
520
521
522 @Test
523 public void testPremultiplyVector() {
524 RealMatrix m = new BlockRealMatrix(testData);
525 assertClose(m.preMultiply(testVector), preMultTest, normTolerance);
526 assertClose(m.preMultiply(new ArrayRealVector(testVector).toArray()),
527 preMultTest, normTolerance);
528 m = new BlockRealMatrix(bigSingular);
529 try {
530 m.preMultiply(testVector);
531 Assert.fail("expecting MathIllegalArgumentException");
532 } catch (MathIllegalArgumentException ex) {
533
534 }
535 }
536
537 @Test
538 public void testArithmeticBlending() {
539
540
541 final RealMatrix m1 = new BlockRealMatrix(new double[][] {
542 { 1, 2, 3 },
543 { 4, 5, 6 },
544 { 7, 8, 9 },
545 });
546 final RealMatrix m2 = new BlockRealMatrix(new double[][] {
547 { 10, 11, 12 },
548 { 13, 14, 15 },
549 { 16, 17, 18 },
550 });
551
552 final double blendingValue = 0.2;
553
554
555 final RealMatrix blendedMatrix = m1.blendArithmeticallyWith(m2, blendingValue);
556
557
558 final RealMatrix expectedMatrix = new BlockRealMatrix(new double[][] {
559 { 2.8, 3.8, 4.8 },
560 { 5.8, 6.8, 7.8 },
561 { 8.8, 9.8, 10.8 }
562 });
563
564 assertClose(expectedMatrix, blendedMatrix, normTolerance);
565 }
566
567 @Test
568 public void testPremultiply() {
569 RealMatrix m3 = new BlockRealMatrix(d3);
570 RealMatrix m4 = new BlockRealMatrix(d4);
571 RealMatrix m5 = new BlockRealMatrix(d5);
572 assertClose(m4.preMultiply(m3), m5, entryTolerance);
573
574 BlockRealMatrix m = new BlockRealMatrix(testData);
575 BlockRealMatrix mInv = new BlockRealMatrix(testDataInv);
576 BlockRealMatrix identity = new BlockRealMatrix(id);
577 assertClose(m.preMultiply(mInv), identity, entryTolerance);
578 assertClose(mInv.preMultiply(m), identity, entryTolerance);
579 assertClose(m.preMultiply(identity), m, entryTolerance);
580 assertClose(identity.preMultiply(mInv), mInv, entryTolerance);
581 try {
582 m.preMultiply(new BlockRealMatrix(bigSingular));
583 Assert.fail("Expecting illegalArgumentException");
584 } catch (MathIllegalArgumentException ex) {
585
586 }
587 }
588
589 @Test
590 public void testGetVectors() {
591 RealMatrix m = new BlockRealMatrix(testData);
592 assertClose(m.getRow(0), testDataRow1, entryTolerance);
593 assertClose(m.getColumn(2), testDataCol3, entryTolerance);
594 try {
595 m.getRow(10);
596 Assert.fail("expecting MathIllegalArgumentException");
597 } catch (MathIllegalArgumentException ex) {
598
599 }
600 try {
601 m.getColumn(-1);
602 Assert.fail("expecting MathIllegalArgumentException");
603 } catch (MathIllegalArgumentException ex) {
604
605 }
606 }
607
608 @Test
609 public void testGetEntry() {
610 RealMatrix m = new BlockRealMatrix(testData);
611 Assert.assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
612 try {
613 m.getEntry(10, 4);
614 Assert.fail ("Expecting MathIllegalArgumentException");
615 } catch (MathIllegalArgumentException ex) {
616
617 }
618 }
619
620
621 @Test
622 public void testExamples() {
623
624 double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
625 RealMatrix m = new BlockRealMatrix(matrixData);
626
627 double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
628 RealMatrix n = new BlockRealMatrix(matrixData2);
629
630 RealMatrix p = m.multiply(n);
631 Assert.assertEquals(2, p.getRowDimension());
632 Assert.assertEquals(2, p.getColumnDimension());
633
634 RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();
635 Assert.assertEquals(2, pInverse.getRowDimension());
636 Assert.assertEquals(2, pInverse.getColumnDimension());
637
638
639 double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
640 RealMatrix coefficients = new BlockRealMatrix(coefficientsData);
641 RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false);
642 RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants);
643 final double cst0 = constants.getEntry(0);
644 final double cst1 = constants.getEntry(1);
645 final double cst2 = constants.getEntry(2);
646 final double sol0 = solution.getEntry(0);
647 final double sol1 = solution.getEntry(1);
648 final double sol2 = solution.getEntry(2);
649 Assert.assertEquals(2 * sol0 + 3 * sol1 -2 * sol2, cst0, 1E-12);
650 Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
651 Assert.assertEquals(4 * sol0 - 3 * sol1 -5 * sol2, cst2, 1E-12);
652 }
653
654
655 @Test
656 public void testGetSubMatrix() {
657 RealMatrix m = new BlockRealMatrix(subTestData);
658 checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
659 checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3);
660 checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3);
661 checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
662 checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
663 checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
664 checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
665 checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
666 checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
667 checkGetSubMatrix(m, null, 1, 0, 2, 4);
668 checkGetSubMatrix(m, null, -1, 1, 2, 2);
669 checkGetSubMatrix(m, null, 1, 0, 2, 2);
670 checkGetSubMatrix(m, null, 1, 0, 2, 4);
671 checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 });
672 checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 });
673 }
674
675 private void checkGetSubMatrix(RealMatrix m, double[][] reference,
676 int startRow, int endRow, int startColumn, int endColumn) {
677 try {
678 RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
679 if (reference != null) {
680 Assert.assertEquals(new BlockRealMatrix(reference), sub);
681 } else {
682 Assert.fail("Expecting MathIllegalArgumentException or MathIllegalArgumentException or MathIllegalArgumentException");
683 }
684 } catch (MathIllegalArgumentException e) {
685 if (reference != null) {
686 throw e;
687 }
688 }
689 }
690
691 private void checkGetSubMatrix(RealMatrix m, double[][] reference,
692 int[] selectedRows, int[] selectedColumns) {
693 try {
694 RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns);
695 if (reference != null) {
696 Assert.assertEquals(new BlockRealMatrix(reference), sub);
697 } else {
698 Assert.fail("Expecting MathIllegalArgumentException or MathIllegalArgumentExceptiono r MathIllegalArgumentException");
699 }
700 } catch (MathIllegalArgumentException e) {
701 if (reference != null) {
702 throw e;
703 }
704 }
705 }
706
707 @Test
708 public void testGetSetMatrixLarge() {
709 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
710 RealMatrix m = new BlockRealMatrix(n, n);
711 RealMatrix sub = new BlockRealMatrix(n - 4, n - 4).scalarAdd(1);
712
713 m.setSubMatrix(sub.getData(), 2, 2);
714 for (int i = 0; i < n; ++i) {
715 for (int j = 0; j < n; ++j) {
716 if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) {
717 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
718 } else {
719 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
720 }
721 }
722 }
723 Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3));
724
725 }
726
727 @Test
728 public void testCopySubMatrix() {
729 RealMatrix m = new BlockRealMatrix(subTestData);
730 checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
731 checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3);
732 checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3);
733 checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
734 checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
735 checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
736 checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
737 checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
738 checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
739
740 checkCopy(m, null, 1, 0, 2, 4);
741 checkCopy(m, null, -1, 1, 2, 2);
742 checkCopy(m, null, 1, 0, 2, 2);
743 checkCopy(m, null, 1, 0, 2, 4);
744 checkCopy(m, null, new int[] {}, new int[] { 0 });
745 checkCopy(m, null, new int[] { 0 }, new int[] { 4 });
746 }
747
748 private void checkCopy(RealMatrix m, double[][] reference,
749 int startRow, int endRow, int startColumn, int endColumn) {
750 try {
751 double[][] sub = (reference == null) ?
752 new double[1][1] :
753 new double[reference.length][reference[0].length];
754 m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
755 if (reference != null) {
756 Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
757 } else {
758 Assert.fail("Expecting MathIllegalArgumentException or MathIllegalArgumentException or MathIllegalArgumentException");
759 }
760 } catch (MathIllegalArgumentException e) {
761 if (reference != null) {
762 throw e;
763 }
764 }
765 }
766
767 private void checkCopy(RealMatrix m, double[][] reference,
768 int[] selectedRows, int[] selectedColumns) {
769 try {
770 double[][] sub = (reference == null) ?
771 new double[1][1] :
772 new double[reference.length][reference[0].length];
773 m.copySubMatrix(selectedRows, selectedColumns, sub);
774 if (reference != null) {
775 Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
776 } else {
777 Assert.fail("Expecting MathIllegalArgumentException or MathIllegalArgumentException or MathIllegalArgumentException");
778 }
779 } catch (MathIllegalArgumentException e) {
780 if (reference != null) {
781 throw e;
782 }
783 }
784 }
785
786 @Test
787 public void testGetRowMatrix() {
788 RealMatrix m = new BlockRealMatrix(subTestData);
789 RealMatrix mRow0 = new BlockRealMatrix(subRow0);
790 RealMatrix mRow3 = new BlockRealMatrix(subRow3);
791 Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
792 Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
793 try {
794 m.getRowMatrix(-1);
795 Assert.fail("Expecting MathIllegalArgumentException");
796 } catch (MathIllegalArgumentException ex) {
797
798 }
799 try {
800 m.getRowMatrix(4);
801 Assert.fail("Expecting MathIllegalArgumentException");
802 } catch (MathIllegalArgumentException ex) {
803
804 }
805 }
806
807 @Test
808 public void testSetRowMatrix() {
809 RealMatrix m = new BlockRealMatrix(subTestData);
810 RealMatrix mRow3 = new BlockRealMatrix(subRow3);
811 Assert.assertNotSame(mRow3, m.getRowMatrix(0));
812 m.setRowMatrix(0, mRow3);
813 Assert.assertEquals(mRow3, m.getRowMatrix(0));
814 try {
815 m.setRowMatrix(-1, mRow3);
816 Assert.fail("Expecting MathIllegalArgumentException");
817 } catch (MathIllegalArgumentException ex) {
818
819 }
820 try {
821 m.setRowMatrix(0, m);
822 Assert.fail("Expecting MathIllegalArgumentException");
823 } catch (MathIllegalArgumentException ex) {
824
825 }
826 }
827
828 @Test
829 public void testGetSetRowMatrixLarge() {
830 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
831 RealMatrix m = new BlockRealMatrix(n, n);
832 RealMatrix sub = new BlockRealMatrix(1, n).scalarAdd(1);
833
834 m.setRowMatrix(2, sub);
835 for (int i = 0; i < n; ++i) {
836 for (int j = 0; j < n; ++j) {
837 if (i != 2) {
838 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
839 } else {
840 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
841 }
842 }
843 }
844 Assert.assertEquals(sub, m.getRowMatrix(2));
845 }
846
847 @Test
848 public void testGetColumnMatrix() {
849 RealMatrix m = new BlockRealMatrix(subTestData);
850 RealMatrix mColumn1 = new BlockRealMatrix(subColumn1);
851 RealMatrix mColumn3 = new BlockRealMatrix(subColumn3);
852 Assert.assertEquals(mColumn1, m.getColumnMatrix(1));
853 Assert.assertEquals(mColumn3, m.getColumnMatrix(3));
854 try {
855 m.getColumnMatrix(-1);
856 Assert.fail("Expecting MathIllegalArgumentException");
857 } catch (MathIllegalArgumentException ex) {
858
859 }
860 try {
861 m.getColumnMatrix(4);
862 Assert.fail("Expecting MathIllegalArgumentException");
863 } catch (MathIllegalArgumentException ex) {
864
865 }
866 }
867
868 @Test
869 public void testSetColumnMatrix() {
870 RealMatrix m = new BlockRealMatrix(subTestData);
871 RealMatrix mColumn3 = new BlockRealMatrix(subColumn3);
872 Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
873 m.setColumnMatrix(1, mColumn3);
874 Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
875 try {
876 m.setColumnMatrix(-1, mColumn3);
877 Assert.fail("Expecting MathIllegalArgumentException");
878 } catch (MathIllegalArgumentException ex) {
879
880 }
881 try {
882 m.setColumnMatrix(0, m);
883 Assert.fail("Expecting MathIllegalArgumentException");
884 } catch (MathIllegalArgumentException ex) {
885
886 }
887 }
888
889 @Test
890 public void testGetSetColumnMatrixLarge() {
891 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
892 RealMatrix m = new BlockRealMatrix(n, n);
893 RealMatrix sub = new BlockRealMatrix(n, 1).scalarAdd(1);
894
895 m.setColumnMatrix(2, sub);
896 for (int i = 0; i < n; ++i) {
897 for (int j = 0; j < n; ++j) {
898 if (j != 2) {
899 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
900 } else {
901 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
902 }
903 }
904 }
905 Assert.assertEquals(sub, m.getColumnMatrix(2));
906
907 }
908
909 @Test
910 public void testGetRowVector() {
911 RealMatrix m = new BlockRealMatrix(subTestData);
912 RealVector mRow0 = new ArrayRealVector(subRow0[0]);
913 RealVector mRow3 = new ArrayRealVector(subRow3[0]);
914 Assert.assertEquals(mRow0, m.getRowVector(0));
915 Assert.assertEquals(mRow3, m.getRowVector(3));
916 try {
917 m.getRowVector(-1);
918 Assert.fail("Expecting MathIllegalArgumentException");
919 } catch (MathIllegalArgumentException ex) {
920
921 }
922 try {
923 m.getRowVector(4);
924 Assert.fail("Expecting MathIllegalArgumentException");
925 } catch (MathIllegalArgumentException ex) {
926
927 }
928 }
929
930 @Test
931 public void testSetRowVector() {
932 RealMatrix m = new BlockRealMatrix(subTestData);
933 RealVector mRow3 = new ArrayRealVector(subRow3[0]);
934 Assert.assertNotSame(mRow3, m.getRowMatrix(0));
935 m.setRowVector(0, mRow3);
936 Assert.assertEquals(mRow3, m.getRowVector(0));
937 try {
938 m.setRowVector(-1, mRow3);
939 Assert.fail("Expecting MathIllegalArgumentException");
940 } catch (MathIllegalArgumentException ex) {
941
942 }
943 try {
944 m.setRowVector(0, new ArrayRealVector(5));
945 Assert.fail("Expecting MathIllegalArgumentException");
946 } catch (MathIllegalArgumentException ex) {
947
948 }
949 }
950
951 @Test
952 public void testGetSetRowVectorLarge() {
953 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
954 RealMatrix m = new BlockRealMatrix(n, n);
955 RealVector sub = new ArrayRealVector(n, 1.0);
956
957 m.setRowVector(2, sub);
958 for (int i = 0; i < n; ++i) {
959 for (int j = 0; j < n; ++j) {
960 if (i != 2) {
961 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
962 } else {
963 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
964 }
965 }
966 }
967 Assert.assertEquals(sub, m.getRowVector(2));
968 }
969
970 @Test
971 public void testGetColumnVector() {
972 RealMatrix m = new BlockRealMatrix(subTestData);
973 RealVector mColumn1 = columnToVector(subColumn1);
974 RealVector mColumn3 = columnToVector(subColumn3);
975 Assert.assertEquals(mColumn1, m.getColumnVector(1));
976 Assert.assertEquals(mColumn3, m.getColumnVector(3));
977 try {
978 m.getColumnVector(-1);
979 Assert.fail("Expecting MathIllegalArgumentException");
980 } catch (MathIllegalArgumentException ex) {
981
982 }
983 try {
984 m.getColumnVector(4);
985 Assert.fail("Expecting MathIllegalArgumentException");
986 } catch (MathIllegalArgumentException ex) {
987
988 }
989 }
990
991 @Test
992 public void testSetColumnVector() {
993 RealMatrix m = new BlockRealMatrix(subTestData);
994 RealVector mColumn3 = columnToVector(subColumn3);
995 Assert.assertNotSame(mColumn3, m.getColumnVector(1));
996 m.setColumnVector(1, mColumn3);
997 Assert.assertEquals(mColumn3, m.getColumnVector(1));
998 try {
999 m.setColumnVector(-1, mColumn3);
1000 Assert.fail("Expecting MathIllegalArgumentException");
1001 } catch (MathIllegalArgumentException ex) {
1002
1003 }
1004 try {
1005 m.setColumnVector(0, new ArrayRealVector(5));
1006 Assert.fail("Expecting MathIllegalArgumentException");
1007 } catch (MathIllegalArgumentException ex) {
1008
1009 }
1010 }
1011
1012 @Test
1013 public void testGetSetColumnVectorLarge() {
1014 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
1015 RealMatrix m = new BlockRealMatrix(n, n);
1016 RealVector sub = new ArrayRealVector(n, 1.0);
1017
1018 m.setColumnVector(2, sub);
1019 for (int i = 0; i < n; ++i) {
1020 for (int j = 0; j < n; ++j) {
1021 if (j != 2) {
1022 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
1023 } else {
1024 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
1025 }
1026 }
1027 }
1028 Assert.assertEquals(sub, m.getColumnVector(2));
1029 }
1030
1031 private RealVector columnToVector(double[][] column) {
1032 double[] data = new double[column.length];
1033 for (int i = 0; i < data.length; ++i) {
1034 data[i] = column[i][0];
1035 }
1036 return new ArrayRealVector(data, false);
1037 }
1038
1039 @Test
1040 public void testGetRow() {
1041 RealMatrix m = new BlockRealMatrix(subTestData);
1042 checkArrays(subRow0[0], m.getRow(0));
1043 checkArrays(subRow3[0], m.getRow(3));
1044 try {
1045 m.getRow(-1);
1046 Assert.fail("Expecting MathIllegalArgumentException");
1047 } catch (MathIllegalArgumentException ex) {
1048
1049 }
1050 try {
1051 m.getRow(4);
1052 Assert.fail("Expecting MathIllegalArgumentException");
1053 } catch (MathIllegalArgumentException ex) {
1054
1055 }
1056 }
1057
1058 @Test
1059 public void testSetRow() {
1060 RealMatrix m = new BlockRealMatrix(subTestData);
1061 Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
1062 m.setRow(0, subRow3[0]);
1063 checkArrays(subRow3[0], m.getRow(0));
1064 try {
1065 m.setRow(-1, subRow3[0]);
1066 Assert.fail("Expecting MathIllegalArgumentException");
1067 } catch (MathIllegalArgumentException ex) {
1068
1069 }
1070 try {
1071 m.setRow(0, new double[5]);
1072 Assert.fail("Expecting MathIllegalArgumentException");
1073 } catch (MathIllegalArgumentException ex) {
1074
1075 }
1076 }
1077
1078 @Test
1079 public void testGetSetRowLarge() {
1080 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
1081 RealMatrix m = new BlockRealMatrix(n, n);
1082 double[] sub = new double[n];
1083 Arrays.fill(sub, 1.0);
1084
1085 m.setRow(2, sub);
1086 for (int i = 0; i < n; ++i) {
1087 for (int j = 0; j < n; ++j) {
1088 if (i != 2) {
1089 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
1090 } else {
1091 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
1092 }
1093 }
1094 }
1095 checkArrays(sub, m.getRow(2));
1096 }
1097
1098 @Test
1099 public void testGetColumn() {
1100 RealMatrix m = new BlockRealMatrix(subTestData);
1101 double[] mColumn1 = columnToArray(subColumn1);
1102 double[] mColumn3 = columnToArray(subColumn3);
1103 checkArrays(mColumn1, m.getColumn(1));
1104 checkArrays(mColumn3, m.getColumn(3));
1105 try {
1106 m.getColumn(-1);
1107 Assert.fail("Expecting MathIllegalArgumentException");
1108 } catch (MathIllegalArgumentException ex) {
1109
1110 }
1111 try {
1112 m.getColumn(4);
1113 Assert.fail("Expecting MathIllegalArgumentException");
1114 } catch (MathIllegalArgumentException ex) {
1115
1116 }
1117 }
1118
1119 @Test
1120 public void testSetColumn() {
1121 RealMatrix m = new BlockRealMatrix(subTestData);
1122 double[] mColumn3 = columnToArray(subColumn3);
1123 Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
1124 m.setColumn(1, mColumn3);
1125 checkArrays(mColumn3, m.getColumn(1));
1126 try {
1127 m.setColumn(-1, mColumn3);
1128 Assert.fail("Expecting MathIllegalArgumentException");
1129 } catch (MathIllegalArgumentException ex) {
1130
1131 }
1132 try {
1133 m.setColumn(0, new double[5]);
1134 Assert.fail("Expecting MathIllegalArgumentException");
1135 } catch (MathIllegalArgumentException ex) {
1136
1137 }
1138 }
1139
1140 @Test
1141 public void testGetSetColumnLarge() {
1142 int n = 3 * BlockRealMatrix.BLOCK_SIZE;
1143 RealMatrix m = new BlockRealMatrix(n, n);
1144 double[] sub = new double[n];
1145 Arrays.fill(sub, 1.0);
1146
1147 m.setColumn(2, sub);
1148 for (int i = 0; i < n; ++i) {
1149 for (int j = 0; j < n; ++j) {
1150 if (j != 2) {
1151 Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
1152 } else {
1153 Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
1154 }
1155 }
1156 }
1157 checkArrays(sub, m.getColumn(2));
1158 }
1159
1160 private double[] columnToArray(double[][] column) {
1161 double[] data = new double[column.length];
1162 for (int i = 0; i < data.length; ++i) {
1163 data[i] = column[i][0];
1164 }
1165 return data;
1166 }
1167
1168 private void checkArrays(double[] expected, double[] actual) {
1169 Assert.assertEquals(expected.length, actual.length);
1170 for (int i = 0; i < expected.length; ++i) {
1171 Assert.assertEquals(expected[i], actual[i], 0);
1172 }
1173 }
1174
1175 @Test
1176 public void testEqualsAndHashCode() {
1177 BlockRealMatrix m = new BlockRealMatrix(testData);
1178 BlockRealMatrix m1 = m.copy();
1179 BlockRealMatrix mt = m.transpose();
1180 Assert.assertTrue(m.hashCode() != mt.hashCode());
1181 Assert.assertEquals(m.hashCode(), m1.hashCode());
1182 Assert.assertEquals(m, m);
1183 Assert.assertEquals(m, m1);
1184 Assert.assertFalse(m.equals(null));
1185 Assert.assertFalse(m.equals(mt));
1186 Assert.assertFalse(m.equals(new BlockRealMatrix(bigSingular)));
1187 }
1188
1189 @Test
1190 public void testToString() {
1191 BlockRealMatrix m = new BlockRealMatrix(testData);
1192 Assert.assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
1193 m.toString());
1194 }
1195
1196 @Test
1197 public void testSetSubMatrix() {
1198 BlockRealMatrix m = new BlockRealMatrix(testData);
1199 m.setSubMatrix(detData2,1,1);
1200 RealMatrix expected = new BlockRealMatrix
1201 (new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
1202 Assert.assertEquals(expected, m);
1203
1204 m.setSubMatrix(detData2,0,0);
1205 expected = new BlockRealMatrix
1206 (new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
1207 Assert.assertEquals(expected, m);
1208
1209 m.setSubMatrix(testDataPlus2,0,0);
1210 expected = new BlockRealMatrix
1211 (new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
1212 Assert.assertEquals(expected, m);
1213
1214
1215 BlockRealMatrix matrix = new BlockRealMatrix
1216 (new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1 , 2}});
1217 matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1);
1218 expected = new BlockRealMatrix
1219 (new double[][] {{1, 2, 3, 4}, {5, 3, 4, 8}, {9, 5 ,6, 2}});
1220 Assert.assertEquals(expected, matrix);
1221
1222
1223 try {
1224 m.setSubMatrix(testData,1,1);
1225 Assert.fail("expecting MathIllegalArgumentException");
1226 } catch (MathIllegalArgumentException e) {
1227
1228 }
1229
1230 try {
1231 m.setSubMatrix(testData,-1,1);
1232 Assert.fail("expecting MathIllegalArgumentException");
1233 } catch (MathIllegalArgumentException e) {
1234
1235 }
1236 try {
1237 m.setSubMatrix(testData,1,-1);
1238 Assert.fail("expecting MathIllegalArgumentException");
1239 } catch (MathIllegalArgumentException e) {
1240
1241 }
1242
1243
1244 try {
1245 m.setSubMatrix(null,1,1);
1246 Assert.fail("expecting NullArgumentException");
1247 } catch (NullArgumentException e) {
1248
1249 }
1250
1251
1252 try {
1253 m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0);
1254 Assert.fail("expecting MathIllegalArgumentException");
1255 } catch (MathIllegalArgumentException e) {
1256
1257 }
1258
1259
1260 try {
1261 m.setSubMatrix(new double[][] {{}}, 0, 0);
1262 Assert.fail("expecting MathIllegalArgumentException");
1263 } catch (MathIllegalArgumentException e) {
1264
1265 }
1266 }
1267
1268 @Test
1269 public void testWalk() {
1270 int rows = 150;
1271 int columns = 75;
1272
1273 RealMatrix m = new BlockRealMatrix(rows, columns);
1274 m.walkInRowOrder(new SetVisitor());
1275 GetVisitor getVisitor = new GetVisitor();
1276 m.walkInOptimizedOrder(getVisitor);
1277 Assert.assertEquals(rows * columns, getVisitor.getCount());
1278
1279 m = new BlockRealMatrix(rows, columns);
1280 m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
1281 getVisitor = new GetVisitor();
1282 m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
1283 Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
1284 for (int i = 0; i < rows; ++i) {
1285 Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
1286 Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
1287 }
1288 for (int j = 0; j < columns; ++j) {
1289 Assert.assertEquals(0.0, m.getEntry(0, j), 0);
1290 Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
1291 }
1292
1293 m = new BlockRealMatrix(rows, columns);
1294 m.walkInColumnOrder(new SetVisitor());
1295 getVisitor = new GetVisitor();
1296 m.walkInOptimizedOrder(getVisitor);
1297 Assert.assertEquals(rows * columns, getVisitor.getCount());
1298
1299 m = new BlockRealMatrix(rows, columns);
1300 m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
1301 getVisitor = new GetVisitor();
1302 m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
1303 Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
1304 for (int i = 0; i < rows; ++i) {
1305 Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
1306 Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
1307 }
1308 for (int j = 0; j < columns; ++j) {
1309 Assert.assertEquals(0.0, m.getEntry(0, j), 0);
1310 Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
1311 }
1312
1313 m = new BlockRealMatrix(rows, columns);
1314 m.walkInOptimizedOrder(new SetVisitor());
1315 getVisitor = new GetVisitor();
1316 m.walkInRowOrder(getVisitor);
1317 Assert.assertEquals(rows * columns, getVisitor.getCount());
1318
1319 m = new BlockRealMatrix(rows, columns);
1320 m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
1321 getVisitor = new GetVisitor();
1322 m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
1323 Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
1324 for (int i = 0; i < rows; ++i) {
1325 Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
1326 Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
1327 }
1328 for (int j = 0; j < columns; ++j) {
1329 Assert.assertEquals(0.0, m.getEntry(0, j), 0);
1330 Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
1331 }
1332
1333 m = new BlockRealMatrix(rows, columns);
1334 m.walkInOptimizedOrder(new SetVisitor());
1335 getVisitor = new GetVisitor();
1336 m.walkInColumnOrder(getVisitor);
1337 Assert.assertEquals(rows * columns, getVisitor.getCount());
1338
1339 m = new BlockRealMatrix(rows, columns);
1340 m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
1341 getVisitor = new GetVisitor();
1342 m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
1343 Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
1344 for (int i = 0; i < rows; ++i) {
1345 Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
1346 Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
1347 }
1348 for (int j = 0; j < columns; ++j) {
1349 Assert.assertEquals(0.0, m.getEntry(0, j), 0);
1350 Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
1351 }
1352
1353 }
1354
1355 @Test
1356 public void testSerial() {
1357 BlockRealMatrix m = new BlockRealMatrix(testData);
1358 Assert.assertEquals(m,UnitTestUtils.serializeAndRecover(m));
1359 }
1360
1361 private static class SetVisitor extends DefaultRealMatrixChangingVisitor {
1362 @Override
1363 public double visit(int i, int j, double value) {
1364 return i + j / 1024.0;
1365 }
1366 }
1367
1368 private static class GetVisitor extends DefaultRealMatrixPreservingVisitor {
1369 private int count = 0;
1370 @Override
1371 public void visit(int i, int j, double value) {
1372 ++count;
1373 Assert.assertEquals(i + j / 1024.0, value, 0.0);
1374 }
1375 public int getCount() {
1376 return count;
1377 }
1378 }
1379
1380
1381
1382
1383 protected void assertClose(RealMatrix m, RealMatrix n, double tolerance) {
1384 Assert.assertTrue(m.subtract(n).getNorm1() < tolerance);
1385 }
1386
1387
1388 protected void assertClose(double[] m, double[] n, double tolerance) {
1389 if (m.length != n.length) {
1390 Assert.fail("vectors not same length");
1391 }
1392 for (int i = 0; i < m.length; i++) {
1393 Assert.assertEquals(m[i], n[i], tolerance);
1394 }
1395 }
1396
1397 private BlockRealMatrix createRandomMatrix(Random r, int rows, int columns) {
1398 BlockRealMatrix m = new BlockRealMatrix(rows, columns);
1399 for (int i = 0; i < rows; ++i) {
1400 for (int j = 0; j < columns; ++j) {
1401 m.setEntry(i, j, 200 * r.nextDouble() - 100);
1402 }
1403 }
1404 return m;
1405 }
1406 }