1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.linear;
18
19 import java.math.BigDecimal;
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24
25
26
27
28
29
30 public final class MatrixUtilsTest extends TestCase {
31
32 protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
33 protected double[][] nullMatrix = null;
34 protected double[] row = {1,2,3};
35 protected BigDecimal[] bigRow =
36 {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)};
37 protected String[] stringRow = {"1", "2", "3"};
38 protected double[][] rowMatrix = {{1,2,3}};
39 protected BigDecimal[][] bigRowMatrix =
40 {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}};
41 protected String[][] stringRowMatrix = {{"1", "2", "3"}};
42 protected double[] col = {0,4,6};
43 protected BigDecimal[] bigCol =
44 {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)};
45 protected String[] stringCol = {"0","4","6"};
46 protected double[] nullDoubleArray = null;
47 protected double[][] colMatrix = {{0},{4},{6}};
48 protected BigDecimal[][] bigColMatrix =
49 {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}};
50 protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
51
52 public MatrixUtilsTest(String name) {
53 super(name);
54 }
55
56 public void setUp() {
57 }
58
59 public static Test suite() {
60 TestSuite suite = new TestSuite(MatrixUtilsTest.class);
61 suite.setName("MatrixUtils Tests");
62 return suite;
63 }
64
65 public void testCreateRealMatrix() {
66 assertEquals(new RealMatrixImpl(testData),
67 MatrixUtils.createRealMatrix(testData));
68 try {
69 MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}});
70 fail("Expecting IllegalArgumentException");
71 } catch (IllegalArgumentException ex) {
72
73 }
74 try {
75 MatrixUtils.createRealMatrix(new double[][] {{}, {}});
76 fail("Expecting IllegalArgumentException");
77 } catch (IllegalArgumentException ex) {
78
79 }
80 try {
81 MatrixUtils.createRealMatrix(null);
82 fail("Expecting NullPointerException");
83 } catch (NullPointerException ex) {
84
85 }
86 }
87
88 public void testCreateBigMatrix() {
89 assertEquals(new BigMatrixImpl(testData),
90 MatrixUtils.createBigMatrix(testData));
91 assertEquals(new BigMatrixImpl(bigColMatrix),
92 MatrixUtils.createBigMatrix(bigColMatrix));
93 assertEquals(new BigMatrixImpl(stringColMatrix),
94 MatrixUtils.createBigMatrix(stringColMatrix));
95 try {
96 MatrixUtils.createBigMatrix(new double[][] {{1}, {1,2}});
97 fail("Expecting IllegalArgumentException");
98 } catch (IllegalArgumentException ex) {
99
100 }
101 try {
102 MatrixUtils.createBigMatrix(new double[][] {{}, {}});
103 fail("Expecting IllegalArgumentException");
104 } catch (IllegalArgumentException ex) {
105
106 }
107 try {
108 MatrixUtils.createBigMatrix(nullMatrix);
109 fail("Expecting NullPointerException");
110 } catch (NullPointerException ex) {
111
112 }
113 }
114
115 public void testCreateRowRealMatrix() {
116 assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row),
117 new RealMatrixImpl(rowMatrix));
118 try {
119 MatrixUtils.createRowRealMatrix(new double[] {});
120 fail("Expecting IllegalArgumentException");
121 } catch (IllegalArgumentException ex) {
122
123 }
124 try {
125 MatrixUtils.createRowRealMatrix(null);
126 fail("Expecting NullPointerException");
127 } catch (NullPointerException ex) {
128
129 }
130 }
131
132 public void testCreateRowBigMatrix() {
133 assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(row),
134 new BigMatrixImpl(rowMatrix));
135 assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(bigRow),
136 new BigMatrixImpl(bigRowMatrix));
137 assertEquals((BigMatrixImpl) MatrixUtils.createRowBigMatrix(stringRow),
138 new BigMatrixImpl(stringRowMatrix));
139 try {
140 MatrixUtils.createRowBigMatrix(new double[] {});
141 fail("Expecting IllegalArgumentException");
142 } catch (IllegalArgumentException ex) {
143
144 }
145 try {
146 MatrixUtils.createRowBigMatrix(nullDoubleArray);
147 fail("Expecting NullPointerException");
148 } catch (NullPointerException ex) {
149
150 }
151 }
152
153 public void testCreateColumnRealMatrix() {
154 assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col),
155 new RealMatrixImpl(colMatrix));
156 try {
157 MatrixUtils.createColumnRealMatrix(new double[] {});
158 fail("Expecting IllegalArgumentException");
159 } catch (IllegalArgumentException ex) {
160
161 }
162 try {
163 MatrixUtils.createColumnRealMatrix(null);
164 fail("Expecting NullPointerException");
165 } catch (NullPointerException ex) {
166
167 }
168 }
169
170 public void testCreateColumnBigMatrix() {
171 assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(col),
172 new BigMatrixImpl(colMatrix));
173 assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(bigCol),
174 new BigMatrixImpl(bigColMatrix));
175 assertEquals((BigMatrixImpl) MatrixUtils.createColumnBigMatrix(stringCol),
176 new BigMatrixImpl(stringColMatrix));
177
178 try {
179 MatrixUtils.createColumnBigMatrix(new double[] {});
180 fail("Expecting IllegalArgumentException");
181 } catch (IllegalArgumentException ex) {
182
183 }
184 try {
185 MatrixUtils.createColumnBigMatrix(nullDoubleArray);
186 fail("Expecting NullPointerException");
187 } catch (NullPointerException ex) {
188
189 }
190 }
191
192
193
194
195 protected void checkIdentityMatrix(RealMatrix m) {
196 for (int i = 0; i < m.getRowDimension(); i++) {
197 for (int j =0; j < m.getColumnDimension(); j++) {
198 if (i == j) {
199 assertEquals(m.getEntry(i, j), 1d, 0);
200 } else {
201 assertEquals(m.getEntry(i, j), 0d, 0);
202 }
203 }
204 }
205 }
206
207 public void testCreateIdentityMatrix() {
208 checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
209 checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
210 checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
211 try {
212 MatrixUtils.createRealIdentityMatrix(0);
213 } catch (IllegalArgumentException ex) {
214
215 }
216 }
217
218
219
220
221 protected void checkIdentityBigMatrix(BigMatrix m) {
222 for (int i = 0; i < m.getRowDimension(); i++) {
223 for (int j =0; j < m.getColumnDimension(); j++) {
224 if (i == j) {
225 assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
226 } else {
227 assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
228 }
229 }
230 }
231 }
232
233 public void testCreateBigIdentityMatrix() {
234 checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
235 checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
236 checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
237 try {
238 MatrixUtils.createRealIdentityMatrix(0);
239 } catch (IllegalArgumentException ex) {
240
241 }
242 }
243
244 }
245