You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/02/13 15:38:50 UTC
svn commit: r744126 [3/3] - in
/commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear:
RecursiveLayoutRealMatrix.java RecursiveLayoutRealMatrixTest.java
Propchange: commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrix.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java?rev=744126&view=auto
==============================================================================
--- commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java (added)
+++ commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java Fri Feb 13 14:38:48 2009
@@ -0,0 +1,1242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.linear;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test cases for the {@link RecursiveLayoutRealMatrix} class.
+ *
+ * @version $Revision$ $Date$
+ */
+
+public final class RecursiveLayoutRealMatrixTest extends TestCase {
+
+ // 3 x 3 identity matrix
+ protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
+
+ // Test data for group operations
+ protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
+ protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
+ protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
+ protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
+ {-1d,0d,-8d} };
+ protected double[] testDataRow1 = {1d,2d,3d};
+ protected double[] testDataCol3 = {3d,3d,8d};
+ protected double[][] testDataInv =
+ { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
+ protected double[] preMultTest = {8,12,33};
+ protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
+ protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
+ protected double[][] testDataPlusInv =
+ { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
+
+ // lu decomposition tests
+ protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
+ protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d},
+ {0.33333333333333,0d,0.33333333333333} };
+
+ // singular matrices
+ protected double[][] singular = { {2d,3d}, {2d,3d} };
+ protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
+ {7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd
+ protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
+ protected double[][] detData2 = { {1d, 3d}, {2d, 4d}};
+
+ // vectors
+ protected double[] testVector = {1,2,3};
+ protected double[] testVector2 = {1,2,3,4};
+
+ // submatrix accessor tests
+ protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5},
+ {2, 4, 6, 8}, {4, 5, 6, 7}};
+ // array selections
+ protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}};
+ protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}};
+ protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}};
+ // effective permutations
+ protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}};
+ protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}};
+ // contiguous ranges
+ protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}};
+ protected double[][] subRows23Cols00 = {{2} , {4}};
+ protected double[][] subRows00Cols33 = {{4}};
+ // row matrices
+ protected double[][] subRow0 = {{1,2,3,4}};
+ protected double[][] subRow3 = {{4,5,6,7}};
+ // column matrices
+ protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}};
+ protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}};
+
+ // tolerances
+ protected double entryTolerance = 10E-16;
+ protected double normTolerance = 10E-14;
+
+ public RecursiveLayoutRealMatrixTest(String name) {
+ super(name);
+ }
+
+ public void setUp() {
+
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(RecursiveLayoutRealMatrixTest.class);
+ suite.setName("RecursiveLayoutRealMatrix Tests");
+ return suite;
+ }
+
+ /** test dimensions */
+ public void testDimensions() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testData2);
+ assertEquals("testData row dimension",3,m.getRowDimension());
+ assertEquals("testData column dimension",3,m.getColumnDimension());
+ assertTrue("testData is square",m.isSquare());
+ assertEquals("testData2 row dimension",m2.getRowDimension(),2);
+ assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
+ assertTrue("testData2 is not square",!m2.isSquare());
+ }
+
+ /** test copy functions */
+ public void testCopyFunctions() {
+ Random r = new Random(66636328996002l);
+ RecursiveLayoutRealMatrix m1 = createRandomMatrix(r, 47, 83);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(m1.getData());
+ assertEquals(m1, m2);
+ RecursiveLayoutRealMatrix m3 = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m4 = new RecursiveLayoutRealMatrix(m3.getData());
+ assertEquals(m3, m4);
+ }
+
+ /** test add */
+ public void testAdd() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix mInv = new RecursiveLayoutRealMatrix(testDataInv);
+ RealMatrix mPlusMInv = m.add(mInv);
+ double[][] sumEntries = mPlusMInv.getData();
+ for (int row = 0; row < m.getRowDimension(); row++) {
+ for (int col = 0; col < m.getColumnDimension(); col++) {
+ assertEquals("sum entry entry",
+ testDataPlusInv[row][col],sumEntries[row][col],
+ entryTolerance);
+ }
+ }
+ }
+
+ /** test add failure */
+ public void testAddFail() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testData2);
+ try {
+ m.add(m2);
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ }
+
+ /** test norm */
+ public void testNorm() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testData2);
+ assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
+ assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
+ }
+
+ /** test Frobenius norm */
+ public void testFrobeniusNorm() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testData2);
+ assertEquals("testData Frobenius norm", Math.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
+ assertEquals("testData2 Frobenius norm", Math.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
+ }
+
+ /** test m-n = m + -n */
+ public void testPlusMinus() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testDataInv);
+ assertClose(m.subtract(m2), m2.scalarMultiply(-1d).add(m), entryTolerance);
+ try {
+ m.subtract(new RecursiveLayoutRealMatrix(testData2));
+ fail("Expecting illegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ }
+
+ /** test multiply */
+ public void testMultiply() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix mInv = new RecursiveLayoutRealMatrix(testDataInv);
+ RecursiveLayoutRealMatrix identity = new RecursiveLayoutRealMatrix(id);
+ RecursiveLayoutRealMatrix m2 = new RecursiveLayoutRealMatrix(testData2);
+ assertClose(m.multiply(mInv), identity, entryTolerance);
+ assertClose(mInv.multiply(m), identity, entryTolerance);
+ assertClose(m.multiply(identity), m, entryTolerance);
+ assertClose(identity.multiply(mInv), mInv, entryTolerance);
+ assertClose(m2.multiply(identity), m2, entryTolerance);
+ try {
+ m.multiply(new RecursiveLayoutRealMatrix(bigSingular));
+ fail("Expecting illegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testSeveralBlocks() {
+
+ RealMatrix m = new RecursiveLayoutRealMatrix(35, 71);
+ for (int i = 0; i < m.getRowDimension(); ++i) {
+ for (int j = 0; j < m.getColumnDimension(); ++j) {
+ m.setEntry(i, j, i + j / 1024.0);
+ }
+ }
+
+ RealMatrix mT = m.transpose();
+ assertEquals(m.getRowDimension(), mT.getColumnDimension());
+ assertEquals(m.getColumnDimension(), mT.getRowDimension());
+ for (int i = 0; i < mT.getRowDimension(); ++i) {
+ for (int j = 0; j < mT.getColumnDimension(); ++j) {
+ assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix mPm = m.add(m);
+ for (int i = 0; i < mPm.getRowDimension(); ++i) {
+ for (int j = 0; j < mPm.getColumnDimension(); ++j) {
+ assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix mPmMm = mPm.subtract(m);
+ for (int i = 0; i < mPmMm.getRowDimension(); ++i) {
+ for (int j = 0; j < mPmMm.getColumnDimension(); ++j) {
+ assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix mTm = mT.multiply(m);
+ for (int i = 0; i < mTm.getRowDimension(); ++i) {
+ for (int j = 0; j < mTm.getColumnDimension(); ++j) {
+ double sum = 0;
+ for (int k = 0; k < mT.getColumnDimension(); ++k) {
+ sum += (k + i / 1024.0) * (k + j / 1024.0);
+ }
+ assertEquals(sum, mTm.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix mmT = m.multiply(mT);
+ for (int i = 0; i < mmT.getRowDimension(); ++i) {
+ for (int j = 0; j < mmT.getColumnDimension(); ++j) {
+ double sum = 0;
+ for (int k = 0; k < m.getColumnDimension(); ++k) {
+ sum += (i + k / 1024.0) * (j + k / 1024.0);
+ }
+ assertEquals(sum, mmT.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix sub1 = m.getSubMatrix(2, 9, 5, 20);
+ for (int i = 0; i < sub1.getRowDimension(); ++i) {
+ for (int j = 0; j < sub1.getColumnDimension(); ++j) {
+ assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix sub2 = m.getSubMatrix(10, 12, 3, 70);
+ for (int i = 0; i < sub2.getRowDimension(); ++i) {
+ for (int j = 0; j < sub2.getColumnDimension(); ++j) {
+ assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix sub3 = m.getSubMatrix(30, 34, 0, 5);
+ for (int i = 0; i < sub3.getRowDimension(); ++i) {
+ for (int j = 0; j < sub3.getColumnDimension(); ++j) {
+ assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0);
+ }
+ }
+
+ RealMatrix sub4 = m.getSubMatrix(30, 32, 62, 65);
+ for (int i = 0; i < sub4.getRowDimension(); ++i) {
+ for (int j = 0; j < sub4.getColumnDimension(); ++j) {
+ assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0);
+ }
+ }
+
+ }
+
+ //Additional Test for RecursiveLayoutRealMatrixTest.testMultiply
+
+ private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}};
+ private double[][] d4 = new double[][] {{1},{2},{3},{4}};
+ private double[][] d5 = new double[][] {{30},{70}};
+
+ public void testMultiply2() {
+ RealMatrix m3 = new RecursiveLayoutRealMatrix(d3);
+ RealMatrix m4 = new RecursiveLayoutRealMatrix(d4);
+ RealMatrix m5 = new RecursiveLayoutRealMatrix(d5);
+ assertClose(m3.multiply(m4), m5, entryTolerance);
+ }
+
+ /** test trace */
+ public void testTrace() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(id);
+ assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
+ m = new RecursiveLayoutRealMatrix(testData2);
+ try {
+ m.getTrace();
+ fail("Expecting NonSquareMatrixException");
+ } catch (NonSquareMatrixException ex) {
+ ;
+ }
+ }
+
+ /** test scalarAdd */
+ public void testScalarAdd() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ assertClose(new RecursiveLayoutRealMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance);
+ }
+
+ /** test operate */
+ public void testOperate() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(id);
+ assertClose(testVector, m.operate(testVector), entryTolerance);
+ assertClose(testVector, m.operate(new RealVectorImpl(testVector)).getData(), entryTolerance);
+ m = new RecursiveLayoutRealMatrix(bigSingular);
+ try {
+ m.operate(testVector);
+ fail("Expecting illegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ }
+
+ public void testMultiplyMedium() {
+ RealMatrix m1 = new RecursiveLayoutRealMatrix(
+ new double[][] {
+ { 80, 45, 13, 77, -82 },
+ { -90, 33, 98, 80, 74 },
+ { 24, -37, 36, -8, -69 },
+ { -74, 2, 32, -67, -65 },
+ { -29, -81, 44, 54, -65 },
+ { 17, 58, -36, -98, 25 },
+ { 48, -64, -95, -75, 34 }
+ });
+ RealMatrix m2 = new RecursiveLayoutRealMatrix(
+ new double[][] {
+ { 81, 58, 70, 18, 5, -57 },
+ { -54, 33, 87, 68, -22, 73 },
+ { -78, -5, 34, -7, -3, -31 },
+ { -16, -82, -68, 7, 10, -47 },
+ { 51, 4, 92, 15, 32, -51 }
+ });
+ RealMatrix m1m2 = m1.multiply(m2);
+ RealMatrix reference = new RecursiveLayoutRealMatrix(
+ new double[][]{
+ { -2378, -582, -2823, 3718, -2483, -1115 },
+ { -14222, -10885, 1271, 1608, 1698, -3033 },
+ { -2257, 371, -6119, -3427, -1462, -1290 },
+ { -10841, 848, -5342, -2864, -3260, 9836 },
+ { -5586, -9263, -17233, -6935, -35, -4847 },
+ { 3896, 11216, 13976, 4191, -1263, 7712 },
+ { 17688, 7433, 2790, -2838, 2271, -2672 }
+ });
+ assertEquals(0, m1m2.subtract(reference).getNorm(), 0.0);
+ }
+
+ public void testOperateLarge() {
+ int testBlockSize = 64;
+ int p = (7 * testBlockSize) / 2;
+ int q = (5 * testBlockSize) / 2;
+ int r = 3 * testBlockSize;
+ Random random = new Random(111007463902334l);
+ RealMatrix m1 = createRandomMatrix(random, p, q);
+ RealMatrix m2 = createRandomMatrix(random, q, r);
+ RealMatrix m1m2 = m1.multiply(m2);
+ for (int i = 0; i < r; ++i) {
+ checkArrays(m1m2.getColumn(i), m1.operate(m2.getColumn(i)));
+ }
+ }
+
+ public void testOperatePremultiplyLarge() {
+ int testBlockSize = 64;
+ int p = (7 * testBlockSize) / 2;
+ int q = (5 * testBlockSize) / 2;
+ int r = 3 * testBlockSize;
+ Random random = new Random(111007463902334l);
+ RealMatrix m1 = createRandomMatrix(random, p, q);
+ RealMatrix m2 = createRandomMatrix(random, q, r);
+ RealMatrix m1m2 = m1.multiply(m2);
+ for (int i = 0; i < p; ++i) {
+ checkArrays(m1m2.getRow(i), m2.preMultiply(m1.getRow(i)));
+ }
+ }
+
+ /** test issue MATH-209 */
+ public void testMath209() {
+ RealMatrix a = new RecursiveLayoutRealMatrix(new double[][] {
+ { 1, 2 }, { 3, 4 }, { 5, 6 }
+ });
+ double[] b = a.operate(new double[] { 1, 1 });
+ assertEquals(a.getRowDimension(), b.length);
+ assertEquals( 3.0, b[0], 1.0e-12);
+ assertEquals( 7.0, b[1], 1.0e-12);
+ assertEquals(11.0, b[2], 1.0e-12);
+ }
+
+ /** test transpose */
+ public void testTranspose() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
+ RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getSolver().getInverse();
+ assertClose(mIT, mTI, normTolerance);
+ m = new RecursiveLayoutRealMatrix(testData2);
+ RealMatrix mt = new RecursiveLayoutRealMatrix(testData2T);
+ assertClose(mt, m.transpose(), normTolerance);
+ }
+
+ /** test preMultiply by vector */
+ public void testPremultiplyVector() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ assertClose(m.preMultiply(testVector), preMultTest, normTolerance);
+ assertClose(m.preMultiply(new RealVectorImpl(testVector).getData()),
+ preMultTest, normTolerance);
+ m = new RecursiveLayoutRealMatrix(bigSingular);
+ try {
+ m.preMultiply(testVector);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ }
+
+ public void testPremultiply() {
+ RealMatrix m3 = new RecursiveLayoutRealMatrix(d3);
+ RealMatrix m4 = new RecursiveLayoutRealMatrix(d4);
+ RealMatrix m5 = new RecursiveLayoutRealMatrix(d5);
+ assertClose(m4.preMultiply(m3), m5, entryTolerance);
+
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix mInv = new RecursiveLayoutRealMatrix(testDataInv);
+ RecursiveLayoutRealMatrix identity = new RecursiveLayoutRealMatrix(id);
+ assertClose(m.preMultiply(mInv), identity, entryTolerance);
+ assertClose(mInv.preMultiply(m), identity, entryTolerance);
+ assertClose(m.preMultiply(identity), m, entryTolerance);
+ assertClose(identity.preMultiply(mInv), mInv, entryTolerance);
+ try {
+ m.preMultiply(new RecursiveLayoutRealMatrix(bigSingular));
+ fail("Expecting illegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ }
+
+ public void testGetVectors() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ assertClose(m.getRow(0), testDataRow1, entryTolerance);
+ assertClose(m.getColumn(2), testDataCol3, entryTolerance);
+ try {
+ m.getRow(10);
+ fail("expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ ;
+ }
+ try {
+ m.getColumn(-1);
+ fail("expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ ;
+ }
+ }
+
+ public void testGetEntry() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
+ try {
+ m.getEntry(10, 4);
+ fail ("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ /** test examples in user guide */
+ public void testExamples() {
+ // Create a real matrix with two rows and three columns
+ double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
+ RealMatrix m = new RecursiveLayoutRealMatrix(matrixData);
+ // One more with three rows, two columns
+ double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
+ RealMatrix n = new RecursiveLayoutRealMatrix(matrixData2);
+ // Now multiply m by n
+ RealMatrix p = m.multiply(n);
+ assertEquals(2, p.getRowDimension());
+ assertEquals(2, p.getColumnDimension());
+ // Invert p
+ RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
+ assertEquals(2, pInverse.getRowDimension());
+ assertEquals(2, pInverse.getColumnDimension());
+
+ // Solve example
+ double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
+ RealMatrix coefficients = new RecursiveLayoutRealMatrix(coefficientsData);
+ double[] constants = {1, -2, 1};
+ double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
+ assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12);
+ assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12);
+ assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
+
+ }
+
+ // test submatrix accessors
+ public void testGetSubMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0, false);
+ checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3, false);
+ checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3, false);
+ checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
+ checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
+ checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
+ checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkGetSubMatrix(m, null, 1, 0, 2, 4, true);
+ checkGetSubMatrix(m, null, -1, 1, 2, 2, true);
+ checkGetSubMatrix(m, null, 1, 0, 2, 2, true);
+ checkGetSubMatrix(m, null, 1, 0, 2, 4, true);
+ checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 }, true);
+ checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 }, true);
+ }
+
+ private void checkGetSubMatrix(RealMatrix m, double[][] reference,
+ int startRow, int endRow, int startColumn, int endColumn,
+ boolean mustFail) {
+ try {
+ RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
+ assertEquals(new RecursiveLayoutRealMatrix(reference), sub);
+ if (mustFail) {
+ fail("Expecting MatrixIndexException");
+ }
+ } catch (MatrixIndexException e) {
+ if (!mustFail) {
+ throw e;
+ }
+ }
+ }
+
+ private void checkGetSubMatrix(RealMatrix m, double[][] reference,
+ int[] selectedRows, int[] selectedColumns,
+ boolean mustFail) {
+ try {
+ RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns);
+ assertEquals(new RecursiveLayoutRealMatrix(reference), sub);
+ if (mustFail) {
+ fail("Expecting MatrixIndexException");
+ }
+ } catch (MatrixIndexException e) {
+ if (!mustFail) {
+ throw e;
+ }
+ }
+ }
+
+ public void testGetSetMatrixLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ RealMatrix sub = new RecursiveLayoutRealMatrix(n - 4, n - 4).scalarAdd(1);
+
+ m.setSubMatrix(sub.getData(), 2, 2);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3));
+
+ }
+
+ public void testCopySubMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0, false);
+ checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3, false);
+ checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3, false);
+ checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
+ checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
+ checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
+ checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+
+ checkCopy(m, null, 1, 0, 2, 4, true);
+ checkCopy(m, null, -1, 1, 2, 2, true);
+ checkCopy(m, null, 1, 0, 2, 2, true);
+ checkCopy(m, null, 1, 0, 2, 4, true);
+ checkCopy(m, null, new int[] {}, new int[] { 0 }, true);
+ checkCopy(m, null, new int[] { 0 }, new int[] { 4 }, true);
+ }
+
+ private void checkCopy(RealMatrix m, double[][] reference,
+ int startRow, int endRow, int startColumn, int endColumn,
+ boolean mustFail) {
+ try {
+ double[][] sub = (reference == null) ?
+ new double[1][1] :
+ new double[reference.length][reference[0].length];
+ m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
+ assertEquals(new RecursiveLayoutRealMatrix(reference), new RecursiveLayoutRealMatrix(sub));
+ if (mustFail) {
+ fail("Expecting MatrixIndexException");
+ }
+ } catch (MatrixIndexException e) {
+ if (!mustFail) {
+ throw e;
+ }
+ }
+ }
+
+ private void checkCopy(RealMatrix m, double[][] reference,
+ int[] selectedRows, int[] selectedColumns,
+ boolean mustFail) {
+ try {
+ double[][] sub = (reference == null) ?
+ new double[1][1] :
+ new double[reference.length][reference[0].length];
+ m.copySubMatrix(selectedRows, selectedColumns, sub);
+ assertEquals(new RecursiveLayoutRealMatrix(reference), new RecursiveLayoutRealMatrix(sub));
+ if (mustFail) {
+ fail("Expecting MatrixIndexException");
+ }
+ } catch (MatrixIndexException e) {
+ if (!mustFail) {
+ throw e;
+ }
+ }
+ }
+
+ public void testGetRowMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealMatrix mRow0 = new RecursiveLayoutRealMatrix(subRow0);
+ RealMatrix mRow3 = new RecursiveLayoutRealMatrix(subRow3);
+ assertEquals("Row0", mRow0, m.getRowMatrix(0));
+ assertEquals("Row3", mRow3, m.getRowMatrix(3));
+ try {
+ m.getRowMatrix(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getRowMatrix(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetRowMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealMatrix mRow3 = new RecursiveLayoutRealMatrix(subRow3);
+ assertNotSame(mRow3, m.getRowMatrix(0));
+ m.setRowMatrix(0, mRow3);
+ assertEquals(mRow3, m.getRowMatrix(0));
+ try {
+ m.setRowMatrix(-1, mRow3);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setRowMatrix(0, m);
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetRowMatrixLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ RealMatrix sub = new RecursiveLayoutRealMatrix(1, n).scalarAdd(1);
+
+ m.setRowMatrix(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (i != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ assertEquals(sub, m.getRowMatrix(2));
+
+ }
+
+ public void testGetColumnMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealMatrix mColumn1 = new RecursiveLayoutRealMatrix(subColumn1);
+ RealMatrix mColumn3 = new RecursiveLayoutRealMatrix(subColumn3);
+ assertEquals(mColumn1, m.getColumnMatrix(1));
+ assertEquals(mColumn3, m.getColumnMatrix(3));
+ try {
+ m.getColumnMatrix(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getColumnMatrix(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetColumnMatrix() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealMatrix mColumn3 = new RecursiveLayoutRealMatrix(subColumn3);
+ assertNotSame(mColumn3, m.getColumnMatrix(1));
+ m.setColumnMatrix(1, mColumn3);
+ assertEquals(mColumn3, m.getColumnMatrix(1));
+ try {
+ m.setColumnMatrix(-1, mColumn3);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setColumnMatrix(0, m);
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetColumnMatrixLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ RealMatrix sub = new RecursiveLayoutRealMatrix(n, 1).scalarAdd(1);
+
+ m.setColumnMatrix(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (j != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ assertEquals(sub, m.getColumnMatrix(2));
+
+ }
+
+ public void testGetRowVector() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealVector mRow0 = new RealVectorImpl(subRow0[0]);
+ RealVector mRow3 = new RealVectorImpl(subRow3[0]);
+ assertEquals(mRow0, m.getRowVector(0));
+ assertEquals(mRow3, m.getRowVector(3));
+ try {
+ m.getRowVector(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getRowVector(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetRowVector() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealVector mRow3 = new RealVectorImpl(subRow3[0]);
+ assertNotSame(mRow3, m.getRowMatrix(0));
+ m.setRowVector(0, mRow3);
+ assertEquals(mRow3, m.getRowVector(0));
+ try {
+ m.setRowVector(-1, mRow3);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setRowVector(0, new RealVectorImpl(5));
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetRowVectorLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ RealVector sub = new RealVectorImpl(n, 1.0);
+
+ m.setRowVector(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (i != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ assertEquals(sub, m.getRowVector(2));
+
+ }
+
+ public void testGetColumnVector() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealVector mColumn1 = columnToVector(subColumn1);
+ RealVector mColumn3 = columnToVector(subColumn3);
+ assertEquals(mColumn1, m.getColumnVector(1));
+ assertEquals(mColumn3, m.getColumnVector(3));
+ try {
+ m.getColumnVector(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getColumnVector(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetColumnVector() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ RealVector mColumn3 = columnToVector(subColumn3);
+ assertNotSame(mColumn3, m.getColumnVector(1));
+ m.setColumnVector(1, mColumn3);
+ assertEquals(mColumn3, m.getColumnVector(1));
+ try {
+ m.setColumnVector(-1, mColumn3);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setColumnVector(0, new RealVectorImpl(5));
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetColumnVectorLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ RealVector sub = new RealVectorImpl(n, 1.0);
+
+ m.setColumnVector(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (j != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ assertEquals(sub, m.getColumnVector(2));
+
+ }
+
+ private RealVector columnToVector(double[][] column) {
+ double[] data = new double[column.length];
+ for (int i = 0; i < data.length; ++i) {
+ data[i] = column[i][0];
+ }
+ return new RealVectorImpl(data, false);
+ }
+
+ public void testGetRow() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ checkArrays(subRow0[0], m.getRow(0));
+ checkArrays(subRow3[0], m.getRow(3));
+ try {
+ m.getRow(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getRow(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetRow() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ assertTrue(subRow3[0][0] != m.getRow(0)[0]);
+ m.setRow(0, subRow3[0]);
+ checkArrays(subRow3[0], m.getRow(0));
+ try {
+ m.setRow(-1, subRow3[0]);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setRow(0, new double[5]);
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetRowLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ double[] sub = new double[n];
+ Arrays.fill(sub, 1.0);
+
+ m.setRow(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (i != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ checkArrays(sub, m.getRow(2));
+
+ }
+
+ public void testGetColumn() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ double[] mColumn1 = columnToArray(subColumn1);
+ double[] mColumn3 = columnToArray(subColumn3);
+ checkArrays(mColumn1, m.getColumn(1));
+ checkArrays(mColumn3, m.getColumn(3));
+ try {
+ m.getColumn(-1);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.getColumn(4);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ }
+
+ public void testSetColumn() {
+ RealMatrix m = new RecursiveLayoutRealMatrix(subTestData);
+ double[] mColumn3 = columnToArray(subColumn3);
+ assertTrue(mColumn3[0] != m.getColumn(1)[0]);
+ m.setColumn(1, mColumn3);
+ checkArrays(mColumn3, m.getColumn(1));
+ try {
+ m.setColumn(-1, mColumn3);
+ fail("Expecting MatrixIndexException");
+ } catch (MatrixIndexException ex) {
+ // expected
+ }
+ try {
+ m.setColumn(0, new double[5]);
+ fail("Expecting InvalidMatrixException");
+ } catch (InvalidMatrixException ex) {
+ // expected
+ }
+ }
+
+ public void testGetSetColumnLarge() {
+ int n = 3 * 64;
+ RealMatrix m = new RecursiveLayoutRealMatrix(n, n);
+ double[] sub = new double[n];
+ Arrays.fill(sub, 1.0);
+
+ m.setColumn(2, sub);
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < n; ++j) {
+ if (j != 2) {
+ assertEquals(0.0, m.getEntry(i, j), 0.0);
+ } else {
+ assertEquals(1.0, m.getEntry(i, j), 0.0);
+ }
+ }
+ }
+ checkArrays(sub, m.getColumn(2));
+
+ }
+
+ private double[] columnToArray(double[][] column) {
+ double[] data = new double[column.length];
+ for (int i = 0; i < data.length; ++i) {
+ data[i] = column[i][0];
+ }
+ return data;
+ }
+
+ private void checkArrays(double[] expected, double[] actual) {
+ assertEquals(expected.length, actual.length);
+ for (int i = 0; i < expected.length; ++i) {
+ assertEquals(expected[i], actual[i], 1.0e-9 * Math.abs(expected[i]));
+ }
+ }
+
+ public void testEqualsAndHashCode() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ RecursiveLayoutRealMatrix m1 = (RecursiveLayoutRealMatrix) m.copy();
+ RecursiveLayoutRealMatrix mt = (RecursiveLayoutRealMatrix) m.transpose();
+ assertTrue(m.hashCode() != mt.hashCode());
+ assertEquals(m.hashCode(), m1.hashCode());
+ assertEquals(m, m);
+ assertEquals(m, m1);
+ assertFalse(m.equals(null));
+ assertFalse(m.equals(mt));
+ assertFalse(m.equals(new RecursiveLayoutRealMatrix(bigSingular)));
+ }
+
+ public void testToString() {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ assertEquals("RecursiveLayoutRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
+ m.toString());
+ }
+
+ public void testSetSubMatrix() throws Exception {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(testData);
+ m.setSubMatrix(detData2,1,1);
+ RealMatrix expected = new RecursiveLayoutRealMatrix
+ (new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
+ assertEquals(expected, m);
+
+ m.setSubMatrix(detData2,0,0);
+ expected = new RecursiveLayoutRealMatrix
+ (new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
+ assertEquals(expected, m);
+
+ m.setSubMatrix(testDataPlus2,0,0);
+ expected = new RecursiveLayoutRealMatrix
+ (new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
+ assertEquals(expected, m);
+
+ // javadoc example
+ RecursiveLayoutRealMatrix matrix = new RecursiveLayoutRealMatrix
+ (new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1 , 2}});
+ matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1);
+ expected = new RecursiveLayoutRealMatrix
+ (new double[][] {{1, 2, 3, 4}, {5, 3, 4, 8}, {9, 5 ,6, 2}});
+ assertEquals(expected, matrix);
+
+ // dimension overflow
+ try {
+ m.setSubMatrix(testData,1,1);
+ fail("expecting MatrixIndexException");
+ } catch (MatrixIndexException e) {
+ // expected
+ }
+ // dimension underflow
+ try {
+ m.setSubMatrix(testData,-1,1);
+ fail("expecting MatrixIndexException");
+ } catch (MatrixIndexException e) {
+ // expected
+ }
+ try {
+ m.setSubMatrix(testData,1,-1);
+ fail("expecting MatrixIndexException");
+ } catch (MatrixIndexException e) {
+ // expected
+ }
+
+ // null
+ try {
+ m.setSubMatrix(null,1,1);
+ fail("expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ // ragged
+ try {
+ m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ // empty
+ try {
+ m.setSubMatrix(new double[][] {{}}, 0, 0);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ }
+
+ public void testWalk() {
+ int rows = 150;
+ int columns = 75;
+
+ RealMatrix m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInRowOrder(new SetVisitor());
+ GetVisitor getVisitor = new GetVisitor();
+ m.walkInOptimizedOrder(getVisitor);
+ assertEquals(rows * columns, getVisitor.getCount());
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+ getVisitor = new GetVisitor();
+ m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+ assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ for (int i = 0; i < rows; ++i) {
+ assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+ }
+ for (int j = 0; j < columns; ++j) {
+ assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+ }
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInColumnOrder(new SetVisitor());
+ getVisitor = new GetVisitor();
+ m.walkInOptimizedOrder(getVisitor);
+ assertEquals(rows * columns, getVisitor.getCount());
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+ getVisitor = new GetVisitor();
+ m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+ assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ for (int i = 0; i < rows; ++i) {
+ assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+ }
+ for (int j = 0; j < columns; ++j) {
+ assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+ }
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInOptimizedOrder(new SetVisitor());
+ getVisitor = new GetVisitor();
+ m.walkInRowOrder(getVisitor);
+ assertEquals(rows * columns, getVisitor.getCount());
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+ getVisitor = new GetVisitor();
+ m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+ assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ for (int i = 0; i < rows; ++i) {
+ assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+ }
+ for (int j = 0; j < columns; ++j) {
+ assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+ }
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInOptimizedOrder(new SetVisitor());
+ getVisitor = new GetVisitor();
+ m.walkInColumnOrder(getVisitor);
+ assertEquals(rows * columns, getVisitor.getCount());
+
+ m = new RecursiveLayoutRealMatrix(rows, columns);
+ m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
+ getVisitor = new GetVisitor();
+ m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
+ assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ for (int i = 0; i < rows; ++i) {
+ assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, columns - 1), 0);
+ }
+ for (int j = 0; j < columns; ++j) {
+ assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(rows - 1, j), 0);
+ }
+
+ }
+
+ private static class SetVisitor extends DefaultRealMatrixChangingVisitor {
+ private static final long serialVersionUID = 1773444180892369386L;
+ public double visit(int i, int j, double value) {
+ return i + j / 1024.0;
+ }
+ }
+
+ private static class GetVisitor extends DefaultRealMatrixPreservingVisitor {
+ private static final long serialVersionUID = -7745543227178932689L;
+ private int count = 0;
+ public void visit(int i, int j, double value) {
+ ++count;
+ assertEquals(i + j / 1024.0, value, 0.0);
+ }
+ public int getCount() {
+ return count;
+ }
+ };
+
+ //--------------- -----------------Protected methods
+
+ /** verifies that two matrices are close (1-norm) */
+ protected void assertClose(RealMatrix m, RealMatrix n, double tolerance) {
+ assertTrue(m.subtract(n).getNorm() < tolerance);
+ }
+
+ /** verifies that two vectors are close (sup norm) */
+ protected void assertClose(double[] m, double[] n, double tolerance) {
+ if (m.length != n.length) {
+ fail("vectors not same length");
+ }
+ for (int i = 0; i < m.length; i++) {
+ assertEquals(m[i], n[i], tolerance);
+ }
+ }
+
+ private RecursiveLayoutRealMatrix createRandomMatrix(Random r, int rows, int columns) {
+ RecursiveLayoutRealMatrix m = new RecursiveLayoutRealMatrix(rows, columns);
+ for (int i = 0; i < rows; ++i) {
+ for (int j = 0; j < columns; ++j) {
+ m.setEntry(i, j, 200 * r.nextDouble() - 100);
+ }
+ }
+ return m;
+ }
+
+}
+
Propchange: commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/experimental/org/apache/commons/math/linear/RecursiveLayoutRealMatrixTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision