1 16 package org.apache.commons.math.linear; 17 18 import java.math.BigDecimal ; 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 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 [] bigRow = 35 {new BigDecimal (1),new BigDecimal (2),new BigDecimal (3)}; 36 protected String [] stringRow = {"1", "2", "3"}; 37 protected double[][] rowMatrix = {{1,2,3}}; 38 protected BigDecimal [][] bigRowMatrix = 39 {{new BigDecimal (1), new BigDecimal (2), new BigDecimal (3)}}; 40 protected String [][] stringRowMatrix = {{"1", "2", "3"}}; 41 protected double[] col = {0,4,6}; 42 protected BigDecimal [] bigCol = 43 {new BigDecimal (0),new BigDecimal (4),new BigDecimal (6)}; 44 protected String [] stringCol = {"0","4","6"}; 45 protected double[] nullDoubleArray = null; 46 protected double[][] colMatrix = {{0},{4},{6}}; 47 protected BigDecimal [][] bigColMatrix = 48 {{new BigDecimal (0)},{new BigDecimal (4)},{new BigDecimal (6)}}; 49 protected String [][] stringColMatrix = {{"0"}, {"4"}, {"6"}}; 50 51 public MatrixUtilsTest(String 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}}); fail("Expecting IllegalArgumentException"); 70 } catch (IllegalArgumentException ex) { 71 } 73 try { 74 MatrixUtils.createRealMatrix(new double[][] {{}, {}}); fail("Expecting IllegalArgumentException"); 76 } catch (IllegalArgumentException ex) { 77 } 79 try { 80 MatrixUtils.createRealMatrix(null); fail("Expecting NullPointerException"); 82 } catch (NullPointerException ex) { 83 } 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}}); fail("Expecting IllegalArgumentException"); 97 } catch (IllegalArgumentException ex) { 98 } 100 try { 101 MatrixUtils.createBigMatrix(new double[][] {{}, {}}); fail("Expecting IllegalArgumentException"); 103 } catch (IllegalArgumentException ex) { 104 } 106 try { 107 MatrixUtils.createBigMatrix(nullMatrix); fail("Expecting NullPointerException"); 109 } catch (NullPointerException ex) { 110 } 112 } 113 114 public void testCreateRowRealMatrix() { 115 assertEquals((RealMatrixImpl) MatrixUtils.createRowRealMatrix(row), 116 new RealMatrixImpl(rowMatrix)); 117 try { 118 MatrixUtils.createRowRealMatrix(new double[] {}); fail("Expecting IllegalArgumentException"); 120 } catch (IllegalArgumentException ex) { 121 } 123 try { 124 MatrixUtils.createRowRealMatrix(null); fail("Expecting NullPointerException"); 126 } catch (NullPointerException ex) { 127 } 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[] {}); fail("Expecting IllegalArgumentException"); 141 } catch (IllegalArgumentException ex) { 142 } 144 try { 145 MatrixUtils.createRowBigMatrix(nullDoubleArray); fail("Expecting NullPointerException"); 147 } catch (NullPointerException ex) { 148 } 150 } 151 152 public void testCreateColumnRealMatrix() { 153 assertEquals((RealMatrixImpl) MatrixUtils.createColumnRealMatrix(col), 154 new RealMatrixImpl(colMatrix)); 155 try { 156 MatrixUtils.createColumnRealMatrix(new double[] {}); fail("Expecting IllegalArgumentException"); 158 } catch (IllegalArgumentException ex) { 159 } 161 try { 162 MatrixUtils.createColumnRealMatrix(null); fail("Expecting NullPointerException"); 164 } catch (NullPointerException ex) { 165 } 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[] {}); fail("Expecting IllegalArgumentException"); 180 } catch (IllegalArgumentException ex) { 181 } 183 try { 184 MatrixUtils.createColumnBigMatrix(nullDoubleArray); fail("Expecting NullPointerException"); 186 } catch (NullPointerException ex) { 187 } 189 } 190 191 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 ex) { 213 } 215 } 216 217 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 ex) { 239 } 241 } 242 243 } 244 245 | Popular Tags |