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