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