diff --git a/src/main/java/com/mattrixwv/matrix/ModMatrix.java b/src/main/java/com/mattrixwv/matrix/ModMatrix.java new file mode 100644 index 0000000..5036a3e --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/ModMatrix.java @@ -0,0 +1,369 @@ +//Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java +//Mattrixwv +// Created: 02-09-22 +//Modified: 02-09-22 +package com.mattrixwv.matrix; + + +import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidScalarException; + + +public class ModMatrix extends IntegerMatrix{ + protected int mod; + + //Helper functions + protected void setMod(int mod){ + if(mod <= 0){ + throw new InvalidScalarException("The mod must be > 0"); + } + + this.mod = mod; + } + protected int modValue(int value){ + int newValue = value % mod; + + if(newValue < 0){ + newValue += mod; + } + + return newValue; + } + protected int[] modValues(int[] values){ + int[] newValues = new int[values.length]; + + for(int cnt = 0;cnt < values.length;++cnt){ + newValues[cnt] = modValue(values[cnt]); + } + + return newValues; + } + protected void modGrid(){ + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + grid[row][col] = modValue(grid[row][col]); + } + } + } + @Override + protected void setGrid(int[][] grid){ + super.setGrid(grid); + modGrid(); + } + + //Constructors + public ModMatrix(int mod){ + super(); + setMod(mod); + modGrid(); + } + public ModMatrix(int[][] grid, int mod){ + super(); + setMod(mod); + setGrid(grid); + } + public ModMatrix(ModMatrix matrix){ + super(); + setMod(matrix.mod); + setGrid(matrix.grid); + } + public ModMatrix(IntegerMatrix matrix, int mod){ + super(); + setMod(mod); + setGrid(matrix.grid); + } + public ModMatrix(int rows, int cols, int fill, int mod){ + super(rows, cols, fill); + setMod(mod); + modGrid(); + } + + //Gets + public int getMod(){ + return mod; + } + @Override + public ModMatrix getRow(int row){ + return new ModMatrix(super.getRow(row), mod); + } + @Override + public ModMatrix getCol(int col){ + return new ModMatrix(super.getCol(col), mod); + } + //Sets + @Override + public void set(int row, int col, int value){ + super.set(row, col, modValue(value)); + } + @Override + public void setRow(int row, int[] elements){ + super.setRow(row, modValues(elements)); + } + @Override + public void setRow(int row, IntegerMatrix matrix){ + setRow(row, new ModMatrix(matrix, Integer.MAX_VALUE)); + } + public void setRow(int row, ModMatrix matrix){ + super.setRow(row, matrix); + modGrid(); + } + @Override + public void setCol(int col, int[] elements){ + super.setCol(col, elements); + modGrid(); + } + @Override + public void setCol(int col, IntegerMatrix matrix){ + setCol(col, new ModMatrix(matrix, Integer.MAX_VALUE)); + } + public void setCol(int col, ModMatrix matrix){ + super.setCol(col, matrix); + modGrid(); + } + //Adds + @Override + public void addRow(int[] elements){ + super.addRow(modValues(elements)); + } + @Override + public void addRow(IntegerMatrix matrix){ + addRow(new ModMatrix(matrix, Integer.MAX_VALUE)); + } + public void addRow(ModMatrix matrix){ + super.addRow(matrix); + modGrid(); + } + @Override + public void addCol(int[] elements){ + super.addCol(modValues(elements)); + } + @Override + public void addCol(IntegerMatrix matrix){ + addCol(new ModMatrix(matrix, Integer.MAX_VALUE)); + } + public void addCol(ModMatrix matrix){ + super.addCol(matrix); + modGrid(); + } + @Override + public ModMatrix appendRight(IntegerMatrix rightSide){ + return appendRight(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public ModMatrix appendRight(ModMatrix rightSide){ + return new ModMatrix(super.appendRight(rightSide), mod); + } + @Override + public ModMatrix appendBottom(IntegerMatrix rightSide){ + return appendBottom(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public ModMatrix appendBottom(ModMatrix rightSide){ + return new ModMatrix(super.appendBottom(rightSide), mod); + } + + //Simple operations + public static ModMatrix generateIdentity(int size){ + return generateIdentity(size, Integer.MAX_VALUE); + } + public static ModMatrix generateIdentity(int size, int mod){ + return new ModMatrix(IntegerMatrix.generateIdentity(size), mod); + } + @Override + public ModMatrix add(IntegerMatrix rightSide){ + return add(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public ModMatrix add(ModMatrix rightSide){ + return new ModMatrix(super.add(rightSide), mod); + } + @Override + public ModMatrix add(int scalar){ + return new ModMatrix(super.add(scalar), mod); + } + @Override + public ModMatrix subtract(IntegerMatrix rightSide){ + return subtract(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public ModMatrix subtract(ModMatrix rightSide){ + return new ModMatrix(super.subtract(rightSide), mod); + } + @Override + public ModMatrix subtract(int scalar){ + return new ModMatrix(super.subtract(scalar), mod); + } + @Override + public ModMatrix multiply(IntegerMatrix matrix){ + return multiply(new ModMatrix(matrix, Integer.MAX_VALUE)); + } + public ModMatrix multiply(ModMatrix matrix){ + return new ModMatrix(super.multiply(matrix), mod); + } + @Override + public ModMatrix multiply(int scalar){ + return new ModMatrix(super.multiply(scalar), mod); + } + @Override + public ModMatrix pow(int power){ + //Make sure the matrix is square so it can be multiplied + if(!isSquare()){ + throw new InvalidGeometryException("The matrix must be square to raise it to a power"); + } + //Make sure the power is positive + if(power < 0){ + throw new InvalidScalarException("The power must be >= 0"); + } + else if(power == 0){ + return new ModMatrix(getNumRows(), getNumCols(), 1, mod); + } + + //Create a new matrix for the product + ModMatrix newMatrix = clone(); + //Multiply the current grid power times + for(int currentPower = 1;currentPower < power;++currentPower){ + newMatrix = newMatrix.multiply(this); + } + + //Return the new grid + return newMatrix; + } + @Override + public int dotProduct(IntegerMatrix rightSide){ + return dotProduct(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public int dotProduct(ModMatrix rightSide){ + return super.dotProduct(rightSide); + } + @Override + public ModMatrix hadamardProduct(IntegerMatrix rightSide){ + return hadamardProduct(new ModMatrix(rightSide, Integer.MAX_VALUE)); + } + public ModMatrix hadamardProduct(ModMatrix rightSide){ + return new ModMatrix(super.hadamardProduct(rightSide), mod); + } + + //Complex operations + @Override + public ModMatrix transpose(){ + return new ModMatrix(super.transpose(), mod); + } + @Override + public int det(){ + return determinant(); + } + @Override + public int determinant(){ + return super.determinant(); + } + @Override + public ModMatrix cof(){ + return cofactor(); + } + @Override + public ModMatrix cofactor(){ + //Make sure the matrix is square + if(!isSquare()){ + throw new InvalidGeometryException("A matrix must be square to find the cofactor matrix"); + } + + //Create a new grid + int[][] newGrid = new int[getNumRows()][getNumCols()]; + //If the grid is 1x1 return the grid + if(getNumRows() == 1){ + newGrid[0][0] = 1; + } + //Use the formula to find the cofactor matrix + else{ + for(int row = 0;row < getNumRows();++row){ + int multiplier = ((row % 2) == 0) ? 1 : -1; + for(int col = 0;col < getNumCols();++col){ + newGrid[row][col] = multiplier * laplaceExpansionHelper(row, col).determinant(); + multiplier = -multiplier; + } + } + } + + //Return the new matrix + return new ModMatrix(newGrid, mod); + } + @Override + public ModMatrix adj(){ + return adjoint(); + } + @Override + public ModMatrix adjoint(){ + return cofactor().transpose(); + } + @Override + public ModMatrix inverse(){ + //Make sure the matrix is square + if(!isSquare()){ + throw new InvalidGeometryException("A matrix must be square for it to have an inverse"); + } + + //Make sure the determinant is not 0 + int determinant = determinant(); + if(determinant == 0){ + throw new InvalidScalarException("The determinant cannot be 0"); + } + //Find the inverse of determinant % mod + int determinantInverse = -1; + for(int num = 1;num < mod;++num){ + if(modValue(determinant * num) == 1){ + determinantInverse = num; + break; + } + } + if(determinantInverse < 0){ + throw new InvalidScalarException("There is no inverse for the determinant"); + } + + //Find the matrix of cofactors and multiply the inverse determinant by cofactors and return + return adjoint().multiply(determinantInverse); + } + + //Object functions + @Override + public boolean equals(Object rightSide){ + if(rightSide.getClass().equals(this.getClass())){ + ModMatrix rightMatrix = (ModMatrix)rightSide; + + //Make sure they have the same number of elements + if(getNumRows() != rightMatrix.getNumRows()){ + return false; + } + else if(getNumCols() != rightMatrix.getNumCols()){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] != rightMatrix.grid[row][col]){ + return false; + } + } + } + + //If false hasn't been return yet then they are equal + return true; + } + else if(rightSide.getClass().equals(int[][].class)){ + int[][] rightMatrix = (int[][])rightSide; + + return equals(new ModMatrix(rightMatrix, mod)); + } + else{ + return false; + } + } + @Override + public int hashCode(){ + return grid.hashCode(); + } + @Override + public String toString(){ + return super.toString() + "\nmod(" + mod + ")"; + } + @Override + public ModMatrix clone(){ + return new ModMatrix(grid, mod); + } +} diff --git a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java index 7838348..75f7843 100644 --- a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 02-08-22 +//Modified: 02-09-22 package com.mattrixwv.matrix; @@ -120,79 +120,82 @@ public class TestDoubleMatrix{ assertTrue("DoubleMatrix 1x1 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals = matrix.equals(grid1); - assertTrue("DoubleMatrix 1x1 failed equals double[][]", gridEquals); + assertTrue("DoubleMatrix 1x1 failed equals double[][].", gridEquals); //2x2 matrix = new DoubleMatrix(grid2); assertTrue("DoubleMatrix 2x2 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals2 = matrix.equals(grid2); - assertTrue("DoubleMatrix 2x2 failed equals double[][]", gridEquals2); + assertTrue("DoubleMatrix 2x2 failed equals double[][].", gridEquals2); //3x3 matrix = new DoubleMatrix(grid3); assertTrue("DoubleMatrix 3x3 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals3 = matrix.equals(grid3); - assertTrue("DoubleMatrix 3x3 failed equals double[][]", gridEquals3); + assertTrue("DoubleMatrix 3x3 failed equals double[][].", gridEquals3); //4x4 matrix = new DoubleMatrix(grid4); assertTrue("DoubleMatrix 4x4 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals4 = matrix.equals(grid4); - assertTrue("DoubleMatrix 4x4 failed equals double[][]", gridEquals4); + assertTrue("DoubleMatrix 4x4 failed equals double[][].", gridEquals4); //10x10 matrix = new DoubleMatrix(grid10); assertTrue("DoubleMatrix 10x10 failed equals DoubleMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals10 = matrix.equals(grid10); - assertTrue("DoubleMatrix 10x10 failed equals double[][]", gridEquals10); + assertTrue("DoubleMatrix 10x10 failed equals double[][].", gridEquals10); } @Test public void testGets(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); - assertEquals("DoubleMatrix 1x1 failed get", 0.5, matrix.get(0, 0), 0.0000001); + assertEquals("DoubleMatrix 1x1 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals("DoubleMatrix 1x1 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("DoubleMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetColumn + correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); + assertEquals("DoubleMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); //2x2 matrix = new DoubleMatrix(grid2); - assertEquals("DoubleMatrix 2x2 failed get", 0.5, matrix.get(0, 0), 0.0000001); + assertEquals("DoubleMatrix 2x2 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5}}); - assertEquals("DoubleMatrix 2x2 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("DoubleMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); - assertEquals("DoubleMatrix 2x2 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("DoubleMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0)); //3x3 matrix = new DoubleMatrix(grid3); - assertEquals("DoubleMatrix 3x3 failed get", 0.5, matrix.get(0, 0), 0.0000001); + assertEquals("DoubleMatrix 3x3 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); - assertEquals("DoubleMatrix 3x3 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("DoubleMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, {0.5} }); - assertEquals("DoubleMatrix 3x3 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("DoubleMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0)); //4x4 matrix = new DoubleMatrix(grid4); - assertEquals("DoubleMatrix 4x4 failed get", 0.5, matrix.get(0, 0), 0.0000001); + assertEquals("DoubleMatrix 4x4 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); - assertEquals("DoubleMatrix 4x4 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("DoubleMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, @@ -200,14 +203,14 @@ public class TestDoubleMatrix{ {0.5}, {0.5} }); - assertEquals("DoubleMatrix 4x4 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("DoubleMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0)); //10x10 matrix = new DoubleMatrix(grid10); - assertEquals("DoubleMatrix 10x10 failed get", 0.5, matrix.get(0, 0), 0.0000001); + assertEquals("DoubleMatrix 10x10 failed get.", 0.5, matrix.get(0, 0), 0.0000001); //GetRow 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)); + assertEquals("DoubleMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); //GetCol correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, @@ -221,7 +224,7 @@ public class TestDoubleMatrix{ {0.5}, {0.5} }); - assertEquals("DoubleMatrix 10x10 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("DoubleMatrix 10x10 failed getCol.", correctMatrix, matrix.getCol(0)); } @Test @@ -231,15 +234,15 @@ public class TestDoubleMatrix{ DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.set(0, 0, 1.5); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.5}}); - assertEquals("DoubleMatrix 1x1 failed set", correctMatrix, matrix); + assertEquals("DoubleMatrix 1x1 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{0.0}); correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); - assertEquals("DoubleMatrix 1x1 failed setRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 1x1 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals("DoubleMatrix 1x1 failed setCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 1x1 failed setCol.", correctMatrix, matrix); //2x2 //Set @@ -249,14 +252,14 @@ public class TestDoubleMatrix{ {2.5, 1.5}, {0.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed set", correctMatrix, matrix); + assertEquals("DoubleMatrix 2x2 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(1, new double[]{1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {1.5, 0.5} }); - assertEquals("DoubleMatrix 2x2 failed setRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 2x2 failed setRow.", correctMatrix, matrix); //SetCol matrix = new DoubleMatrix(grid2); matrix.setCol(0, new double[]{2.5, 2.5}); @@ -264,7 +267,7 @@ public class TestDoubleMatrix{ {2.5, 1.5}, {2.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed setCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 2x2 failed setCol.", correctMatrix, matrix); //3x3 //Set @@ -275,7 +278,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed set", correctMatrix, matrix); + assertEquals("DoubleMatrix 3x3 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{0, 0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -283,7 +286,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed setRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 3x3 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0, 0, 0}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -291,7 +294,7 @@ public class TestDoubleMatrix{ {0.0, 1.5, 2.5}, {0.0, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed setCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 3x3 failed setCol.", correctMatrix, matrix); //4x4 //Set @@ -303,7 +306,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed set", correctMatrix, matrix); + assertEquals("DoubleMatrix 4x4 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -312,7 +315,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed setRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 4x4 failed setRow.", correctMatrix, matrix); //SetCol matrix.setCol(0, new double[]{0, 0, 0, 0}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -321,6 +324,7 @@ public class TestDoubleMatrix{ {0.0, 1.5, 2.5, 3.5}, {0.0, 1.5, 2.5, 3.5} }); + assertEquals("DoubleMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 //Set @@ -338,7 +342,7 @@ public class TestDoubleMatrix{ {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 set", correctMatrix, matrix); + assertEquals("DoubleMatrix 10x10 failed set.", correctMatrix, matrix); //SetRow 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[][]{ @@ -353,7 +357,7 @@ public class TestDoubleMatrix{ {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 set", correctMatrix, matrix); + assertEquals("DoubleMatrix 10x10 failed setRow.", correctMatrix, matrix); //SetCol 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[][]{ @@ -368,7 +372,7 @@ public class TestDoubleMatrix{ {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("DoubleMatrix 10x10 failed setCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 10x10 failed setCol.", correctMatrix, matrix); } @Test @@ -378,12 +382,12 @@ public class TestDoubleMatrix{ DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.addRow(new double[]{0.5}); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); - assertEquals("DoubleMatrix 1x1 failed addRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 1x1 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid1); matrix.addCol(new double[]{0.5}); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); - assertEquals("DoubleMatrix 1x1 failed addCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 1x1 failed addCol.", correctMatrix, matrix); //2x2 //AddRow @@ -394,7 +398,7 @@ public class TestDoubleMatrix{ {0.5, 1.5}, {0.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed addRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 2x2 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid2); matrix.addCol(new double[]{2.5, 2.5}); @@ -402,7 +406,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 2x2 failed addCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 2x2 failed addCol.", correctMatrix, matrix); //3x3 //AddRow @@ -414,7 +418,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed addRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 3x3 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid3); matrix.addCol(new double[]{3.5, 3.5, 3.5}); @@ -423,7 +427,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 3x3 failed addCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 //AddRow @@ -436,7 +440,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed addRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 4x4 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new DoubleMatrix(grid4); matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); @@ -446,7 +450,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5}, {0.5, 1.5, 2.5, 3.5, 4.5} }); - assertEquals("DoubleMatrix 4x4 failed addCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 //AddRow @@ -465,7 +469,7 @@ public class TestDoubleMatrix{ {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 addRow", correctMatrix, matrix); + assertEquals("DoubleMatrix 10x10 failed addRow.", correctMatrix, matrix); //AddColumn 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}); @@ -481,7 +485,7 @@ public class TestDoubleMatrix{ {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("DoubleMatrix 10x10 failed addCol", correctMatrix, matrix); + assertEquals("DoubleMatrix 10x10 failed addCol.", correctMatrix, matrix); } @Test @@ -491,13 +495,13 @@ public class TestDoubleMatrix{ 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)); + assertEquals("DoubleMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} }); - assertEquals("DoubleMatrix 1x1 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("DoubleMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 //appendRight @@ -507,7 +511,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, @@ -515,7 +519,7 @@ public class TestDoubleMatrix{ {0.5, 1.5}, {0.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("DoubleMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 //appendRight @@ -526,7 +530,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5}, @@ -536,7 +540,7 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("DoubleMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 //appendRight @@ -548,7 +552,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5}, @@ -560,7 +564,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 //appendRight @@ -578,7 +582,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom 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}, @@ -592,7 +596,7 @@ public class TestDoubleMatrix{ {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)); + assertEquals("DoubleMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } @Test diff --git a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java index 3770444..ada38fc 100644 --- a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-08-22 +//Modified: 02-09-22 package com.mattrixwv.matrix; @@ -120,82 +120,82 @@ public class TestIntegerMatrix{ assertTrue("IntegerMatrix 1x1 failed equals IntegerMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals = matrix.equals(grid1); - assertTrue("IntegerMatrix 1x1 failed equals int[][]", gridEquals); + assertTrue("IntegerMatrix 1x1 failed equals int[][].", gridEquals); //2x2 matrix = new IntegerMatrix(grid2); assertTrue("IntegerMatrix 2x2 failed equals IntegerMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals2 = matrix.equals(grid2); - assertTrue("IntegerMatrix 2x2 failed equals int[][]", gridEquals2); + assertTrue("IntegerMatrix 2x2 failed equals int[][].", gridEquals2); //3x3 matrix = new IntegerMatrix(grid3); assertTrue("IntegerMatrix 3x3 failed equals IntegerMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals3 = matrix.equals(grid3); - assertTrue("IntegerMatrix 3x3 failed equals int[][]", gridEquals3); + assertTrue("IntegerMatrix 3x3 failed equals int[][].", gridEquals3); //4x4 matrix = new IntegerMatrix(grid4); assertTrue("IntegerMatrix 4x4 failed equals IntegerMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals4 = matrix.equals(grid4); - assertTrue("IntegerMatrix 4x4 failed equals int[][]", gridEquals4); + assertTrue("IntegerMatrix 4x4 failed equals int[][].", gridEquals4); //10x10 matrix = new IntegerMatrix(grid10); assertTrue("IntegerMatrix = 10x10 failed equals IntegerMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals10 = matrix.equals(grid10); - assertTrue("IntegerMatrix 10x10 failed equals int[][]", gridEquals10); + assertTrue("IntegerMatrix 10x10 failed equals int[][].", gridEquals10); } @Test public void testGets(){ //1x1 IntegerMatrix matrix = new IntegerMatrix(grid1); - assertEquals("IntegerMatrix 1x1 failed get", 1, matrix.get(0, 0)); + assertEquals("IntegerMatrix 1x1 failed get.", 1, matrix.get(0, 0)); //GetRow IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("IntegerMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed getColumn", correctMatrix, matrix.getCol(0)); + assertEquals("IntegerMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); //2x2 matrix = new IntegerMatrix(grid2); - assertEquals("IntegerMatrix 2x2 failed get", 1, matrix.get(0, 0)); + assertEquals("IntegerMatrix 2x2 failed get.", 1, matrix.get(0, 0)); //GetRow correctMatrix = new IntegerMatrix(new int[][]{{1, 2}}); - assertEquals("IntegerMatrix 2x2 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("IntegerMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1} }); - assertEquals("IntegerMatrix 2x2 failed getColumns", correctMatrix, matrix.getCol(0)); + assertEquals("IntegerMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0)); //3x3 matrix = new IntegerMatrix(grid3); - assertEquals("IntegerMatrix 3x3 failed get", 1, matrix.get(0, 0)); + assertEquals("IntegerMatrix 3x3 failed get.", 1, matrix.get(0, 0)); //GetRow correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}}); - assertEquals("IntegerMatrix 3x3 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("IntegerMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1}, {1} }); - assertEquals("IntegerMatrix 3x3 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("IntegerMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0)); //4x4 matrix = new IntegerMatrix(grid4); - assertEquals("IntegerMatrix 4x4 failed get", 1, matrix.get(0, 0)); + assertEquals("IntegerMatrix 4x4 failed get.", 1, matrix.get(0, 0)); //GetRow correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4}}); - assertEquals("IntegerMatrix 4x4 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("IntegerMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, @@ -203,14 +203,14 @@ public class TestIntegerMatrix{ {1}, {1} }); - assertEquals("IntegerMatrix 4x4 failed getCol", correctMatrix, matrix.getCol(0)); + assertEquals("IntegerMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0)); //10x10 matrix = new IntegerMatrix(grid10); - assertEquals("IntegerMatrix 10x10 failed get", 1, matrix.get(0, 0)); + assertEquals("IntegerMatrix 10x10 failed get.", 1, matrix.get(0, 0)); //GetRow correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); - assertEquals("IntegerMatrix 10x10 failed getRow", correctMatrix, matrix.getRow(0)); + assertEquals("IntegerMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, @@ -224,7 +224,7 @@ public class TestIntegerMatrix{ {1}, {1} }); - assertEquals("IntegerMatrix 10x10 failed getColumn", correctMatrix, matrix.getCol(0)); + assertEquals("IntegerMatrix 10x10 failed getColumn.", correctMatrix, matrix.getCol(0)); } @Test @@ -234,15 +234,15 @@ public class TestIntegerMatrix{ IntegerMatrix matrix = new IntegerMatrix(grid1); matrix.set(0, 0, 2); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); - assertEquals("IntegerMatrix 1x1 failed set", correctMatrix, matrix); + assertEquals("IntegerMatrix 1x1 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new int[]{0}); correctMatrix = new IntegerMatrix(new int[][]{{0}}); - assertEquals("IntegerMatrix 1x1 failed setRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 1x1 failed setRow.", correctMatrix, matrix); //SetColumn matrix.setCol(0, new int[]{1}); correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed setCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 1x1 failed setCol.", correctMatrix, matrix); //2x2 //Set @@ -252,14 +252,14 @@ public class TestIntegerMatrix{ {3, 2}, {1, 2} }); - assertEquals("IntegerMatrix 2x2 failed set", correctMatrix, matrix); + assertEquals("IntegerMatrix 2x2 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(1, new int[]{2, 1}); correctMatrix = new IntegerMatrix(new int[][]{ {3, 2}, {2, 1} }); - assertEquals("IntegerMatrix 2x2 failed set row", correctMatrix, matrix); + assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); //SetColumn matrix = new IntegerMatrix(grid2); matrix.setCol(0, new int[]{3, 3}); @@ -267,7 +267,7 @@ public class TestIntegerMatrix{ {3, 2}, {3, 2} }); - assertEquals("IntegerMatrix 2x2 failed set row", correctMatrix, matrix); + assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); //3x3 //Set @@ -278,7 +278,7 @@ public class TestIntegerMatrix{ {1, 2, 3}, {1, 2, 3}, }); - assertEquals("IntegerMatrix 3x3 failed set", correctMatrix, matrix); + assertEquals("IntegerMatrix 3x3 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new int[]{0, 1, 2}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -286,7 +286,7 @@ public class TestIntegerMatrix{ {1, 2, 3}, {1, 2, 3} }); - assertEquals("IntegerMatrix 3x3 failed setRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 3x3 failed setRow.", correctMatrix, matrix); //SetColumn matrix.setCol(0, new int[]{0, 0, 0}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -294,7 +294,7 @@ public class TestIntegerMatrix{ {0, 2, 3}, {0, 2, 3} }); - assertEquals("IntegerMatrix 3x3 failed setColumn", correctMatrix, matrix); + assertEquals("IntegerMatrix 3x3 failed setColumn.", correctMatrix, matrix); //4x4 //Set @@ -306,7 +306,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4}, {1, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed set", correctMatrix, matrix); + assertEquals("IntegerMatrix 4x4 failed set.", correctMatrix, matrix); //SetRow matrix.setRow(0, new int[]{4, 3, 2, 1}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -315,7 +315,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4}, {1, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed setRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 4x4 failed setRow.", correctMatrix, matrix); //SetColumn matrix.setCol(0, new int[]{0, 0, 0, 0}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -324,7 +324,7 @@ public class TestIntegerMatrix{ {0, 2, 3, 4}, {0, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed setCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 //Set @@ -342,7 +342,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); - assertEquals("IntegerMatrix 10x10 failed set", correctMatrix, matrix); + assertEquals("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); //SetRow matrix.setRow(0, new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -357,7 +357,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); - assertEquals("IntegerMatrix 10x10 failed setRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); //SetColumn matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -372,7 +372,7 @@ public class TestIntegerMatrix{ {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); - assertEquals("IntegerMatrix 10x10 failed setColumn", correctMatrix, matrix); + assertEquals("IntegerMatrix 10x10 failed setColumn.", correctMatrix, matrix); } @Test @@ -382,12 +382,12 @@ public class TestIntegerMatrix{ IntegerMatrix matrix = new IntegerMatrix(grid1); matrix.addRow(new int[]{1}); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}, {1}}); - assertEquals("IntegerMatrix 1x1 failed addRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 1x1 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new IntegerMatrix(grid1); matrix.addCol(new int[]{1}); correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); - assertEquals("IntegerMatrix 1x1 failed addCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 1x1 failed addCol.", correctMatrix, matrix); //2x2 //AddRow @@ -398,7 +398,7 @@ public class TestIntegerMatrix{ {1, 2}, {1, 2} }); - assertEquals("IntegerMatrix 2x2 failed addRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 2x2 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new IntegerMatrix(grid2); matrix.addCol(new int[]{3, 3}); @@ -406,7 +406,7 @@ public class TestIntegerMatrix{ {1, 2, 3}, {1, 2, 3} }); - assertEquals("IntegerMatrix 2x2 failed addCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 2x2 failed addCol.", correctMatrix, matrix); //3x3 //AddRow @@ -418,7 +418,7 @@ public class TestIntegerMatrix{ {1, 2, 3}, {1, 2, 3} }); - assertEquals("IntegerMatrix 3x3 failed addRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 3x3 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new IntegerMatrix(grid3); matrix.addCol(new int[]{4, 4, 4}); @@ -427,7 +427,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4}, {1, 2, 3, 4} }); - assertEquals("IntegerMatrix 3x3 failed addCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 //AddRow @@ -440,7 +440,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4}, {1, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed addRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 4x4 failed addRow.", correctMatrix, matrix); //AddColumn matrix.addCol(new int[]{5, 5, 5, 5, 5}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -450,7 +450,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5} }); - assertEquals("IntegerMatrix 4x4 failed addCol", correctMatrix, matrix); + assertEquals("IntegerMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 //AddRow @@ -469,7 +469,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); - assertEquals("IntegerMatrix 10x10 failed addRow", correctMatrix, matrix); + assertEquals("IntegerMatrix 10x10 failed addRow.", correctMatrix, matrix); //AddColumn matrix = new IntegerMatrix(grid10); matrix.addCol(new int[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); @@ -485,7 +485,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} }); - assertEquals("IntegerMatrix 10x10 failed addColumn", correctMatrix, matrix); + assertEquals("IntegerMatrix 10x10 failed addColumn.", correctMatrix, matrix); } @Test @@ -495,13 +495,13 @@ public class TestIntegerMatrix{ IntegerMatrix matrix = new IntegerMatrix(grid1); IntegerMatrix secondMatrix = new IntegerMatrix(grid1); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); - assertEquals("IntegerMatrix 1x1 failed appendRight", correctMatrix, matrix.appendRight(secondMatrix)); + assertEquals("IntegerMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1} }); - assertEquals("IntegerMatrix 1x1 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("IntegerMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 //appendRight @@ -511,7 +511,7 @@ public class TestIntegerMatrix{ {1, 2, 1, 2}, {1, 2, 1, 2} }); - assertEquals("IntegerMatrix 2x2 failed appendRight", correctMatrix, matrix.appendRight(secondMatrix)); + assertEquals("IntegerMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new IntegerMatrix(new int[][]{ {1, 2}, @@ -519,7 +519,7 @@ public class TestIntegerMatrix{ {1, 2}, {1, 2} }); - assertEquals("IntegerMatrix 2x2 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("IntegerMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 //appendRight @@ -530,7 +530,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 1, 2, 3}, {1, 2, 3, 1, 2, 3} }); - assertEquals("IntegerMatrix 3x3 failed appendRight", correctMatrix, matrix.appendRight(secondMatrix)); + assertEquals("IntegerMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new IntegerMatrix(new int[][]{ {1, 2, 3}, @@ -540,7 +540,7 @@ public class TestIntegerMatrix{ {1, 2, 3}, {1, 2, 3} }); - assertEquals("IntegerMatrix 3x3 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("IntegerMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 //appendRight @@ -552,7 +552,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 1, 2, 3, 4}, {1, 2, 3, 4, 1, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed appendRight", correctMatrix, matrix.appendRight(secondMatrix)); + assertEquals("IntegerMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new IntegerMatrix(new int[][]{ {1, 2, 3, 4}, @@ -564,7 +564,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4}, {1, 2, 3, 4} }); - assertEquals("IntegerMatrix 4x4 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("IntegerMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 //appendRight @@ -582,7 +582,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); - assertEquals("IntegerMatrix 10x10 failed appendRight", correctMatrix, matrix.appendRight(secondMatrix)); + assertEquals("IntegerMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); //appendBottom correctMatrix = new IntegerMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, @@ -596,7 +596,7 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, }); - assertEquals("IntegerMatrix 10x10 failed appendBottom", correctMatrix, matrix.appendBottom(secondMatrix)); + assertEquals("IntegerMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } @Test @@ -926,7 +926,7 @@ public class TestIntegerMatrix{ matrix = new IntegerMatrix(grid10); transformMatrix = new IntegerMatrix(transformGrid10_2); assertEquals("IntegerMatrix 10x10 failed dot product IntegerMatrix.", 35750, matrix.dotProduct(transformMatrix)); - } + } @Test public void testHadamardProduct(){ @@ -989,7 +989,7 @@ public class TestIntegerMatrix{ //1x1 IntegerMatrix matrix = new IntegerMatrix(grid1); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed transpose", correctMatrix, matrix.transpose()); + assertEquals("IntegerMatrix 1x1 failed transpose.", correctMatrix, matrix.transpose()); //2x2 matrix = new IntegerMatrix(grid2); @@ -997,7 +997,7 @@ public class TestIntegerMatrix{ {1, 1}, {2, 2} }); - assertEquals("IntegerMatrix 2x2 failed transpose", correctMatrix, matrix.transpose()); + assertEquals("IntegerMatrix 2x2 failed transpose.", correctMatrix, matrix.transpose()); //3x3 matrix = new IntegerMatrix(grid3); @@ -1006,7 +1006,7 @@ public class TestIntegerMatrix{ {2, 2, 2}, {3, 3, 3} }); - assertEquals("IntegerMatrix 3x3 failed transpose", correctMatrix, matrix.transpose()); + assertEquals("IntegerMatrix 3x3 failed transpose.", correctMatrix, matrix.transpose()); //4x4 matrix = new IntegerMatrix(grid4); @@ -1016,7 +1016,7 @@ public class TestIntegerMatrix{ {3, 3, 3, 3}, {4, 4, 4, 4} }); - assertEquals("IntegerMatrix 4x4 failed transpose", correctMatrix, matrix.transpose()); + assertEquals("IntegerMatrix 4x4 failed transpose.", correctMatrix, matrix.transpose()); //10x10 matrix = new IntegerMatrix(grid10); @@ -1032,7 +1032,7 @@ public class TestIntegerMatrix{ {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, }); - assertEquals("IntegerMatrix 10x10 failed transpose", correctMatrix, matrix.transpose()); + assertEquals("IntegerMatrix 10x10 failed transpose.", correctMatrix, matrix.transpose()); } @Test diff --git a/src/test/java/com/mattrixwv/matrix/TestModMatrix.java b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java new file mode 100644 index 0000000..7bf192b --- /dev/null +++ b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java @@ -0,0 +1,1311 @@ +//Matrix/src/test/java/com/mattixwv/matrix/TestModMatrix.java +//Mattrixwv +// Created: 02-09-22 +//Modified: 02-09-22 +package com.mattrixwv.matrix; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + + +public class TestModMatrix{ + //Grid 1x1 + private static final int[][] grid1 = { + {1} + }; + private static final int[][] transformGrid1_1 = { + {1} + }; + private static final int[][] transformGrid1_2 = { + {2} + }; + + //Grid 2x2 + private static final int[][] grid2 = { + {1, 2}, + {1, 2} + }; + private static final int[][] transformGrid2_1 = { + {1, 0}, + {1, 0} + }; + private static final int[][] transformGrid2_2 = { + {2, 3}, + {2, 3} + }; + + //Grid 3x3 + private static final int[][] grid3 = { + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }; + private static final int[][] transformGrid3_1 = { + {2, 1, 0}, + {2, 1, 0}, + {2, 1, 0} + }; + private static final int[][] transformGrid3_2 = { + {2, 3, 4}, + {2, 3, 4}, + {2, 3, 4} + }; + + //Grid 4x4 + private static final int[][] grid4 = { + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }; + private static final int[][] transformGrid4_1 = { + {3, 2, 1, 0}, + {3, 2, 1, 0}, + {3, 2, 1, 0}, + {3, 2, 1, 0} + }; + private static final int[][] transformGrid4_2 = { + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5} + }; + + //Grid 10x10 + private static final int[][] grid10 = { + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }; + private static final int[][] transformGrid10_1 = { + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + }; + private static final int[][] transformGrid10_2 = { + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11} + }; + + + @Test + public void testEquals(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + assertTrue("ModMatrix 1x1 failed equals ModMatrix.", matrix.equals(matrix)); + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals = matrix.equals(grid1); + assertTrue("ModMatrix 1x1 failed equals int[][].", gridEquals); + + //2x2 + matrix = new ModMatrix(grid2, 26); + assertTrue("ModMatrix 2x2 failed equals ModMatrix.", matrix.equals(matrix)); + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals2 = matrix.equals(grid2); + assertTrue("ModMatrix 2x2 failed equals int[][].", gridEquals2); + + //3x3 + matrix = new ModMatrix(grid3, 26); + assertTrue("ModMatrix 3x3 failed equals ModMatrix.", matrix.equals(matrix)); + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals3 = matrix.equals(grid3); + assertTrue("ModMatrix 3x3 failed equals int[][].", gridEquals3); + + //4x4 + matrix = new ModMatrix(grid4, 26); + assertTrue("ModMatrix 4x4 failed equals ModMatrix.", matrix.equals(matrix)); + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals4 = matrix.equals(grid4); + assertTrue("ModMatrix 4x4 failed equals int[][].", gridEquals4); + + //10x10 + matrix = new ModMatrix(grid10, 26); + assertTrue("ModMatrix 10x10 failed equals ModMatrix.", matrix.equals(matrix)); + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals10 = matrix.equals(grid10); + assertTrue("ModMatrix 10x10 failed equals int[][].", gridEquals10); + } + + @Test + public void testGets(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + assertEquals("ModMatrix 1x1 failed get.", 1, matrix.get(0, 0)); + //GetRow + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetColumn + correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + assertEquals("ModMatrix 2x2 failed get.", 1, matrix.get(0, 0)); + //GetRow + correctMatrix = new ModMatrix(new int[][]{{1, 2}}, 26); + assertEquals("ModMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetColumn + correctMatrix = new ModMatrix(new int[][]{ + {1}, + {1}, + }, 26); + assertEquals("ModMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + assertEquals("ModMatrix 3x3 failed get.", 1, matrix.get(0, 0)); + //GetRow + correctMatrix = new ModMatrix(new int[][]{{1, 2, 3}}, 26); + assertEquals("ModMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetCol + correctMatrix = new ModMatrix(new int[][]{ + {1}, + {1}, + {1} + }, 26); + assertEquals("ModMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + assertEquals("ModMatrix 4x4 failed get.", 1, matrix.get(0, 0)); + //GetRow + correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4}}, 26); + assertEquals("ModMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetCol + correctMatrix = new ModMatrix(new int[][]{ + {1}, + {1}, + {1}, + {1} + }, 26); + assertEquals("ModMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + assertEquals("ModMatrix 10x10 failed get.", 1, matrix.get(0, 0)); + //GetRow + correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 26); + assertEquals("ModMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + //GetColumn + correctMatrix = new ModMatrix(new int[][]{ + {1}, + {1}, + {1}, + {1}, + {1}, + {1}, + {1}, + {1}, + {1}, + {1} + }, 26); + assertEquals("ModMatrix 10x10 failed getColumn.", correctMatrix, matrix.getCol(0)); + } + + @Test + public void testSets(){ + //1x1 + //Set + ModMatrix matrix = new ModMatrix(grid1, 26); + matrix.set(0, 0, 2); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); + assertEquals("ModMatrix 1x1 failed set.", correctMatrix, matrix); + //SetRow + matrix.setRow(0, new int[]{0}); + correctMatrix = new ModMatrix(new int[][]{{0}}, 26); + assertEquals("ModMatrix 1x1 failed setRow.", correctMatrix, matrix); + //SetColumn + matrix.setCol(0, new int[]{1}); + correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //2x2 + //Set + matrix = new ModMatrix(grid2, 26); + matrix.set(0, 0, 3); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2}, + {1, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed set.", correctMatrix, matrix); + //SetRow + matrix.setRow(1, new int[]{2, 1}); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2}, + {2, 1} + }, 26); + assertEquals("ModMatrix 2x2 failed set row.", correctMatrix, matrix); + //SetColumn + matrix = new ModMatrix(grid2, 26); + matrix.setCol(0, new int[]{3, 3}); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2}, + {3, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed set row.", correctMatrix, matrix); + + //3x3 + //Set + matrix = new ModMatrix(grid3, 26); + matrix.set(0, 0, 3); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + }, 26); + assertEquals("ModMatrix 3x3 failed set.", correctMatrix, matrix); + //SetRow + matrix.setRow(0, new int[]{0, 1, 2}); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1, 2}, + {1, 2, 3}, + {1, 2, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed setRow.", correctMatrix, matrix); + //SetColumn + matrix.setCol(0, new int[]{0, 0, 0}); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1, 2}, + {0, 2, 3}, + {0, 2, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed setColumn.", correctMatrix, matrix); + + //4x4 + //Set + matrix = new ModMatrix(grid4, 26); + matrix.set(0, 0, 3); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed set.", correctMatrix, matrix); + //SetRow + matrix.setRow(0, new int[]{4, 3, 2, 1}); + correctMatrix = new ModMatrix(new int[][]{ + {4, 3, 2, 1}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed setRow.", correctMatrix, matrix); + //SetColumn + matrix.setCol(0, new int[]{0, 0, 0, 0}); + correctMatrix = new ModMatrix(new int[][]{ + {0, 3, 2, 1}, + {0, 2, 3, 4}, + {0, 2, 3, 4}, + {0, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed setCol.", correctMatrix, matrix); + + //10x10 + //Set + matrix = new ModMatrix(grid10, 26); + matrix.set(0, 0, 3); + correctMatrix = new ModMatrix(new int[][]{ + {3, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }, 26); + assertEquals("ModMatrix 10x10 failed set.", correctMatrix, matrix); + //SetRow + matrix.setRow(0, new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); + correctMatrix = new ModMatrix(new int[][]{ + {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }, 26); + assertEquals("ModMatrix 10x10 failed setRow.", correctMatrix, matrix); + //SetColumn + matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); + correctMatrix = new ModMatrix(new int[][]{ + {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }, 26); + assertEquals("ModMatrix 10x10 failed setColumn.", correctMatrix, matrix); + } + + @Test + public void testAdds(){ + //1x1 + //AddRow + ModMatrix matrix = new ModMatrix(grid1, 26); + matrix.addRow(new int[]{1}); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}, {1}}, 26); + assertEquals("ModMatrix 1x1 failed addRow.", correctMatrix, matrix); + //AddColumn + matrix = new ModMatrix(grid1, 26); + matrix.addCol(new int[]{1}); + correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); + assertEquals("ModMatrix 1x1 failed addCol.", correctMatrix, matrix); + + //2x2 + //AddRow + matrix = new ModMatrix(grid2, 26); + matrix.addRow(new int[]{1, 2}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2}, + {1, 2}, + {1, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed addRow.", correctMatrix, matrix); + //AddColumn + matrix = new ModMatrix(grid2, 26); + matrix.addCol(new int[]{3, 3}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3} + }, 26); + assertEquals("ModMatrix 2x2 failed addCol.", correctMatrix, matrix); + + //3x3 + //AddRow + matrix = new ModMatrix(grid3, 26); + matrix.addRow(new int[]{1, 2, 3}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed addRow.", correctMatrix, matrix); + //AddColumn + matrix = new ModMatrix(grid3, 26); + matrix.addCol(new int[]{4, 4, 4}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 3x3 failed addCol.", correctMatrix, matrix); + + //4x4 + //AddRow + matrix = new ModMatrix(grid4, 26); + matrix.addRow(new int[]{1, 2, 3, 4}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed addRow.", correctMatrix, matrix); + //AddColumn + matrix.addCol(new int[]{5, 5, 5, 5, 5}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5} + }, 26); + assertEquals("ModMatrix 4x4 failed addCol.", correctMatrix, matrix); + + //10x10 + //AddRow + matrix = new ModMatrix(grid10, 26); + matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }, 26); + assertEquals("ModMatrix 10x10 failed addRow.", correctMatrix, matrix); + //AddColumn + matrix = new ModMatrix(grid10, 26); + matrix.addCol(new int[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} + }, 26); + assertEquals("ModMatrix 10x10 failed addColumn.", correctMatrix, matrix); + } + + @Test + public void testAppends(){ + //1x1 + //appendRight + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix secondMatrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); + assertEquals("ModMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); + //appendBottom + correctMatrix = new ModMatrix(new int[][]{ + {1}, + {1} + }, 26); + assertEquals("ModMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //2x2 + //appendRight + matrix = new ModMatrix(grid2, 26); + secondMatrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 1, 2}, + {1, 2, 1, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); + //appendBottom + correctMatrix = new ModMatrix(new int[][]{ + {1, 2}, + {1, 2}, + {1, 2}, + {1, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //3x3 + //appendRight + matrix = new ModMatrix(grid3, 26); + secondMatrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 1, 2, 3}, + {1, 2, 3, 1, 2, 3}, + {1, 2, 3, 1, 2, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); + //appendBottom + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //4x4 + //appendRight + matrix = new ModMatrix(grid4, 26); + secondMatrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 1, 2, 3, 4}, + {1, 2, 3, 4, 1, 2, 3, 4}, + {1, 2, 3, 4, 1, 2, 3, 4}, + {1, 2, 3, 4, 1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); + //appendBottom + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //10x10 + //appendRight + matrix = new ModMatrix(grid10, 26); + secondMatrix = new ModMatrix(grid10, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }, 26); + assertEquals("ModMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); + //appendBottom + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + }, 26); + assertEquals("ModMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + } + + @Test + public void testAddition(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix transformMatrix = new ModMatrix(transformGrid1_1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); + assertEquals("ModMatrix 1x1 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); + assertEquals("ModMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(1)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + transformMatrix = new ModMatrix(transformGrid2_1, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, 2}, + {2, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 3}, + {2, 3} + }, 26); + assertEquals("ModMatrix 2x2 failed add scalar.", correctMatrix, matrix.add(1)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + transformMatrix = new ModMatrix(transformGrid3_1, 26); + correctMatrix = new ModMatrix(new int[][]{ + {3, 3, 3}, + {3, 3, 3}, + {3, 3, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 3, 4}, + {2, 3, 4}, + {2, 3, 4} + }, 26); + assertEquals("ModMatrix 3x3 failed add scalar.", correctMatrix, matrix.add(1)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + transformMatrix = new ModMatrix(transformGrid4_1, 26); + correctMatrix = new ModMatrix(new int[][]{ + {4, 4, 4, 4}, + {4, 4, 4, 4}, + {4, 4, 4, 4}, + {4, 4, 4, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5} + }, 26); + assertEquals("ModMatrix 4x4 failed add scalar.", correctMatrix, matrix.add(1)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + transformMatrix = new ModMatrix(transformGrid10_1, 26); + correctMatrix = new ModMatrix(new int[][]{ + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} + }, 26); + assertEquals("ModMatrix 5x5 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11} + }, 26); + assertEquals("ModMatrix 10x10 failed add ModMatrix.", correctMatrix, matrix.add(1)); + } + + @Test + public void testSubtraction(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix transformMatrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{ + {0} + }, 26); + assertEquals("ModMatrix 1x1 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + assertEquals("ModMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + transformMatrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(2, 2, 0, 26); + assertEquals("ModMatrix 2x2 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1}, + {0, 1} + }, 26); + assertEquals("ModMatrix 2x2 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + transformMatrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(3, 3, 0, 26); + assertEquals("ModMatrix 3x3 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1, 2}, + {0, 1, 2}, + {0, 1, 2} + }, 26); + assertEquals("ModMatrix 3x3 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + transformMatrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(4, 4, 0, 26); + assertEquals("ModMatrix 4x4 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1, 2, 3}, + {0, 1, 2, 3}, + {0, 1, 2, 3}, + {0, 1, 2, 3} + }, 26); + assertEquals("ModMatrix 4x4 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + transformMatrix = new ModMatrix(grid10, 26); + correctMatrix = new ModMatrix(10, 10, 0, 26); + assertEquals("ModMatrix 10x10 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new ModMatrix(new int[][]{ + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + }, 26); + assertEquals("ModMatrix 10x10 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + } + + @Test + public void testMultiplication(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); + ModMatrix correctMatrix = new ModMatrix(transformGrid1_2, 26); + assertEquals("ModMatrix 1x1 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + assertEquals("ModMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + transformMatrix = new ModMatrix(transformGrid2_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {6, 9}, + {6, 9} + }, 26); + assertEquals("ModMatrix 2x2 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + ModMatrix vector = new ModMatrix(new int[][]{ + {2}, + {3} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {8}, + {8} + }, 26); + assertEquals("ModMatrix 2x2 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 4}, + {2, 4} + }, 26); + assertEquals("ModMatrix 2x2 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + transformMatrix = new ModMatrix(transformGrid3_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {12, 18, 24}, + {12, 18, 24}, + {12, 18, 24} + }, 26); + assertEquals("ModMatrix 3x3 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new ModMatrix(new int[][]{ + {2}, + {3}, + {4} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {20}, + {20}, + {20} + }, 26); + assertEquals("ModMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 4, 6}, + {2, 4, 6}, + {2, 4, 6} + }, 26); + assertEquals("ModMatrix 3x3 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + transformMatrix = new ModMatrix(transformGrid4_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {20, 30, 40, 50}, + {20, 30, 40, 50}, + {20, 30, 40, 50}, + {20, 30, 40, 50}, + }, 26); + assertEquals("ModMatrix 4x4 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new ModMatrix(new int[][]{ + {2}, + {3}, + {4}, + {5} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {40}, + {40}, + {40}, + {40} + }, 26); + assertEquals("ModMatrix 4x4 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 4, 6, 8}, + {2, 4, 6, 8}, + {2, 4, 6, 8}, + {2, 4, 6, 8} + }, 26); + assertEquals("ModMatrix 4x4 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + transformMatrix = new ModMatrix(transformGrid10_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, + {110, 165, 220, 275, 330, 385, 440, 495, 550, 605} + }, 26); + assertEquals("ModMatrix 10x10 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); + vector = new ModMatrix(new int[][]{ + {2}, + {3}, + {4}, + {5}, + {6}, + {7}, + {8}, + {9}, + {10}, + {11} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440} + }, 26); + assertEquals("ModMatrix 10x10 failed multiplication vector.", correctMatrix, matrix.multiply(vector)); + correctMatrix = new ModMatrix(new int[][]{ + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, + {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} + }, 26); + assertEquals("ModMatrix 10x10 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + } + + @Test + public void testDotProduct(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); + assertEquals("ModMatrix 1x1 failed dot product ModMatrix.", 2, matrix.dotProduct(transformMatrix)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + transformMatrix = new ModMatrix(transformGrid2_2, 26); + assertEquals("ModMatrix 2x2 failed dot product ModMatrix.", 30, matrix.dotProduct(transformMatrix)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + transformMatrix = new ModMatrix(transformGrid3_2, 26); + assertEquals("ModMatrix 3x3 failed dot product ModMatrix.", 162, matrix.dotProduct(transformMatrix)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + transformMatrix = new ModMatrix(transformGrid4_2, 26); + assertEquals("ModMatrix 4x4 failed dot product ModMatrix.", 560, matrix.dotProduct(transformMatrix)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + transformMatrix = new ModMatrix(transformGrid10_2, 26); + assertEquals("ModMatrix 10x10 failed dot product ModMatrix.", 35750, matrix.dotProduct(transformMatrix)); + } + + @Test + public void testHadamardProduct(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); + assertEquals("ModMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + transformMatrix = new ModMatrix(transformGrid2_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, 6}, + {2, 6} + }, 26); + assertEquals("ModMatrix 2x2 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + transformMatrix = new ModMatrix(transformGrid3_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, 6, 12}, + {2, 6, 12}, + {2, 6, 12} + }, 26); + assertEquals("ModMatrix 3x3 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + transformMatrix = new ModMatrix(transformGrid4_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, 6, 12, 20}, + {2, 6, 12, 20}, + {2, 6, 12, 20}, + {2, 6, 12, 20} + }, 26); + assertEquals("ModMatrix 4x4 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + transformMatrix = new ModMatrix(transformGrid10_2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, + {2, 6, 12, 20, 30, 42, 56, 72, 90, 110} + }, 26); + assertEquals("ModMatrix 10x10 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + } + + @Test + public void testTranspose(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed transpose.", correctMatrix, matrix.transpose()); + + //2x2 + matrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 1}, + {2, 2} + }, 26); + assertEquals("ModMatrix 2x2 failed transpose.", correctMatrix, matrix.transpose()); + + //3x3 + matrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 1, 1}, + {2, 2, 2}, + {3, 3, 3} + }, 26); + assertEquals("ModMatrix 3x3 failed transpose.", correctMatrix, matrix.transpose()); + + //4x4 + matrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed transpose.", correctMatrix, matrix.transpose()); + + //10x10 + matrix = new ModMatrix(grid10, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, + {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, + {6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, + {7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, + {8, 8, 8, 8, 8, 8, 8, 8, 8, 8}, + {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, + }, 26); + assertEquals("ModMatrix 10x10 failed transpose.", correctMatrix, matrix.transpose()); + } + + @Test + public void testDeterminant(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + assertEquals("ModMatrix 1x1 failed determinant.", 1, matrix.determinant()); + + //2x2 + matrix = new ModMatrix(grid2, 26); + assertEquals("ModMatrix 2x2 failed determinant1.", 0, matrix.determinant()); + matrix = new ModMatrix(new int[][]{ + {1, 4}, + {4, 1} + }, 26); + assertEquals("ModMatrix 2x2 failed determinant2.", -15, matrix.determinant()); + + //3x3 + matrix = new ModMatrix(grid3, 26); + assertEquals("ModMatrix 3x3 failed determinant1.", 0, matrix.determinant()); + matrix = new ModMatrix(new int[][]{ + {1, 4, 2}, + {2, 4, 1}, + {4, 1, 2} + }, 26); + assertEquals("ModMatrix 3x3 failed determinant2.", -21, matrix.determinant()); + + //4x4 + matrix = new ModMatrix(grid4, 26); + assertEquals("ModMatrix 4x4 failed determiant1.", 0, matrix.determinant()); + matrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {2, 3, 4, 1}, + {3, 4, 1, 2}, + {4, 1, 2, 3} + }, 26); + assertEquals("ModMatrix 4x4 failed determinant2.", 160, matrix.determinant()); + + //10x10 + matrix = new ModMatrix(grid10, 26); + assertEquals("ModMatrix 10x10 failed determinant1.", 0, matrix.determinant()); + matrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 1}, + {3, 4, 5, 6, 7, 8, 9, 10, 1, 2}, + {4, 5, 6, 7, 8, 9, 10, 1, 2, 3}, + {5, 6, 7, 8, 9, 10, 1, 2, 3, 4}, + {6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, + {7, 8, 9, 10, 1, 2, 3, 4, 5, 6}, + {8, 9, 10, 1, 2, 3, 4, 5, 6, 7}, + {9, 10, 1, 2, 3, 4, 5, 6, 7, 8}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + }, 26); + assertEquals("ModMatrix 10x10 failed determinant2.", -10000000, matrix.determinant()); + } + + @Test + public void testCofactor(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(grid1, 26); + assertEquals("ModMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + + //2x2 + matrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, -1}, + {-2, 1} + }, 26); + assertEquals("ModMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + + //3x3 + matrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(3, 3, 0, 26); + assertEquals("ModMatrix 3x3 failed cofactor1.", correctMatrix, matrix.cofactor()); + matrix = new ModMatrix(new int[][]{ + {1, 4, 2}, + {2, 4, 1}, + {4, 1, 2} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {7, 0, -14}, + {-6, -6, 15}, + {-4, 3, -4} + }, 26); + assertEquals("ModMatrix 3x3 failed cofactor2.", correctMatrix, matrix.cofactor()); + + //4x4 + matrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(4, 4, 0, 26); + assertEquals("ModMatrix 4x4 failed cofactor1.", correctMatrix, matrix.cofactor()); + matrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {2, 3, 4, 1}, + {3, 4, 1, 2}, + {4, 1, 2, 3} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {-36, 4, 4, 44}, + {4, 4, 44, -36}, + {4, 44, -36, 4}, + {44, -36, 4, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed cofactor2.", correctMatrix, matrix.cofactor()); + + //10x10 + //?Skipping 10x10 test because test took > 5s by itself + /* + matrix = new ModMatrix(grid10); + correctMatrix = new ModMatrix(10, 10, 0); + assertEquals("ModMatrix 10x10 failed cofactor1.", correctMatrix, matrix.cofactor()); + */ + } + + @Test + public void testPower(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + + //2x2 + matrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {9, 18}, + {9, 18} + }, 26); + assertEquals("ModMatrix 2x2 failed power.", correctMatrix, matrix.pow(3)); + + //3x3 + matrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(new int[][]{ + {36, 72, 108}, + {36, 72, 108}, + {36, 72, 108} + }, 26); + assertEquals("ModMatrix 3x3 failed power.", correctMatrix, matrix.pow(3)); + + //4x4 + //0 + matrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(new int[][]{ + {1, 1, 1, 1}, + {1, 1, 1, 1}, + {1, 1, 1, 1}, + {1, 1, 1, 1} + }, 26); + assertEquals("ModMatrix 4x4 failed power 0.", correctMatrix, matrix.pow(0)); + //1 + correctMatrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed power 1.", correctMatrix, matrix.pow(1)); + //3 + correctMatrix = new ModMatrix(new int[][]{ + {100, 200, 300, 400}, + {100, 200, 300, 400}, + {100, 200, 300, 400}, + {100, 200, 300, 400} + }, 26); + assertEquals("ModMatrix 4x4 failed power 3.", correctMatrix, matrix.pow(3)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + correctMatrix = new ModMatrix(new int[][]{ + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, + {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250} + }, 26); + assertEquals("ModMatrix 10x10 failed power.", correctMatrix, matrix.pow(3)); + } + + @Test + public void testAdjoint(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(grid1, 26); + assertEquals("ModMatrix 1x1 failed adjoint.", correctMatrix, matrix.adjoint()); + + //2x2 + matrix = new ModMatrix(grid2, 26); + correctMatrix = new ModMatrix(new int[][]{ + {2, -2}, + {-1, 1} + }, 26); + assertEquals("ModMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + + //3x3 + matrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(3, 3, 0, 26); + assertEquals("ModMatrix 3x3 failed adjoint.", correctMatrix, matrix.adjoint()); + + //4x4 + matrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {2, 3, 4, 1}, + {3, 4, 1, 2}, + {4, 1, 2, 3} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {-36, 4, 4, 44}, + {4, 4, 44, -36}, + {4, 44, -36, 4}, + {44, -36, 4, 4} + }, 26); + assertEquals("ModMatrix 4x4 failed adjoint.", correctMatrix, matrix.adjoint()); + + //10x10 + //?Skipping 10x10 test because test took > 5s by itself + /* + matrix = new ModMatrix(grid10); + correctMatrix = new ModMatrix(10, 10, 0); + assertEquals("ModMatrix 10x10 failed adjoint.", correctMatrix, matrix.adjoint()); + */ + } + + @Test + public void testInverse(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(grid1, 26); + assertEquals("ModMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + + //2x2 + matrix = new ModMatrix(new int[][]{ + {1, 4}, + {4, 1} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {19, 2}, + {2, 19} + }, 26); + assertEquals("ModMatrix 2x2 failed inverse.", correctMatrix, matrix.inverse()); + + //3x3 + matrix = new ModMatrix(new int[][]{ + {1, 4, 2}, + {2, 4, 1}, + {4, 1, 2} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {17, 4, 20}, + { 0, 4, 11}, + {18, 3, 20} + }, 26); + assertEquals("ModMatrix 3x3 failed inverse.", correctMatrix, matrix.inverse()); + + //4x4 + matrix = new ModMatrix(new int[][]{ + {1, 2, 3, 4}, + {4, 2, 1, 3}, + {5, 1, 8, 7}, + {5, 6, 4, 2} + }, 26); + correctMatrix = new ModMatrix(new int[][]{ + {12, 24, 22, 19}, + {21, 17, 3, 0}, + { 9, 6, 22, 13}, + {19, 7, 22, 5} + }, 26); + assertEquals("ModMatrix 4x4 failed inverse.", correctMatrix, matrix.inverse()); + + //10x10 + //?Skipped 10x10 because it would take a long time to compute + } +}