public static void testGet(Matrix m) { int rows = m.rows(); int cols = m.columns(); for (int trials = 0; trials < 100; ++trials) { int i = (int) (rows * Math.random()); int j = (int) (cols * Math.random()); double v = m.get(i, j); double r = Math.random(); m.set(i, j, r); m.set(i, j, v); assertEquals(v, m.get(i, j), 0.01); } }
@Test public void testNonSquareMatrix() { int[][] testInput = { {6, 0, 5}, {0, 1, 1}, {1, 5, 0}, {0, 1, 0}, {1, 5, 7}, {1, 1, 1} }; double[][] testOutput = {{}}; double error = .00001; Matrix inputMatrix = new YaleSparseMatrix(6, 3); for (int row = 0; row < 6; ++row) for (int col = 0; col < 3; ++col) inputMatrix.set(row, col, testInput[row][col]); Transform transform = new TfIdfTransform(); Matrix outputMatrix = transform.transform(inputMatrix); for (int row = 0; row < 6; ++row) { for (int col = 0; col < 3; ++col) System.out.println(outputMatrix.get(row, col)); // assertEquals(testOutput[row][col], // outputMatrix.get(row, col), error); } }
@Test public void testSparseListMatrix() { List<SparseDoubleVector> vectors = new ArrayList<SparseDoubleVector>(); for (double[] values : VALUES) vectors.add(new CompactSparseVector(values)); Matrix m = new ListMatrix<SparseDoubleVector>(vectors); assertEquals(VALUES.length, m.rows()); assertEquals(VALUES[0].length, m.columns()); for (int r = 0; r < m.rows(); ++r) for (int c = 0; c < m.columns(); ++c) assertEquals(VALUES[r][c], m.get(r, c), .0001); m.set(0, 0, 0); assertEquals(0, vectors.get(0).get(0), .0001); for (int r = 0; r < m.rows(); ++r) assertEquals(vectors.get(r), m.getRowVector(r)); }
@Test public void testMatrixTransformSize() { int[][] testInput = { {0, 0, 0, 0, 1, 1, 2, 4, 5, 0}, {0, 1, 1, 2, 0, 1, 5, 2, 8, 10}, {1, 5, 0, 0, 1, 0, 6, 3, 7, 9}, {0, 1, 0, 1, 0, 1, 2, 0, 3, 0}, {1, 5, 7, 0, 0, 1, 6, 10, 2, 45}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; double[][] testOutput = { { 0.00000, 0.00000, 0.00000, 0.00000, 0.17028, 0.10217, 0.04644, 0.10217, 0.09824, 0.00000, }, { 0.00000, 0.00810, 0.01171, 0.05268, 0.00000, 0.02107, 0.02395, 0.01054, 0.03242, 0.01621, }, { 0.07438, 0.08582, 0.00000, 0.00000, 0.07438, 0.00000, 0.06086, 0.03347, 0.06008, 0.03090, }, { 0.00000, 0.03929, 0.00000, 0.12771, 0.00000, 0.10217, 0.04644, 0.00000, 0.05894, 0.00000, }, { 0.03512, 0.04052, 0.08195, 0.00000, 0.00000, 0.02107, 0.02873, 0.05268, 0.00810, 0.07294, }, { -0.03177, -0.00733, -0.01059, -0.02383, -0.03177, -0.01906, -0.00433, -0.00477, -0.00367, -0.00147, } }; double error = .00001; Matrix inputMatrix = new YaleSparseMatrix(6, 10); for (int row = 0; row < 6; ++row) for (int col = 0; col < 10; ++col) inputMatrix.set(row, col, testInput[row][col]); Transform transform = new TfIdfTransform(); Matrix outputMatrix = transform.transform(inputMatrix); for (int row = 0; row < 6; ++row) { for (int col = 0; col < 10; ++col) assertEquals(testOutput[row][col], outputMatrix.get(row, col), error); } }