~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/SparseRealMatrixTest.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.Test;
20
19
import junit.framework.TestCase;
21
 
import junit.framework.TestSuite;
22
20
 
23
21
import org.apache.commons.math.TestUtils;
24
22
 
25
23
/**
26
24
 * Test cases for the {@link OpenMapRealMatrix} class.
27
 
 * 
28
 
 * @version $Revision: 790243 $ $Date: 2008-11-07 06:48:13 -0800 (Fri, 07 Nov
 
25
 *
 
26
 * @version $Revision: 902201 $ $Date: 2008-11-07 06:48:13 -0800 (Fri, 07 Nov
29
27
 *          2008) $
30
28
 */
31
29
public final class SparseRealMatrixTest extends TestCase {
108
106
        super(name);
109
107
    }
110
108
 
111
 
    public static Test suite() {
112
 
        TestSuite suite = new TestSuite(SparseRealMatrixTest.class);
113
 
        suite.setName("SparseRealMatrix Tests");
114
 
        return suite;
115
 
    }
116
 
 
117
109
    /** test dimensions */
118
110
    public void testDimensions() {
119
111
        OpenMapRealMatrix m = createSparseMatrix(testData);
146
138
        RealMatrix mPlusMInv = m.add(mInv);
147
139
        for (int row = 0; row < m.getRowDimension(); row++) {
148
140
            for (int col = 0; col < m.getColumnDimension(); col++) {
149
 
                assertEquals("sum entry entry", 
150
 
                    mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col), 
 
141
                assertEquals("sum entry entry",
 
142
                    mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col),
151
143
                    entryTolerance);
152
144
            }
153
145
        }
242
234
    /** test sclarAdd */
243
235
    public void testScalarAdd() {
244
236
        RealMatrix m = createSparseMatrix(testData);
245
 
        assertClose("scalar add", createSparseMatrix(testDataPlus2), 
 
237
        assertClose("scalar add", createSparseMatrix(testDataPlus2),
246
238
            m.scalarAdd(2d), entryTolerance);
247
239
    }
248
240
 
275
267
 
276
268
    /** test transpose */
277
269
    public void testTranspose() {
278
 
        
279
 
        RealMatrix m = createSparseMatrix(testData); 
 
270
 
 
271
        RealMatrix m = createSparseMatrix(testData);
280
272
        RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
281
273
        RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getSolver().getInverse();
282
274
        assertClose("inverse-transpose", mIT, mTI, normTolerance);
368
360
        assertEquals(2, p.getRowDimension());
369
361
        assertEquals(2, p.getColumnDimension());
370
362
        // Invert p
371
 
        RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse(); 
 
363
        RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
372
364
        assertEquals(2, pInverse.getRowDimension());
373
365
        assertEquals(2, pInverse.getColumnDimension());
374
366
 
401
393
        assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
402
394
        assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
403
395
        assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
404
 
        assertEquals("Rows02Cols13", mRows02Cols13, 
 
396
        assertEquals("Rows02Cols13", mRows02Cols13,
405
397
            m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 }));
406
 
        assertEquals("Rows03Cols12", mRows03Cols12, 
 
398
        assertEquals("Rows03Cols12", mRows03Cols12,
407
399
            m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 }));
408
 
        assertEquals("Rows03Cols123", mRows03Cols123, 
 
400
        assertEquals("Rows03Cols123", mRows03Cols123,
409
401
            m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
410
 
        assertEquals("Rows20Cols123", mRows20Cols123, 
 
402
        assertEquals("Rows20Cols123", mRows20Cols123,
411
403
            m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
412
 
        assertEquals("Rows31Cols31", mRows31Cols31, 
 
404
        assertEquals("Rows31Cols31", mRows31Cols31,
413
405
            m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
414
 
        assertEquals("Rows31Cols31", mRows31Cols31, 
 
406
        assertEquals("Rows31Cols31", mRows31Cols31,
415
407
            m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
416
408
 
417
409
        try {
542
534
 
543
535
    public void testEqualsAndHashCode() {
544
536
        OpenMapRealMatrix m = createSparseMatrix(testData);
545
 
        OpenMapRealMatrix m1 = (OpenMapRealMatrix) m.copy();
 
537
        OpenMapRealMatrix m1 = m.copy();
546
538
        OpenMapRealMatrix mt = (OpenMapRealMatrix) m.transpose();
547
539
        assertTrue(m.hashCode() != mt.hashCode());
548
540
        assertEquals(m.hashCode(), m1.hashCode());
555
547
 
556
548
    public void testToString() {
557
549
        OpenMapRealMatrix m = createSparseMatrix(testData);
558
 
        assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", 
 
550
        assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
559
551
            m.toString());
560
552
        m = new OpenMapRealMatrix(1, 1);
561
553
        assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());
579
571
        assertEquals(expected, m);
580
572
 
581
573
        // javadoc example
582
 
        OpenMapRealMatrix matrix = 
583
 
            createSparseMatrix(new double[][] { 
 
574
        OpenMapRealMatrix matrix =
 
575
            createSparseMatrix(new double[][] {
584
576
        { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 1, 2 } });
585
577
        matrix.setSubMatrix(new double[][] { { 3, 4 }, { 5, 6 } }, 1, 1);
586
578
        expected = createSparseMatrix(new double[][] {
664
656
                    tolerance);
665
657
        }
666
658
    }
667
 
    
 
659
 
668
660
    private OpenMapRealMatrix createSparseMatrix(double[][] data) {
669
661
        OpenMapRealMatrix matrix = new OpenMapRealMatrix(data.length, data[0].length);
670
662
        for (int row = 0; row < data.length; row++) {