View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.linear;
18  
19  import org.junit.Test;
20  import org.junit.Assert;
21  import org.apache.commons.math4.legacy.TestUtils;
22  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
23  import org.apache.commons.math4.legacy.exception.NoDataException;
24  import org.apache.commons.math4.legacy.exception.NullArgumentException;
25  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
26  import org.apache.commons.math4.legacy.exception.OutOfRangeException;
27  
28  /**
29   * Test cases for the {@link OpenMapRealMatrix} class.
30   *
31   */
32  public final class SparseRealMatrixTest {
33  
34      // 3 x 3 identity matrix
35      protected double[][] id = { { 1d, 0d, 0d }, { 0d, 1d, 0d }, { 0d, 0d, 1d } };
36      // Test data for group operations
37      protected double[][] testData = { { 1d, 2d, 3d }, { 2d, 5d, 3d },
38              { 1d, 0d, 8d } };
39      protected double[][] testDataLU = { { 2d, 5d, 3d }, { .5d, -2.5d, 6.5d },
40              { 0.5d, 0.2d, .2d } };
41      protected double[][] testDataPlus2 = { { 3d, 4d, 5d }, { 4d, 7d, 5d },
42              { 3d, 2d, 10d } };
43      protected double[][] testDataMinus = { { -1d, -2d, -3d },
44              { -2d, -5d, -3d }, { -1d, 0d, -8d } };
45      protected double[] testDataRow1 = { 1d, 2d, 3d };
46      protected double[] testDataCol3 = { 3d, 3d, 8d };
47      protected double[][] testDataInv = { { -40d, 16d, 9d }, { 13d, -5d, -3d },
48              { 5d, -2d, -1d } };
49      protected double[] preMultTest = { 8, 12, 33 };
50      protected double[][] testData2 = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
51      protected double[][] testData2T = { { 1d, 2d }, { 2d, 5d }, { 3d, 3d } };
52      protected double[][] testDataPlusInv = { { -39d, 18d, 12d },
53              { 15d, 0d, 0d }, { 6d, -2d, 7d } };
54  
55      // lu decomposition tests
56      protected double[][] luData = { { 2d, 3d, 3d }, { 0d, 5d, 7d }, { 6d, 9d, 8d } };
57      protected double[][] luDataLUDecomposition = { { 6d, 9d, 8d },
58              { 0d, 5d, 7d }, { 0.33333333333333, 0d, 0.33333333333333 } };
59  
60      // singular matrices
61      protected double[][] singular = { { 2d, 3d }, { 2d, 3d } };
62      protected double[][] bigSingular = { { 1d, 2d, 3d, 4d },
63              { 2d, 5d, 3d, 4d }, { 7d, 3d, 256d, 1930d }, { 3d, 7d, 6d, 8d } }; // 4th
64  
65      // row
66      // =
67      // 1st
68      // +
69      // 2nd
70      protected double[][] detData = { { 1d, 2d, 3d }, { 4d, 5d, 6d },
71              { 7d, 8d, 10d } };
72      protected double[][] detData2 = { { 1d, 3d }, { 2d, 4d } };
73  
74      // vectors
75      protected double[] testVector = { 1, 2, 3 };
76      protected double[] testVector2 = { 1, 2, 3, 4 };
77  
78      // submatrix accessor tests
79      protected double[][] subTestData = { { 1, 2, 3, 4 },
80              { 1.5, 2.5, 3.5, 4.5 }, { 2, 4, 6, 8 }, { 4, 5, 6, 7 } };
81  
82      // array selections
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      // effective permutations
88      protected double[][] subRows20Cols123 = { { 4, 6, 8 }, { 2, 3, 4 } };
89      protected double[][] subRows31Cols31 = { { 7, 5 }, { 4.5, 2.5 } };
90  
91      // contiguous ranges
92      protected double[][] subRows01Cols23 = { { 3, 4 }, { 3.5, 4.5 } };
93      protected double[][] subRows23Cols00 = { { 2 }, { 4 } };
94      protected double[][] subRows00Cols33 = { { 4 } };
95  
96      // row matrices
97      protected double[][] subRow0 = { { 1, 2, 3, 4 } };
98      protected double[][] subRow3 = { { 4, 5, 6, 7 } };
99  
100     // column matrices
101     protected double[][] subColumn1 = { { 2 }, { 2.5 }, { 4 }, { 5 } };
102     protected double[][] subColumn3 = { { 4 }, { 4.5 }, { 8 }, { 7 } };
103 
104     // tolerances
105     protected double entryTolerance = 10E-16;
106     protected double normTolerance = 10E-14;
107 
108     /** test dimensions */
109     @Test
110     public void testDimensions() {
111         OpenMapRealMatrix m = createSparseMatrix(testData);
112         OpenMapRealMatrix m2 = createSparseMatrix(testData2);
113         Assert.assertEquals("testData row dimension", 3, m.getRowDimension());
114         Assert.assertEquals("testData column dimension", 3, m.getColumnDimension());
115         Assert.assertTrue("testData is square", m.isSquare());
116         Assert.assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
117         Assert.assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
118         Assert.assertFalse("testData2 is not square", m2.isSquare());
119     }
120 
121     /** test copy functions */
122     @Test
123     public void testCopyFunctions() {
124         OpenMapRealMatrix m1 = createSparseMatrix(testData);
125         RealMatrix m2 = m1.copy();
126         Assert.assertEquals(m1.getClass(), m2.getClass());
127         Assert.assertEquals(m2, m1);
128         OpenMapRealMatrix m3 = createSparseMatrix(testData);
129         RealMatrix m4 = m3.copy();
130         Assert.assertEquals(m3.getClass(), m4.getClass());
131         Assert.assertEquals(m4, m3);
132     }
133 
134     /** test add */
135     @Test
136     public void testAdd() {
137         OpenMapRealMatrix m = createSparseMatrix(testData);
138         OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);
139         OpenMapRealMatrix mDataPlusInv = createSparseMatrix(testDataPlusInv);
140         RealMatrix mPlusMInv = m.add(mInv);
141         for (int row = 0; row < m.getRowDimension(); row++) {
142             for (int col = 0; col < m.getColumnDimension(); col++) {
143                 Assert.assertEquals("sum entry entry",
144                     mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col),
145                     entryTolerance);
146             }
147         }
148     }
149 
150     /** test add failure */
151     @Test
152     public void testAddFail() {
153         OpenMapRealMatrix m = createSparseMatrix(testData);
154         OpenMapRealMatrix m2 = createSparseMatrix(testData2);
155         try {
156             m.add(m2);
157             Assert.fail("MathIllegalArgumentException expected");
158         } catch (MathIllegalArgumentException ex) {
159             // ignored
160         }
161     }
162 
163     /** test norm */
164     @Test
165     public void testNorm() {
166         OpenMapRealMatrix m = createSparseMatrix(testData);
167         OpenMapRealMatrix m2 = createSparseMatrix(testData2);
168         Assert.assertEquals("testData norm", 14d, m.getNorm(), entryTolerance);
169         Assert.assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance);
170     }
171 
172     /** test m-n = m + -n */
173     @Test
174     public void testPlusMinus() {
175         OpenMapRealMatrix m = createSparseMatrix(testData);
176         OpenMapRealMatrix n = createSparseMatrix(testDataInv);
177         assertClose("m-n = m + -n", m.subtract(n),
178             n.scalarMultiply(-1d).add(m), entryTolerance);
179         try {
180             m.subtract(createSparseMatrix(testData2));
181             Assert.fail("Expecting illegalArgumentException");
182         } catch (MathIllegalArgumentException ex) {
183             // ignored
184         }
185     }
186 
187     /** test multiply */
188     @Test
189     public void testMultiply() {
190         OpenMapRealMatrix m = createSparseMatrix(testData);
191         OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);
192         OpenMapRealMatrix identity = createSparseMatrix(id);
193         OpenMapRealMatrix m2 = createSparseMatrix(testData2);
194         assertClose("inverse multiply", m.multiply(mInv), identity,
195                 entryTolerance);
196         assertClose("inverse multiply", m.multiply(new BlockRealMatrix(testDataInv)), identity,
197                     entryTolerance);
198         assertClose("inverse multiply", mInv.multiply(m), identity,
199                 entryTolerance);
200         assertClose("identity multiply", m.multiply(identity), m,
201                 entryTolerance);
202         assertClose("identity multiply", identity.multiply(mInv), mInv,
203                 entryTolerance);
204         assertClose("identity multiply", m2.multiply(identity), m2,
205                 entryTolerance);
206         try {
207             m.multiply(createSparseMatrix(bigSingular));
208             Assert.fail("Expecting illegalArgumentException");
209         } catch (MathIllegalArgumentException ex) {
210             // ignored
211         }
212     }
213 
214     // Additional Test for Array2DRowRealMatrixTest.testMultiply
215 
216     private double[][] d3 = new double[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
217     private double[][] d4 = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
218     private double[][] d5 = new double[][] { { 30 }, { 70 } };
219 
220     @Test
221     public void testMultiply2() {
222         RealMatrix m3 = createSparseMatrix(d3);
223         RealMatrix m4 = createSparseMatrix(d4);
224         RealMatrix m5 = createSparseMatrix(d5);
225         assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
226     }
227 
228     /** test trace */
229     @Test
230     public void testTrace() {
231         RealMatrix m = createSparseMatrix(id);
232         Assert.assertEquals("identity trace", 3d, m.getTrace(), entryTolerance);
233         m = createSparseMatrix(testData2);
234         try {
235             m.getTrace();
236             Assert.fail("Expecting NonSquareMatrixException");
237         } catch (NonSquareMatrixException ex) {
238             // ignored
239         }
240     }
241 
242     /** test scalarAdd */
243     @Test
244     public void testScalarAdd() {
245         RealMatrix m = createSparseMatrix(testData);
246         assertClose("scalar add", createSparseMatrix(testDataPlus2),
247             m.scalarAdd(2d), entryTolerance);
248     }
249 
250     /** test operate */
251     @Test
252     public void testOperate() {
253         RealMatrix m = createSparseMatrix(id);
254         assertClose("identity operate", testVector, m.operate(testVector),
255                 entryTolerance);
256         assertClose("identity operate", testVector, m.operate(
257                 new ArrayRealVector(testVector)).toArray(), entryTolerance);
258         m = createSparseMatrix(bigSingular);
259         try {
260             m.operate(testVector);
261             Assert.fail("Expecting illegalArgumentException");
262         } catch (MathIllegalArgumentException ex) {
263             // ignored
264         }
265     }
266 
267     /** test issue MATH-209 */
268     @Test
269     public void testMath209() {
270         RealMatrix a = createSparseMatrix(new double[][] {
271                 { 1, 2 }, { 3, 4 }, { 5, 6 } });
272         double[] b = a.operate(new double[] { 1, 1 });
273         Assert.assertEquals(a.getRowDimension(), b.length);
274         Assert.assertEquals(3.0, b[0], 1.0e-12);
275         Assert.assertEquals(7.0, b[1], 1.0e-12);
276         Assert.assertEquals(11.0, b[2], 1.0e-12);
277     }
278 
279     /** test transpose */
280     @Test
281     public void testTranspose() {
282         RealMatrix m = createSparseMatrix(testData);
283         RealMatrix mIT = new LUDecomposition(m).getSolver().getInverse().transpose();
284         RealMatrix mTI = new LUDecomposition(m.transpose()).getSolver().getInverse();
285         assertClose("inverse-transpose", mIT, mTI, normTolerance);
286         m = createSparseMatrix(testData2);
287         RealMatrix mt = createSparseMatrix(testData2T);
288         assertClose("transpose",mt,m.transpose(),normTolerance);
289     }
290 
291     /** test preMultiply by vector */
292     @Test
293     public void testPremultiplyVector() {
294         RealMatrix m = createSparseMatrix(testData);
295         assertClose("premultiply", m.preMultiply(testVector), preMultTest,
296             normTolerance);
297         assertClose("premultiply", m.preMultiply(
298             new ArrayRealVector(testVector).toArray()), preMultTest, normTolerance);
299         m = createSparseMatrix(bigSingular);
300         try {
301             m.preMultiply(testVector);
302             Assert.fail("expecting MathIllegalArgumentException");
303         } catch (MathIllegalArgumentException ex) {
304             // ignored
305         }
306     }
307 
308     @Test
309     public void testPremultiply() {
310         RealMatrix m3 = createSparseMatrix(d3);
311         RealMatrix m4 = createSparseMatrix(d4);
312         RealMatrix m5 = createSparseMatrix(d5);
313         assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);
314 
315         OpenMapRealMatrix m = createSparseMatrix(testData);
316         OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);
317         OpenMapRealMatrix identity = createSparseMatrix(id);
318         assertClose("inverse multiply", m.preMultiply(mInv), identity,
319                 entryTolerance);
320         assertClose("inverse multiply", mInv.preMultiply(m), identity,
321                 entryTolerance);
322         assertClose("identity multiply", m.preMultiply(identity), m,
323                 entryTolerance);
324         assertClose("identity multiply", identity.preMultiply(mInv), mInv,
325                 entryTolerance);
326         try {
327             m.preMultiply(createSparseMatrix(bigSingular));
328             Assert.fail("Expecting illegalArgumentException");
329         } catch (MathIllegalArgumentException ex) {
330             // ignored
331         }
332     }
333 
334     @Test
335     public void testGetVectors() {
336         RealMatrix m = createSparseMatrix(testData);
337         assertClose("get row", m.getRow(0), testDataRow1, entryTolerance);
338         assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance);
339         try {
340             m.getRow(10);
341             Assert.fail("expecting OutOfRangeException");
342         } catch (OutOfRangeException ex) {
343             // ignored
344         }
345         try {
346             m.getColumn(-1);
347             Assert.fail("expecting OutOfRangeException");
348         } catch (OutOfRangeException ex) {
349             // ignored
350         }
351     }
352 
353     @Test
354     public void testGetEntry() {
355         RealMatrix m = createSparseMatrix(testData);
356         Assert.assertEquals("get entry", m.getEntry(0, 1), 2d, entryTolerance);
357         try {
358             m.getEntry(10, 4);
359             Assert.fail("Expecting OutOfRangeException");
360         } catch (OutOfRangeException ex) {
361             // expected
362         }
363     }
364 
365     /** test examples in user guide */
366     @Test
367     public void testExamples() {
368         // Create a real matrix with two rows and three columns
369         double[][] matrixData = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
370         RealMatrix m = createSparseMatrix(matrixData);
371         // One more with three rows, two columns
372         double[][] matrixData2 = { { 1d, 2d }, { 2d, 5d }, { 1d, 7d } };
373         RealMatrix n = createSparseMatrix(matrixData2);
374         // Now multiply m by n
375         RealMatrix p = m.multiply(n);
376         Assert.assertEquals(2, p.getRowDimension());
377         Assert.assertEquals(2, p.getColumnDimension());
378         // Invert p
379         RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();
380         Assert.assertEquals(2, pInverse.getRowDimension());
381         Assert.assertEquals(2, pInverse.getColumnDimension());
382 
383         // Solve example
384         double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 },
385                 { 4, -3, -5 } };
386         RealMatrix coefficients = createSparseMatrix(coefficientsData);
387         RealVector constants = new ArrayRealVector(new double[]{ 1, -2, 1 }, false);
388         RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants);
389         final double cst0 = constants.getEntry(0);
390         final double cst1 = constants.getEntry(1);
391         final double cst2 = constants.getEntry(2);
392         final double sol0 = solution.getEntry(0);
393         final double sol1 = solution.getEntry(1);
394         final double sol2 = solution.getEntry(2);
395         Assert.assertEquals(2 * sol0 + 3 * sol1 - 2 * sol2, cst0, 1E-12);
396         Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
397         Assert.assertEquals(4 * sol0 - 3 * sol1 - 5 * sol2, cst2, 1E-12);
398     }
399 
400     // test submatrix accessors
401     @Test
402     public void testSubMatrix() {
403         RealMatrix m = createSparseMatrix(subTestData);
404         RealMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00);
405         RealMatrix mRows00Cols33 = createSparseMatrix(subRows00Cols33);
406         RealMatrix mRows01Cols23 = createSparseMatrix(subRows01Cols23);
407         RealMatrix mRows02Cols13 = createSparseMatrix(subRows02Cols13);
408         RealMatrix mRows03Cols12 = createSparseMatrix(subRows03Cols12);
409         RealMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123);
410         RealMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123);
411         RealMatrix mRows31Cols31 = createSparseMatrix(subRows31Cols31);
412         Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
413         Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
414         Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
415         Assert.assertEquals("Rows02Cols13", mRows02Cols13,
416             m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 }));
417         Assert.assertEquals("Rows03Cols12", mRows03Cols12,
418             m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 }));
419         Assert.assertEquals("Rows03Cols123", mRows03Cols123,
420             m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
421         Assert.assertEquals("Rows20Cols123", mRows20Cols123,
422             m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
423         Assert.assertEquals("Rows31Cols31", mRows31Cols31,
424             m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
425         Assert.assertEquals("Rows31Cols31", mRows31Cols31,
426             m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
427 
428         try {
429             m.getSubMatrix(1, 0, 2, 4);
430             Assert.fail("Expecting NumberIsTooSmallException");
431         } catch (NumberIsTooSmallException ex) {
432             // expected
433         }
434         try {
435             m.getSubMatrix(-1, 1, 2, 2);
436             Assert.fail("Expecting OutOfRangeException");
437         } catch (OutOfRangeException ex) {
438             // expected
439         }
440         try {
441             m.getSubMatrix(1, 0, 2, 2);
442             Assert.fail("Expecting NumberIsTooSmallException");
443         } catch (NumberIsTooSmallException ex) {
444             // expected
445         }
446         try {
447             m.getSubMatrix(1, 0, 2, 4);
448             Assert.fail("Expecting NumberIsTooSmallException");
449         } catch (NumberIsTooSmallException ex) {
450             // expected
451         }
452         try {
453             m.getSubMatrix(new int[] {}, new int[] { 0 });
454             Assert.fail("Expecting NoDataException");
455         } catch (NoDataException ex) {
456             // expected
457         }
458         try {
459             m.getSubMatrix(new int[] { 0 }, new int[] { 4 });
460             Assert.fail("Expecting OutOfRangeException");
461         } catch (OutOfRangeException ex) {
462             // expected
463         }
464     }
465 
466     @Test
467     public void testGetRowMatrix() {
468         RealMatrix m = createSparseMatrix(subTestData);
469         RealMatrix mRow0 = createSparseMatrix(subRow0);
470         RealMatrix mRow3 = createSparseMatrix(subRow3);
471         Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
472         Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
473         try {
474             m.getRowMatrix(-1);
475             Assert.fail("Expecting OutOfRangeException");
476         } catch (OutOfRangeException ex) {
477             // expected
478         }
479         try {
480             m.getRowMatrix(4);
481             Assert.fail("Expecting OutOfRangeException");
482         } catch (OutOfRangeException ex) {
483             // expected
484         }
485     }
486 
487     @Test
488     public void testGetColumnMatrix() {
489         RealMatrix m = createSparseMatrix(subTestData);
490         RealMatrix mColumn1 = createSparseMatrix(subColumn1);
491         RealMatrix mColumn3 = createSparseMatrix(subColumn3);
492         Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
493         Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
494         try {
495             m.getColumnMatrix(-1);
496             Assert.fail("Expecting OutOfRangeException");
497         } catch (OutOfRangeException ex) {
498             // expected
499         }
500         try {
501             m.getColumnMatrix(4);
502             Assert.fail("Expecting OutOfRangeException");
503         } catch (OutOfRangeException ex) {
504             // expected
505         }
506     }
507 
508     @Test
509     public void testGetRowVector() {
510         RealMatrix m = createSparseMatrix(subTestData);
511         RealVector mRow0 = new ArrayRealVector(subRow0[0]);
512         RealVector mRow3 = new ArrayRealVector(subRow3[0]);
513         Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
514         Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
515         try {
516             m.getRowVector(-1);
517             Assert.fail("Expecting OutOfRangeException");
518         } catch (OutOfRangeException ex) {
519             // expected
520         }
521         try {
522             m.getRowVector(4);
523             Assert.fail("Expecting OutOfRangeException");
524         } catch (OutOfRangeException ex) {
525             // expected
526         }
527     }
528 
529     @Test
530     public void testGetColumnVector() {
531         RealMatrix m = createSparseMatrix(subTestData);
532         RealVector mColumn1 = columnToVector(subColumn1);
533         RealVector mColumn3 = columnToVector(subColumn3);
534         Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
535         Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
536         try {
537             m.getColumnVector(-1);
538             Assert.fail("Expecting OutOfRangeException");
539         } catch (OutOfRangeException ex) {
540             // expected
541         }
542         try {
543             m.getColumnVector(4);
544             Assert.fail("Expecting OutOfRangeException");
545         } catch (OutOfRangeException ex) {
546             // expected
547         }
548     }
549 
550     private RealVector columnToVector(double[][] column) {
551         double[] data = new double[column.length];
552         for (int i = 0; i < data.length; ++i) {
553             data[i] = column[i][0];
554         }
555         return new ArrayRealVector(data, false);
556     }
557 
558     @Test
559     public void testEqualsAndHashCode() {
560         OpenMapRealMatrix m = createSparseMatrix(testData);
561         OpenMapRealMatrix m1 = m.copy();
562         OpenMapRealMatrix mt = (OpenMapRealMatrix) m.transpose();
563         Assert.assertTrue(m.hashCode() != mt.hashCode());
564         Assert.assertEquals(m.hashCode(), m1.hashCode());
565         Assert.assertEquals(m, m);
566         Assert.assertEquals(m, m1);
567         Assert.assertNotEquals(m, null);
568         Assert.assertNotEquals(m, mt);
569         Assert.assertNotEquals(m, createSparseMatrix(bigSingular));
570     }
571 
572     @Test
573     public void testToString() {
574         OpenMapRealMatrix m = createSparseMatrix(testData);
575         Assert.assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
576             m.toString());
577         m = new OpenMapRealMatrix(1, 1);
578         Assert.assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());
579     }
580 
581     @Test
582     public void testSetSubMatrix() {
583         OpenMapRealMatrix m = createSparseMatrix(testData);
584         m.setSubMatrix(detData2, 1, 1);
585         RealMatrix expected = createSparseMatrix(new double[][] {
586                 { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 3.0 }, { 1.0, 2.0, 4.0 } });
587         Assert.assertEquals(expected, m);
588 
589         m.setSubMatrix(detData2, 0, 0);
590         expected = createSparseMatrix(new double[][] {
591                 { 1.0, 3.0, 3.0 }, { 2.0, 4.0, 3.0 }, { 1.0, 2.0, 4.0 } });
592         Assert.assertEquals(expected, m);
593 
594         m.setSubMatrix(testDataPlus2, 0, 0);
595         expected = createSparseMatrix(new double[][] {
596                 { 3.0, 4.0, 5.0 }, { 4.0, 7.0, 5.0 }, { 3.0, 2.0, 10.0 } });
597         Assert.assertEquals(expected, m);
598 
599         // javadoc example
600         OpenMapRealMatrix matrix =
601             createSparseMatrix(new double[][] {
602         { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 1, 2 } });
603         matrix.setSubMatrix(new double[][] { { 3, 4 }, { 5, 6 } }, 1, 1);
604         expected = createSparseMatrix(new double[][] {
605                 { 1, 2, 3, 4 }, { 5, 3, 4, 8 }, { 9, 5, 6, 2 } });
606         Assert.assertEquals(expected, matrix);
607 
608         // dimension overflow
609         try {
610             m.setSubMatrix(testData, 1, 1);
611             Assert.fail("expecting OutOfRangeException");
612         } catch (OutOfRangeException e) {
613             // expected
614         }
615         // dimension underflow
616         try {
617             m.setSubMatrix(testData, -1, 1);
618             Assert.fail("expecting OutOfRangeException");
619         } catch (OutOfRangeException e) {
620             // expected
621         }
622         try {
623             m.setSubMatrix(testData, 1, -1);
624             Assert.fail("expecting OutOfRangeException");
625         } catch (OutOfRangeException e) {
626             // expected
627         }
628 
629         // null
630         try {
631             m.setSubMatrix(null, 1, 1);
632             Assert.fail("expecting NullArgumentException");
633         } catch (NullArgumentException e) {
634             // expected
635         }
636         try {
637             new OpenMapRealMatrix(0, 0);
638             Assert.fail("expecting MathIllegalArgumentException");
639         } catch (MathIllegalArgumentException e) {
640             // expected
641         }
642 
643         // ragged
644         try {
645             m.setSubMatrix(new double[][] { { 1 }, { 2, 3 } }, 0, 0);
646             Assert.fail("expecting MathIllegalArgumentException");
647         } catch (MathIllegalArgumentException e) {
648             // expected
649         }
650 
651         // empty
652         try {
653             m.setSubMatrix(new double[][] { {} }, 0, 0);
654             Assert.fail("expecting MathIllegalArgumentException");
655         } catch (MathIllegalArgumentException e) {
656             // expected
657         }
658     }
659 
660     @Test
661     public void testSerial()  {
662         OpenMapRealMatrix m = createSparseMatrix(testData);
663         Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
664     }
665 
666     // --------------- -----------------Protected methods
667 
668     /** verifies that two matrices are close (1-norm) */
669     protected void assertClose(String msg, RealMatrix m, RealMatrix n,
670             double tolerance) {
671         Assert.assertTrue(msg, m.subtract(n).getNorm() < tolerance);
672     }
673 
674     /** verifies that two vectors are close (sup norm) */
675     protected void assertClose(String msg, double[] m, double[] n,
676             double tolerance) {
677         if (m.length != n.length) {
678             Assert.fail("vectors not same length");
679         }
680         for (int i = 0; i < m.length; i++) {
681             Assert.assertEquals(msg + " " + i + " elements differ", m[i], n[i],
682                     tolerance);
683         }
684     }
685 
686     private OpenMapRealMatrix createSparseMatrix(double[][] data) {
687         OpenMapRealMatrix matrix = new OpenMapRealMatrix(data.length, data[0].length);
688         for (int row = 0; row < data.length; row++) {
689             for (int col = 0; col < data[row].length; col++) {
690                 matrix.setEntry(row, col, data[row][col]);
691             }
692         }
693         return matrix;
694     }
695 }