~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/linear/SparseFieldMatrixTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
8
 
 * 
 
8
 *
9
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 * 
 
10
 *
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.
16
16
 */
17
17
package org.apache.commons.math.linear;
18
18
 
 
19
import junit.framework.TestCase;
 
20
 
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;
23
25
 
24
 
import junit.framework.Test;
25
 
import junit.framework.TestCase;
26
 
import junit.framework.TestSuite;
27
 
 
28
26
/**
29
27
 * Test cases for the {@link SparseFieldMatrix} class.
30
28
 *
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) $
32
30
 */
33
31
public class SparseFieldMatrixTest extends TestCase {
34
32
    // 3 x 3 identity matrix
123
121
        } catch (FractionConversionException e) {
124
122
            // ignore, can't happen
125
123
        }
126
 
        
127
 
                
128
 
    }
129
 
 
130
 
    public static Test suite() {
131
 
        TestSuite suite = new TestSuite(SparseFieldMatrixTest.class);
132
 
        suite.setName("SparseFieldMatrix<Fraction> Tests");
133
 
        return suite;
 
124
 
 
125
 
134
126
    }
135
127
 
136
128
    /** test dimensions */
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(),
170
162
                    entryTolerance);
171
163
            }
172
164
        }
184
176
        }
185
177
    }
186
178
 
187
 
    
 
179
 
188
180
    /** test m-n = m + -n */
189
181
    public void testPlusMinus() {
190
182
        SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
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);
259
251
    }
260
252
 
287
279
 
288
280
    /** test transpose */
289
281
    public void testTranspose() {
290
 
        
291
 
        FieldMatrix<Fraction> m = createSparseMatrix(testData); 
 
282
 
 
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());
382
374
        // Invert p
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());
386
378
 
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 }));
428
420
 
429
421
        try {
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}}",
572
564
            m.toString());
573
565
        m = new SparseFieldMatrix<Fraction>(field, 1, 1);
574
566
        assertEquals("SparseFieldMatrix<Fraction>{{0.0}}", m.toString());
593
585
        assertEquals(expected, m);
594
586
 
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[][] {
663
655
            for(int j=0; j < m.getColumnDimension(); j++){
664
656
                assertEquals(msg, m.getEntry(i,j).doubleValue(), n.getEntry(i,j).doubleValue(), tolerance);
665
657
            }
666
 
            
 
658
 
667
659
        }
668
660
    }
669
661
 
678
670
                    tolerance);
679
671
        }
680
672
    }
681
 
    
 
673
 
682
674
    private SparseFieldMatrix<Fraction> createSparseMatrix(Fraction[][] data) {
683
675
        SparseFieldMatrix<Fraction> matrix = new SparseFieldMatrix<Fraction>(field, data.length, data[0].length);
684
676
        for (int row = 0; row < data.length; row++) {