KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > linear > MatrixUtilsTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.linear;
17
18 import java.math.BigDecimal JavaDoc;
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 /**
24  * Test cases for the {@link MatrixUtils} class.
25  *
26  * @version $Revision$ $Date: 2005-05-21 09:29:19 -0700 (Sat, 21 May 2005) $
27  */

28
29 public final class MatrixUtilsTest extends TestCase {
30     
31     protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
32     protected double[][] nullMatrix = null;
33     protected double[] row = {1,2,3};
34     protected BigDecimal JavaDoc[] bigRow =
35         {new BigDecimal JavaDoc(1),new BigDecimal JavaDoc(2),new BigDecimal JavaDoc(3)};
36     protected String JavaDoc[] stringRow = {"1", "2", "3"};
37     protected double[][] rowMatrix = {{1,2,3}};
38     protected BigDecimal JavaDoc[][] bigRowMatrix =
39         {{new BigDecimal JavaDoc(1), new BigDecimal JavaDoc(2), new BigDecimal JavaDoc(3)}};
40     protected String JavaDoc[][] stringRowMatrix = {{"1", "2", "3"}};
41     protected double[] col = {0,4,6};
42     protected BigDecimal JavaDoc[] bigCol =
43         {new BigDecimal JavaDoc(0),new BigDecimal JavaDoc(4),new BigDecimal JavaDoc(6)};
44     protected String JavaDoc[] stringCol = {"0","4","6"};
45     protected double[] nullDoubleArray = null;
46     protected double[][] colMatrix = {{0},{4},{6}};
47     protected BigDecimal JavaDoc[][] bigColMatrix =
48         {{new BigDecimal JavaDoc(0)},{new BigDecimal JavaDoc(4)},{new BigDecimal JavaDoc(6)}};
49     protected String JavaDoc[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
50     
51     public MatrixUtilsTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     public void setUp() {
56     }
57     
58     public static Test suite() {
59         TestSuite suite = new TestSuite(MatrixUtilsTest.class);
60         suite.setName("MatrixUtils Tests");
61         return suite;
62     }
63     
64     public void testCreateRealMatrix() {
65         assertEquals(new RealMatrixImpl(testData),
66                 MatrixUtils.createRealMatrix(testData));
67         try {
68             MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}}); // ragged
69
fail("Expecting IllegalArgumentException");
70         } catch (IllegalArgumentException JavaDoc ex) {
71             // expected
72
}
73         try {
74             MatrixUtils.createRealMatrix(new double[][] {{}, {}}); // no columns
75
fail("Expecting IllegalArgumentException");
76         } catch (IllegalArgumentException JavaDoc ex) {
77             // expected
78
}
79         try {
80             MatrixUtils.createRealMatrix(null); // null
81
fail("Expecting NullPointerException");
82         } catch (NullPointerException JavaDoc ex) {
83             // expected
84
}
85     }
86     
87     public void testCreateBigMatrix() {
88         assertEquals(new BigMatrixImpl(testData),
89                 MatrixUtils.createBigMatrix(testData));
90         assertEquals(new BigMatrixImpl(bigColMatrix),
91                 MatrixUtils.createBigMatrix(bigColMatrix));
92         assertEquals(new BigMatrixImpl(stringColMatrix),
93                 MatrixUtils.createBigMatrix(stringColMatrix));
94         try {
95             MatrixUtils.createBigMatrix(new double[][] {{1}, {1,2}}); // ragged
96
fail("Expecting IllegalArgumentException");
97         } catch (IllegalArgumentException JavaDoc ex) {
98             // expected
99
}
100         try {
101             MatrixUtils.createBigMatrix(new double[][] {{}, {}}); // no columns
102
fail("Expecting IllegalArgumentException");
103         } catch (IllegalArgumentException JavaDoc ex) {
104             // expected
105
}
106         try {
107             MatrixUtils.createBigMatrix(nullMatrix); // null
108
fail("Expecting NullPointerException");
109         } catch (NullPointerException JavaDoc ex) {
110             // expected
111
}
112     }
113         
114     public void testCreateRowRealMatrix() {
115         assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row),
116                new RealMatrixImpl(rowMatrix));
117         try {
118             MatrixUtils.createRowRealMatrix(new double[] {}); // empty
119
fail("Expecting IllegalArgumentException");
120         } catch (IllegalArgumentException JavaDoc ex) {
121             // expected
122
}
123         try {
124             MatrixUtils.createRowRealMatrix(null); // null
125
fail("Expecting NullPointerException");
126         } catch (NullPointerException JavaDoc ex) {
127             // expected
128
}
129     }
130     
131     public void testCreateRowBigMatrix() {
132         assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(row),
133                 new BigMatrixImpl(rowMatrix));
134         assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(bigRow),
135                 new BigMatrixImpl(bigRowMatrix));
136         assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(stringRow),
137                 new BigMatrixImpl(stringRowMatrix));
138         try {
139             MatrixUtils.createRowBigMatrix(new double[] {}); // empty
140
fail("Expecting IllegalArgumentException");
141         } catch (IllegalArgumentException JavaDoc ex) {
142             // expected
143
}
144         try {
145             MatrixUtils.createRowBigMatrix(nullDoubleArray); // null
146
fail("Expecting NullPointerException");
147         } catch (NullPointerException JavaDoc ex) {
148             // expected
149
}
150     }
151     
152     public void testCreateColumnRealMatrix() {
153         assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col),
154                 new RealMatrixImpl(colMatrix));
155         try {
156             MatrixUtils.createColumnRealMatrix(new double[] {}); // empty
157
fail("Expecting IllegalArgumentException");
158         } catch (IllegalArgumentException JavaDoc ex) {
159             // expected
160
}
161         try {
162             MatrixUtils.createColumnRealMatrix(null); // null
163
fail("Expecting NullPointerException");
164         } catch (NullPointerException JavaDoc ex) {
165             // expected
166
}
167     }
168     
169     public void testCreateColumnBigMatrix() {
170         assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(col),
171                 new BigMatrixImpl(colMatrix));
172         assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(bigCol),
173                 new BigMatrixImpl(bigColMatrix));
174         assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(stringCol),
175                 new BigMatrixImpl(stringColMatrix));
176        
177         try {
178             MatrixUtils.createColumnBigMatrix(new double[] {}); // empty
179
fail("Expecting IllegalArgumentException");
180         } catch (IllegalArgumentException JavaDoc ex) {
181             // expected
182
}
183         try {
184             MatrixUtils.createColumnBigMatrix(nullDoubleArray); // null
185
fail("Expecting NullPointerException");
186         } catch (NullPointerException JavaDoc ex) {
187             // expected
188
}
189     }
190     
191     /**
192      * Verifies that the matrix is an identity matrix
193      */

