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.Test;
20
19
import junit.framework.TestCase;
21
import junit.framework.TestSuite;
23
21
import org.apache.commons.math.TestUtils;
26
24
* Test cases for the {@link OpenMapRealMatrix} class.
28
* @version $Revision: 790243 $ $Date: 2008-11-07 06:48:13 -0800 (Fri, 07 Nov
26
* @version $Revision: 902201 $ $Date: 2008-11-07 06:48:13 -0800 (Fri, 07 Nov
31
29
public final class SparseRealMatrixTest extends TestCase {
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),
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);
276
268
/** test transpose */
277
269
public void testTranspose() {
279
RealMatrix m = createSparseMatrix(testData);
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());
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());
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 }));
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());
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}}",
560
552
m = new OpenMapRealMatrix(1, 1);
561
553
assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());
579
571
assertEquals(expected, m);
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[][] {