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