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  /**
25   * Test cases for the {@link MatrixUtils} class.
26   *
27   * @version $Revision: 480442 $ $Date: 2006-11-29 00:21:22 -0700 (Wed, 29 Nov 2006) $
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}});  // ragged
70              fail("Expecting IllegalArgumentException");
71          } catch (IllegalArgumentException ex) {
72              // expected
73          } 
74          try {
75              MatrixUtils.createRealMatrix(new double[][] {{}, {}});  // no columns
76              fail("Expecting IllegalArgumentException");
77          } catch (IllegalArgumentException ex) {
78              // expected
79          }
80          try {
81              MatrixUtils.createRealMatrix(null);  // null
82              fail("Expecting NullPointerException");
83          } catch (NullPointerException ex) {
84              // expected
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}});  // ragged
97              fail("Expecting IllegalArgumentException");
98          } catch (IllegalArgumentException ex) {
99              // expected
100         } 
101         try {
102             MatrixUtils.createBigMatrix(new double[][] {{}, {}});  // no columns
103             fail("Expecting IllegalArgumentException");
104         } catch (IllegalArgumentException ex) {
105             // expected
106         }
107         try {
108             MatrixUtils.createBigMatrix(nullMatrix);  // null
109             fail("Expecting NullPointerException");
110         } catch (NullPointerException ex) {
111             // expected
112         } 
113     }
114         
115     public void testCreateRowRealMatrix() {
116         assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row),
117                new RealMatrixImpl(rowMatrix));
118         try {
119             MatrixUtils.createRowRealMatrix(new double[] {});  // empty
120             fail("Expecting IllegalArgumentException");
121         } catch (IllegalArgumentException ex) {
122             // expected
123         }
124         try {
125             MatrixUtils.createRowRealMatrix(null);  // null
126             fail("Expecting NullPointerException");
127         } catch (NullPointerException ex) {
128             // expected
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[] {});  // empty
141             fail("Expecting IllegalArgumentException");
142         } catch (IllegalArgumentException ex) {
143             // expected
144         }
145         try {
146             MatrixUtils.createRowBigMatrix(nullDoubleArray);  // null
147             fail("Expecting NullPointerException");
148         } catch (NullPointerException ex) {
149             // expected
150         } 
151     }
152     
153     public void testCreateColumnRealMatrix() {
154         assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col),
155                 new RealMatrixImpl(colMatrix));
156         try {
157             MatrixUtils.createColumnRealMatrix(new double[] {});  // empty
158             fail("Expecting IllegalArgumentException");
159         } catch (IllegalArgumentException ex) {
160             // expected
161         }
162         try {
163             MatrixUtils.createColumnRealMatrix(null);  // null
164             fail("Expecting NullPointerException");
165         } catch (NullPointerException ex) {
166             // expected
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[] {});  // empty
180             fail("Expecting IllegalArgumentException");
181         } catch (IllegalArgumentException ex) {
182             // expected
183         }
184         try {
185             MatrixUtils.createColumnBigMatrix(nullDoubleArray);  // null
186             fail("Expecting NullPointerException");
187         } catch (NullPointerException ex) {
188             // expected
189         } 
190     }
191     
192     /**
193      * Verifies that the matrix is an identity matrix
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             // expected
215         }
216     }
217     
218     /**
219      * Verifies that the matrix is an identity matrix
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             // expected
241         }
242     }
243         
244 }
245