5
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
6
* (the "License"); you may not use this file except in compliance with
7
7
* the License. You may obtain a copy of the License at
9
9
* http://www.apache.org/licenses/LICENSE-2.0
11
11
* Unless required by applicable law or agreed to in writing, software
12
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
17
package org.apache.commons.math.linear;
19
import junit.framework.TestCase;
19
21
import org.apache.commons.math.Field;
20
22
import org.apache.commons.math.fraction.Fraction;
21
23
import org.apache.commons.math.fraction.FractionConversionException;
22
24
import org.apache.commons.math.fraction.FractionField;
24
import junit.framework.Test;
25
import junit.framework.TestCase;
26
import junit.framework.TestSuite;
29
27
* Test cases for the {@link SparseFieldMatrix} class.
31
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
29
* @version $Revision: 902201 $ $Date: 2010-01-22 13:18:16 -0500 (Fri, 22 Jan 2010) $
33
31
public class SparseFieldMatrixTest extends TestCase {
34
32
// 3 x 3 identity matrix
165
157
FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
166
158
for (int row = 0; row < m.getRowDimension(); row++) {
167
159
for (int col = 0; col < m.getColumnDimension(); col++) {
168
assertEquals("sum entry entry",
169
mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(),
160
assertEquals("sum entry entry",
161
mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(),
254
246
/** test sclarAdd */
255
247
public void testScalarAdd() {
256
248
FieldMatrix<Fraction> m = createSparseMatrix(testData);
257
assertClose("scalar add", createSparseMatrix(testDataPlus2),
249
assertClose("scalar add", createSparseMatrix(testDataPlus2),
258
250
m.scalarAdd(new Fraction(2)), entryTolerance);
288
280
/** test transpose */
289
281
public void testTranspose() {
291
FieldMatrix<Fraction> m = createSparseMatrix(testData);
283
FieldMatrix<Fraction> m = createSparseMatrix(testData);
292
284
FieldMatrix<Fraction> mIT = new FieldLUDecompositionImpl<Fraction>(m).getSolver().getInverse().transpose();
293
285
FieldMatrix<Fraction> mTI = new FieldLUDecompositionImpl<Fraction>(m.transpose()).getSolver().getInverse();
294
286
assertClose("inverse-transpose", mIT, mTI, normTolerance);
380
372
assertEquals(2, p.getRowDimension());
381
373
assertEquals(2, p.getColumnDimension());
383
FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
375
FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
384
376
assertEquals(2, pInverse.getRowDimension());
385
377
assertEquals(2, pInverse.getColumnDimension());
413
405
assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
414
406
assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
415
407
assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
416
assertEquals("Rows02Cols13", mRows02Cols13,
408
assertEquals("Rows02Cols13", mRows02Cols13,
417
409
m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 }));
418
assertEquals("Rows03Cols12", mRows03Cols12,
410
assertEquals("Rows03Cols12", mRows03Cols12,
419
411
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 }));
420
assertEquals("Rows03Cols123", mRows03Cols123,
412
assertEquals("Rows03Cols123", mRows03Cols123,
421
413
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
422
assertEquals("Rows20Cols123", mRows20Cols123,
414
assertEquals("Rows20Cols123", mRows20Cols123,
423
415
m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
424
assertEquals("Rows31Cols31", mRows31Cols31,
416
assertEquals("Rows31Cols31", mRows31Cols31,
425
417
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
426
assertEquals("Rows31Cols31", mRows31Cols31,
418
assertEquals("Rows31Cols31", mRows31Cols31,
427
419
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
568
560
/* Disable for now
569
561
public void testToString() {
570
562
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
571
assertEquals("SparseFieldMatrix<Fraction>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
563
assertEquals("SparseFieldMatrix<Fraction>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
573
565
m = new SparseFieldMatrix<Fraction>(field, 1, 1);
574
566
assertEquals("SparseFieldMatrix<Fraction>{{0.0}}", m.toString());
593
585
assertEquals(expected, m);
595
587
// javadoc example
596
SparseFieldMatrix<Fraction> matrix =
597
createSparseMatrix(new Fraction[][] {
588
SparseFieldMatrix<Fraction> matrix =
589
createSparseMatrix(new Fraction[][] {
598
590
{ new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) }, { new Fraction(9), new Fraction(0), new Fraction(1), new Fraction(2) } });
599
591
matrix.setSubMatrix(new Fraction[][] { { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }, 1, 1);
600
592
expected = createSparseMatrix(new Fraction[][] {