194     protected void checkIdentityMatrix(RealMatrix m) {
195         for (int i = 0; i < m.getRowDimension(); i++) {
196             for (int j =0; j < m.getColumnDimension(); j++) {
197                 if (i == j) {
198                     assertEquals(m.getEntry(i, j), 1d, 0);
199                 } else {
200                     assertEquals(m.getEntry(i, j), 0d, 0);
201                 }
202             }
203         }
204     }
205     
206     public void testCreateIdentityMatrix() {
207         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
208         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
209         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
210         try {
211             MatrixUtils.createRealIdentityMatrix(0);
212         } catch (IllegalArgumentException JavaDoc ex) {
213             // expected
214
}
215     }
216     
217     /**
218      * Verifies that the matrix is an identity matrix
219      */

220     protected void checkIdentityBigMatrix(BigMatrix m) {
221         for (int i = 0; i < m.getRowDimension(); i++) {
222             for (int j =0; j < m.getColumnDimension(); j++) {
223                 if (i == j) {
224                     assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
225                 } else {
226                     assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
227                 }
228             }
229         }
230     }
231     
232     public void testCreateBigIdentityMatrix() {
233         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
234         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
235         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
236         try {
237             MatrixUtils.createRealIdentityMatrix(0);
238         } catch (IllegalArgumentException JavaDoc ex) {
239             // expected
240
}
241     }
242         
243 }
244
245
Popular Tags