001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.linear;
018    
019    import java.math.BigDecimal;
020    import junit.framework.Test;
021    import junit.framework.TestCase;
022    import junit.framework.TestSuite;
023    
024    import org.apache.commons.math.fraction.BigFraction;
025    import org.apache.commons.math.fraction.Fraction;
026    import org.apache.commons.math.fraction.FractionConversionException;
027    import org.apache.commons.math.fraction.FractionField;
028    
029    /**
030     * Test cases for the {@link MatrixUtils} class.
031     *
032     * @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
033     */
034    
035    public final class MatrixUtilsTest extends TestCase {
036        
037        protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
038        protected double[][] nullMatrix = null;
039        protected double[] row = {1,2,3};
040        protected BigDecimal[] bigRow = 
041            {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)};
042        protected String[] stringRow = {"1", "2", "3"};
043        protected Fraction[] fractionRow = 
044            {new Fraction(1),new Fraction(2),new Fraction(3)};
045        protected double[][] rowMatrix = {{1,2,3}};
046        protected BigDecimal[][] bigRowMatrix = 
047            {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}};
048        protected String[][] stringRowMatrix = {{"1", "2", "3"}};
049        protected Fraction[][] fractionRowMatrix = 
050            {{new Fraction(1), new Fraction(2), new Fraction(3)}};
051        protected double[] col = {0,4,6};
052        protected BigDecimal[] bigCol = 
053            {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)};
054        protected String[] stringCol = {"0","4","6"};
055        protected Fraction[] fractionCol = 
056            {new Fraction(0),new Fraction(4),new Fraction(6)};
057        protected double[] nullDoubleArray = null;
058        protected double[][] colMatrix = {{0},{4},{6}};
059        protected BigDecimal[][] bigColMatrix = 
060            {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}};
061        protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
062        protected Fraction[][] fractionColMatrix = 
063            {{new Fraction(0)},{new Fraction(4)},{new Fraction(6)}};
064        
065        public MatrixUtilsTest(String name) {
066            super(name);
067        }
068        
069    
070        public static Test suite() {
071            TestSuite suite = new TestSuite(MatrixUtilsTest.class);
072            suite.setName("MatrixUtils Tests");
073            return suite;
074        }
075        
076        public void testCreateRealMatrix() {
077            assertEquals(new BlockRealMatrix(testData), 
078                    MatrixUtils.createRealMatrix(testData));
079            try {
080                MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}});  // ragged
081                fail("Expecting IllegalArgumentException");
082            } catch (IllegalArgumentException ex) {
083                // expected
084            } 
085            try {
086                MatrixUtils.createRealMatrix(new double[][] {{}, {}});  // no columns
087                fail("Expecting IllegalArgumentException");
088            } catch (IllegalArgumentException ex) {
089                // expected
090            }
091            try {
092                MatrixUtils.createRealMatrix(null);  // null
093                fail("Expecting NullPointerException");
094            } catch (NullPointerException ex) {
095                // expected
096            } 
097        }
098    
099        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