@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 populate() { int row = 2, column = 2; int[] input = {1, 2, 3, 4}; int[][] expected = {{1, 2}, {3, 4}}; Matrix actualMatrix = new Matrix(row, column); actualMatrix.populate(input); assertArrayEquals(actualMatrix.matrix, expected); }
@Test public void vectorProductTest() { Matrix m = Matrix.identityMatrix(); Vector v = new Vector(2, 3, 4); Vector out = m.product(v); assertEquals(2, out.x, 0); assertEquals(3, out.y, 0); assertEquals(4, out.z, 0); }
@Test public void determinantOfMatrix() { int[] numbers = {3, 4, 5, 6, 7, 8, 1, 9, 6}; Matrix matrix = new Matrix(3, 3); matrix.populate(numbers); int[][] input = matrix.matrix; int result = matrix.determinant(input, 3); System.out.println(result); assertEquals(result, 33); }
@Test public void testIsSquaredShouldFail() throws Exception { Matrix a = new Matrix( new double[][] { {2, 1, 1}, {1, -1, -1}, }); assertEquals(false, a.isSquared()); }
@Test public void testIsRegularShouldSuccess() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {3, 2, 1}, {2, 1, 3} }); assertEquals(true, a.isRegular()); }
public static void testSet(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 = Math.random(); m.set(i, j, v); assertEquals(v, m.get(i, j), 0.01); } }
@Test public void testIsTriangularAnyShouldSuccess() throws Exception { Matrix a = new Matrix( new double[][] { {2, 0, 0}, {1, 1, 0}, {1, 2, 1} }); assertEquals(true, a.isTriangular(Matrix.TriangleType.ANY, false)); }
@Test public void testGetDeterminant() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {3, 2, 1}, {2, 1, 3} }); double expected = -12; assertEquals(expected, a.getDeterminant(), 0); }
@Test public void testGetTrace() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {3, 2, 1}, {2, 1, 3} }); double expected = 6.0; assertEquals(expected, a.getTrace(), 0); }
@Test public void testGetTransposed() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {3, 2, 1}, {2, 1, 3} }); Matrix c = a.getTransposed(); Matrix r = new Matrix( new double[][] { {1, 3, 2}, {2, 2, 1}, {3, 1, 3} }); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(r.getElement(i, j), c.getElement(i, j), 0); } } }
@Test public void testGetCoMatrix() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {4, 5, 6}, {5, 4, 3} }); Matrix c = a.getCoMatrix(); Matrix r = new Matrix( new double[][] { {-9, 18, -9}, {6, -12, 6}, {-3, 6, -3} }); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(r.getElement(i, j), c.getElement(i, j), 0); } } }
@Test public void testGetInversed() throws Exception { Matrix a = new Matrix( new double[][] { {2, 1, 1}, {1, -1, -1}, {1, 2, 1} }); Matrix c = a.getInversed(); Matrix r = new Matrix( new double[][] { {1.0 / 3.0, 1.0 / 3.0, 0}, {-(2.0 / 3.0), 1.0 / 3.0, 1}, {1, -1, -1} }); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(r.getElement(i, j), c.getElement(i, j), 0); } } }
@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); } }
@Test public void testScalarProduct() throws Exception { Matrix a = new Matrix(new double[][] {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}}); Matrix b = a.scalarProduct(3); Matrix r = new Matrix(new double[][] {{3, 6, 9}, {9, 6, 3}, {6, 3, 9}}); for (int i = 0; i < b.getNumRows(); i++) { for (int j = 0; j < b.getNumColumns(); j++) { assertEquals(r.getElement(i, j), b.getElement(i, j), 0); } } }
@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 testAddShouldPass() throws Exception { Matrix a = new Matrix(new double[][] {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}}); Matrix b = new Matrix(new double[][] {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}}); Matrix c = a.add(b); Matrix r = new Matrix(new double[][] {{2, 4, 6}, {6, 4, 2}, {4, 2, 6}}); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(r.getElement(i, j), c.getElement(i, j), 0); } } }
@Test public void testIsTriangularStrictAnyShouldSuccess() throws Exception { Matrix a = new Matrix( new double[][] { {0, 0, 0}, {1, 0, 0}, {1, 2, 0} }); assertEquals(true, a.isTriangular(Matrix.TriangleType.ANY, true)); Matrix b = new Matrix( new double[][] { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }); assertEquals(true, b.isTriangular(Matrix.TriangleType.INFERIOR, true)); }
@Test public void testMatrixMultiplication() throws Exception { Matrix a = new Matrix(new double[][] {{1, 2, 3}, {3, 2, 1}, {2, 1, 3}}); Matrix b = new Matrix(new double[][] {{4, 5, 6}, {6, 5, 4}, {4, 6, 5}}); Matrix c = a.matrixMultiplication(b); Matrix r = new Matrix(new double[][] {{28, 33, 29}, {28, 31, 31}, {26, 33, 31}}); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(r.getElement(i, j), c.getElement(i, j), 0); } } }
@Test public void multiplyTwoMatrix() { int row = 2, column = 2; int[] firstArrayInput = {1, 2, 3, 4}; int[] secondArrayInput = {2, 3, 4, 5}; int[] expectedOutput = {11, 16, 19, 28}; Matrix firstMatrix = new Matrix(row, column); Matrix secondMatrix = new Matrix(row, column); Matrix expectedMatrix = new Matrix(row, column); firstMatrix.populate(firstArrayInput); secondMatrix.populate(secondArrayInput); expectedMatrix.populate(expectedOutput); Matrix destinationMatrix = firstMatrix.multiply(secondMatrix); assertArrayEquals(destinationMatrix.matrix, expectedMatrix.matrix); }
@Test public void testGetSubmatrix() throws Exception { Matrix a = new Matrix( new double[][] { {1, 2, 3}, {3, 2, 1}, {2, 1, 3} }); Matrix b = a.getSubmatrix(1, 1); Matrix c = new Matrix( new double[][] { {1, 3}, {2, 3} }); for (int i = 0; i < c.getNumRows(); i++) { for (int j = 0; j < c.getNumColumns(); j++) { assertEquals(c.getElement(i, j), b.getElement(i, j), 0); } } }
@Test public void basicOpsTest() { /** TRANSPOSE */ double[][] basicArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; Matrix m = new Matrix(basicArray); Matrix t = m.transpose(); double[][] tArray = {{1, 4, 7, 10}, {2, 5, 8, 11}, {3, 6, 9, 12}}; assertTrue(t.equals(new Matrix(tArray))); double[][] oneElArr = {{4}}; Matrix oneEl = new Matrix(oneElArr); assertTrue(oneEl.transpose().equals(oneEl)); /** MULTIPLICATION */ Matrix scaledThree = m.multiply(3); double[][] scaledThreeArr = {{3, 6, 9}, {12, 15, 18}, {21, 24, 27}, {30, 33, 36}}; assertTrue(scaledThree.equals(new Matrix(scaledThreeArr))); double[][] oneCol = {{1}, {1}, {1}}; Matrix prod = m.multiply(new Matrix(oneCol)); double[][] oneColProd = {{6}, {15}, {24}, {33}}; assertTrue(prod.equals(new Matrix(oneColProd))); /** TOSTRING */ assertEquals( m.toString(), "[[ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ], [ 10.0, 11.0, 12.0 ] ]"); }
@Test public void testIsRegularShouldFail() throws Exception { Matrix a = new Matrix(new double[][] {{1, 0, 3}, {3, 0, 1}, {2, 0, 3}}); assertEquals(false, a.isRegular()); }
@Test public void moreOpsTest() { double[][] basicArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; Matrix m = new Matrix(basicArray); /** MINOR ARRAY (2, 2) */ double[][] minorArr = {{1, 3}, {7, 9}, {10, 12}}; assertTrue(m.minor(2, 2).equals(new Matrix(minorArr))); /** DETERMINANT AND INVERSE */ double[][] basicSquare = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Matrix basicSquareMatrix = new Matrix(basicSquare); assertTrue(basicSquareMatrix.det() == 0); assertFalse(basicSquareMatrix.invertible()); // since det = 0; double[][] invertibleArr = {{7, 2, 1}, {0, 3, -1}, {-3, 4, -2}}; Matrix invertibleMatrix = new Matrix(invertibleArr); double[][] inverse = {{-2, 8, -5}, {3, -11, 7}, {9, -34, 21}}; assertTrue(invertibleMatrix.inverse().equals(new Matrix(inverse))); /** ECHELON FORM */ double[][] echelonRes1 = {{1, 2, 0}, {0, 1, 1.25}, {0, 0, 1}}; Matrix echelonMatrix1 = new Matrix(echelonRes1); double[][] testArr = {{1, 2, 0}, {0, 4, 5}, {3, 2, 6}}; Matrix testMatrix = new Matrix(testArr); testMatrix.echelonForm(); double[][] testArr2 = {{0, 4, 5}, {1, 2, 0}, {3, 2, 6}}; Matrix testMatrix2 = new Matrix(testArr2); testMatrix2.echelonForm(); assertTrue(testMatrix.equals(echelonMatrix1)); assertTrue(testMatrix2.equals(echelonMatrix1)); double[][] identityArr = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; Matrix identityMatrix = new Matrix(identityArr); testMatrix.reducedEchelonForm(); assertTrue(testMatrix.equals(identityMatrix)); double[][] moreRows = {{4, 5}, {2, 1}, {0, 0}}; Matrix moreRowsMatrix = new Matrix(moreRows); double[][] goodMoreRows = {{1, 0}, {0, 1}, {0, 0}}; Matrix goodMoreRowsMatrix = new Matrix(goodMoreRows); moreRowsMatrix.reducedEchelonForm(); assertTrue(moreRowsMatrix.equals(goodMoreRowsMatrix)); double[][] moreCols = {{2, 4, 8}, {1, 2, 9}}; Matrix moreColsMatrix = new Matrix(moreCols); double[][] goodMoreCols = {{1, 2, 0}, {0, 0, 1}}; Matrix goodMoreColsMatrix = new Matrix(goodMoreCols); moreColsMatrix.reducedEchelonForm(); assertTrue(moreColsMatrix.equals(goodMoreColsMatrix)); /** PIVOT COUNT */ assertTrue(moreColsMatrix.pivots() == 2); assertTrue(moreRowsMatrix.pivots() == 2); assertTrue(testMatrix.pivots() == 3); }