001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.linear;
017:
018: import java.math.BigDecimal;
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: /**
024: * Test cases for the {@link MatrixUtils} class.
025: *
026: * @version $Revision: 171229 $ $Date: 2005-05-21 09:29:19 -0700 (Sat, 21 May 2005) $
027: */
028:
029: public final class MatrixUtilsTest extends TestCase {
030:
031: protected double[][] testData = { { 1d, 2d, 3d }, { 2d, 5d, 3d },
032: { 1d, 0d, 8d } };
033: protected double[][] nullMatrix = null;
034: protected double[] row = { 1, 2, 3 };
035: protected BigDecimal[] bigRow = { new BigDecimal(1),
036: new BigDecimal(2), new BigDecimal(3) };
037: protected String[] stringRow = { "1", "2", "3" };
038: protected double[][] rowMatrix = { { 1, 2, 3 } };
039: protected BigDecimal[][] bigRowMatrix = { { new BigDecimal(1),
040: new BigDecimal(2), new BigDecimal(3) } };
041: protected String[][] stringRowMatrix = { { "1", "2", "3" } };
042: protected double[] col = { 0, 4, 6 };
043: protected BigDecimal[] bigCol = { new BigDecimal(0),
044: new BigDecimal(4), new BigDecimal(6) };
045: protected String[] stringCol = { "0", "4", "6" };
046: protected double[] nullDoubleArray = null;
047: protected double[][] colMatrix = { { 0 }, { 4 }, { 6 } };
048: protected BigDecimal[][] bigColMatrix = { { new BigDecimal(0) },
049: { new BigDecimal(4) }, { new BigDecimal(6) } };
050: protected String[][] stringColMatrix = { { "0" }, { "4" }, { "6" } };
051:
052: public MatrixUtilsTest(String name) {
053: super (name);
054: }
055:
056: public void setUp() {
057: }
058:
059: public static Test suite() {
060: TestSuite suite = new TestSuite(MatrixUtilsTest.class);
061: suite.setName("MatrixUtils Tests");
062: return suite;
063: }
064:
065: public void testCreateRealMatrix() {
066: assertEquals(new RealMatrixImpl(testData), MatrixUtils
067: .createRealMatrix(testData));
068: try {
069: MatrixUtils.createRealMatrix(new double[][] { { 1 },
070: { 1, 2 } }); // ragged
071: fail("Expecting IllegalArgumentException");
072: } catch (IllegalArgumentException ex) {
073: // expected
074: }
075: try {
076: MatrixUtils.createRealMatrix(new double[][] { {}, {} }); // no columns
077: fail("Expecting IllegalArgumentException");
078: } catch (IllegalArgumentException ex) {
079: // expected
080: }
081: try {
082: MatrixUtils.createRealMatrix(null); // null
083: fail("Expecting NullPointerException");
084: } catch (NullPointerException ex) {
085: // expected
086: }
087: }
088:
089: public void testCreateBigMatrix() {
090: assertEquals(new BigMatrixImpl(testData), MatrixUtils
091: .createBigMatrix(testData));
092: assertEquals(new BigMatrixImpl(bigColMatrix), MatrixUtils
093: .createBigMatrix(bigColMatrix));
094: assertEquals(new BigMatrixImpl(stringColMatrix), MatrixUtils
095: .createBigMatrix(stringColMatrix));
096: try {
097: MatrixUtils.createBigMatrix(new double[][] { { 1 },
098: { 1, 2 } }); // ragged
099: fail("Expecting IllegalArgumentException");
100: } catch (IllegalArgumentException ex) {
101: // expected
102: }
103: try {
104: MatrixUtils.createBigMatrix(new double[][] { {}, {} }); // no columns
105: fail("Expecting IllegalArgumentException");
106: } catch (IllegalArgumentException ex) {
107: // expected
108: }
109: try {
110: MatrixUtils.createBigMatrix(nullMatrix); // null
111: fail("Expecting NullPointerException");
112: } catch (NullPointerException ex) {
113: // expected
114: }
115: }
116:
117: public void testCreateRowRealMatrix() {
118: assertEquals((RealMatrixImpl) MatrixUtils
119: .createRowRealMatrix(row),
120: new RealMatrixImpl(rowMatrix));
121: try {
122: MatrixUtils.createRowRealMatrix(new double[] {}); // empty
123: fail("Expecting IllegalArgumentException");
124: } catch (IllegalArgumentException ex) {
125: // expected
126: }
127: try {
128: MatrixUtils.createRowRealMatrix(null); // null
129: fail("Expecting NullPointerException");
130: } catch (NullPointerException ex) {
131: // expected
132: }
133: }
134:
135: public void testCreateRowBigMatrix() {
136: assertEquals((BigMatrixImpl) MatrixUtils
137: .createRowBigMatrix(row), new BigMatrixImpl(rowMatrix));
138: assertEquals((BigMatrixImpl) MatrixUtils
139: .createRowBigMatrix(bigRow), new BigMatrixImpl(
140: bigRowMatrix));
141: assertEquals((BigMatrixImpl) MatrixUtils
142: .createRowBigMatrix(stringRow), new BigMatrixImpl(
143: stringRowMatrix));
144: try {
145: MatrixUtils.createRowBigMatrix(new double[] {}); // empty
146: fail("Expecting IllegalArgumentException");
147: } catch (IllegalArgumentException ex) {
148: // expected
149: }
150: try {
151: MatrixUtils.createRowBigMatrix(nullDoubleArray); // null
152: fail("Expecting NullPointerException");
153: } catch (NullPointerException ex) {
154: // expected
155: }
156: }
157:
158: public void testCreateColumnRealMatrix() {
159: assertEquals((RealMatrixImpl) MatrixUtils
160: .createColumnRealMatrix(col), new RealMatrixImpl(
161: colMatrix));
162: try {
163: MatrixUtils.createColumnRealMatrix(new double[] {}); // empty
164: fail("Expecting IllegalArgumentException");
165: } catch (IllegalArgumentException ex) {
166: // expected
167: }
168: try {
169: MatrixUtils.createColumnRealMatrix(null); // null
170: fail("Expecting NullPointerException");
171: } catch (NullPointerException ex) {
172: // expected
173: }
174: }
175:
176: public void testCreateColumnBigMatrix() {
177: assertEquals((BigMatrixImpl) MatrixUtils
178: .createColumnBigMatrix(col), new BigMatrixImpl(
179: colMatrix));
180: assertEquals((BigMatrixImpl) MatrixUtils
181: .createColumnBigMatrix(bigCol), new BigMatrixImpl(
182: bigColMatrix));
183: assertEquals((BigMatrixImpl) MatrixUtils
184: .createColumnBigMatrix(stringCol), new BigMatrixImpl(
185: stringColMatrix));
186:
187: try {
188: MatrixUtils.createColumnBigMatrix(new double[] {}); // empty
189: fail("Expecting IllegalArgumentException");
190: } catch (IllegalArgumentException ex) {
191: // expected
192: }
193: try {
194: MatrixUtils.createColumnBigMatrix(nullDoubleArray); // null
195: fail("Expecting NullPointerException");
196: } catch (NullPointerException ex) {
197: // expected
198: }
199: }
200:
201: /**
202: * Verifies that the matrix is an identity matrix
203: */
204: protected void checkIdentityMatrix(RealMatrix m) {
205: for (int i = 0; i < m.getRowDimension(); i++) {
206: for (int j = 0; j < m.getColumnDimension(); j++) {
207: if (i == j) {
208: assertEquals(m.getEntry(i, j), 1d, 0);
209: } else {
210: assertEquals(m.getEntry(i, j), 0d, 0);
211: }
212: }
213: }
214: }
215:
216: public void testCreateIdentityMatrix() {
217: checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
218: checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
219: checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
220: try {
221: MatrixUtils.createRealIdentityMatrix(0);
222: } catch (IllegalArgumentException ex) {
223: // expected
224: }
225: }
226:
227: /**
228: * Verifies that the matrix is an identity matrix
229: */
230: protected void checkIdentityBigMatrix(BigMatrix m) {
231: for (int i = 0; i < m.getRowDimension(); i++) {
232: for (int j = 0; j < m.getColumnDimension(); j++) {
233: if (i == j) {
234: assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
235: } else {
236: assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
237: }
238: }
239: }
240: }
241:
242: public void testCreateBigIdentityMatrix() {
243: checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
244: checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
245: checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
246: try {
247: MatrixUtils.createRealIdentityMatrix(0);
248: } catch (IllegalArgumentException ex) {
249: // expected
250: }
251: }
252:
253: }
|