1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  import org.apache.commons.math.fraction.BigFraction;
25  import org.apache.commons.math.fraction.Fraction;
26  import org.apache.commons.math.fraction.FractionConversionException;
27  import org.apache.commons.math.fraction.FractionField;
28  
29  /**
30   * Test cases for the {@link MatrixUtils} class.
31   *
32   * @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
33   */
34  
35  public final class MatrixUtilsTest extends TestCase {
36      
37      protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
38      protected double[][] nullMatrix = null;
39      protected double[] row = {1,2,3};
40      protected BigDecimal[] bigRow = 
41          {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)};
42      protected String[] stringRow = {"1", "2", "3"};
43      protected Fraction[] fractionRow = 
44          {new Fraction(1),new Fraction(2),new Fraction(3)};
45      protected double[][] rowMatrix = {{1,2,3}};
46      protected BigDecimal[][] bigRowMatrix = 
47          {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}};
48      protected String[][] stringRowMatrix = {{"1", "2", "3"}};
49      protected Fraction[][] fractionRowMatrix = 
50          {{new Fraction(1), new Fraction(2), new Fraction(3)}};
51      protected double[] col = {0,4,6};
52      protected BigDecimal[] bigCol = 
53          {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)};
54      protected String[] stringCol = {"0","4","6"};
55      protected Fraction[] fractionCol = 
56          {new Fraction(0),new Fraction(4),new Fraction(6)};
57      protected double[] nullDoubleArray = null;
58      protected double[][] colMatrix = {{0},{4},{6}};
59      protected BigDecimal[][] bigColMatrix = 
60          {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}};
61      protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
62      protected Fraction[][] fractionColMatrix = 
63          {{new Fraction(0)},{new Fraction(4)},{new Fraction(6)}};
64      
65      public MatrixUtilsTest(String name) {
66          super(name);
67      }
68      
69  
70      public static Test suite() {
71          TestSuite suite = new TestSuite(MatrixUtilsTest.class);
72          suite.setName("MatrixUtils Tests");
73          return suite;
74      }
75      
76      public void testCreateRealMatrix() {
77          assertEquals(new BlockRealMatrix(testData), 
78                  MatrixUtils.createRealMatrix(testData));
79          try {
80              MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}});  // ragged
81              fail("Expecting IllegalArgumentException");
82          } catch (IllegalArgumentException ex) {
83              // expected
84          } 
85          try {
86              MatrixUtils.createRealMatrix(new double[][] {{}, {}});  // no columns
87              fail("Expecting IllegalArgumentException");
88          } catch (IllegalArgumentException ex) {
89              // expected
90          }
91          try {
92              MatrixUtils.createRealMatrix(null);  // null
93              fail("Expecting NullPointerException");
94          } catch (NullPointerException ex) {
95              // expected
96          } 
97      }
98  
99      public void testcreateFieldMatrix() {
100         assertEquals(new Array2DRowFieldMatrix<Fraction>(asFraction(testData)), 
101                      MatrixUtils.createFieldMatrix(asFraction(testData)));
102         assertEquals(new Array2DRowFieldMatrix<Fraction>(fractionColMatrix), 
103                      MatrixUtils.createFieldMatrix(fractionColMatrix));
104         try {
105             MatrixUtils.createFieldMatrix(asFraction(new double[][] {{1}, {1,2}}));  // ragged
106             fail("Expecting IllegalArgumentException");
107         } catch (IllegalArgumentException ex) {
108             // expected
109         } 
110         try {
111             MatrixUtils.createFieldMatrix(asFraction(new double[][] {{}, {}}));  // no columns
112             fail("Expecting IllegalArgumentException");
113         } catch (IllegalArgumentException ex) {
114             // expected
115         }
116         try {
117             MatrixUtils.createFieldMatrix((Fraction[][])null);  // null
118             fail("Expecting NullPointerException");
119         } catch (NullPointerException ex) {
120             // expected
121         } 
122     }
123 
124     @Deprecated
125     public void testCreateBigMatrix() {
126         assertEquals(new BigMatrixImpl(testData), 
127                 MatrixUtils.createBigMatrix(testData));
128         assertEquals(new BigMatrixImpl(BigMatrixImplTest.asBigDecimal(testData), true), 
129                 MatrixUtils.createBigMatrix(BigMatrixImplTest.asBigDecimal(testData), false));
130         assertEquals(new BigMatrixImpl(BigMatrixImplTest.asBigDecimal(testData), false), 
131                 MatrixUtils.createBigMatrix(BigMatrixImplTest.asBigDecimal(testData), true));
132         assertEquals(new BigMatrixImpl(bigColMatrix), 
133                 MatrixUtils.createBigMatrix(bigColMatrix));
134         assertEquals(new BigMatrixImpl(stringColMatrix), 
135                 MatrixUtils.createBigMatrix(stringColMatrix));
136         try {
137             MatrixUtils.createBigMatrix(new double[][] {{1}, {1,2}});  // ragged
138             fail("Expecting IllegalArgumentException");
139         } catch (IllegalArgumentException ex) {
140             // expected
141         } 
142         try {
143             MatrixUtils.createBigMatrix(new double[][] {{}, {}});  // no columns
144             fail("Expecting IllegalArgumentException");
145         } catch (IllegalArgumentException ex) {
146             // expected
147         }
148         try {
149             MatrixUtils.createBigMatrix(nullMatrix);  // null
150             fail("Expecting NullPointerException");
151         } catch (NullPointerException ex) {
152             // expected
153         } 
154     }
155         
156     public void testCreateRowRealMatrix() {
157         assertEquals(MatrixUtils.createRowRealMatrix(row),
158                      new BlockRealMatrix(rowMatrix));
159         try {
160             MatrixUtils.createRowRealMatrix(new double[] {});  // empty
161             fail("Expecting IllegalArgumentException");
162         } catch (IllegalArgumentException ex) {
163             // expected
164         }
165         try {
166             MatrixUtils.createRowRealMatrix(null);  // null
167             fail("Expecting NullPointerException");
168         } catch (NullPointerException ex) {
169             // expected
170         } 
171     }
172     
173     public void testCreateRowFieldMatrix() {
174         assertEquals(MatrixUtils.createRowFieldMatrix(asFraction(row)),
175                      new Array2DRowFieldMatrix<Fraction>(asFraction(rowMatrix)));
176         assertEquals(MatrixUtils.createRowFieldMatrix(fractionRow),
177                      new Array2DRowFieldMatrix<Fraction>(fractionRowMatrix));
178         try {
179             MatrixUtils.createRowFieldMatrix(new Fraction[] {});  // empty
180             fail("Expecting IllegalArgumentException");
181         } catch (IllegalArgumentException ex) {
182             // expected
183         }
184         try {
185             MatrixUtils.createRowFieldMatrix((Fraction[]) null);  // null
186             fail("Expecting NullPointerException");
187         } catch (NullPointerException ex) {
188             // expected
189         } 
190     }
191 
192     @Deprecated
193     public void testCreateRowBigMatrix() {
194         assertEquals(MatrixUtils.createRowBigMatrix(row),
195                 new BigMatrixImpl(rowMatrix));
196         assertEquals(MatrixUtils.createRowBigMatrix(bigRow),
197                 new BigMatrixImpl(bigRowMatrix));
198         assertEquals(MatrixUtils.createRowBigMatrix(stringRow),
199                 new BigMatrixImpl(stringRowMatrix));
200         try {
201             MatrixUtils.createRowBigMatrix(new double[] {});  // empty
202             fail("Expecting IllegalArgumentException");
203         } catch (IllegalArgumentException ex) {
204             // expected
205         }
206         try {
207             MatrixUtils.createRowBigMatrix(nullDoubleArray);  // null
208             fail("Expecting NullPointerException");
209         } catch (NullPointerException ex) {
210             // expected
211         } 
212     }
213 
214     public void testCreateColumnRealMatrix() {
215         assertEquals(MatrixUtils.createColumnRealMatrix(col),
216                      new BlockRealMatrix(colMatrix));
217         try {
218             MatrixUtils.createColumnRealMatrix(new double[] {});  // empty
219             fail("Expecting IllegalArgumentException");
220         } catch (IllegalArgumentException ex) {
221             // expected
222         }
223         try {
224             MatrixUtils.createColumnRealMatrix(null);  // null
225             fail("Expecting NullPointerException");
226         } catch (NullPointerException ex) {
227             // expected
228         } 
229     }
230     
231     public void testCreateColumnFieldMatrix() {
232         assertEquals(MatrixUtils.createColumnFieldMatrix(asFraction(col)),
233                      new Array2DRowFieldMatrix<Fraction>(asFraction(colMatrix)));
234         assertEquals(MatrixUtils.createColumnFieldMatrix(fractionCol),
235                      new Array2DRowFieldMatrix<Fraction>(fractionColMatrix));
236 
237         try {
238             MatrixUtils.createColumnFieldMatrix(new Fraction[] {});  // empty
239             fail("Expecting IllegalArgumentException");
240         } catch (IllegalArgumentException ex) {
241             // expected
242         }
243         try {
244             MatrixUtils.createColumnFieldMatrix((Fraction[]) null);  // null
245             fail("Expecting NullPointerException");
246         } catch (NullPointerException ex) {
247             // expected
248         } 
249     }
250 
251     @Deprecated
252     public void testCreateColumnBigMatrix() {
253         assertEquals(MatrixUtils.createColumnBigMatrix(col),
254                 new BigMatrixImpl(colMatrix));
255         assertEquals(MatrixUtils.createColumnBigMatrix(bigCol),
256                 new BigMatrixImpl(bigColMatrix));
257         assertEquals(MatrixUtils.createColumnBigMatrix(stringCol),
258                 new BigMatrixImpl(stringColMatrix));   
259        
260         try {
261             MatrixUtils.createColumnBigMatrix(new double[] {});  // empty
262             fail("Expecting IllegalArgumentException");
263         } catch (IllegalArgumentException ex) {
264             // expected
265         }
266         try {
267             MatrixUtils.createColumnBigMatrix(nullDoubleArray);  // null
268             fail("Expecting NullPointerException");
269         } catch (NullPointerException ex) {
270             // expected
271         } 
272     }
273 
274     /**
275      * Verifies that the matrix is an identity matrix
276      */
277     protected void checkIdentityMatrix(RealMatrix m) {
278         for (int i = 0; i < m.getRowDimension(); i++) {
279             for (int j =0; j < m.getColumnDimension(); j++) {
280                 if (i == j) {
281                     assertEquals(m.getEntry(i, j), 1d, 0);
282                 } else {
283                     assertEquals(m.getEntry(i, j), 0d, 0);
284                 }
285             }
286         }   
287     }
288     
289     public void testCreateIdentityMatrix() {
290         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
291         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
292         checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
293         try {
294             MatrixUtils.createRealIdentityMatrix(0);
295         } catch (IllegalArgumentException ex) {
296             // expected
297         }
298     }
299     
300     /**
301      * Verifies that the matrix is an identity matrix
302      */
303     protected void checkIdentityFieldMatrix(FieldMatrix<Fraction> m) {
304         for (int i = 0; i < m.getRowDimension(); i++) {
305             for (int j =0; j < m.getColumnDimension(); j++) {
306                 if (i == j) {
307                     assertEquals(m.getEntry(i, j), Fraction.ONE);
308                 } else {
309                     assertEquals(m.getEntry(i, j), Fraction.ZERO);
310                 }
311             }
312         }   
313     }
314     
315     public void testcreateFieldIdentityMatrix() {
316         checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 3));
317         checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 2));
318         checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 1));
319         try {
320             MatrixUtils.createRealIdentityMatrix(0);
321         } catch (IllegalArgumentException ex) {
322             // expected
323         }
324     }
325 
326     public void testBigFractionConverter() {
327         BigFraction[][] bfData = {
328                 { new BigFraction(1), new BigFraction(2), new BigFraction(3) },
329                 { new BigFraction(2), new BigFraction(5), new BigFraction(3) },
330                 { new BigFraction(1), new BigFraction(0), new BigFraction(8) }
331         };
332         FieldMatrix<BigFraction> m = new Array2DRowFieldMatrix<BigFraction>(bfData, false);
333         RealMatrix converted = MatrixUtils.bigFractionMatrixToRealMatrix(m);
334         RealMatrix reference = new Array2DRowRealMatrix(testData, false);
335         assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0);
336     }
337 
338     public void testFractionConverter() {
339         Fraction[][] fData = {
340                 { new Fraction(1), new Fraction(2), new Fraction(3) },
341                 { new Fraction(2), new Fraction(5), new Fraction(3) },
342                 { new Fraction(1), new Fraction(0), new Fraction(8) }
343         };
344         FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(fData, false);
345         RealMatrix converted = MatrixUtils.fractionMatrixToRealMatrix(m);
346         RealMatrix reference = new Array2DRowRealMatrix(testData, false);
347         assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0);
348     }
349 
350     public static final Fraction[][] asFraction(double[][] data) {
351         Fraction d[][] = new Fraction[data.length][];
352         try {
353             for (int i = 0; i < data.length; ++i) {
354                 double[] dataI = data[i];
355                 Fraction[] dI  = new Fraction[dataI.length];
356                 for (int j = 0; j < dataI.length; ++j) {
357                     dI[j] = new Fraction(dataI[j]);
358                 }
359                 d[i] = dI;
360             }
361         } catch (FractionConversionException fce) {
362             fail(fce.getMessage());
363         }
364         return d;
365     }
366 
367     public static final Fraction[] asFraction(double[] data) {
368         Fraction d[] = new Fraction[data.length];
369         try {
370             for (int i = 0; i < data.length; ++i) {
371                 d[i] = new Fraction(data[i]);
372             }
373         } catch (FractionConversionException fce) {
374             fail(fce.getMessage());
375         }
376         return d;
377     }
378 
379     /**
380      * Verifies that the matrix is an identity matrix
381      */
382     @Deprecated
383     protected void checkIdentityBigMatrix(BigMatrix m) {
384         for (int i = 0; i < m.getRowDimension(); i++) {
385             for (int j =0; j < m.getColumnDimension(); j++) {
386                 if (i == j) {
387                     assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
388                 } else {
389                     assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
390                 }
391             }
392         }   
393     }
394 
395     @Deprecated
396     public void testCreateBigIdentityMatrix() {
397         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
398         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
399         checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
400         try {
401             MatrixUtils.createRealIdentityMatrix(0);
402         } catch (IllegalArgumentException ex) {
403             // expected
404         }
405     }
406 
407 }
408