From b5bfbf71f4f384c1a35ac7e87c3e7aa9f784491e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 2 Feb 2022 22:10:46 -0500 Subject: [PATCH] Added start of integer matrix --- pom.xml | 85 +++ .../com/mattrixwv/matrix/IntegerMatrix.java | 455 +++++++++++++ .../InvalidCoordinatesException.java | 21 + .../exceptions/InvalidGeometryException.java | 21 + .../exceptions/InvalidRowSizeException.java | 21 + .../mattrixwv/matrix/TestIntegerMatrix.java | 643 ++++++++++++++++++ 6 files changed, 1246 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/mattrixwv/matrix/IntegerMatrix.java create mode 100644 src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java create mode 100644 src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java create mode 100644 src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java create mode 100644 src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4a2e99e --- /dev/null +++ b/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + com.mattrixwv + Matrix + 1.0-SNAPSHOT + + Matrix + https://www.mattrixwv.com + + + UTF-8 + 11 + 11 + 11 + + + + + junit + junit + 4.13.2 + test + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.0.0-M3 + + + enforce-maven + + enforce + + + + + 3.6.3 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + maven-compiler-plugin + 3.8.1 + + + maven-surefire-plugin + 3.0.0-M5 + + + maven-jar-plugin + 3.2.0 + + + maven-install-plugin + 3.0.0-M1 + + + + maven-site-plugin + 3.9.1 + + + maven-project-info-reports-plugin + 3.1.1 + + + + diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java new file mode 100644 index 0000000..2f0fdad --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -0,0 +1,455 @@ +//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 02-01-22 +package com.mattrixwv.matrix; + + +import java.util.StringJoiner; + +import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; +import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; + + +public class IntegerMatrix{ + private int[][] grid; + + //Helper functions + private void setGrid(int[][] grid){ + if(grid.length == 0){ + grid = new int[0][0]; + } + else if(grid[0].length == 0){ + grid = new int[grid.length][0]; + } + else{ + //Make sure all rows are the same length + int length = grid[0].length; + for(int cnt = 1;cnt < grid.length;++cnt){ + if(grid[cnt].length != length){ + throw new InvalidRowSizeException("All rows in a matrix must be the same size"); + } + } + + //Copy every element over to a new grid + int[][] newGrid = new int[grid.length][grid[0].length]; + for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ + for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ + newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt]; + } + } + + //Save the new grid + this.grid = newGrid; + } + } + private int[][] copyGrid(){ + //Allocate memory for the new grid + int[][] newGrid = new int[grid.length][grid[0].length]; + + //Copy every element from the current grid to the new one + for(int row = 0;row < grid.length;++row){ + for(int col = 0;col < grid[0].length;++col){ + newGrid[row][col] = grid[row][col]; + } + } + + //Return the new grid + return newGrid; + } + + //Constructors + public IntegerMatrix(){ + grid = new int[0][0]; + } + public IntegerMatrix(int[][]grid){ + setGrid(grid); + } + public IntegerMatrix(IntegerMatrix matrix){ + setGrid(matrix.grid); + } + + //Gets + public int get(int row, int col){ + //Make sure the row and column are valid + if(row >= grid.length){ + throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); + } + else if(col >= grid[0].length){ + throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); + } + + //Return the location in the grid + return grid[row][col]; + } + public IntegerMatrix getRow(int row){ + //Make sure the row number is valid + if((row < 0) || (row >= grid.length)){ + throw new InvalidCoordinatesException("The row number " + row + " is not valid"); + } + + //Generate a copy of the row + int[][] newRow = new int[1][grid[row].length]; + for(int col = 0;col < grid[row].length;++col){ + newRow[0][col] = grid[row][col]; + } + + //Return the new matrix + return new IntegerMatrix(newRow); + } + public int getNumRows(){ + return grid.length; + } + public IntegerMatrix getCol(int col){ + //Make sure the column number is valid + if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ + throw new InvalidCoordinatesException("The column number " + col + " is not valid"); + } + + //Generate a copy of the col + int[][] newColumn = new int[grid.length][1]; + for(int row = 0;row < grid.length;++row){ + newColumn[row][0] = grid[row][col]; + } + + //Return the new matrix + return new IntegerMatrix(newColumn); + } + public int getNumCols(){ + if(grid.length > 0){ + return grid[0].length; + } + else{ + return 0; + } + } + //Sets + public void set(int row, int col, int value){ + //Make sure the row number is valid + if((row < 0) || (row >= grid.length)){ + throw new InvalidCoordinatesException("Invalid row number " + row); + } + //Make sure the column number is valid + if((col < 0) || (col >= getNumCols())){ + throw new InvalidCoordinatesException("Invalid column number " + col); + } + + //Save the element + grid[row][col] = value; + } + public void setRow(int row, int[] elements){ + //Make sure the row number is valid + if((row < 0) || (row >= grid.length)){ + throw new InvalidCoordinatesException("Invalid row number " + row); + } + //Make sure the number of elements is valid + if(elements.length != getNumCols()){ + throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + } + + //Save the elements + for(int col = 0;col < elements.length;++col){ + grid[row][col] = elements[col]; + } + } + public void setCol(int col, int[] elements){ + //Make sure the column number is valid + if((col < 0) || (col >= getNumCols())){ + throw new InvalidCoordinatesException("Invalid column number " + col); + } + //Make sure the number of elements is valid + if(elements.length != grid.length){ + throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length); + } + + //Save the elements + for(int row = 0;row < elements.length;++row){ + grid[row][col] = elements[row]; + } + } + //Adds + public void addRow(int[] elements){ + //Make sure the number of columns is valid + if((grid.length == 0) || (getNumCols() == elements.length)){ + int[][] newGrid = new int[grid.length + 1][elements.length]; + //Copy all existing data into the new grid + for(int row = 0;row < grid.length;++row){ + newGrid[row] = grid[row]; + } + grid = newGrid; + } + else{ + throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + } + + //Add all elements to the grid + for(int col = 0;col < elements.length;++col){ + grid[grid.length - 1][col] = elements[col]; + } + } + public void addCol(int[] elements){ + //Make sure the number of rows is valid + if(grid.length == 0){ + grid = new int[1][elements.length]; + } + else if(grid.length == elements.length){ + //Copy all existing data into the new grid + for(int row = 0;row < grid.length;++row){ + int[] workingRow = new int[grid[0].length + 1]; + for(int workingCol = 0;workingCol < grid[0].length;++workingCol){ + workingRow[workingCol] = grid[row][workingCol]; + } + grid[row] = workingRow; + } + } + else{ + throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + } + + //Add all elements to the grid + for(int row = 0;row < elements.length;++row){ + grid[row][grid[row].length - 1] = elements[row]; + } + } + + //Simple operations + public static IntegerMatrix generateIdentity(int size){ + //Make sure the size is valid + if(size > 0){ + //Create a grid with the correct size + int[][] newGrid = new int[size][size]; + + //Go through every element and make sure it is set correctly + for(int row = 0;row < size;++row){ + for(int col = 0;col < size;++col){ + if(col == row){ + newGrid[row][col] = 1; + } + else{ + newGrid[row][col] = 0; + } + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + else{ + throw new InvalidGeometryException("An identity matrix must have a size > 0"); + } + } + public static IntegerMatrix generateFilled(int size, int fill){ + //Create a grid with the correct size + int[][] newGrid = new int[size][size]; + + //Set each element in the grid + for(int row = 0;row < size;++row){ + for(int col = 0;col < size;++col){ + newGrid[row][col] = fill; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix add(IntegerMatrix rightSide){ + //Make sure the matrices have compatable geometry + if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){ + throw new InvalidGeometryException("Both matrices must have the same number of rows and columns: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols()); + } + + //Create a new grid with the same elements as the current grid + int[][] newGrid = copyGrid(); + + //Add each element in the righ tmatrix to the corresponding element in the left matrix + for(int row = 0;row < newGrid.length;++row){ + for(int col = 0;col < newGrid[0].length;++col){ + newGrid[row][col] += rightSide.grid[row][col]; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix add(int scalar){ + //Create a new grid with the same elements as the current grid + int[][] newGrid = copyGrid(); + + //Add the scalar to each element in the grid + for(int row = 0;row < newGrid.length;++row){ + for(int col = 0;col < newGrid[0].length;++col){ + newGrid[row][col] += scalar; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix subtract(IntegerMatrix rightSide){ + //Make sure the matrices have compatable geometry + if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){ + throw new InvalidGeometryException("Both matrices must have the same number of rows and columsn: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols()); + } + + //Create a new grid with the same elements as the current gird + int[][] newGrid = copyGrid(); + + //Subtract each element in the righ tmatrix from the corresponding element in the left matrix + for(int row = 0;row < newGrid.length;++row){ + for(int col = 0;col < newGrid[0].length;++col){ + newGrid[row][col] -= grid[row][col]; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix subtract(int scalar){ + //Create a new grid with the same elements as the current grid + int[][] newGrid = copyGrid(); + + //Subtract the scalar from each element in the grid + for(int row = 0;row < newGrid.length;++row){ + for(int col = 0;col < newGrid[0].length;++col){ + newGrid[row][col] -= scalar; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix multiply(IntegerMatrix rightSide){ + //Make sure the matrices have compatable geometry + if(getNumCols() != rightSide.getNumRows()){ + throw new InvalidGeometryException("The left matrix must have the same number of columns as the right matrix has rows: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols()); + } + + //Create a new grid with the same elements as the current grid + int[][] newGrid = new int[getNumRows()][rightSide.getNumCols()]; + + //Multiply each row in the left matrix with each column int he right matrix to come up with the current element + for(int leftRow = 0;leftRow < grid.length;++leftRow){ + for(int rightCol = 0;rightCol < rightSide.getNumCols();++rightCol){ + //Get a sum of the product of each column in the current row (left) and each row in the current column (right) + int elementProductSum = 0; + for(int incrementCounter = 0;incrementCounter < grid.length;++incrementCounter){ + elementProductSum += grid[leftRow][incrementCounter] * rightSide.grid[incrementCounter][rightCol]; + } + newGrid[leftRow][rightCol] = elementProductSum; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public IntegerMatrix multiply(int scalar){ + //Create a new grid with the same elements as the current grid + int[][] newGrid = copyGrid(); + + //Multiply every element in the grid by the scalar + for(int row = 0;row < grid.length;++row){ + for(int col = 0;col < grid[0].length;++col){ + newGrid[row][col] *= scalar; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + public int dotProduct(IntegerMatrix rightSide){ + //Make sure the matrices have compatable geometry + if(getNumCols() != rightSide.getNumRows()){ + throw new InvalidGeometryException("The left matrix must have the same number of columns as the right matrix has rows: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols()); + } + + //Multiply each row in the left matrix with each column in the right matrix to come up with the current element + //?Would be simpler, but slower, to get the product matrix to come up with the current element + int sum = 0; + for(int leftRow = 0;leftRow < grid.length;++leftRow){ + for(int rightCol = 0;rightCol < rightSide.getNumCols();++rightCol){ + for(int incrementCounter = 0;incrementCounter < grid.length;++incrementCounter){ + sum += grid[leftRow][incrementCounter] * rightSide.grid[incrementCounter][rightCol]; + } + } + } + + //Return the sum + return sum; + } + public IntegerMatrix hadamardProduct(IntegerMatrix rightSide){ + //Make sure the matrices have compatable geometry + if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){ + throw new InvalidGeometryException("Both matrices must have the same number of rows and columns: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols()); + } + + //Create a new grid with the same element as the current grid + int[][] newGrid = copyGrid(); + + //Multiply each element in the right matrix to the corresponding element in the left matrix + for(int row = 0;row < newGrid.length;++row){ + for(int col = 0;col < newGrid[0].length;++col){ + newGrid[row][col] *= rightSide.grid[row][col]; + } + } + + //Return the new matrix + return new IntegerMatrix(newGrid); + } + + //Complex operations + + //Object funtions + @Override + public boolean equals(Object rightSide){ + if(rightSide.getClass().equals(this.getClass())){ + IntegerMatrix rightMatrix = (IntegerMatrix)rightSide; + + //Make sure they have the same number of elements + if(grid.length != rightMatrix.grid.length){ + return false; + } + else if(getNumCols() != rightMatrix.getNumCols()){ + return false; + } + + //Check every element + for(int row = 0;row < grid.length;++row){ + for(int col = 0;col < grid[0].length;++col){ + if(grid[row][col] != rightMatrix.grid[row][col]){ + return false; + } + } + } + + //If false hasn't been returned yet then they are equal + return true; + } + else if(rightSide.getClass().equals(int[][].class)){ + int[][] rightMatrix = (int[][])rightSide; + + return equals(new IntegerMatrix(rightMatrix)); + } + else{ + return false; + } + } + @Override + public String toString(){ + StringJoiner matrix = new StringJoiner("\n"); + for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ + StringJoiner row = new StringJoiner(",", "[", "]"); + + for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ + row.add(Integer.toString(grid[rowCnt][colCnt])); + } + + matrix.add(row.toString()); + } + return matrix.toString(); + } + @Override + public IntegerMatrix clone(){ + return new IntegerMatrix(grid); + } +} diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java new file mode 100644 index 0000000..166fda7 --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java @@ -0,0 +1,21 @@ +//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidCoordinates.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 02-01-22 +package com.mattrixwv.matrix.exceptions; + + +public class InvalidCoordinatesException extends RuntimeException{ + public InvalidCoordinatesException(){ + super(); + } + public InvalidCoordinatesException(String message){ + super(message); + } + public InvalidCoordinatesException(Throwable throwable){ + super(throwable); + } + public InvalidCoordinatesException(String message, Throwable throwable){ + super(message, throwable); + } +} diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java new file mode 100644 index 0000000..3a11a9a --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java @@ -0,0 +1,21 @@ +//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidGeometryException.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 02-02-22 +package com.mattrixwv.matrix.exceptions; + + +public class InvalidGeometryException extends RuntimeException{ + public InvalidGeometryException(){ + super(); + } + public InvalidGeometryException(String message){ + super(message); + } + public InvalidGeometryException(Throwable throwable){ + super(throwable); + } + public InvalidGeometryException(String message, Throwable throwable){ + super(message, throwable); + } +} diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java new file mode 100644 index 0000000..fa6432f --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java @@ -0,0 +1,21 @@ +//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidRowSizeException.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 02-01-22 +package com.mattrixwv.matrix.exceptions; + + +public class InvalidRowSizeException extends RuntimeException{ + public InvalidRowSizeException(){ + super(); + } + public InvalidRowSizeException(String message){ + super(message); + } + public InvalidRowSizeException(Throwable throwable){ + super(throwable); + } + public InvalidRowSizeException(String message, Throwable throwable){ + super(message, throwable); + } +} diff --git a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java new file mode 100644 index 0000000..fac1525 --- /dev/null +++ b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java @@ -0,0 +1,643 @@ +//Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 02-02-22 +package com.mattrixwv.matrix; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + + +public class TestIntegerMatrix{ + //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; + IntegerMatrix matrix = new IntegerMatrix(grid1); + 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); + + //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); + + //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); + + //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); + + //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); + } + + @Test + public void testGetsSetsAdds(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + 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)); + //GetColumn + correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals("IntegerMatrix 1x1 failed getColumn", correctMatrix, matrix.getCol(0)); + //Set + matrix.set(0, 0, 2); + correctMatrix = new IntegerMatrix(new int[][]{{2}}); + 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); + //SetColumn + matrix.setCol(0, new int[]{1}); + correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals("IntegerMatrix 1x1 failed setCol", correctMatrix, matrix); + //AddRow + matrix.addRow(new int[]{1}); + correctMatrix = new IntegerMatrix(new int[][]{{1}, {1}}); + 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); + + + //2x2 + matrix = new IntegerMatrix(grid2); + assertEquals("IntegerMatrix 2x2 failed get", 1, matrix.get(0, 0)); + //GetRow + //GetColumn + //Set + //SetRow + //SetColumn + //AddRow + //AddColumn + + + //3x3 + matrix = new IntegerMatrix(grid3); + assertEquals("IntegerMatrix 3x3 failed get", 1, matrix.get(0, 0)); + //GetRow + //GetColumn + //Set + //SetRow + //SetColumn + //AddRow + //AddColumn + + + //4x4 + matrix = new IntegerMatrix(grid4); + assertEquals("IntegerMatrix 4x4 failed get", 1, matrix.get(0, 0)); + //GetRow + //GetColumn + //Set + //SetRow + //SetColumn + //AddRow + //AddColumn + + + //10x10 + matrix = new IntegerMatrix(grid10); + assertEquals("IntegerMatrix 10x10 failed get", 1, matrix.get(0, 0)); + //GetRow + //GetColumn + //Set + //SetRow + //SetColumn + //AddRow + //AddColumn + } + + @Test + public void testAddition(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_1); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); + assertEquals("IntegerMatrix 1x1 failed add IntegerMatrix", correctMatrix, matrix.add(transformMatrix)); + assertEquals("IntegerMatrix 1x1 failed add scalar", correctMatrix, matrix.add(1)); + + //2x2 + matrix = new IntegerMatrix(grid2); + transformMatrix = new IntegerMatrix(transformGrid2_1); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 2}, + {2, 2} + }); + assertEquals("IntegerMatrix 2x2 failed add IntegerMatrix", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 3}, + {2, 3} + }); + assertEquals("IntegerMatrix 2x2 failed add scalar", correctMatrix, matrix.add(1)); + + //3x3 + matrix = new IntegerMatrix(grid3); + transformMatrix = new IntegerMatrix(transformGrid3_1); + correctMatrix = new IntegerMatrix(new int[][]{ + {3, 3, 3}, + {3, 3, 3}, + {3, 3, 3} + }); + assertEquals("IntegerMatrix 3x3 failed add IntegerMatrix", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 3, 4}, + {2, 3, 4}, + {2, 3, 4} + }); + assertEquals("IntegerMatrix 3x3 failed add scalar", correctMatrix, matrix.add(1)); + + //4x4 + matrix = new IntegerMatrix(grid4); + transformMatrix = new IntegerMatrix(transformGrid4_1); + correctMatrix = new IntegerMatrix(new int[][]{ + {4, 4, 4, 4}, + {4, 4, 4, 4}, + {4, 4, 4, 4}, + {4, 4, 4, 4} + }); + assertEquals("IntegerMatrix 4x4 failed add IntegerMatrix", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5}, + {2, 3, 4, 5} + }); + assertEquals("IntegerMatrix 4x4 failed add scalar", correctMatrix, matrix.add(1)); + + //10x10 + matrix = new IntegerMatrix(grid10); + transformMatrix = new IntegerMatrix(transformGrid10_1); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 5x5 failed add IntegerMatrix", correctMatrix, matrix.add(transformMatrix)); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 10x10 failed add IntegerMatrix", correctMatrix, matrix.add(1)); + } + + @Test + public void testSubtraction(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_1); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{ + {0} + }); + assertEquals("IntegerMatrix 1x1 failed subtract IntegerMatrix", correctMatrix, matrix.subtract(transformMatrix)); + assertEquals("IntegerMatrix 1x1 failed subtract scalar", correctMatrix, matrix.subtract(1)); + + //2x2 + matrix = new IntegerMatrix(grid2); + transformMatrix = new IntegerMatrix(grid2); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 0}, + {0, 0} + }); + assertEquals("IntegerMatrix 2x2 failed subtract IntegerMatrix", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 1}, + {0, 1} + }); + assertEquals("IntegerMatrix 2x2 failed subtract scalar", correctMatrix, matrix.subtract(1)); + + //3x3 + matrix = new IntegerMatrix(grid3); + transformMatrix = new IntegerMatrix(grid3); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0} + }); + assertEquals("IntegerMatrix 3x3 failed subtract IntegerMatrix", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 1, 2}, + {0, 1, 2}, + {0, 1, 2} + }); + assertEquals("IntegerMatrix 3x3 failed subtract scalar", correctMatrix, matrix.subtract(1)); + + //4x4 + matrix = new IntegerMatrix(grid4); + transformMatrix = new IntegerMatrix(grid4); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0} + }); + assertEquals("IntegerMatrix 4x4 failed subtract IntegerMatrix", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 1, 2, 3}, + {0, 1, 2, 3}, + {0, 1, 2, 3}, + {0, 1, 2, 3} + }); + assertEquals("IntegerMatrix 4x4 failed subtract scalar", correctMatrix, matrix.subtract(1)); + + //10x10 + matrix = new IntegerMatrix(grid10); + transformMatrix = new IntegerMatrix(grid10); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + }); + assertEquals("IntegerMatrix 10x10 failed subtract IntegerMatrix", correctMatrix, matrix.subtract(transformMatrix)); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 10x10 failed subtract scalar", correctMatrix, matrix.subtract(1)); + } + + @Test + public void testMultiplication(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2); + IntegerMatrix correctMatrix = new IntegerMatrix(transformGrid1_2); + assertEquals("IntegerMatrix 1x1 failed multiplication IntegerMatrix", correctMatrix, matrix.multiply(transformMatrix)); + assertEquals("IntegerMatrix 1x1 failed multiplication scalar", correctMatrix, matrix.multiply(2)); + + //2x2 + matrix = new IntegerMatrix(grid2); + transformMatrix = new IntegerMatrix(transformGrid2_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {6, 9}, + {6, 9} + }); + assertEquals("IntegerMatrix 2x2 failed multiplication IntegerMatrix", correctMatrix, matrix.multiply(transformMatrix)); + IntegerMatrix vector = new IntegerMatrix(new int[][]{ + {2}, + {3} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {8}, + {8} + }); + assertEquals("IntegerMatrix 2x2 failed multiplication vector", correctMatrix, matrix.multiply(vector)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 4}, + {2, 4} + }); + assertEquals("IntegerMatrix 2x2 failed multiplication scalar", correctMatrix, matrix.multiply(2)); + + //3x3 + matrix = new IntegerMatrix(grid3); + transformMatrix = new IntegerMatrix(transformGrid3_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {12, 18, 24}, + {12, 18, 24}, + {12, 18, 24} + }); + assertEquals("IntegerMatrix 3x3 failed multiplication IntegerMatrix", correctMatrix, matrix.multiply(transformMatrix)); + vector = new IntegerMatrix(new int[][]{ + {2}, + {3}, + {4} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {20}, + {20}, + {20} + }); + assertEquals("IntegerMatrix 3x3 failed multiplication vector", correctMatrix, matrix.multiply(vector)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 4, 6}, + {2, 4, 6}, + {2, 4, 6} + }); + assertEquals("IntegerMatrix 3x3 failed multiplication scalar", correctMatrix, matrix.multiply(2)); + + //4x4 + matrix = new IntegerMatrix(grid4); + transformMatrix = new IntegerMatrix(transformGrid4_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {20, 30, 40, 50}, + {20, 30, 40, 50}, + {20, 30, 40, 50}, + {20, 30, 40, 50}, + }); + assertEquals("IntegerMatrix 4x4 failed multiplication IntegerMatrix", correctMatrix, matrix.multiply(transformMatrix)); + vector = new IntegerMatrix(new int[][]{ + {2}, + {3}, + {4}, + {5} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {40}, + {40}, + {40}, + {40} + }); + assertEquals("IntegerMatrix 4x4 failed multiplication vector", correctMatrix, matrix.multiply(vector)); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 4, 6, 8}, + {2, 4, 6, 8}, + {2, 4, 6, 8}, + {2, 4, 6, 8} + }); + assertEquals("IntegerMatrix 4x4 failed multiplication scalar", correctMatrix, matrix.multiply(2)); + + //10x10 + matrix = new IntegerMatrix(grid10); + transformMatrix = new IntegerMatrix(transformGrid10_2); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 10x10 failed multiplication IntegerMatrix", correctMatrix, matrix.multiply(transformMatrix)); + vector = new IntegerMatrix(new int[][]{ + {2}, + {3}, + {4}, + {5}, + {6}, + {7}, + {8}, + {9}, + {10}, + {11} + }); + correctMatrix = new IntegerMatrix(new int[][]{ + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440}, + {440} + }); + assertEquals("IntegerMatrix 10x10 failed multiplication vector", correctMatrix, matrix.multiply(vector)); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 10x10 failed multiplication scalar", correctMatrix, matrix.multiply(2)); + } + + @Test + public void testDotProduct(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2); + assertEquals("IntegerMatrix 1x1 failed dot product IntegerMatrix", 2, matrix.dotProduct(transformMatrix)); + + //2x2 + matrix = new IntegerMatrix(grid2); + transformMatrix = new IntegerMatrix(transformGrid2_2); + assertEquals("IntegerMatrix 2x2 failed dot product IntegerMatrix", 30, matrix.dotProduct(transformMatrix)); + + //3x3 + matrix = new IntegerMatrix(grid3); + transformMatrix = new IntegerMatrix(transformGrid3_2); + assertEquals("IntegerMatrix 3x3 failed dot product IntegerMatrix", 162, matrix.dotProduct(transformMatrix)); + + //4x4 + matrix = new IntegerMatrix(grid4); + transformMatrix = new IntegerMatrix(transformGrid4_2); + assertEquals("IntegerMatrix 4x4 failed dot product IntegerMatrix", 560, matrix.dotProduct(transformMatrix)); + + //10x10 + matrix = new IntegerMatrix(grid10); + transformMatrix = new IntegerMatrix(transformGrid10_2); + assertEquals("IntegerMatrix 10x10 failed dot product IntegerMatrix", 35750, matrix.dotProduct(transformMatrix)); + } + + @Test + public void testHadamardProduct(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); + assertEquals("IntegerMatrix 1x1 failed dot product IntegerMatrix", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //2x2 + matrix = new IntegerMatrix(grid2); + transformMatrix = new IntegerMatrix(transformGrid2_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 6}, + {2, 6} + }); + assertEquals("IntegerMatrix 2x2 failed dot product IntegerMatrix", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //3x3 + matrix = new IntegerMatrix(grid3); + transformMatrix = new IntegerMatrix(transformGrid3_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 6, 12}, + {2, 6, 12}, + {2, 6, 12} + }); + assertEquals("IntegerMatrix 3x3 failed dot product IntegerMatrix", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //4x4 + matrix = new IntegerMatrix(grid4); + transformMatrix = new IntegerMatrix(transformGrid4_2); + correctMatrix = new IntegerMatrix(new int[][]{ + {2, 6, 12, 20}, + {2, 6, 12, 20}, + {2, 6, 12, 20}, + {2, 6, 12, 20} + }); + assertEquals("IntegerMatrix 4x4 failed dot product IntegerMatrix", correctMatrix, matrix.hadamardProduct(transformMatrix)); + + //10x10 + matrix = new IntegerMatrix(grid10); + transformMatrix = new IntegerMatrix(transformGrid10_2); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 10x10 failed dot product IntegerMatrix", correctMatrix, matrix.hadamardProduct(transformMatrix)); + } +}