//Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 //Modified: 02-09-22 package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import java.util.Arrays; import org.junit.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{ //Grid 1x1 private static final double[][] grid1 = { {0.5} }; private static final double[][] transformGrid1_1 = { {0.5} }; private static final double[][] transformGrid1_2 = { {1.5} }; //Grid 2x2 private static final double[][] grid2 = { {0.5, 1.5}, {0.5, 1.5} }; private static final double[][] transformGrid2_1 = { {0.5, 0}, {0.5, 0} }; private static final double[][] transformGrid2_2 = { {1.5, 2.5}, {1.5, 2.5} }; //Grid 3x3 private static final double[][] grid3 = { {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }; private static final double[][] transformGrid3_1 = { {1.5, 0.5, 0}, {1.5, 0.5, 0}, {1.5, 0.5, 0} }; private static final double[][] transformGrid3_2 = { {1.5, 2.5, 3.5}, {1.5, 2.5, 3.5}, {1.5, 2.5, 3.5} }; //Grid 4x4 private static final double[][] grid4 = { {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }; private static final double[][] transformGrid4_1 = { {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0} }; private static final double[][] transformGrid4_2 = { {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0}, {2.5, 1.5, 0.5, 0} }; //Grid 10x10 private static final double[][] grid10 = { {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }; private static final double[][] transformGrid10_1 = { {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0} }; private static final double[][] transformGrid10_2 = { {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} }; @Test public void testConstructor(){ //Default constructor DoubleMatrix matrix = new DoubleMatrix(); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); //Filler constructor //0 rows assertThrows(InvalidGeometryException.class, () -> { new DoubleMatrix(0, 0, 0.0); }); //0 cols assertThrows(InvalidGeometryException.class, () -> { new DoubleMatrix(1, 0, 0.0); }); //Good values matrix = new DoubleMatrix(2, 2, 0.0); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertEquals(0.0, matrix.get(0, 0), 0.0001); assertEquals(0.0, matrix.get(0, 1), 0.0001); assertEquals(0.0, matrix.get(1, 0), 0.0001); assertEquals(0.0, matrix.get(1, 1), 0.0001); //Matrix constructor matrix.set(0, 0, 1.0); matrix.set(0, 1, 2.0); matrix.set(1, 0, 1.0); matrix.set(1, 1, 2.0); DoubleMatrix matrix2 = new DoubleMatrix(matrix); assertEquals(2, matrix2.getNumRows()); assertEquals(2, matrix2.getNumCols()); assertEquals(1.0, matrix2.get(0, 0), 0.0001); assertEquals(2.0, matrix2.get(0, 1), 0.0001); assertEquals(1.0, matrix2.get(1, 0), 0.0001); assertEquals(2.0, matrix2.get(1, 1), 0.0001); //Array constructor //0 length double[][] grid = new double[0][0]; matrix = new DoubleMatrix(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); //0 cols grid = new double[1][0]; matrix = new DoubleMatrix(grid); assertEquals(1, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); //Uneven rows assertThrows(InvalidRowSizeException.class, () -> { double[][] grid1 = new double[2][]; grid1[0] = new double[1]; grid1[0][0] = 0.0; grid1[1] = new double[2]; grid1[1][0] = 1.0; grid1[1][1] = 2.0; new DoubleMatrix(grid1); }); //2x2 grid = grid2; matrix = new DoubleMatrix(grid); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertEquals(0.5, matrix.get(0, 0), 0.0001); assertEquals(1.5, matrix.get(0, 1), 0.0001); assertEquals(0.5, matrix.get(1, 0), 0.0001); assertEquals(1.5, matrix.get(1, 1), 0.0001); } @Test public void testEquals(){ //Invalid equals DoubleMatrix matrix = new DoubleMatrix(grid1); assertNotEquals(matrix, null); assertNotEquals(matrix, new int[0]); //1x1 matrix = new DoubleMatrix(grid1); boolean gridEquals = matrix.equals(matrix); assertTrue("DoubleMatrix 1x1 failed equals DoubleMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") boolean gridEquals1 = matrix.equals(grid1); assertTrue("DoubleMatrix 1x1 failed equals double[][].", gridEquals1); //With delta boolean gridEquals12 = matrix.equals(matrix, 0.0001); assertTrue(gridEquals12); //2x2 matrix = new DoubleMatrix(grid2); boolean gridEquals2 = matrix.equals(matrix); assertTrue("DoubleMatrix 2x2 failed equals DoubleMatrix.", gridEquals2); @SuppressWarnings("unlikely-arg-type") boolean gridEquals21 = matrix.equals(grid2); assertTrue("DoubleMatrix 2x2 failed equals double[][].", gridEquals21); //false @SuppressWarnings("unlikely-arg-type") boolean gridEquals22 = matrix.equals(transformGrid2_1); assertFalse(gridEquals22); gridEquals2 = matrix.equals(new DoubleMatrix(grid3)); assertFalse(gridEquals2); gridEquals2 = matrix.equals(new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} })); assertFalse(gridEquals2); //3x3 matrix = new DoubleMatrix(grid3); boolean gridEquals3 = matrix.equals(matrix); assertTrue("DoubleMatrix 3x3 failed equals DoubleMatrix.", gridEquals3); @SuppressWarnings("unlikely-arg-type") boolean gridEquals31 = matrix.equals(grid3); assertTrue("DoubleMatrix 3x3 failed equals double[][].", gridEquals31); //4x4 matrix = new DoubleMatrix(grid4); boolean gridEquals4 = matrix.equals(matrix); assertTrue("DoubleMatrix 4x4 failed equals DoubleMatrix.", gridEquals4); @SuppressWarnings("unlikely-arg-type") boolean gridEquals41 = matrix.equals(grid4); assertTrue("DoubleMatrix 4x4 failed equals double[][].", gridEquals41); //10x10 matrix = new DoubleMatrix(grid10); boolean gridEquals10 = matrix.equals(matrix); assertTrue("DoubleMatrix 10x10 failed equals DoubleMatrix.", gridEquals10); @SuppressWarnings("unlikely-arg-type") boolean gridEquals101 = matrix.equals(grid10); assertTrue("DoubleMatrix 10x10 failed equals double[][].", gridEquals101); } @Test public void testGet(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); assertEquals("DoubleMatrix 1x1 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //Invalid gets final DoubleMatrix testMatrix = new DoubleMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(3, 3); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(-1, -1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(0, 3); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(0, -1); }); //2x2 matrix = new DoubleMatrix(grid2); assertEquals("DoubleMatrix 2x2 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //3x3 matrix = new DoubleMatrix(grid3); assertEquals("DoubleMatrix 3x3 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //4x4 matrix = new DoubleMatrix(grid4); assertEquals("DoubleMatrix 4x4 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //10x10 matrix = new DoubleMatrix(grid10); assertEquals("DoubleMatrix 10x10 failed get.", 0.5, matrix.get(0, 0), 0.0000001); } @Test public void testGetRow(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); //Invalid gets final DoubleMatrix testMatrix = new DoubleMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getRow(-1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getRow(3); }); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5}}); assertEquals("DoubleMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); assertEquals("DoubleMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); //4x4 matrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); assertEquals("DoubleMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); //10x10 matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}}); assertEquals("DoubleMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); } @Test public void testGetCol(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); //Invalid gets final DoubleMatrix testMatrix = new DoubleMatrix(); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getCol(-1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getCol(3); }); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix2.getCol(3); }); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); assertEquals("DoubleMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0)); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5} }); assertEquals("DoubleMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0)); //4x4 matrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5}, {0.5} }); assertEquals("DoubleMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0)); //10x10 matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5}, {0.5}, {0.5}, {0.5}, {0.5}, {0.5}, {0.5}, {0.5} }); assertEquals("DoubleMatrix 10x10 failed getCol.", correctMatrix, matrix.getCol(0)); } @Test public void testSet(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.set(0, 0, 1.5); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.5}}); assertEquals(correctMatrix, matrix); //Invalid sets final DoubleMatrix testMatrix = new DoubleMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(-1, -1, 0.0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(2, 2, 0.0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(0, -1, 0.0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(0, 2, 0.0); }); //2x2 matrix = new DoubleMatrix(grid2); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {0.5, 1.5} }); assertEquals(correctMatrix, matrix); //3x3 matrix = new DoubleMatrix(grid3); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals(correctMatrix, matrix); //4x4 matrix = new DoubleMatrix(grid4); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix); //10x10 matrix = new DoubleMatrix(grid10); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals(correctMatrix, matrix); } @Test public void testSetRow(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.setRow(0, new double[]{0.0}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); assertEquals(correctMatrix, matrix); //Invalid setRows final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final double[] testGrid = {0.0, 0.0}; final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setRow(-1, testGrid); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setRow(2, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, (double[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.setRow(0, (DoubleMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); matrix.setRow(1, new double[]{1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {1.5, 0.5} }); assertEquals(correctMatrix, matrix); DoubleMatrix matrix2 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); matrix.setRow(1, matrix2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.0, 0.0} }); assertEquals(correctMatrix, matrix); //3x3 matrix = new DoubleMatrix(grid3); matrix.setRow(0, new double[]{0, 0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 0.5, 1.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals(correctMatrix, matrix); //4x4 matrix = new DoubleMatrix(grid4); matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {3.5, 2.5, 1.5, 0.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix); //10x10 matrix = new DoubleMatrix(grid10); matrix.setRow(0, new double[]{9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals(correctMatrix, matrix); } @Test public void testSetCol(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.setCol(0, new double[]{0.5}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals(correctMatrix, matrix); //Invalid setCols final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(); final double[] testGrid = {0.0, 0.0}; assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(-1, testGrid); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(2, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setCol(0, (double[])null); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(0, testGrid); }); assertThrows(NullMatrixException.class, () -> { testMatrix.setCol(0, (DoubleMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setCol(0, testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); matrix.setCol(0, new double[]{2.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {2.5, 1.5} }); assertEquals(correctMatrix, matrix); //Matrix DoubleMatrix vector = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); matrix.setCol(1, vector); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 0.0}, {2.5, 0.0} }); assertEquals(correctMatrix, matrix); //3x3 matrix = new DoubleMatrix(grid3); matrix.setCol(0, new double[]{0.0, 0.0, 0.0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.5, 2.5}, {0.0, 1.5, 2.5}, {0.0, 1.5, 2.5} }); assertEquals(correctMatrix, matrix); //4x4 matrix = new DoubleMatrix(grid4); matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix); //10x10 matrix = new DoubleMatrix(grid10); matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals(correctMatrix, matrix); } @Test public void testAddRow(){ //0x0 DoubleMatrix matrix = new DoubleMatrix(); matrix.addRow(new double[]{0.0}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); assertEquals(correctMatrix, matrix); //1x1 matrix = new DoubleMatrix(grid1); matrix.addRow(new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); assertEquals(correctMatrix, matrix); //Invalid adds final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addRow(new double[]{0.0, 0.0}); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addRow((double[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addRow((DoubleMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addRow(testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); matrix.addRow(new double[]{0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5} }); assertEquals(correctMatrix, matrix); //Matrix matrix = new DoubleMatrix(grid2); matrix.addRow(new DoubleMatrix(new double[][]{{1.0, 2.0}})); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.5, 1.5}, {1.0, 2.0} }); assertEquals(correctMatrix, matrix); //3x3 matrix = new DoubleMatrix(grid3); matrix.addRow(new double[]{0.5, 1.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals(correctMatrix, matrix); //4x4 matrix = new DoubleMatrix(grid4); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix); //10x10 matrix = new DoubleMatrix(grid10); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals(correctMatrix, matrix); } @Test public void testAddCol(){ //0x0 DoubleMatrix matrix = new DoubleMatrix(); matrix.addCol(new double[]{0.0}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); assertEquals(correctMatrix, matrix); //1x1 matrix = new DoubleMatrix(grid1); matrix.addCol(new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); assertEquals(correctMatrix, matrix); //Invalid adds final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addCol(new double[]{0.0, 0.}); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addCol((double[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addCol((DoubleMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addCol(testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); matrix.addCol(new double[]{2.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals(correctMatrix, matrix); //Matrix matrix = new DoubleMatrix(grid2); matrix.addCol(new DoubleMatrix(new double[][]{{0.0}, {0.0}})); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 0.0}, {0.5, 1.5, 0.0} }); assertEquals(correctMatrix, matrix); //3x3 matrix = new DoubleMatrix(grid3); matrix.addCol(new double[]{3.5, 3.5, 3.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix); //4x4 matrix = new DoubleMatrix(grid4); matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5} }); assertEquals(correctMatrix, matrix); //10x10 matrix = new DoubleMatrix(grid10); matrix.addCol(new double[]{10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5}); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} }); assertEquals(correctMatrix, matrix); } @Test public void testAppendRight(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix secondMatrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); assertEquals("DoubleMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //Invalid appends final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.appendRight(testMatrix2); }); assertThrows(NullMatrixException.class, () -> { testMatrix.appendRight(null); }); //2x2 matrix = new DoubleMatrix(grid2); secondMatrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 0.5, 1.5}, {0.5, 1.5, 0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //3x3 matrix = new DoubleMatrix(grid3); secondMatrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, {0.5, 1.5, 2.5, 0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //4x4 matrix = new DoubleMatrix(grid4); secondMatrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //10x10 matrix = new DoubleMatrix(grid10); secondMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); } @Test public void testAppendBottom(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix secondMatrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); assertEquals("DoubleMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //Invalid appends final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.appendBottom(testMatrix2); }); assertThrows(NullMatrixException.class, () -> { testMatrix.appendBottom(null); }); //2x2 matrix = new DoubleMatrix(grid2); secondMatrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5}, {0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 matrix = new DoubleMatrix(grid3); secondMatrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 matrix = new DoubleMatrix(grid4); secondMatrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 matrix = new DoubleMatrix(grid10); secondMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } @Test public void testIsSquare(){ DoubleMatrix matrix = new DoubleMatrix(); assertFalse(matrix.isSquare()); matrix = new DoubleMatrix(2, 2, 0.0); assertTrue(matrix.isSquare()); matrix = new DoubleMatrix(2, 3, 0.0); assertFalse(matrix.isSquare()); } @Test public void testAddition(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("DoubleMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(0.5)); //Invalid adds final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); assertThrows(InvalidGeometryException.class, () -> { testMatrix.add(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.add(testMatrix3); }); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_1); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 1.5}, {1.0, 1.5} }); assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0}, {1.0, 2.0} }); assertEquals("DoubleMatrix 2x2 failed add scalar.", correctMatrix, matrix.add(0.5)); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(transformGrid3_1); correctMatrix = new DoubleMatrix(new double[][]{ {2.0, 2.0, 2.5}, {2.0, 2.0, 2.5}, {2.0, 2.0, 2.5} }); assertEquals("DoubleMatrix 3x3 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0} }); assertEquals("DoubleMatrix 3x3 failed add scalar.", correctMatrix, matrix.add(0.5)); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(transformGrid4_1); correctMatrix = new DoubleMatrix(new double[][]{ {3.0, 3.0, 3.0, 3.5}, {3.0, 3.0, 3.0, 3.5}, {3.0, 3.0, 3.0, 3.5}, {3.0, 3.0, 3.0, 3.5} }); assertEquals("DoubleMatrix 4x4 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0} }); assertEquals("DoubleMatrix 4x4 failed add scalar.", correctMatrix, matrix.add(0.5)); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(transformGrid10_1); correctMatrix = new DoubleMatrix(new double[][]{ {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5} }); assertEquals("DoubleMatrix 10x10 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} }); assertEquals("DoubleMatrix 10x10 failed add scalar.", correctMatrix, matrix.add(0.5)); } @Test public void testSubtraction(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(1, 1, 0); assertEquals("DoubleMatrix 1x1 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("DoubleMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); //Invalid subtracts final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); assertThrows(InvalidGeometryException.class, () -> { testMatrix.subtract(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.subtract(testMatrix3); }); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(2, 2, 0); assertEquals("DoubleMatrix 2x2 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.0}, {0.0, 1.0} }); assertEquals("DoubleMatrix 2x2 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(3, 3, 0); assertEquals("DoubleMatrix 3x3 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.0, 2.0}, {0.0, 1.0, 2.0}, {0.0, 1.0, 2.0} }); assertEquals("DoubleMatrix 3x3 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(4, 4, 0); assertEquals("DoubleMatrix 4x4 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.0, 2.0, 3.0}, {0.0, 1.0, 2.0, 3.0}, {0.0, 1.0, 2.0, 3.0}, {0.0, 1.0, 2.0, 3.0} }); assertEquals("DoubleMatrix 4x4 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(10, 10, 0); assertEquals("DoubleMatrix 10x10 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{ {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0} }); assertEquals("DoubleMatrix 10x10 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); } @Test public void testMultiplication(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); assertEquals("DoubleMatrix 1x1 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); assertEquals("DoubleMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); //Invalid multiplication final DoubleMatrix testMatrix = new DoubleMatrix(grid1); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.multiply(testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); correctMatrix = new DoubleMatrix(new double[][]{ {3.0, 5.0}, {3.0, 5.0} }); assertEquals("DoubleMatrix 2x2 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); DoubleMatrix vector = new DoubleMatrix(new double[][]{ {1.5}, {2.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {4.5}, {4.5} }); assertEquals("DoubleMatrix 2x2 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 3.0}, {1.0, 3.0} }); assertEquals("DoubleMatrix 2x2 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(transformGrid3_2); correctMatrix = new DoubleMatrix(new double[][]{ {6.75, 11.25, 15.75}, {6.75, 11.25, 15.75}, {6.75, 11.25, 15.75} }); assertEquals("DoubleMatrix 3x3 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); vector = new DoubleMatrix(new double[][]{ {1.5}, {2.5}, {3.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {13.25}, {13.25}, {13.25} }); assertEquals("DoubleMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 3.0, 5.0}, {1.0, 3.0, 5.0}, {1.0, 3.0, 5.0} }); assertEquals("DoubleMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(2.0)); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(transformGrid4_2); correctMatrix = new DoubleMatrix(new double[][]{ {20, 12, 4, 0}, {20, 12, 4, 0}, {20, 12, 4, 0}, {20, 12, 4, 0} }); assertEquals("DoubleMatrix 4x4 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); vector = new DoubleMatrix(new double[][]{ {2.5}, {1.5}, {0.5}, {0.0} }); correctMatrix = new DoubleMatrix(new double[][]{ {4.75}, {4.75}, {4.75}, {4.75} }); assertEquals("DoubleMatrix 4x4 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 3.0, 5.0, 7.0}, {1.0, 3.0, 5.0, 7.0}, {1.0, 3.0, 5.0, 7.0}, {1.0, 3.0, 5.0, 7.0} }); assertEquals("DoubleMatrix 4x4 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(transformGrid10_2); correctMatrix = new DoubleMatrix(new double[][]{ {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, {75, 125, 175, 225, 275, 325, 375, 425, 475, 525} }); assertEquals("DoubleMatrix 10x10 failed multiplication DoubleMatrix.", correctMatrix, matrix.multiply(transformMatrix)); vector = new DoubleMatrix(new double[][]{ {1.5}, {2.5}, {3.5}, {4.5}, {5.5}, {6.5}, {7.5}, {8.5}, {9.5}, {10.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {382.5}, {382.5}, {382.5}, {382.5}, {382.5}, {382.5}, {382.5}, {382.5}, {382.5}, {382.5} }); assertEquals("DoubleMatrix 10x10 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0} }); assertEquals("DoubleMatrix 10x10 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); } @Test public void testDotProduct(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); assertEquals("DoubleMatrix 1x1 failed dot product DoubleMatrix.", 0.75, matrix.dotProduct(transformMatrix), 0.0000001); //Invalid products DoubleMatrix testMatrix = new DoubleMatrix(grid1); DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { testMatrix.dotProduct(testMatrix2); }); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); assertEquals("DoubleMatrix 2x2 failed dot product DoubleMatrix.", 16, matrix.dotProduct(transformMatrix), 0.0000001); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(transformGrid3_2); assertEquals("DoubleMatrix 3x3 failed dot product DoubleMatrix.", 101.25, matrix.dotProduct(transformMatrix), 0.0000001); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(transformGrid4_2); assertEquals("DoubleMatrix 4x4 failed dot product DoubleMatrix.", 144, matrix.dotProduct(transformMatrix), 0.0000001); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(transformGrid10_2); assertEquals("DoubleMatrix 10x10 failed dot product DoubleMatrix.", 30000, matrix.dotProduct(transformMatrix), 0.0000001); } @Test public void testHadamardProduct(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); assertEquals("DoubleMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); //Invalid hadamard products DoubleMatrix testMatrix = new DoubleMatrix(grid1); DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); assertThrows(InvalidGeometryException.class, () -> { testMatrix.hadamardProduct(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.hadamardProduct(testMatrix3); }); //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); correctMatrix = new DoubleMatrix(new double[][]{ {0.75, 3.75}, {0.75, 3.75} }); assertEquals("DoubleMatrix 2x2 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); //3x3 matrix = new DoubleMatrix(grid3); transformMatrix = new DoubleMatrix(transformGrid3_2); correctMatrix = new DoubleMatrix(new double[][]{ {0.75, 3.75, 8.75}, {0.75, 3.75, 8.75}, {0.75, 3.75, 8.75} }); assertEquals("DoubleMatrix 3x3 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); //4x4 matrix = new DoubleMatrix(grid4); transformMatrix = new DoubleMatrix(transformGrid4_2); correctMatrix = new DoubleMatrix(new double[][]{ {1.25, 2.25, 1.25, 0}, {1.25, 2.25, 1.25, 0}, {1.25, 2.25, 1.25, 0}, {1.25, 2.25, 1.25, 0} }); assertEquals("DoubleMatrix 4x4 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); //10x10 matrix = new DoubleMatrix(grid10); transformMatrix = new DoubleMatrix(transformGrid10_2); correctMatrix = new DoubleMatrix(new double[][]{ {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75} }); assertEquals("DoubleMatrix 10x10 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); } @Test public void testTranspose(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); assertEquals("DoubleMatrix 1x1 failed transpose.", correctMatrix, matrix.transpose()); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 0.5}, {1.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed transpose.", correctMatrix, matrix.transpose()); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 0.5, 0.5}, {1.5, 1.5, 1.5}, {2.5, 2.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed transpose.", correctMatrix, matrix.transpose()); //4x4 matrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 0.5, 0.5, 0.5}, {1.5, 1.5, 1.5, 1.5}, {2.5, 2.5, 2.5, 2.5}, {3.5, 3.5, 3.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed transpose.", correctMatrix, matrix.transpose()); //10x10 matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}, {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}, {2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5}, {3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5}, {4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5}, {5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5}, {6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5}, {7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5}, {8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5}, {9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed transpose.", correctMatrix, matrix.transpose()); } @Test public void testDeterminant(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); assertEquals("DoubleMatrix 1x1 failed determinant.", 0.5, matrix.determinant(), 0.0000001); //Invalid determinants DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); assertThrows(InvalidGeometryException.class, () -> { testMatrix.determinant(); }); //2x2 matrix = new DoubleMatrix(grid2); assertEquals("IntegerMatrix 2x2 failed determinant1.", 0, matrix.determinant(), 0.0000001); matrix = new DoubleMatrix(new double[][]{ {0.5, 3.5}, {3.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed determinant2.", -12.0, matrix.determinant(), 0.0000001); //det assertEquals(matrix.determinant(), matrix.det(), 0.0000001); //3x3 matrix = new DoubleMatrix(grid3); assertEquals("IntegerMatrix 3x3 failed determinant1.", 0, matrix.determinant(), 0.0000001); matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {1.5, 2.5, 0.5}, {2.5, 0.5, 1.5} }); assertEquals("DoubleMatrix 3x3 failed determinant2.", -13.5, matrix.determinant(), 0.0000001); //4x4 matrix = new DoubleMatrix(grid4); assertEquals("DoubleMatrix 4x4 failed determinant1.", 0, matrix.determinant(), 0.0000001); matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {1.5, 2.5, 3.5, 0.5}, {2.5, 3.5, 0.5, 1.5}, {3.5, 0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 4x4 failed determinant2.", 128.0, matrix.determinant(), 0.0000001); //Column matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 0.0, 2.5, 3.5}, {0.5, 0.0, 2.5, 3.5}, {0.0, 0.0, 2.5, 3.5} }); assertEquals(0.0, matrix.determinant(), 0.0000001); //Column2 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 0.0, 3.5}, {0.5, 1.5, 0.0, 3.5}, {0.5, 1.5, 0.0, 3.5}, {0.0, 1.5, 0.0, 3.5} }); assertEquals(0.0, matrix.determinant(), 0.0000001); //10x10 matrix = new DoubleMatrix(grid10); assertEquals("DoubleMatrix 10x10 failed determinant1.", 0, matrix.determinant(), 0.0000001); matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5}, {2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5}, {3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5}, {4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5}, {5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5}, {6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5}, {7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5}, {8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0}, }); assertEquals("DoubleMatrix 10x10 failed determinatn2.", -10000000.0, matrix.determinant(), 0.0000001); } @Test public void testCofactor(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1}}); assertEquals("DoubleMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); //Invalid cofactor DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); assertThrows(InvalidGeometryException.class, () -> { testMatrix.cofactor(); }); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {1.5, -0.5}, {-1.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); //cof assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(3, 3, 0); assertEquals("DoubleMatrix 3x3 failed cofactor1.", correctMatrix, matrix.cofactor()); matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {1.5, 2.5, 0.5}, {2.5, 0.5, 1.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {3.5, -1, -5.5}, {-1, -5.5, 3.5}, {-5.5, 3.5, -1} }); assertEquals("DoubleMatrix 3x3 failed cofactor2.", correctMatrix, matrix.cofactor()); //4x4 matrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(4, 4, 0); assertEquals("DoubleMatrix 4x4 failed cofactor1.", correctMatrix, matrix.cofactor()); matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {1.5, 2.5, 3.5, 0.5}, {2.5, 3.5, 0.5, 1.5}, {3.5, 0.5, 1.5, 2.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {-28, 4, 4, 36}, {4, 4, 36, -28}, {4, 36, -28, 4}, {36, -28, 4, 4} }); assertEquals("DoubleMatrix 4x4 failed cofactor2.", correctMatrix, matrix.cofactor()); //10x10 //?Skipping 10x10 test because test took > 5s by itself /* matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(10, 10, 0); assertEquals("DoubleMatrix 10x10 failed cofactor1.", correctMatrix, matrix.cofactor()); */ } @Test public void testPower(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.125}}); assertEquals("DoubleMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); //Invalid powers final DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); assertThrows(InvalidGeometryException.class, () -> { testMatrix.pow(1); }); assertThrows(InvalidScalarException.class, () -> { testMatrix2.pow(-1); }); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {2, 6}, {2, 6} }); assertEquals("DoubleMatrix 2x2 failed power.", correctMatrix, matrix.pow(3)); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ {10.125, 30.375, 50.625}, {10.125, 30.375, 50.625}, {10.125, 30.375, 50.625} }); assertEquals("DoubleMatrix 3x3 failed power.", correctMatrix, matrix.pow(3)); //4x4 //0 matrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0} }); assertEquals(correctMatrix, matrix.pow(0)); //1 correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); assertEquals(correctMatrix, matrix.pow(1)); //3 correctMatrix = new DoubleMatrix(new double[][]{ {32.0, 96.0, 160.0, 224.0}, {32.0, 96.0, 160.0, 224.0}, {32.0, 96.0, 160.0, 224.0}, {32.0, 96.0, 160.0, 224.0} }); assertEquals("DoubleMatrix 4x4 failed power.", correctMatrix, matrix.pow(3)); //10x10 matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750} }); assertEquals("DoubleMatrix 10x10 failed power.", correctMatrix, matrix.pow(3)); } @Test public void testAdjoint(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); assertEquals("DoubleMatrix 1x1 failed adjoint.", correctMatrix, matrix.adjoint()); //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {1.5, -1.5}, {-0.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); //adj assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(3, 3, 0); assertEquals("DoubleMatrix 3x3 failed adjoint.", correctMatrix, matrix.adjoint()); //4x4 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {1.5, 2.5, 3.5, 0.5}, {2.5, 3.5, 0.5, 1.5}, {3.5, 0.5, 1.5, 2.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {-28, 4, 4, 36}, {4, 4, 36, -28}, {4, 36, -28, 4}, {36, -28, 4, 4} }); assertEquals("DoubleMatrix 4x4 failed adjoint.", correctMatrix, matrix.adjoint()); //10x10 //?Skippng 10x10 test because test took > 5s by itself /* matrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(10, 10, 0); assertEquals("DoubleMatrix 10x10 failed adjoint.", correctMatrix, matrix.adjoint()); */ } @Test public void testInverse(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{2.0}}); assertEquals("DoubleMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); //Invalid inverse DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{ {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0}, {1.0, 2.0, 3.0} }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.inverse(); }); assertThrows(InvalidScalarException.class, () -> { testMatrix2.inverse(); }); //2x2 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, {1.5, 0.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {-0.25, 0.75}, {0.75, -0.25} }); assertEquals("DoubleMatrix 2x2 failed inverse.", correctMatrix, matrix.inverse()); //3x3 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, {1.5, 2.5, 0.5}, {2.5, 0.5, 1.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {-7.0/27.0, 2.0/27.0, 11.0/27.0}, {2.0/27.0, 11.0/27.0, -7.0/27.0}, {11.0/27.0, -7.0/27.0, 2.0/27.0} }); assertEquals("DoubleMatrix 3x3 failed inverse.", correctMatrix, matrix.inverse()); //4x4 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, {1.5, 2.5, 3.5, 0.5}, {2.5, 3.5, 0.5, 1.5}, {3.5, 0.5, 1.5, 2.5} }); correctMatrix = new DoubleMatrix(new double[][]{ {-0.21875, 0.03125, 0.03125, 0.28125}, {0.03125, 0.03125, 0.28125, -0.21875}, {0.03125, 0.28125, -0.21875, 0.03125}, {0.28125, -0.21875, 0.03125, 0.03125} }); assertEquals("DoubleMatrix 4x4 failed inverse.", correctMatrix, matrix.inverse()); //10x10 //?Skipped 10x10 because it would take a long time to compute } @Test public void testGenerateIdentity(){ //0x0 assertThrows(InvalidGeometryException.class, () -> { DoubleMatrix.generateIdentity(0); }); //1x1 DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); assertEquals(correctMatrix, DoubleMatrix.generateIdentity(1)); //2x2 correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 0.0}, {0.0, 1.0} }); assertEquals(correctMatrix, DoubleMatrix.generateIdentity(2)); //3x3 correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0} }); assertEquals(correctMatrix, DoubleMatrix.generateIdentity(3)); //4x4 correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }); assertEquals(correctMatrix, DoubleMatrix.generateIdentity(4)); //10x10 correctMatrix = new DoubleMatrix(new double[][]{ {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0} }); assertEquals(correctMatrix, DoubleMatrix.generateIdentity(10)); } @Test public void testHashCode(){ DoubleMatrix matrix = new DoubleMatrix(); assertEquals(Arrays.hashCode(new double[0][0]), matrix.hashCode()); } @Test public void testToString(){ DoubleMatrix matrix = new DoubleMatrix(grid3); String matrixString = "[0.5,1.5,2.5]\n[0.5,1.5,2.5]\n[0.5,1.5,2.5]"; assertEquals(matrixString, matrix.toString()); } }