//Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 //Modified: 08-10-24 package com.mattrixwv.matrix; import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; import org.junit.jupiter.api.Test; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestDoubleMatrix{ private static final double[][] negativeGrid2 = new double[][]{ {-1, -2}, {-3, -4} }; private static final double[][] negativeGrid10 = new double[][]{ { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} }; private static final double[][] negativeGrid2x10 = new double[][]{ { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; private static final double[][] negativeGrid10x2 = new double[][]{ { -1, -2}, { -3, -4}, { -5, -6}, { -7, -8}, { -9, -10}, {-11, -12}, {-13, -14}, {-15, -16}, {-17, -18}, {-19, -20} }; //! Constructor @Test public void testConstructor_default(){ DoubleMatrix matrix = new DoubleMatrix(); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_fill0Rows(){ assertThrows(InvalidGeometryException.class, () -> { new DoubleMatrix(0, 1, 0); }); } @Test public void testConstructor_fill0Cols(){ assertThrows(InvalidGeometryException.class, () -> { new DoubleMatrix(1, 0, 0); }); } @Test public void testConstructor_fillSize2(){ DoubleMatrix matrix = new DoubleMatrix(2, 2, -1); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertEquals(-1, matrix.get(0, 0)); assertEquals(-1, matrix.get(0, 1)); assertEquals(-1, matrix.get(1, 0)); assertEquals(-1, matrix.get(1, 1)); } @Test public void testConstructor_fillSize10(){ DoubleMatrix matrix = new DoubleMatrix(10, 10, -1); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(double[] row : matrix.copyGrid()){ for(double num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_arraySize0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_arraySize2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testConstructor_arraySize10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testConstructor_arraySize2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testConstructor_arraySize10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testConstructor_arrayUnevenRows(){ double[][] grid = new double[][]{ {-1, -2, -3}, {-4, -5}, {-6, -7, -8, -9}, {-10, -11, -12} }; assertThrows(InvalidRowSizeException.class, () -> { new DoubleMatrix(grid); }); } @Test public void testConstructor_matrixSize0(){ DoubleMatrix originalMatrix = new DoubleMatrix(); DoubleMatrix matrix = new DoubleMatrix(originalMatrix); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_matrixSize2(){ DoubleMatrix originalMatrix = new DoubleMatrix(2, 2, -1); DoubleMatrix matrix = new DoubleMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(double[] row : matrix.copyGrid()){ for(double num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10(){ DoubleMatrix originalMatrix = new DoubleMatrix(10, 10, -1); DoubleMatrix matrix = new DoubleMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(double[] row : matrix.copyGrid()){ for(double num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize2x10(){ DoubleMatrix originalMatrix = new DoubleMatrix(2, 10, -1); DoubleMatrix matrix = new DoubleMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(double[] row : matrix.copyGrid()){ for(double num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10x2(){ DoubleMatrix originalMatrix = new DoubleMatrix(10, 2, -1); DoubleMatrix matrix = new DoubleMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(double[] row : matrix.copyGrid()){ for(double num : row){ assertEquals(-1, num); } } } //! setGrid() @Test public void testSetGrid_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(grid); assertEquals(2, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2(){ DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testSetGrid_size10(){ DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testSetGrid_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testSetGrid_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(); matrix.setGrid(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testCopyGrid_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testCopyGrid_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } //! isSquare() @Test public void testIsSquare_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertFalse(matrix.isSquare()); } //! laplaceExpansionHelper() @Test public void testLaplaceExpansionHelper_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix result = matrix.laplaceExpansionHelper(0, 0); assertArrayEquals(new double[][]{{-4}}, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size2_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(-1, 0); }); } @Test public void testLaplaceExpansionHelper_size2_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(2, 0); }); } @Test public void testLaplaceExpansionHelper_size2_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, -1); }); } @Test public void testLaplaceExpansionHelper_size2_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, 2); }); } @Test public void testLaplaceExpansionHelper_size10_loc0x0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); DoubleMatrix result = matrix.laplaceExpansionHelper(0, 0); double[][] expectedGrid = new double[][]{ {-12, -13, -14, -15, -16, -17, -18, -19, -20}, {-22, -23, -24, -25, -26, -27, -28, -29, -30}, {-32, -33, -34, -35, -36, -37, -38, -39, -40}, {-42, -43, -44, -45, -46, -47, -48, -49, -50}, {-52, -53, -54, -55, -56, -57, -58, -59, -60}, {-62, -63, -64, -65, -66, -67, -68, -69, -70}, {-72, -73, -74, -75, -76, -77, -78, -79, -80}, {-82, -83, -84, -85, -86, -87, -88, -89, -90}, {-92, -93, -94, -95, -96, -97, -98, -99, -100} }; assertArrayEquals(expectedGrid, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size10_loc4x4(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); DoubleMatrix result = matrix.laplaceExpansionHelper(4, 4); double[][] expectedGrid = new double[][]{ { -1, -2, -3, -4, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -16, -17, -18, -19, -20}, {-21, -22, -23, -24, -26, -27, -28, -29, -30}, {-31, -32, -33, -34, -36, -37, -38, -39, -40}, {-51, -52, -53, -54, -56, -57, -58, -59, -60}, {-61, -62, -63, -64, -66, -67, -68, -69, -70}, {-71, -72, -73, -74, -76, -77, -78, -79, -80}, {-81, -82, -83, -84, -86, -87, -88, -89, -90}, {-91, -92, -93, -94, -96, -97, -98, -99, -100} }; assertArrayEquals(expectedGrid, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } //! get() @Test public void testGet_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(2, 0); }); } @Test public void testGet_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(-1, 0); }); } @Test public void testGet_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, 2); }); } @Test public void testGet_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, -1); }); } @Test public void testGet(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(-1, matrix.get(0, 0)); assertEquals(-2, matrix.get(0, 1)); assertEquals(-3, matrix.get(1, 0)); assertEquals(-4, matrix.get(1, 1)); } //! getRow() @Test public void testGetRow_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(2); }); } @Test public void testGetRow_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(-1); }); } @Test public void testGetRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(new double[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); assertArrayEquals(new double[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); } //! getColumn() @Test public void testGetCol_0Rows(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(0); }); } @Test public void testGetCol_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(2); }); } @Test public void testGetCol_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(-1); }); } @Test public void testGetCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(new double[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); assertArrayEquals(new double[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); } //! getNumRows() @Test public void testGetNumRows_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); } @Test public void testGetNumRows_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); } //! getNumCols() @Test public void testGetNumCols_size0(){ double[][] grid = new double[0][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(2, matrix.getNumCols()); } @Test public void testGetNumCols_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertEquals(2, matrix.getNumCols()); } //! set() @Test public void testSet_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(-1, 0, 0); }); } @Test public void testSet_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(2, 0, 0); }); } @Test public void testSet_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, -1, 0); }); } @Test public void testSet_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, 2, 0); }); } @Test public void testSet(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.set(0, 0, -5); matrix.set(0, 1, -6); matrix.set(1, 0, -7); matrix.set(1, 1, -8); assertEquals(-5, matrix.get(0, 0)); assertEquals(-6, matrix.get(0, 1)); assertEquals(-7, matrix.get(1, 0)); assertEquals(-8, matrix.get(1, 1)); } //! setRow() @Test public void testSetRow_array_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new double[]{0}); }); } @Test public void testSetRow_array_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new double[]{0}); }); } @Test public void testSetRow_array_nullArray(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, (double[])null); }); } @Test public void testSetRow_array_arrayLength0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new double[0]); }); } @Test public void testSetRow_array_invalidArrayLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new double[]{0, 0, 0}); }); } @Test public void testSetRow_array(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.setRow(0, new double[]{-5, -6}); matrix.setRow(1, new double[]{-7, -8}); assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetRow_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setRow(0, (DoubleMatrix)null); }); } @Test public void testSetRow_matrix_multipleRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new DoubleMatrix(new double[][]{{0, 0}, {0, 0}})); }); } @Test public void testSetRow_matrix_negativeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new DoubleMatrix(new double[][]{{0}})); }); } @Test public void testSetRow_matrix_largeRow(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new DoubleMatrix(new double[][]{{0}})); }); } @Test public void testSetRow_matrix_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new DoubleMatrix(new double[][]{{}})); }); } @Test public void testSetRow_matrix_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new DoubleMatrix(new double[][]{{0, 0, 0}})); }); } @Test public void testSetRow_matrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.setRow(0, new DoubleMatrix(new double[][]{{-5, -6}})); matrix.setRow(1, new DoubleMatrix(new double[][]{{-7, -8}})); assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! setCol() @Test public void testSetCol_array_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new double[]{0}); }); } @Test public void testSetCol_array_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new double[]{0}); }); } @Test public void testSetCol_array_nullArray(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, (double[])null); }); } @Test public void testSetCol_array_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new double[0]); }); } @Test public void testSetCol_array_invalidArrayLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new double[]{0, 0, 0}); }); } @Test public void testSetCol_array(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.setCol(0, new double[]{-5, -7}); matrix.setCol(1, new double[]{-6, -8}); assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetCol_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setCol(0, (DoubleMatrix)null); }); } @Test public void testSetCol_matrix_multipleCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testSetCol_matrix_negativeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new DoubleMatrix(new double[][]{{0}})); }); } @Test public void testSetCol_matrix_largeCol(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new DoubleMatrix(new double[][]{{0}})); }); } @Test public void testSetCol_matrix_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new DoubleMatrix(new double[][]{{}})); }); } @Test public void testSetCol_matrix_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new DoubleMatrix(new double[][]{{0, 0, 0}})); }); } @Test public void testSetCol_matrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.setCol(0, new DoubleMatrix(new double[][]{{-5}, {-7}})); matrix.setCol(1, new DoubleMatrix(new double[][]{{-6}, {-8}})); assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! addRow() @Test public void testAddRow_array_nullArray(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((double[])null); }); } @Test public void testAddRow_array_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new double[0]); }); } @Test public void testAddRow_array_invalidArrayLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new double[]{0, 0, 0}); }); } @Test public void testAddRow_array(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.addRow(new double[]{-5, -6}); assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } @Test public void testAddRow_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((DoubleMatrix)null); }); } @Test public void testAddRow_matrix_multipleRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}})); }); } @Test public void testAddRow_matrix_noRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new DoubleMatrix(new double[0][0])); }); } @Test public void testAddRow_matrix_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new DoubleMatrix(new double[][]{{0, 0, 0}})); }); } @Test public void testAddRow_matrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.addRow(new DoubleMatrix(new double[][]{{-5, -6}})); assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } //! addCol() @Test public void testAddCol_array_nullArray(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((double[])null); }); } @Test public void testAddCol_array_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new double[0]); }); } @Test public void testAddCol_array_invalidArrayLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new double[]{0, 0, 0}); }); } @Test public void testAddCol_array(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.addCol(new double[]{-5, -6}); assertArrayEquals(new double[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } @Test public void testAddCol_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((DoubleMatrix)null); }); } @Test public void testAddCol_matrix_multipleCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testAddCol_matrix_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new DoubleMatrix(new double[][]{{}})); }); } @Test public void testAddCol_matrix_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new DoubleMatrix(new double[][]{{0, 0, 0}})); }); } @Test public void testAddCol_matrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix.addCol(new DoubleMatrix(new double[][]{{-5}, {-6}})); assertArrayEquals(new double[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } //! appendRight() @Test public void testAppendRight_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendRight((DoubleMatrix)null); }); } @Test public void testAppendRight_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new DoubleMatrix(new double[0][0])); }); } @Test public void testAppendRight_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new DoubleMatrix(new double[][]{{0, 0, 0}})); }); } @Test public void testAppendRight(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.appendRight(matrix); assertArrayEquals(new double[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); } //! appendBottom() @Test public void testAppendBottom_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendBottom((DoubleMatrix)null); }); } @Test public void testAppendBottom_length0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new DoubleMatrix(new double[0][0])); }); } @Test public void testAppendBottom_invalidLength(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new DoubleMatrix(new double[][]{{0}, {0}, {0}})); }); } @Test public void testAppendBottom(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.appendBottom(matrix); assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); } //! add() @Test public void testAdd_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.add((DoubleMatrix)null); }); } @Test public void testAdd_matrix_fewRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testAdd_matrix_manyRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new DoubleMatrix(new double[][]{{0}, {0}})); }); } @Test public void testAdd_matrix_fewCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new DoubleMatrix(new double[][]{{0}})); }); } @Test public void testAdd_matrix_manyCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testAdd_matrix_size2(){ DoubleMatrix addMatrix = new DoubleMatrix(new double[][]{{-4, -3}, {-2, -1}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.add(addMatrix); assertArrayEquals(new double[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); } @Test public void testAdd_matrix_size10(){ DoubleMatrix addMatrix = new DoubleMatrix(new double[][]{ {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} }); DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); matrix = matrix.add(addMatrix); assertArrayEquals(new double[][]{ {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101} }, matrix.copyGrid()); } @Test public void testAdd_scalar(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.add(1); assertArrayEquals(new double[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); } //! subtract() @Test public void testSubtract_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.subtract((DoubleMatrix)null); }); } @Test public void testSubtract_matrix_fewRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testSubtract_matrix_manyRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testSubtract_matrix_fewCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new DoubleMatrix(new double[][]{{0}, {0}})); }); } @Test public void testSubtract_matrix_manyCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testSubtract_matrix_size2(){ DoubleMatrix subMatrix = new DoubleMatrix(new double[][]{{-4, -3}, {-2, -1}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.subtract(subMatrix); assertArrayEquals(new double[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); } @Test public void testSubtract_matrix_size10(){ DoubleMatrix subMatrix = new DoubleMatrix(new double[][]{ {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} }); DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); matrix = matrix.subtract(subMatrix); assertArrayEquals(new double[][]{ { 99, 97, 95, 93, 91, 89, 87, 85, 83, 81}, { 79, 77, 75, 73, 71, 69, 67, 65, 63, 61}, { 59, 57, 55, 53, 51, 49, 47, 45, 43, 41}, { 39, 37, 35, 33, 31, 29, 27, 25, 23, 21}, { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, { -1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, {-21, -23, -25, -27, -29, -31, -33, -35, -37, -39}, {-41, -43, -45, -47, -49, -51, -53, -55, -57, -59}, {-61, -63, -65, -67, -69, -71, -73, -75, -77, -79}, {-81, -83, -85, -87, -89, -91, -93, -95, -97, -99} }, matrix.copyGrid()); } @Test public void testSubtract_scalar(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.subtract(1); assertArrayEquals(new double[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); } //! multiply() @Test public void testMultiply_matrix_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.multiply((DoubleMatrix)null); }); } @Test public void testMultiply_matrix_manyRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testMultiply_matrix_fewRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testMultiply_matrix_square(){ DoubleMatrix mulMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new double[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); } @Test public void testMultiply_matrix_rectangle(){ DoubleMatrix mulMatrix = new DoubleMatrix(new double[][]{{1, 2, 3}, {4, 5, 6}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new double[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); } @Test public void testMultiply_scalar(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); matrix = matrix.multiply(2); assertArrayEquals(new double[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); } //! pow @Test public void testPow_rectangle(){ DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1, 2, 3}, {4, 5, 6}}); assertThrows(InvalidGeometryException.class, () -> { matrix.pow(2); }); } @Test public void testPow_negative(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidScalarException.class, () -> { matrix.pow(-1); }); } @Test public void testPow_0(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix result = matrix.pow(0); assertEquals(new DoubleMatrix(2, 2, 1), result); } @Test public void testPow_2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix result = matrix.pow(2); assertEquals(new DoubleMatrix(new double[][]{{7, 10}, {15, 22}}), result); } @Test public void testPow_3(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix result = matrix.pow(3); assertEquals(new DoubleMatrix(new double[][]{{-37, -54}, {-81, -118}}), result); } //! dotProduct() @Test public void testDotProduct_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.dotProduct((DoubleMatrix)null); }); } @Test public void testDotProduct_fewRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testDotProduct_manyRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testDotProduct_size2(){ DoubleMatrix dotMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); double result = matrix.dotProduct(dotMatrix); assertEquals(-54, result); } @Test public void testDotProduct_size10(){ DoubleMatrix dotMatrix = new DoubleMatrix(new double[][]{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} }); DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); double result = matrix.dotProduct(dotMatrix); assertEquals(-2632750, result); } //! hadamardProduct() @Test public void testHadamardProduct_nullMatrix(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.hadamardProduct((DoubleMatrix)null); }); } @Test public void testHadamardProduct_fewRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0}})); }); } @Test public void testHadamardProduct_manyRows(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testHadamardProduct_fewCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0}, {0}})); }); } @Test public void testHadamardProduct_manyCols(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testHadamardProduct_size2(){ DoubleMatrix hadMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new double[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); } @Test public void testHadamardProduct_size10(){ DoubleMatrix hadMatrix = new DoubleMatrix(new double[][]{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} }); DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); DoubleMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new double[][]{ { -1, -4, -9, -16, -25, -36, -49, -64, -81, -100}, { -121, -144, -169, -196, -225, -256, -289, -324, -361, -400}, { -441, -484, -529, -576, -625, -676, -729, -784, -841, -900}, { -961, -1024, -1089, -1156, -1225, -1296, -1369, -1444, -1521, -1600}, {-1681, -1764, -1849, -1936, -2025, -2116, -2209, -2304, -2401, -2500}, {-2601, -2704, -2809, -2916, -3025, -3136, -3249, -3364, -3481, -3600}, {-3721, -3844, -3969, -4096, -4225, -4356, -4489, -4624, -4761, -4900}, {-5041, -5184, -5329, -5476, -5625, -5776, -5929, -6084, -6241, -6400}, {-6561, -6724, -6889, -7056, -7225, -7396, -7569, -7744, -7921, -8100}, {-8281, -8464, -8649, -8836, -9025, -9216, -9409, -9604, -9801, -10000} }, result.copyGrid()); } //! transpose() @Test public void testTranspose_size0(){ DoubleMatrix matrix = new DoubleMatrix(); DoubleMatrix result = matrix.transpose(); assertEquals(new DoubleMatrix(), result); } @Test public void testTranspose_size0x2(){ double[][] grid = new double[0][2]; DoubleMatrix matrix = new DoubleMatrix(grid); assertArrayEquals(new double[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2x0(){ double[][] grid = new double[2][0]; DoubleMatrix matrix = new DoubleMatrix(grid); assertArrayEquals(new double[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(new double[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); } @Test public void testTranspose_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertArrayEquals(new double[][]{ {-1, -11, -21, -31, -41, -51, -61, -71, -81, -91}, {-2, -12, -22, -32, -42, -52, -62, -72, -82, -92}, {-3, -13, -23, -33, -43, -53, -63, -73, -83, -93}, {-4, -14, -24, -34, -44, -54, -64, -74, -84, -94}, {-5, -15, -25, -35, -45, -55, -65, -75, -85, -95}, {-6, -16, -26, -36, -46, -56, -66, -76, -86, -96}, {-7, -17, -27, -37, -47, -57, -67, -77, -87, -97}, {-8, -18, -28, -38, -48, -58, -68, -78, -88, -98}, {-9, -19, -29, -39, -49, -59, -69, -79, -89, -99}, {-10, -20, -30, -40, -50, -60, -70, -80, -90, -100} }, matrix.transpose().copyGrid()); } @Test public void testTranspose_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertArrayEquals(new double[][]{ {-1, -11}, {-2, -12}, {-3, -13}, {-4, -14}, {-5, -15}, {-6, -16}, {-7, -17}, {-8, -18}, {-9, -19}, {-10, -20} }, matrix.transpose().copyGrid()); } @Test public void testTranspose_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertArrayEquals(new double[][]{ {-1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20} }, matrix.transpose().copyGrid()); } //! determinant() / det() @Test public void testDeterminant_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size1(){ DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); assertEquals(1, matrix.determinant()); assertEquals(1, matrix.det()); } @Test public void testDeterminant_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(-2, matrix.determinant()); assertEquals(-2, matrix.det()); } @Test public void testDeterminant_size3(){ DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } //! cofactor() / cof() @Test public void testCofactor_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size1(){ double[][] expectedGrid = new double[][]{{1}}; DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2(){ double[][] expectedGrid = new double[][]{{-4, 3}, {2, -1}}; DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size10(){ double[][] expectedGrid = new double[][]{ { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D} }; DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } //! adjoint() / adj() @Test public void testAdjoint_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size1(){ double[][] expectedGrid = new double[][]{{1}}; DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2(){ double[][] expectedGrid = new double[][]{{-4, 2}, {3, -1}}; DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size10(){ double[][] expectedGrid = new double[][]{ { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D} }; DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } //! inverse() @Test public void testInverse_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size1(){ double[][] expectedGrid = new double[][]{{-1}}; DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size2(){ double[][] expectedGrid = new double[][]{{-2, 3}, {3, -4}}; DoubleMatrix matrix = new DoubleMatrix(new double[][]{{4, 3}, {3, 2}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertThrows(InvalidScalarException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size2x10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size10x2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } //! equals() @Test public void testEquals_null(){ DoubleMatrix matrix = new DoubleMatrix(); assertFalse(matrix.equals(null)); } @Test public void testEquals_matrixObject(){ DoubleMatrix equalsMatrix = new DoubleMatrix(negativeGrid2); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertTrue(matrix.equals((Object)equalsMatrix)); } @Test public void testEquals_array(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertTrue(matrix.equals(negativeGrid2)); } @Test public void testEquals_invalidType(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertFalse(matrix.equals(1)); } @Test public void testEquals_manyRows(){ DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewRows(){ DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_manyCols(){ DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2, -3}, {-4, -5, -6}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewCols(){ DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1}, {-2}}); DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_notEquals(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix equalsMatrix = matrix.transpose(); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_equals(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); DoubleMatrix equalsMatrix = new DoubleMatrix(negativeGrid2); assertTrue(matrix.equals(equalsMatrix)); } @Test public void testEquals_self(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertTrue(matrix.equals(matrix)); } //! hashCode() @Test public void testHashCode_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size1(){ DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } //! toString() @Test public void testToString_size0(){ DoubleMatrix matrix = new DoubleMatrix(); assertEquals("[]", matrix.toString()); } @Test public void testToString_size1(){ DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); assertEquals("[1.0]", matrix.toString()); } @Test public void testToString_size2(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); assertEquals("[-1.0, -2.0]\n[-3.0, -4.0]", matrix.toString()); } @Test public void testToString_size10(){ DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); assertEquals( "[-1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -10.0]\n" + "[-11.0, -12.0, -13.0, -14.0, -15.0, -16.0, -17.0, -18.0, -19.0, -20.0]\n" + "[-21.0, -22.0, -23.0, -24.0, -25.0, -26.0, -27.0, -28.0, -29.0, -30.0]\n" + "[-31.0, -32.0, -33.0, -34.0, -35.0, -36.0, -37.0, -38.0, -39.0, -40.0]\n" + "[-41.0, -42.0, -43.0, -44.0, -45.0, -46.0, -47.0, -48.0, -49.0, -50.0]\n" + "[-51.0, -52.0, -53.0, -54.0, -55.0, -56.0, -57.0, -58.0, -59.0, -60.0]\n" + "[-61.0, -62.0, -63.0, -64.0, -65.0, -66.0, -67.0, -68.0, -69.0, -70.0]\n" + "[-71.0, -72.0, -73.0, -74.0, -75.0, -76.0, -77.0, -78.0, -79.0, -80.0]\n" + "[-81.0, -82.0, -83.0, -84.0, -85.0, -86.0, -87.0, -88.0, -89.0, -90.0]\n" + "[-91.0, -92.0, -93.0, -94.0, -95.0, -96.0, -97.0, -98.0, -99.0, -100.0]", matrix.toString()); } //! generateIdentity() @Test public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { DoubleMatrix.generateIdentity(0); }); } @Test public void testGenerateIdentity_negativeSize(){ assertThrows(InvalidGeometryException.class, () -> { DoubleMatrix.generateIdentity(-1); }); } @Test public void testGenerateIdentity_size2(){ DoubleMatrix matrix = DoubleMatrix.generateIdentity(2); assertArrayEquals(new double[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size3(){ DoubleMatrix matrix = DoubleMatrix.generateIdentity(3); assertArrayEquals(new double[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size10(){ DoubleMatrix matrix = DoubleMatrix.generateIdentity(10); assertArrayEquals(new double[][]{ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} }, matrix.copyGrid()); } }