@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); test.set("Fee", "Foo", 1, 1, 9); assertNotNull("row", test.getRowLabelBindings()); assertNotNull("row", test.getRowLabelBindings()); assertEquals("Fee", 1, test.getRowLabelBindings().get("Fee").intValue()); assertEquals("Foo", 1, test.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", test.get(1, 1), test.get("Fee", "Foo"), EPSILON); test.get("Fie", "Foe"); }
private static void print(Matrix m) { for (int i = 0; i < m.rowSize(); i++) { for (int j = 0; j < m.columnSize(); j++) { if (Math.abs(m.get(i, j)) > 1.0e-10) { System.out.printf("%10.3f ", m.get(i, j)); } else { System.out.printf("%10s ", (i + j) % 3 == 0 ? "." : ""); } } System.out.printf("\n"); } System.out.printf("\n"); } }
@Test(expected = IllegalStateException.class) public void testSettingLabelBindings() { Matrix m = matrixFactory(new double[][]{{1, 3, 4}, {5, 2, 3}, {1, 4, 2}}); assertNull("row bindings", m.getRowLabelBindings()); assertNull("col bindings", m.getColumnLabelBindings()); m.set("Fee", "Foo", 1, 2, 9); assertNotNull("row", m.getRowLabelBindings()); assertNotNull("row", m.getRowLabelBindings()); assertEquals("Fee", 1, m.getRowLabelBindings().get("Fee").intValue()); assertEquals("Fee", 2, m.getColumnLabelBindings().get("Foo").intValue()); assertEquals("FeeFoo", m.get(1, 2), m.get("Fee", "Foo"), EPSILON); m.get("Fie", "Foe"); }
private static void printMatrix(String name, Matrix m) { int rows = m.numRows(); int columns = m.numCols(); System.out.printf("%s - %d x %d\n", name, rows, columns); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.printf("%10.5f", m.get(i, j)); } System.out.printf("\n"); } System.out.printf("\n"); System.out.printf("\n"); }
@Test public void testLabelBindings() { assertNull("row bindings", test.getRowLabelBindings()); assertNull("col bindings", test.getColumnLabelBindings()); Map<String, Integer> rowBindings = Maps.newHashMap(); rowBindings.put("Fee", 0); rowBindings.put("Fie", 1); test.setRowLabelBindings(rowBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); Map<String, Integer> colBindings = Maps.newHashMap(); colBindings.put("Foo", 0); colBindings.put("Bar", 1); test.setColumnLabelBindings(colBindings); assertEquals("row", rowBindings, test.getRowLabelBindings()); assertEquals("Fee", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); double[] newrow = {9, 8}; test.set("Fie", newrow); assertEquals("FeeBar", test.get(0, 1), test.get("Fee", "Bar"), EPSILON); }
private static void printMatrix(String name, Matrix m) { int rows = m.numRows(); int columns = m.numCols(); System.out.printf("%s - %d x %d\n", name, rows, columns); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.printf("%10.5f", m.get(i, j)); } System.out.printf("\n"); } System.out.printf("\n"); System.out.printf("\n"); }
@Test(expected = IndexException.class) public void testGetIndexOver() { for (int row = 0; row < test.rowSize() + 1; row++) { for (int col = 0; col < test.columnSize(); col++) { test.get(row, col); } } }
@Test(expected = IndexException.class) public void testGetIndexUnder() { for (int row = -1; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { test.get(row, col); } } }
@Test(expected = IndexException.class) public void testGetIndexOver() { for (int row = 0; row < test.rowSize() + 1; row++) { for (int col = 0; col < test.columnSize(); col++) { test.get(row, col); } } }
@Test(expected = IndexException.class) public void testGetIndexUnder() { for (int row = -1; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { test.get(row, col); } } }
@Test public void testGet() { for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row][col], test .get(row, col), EPSILON); } } }
@Test public void testGet() { for (int row = 0; row < test.rowSize(); row++) { for (int col = 0; col < test.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row + 1][col + 1], test.get(row, col), EPSILON); } } }
@Test public void testClone() { double oldValue = 1.23; double newValue = 2.34; double[][] values = {{oldValue, 3}, {3, 5}, {7, 9}}; Matrix matrix = matrixFactory(values); Matrix clone = matrix.clone(); clone.set(0, 0, newValue); //test whether the update in the clone is reflected in the original matrix assertEquals("Matrix clone is not independent of the original", oldValue, matrix.get(0, 0), EPSILON); }
@Test public void testColumnView() { assertEquals(test.rowSize(), test.viewColumn(0).size()); assertEquals(test.rowSize(), test.viewColumn(1).size()); Random gen = RandomUtils.getRandom(); for (int col = 0; col < test.columnSize(); col++) { int j = gen.nextInt(test.columnSize()); double old = test.get(col, j); double v = gen.nextGaussian(); test.viewColumn(col).set(j, v); assertEquals(v, test.get(j, col), 0); assertEquals(v, test.viewColumn(col).get(j), 0); test.set(j, col, old); assertEquals(old, test.get(j, col), 0); assertEquals(old, test.viewColumn(col).get(j), 0); } }
@Test public void testRowView() { assertEquals(test.columnSize(), test.viewRow(1).size()); assertEquals(test.columnSize(), test.viewRow(2).size()); Random gen = RandomUtils.getRandom(); for (int row = 0; row < test.rowSize(); row++) { int j = gen.nextInt(test.columnSize()); double old = test.get(row, j); double v = gen.nextGaussian(); test.viewRow(row).set(j, v); assertEquals(v, test.get(row, j), 0); assertEquals(v, test.viewRow(row).get(j), 0); test.set(row, j, old); assertEquals(old, test.get(row, j), 0); assertEquals(old, test.viewRow(row).get(j), 0); } }
@Test public void testTimesMatrix() { Matrix transpose = test.transpose(); Matrix value = test.times(transpose); assertEquals("rows", test.rowSize(), value.rowSize()); assertEquals("cols", test.rowSize(), value.columnSize()); Matrix expected = new DenseMatrix(new double[][]{{5.0, 11.0, 17.0}, {11.0, 25.0, 39.0}, {17.0, 39.0, 61.0}}).times(1.21); for (int i = 0; i < expected.numCols(); i++) { for (int j = 0; j < expected.numRows(); j++) { assertTrue("Matrix times transpose not correct: " + i + ", " + j + "\nexpected:\n\t" + expected + "\nactual:\n\t" + value, Math.abs(expected.get(i, j) - value.get(i, j)) < 1.0e-12); } } Matrix timestest = new DenseMatrix(10, 1); /* will throw ArrayIndexOutOfBoundsException exception without MAHOUT-26 */ timestest.transpose().times(timestest); }
@Test public void testViewPart() { int[] offset = {1, 1}; int[] size = {2, 1}; Matrix view = test.viewPart(offset, size); assertEquals(2, view.rowSize()); assertEquals(1, view.columnSize()); for (int row = 0; row < view.rowSize(); row++) { for (int col = 0; col < view.columnSize(); col++) { assertEquals("value[" + row + "][" + col + ']', values[row + 1][col + 1], view.get(row, col), EPSILON); } } }
@Test public void testBasics() { Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false); System.out.println(a.toString()); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus( new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10); assertEquals(4, a.get(0, 3), 1.0e-10); System.out.println(a); Matrix m = new DenseMatrix(4, 4).assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); System.out.println(m); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); System.out.println(a.plus(a)); assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }
/** * Predict the power law growth in number of unique samples from the first few data points. * Also check that the fitted growth coefficient is about right. * * @param m * @param currentIndex Total data points seen so far. Unique values should be log(currentIndex)*expectedCoefficient + offset. * @param expectedCoefficient What slope do we expect. * @return The predicted value for log(currentIndex) */ private static double predictSize(Matrix m, int currentIndex, double expectedCoefficient) { int rows = m.rowSize(); Matrix a = m.viewPart(0, rows, 1, 2); Matrix b = m.viewPart(0, rows, 0, 1); Matrix ata = a.transpose().times(a); Matrix atb = a.transpose().times(b); QRDecomposition s = new QRDecomposition(ata); Matrix r = s.solve(atb).transpose(); assertEquals(expectedCoefficient, r.get(0, 0), 0.2); return r.times(new DenseVector(new double[]{Math.log(currentIndex), 1})).get(0); }
@Test public void testBasics() { Matrix a = new UpperTriangular(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus( new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10); assertEquals(4, a.get(0, 3), 1.0e-10); print(a); Matrix m = new DenseMatrix(4, 4).assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); print(m); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }