From 2162130cd3ad39da7062ad92b66ce65b4c0f7eb1 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 11 Aug 2024 18:46:38 -0400 Subject: [PATCH] Bugfixes Update tests Add javadoc comments --- pom.xml | 3 +- .../mattrixwv/matrix/BigIntegerMatrix.java | 426 +- .../com/mattrixwv/matrix/DoubleMatrix.java | 443 +- .../com/mattrixwv/matrix/IntegerMatrix.java | 426 +- .../java/com/mattrixwv/matrix/LongMatrix.java | 426 +- .../java/com/mattrixwv/matrix/ModMatrix.java | 465 +- .../InvalidCoordinatesException.java | 48 +- .../exceptions/InvalidGeometryException.java | 48 +- .../exceptions/InvalidRowSizeException.java | 37 +- .../exceptions/InvalidScalarException.java | 37 +- .../exceptions/NullMatrixException.java | 37 +- .../matrix/TestBigIntegerMatrix.java | 3739 ++++++++------- .../mattrixwv/matrix/TestDoubleMatrix.java | 3721 ++++++++------- .../mattrixwv/matrix/TestIntegerMatrix.java | 4137 +++++++++-------- .../com/mattrixwv/matrix/TestLongMatrix.java | 3717 ++++++++------- .../com/mattrixwv/matrix/TestModMatrix.java | 4136 +++++++++------- .../TestInvalidCoordinatesException.java | 40 +- .../TestInvalidGeometryException.java | 26 +- .../TestInvalidRowSizeException.java | 26 +- .../TestInvalidScalarException.java | 26 +- .../exceptions/TestNullMatrixException.java | 27 +- src/test/resources/junit-platform.properties | 4 + 22 files changed, 13147 insertions(+), 8848 deletions(-) create mode 100644 src/test/resources/junit-platform.properties diff --git a/pom.xml b/pom.xml index ea06e2c..9b39d71 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ com.mattrixwv matrix - 1.1.4-SNAPSHOT + 1.2.0 Matrix A library for performing Matrix operations @@ -252,7 +252,6 @@ ${gpg.keyname} - ${gpg.keyname} diff --git a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java index 733a566..aac56b9 100644 --- a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 06-30-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; @@ -16,10 +16,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; +/** + * Represents a matrix of big integers and provides various matrix operations. + */ public class BigIntegerMatrix{ + /** + * The grid that represents the matrix + */ protected BigInteger[][] grid; - //Helper functions + //?Helper functions + /** + * Sets the matrix grid to the specified 2D array. Validates the input to ensure + * all rows are of equal length. + * + * @param grid The 2D array to set as the matrix grid. + * @throws InvalidRowSizeException If the rows of the matrix are not all the same length. + */ protected void setGrid(BigInteger[][] grid){ if(grid.length == 0){ this.grid = new BigInteger[0][0]; @@ -46,18 +59,32 @@ public class BigIntegerMatrix{ this.grid = newGrid; } } + /** + * Creates a deep copy of the matrix grid. + * + * @return A new BigIntegerMatrix instance containing the copied grid. + */ protected BigInteger[][] copyGrid(){ + if(getNumCols() == 0){ + return new BigInteger[grid.length][0]; + } + //Allocate memory for the new grid BigInteger[][] newGrid = new BigInteger[grid.length][grid[0].length]; //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - newGrid[row] = Arrays.copyOf(grid[row], grid.length); + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } //Return the new grid return newGrid; } + /** + * Checks if the matrix is a square matrix (i.e., the number of rows equals the number of columns). + * + * @return {@code true} if the matrix is square, {@code false} otherwise. + */ public boolean isSquare(){ if(getNumRows() == 0){ return false; @@ -66,7 +93,16 @@ public class BigIntegerMatrix{ return getNumRows() == getNumCols(); } } - //Returns a matrix with the supplied row and column removed + /** + * Creates a new matrix with the specified row and column removed. + * This matrix is smaller by one row and one column than the current matrix. + * + * @param row The index of the row to remove. + * @param col The index of the column to remove. + * @return A new BigIntegerMatrix instance with the specified row and column removed. + * @throws InvalidGeometryException If the matrix is not square or too small for Laplace expansion. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ protected BigIntegerMatrix laplaceExpansionHelper(int row, int col){ //Make sure the matrix is large enough to have this operation performed if((getNumRows() <= 1) || (getNumCols() <= 1)){ @@ -110,16 +146,37 @@ public class BigIntegerMatrix{ return new BigIntegerMatrix(newGrid); } - //Constructors + //?Constructors + /** + * Constructs an empty matrix (0x0). + */ public BigIntegerMatrix(){ grid = new BigInteger[0][0]; } + /** + * Constructs a matrix with the specified grid. + * + * @param grid The 2D array to initialize the matrix with. + */ public BigIntegerMatrix(BigInteger[][] grid){ setGrid(grid); } + /** + * Constructs a copy of the specified matrix. + * + * @param matrix The matrix to copy. + */ public BigIntegerMatrix(BigIntegerMatrix matrix){ setGrid(matrix.grid); } + /** + * Constructs a matrix with the specified number of rows and columns, filled with the specified value. + * + * @param rows The number of rows. + * @param cols The number of columns. + * @param fill The value to fill the matrix with. + * @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero. + */ public BigIntegerMatrix(int rows, int cols, BigInteger fill){ if(rows <= 0){ throw new InvalidGeometryException("A filled matrix must have at least 1 row"); @@ -137,7 +194,15 @@ public class BigIntegerMatrix{ } } - //Gets + //?Gets + /** + * Gets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @return The value at the specified row and column. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public BigInteger get(int row, int col){ //Make sure the row and column are valid if(row >= grid.length){ @@ -146,7 +211,7 @@ public class BigIntegerMatrix{ else if(row < 0){ throw new InvalidCoordinatesException("Row cannot be less than 0"); } - else if(col >= grid[0].length){ + else if(col >= grid[row].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } else if(col < 0){ @@ -156,6 +221,13 @@ public class BigIntegerMatrix{ //Return the location in the grid return grid[row][col]; } + /** + * Returns a new matrix that is a copy of the specified row. + * + * @param row The index of the row to retrieve. + * @return A new BigIntegerMatrix instance containing the specified row. + * @throws InvalidCoordinatesException If the row index is out of bounds. + */ public BigIntegerMatrix getRow(int row){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -169,12 +241,24 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newRow); } + /** + * Returns the number of rows in the matrix. + * + * @return The number of rows. + */ public int getNumRows(){ return grid.length; } + /** + * Returns a new matrix that is a copy of the specified column. + * + * @param col The index of the column to retrieve. + * @return A new BigIntegerMatrix instance containing the specified column. + * @throws InvalidCoordinatesException If the column index is out of bounds. + */ public BigIntegerMatrix getCol(int col){ //Make sure the column number is valid - if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ + if((col < 0) || (grid.length == 0) || (col >= grid[0].length)){ throw new InvalidCoordinatesException("The column number " + col + " is not valid"); } @@ -187,6 +271,11 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newColumn); } + /** + * Returns the number of columns in the matrix. + * + * @return The number of columns. + */ public int getNumCols(){ if(grid.length > 0){ return grid[0].length; @@ -196,7 +285,15 @@ public class BigIntegerMatrix{ } } - //Sets + //?Sets + /** + * Sets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @param value The value to set. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public void set(int row, int col, BigInteger value){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -210,6 +307,14 @@ public class BigIntegerMatrix{ //Save the element grid[row][col] = value; } + /** + * Sets the specified row with the given array of elements. + * + * @param row The row index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the row index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void setRow(int row, BigInteger[] elements){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -226,6 +331,14 @@ public class BigIntegerMatrix{ //Save the elements grid[row] = Arrays.copyOf(elements, elements.length); } + /** + * Sets the specified row with the given matrix containing a single row. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void setRow(int row, BigIntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -239,6 +352,14 @@ public class BigIntegerMatrix{ //Set the row setRow(row, matrix.grid[0]); } + /** + * Sets the specified column with the given array of elements. + * + * @param col The column index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the column index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void setCol(int col, BigInteger[] elements){ //Make sure the column number is valid if((col < 0) || (col >= getNumCols())){ @@ -249,7 +370,7 @@ public class BigIntegerMatrix{ throw new InvalidGeometryException("Column cannot be null"); } else if(elements.length != grid.length){ - throw new InvalidCoordinatesException(elements.length, grid.length); + throw new InvalidGeometryException(elements.length, grid.length); } //Save the elements @@ -257,6 +378,14 @@ public class BigIntegerMatrix{ grid[row][col] = elements[row]; } } + /** + * Sets the specified column with the given matrix containing a single column. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void setCol(int col, BigIntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -276,7 +405,14 @@ public class BigIntegerMatrix{ setCol(col, vector); } - //Adds + //?Adds + /** + * Adds a new row with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new row. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void addRow(BigInteger[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -298,6 +434,13 @@ public class BigIntegerMatrix{ //Add all elements to the grid grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void addRow(BigIntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -311,6 +454,13 @@ public class BigIntegerMatrix{ //Add the row addRow(matrix.grid[0]); } + /** + * Adds a new column with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new column. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void addCol(BigInteger[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -334,6 +484,13 @@ public class BigIntegerMatrix{ throw new InvalidGeometryException(elements.length, getNumCols()); } } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void addCol(BigIntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -352,6 +509,14 @@ public class BigIntegerMatrix{ //Add the column addCol(vector); } + /** + * Appends the specified matrix to the right side of the current matrix. + * + * @param rightSide The matrix to append to the right side. + * @return A new BigIntegerMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ public BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){ //Make sure the matrix isn't null if(rightSide == null){ @@ -377,26 +542,34 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } - public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){ + /** + * Appends the specified matrix to the bottom of the current matrix. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new BigIntegerMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ + public BigIntegerMatrix appendBottom(BigIntegerMatrix bottomSide){ //Make sure the matrix isn't null - if(rightSide == null){ + if(bottomSide == null){ throw new NullMatrixException(); } //Make sure the matrices have the same number of columns - else if(getNumCols() != rightSide.getNumCols()){ - throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); + else if(getNumCols() != bottomSide.getNumCols()){ + throw new InvalidGeometryException("Invalid number of columns. " + bottomSide.getNumCols() + " must be " + getNumCols()); } //Traverse both matrices and set their values in the new matrix - BigInteger[][] newGrid = new BigInteger[getNumRows() + rightSide.getNumRows()][getNumCols()]; + BigInteger[][] newGrid = new BigInteger[getNumRows() + bottomSide.getNumRows()][getNumCols()]; for(int col = 0;col < getNumCols();++col){ //Set all elements from the current grid's column for(int row = 0;row < getNumRows();++row){ newGrid[row][col] = grid[row][col]; } //Set all elements from the right side grid's column - for(int row = 0;row < rightSide.getNumRows();++row){ - newGrid[getNumRows() + row][col] = rightSide.grid[row][col]; + for(int row = 0;row < bottomSide.getNumRows();++row){ + newGrid[getNumRows() + row][col] = bottomSide.grid[row][col]; } } @@ -404,7 +577,14 @@ public class BigIntegerMatrix{ return new BigIntegerMatrix(newGrid); } - //Simple operations + //?Simple operations + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @return A new BigIntegerMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static BigIntegerMatrix generateIdentity(int size){ //Make sure the size is valid if(size > 0){ @@ -430,8 +610,20 @@ public class BigIntegerMatrix{ throw new InvalidGeometryException("An identity matrix must have a size > 0"); } } + /** + * Adds the specified matrix to the current matrix. + * + * @param rightSide The matrix to add. + * @return A new BigIntegerMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public BigIntegerMatrix add(BigIntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -439,7 +631,7 @@ public class BigIntegerMatrix{ //Create a new grid with the same elements as the current grid BigInteger[][] newGrid = copyGrid(); - //Add each element in the righ tmatrix to the corresponding element in the left matrix + //Add 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] = newGrid[row][col].add(rightSide.grid[row][col]); @@ -449,6 +641,12 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Adds the scalar to every element in the matrix. + * + * @param scalar The scalar to add. + * @return A new BigIntegerMatrix instance with the result of the addition. + */ public BigIntegerMatrix add(BigInteger scalar){ //Create a new grid with the same elements as the current grid BigInteger[][] newGrid = copyGrid(); @@ -463,8 +661,20 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Subtracts the specified matrix from the current matrix. + * + * @param rightSide The matrix to subtract. + * @return A new BigIntegerMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public BigIntegerMatrix subtract(BigIntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -472,16 +682,22 @@ public class BigIntegerMatrix{ //Create a new grid with the same elements as the current gird BigInteger[][] newGrid = copyGrid(); - //Subtract each element in the righ tmatrix from the corresponding element in the left matrix + //Subtract each element in the right matrix 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] = newGrid[row][col].subtract(grid[row][col]); + newGrid[row][col] = newGrid[row][col].subtract(rightSide.grid[row][col]); } } //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Subtracts the scalar from every element in the matrix. + * + * @param scalar The scalar to subtract. + * @return A new BigIntegerMatrix instance with the result of the subtraction. + */ public BigIntegerMatrix subtract(BigInteger scalar){ //Create a new grid with the same elements as the current grid BigInteger[][] newGrid = copyGrid(); @@ -496,8 +712,20 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Multiplies the current matrix by the specified matrix. + * + * @param rightSide The matrix to multiply by. + * @return A new BigIntegerMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ public BigIntegerMatrix multiply(BigIntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -520,6 +748,12 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Multiplies every element in the matrix by the scalar. + * + * @param scalar the scalar to multiply + * @return A new BigIntegerMatrix instance with the result of the multiplication + */ public BigIntegerMatrix multiply(BigInteger scalar){ //Create a new grid with the same elements as the current grid BigInteger[][] newGrid = copyGrid(); @@ -534,6 +768,14 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Multiplies the current matrix by itself the given number of times. + * + * @param power The number of times to multiply the matrix by itself + * @return A new BigIntegerMatrix instance with the result of the multiplication + * @throws InvalidScalarException If the power is negative + * @throws InvalidGeometryException If the matrix is not square + */ public BigIntegerMatrix pow(int power){ //Make sure the matrix is square so it can be multiplied if(!isSquare()){ @@ -557,8 +799,21 @@ public class BigIntegerMatrix{ //Return the new grid return newMatrix; } + /** + * Calculates the dot product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public BigInteger dotProduct(BigIntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -577,8 +832,21 @@ public class BigIntegerMatrix{ //Return the sum return sum; } + /** + * Calculates the Hadamard product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public BigIntegerMatrix hadamardProduct(BigIntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -597,7 +865,12 @@ public class BigIntegerMatrix{ return new BigIntegerMatrix(newGrid); } - //Complex operations + //?Complex operations + /** + * Transposes the current matrix (i.e., swaps rows and columns). + * + * @return A new BigIntegerMatrix instance representing the transposed matrix. + */ public BigIntegerMatrix transpose(){ //Create a new grid BigInteger[][] newGrid = new BigInteger[getNumCols()][getNumRows()]; @@ -612,16 +885,41 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #determinant() + */ public BigInteger det(){ return determinant(); } + /** + * Calculates the determinant of a 2x2 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 2x2. + */ private BigInteger det2(){ return (grid[0][0].multiply(grid[1][1])).subtract(grid[0][1].multiply(grid[1][0])); } + /** + * Calculates the determinant of a 3x3 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 3x3. + */ private BigInteger det3(){ return grid[0][0].multiply(grid[1][1]).multiply(grid[2][2]).add(grid[0][1].multiply(grid[1][2]).multiply(grid[2][0])).add(grid[0][2].multiply(grid[1][0]).multiply(grid[2][1])).subtract (grid[2][0].multiply(grid[1][1]).multiply(grid[0][2])).subtract(grid[2][1].multiply(grid[1][2]).multiply(grid[0][0])).subtract(grid[2][2].multiply(grid[1][0]).multiply(grid[0][1])); } + /** + * Calculates the determinant of a 4x4 or larger matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 4x4. + */ private BigInteger det4(){ BigInteger det = BigInteger.ZERO; @@ -691,6 +989,12 @@ public class BigIntegerMatrix{ return det; } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public BigInteger determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -714,9 +1018,22 @@ public class BigIntegerMatrix{ //Return the determinant return det; } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new BigIntegerMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #cofactor() + */ public BigIntegerMatrix cof(){ return cofactor(); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new BigIntegerMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public BigIntegerMatrix cofactor(){ //Make sure the matrix is square if(!isSquare()){ @@ -743,12 +1060,31 @@ public class BigIntegerMatrix{ //Return the new matrix return new BigIntegerMatrix(newGrid); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new BigIntegerMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #adjoint() + */ public BigIntegerMatrix adj(){ return adjoint(); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new BigIntegerMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public BigIntegerMatrix adjoint(){ return cofactor().transpose(); } + /** + * Calculates the inverse of the current matrix. + * + * @return A new BigIntegerMatrix instance representing the inverse matrix. + * @throws InvalidGeometryException If the matrix is not square or if the determinant is 0. + */ public BigIntegerMatrix inverse(){ //Make sure the matrix is square if(!isSquare()){ @@ -765,7 +1101,15 @@ public class BigIntegerMatrix{ return adjoint().multiply(BigInteger.ONE.divide(determinant)); } - //Object funtions + //?Object functions + /** + * Determines whether the given object is equal to the current matrix. + * Can determine equality using BigIntegerMatrix or BigInteger[][]. + * + * @param rightSide The object to compare to the current matrix. + * @return True if the objects are equal, false otherwise. + * @see #equals(BigIntegerMatrix) + */ @Override public boolean equals(Object rightSide){ if(rightSide == null){ @@ -782,7 +1126,17 @@ public class BigIntegerMatrix{ return false; } } + /** + * Determines whether the given BigIntegerMatrix is equal to the current matrix. + * + * @param rightMatrix The BigIntegerMatrix to compare to the current matrix. + * @return True if the matrices are equal, false otherwise. + */ private boolean equals(BigIntegerMatrix rightMatrix){ + if(rightMatrix == null){ + return false; + } + //Make sure they have the same number of elements if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ return false; @@ -800,15 +1154,25 @@ public class BigIntegerMatrix{ //If false hasn't been returned yet then they are equal return true; } + /** + * Calculates a hash code for the current matrix. + * + * @return The hash code for the current matrix. + */ @Override public int hashCode(){ return Arrays.hashCode(grid); } + /** + * Returns a string representation of the matrix, with rows and columns formatted for readability. + * + * @return A string representation of the matrix. + */ @Override public String toString(){ - StringJoiner matrix = new StringJoiner("\n"); + StringJoiner matrix = new StringJoiner("\n").setEmptyValue("[]"); for(int rowCnt = 0;rowCnt < getNumRows();++rowCnt){ - StringJoiner row = new StringJoiner(",", "[", "]"); + StringJoiner row = new StringJoiner(", ", "[", "]"); for(int colCnt = 0;colCnt < getNumCols();++colCnt){ row.add(grid[rowCnt][colCnt].toString()); diff --git a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java index 15b848e..a4b8958 100644 --- a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 06-30-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; @@ -15,11 +15,27 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; +/** + * Represents a matrix of floating point values and provides various matrix operations. + */ public class DoubleMatrix{ + /** + * The grid that represents the matrix + */ protected double[][] grid; + /** + * The difference an element can be and still be considered equals + */ protected double delta; - //Helper functions + //?Helper functions + /** + * Sets the matrix grid to the specified 2D array. Validates the input to ensure + * all rows are of equal length. + * + * @param grid The 2D array to set as the matrix grid. + * @throws InvalidRowSizeException If the rows of the matrix are not all the same length. + */ protected void setGrid(double[][] grid){ if(grid.length == 0){ this.grid = new double[0][0]; @@ -46,18 +62,32 @@ public class DoubleMatrix{ this.grid = newGrid; } } + /** + * Creates a deep copy of the matrix grid. + * + * @return A new DoubleMatrix instance containing the copied grid. + */ protected double[][] copyGrid(){ + if(getNumCols() == 0){ + return new double[grid.length][0]; + } + //Allocate memory for the new grid double[][] newGrid = new double[grid.length][grid[0].length]; //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - newGrid[row] = Arrays.copyOf(grid[row], grid.length); + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } //Return the new grid return newGrid; } + /** + * Checks if the matrix is a square matrix (i.e., the number of rows equals the number of columns). + * + * @return {@code true} if the matrix is square, {@code false} otherwise. + */ public boolean isSquare(){ if(getNumRows() == 0){ return false; @@ -66,7 +96,16 @@ public class DoubleMatrix{ return getNumRows() == getNumCols(); } } - //Returns a matrix with the supplied row and column removed + /** + * Creates a new matrix with the specified row and column removed. + * This matrix is smaller by one row and one column than the current matrix. + * + * @param row The index of the row to remove. + * @param col The index of the column to remove. + * @return A new DoubleMatrix instance with the specified row and column removed. + * @throws InvalidGeometryException If the matrix is not square or too small for Laplace expansion. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ protected DoubleMatrix laplaceExpansionHelper(int row, int col){ //Make sure the matrix is large enough to have this operation performed if((getNumRows() <= 1) || (getNumCols() <= 1)){ @@ -110,19 +149,40 @@ public class DoubleMatrix{ return new DoubleMatrix(newGrid); } - //Constructors + //?Constructors + /** + * Constructs an empty matrix (0x0). + */ public DoubleMatrix(){ grid = new double[0][0]; delta = 0.0; } + /** + * Constructs a matrix with the specified grid. + * + * @param grid The 2D array to initialize the matrix with. + */ public DoubleMatrix(double[][] grid){ setGrid(grid); delta = 0.0; } + /** + * Constructs a copy of the specified matrix. + * + * @param matrix The matrix to copy. + */ public DoubleMatrix(DoubleMatrix matrix){ setGrid(matrix.grid); delta = 0.0; } + /** + * Constructs a matrix with the specified number of rows and columns, filled with the specified value. + * + * @param rows The number of rows. + * @param cols The number of columns. + * @param fill The value to fill the matrix with. + * @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero. + */ public DoubleMatrix(int rows, int cols, double fill){ if(rows <= 0){ throw new InvalidGeometryException("A filled matrix must have at least 1 row"); @@ -141,7 +201,15 @@ public class DoubleMatrix{ } } - //Gets + //?Gets + /** + * Gets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @return The value at the specified row and column. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public double get(int row, int col){ //Make sure the row and column are valid if(row >= grid.length){ @@ -150,7 +218,7 @@ public class DoubleMatrix{ else if(row < 0){ throw new InvalidCoordinatesException("Row cannot be less than 0"); } - else if(col >= grid[0].length){ + else if(col >= grid[row].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } else if(col < 0){ @@ -160,6 +228,13 @@ public class DoubleMatrix{ //Return the location in the grid return grid[row][col]; } + /** + * Returns a new matrix that is a copy of the specified row. + * + * @param row The index of the row to retrieve. + * @return A new DoubleMatrix instance containing the specified row. + * @throws InvalidCoordinatesException If the row index is out of bounds. + */ public DoubleMatrix getRow(int row){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -173,12 +248,24 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newRow); } + /** + * Returns the number of rows in the matrix. + * + * @return The number of rows. + */ public int getNumRows(){ return grid.length; } + /** + * Returns a new matrix that is a copy of the specified column. + * + * @param col The index of the column to retrieve. + * @return A new DoubleMatrix instance containing the specified column. + * @throws InvalidCoordinatesException If the column index is out of bounds. + */ public DoubleMatrix getCol(int col){ //Make sure the column number is valid - if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ + if((col < 0) || (grid.length == 0) || (col >= grid[0].length)){ throw new InvalidCoordinatesException("The column number " + col + " is not valid"); } @@ -191,6 +278,11 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newColumn); } + /** + * Returns the number of columns in the matrix. + * + * @return The number of columns. + */ public int getNumCols(){ if(grid.length > 0){ return grid[0].length; @@ -200,7 +292,15 @@ public class DoubleMatrix{ } } - //Sets + //?Sets + /** + * Sets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @param value The value to set. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public void set(int row, int col, double value){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -214,6 +314,14 @@ public class DoubleMatrix{ //Save the element grid[row][col] = value; } + /** + * Sets the specified row with the given array of elements. + * + * @param row The row index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the row index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void setRow(int row, double[] elements){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -230,6 +338,14 @@ public class DoubleMatrix{ //Save the elements grid[row] = Arrays.copyOf(elements, elements.length); } + /** + * Sets the specified row with the given matrix containing a single row. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void setRow(int row, DoubleMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -243,6 +359,14 @@ public class DoubleMatrix{ //Set the row setRow(row, matrix.grid[0]); } + /** + * Sets the specified column with the given array of elements. + * + * @param col The column index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the column index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void setCol(int col, double[] elements){ //Make sure the column number is valid if((col < 0) || (col >= getNumCols())){ @@ -253,7 +377,7 @@ public class DoubleMatrix{ throw new InvalidGeometryException("Column cannot be null"); } else if(elements.length != grid.length){ - throw new InvalidCoordinatesException(elements.length, grid.length); + throw new InvalidGeometryException(elements.length, grid.length); } //Save the elements @@ -261,6 +385,14 @@ public class DoubleMatrix{ grid[row][col] = elements[row]; } } + /** + * Sets the specified column with the given matrix containing a single column. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void setCol(int col, DoubleMatrix matrix){ //Make sure teh matrix isn't null if(matrix == null){ @@ -280,7 +412,14 @@ public class DoubleMatrix{ setCol(col, vector); } - //Adds + //?Adds + /** + * Adds a new row with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new row. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void addRow(double[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -302,6 +441,13 @@ public class DoubleMatrix{ //Add all elements to the grid grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void addRow(DoubleMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -315,6 +461,13 @@ public class DoubleMatrix{ //Add the row addRow(matrix.grid[0]); } + /** + * Adds a new column with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new column. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void addCol(double[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -338,6 +491,13 @@ public class DoubleMatrix{ throw new InvalidGeometryException(elements.length, getNumCols()); } } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void addCol(DoubleMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -356,6 +516,14 @@ public class DoubleMatrix{ //Add the column addCol(vector); } + /** + * Appends the specified matrix to the right side of the current matrix. + * + * @param rightSide The matrix to append to the right side. + * @return A new DoubleMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ public DoubleMatrix appendRight(DoubleMatrix rightSide){ //Make sure teh matrix isn't null if(rightSide == null){ @@ -381,26 +549,34 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } - public DoubleMatrix appendBottom(DoubleMatrix rightSide){ + /** + * Appends the specified matrix to the bottom of the current matrix. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new DoubleMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ + public DoubleMatrix appendBottom(DoubleMatrix bottomSide){ //Make sure the matrix isn't null - if(rightSide == null){ + if(bottomSide == null){ throw new NullMatrixException(); } //Make sure the matrices have the same number of columns - else if(getNumCols() != rightSide.getNumCols()){ - throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); + else if(getNumCols() != bottomSide.getNumCols()){ + throw new InvalidGeometryException("Invalid number of columns. " + bottomSide.getNumCols() + " must be " + getNumCols()); } //Traverse both matrices and set their values in the new matrix - double[][] newGrid = new double[getNumRows() + rightSide.getNumRows()][getNumCols()]; + double[][] newGrid = new double[getNumRows() + bottomSide.getNumRows()][getNumCols()]; for(int col = 0;col < getNumCols();++col){ //Set all elements from the current grid's column for(int row = 0;row < getNumRows();++row){ newGrid[row][col] = grid[row][col]; } //Set all elements from the right side grid's column - for(int row = 0;row < rightSide.getNumRows();++row){ - newGrid[getNumRows() + row][col] = rightSide.grid[row][col]; + for(int row = 0;row < bottomSide.getNumRows();++row){ + newGrid[getNumRows() + row][col] = bottomSide.grid[row][col]; } } @@ -408,7 +584,14 @@ public class DoubleMatrix{ return new DoubleMatrix(newGrid); } - //Simple operations + //?Simple operations + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @return A new DoubleMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static DoubleMatrix generateIdentity(int size){ //Make sure the size is valid if(size > 0){ @@ -434,8 +617,20 @@ public class DoubleMatrix{ throw new InvalidGeometryException("An identity matrix must have a size > 0"); } } + /** + * Adds the specified matrix to the current matrix. + * + * @param rightSide The matrix to add. + * @return A new DoubleMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public DoubleMatrix add(DoubleMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -443,7 +638,7 @@ public class DoubleMatrix{ //Create a new grid with the same elements as the current grid double[][] newGrid = copyGrid(); - //Add each element in the righ tmatrix to the corresponding element in the left matrix + //Add 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]; @@ -453,6 +648,12 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Adds the scalar to every element in the matrix. + * + * @param scalar The scalar to add. + * @return A new DoubleMatrix instance with the result of the addition. + */ public DoubleMatrix add(double scalar){ //Create a new grid with the same elements as the current grid double[][] newGrid = copyGrid(); @@ -467,8 +668,20 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Subtracts the specified matrix from the current matrix. + * + * @param rightSide The matrix to subtract. + * @return A new DoubleMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public DoubleMatrix subtract(DoubleMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -476,16 +689,22 @@ public class DoubleMatrix{ //Create a new grid with the same elements as the current gird double[][] newGrid = copyGrid(); - //Subtract each element in the righ tmatrix from the corresponding element in the left matrix + //Subtract each element in the right matrix 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]; + newGrid[row][col] -= rightSide.grid[row][col]; } } //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Subtracts the scalar from every element in the matrix. + * + * @param scalar The scalar to subtract. + * @return A new DoubleMatrix instance with the result of the subtraction. + */ public DoubleMatrix subtract(double scalar){ //Create a new grid with the same elements as the current grid double[][] newGrid = copyGrid(); @@ -500,8 +719,20 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Multiplies the current matrix by the specified matrix. + * + * @param rightSide The matrix to multiply by. + * @return A new DoubleMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ public DoubleMatrix multiply(DoubleMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -524,6 +755,12 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Multiplies every element in the matrix by the scalar. + * + * @param scalar the scalar to multiply + * @return A new DoubleMatrix instance with the result of the multiplication + */ public DoubleMatrix multiply(double scalar){ //Create a new grid with the same elements as the current grid double[][] newGrid = copyGrid(); @@ -538,6 +775,14 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Multiplies the current matrix by itself the given number of times. + * + * @param power The number of times to multiply the matrix by itself + * @return A new DoubleMatrix instance with the result of the multiplication + * @throws InvalidScalarException If the power is negative + * @throws InvalidGeometryException If the matrix is not square + */ public DoubleMatrix pow(int power){ //Make sure the matrix is square so it can be multiplied if(!isSquare()){ @@ -561,8 +806,21 @@ public class DoubleMatrix{ //Return the new grid return newMatrix; } + /** + * Calculates the dot product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public double dotProduct(DoubleMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -581,8 +839,21 @@ public class DoubleMatrix{ //Return the sum return sum; } + /** + * Calculates the Hadamard product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public DoubleMatrix hadamardProduct(DoubleMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -601,7 +872,12 @@ public class DoubleMatrix{ return new DoubleMatrix(newGrid); } - //Complex operations + //?Complex operations + /** + * Transposes the current matrix (i.e., swaps rows and columns). + * + * @return A new DoubleMatrix instance representing the transposed matrix. + */ public DoubleMatrix transpose(){ //Create a new grid double[][] newGrid = new double[getNumCols()][getNumRows()]; @@ -616,16 +892,41 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #determinant() + */ public double det(){ return determinant(); } + /** + * Calculates the determinant of a 2x2 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 2x2. + */ private double det2(){ return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); } + /** + * Calculates the determinant of a 3x3 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 3x3. + */ private double det3(){ return (grid[0][0] * grid[1][1] * grid[2][2]) + (grid[0][1] * grid[1][2] * grid[2][0]) + (grid[0][2] * grid[1][0] * grid[2][1]) - (grid[2][0] * grid[1][1] * grid[0][2]) - (grid[2][1] * grid[1][2] * grid[0][0]) - (grid[2][2] * grid[1][0] * grid[0][1]); } + /** + * Calculates the determinant of a 4x4 or larger matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 4x4. + */ private double det4(){ double det = 0; @@ -695,6 +996,12 @@ public class DoubleMatrix{ return det; } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public double determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -718,9 +1025,22 @@ public class DoubleMatrix{ //Return the determinant return det; } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new DoubleMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #cofactor() + */ public DoubleMatrix cof(){ return cofactor(); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new DoubleMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public DoubleMatrix cofactor(){ //Make sure the matrix is square if(!isSquare()){ @@ -747,12 +1067,31 @@ public class DoubleMatrix{ //Return the new matrix return new DoubleMatrix(newGrid); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new DoubleMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #adjoint() + */ public DoubleMatrix adj(){ return adjoint(); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new DoubleMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public DoubleMatrix adjoint(){ return cofactor().transpose(); } + /** + * Calculates the inverse of the current matrix. + * + * @return A new DoubleMatrix instance representing the inverse matrix. + * @throws InvalidGeometryException If the matrix is not square or if the determinant is 0. + */ public DoubleMatrix inverse(){ //Make sure the matrix is square if(!isSquare()){ @@ -769,7 +1108,15 @@ public class DoubleMatrix{ return adjoint().multiply(1 / determinant); } - //Object funtions + //?Object functions + /** + * Determines whether the given object is equal to the current matrix. + * Can determine equality using DoubleMatrix or double[][]. + * + * @param rightSide The object to compare to the current matrix. + * @return True if the objects are equal, false otherwise. + * @see #equals(DoubleMatrix) + */ @Override public boolean equals(Object rightSide){ if(rightSide == null){ @@ -786,7 +1133,17 @@ public class DoubleMatrix{ return false; } } + /** + * Determines whether the given DoubleMatrix is equal to the current matrix. + * + * @param rightMatrix The DoubleMatrix to compare to the current matrix. + * @return True if the matrices are equal, false otherwise. + */ private boolean equals(DoubleMatrix rightMatrix){ + if(rightMatrix == null){ + return false; + } + //Make sure they have the same number of elements if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ return false; @@ -804,22 +1161,46 @@ public class DoubleMatrix{ //If false hasn't been returned yet then they are equal return true; } + /** + * Determines whether the given object is approximately equal to the current matrix, with delta as the allowed difference to be "equal". + * + * @param rightSide The object to compare to the current matrix. + * @param delta The allowed difference for equality. + * @return True if the objects are approximately equal, false otherwise. + * @see #setEqualsDelta(double) + * @see #equals(Object) + */ public boolean equals(Object rightSide, double delta){ setEqualsDelta(delta); return equals(rightSide); } + /** + * Set the difference that is allowed for equality. + * + * @param delta The allowed difference for equality. + */ public void setEqualsDelta(double delta){ this.delta = Math.abs(delta); } + /** + * Calculates a hash code for the current matrix. + * + * @return The hash code for the current matrix. + */ @Override public int hashCode(){ return Arrays.hashCode(grid); } + /** + * Returns a string representation of the matrix, with rows and columns formatted for readability. + * + * @return A string representation of the matrix. + */ @Override public String toString(){ - StringJoiner matrix = new StringJoiner("\n"); + StringJoiner matrix = new StringJoiner("\n").setEmptyValue("[]"); for(int rowCnt = 0;rowCnt < getNumRows();++rowCnt){ - StringJoiner row = new StringJoiner(",", "[", "]"); + StringJoiner row = new StringJoiner(", ", "[", "]"); for(int colCnt = 0;colCnt < getNumCols();++colCnt){ row.add(Double.toString(grid[rowCnt][colCnt])); diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 8558a28..4f88ca5 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java //Mattrixwv // Created: 02-01-22 -//Modified: 06-30-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; @@ -15,10 +15,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; +/** + * Represents a matrix of integers and provides various matrix operations. + */ public class IntegerMatrix{ + /** + * The grid that represents the matrix + */ protected int[][] grid; - //Helper functions + //?Helper functions + /** + * Sets the matrix grid to the specified 2D array. Validates the input to ensure + * all rows are of equal length. + * + * @param grid The 2D array to set as the matrix grid. + * @throws InvalidRowSizeException If the rows of the matrix are not all the same length. + */ protected void setGrid(int[][] grid){ if(grid.length == 0){ this.grid = new int[0][0]; @@ -45,18 +58,32 @@ public class IntegerMatrix{ this.grid = newGrid; } } + /** + * Creates a deep copy of the matrix grid. + * + * @return A new IntegerMatrix instance containing the copied grid. + */ protected int[][] copyGrid(){ + if(getNumCols() == 0){ + return new int[grid.length][0]; + } + //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){ - newGrid[row] = Arrays.copyOf(grid[row], grid.length); + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } //Return the new grid return newGrid; } + /** + * Checks if the matrix is a square matrix (i.e., the number of rows equals the number of columns). + * + * @return {@code true} if the matrix is square, {@code false} otherwise. + */ public boolean isSquare(){ if(getNumRows() == 0){ return false; @@ -65,7 +92,16 @@ public class IntegerMatrix{ return getNumRows() == getNumCols(); } } - //Returns a matrix with the supplied row and column removed + /** + * Creates a new matrix with the specified row and column removed. + * This matrix is smaller by one row and one column than the current matrix. + * + * @param row The index of the row to remove. + * @param col The index of the column to remove. + * @return A new IntegerMatrix instance with the specified row and column removed. + * @throws InvalidGeometryException If the matrix is not square or too small for Laplace expansion. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ protected IntegerMatrix laplaceExpansionHelper(int row, int col){ //Make sure the matrix is large enough to have this operation performed if((getNumRows() <= 1) || (getNumCols() <= 1)){ @@ -109,16 +145,37 @@ public class IntegerMatrix{ return new IntegerMatrix(newGrid); } - //Constructors + //?Constructors + /** + * Constructs an empty matrix (0x0). + */ public IntegerMatrix(){ grid = new int[0][0]; } + /** + * Constructs a matrix with the specified grid. + * + * @param grid The 2D array to initialize the matrix with. + */ public IntegerMatrix(int[][] grid){ setGrid(grid); } + /** + * Constructs a copy of the specified matrix. + * + * @param matrix The matrix to copy. + */ public IntegerMatrix(IntegerMatrix matrix){ setGrid(matrix.grid); } + /** + * Constructs a matrix with the specified number of rows and columns, filled with the specified value. + * + * @param rows The number of rows. + * @param cols The number of columns. + * @param fill The value to fill the matrix with. + * @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero. + */ public IntegerMatrix(int rows, int cols, int fill){ if(rows <= 0){ throw new InvalidGeometryException("A filled matrix must have at least 1 row"); @@ -136,7 +193,15 @@ public class IntegerMatrix{ } } - //Gets + //?Gets + /** + * Gets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @return The value at the specified row and column. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public int get(int row, int col){ //Make sure the row and column are valid if(row >= grid.length){ @@ -145,7 +210,7 @@ public class IntegerMatrix{ else if(row < 0){ throw new InvalidCoordinatesException("Row cannot be less than 0"); } - else if(col >= grid[0].length){ + else if(col >= grid[row].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } else if(col < 0){ @@ -155,6 +220,13 @@ public class IntegerMatrix{ //Return the location in the grid return grid[row][col]; } + /** + * Returns a new matrix that is a copy of the specified row. + * + * @param row The index of the row to retrieve. + * @return A new IntegerMatrix instance containing the specified row. + * @throws InvalidCoordinatesException If the row index is out of bounds. + */ public IntegerMatrix getRow(int row){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -168,12 +240,24 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newRow); } + /** + * Returns the number of rows in the matrix. + * + * @return The number of rows. + */ public int getNumRows(){ return grid.length; } + /** + * Returns a new matrix that is a copy of the specified column. + * + * @param col The index of the column to retrieve. + * @return A new IntegerMatrix instance containing the specified column. + * @throws InvalidCoordinatesException If the column index is out of bounds. + */ public IntegerMatrix getCol(int col){ //Make sure the column number is valid - if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ + if((col < 0) || (grid.length == 0) || (col >= grid[0].length)){ throw new InvalidCoordinatesException("The column number " + col + " is not valid"); } @@ -186,6 +270,11 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newColumn); } + /** + * Returns the number of columns in the matrix. + * + * @return The number of columns. + */ public int getNumCols(){ if(grid.length > 0){ return grid[0].length; @@ -195,7 +284,15 @@ public class IntegerMatrix{ } } - //Sets + //?Sets + /** + * Sets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @param value The value to set. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public void set(int row, int col, int value){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -209,6 +306,14 @@ public class IntegerMatrix{ //Save the element grid[row][col] = value; } + /** + * Sets the specified row with the given array of elements. + * + * @param row The row index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the row index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void setRow(int row, int[] elements){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -225,6 +330,14 @@ public class IntegerMatrix{ //Save the elements grid[row] = Arrays.copyOf(elements, elements.length); } + /** + * Sets the specified row with the given matrix containing a single row. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void setRow(int row, IntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -238,6 +351,14 @@ public class IntegerMatrix{ //Set the row setRow(row, matrix.grid[0]); } + /** + * Sets the specified column with the given array of elements. + * + * @param col The column index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the column index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void setCol(int col, int[] elements){ //Make sure the column number is valid if((col < 0) || (col >= getNumCols())){ @@ -248,7 +369,7 @@ public class IntegerMatrix{ throw new InvalidGeometryException("Column cannot be null"); } else if(elements.length != grid.length){ - throw new InvalidCoordinatesException(elements.length, grid.length); + throw new InvalidGeometryException(elements.length, grid.length); } //Save the elements @@ -256,6 +377,14 @@ public class IntegerMatrix{ grid[row][col] = elements[row]; } } + /** + * Sets the specified column with the given matrix containing a single column. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void setCol(int col, IntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -275,7 +404,14 @@ public class IntegerMatrix{ setCol(col, vector); } - //Adds + //?Adds + /** + * Adds a new row with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new row. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void addRow(int[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -297,6 +433,13 @@ public class IntegerMatrix{ //Add all elements to the grid grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void addRow(IntegerMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -310,6 +453,13 @@ public class IntegerMatrix{ //Add the row addRow(matrix.grid[0]); } + /** + * Adds a new column with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new column. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void addCol(int[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -333,8 +483,15 @@ public class IntegerMatrix{ throw new InvalidGeometryException(elements.length, getNumCols()); } } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void addCol(IntegerMatrix matrix){ - //Make sure teh matrix isn't null + //Make sure the matrix isn't null if(matrix == null){ throw new NullMatrixException(); } @@ -351,6 +508,14 @@ public class IntegerMatrix{ //Add the column addCol(vector); } + /** + * Appends the specified matrix to the right side of the current matrix. + * + * @param rightSide The matrix to append to the right side. + * @return A new IntegerMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ public IntegerMatrix appendRight(IntegerMatrix rightSide){ //Make sure the matrix isn't null if(rightSide == null){ @@ -376,26 +541,34 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } - public IntegerMatrix appendBottom(IntegerMatrix rightSide){ + /** + * Appends the specified matrix to the bottom of the current matrix. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new IntegerMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ + public IntegerMatrix appendBottom(IntegerMatrix bottomSide){ //Make sure the matrix isn't null - if(rightSide == null){ + if(bottomSide == null){ throw new NullMatrixException(); } //Make sure the matrices have the same number of columns - else if(getNumCols() != rightSide.getNumCols()){ - throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); + else if(getNumCols() != bottomSide.getNumCols()){ + throw new InvalidGeometryException("Invalid number of columns. " + bottomSide.getNumCols() + " must be " + getNumCols()); } //Traverse both matrices and set their values in the new matrix - int[][] newGrid = new int[getNumRows() + rightSide.getNumRows()][getNumCols()]; + int[][] newGrid = new int[getNumRows() + bottomSide.getNumRows()][getNumCols()]; for(int col = 0;col < getNumCols();++col){ //Set all elements from the current grid's column for(int row = 0;row < getNumRows();++row){ newGrid[row][col] = grid[row][col]; } //Set all elements from the right side grid's column - for(int row = 0;row < rightSide.getNumRows();++row){ - newGrid[getNumRows() + row][col] = rightSide.grid[row][col]; + for(int row = 0;row < bottomSide.getNumRows();++row){ + newGrid[getNumRows() + row][col] = bottomSide.grid[row][col]; } } @@ -403,7 +576,14 @@ public class IntegerMatrix{ return new IntegerMatrix(newGrid); } - //Simple operations + //?Simple operations + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @return A new IntegerMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static IntegerMatrix generateIdentity(int size){ //Make sure the size is valid if(size > 0){ @@ -429,8 +609,20 @@ public class IntegerMatrix{ throw new InvalidGeometryException("An identity matrix must have a size > 0"); } } + /** + * Adds the specified matrix to the current matrix. + * + * @param rightSide The matrix to add. + * @return A new IntegerMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public IntegerMatrix add(IntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -438,7 +630,7 @@ public class IntegerMatrix{ //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 + //Add 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]; @@ -448,6 +640,12 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Adds the scalar to every element in the matrix. + * + * @param scalar The scalar to add. + * @return A new IntegerMatrix instance with the result of the addition. + */ public IntegerMatrix add(int scalar){ //Create a new grid with the same elements as the current grid int[][] newGrid = copyGrid(); @@ -462,25 +660,43 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Subtracts the specified matrix from the current matrix. + * + * @param rightSide The matrix to subtract. + * @return A new IntegerMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public IntegerMatrix subtract(IntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); + 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 gird int[][] newGrid = copyGrid(); - //Subtract each element in the righ tmatrix from the corresponding element in the left matrix + //Subtract each element in the right matrix 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]; + newGrid[row][col] -= rightSide.grid[row][col]; } } //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Subtracts the scalar from every element in the matrix. + * + * @param scalar The scalar to subtract. + * @return A new IntegerMatrix instance with the result of the subtraction. + */ public IntegerMatrix subtract(int scalar){ //Create a new grid with the same elements as the current grid int[][] newGrid = copyGrid(); @@ -495,8 +711,20 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Multiplies the current matrix by the specified matrix. + * + * @param rightSide The matrix to multiply by. + * @return A new IntegerMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ public IntegerMatrix multiply(IntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -519,6 +747,12 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Multiplies every element in the matrix by the scalar. + * + * @param scalar the scalar to multiply + * @return A new IntegerMatrix instance with the result of the multiplication + */ public IntegerMatrix multiply(int scalar){ //Create a new grid with the same elements as the current grid int[][] newGrid = copyGrid(); @@ -533,6 +767,14 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Multiplies the current matrix by itself the given number of times. + * + * @param power The number of times to multiply the matrix by itself + * @return A new IntegerMatrix instance with the result of the multiplication + * @throws InvalidScalarException If the power is negative + * @throws InvalidGeometryException If the matrix is not square + */ public IntegerMatrix pow(int power){ //Make sure the matrix is square so it can be multiplied if(!isSquare()){ @@ -556,8 +798,21 @@ public class IntegerMatrix{ //Return the new grid return newMatrix; } + /** + * Calculates the dot product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public int dotProduct(IntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -576,8 +831,21 @@ public class IntegerMatrix{ //Return the sum return sum; } + /** + * Calculates the Hadamard product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public IntegerMatrix hadamardProduct(IntegerMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -596,7 +864,12 @@ public class IntegerMatrix{ return new IntegerMatrix(newGrid); } - //Complex operations + //?Complex operations + /** + * Transposes the current matrix (i.e., swaps rows and columns). + * + * @return A new IntegerMatrix instance representing the transposed matrix. + */ public IntegerMatrix transpose(){ //Create a new grid int[][] newGrid = new int[getNumCols()][getNumRows()]; @@ -611,16 +884,41 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #determinant() + */ public int det(){ return determinant(); } + /** + * Calculates the determinant of a 2x2 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 2x2. + */ private int det2(){ return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); } + /** + * Calculates the determinant of a 3x3 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 3x3. + */ private int det3(){ return (grid[0][0] * grid[1][1] * grid[2][2]) + (grid[0][1] * grid[1][2] * grid[2][0]) + (grid[0][2] * grid[1][0] * grid[2][1]) - (grid[2][0] * grid[1][1] * grid[0][2]) - (grid[2][1] * grid[1][2] * grid[0][0]) - (grid[2][2] * grid[1][0] * grid[0][1]); } + /** + * Calculates the determinant of a 4x4 or larger matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 4x4. + */ private int det4(){ int det = 0; @@ -690,6 +988,12 @@ public class IntegerMatrix{ return det; } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public int determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -713,9 +1017,22 @@ public class IntegerMatrix{ //Return the determinant return det; } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new IntegerMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #cofactor() + */ public IntegerMatrix cof(){ return cofactor(); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new IntegerMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public IntegerMatrix cofactor(){ //Make sure the matrix is square if(!isSquare()){ @@ -742,12 +1059,31 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new IntegerMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #adjoint() + */ public IntegerMatrix adj(){ return adjoint(); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new IntegerMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public IntegerMatrix adjoint(){ return cofactor().transpose(); } + /** + * Calculates the inverse of the current matrix. + * + * @return A new IntegerMatrix instance representing the inverse matrix. + * @throws InvalidGeometryException If the matrix is not square or if the determinant is 0. + */ public IntegerMatrix inverse(){ //Make sure the matrix is square if(!isSquare()){ @@ -764,7 +1100,15 @@ public class IntegerMatrix{ return adjoint().multiply(1 / determinant); } - //Object funtions + //?Object functions + /** + * Determines whether the given object is equal to the current matrix. + * Can determine equality using IntegerMatrix or int[][]. + * + * @param rightSide The object to compare to the current matrix. + * @return True if the objects are equal, false otherwise. + * @see #equals(IntegerMatrix) + */ @Override public boolean equals(Object rightSide){ if(rightSide == null){ @@ -781,6 +1125,12 @@ public class IntegerMatrix{ return false; } } + /** + * Determines whether the given IntegerMatrix is equal to the current matrix. + * + * @param rightMatrix The IntegerMatrix to compare to the current matrix. + * @return True if the matrices are equal, false otherwise. + */ private boolean equals(IntegerMatrix rightMatrix){ //Make sure they have the same number of elements if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ @@ -799,15 +1149,25 @@ public class IntegerMatrix{ //If false hasn't been returned yet then they are equal return true; } + /** + * Calculates a hash code for the current matrix. + * + * @return The hash code for the current matrix. + */ @Override public int hashCode(){ return Arrays.hashCode(grid); } + /** + * Returns a string representation of the matrix, with rows and columns formatted for readability. + * + * @return A string representation of the matrix. + */ @Override public String toString(){ - StringJoiner matrix = new StringJoiner("\n"); + StringJoiner matrix = new StringJoiner("\n").setEmptyValue("[]"); for(int rowCnt = 0;rowCnt < getNumRows();++rowCnt){ - StringJoiner row = new StringJoiner(",", "[", "]"); + StringJoiner row = new StringJoiner(", ", "[", "]"); for(int colCnt = 0;colCnt < getNumCols();++colCnt){ row.add(Integer.toString(grid[rowCnt][colCnt])); diff --git a/src/main/java/com/mattrixwv/matrix/LongMatrix.java b/src/main/java/com/mattrixwv/matrix/LongMatrix.java index b215c1a..c6fa784 100644 --- a/src/main/java/com/mattrixwv/matrix/LongMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/LongMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 06-30-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; @@ -15,10 +15,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; +/** + * Represents a matrix of long integers and provides various matrix operations. + */ public class LongMatrix{ + /** + * The grid that represents the matrix + */ protected long[][] grid; - //Helper functions + //?Helper functions + /** + * Sets the matrix grid to the specified 2D array. Validates the input to ensure + * all rows are of equal length. + * + * @param grid The 2D array to set as the matrix grid. + * @throws InvalidRowSizeException If the rows of the matrix are not all the same length. + */ protected void setGrid(long[][] grid){ if(grid.length == 0){ this.grid = new long[0][0]; @@ -45,18 +58,32 @@ public class LongMatrix{ this.grid = newGrid; } } + /** + * Creates a deep copy of the matrix grid. + * + * @return A new LongMatrix instance containing the copied grid. + */ protected long[][] copyGrid(){ + if(getNumCols() == 0){ + return new long[grid.length][0]; + } + //Allocate memory for the new grid long[][] newGrid = new long[grid.length][grid[0].length]; //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - newGrid[row] = Arrays.copyOf(grid[row], grid.length); + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } //Return the new grid return newGrid; } + /** + * Checks if the matrix is a square matrix (i.e., the number of rows equals the number of columns). + * + * @return {@code true} if the matrix is square, {@code false} otherwise. + */ public boolean isSquare(){ if(getNumRows() == 0){ return false; @@ -65,7 +92,16 @@ public class LongMatrix{ return getNumRows() == getNumCols(); } } - //Returns a matrix with the supplied row and column removed + /** + * Creates a new matrix with the specified row and column removed. + * This matrix is smaller by one row and one column than the current matrix. + * + * @param row The index of the row to remove. + * @param col The index of the column to remove. + * @return A new LongMatrix instance with the specified row and column removed. + * @throws InvalidGeometryException If the matrix is not square or too small for Laplace expansion. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ protected LongMatrix laplaceExpansionHelper(int row, int col){ //Make sure the matrix is large enough to have this operation performed if((getNumRows() <= 1) || (getNumCols() <= 1)){ @@ -109,16 +145,37 @@ public class LongMatrix{ return new LongMatrix(newGrid); } - //Constructors + //?Constructors + /** + * Constructs an empty matrix (0x0). + */ public LongMatrix(){ grid = new long[0][0]; } + /** + * Constructs a matrix with the specified grid. + * + * @param grid The 2D array to initialize the matrix with. + */ public LongMatrix(long[][] grid){ setGrid(grid); } + /** + * Constructs a copy of the specified matrix. + * + * @param matrix The matrix to copy. + */ public LongMatrix(LongMatrix matrix){ setGrid(matrix.grid); } + /** + * Constructs a matrix with the specified number of rows and columns, filled with the specified value. + * + * @param rows The number of rows. + * @param cols The number of columns. + * @param fill The value to fill the matrix with. + * @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero. + */ public LongMatrix(int rows, int cols, long fill){ if(rows <= 0){ throw new InvalidGeometryException("A filled matrix must have at least 1 row"); @@ -136,7 +193,15 @@ public class LongMatrix{ } } - //Gets + //?Gets + /** + * Gets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @return The value at the specified row and column. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public long get(int row, int col){ //Make sure the row and column are valid if(row >= grid.length){ @@ -145,7 +210,7 @@ public class LongMatrix{ else if(row < 0){ throw new InvalidCoordinatesException("Row cannot be less than 0"); } - else if(col >= grid[0].length){ + else if(col >= grid[row].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } else if(col < 0){ @@ -155,6 +220,13 @@ public class LongMatrix{ //Return the location in the grid return grid[row][col]; } + /** + * Returns a new matrix that is a copy of the specified row. + * + * @param row The index of the row to retrieve. + * @return A new LongMatrix instance containing the specified row. + * @throws InvalidCoordinatesException If the row index is out of bounds. + */ public LongMatrix getRow(int row){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -168,12 +240,24 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newRow); } + /** + * Returns the number of rows in the matrix. + * + * @return The number of rows. + */ public int getNumRows(){ return grid.length; } + /** + * Returns a new matrix that is a copy of the specified column. + * + * @param col The index of the column to retrieve. + * @return A new LongMatrix instance containing the specified column. + * @throws InvalidCoordinatesException If the column index is out of bounds. + */ public LongMatrix getCol(int col){ //Make sure the column number is valid - if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ + if((col < 0) || (grid.length == 0) || (col >= grid[0].length)){ throw new InvalidCoordinatesException("The column number " + col + " is not valid"); } @@ -186,6 +270,11 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newColumn); } + /** + * Returns the number of columns in the matrix. + * + * @return The number of columns. + */ public int getNumCols(){ if(grid.length > 0){ return grid[0].length; @@ -195,7 +284,15 @@ public class LongMatrix{ } } - //Sets + //?Sets + /** + * Sets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @param value The value to set. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ public void set(int row, int col, long value){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -209,6 +306,14 @@ public class LongMatrix{ //Save the element grid[row][col] = value; } + /** + * Sets the specified row with the given array of elements. + * + * @param row The row index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the row index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void setRow(int row, long[] elements){ //Make sure the row number is valid if((row < 0) || (row >= grid.length)){ @@ -225,6 +330,14 @@ public class LongMatrix{ //Save the elements grid[row] = Arrays.copyOf(elements, elements.length); } + /** + * Sets the specified row with the given matrix containing a single row. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void setRow(int row, LongMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -238,6 +351,14 @@ public class LongMatrix{ //Set the row setRow(row, matrix.grid[0]); } + /** + * Sets the specified column with the given array of elements. + * + * @param col The column index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the column index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void setCol(int col, long[] elements){ //Make sure the column number is valid if((col < 0) || (col >= getNumCols())){ @@ -248,7 +369,7 @@ public class LongMatrix{ throw new InvalidGeometryException("Column cannot be null"); } else if(elements.length != grid.length){ - throw new InvalidCoordinatesException(elements.length, grid.length); + throw new InvalidGeometryException(elements.length, grid.length); } //Save the elements @@ -256,6 +377,14 @@ public class LongMatrix{ grid[row][col] = elements[row]; } } + /** + * Sets the specified column with the given matrix containing a single column. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void setCol(int col, LongMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -275,7 +404,14 @@ public class LongMatrix{ setCol(col, vector); } - //Adds + //?Adds + /** + * Adds a new row with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new row. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ public void addRow(long[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -297,6 +433,13 @@ public class LongMatrix{ //Add all elements to the grid grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void addRow(LongMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -310,6 +453,13 @@ public class LongMatrix{ //Add the row addRow(matrix.grid[0]); } + /** + * Adds a new column with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new column. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ public void addCol(long[] elements){ //Make sure the matrix isn't null if(elements == null){ @@ -333,6 +483,13 @@ public class LongMatrix{ throw new InvalidGeometryException(elements.length, getNumCols()); } } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void addCol(LongMatrix matrix){ //Make sure the matrix isn't null if(matrix == null){ @@ -351,6 +508,14 @@ public class LongMatrix{ //Add the column addCol(vector); } + /** + * Appends the specified matrix to the right side of the current matrix. + * + * @param rightSide The matrix to append to the right side. + * @return A new LongMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ public LongMatrix appendRight(LongMatrix rightSide){ //Make sure the matrix isn't null if(rightSide == null){ @@ -376,26 +541,34 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } - public LongMatrix appendBottom(LongMatrix rightSide){ + /** + * Appends the specified matrix to the bottom of the current matrix. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new LongMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ + public LongMatrix appendBottom(LongMatrix bottomSide){ //Make sure the matrix isn't null - if(rightSide == null){ + if(bottomSide == null){ throw new NullMatrixException(); } //Make sure the matrices have the same number of columns - else if(getNumCols() != rightSide.getNumCols()){ - throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); + else if(getNumCols() != bottomSide.getNumCols()){ + throw new InvalidGeometryException("Invalid number of columns. " + bottomSide.getNumCols() + " must be " + getNumCols()); } //Traverse both matrices and set their values in the new matrix - long[][] newGrid = new long[getNumRows() + rightSide.getNumRows()][getNumCols()]; + long[][] newGrid = new long[getNumRows() + bottomSide.getNumRows()][getNumCols()]; for(int col = 0;col < getNumCols();++col){ //Set all elements from the current grid's column for(int row = 0;row < getNumRows();++row){ newGrid[row][col] = grid[row][col]; } //Set all elements from the right side grid's column - for(int row = 0;row < rightSide.getNumRows();++row){ - newGrid[getNumRows() + row][col] = rightSide.grid[row][col]; + for(int row = 0;row < bottomSide.getNumRows();++row){ + newGrid[getNumRows() + row][col] = bottomSide.grid[row][col]; } } @@ -403,7 +576,14 @@ public class LongMatrix{ return new LongMatrix(newGrid); } - //Simple operations + //?Simple operations + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @return A new LongMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static LongMatrix generateIdentity(int size){ //Make sure the size is valid if(size > 0){ @@ -429,8 +609,20 @@ public class LongMatrix{ throw new InvalidGeometryException("An identity matrix must have a size > 0"); } } + /** + * Adds the specified matrix to the current matrix. + * + * @param rightSide The matrix to add. + * @return A new LongMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public LongMatrix add(LongMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -438,7 +630,7 @@ public class LongMatrix{ //Create a new grid with the same elements as the current grid long[][] newGrid = copyGrid(); - //Add each element in the righ tmatrix to the corresponding element in the left matrix + //Add 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]; @@ -448,6 +640,12 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Adds the scalar to every element in the matrix. + * + * @param scalar The scalar to add. + * @return A new LongMatrix instance with the result of the addition. + */ public LongMatrix add(long scalar){ //Create a new grid with the same elements as the current grid long[][] newGrid = copyGrid(); @@ -462,8 +660,20 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Subtracts the specified matrix from the current matrix. + * + * @param rightSide The matrix to subtract. + * @return A new LongMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public LongMatrix subtract(LongMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -471,16 +681,22 @@ public class LongMatrix{ //Create a new grid with the same elements as the current gird long[][] newGrid = copyGrid(); - //Subtract each element in the righ tmatrix from the corresponding element in the left matrix + //Subtract each element in the right matrix 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]; + newGrid[row][col] -= rightSide.grid[row][col]; } } //Return the new matrix return new LongMatrix(newGrid); } + /** + * Subtracts the scalar from every element in the matrix. + * + * @param scalar The scalar to subtract. + * @return A new LongMatrix instance with the result of the subtraction. + */ public LongMatrix subtract(long scalar){ //Create a new grid with the same elements as the current grid long[][] newGrid = copyGrid(); @@ -495,8 +711,20 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Multiplies the current matrix by the specified matrix. + * + * @param rightSide The matrix to multiply by. + * @return A new LongMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ public LongMatrix multiply(LongMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -519,6 +747,12 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Multiplies every element in the matrix by the scalar. + * + * @param scalar the scalar to multiply + * @return A new LongMatrix instance with the result of the multiplication + */ public LongMatrix multiply(long scalar){ //Create a new grid with the same elements as the current grid long[][] newGrid = copyGrid(); @@ -533,6 +767,14 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Multiplies the current matrix by itself the given number of times. + * + * @param power The number of times to multiply the matrix by itself + * @return A new LongMatrix instance with the result of the multiplication + * @throws InvalidScalarException If the power is negative + * @throws InvalidGeometryException If the matrix is not square + */ public LongMatrix pow(long power){ //Make sure the matrix is square so it can be multiplied if(!isSquare()){ @@ -556,8 +798,21 @@ public class LongMatrix{ //Return the new grid return newMatrix; } + /** + * Calculates the dot product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public long dotProduct(LongMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -576,8 +831,21 @@ public class LongMatrix{ //Return the sum return sum; } + /** + * Calculates the Hadamard product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public LongMatrix hadamardProduct(LongMatrix rightSide){ - //Make sure the matrices have compatable geometry + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } + + //Make sure the matrices have compatible 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()); } @@ -596,7 +864,12 @@ public class LongMatrix{ return new LongMatrix(newGrid); } - //Complex operations + //?Complex operations + /** + * Transposes the current matrix (i.e., swaps rows and columns). + * + * @return A new LongMatrix instance representing the transposed matrix. + */ public LongMatrix transpose(){ //Create a new grid long[][] newGrid = new long[getNumCols()][getNumRows()]; @@ -611,16 +884,41 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #determinant() + */ public long det(){ return determinant(); } + /** + * Calculates the determinant of a 2x2 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 2x2. + */ public long det2(){ return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); } + /** + * Calculates the determinant of a 3x3 matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 3x3. + */ public long det3(){ return (grid[0][0] * grid[1][1] * grid[2][2]) + (grid[0][1] * grid[1][2] * grid[2][0]) + (grid[0][2] * grid[1][0] * grid[2][1]) - (grid[2][0] * grid[1][1] * grid[0][2]) - (grid[2][1] * grid[1][2] * grid[0][0]) - (grid[2][2] * grid[1][0] * grid[0][1]); } + /** + * Calculates the determinant of a 4x4 or larger matrix. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not 4x4. + */ public long det4(){ long det = 0; @@ -690,6 +988,12 @@ public class LongMatrix{ return det; } + /** + * Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used. + * + * @return The determinant of the matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public long determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -713,9 +1017,22 @@ public class LongMatrix{ //Return the determinant return det; } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new LongMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #cofactor() + */ public LongMatrix cof(){ return cofactor(); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new LongMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public LongMatrix cofactor(){ //Make sure the matrix is square if(!isSquare()){ @@ -742,12 +1059,31 @@ public class LongMatrix{ //Return the new matrix return new LongMatrix(newGrid); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new LongMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #adjoint() + */ public LongMatrix adj(){ return adjoint(); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new LongMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ public LongMatrix adjoint(){ return cofactor().transpose(); } + /** + * Calculates the inverse of the current matrix. + * + * @return A new LongMatrix instance representing the inverse matrix. + * @throws InvalidGeometryException If the matrix is not square or if the determinant is 0. + */ public LongMatrix inverse(){ //Make sure the matrix is square if(!isSquare()){ @@ -764,7 +1100,15 @@ public class LongMatrix{ return adjoint().multiply(1 / determinant); } - //Object funtions + //?Object functions + /** + * Determines whether the given object is equal to the current matrix. + * Can determine equality using LongMatrix or long[][]. + * + * @param rightSide The object to compare to the current matrix. + * @return True if the objects are equal, false otherwise. + * @see #equals(LongMatrix) + */ @Override public boolean equals(Object rightSide){ if(rightSide == null){ @@ -781,7 +1125,17 @@ public class LongMatrix{ return false; } } + /** + * Determines whether the given LongMatrix is equal to the current matrix. + * + * @param rightMatrix The LongMatrix to compare to the current matrix. + * @return True if the matrices are equal, false otherwise. + */ private boolean equals(LongMatrix rightMatrix){ + if(rightMatrix == null){ + return false; + } + //Make sure they have the same number of elements if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ return false; @@ -799,15 +1153,25 @@ public class LongMatrix{ //If false hasn't been returned yet then they are equal return true; } + /** + * Calculates a hash code for the current matrix. + * + * @return The hash code for the current matrix. + */ @Override public int hashCode(){ return Arrays.hashCode(grid); } + /** + * Returns a string representation of the matrix, with rows and columns formatted for readability. + * + * @return A string representation of the matrix. + */ @Override public String toString(){ - StringJoiner matrix = new StringJoiner("\n"); + StringJoiner matrix = new StringJoiner("\n").setEmptyValue("[]"); for(int rowCnt = 0;rowCnt < getNumRows();++rowCnt){ - StringJoiner row = new StringJoiner(",", "[", "]"); + StringJoiner row = new StringJoiner(", ", "[", "]"); for(int colCnt = 0;colCnt < getNumCols();++colCnt){ row.add(Long.toString(grid[rowCnt][colCnt])); diff --git a/src/main/java/com/mattrixwv/matrix/ModMatrix.java b/src/main/java/com/mattrixwv/matrix/ModMatrix.java index 9dde910..b723895 100644 --- a/src/main/java/com/mattrixwv/matrix/ModMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/ModMatrix.java @@ -1,20 +1,35 @@ //Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java //Mattrixwv // Created: 02-09-22 -//Modified: 07-01-22 +//Modified: 08-11-24 package com.mattrixwv.matrix; import java.util.Arrays; +import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidScalarException; +import com.mattrixwv.matrix.exceptions.NullMatrixException; +/** + * Represents a matrix of integers that have been run through a modulus function, and provides various matrix operations. + */ public class ModMatrix extends IntegerMatrix{ + /** + * The mod used on each element of the matrix + */ protected int mod; - //Helper functions + //?Helper functions + /** + * Set the mod values + * + * @param mod The new mod value + * @throws InvalidScalarException If the mod value is less than or equal to 0 + */ protected void setMod(int mod){ if(mod <= 0){ throw new InvalidScalarException("The mod must be > 0"); @@ -22,6 +37,12 @@ public class ModMatrix extends IntegerMatrix{ this.mod = mod; } + /** + * Get the mod value of a number + * + * @param value The value to mod + * @return The modded value + */ protected int modValue(int value){ int newValue = value % mod; @@ -31,9 +52,16 @@ public class ModMatrix extends IntegerMatrix{ return newValue; } + /** + * Get the mod value of each element in an array + * + * @param values The array of values + * @return The array of modded values + * @throws NullMatrixException If the array is null + */ protected int[] modValues(int[] values){ if(values == null){ - throw new InvalidGeometryException("Array cannot be null"); + throw new NullMatrixException("Array cannot be null"); } int[] newValues = new int[values.length]; @@ -44,6 +72,9 @@ public class ModMatrix extends IntegerMatrix{ return newValues; } + /** + * Get the mod value of each element in the matrix + */ protected void modGrid(){ for(int row = 0;row < getNumRows();++row){ for(int col = 0;col < getNumCols();++col){ @@ -51,161 +82,440 @@ public class ModMatrix extends IntegerMatrix{ } } } + /** + * Sets the matrix grid to the specified 2D array. Validates the input to ensure + * all rows are of equal length. + * + * @param grid The 2D array to set as the matrix grid. + * @throws InvalidRowSizeException If the rows of the matrix are not all the same length. + */ @Override protected void setGrid(int[][] grid){ super.setGrid(grid); modGrid(); } - //Constructors + //?Constructors + /** + * Constructs an empty matrix (0x0). + * + * @param mod The mod value to use. + */ public ModMatrix(int mod){ super(); setMod(mod); modGrid(); } + /** + * Constructs a matrix with the specified grid. + * + * @param grid The 2D array to initialize the matrix with. + * @param mod The mod value to use. + */ public ModMatrix(int[][] grid, int mod){ super(); setMod(mod); setGrid(grid); } + /** + * Constructs a copy of the specified matrix. + * + * @param matrix The matrix to copy. + */ public ModMatrix(ModMatrix matrix){ super(); setMod(matrix.mod); setGrid(matrix.grid); } + /** + * Constructs a ModMatrix based on the given IntegerMatrix + * + * @param matrix The matrix to copy. + * @param mod The mod value to use. + */ public ModMatrix(IntegerMatrix matrix, int mod){ super(); setMod(mod); setGrid(matrix.grid); } + /** + * Constructs a matrix with the specified number of rows and columns, filled with the specified value. + * + * @param rows The number of rows. + * @param cols The number of columns. + * @param fill The value to fill the matrix with. + * @param mod The mod value to use. + * @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero. + */ public ModMatrix(int rows, int cols, int fill, int mod){ super(rows, cols, fill); setMod(mod); modGrid(); } - //Gets + //?Gets + /** + * Get the mod value of the matrix + * + * @return The mod value + */ public int getMod(){ return mod; } + /** + * Returns a new matrix that is a copy of the specified row. + * + * @param row The index of the row to retrieve. + * @return A new ModMatrix instance containing the specified row. + * @throws InvalidCoordinatesException If the row index is out of bounds. + */ @Override public ModMatrix getRow(int row){ return new ModMatrix(super.getRow(row), mod); } + /** + * Returns the number of rows in the matrix. + * + * @return The number of rows. + */ @Override public ModMatrix getCol(int col){ return new ModMatrix(super.getCol(col), mod); } - //Sets + + //?Sets + /** + * Sets the value at the specified row and column. + * + * @param row The row index. + * @param col The column index. + * @param value The value to set. + * @throws InvalidCoordinatesException If the row or column index is out of bounds. + */ @Override public void set(int row, int col, int value){ super.set(row, col, modValue(value)); } + /** + * Sets the specified row with the given array of elements. + * + * @param row The row index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the row index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ @Override public void setRow(int row, int[] elements){ super.setRow(row, modValues(elements)); } + /** + * Sets the specified row with the given matrix containing a single row. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ @Override public void setRow(int row, IntegerMatrix matrix){ - setRow(row, new ModMatrix(matrix, Integer.MAX_VALUE)); + setRow(row, new ModMatrix(matrix, mod)); } + /** + * Sets the specified row with the given matrix containing a single row. + * + * @param row The row index. + * @param matrix The matrix containing a single row to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void setRow(int row, ModMatrix matrix){ super.setRow(row, matrix); modGrid(); } + /** + * Sets the specified column with the given array of elements. + * + * @param col The column index. + * @param elements The array of elements to set. + * @throws InvalidCoordinatesException If the column index is out of bounds. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ @Override public void setCol(int col, int[] elements){ super.setCol(col, elements); modGrid(); } + /** + * Sets the specified column with the given matrix containing a single column. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ @Override public void setCol(int col, IntegerMatrix matrix){ - setCol(col, new ModMatrix(matrix, Integer.MAX_VALUE)); + setCol(col, new ModMatrix(matrix, mod)); } + /** + * Sets the specified column with the given matrix containing a single column. + * + * @param col The column index. + * @param matrix The matrix containing a single column to set. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void setCol(int col, ModMatrix matrix){ super.setCol(col, matrix); modGrid(); } - //Adds + + //?Adds + /** + * Adds a new row with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new row. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of columns. + */ @Override public void addRow(int[] elements){ super.addRow(modValues(elements)); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ @Override public void addRow(IntegerMatrix matrix){ - addRow(new ModMatrix(matrix, Integer.MAX_VALUE)); + addRow(new ModMatrix(matrix, mod)); } + /** + * Adds a new row with the given matrix containing a single row to the matrix. + * + * @param matrix The matrix containing a single row to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single row. + */ public void addRow(ModMatrix matrix){ super.addRow(matrix); modGrid(); } + /** + * Adds a new column with the specified array of elements to the matrix. + * + * @param elements The array of elements to add as a new column. + * @throws NullMatrixException If the elements array is null. + * @throws InvalidGeometryException If the length of the elements array does not match the number of rows. + */ @Override public void addCol(int[] elements){ super.addCol(modValues(elements)); } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ @Override public void addCol(IntegerMatrix matrix){ - addCol(new ModMatrix(matrix, Integer.MAX_VALUE)); + addCol(new ModMatrix(matrix, mod)); } + /** + * Adds a new column with the given matrix containing a single column to the matrix. + * + * @param matrix The matrix containing a single column to add. + * @throws NullMatrixException If the matrix is null. + * @throws InvalidGeometryException If the matrix does not contain a single column. + */ public void addCol(ModMatrix matrix){ super.addCol(matrix); modGrid(); } + /** + * Appends the specified matrix to the right side of the current matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to append to the right side. + * @return A new ModMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ @Override public ModMatrix appendRight(IntegerMatrix rightSide){ - return appendRight(new ModMatrix(rightSide, Integer.MAX_VALUE)); + return appendRight(new ModMatrix(rightSide, mod)); } + /** + * Appends the specified matrix to the right side of the current matrix. + * + * @param rightSide The matrix to append to the right side. + * @return A new ModMatrix instance with the right-side matrix appended. + * @throws NullMatrixException If the right-side matrix is null. + * @throws InvalidGeometryException If the number of rows does not match. + */ public ModMatrix appendRight(ModMatrix rightSide){ return new ModMatrix(super.appendRight(rightSide), mod); } + /** + * Appends the specified matrix to the bottom of the current matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new ModMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ @Override - public ModMatrix appendBottom(IntegerMatrix rightSide){ - return appendBottom(new ModMatrix(rightSide, Integer.MAX_VALUE)); + public ModMatrix appendBottom(IntegerMatrix bottomSide){ + return appendBottom(new ModMatrix(bottomSide, mod)); } - public ModMatrix appendBottom(ModMatrix rightSide){ - return new ModMatrix(super.appendBottom(rightSide), mod); + /** + * Appends the specified matrix to the bottom of the current matrix. + * + * @param bottomSide The matrix to append to the bottom. + * @return A new ModMatrix instance with the bottom matrix appended. + * @throws NullMatrixException If the bottom matrix is null. + * @throws InvalidGeometryException If the number of columns does not match. + */ + public ModMatrix appendBottom(ModMatrix bottomSide){ + return new ModMatrix(super.appendBottom(bottomSide), mod); } - //Simple operations + //?Simple operations + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @return A new ModMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static ModMatrix generateIdentity(int size){ return generateIdentity(size, Integer.MAX_VALUE); } + /** + * Generates an identity matrix of the given size. + * + * @param size The size of the identity matrix. + * @param mod The mod value to use. + * @return A new ModMatrix instance representing the identity matrix. + * @throws InvalidGeometryException If the size is less than or equal to zero. + */ public static ModMatrix generateIdentity(int size, int mod){ return new ModMatrix(IntegerMatrix.generateIdentity(size), mod); } + /** + * Adds the specified matrix to the current matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to add. + * @return A new ModMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ @Override public ModMatrix add(IntegerMatrix rightSide){ - return add(new ModMatrix(rightSide, Integer.MAX_VALUE)); + return add(new ModMatrix(rightSide, mod)); } + /** + * Adds the specified matrix to the current matrix. + * + * @param rightSide The matrix to add. + * @return A new ModMatrix instance with the result of the addition. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public ModMatrix add(ModMatrix rightSide){ return new ModMatrix(super.add(rightSide), mod); } + /** + * Adds the scalar to every element in the matrix. + * + * @param scalar The scalar to add. + * @return A new ModMatrix instance with the result of the addition. + */ @Override public ModMatrix add(int scalar){ return new ModMatrix(super.add(scalar), mod); } + /** + * Subtracts the specified matrix from the current matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to subtract. + * @return A new ModMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ @Override public ModMatrix subtract(IntegerMatrix rightSide){ - return subtract(new ModMatrix(rightSide, Integer.MAX_VALUE)); + return subtract(new ModMatrix(rightSide, mod)); } + /** + * Subtracts the specified matrix from the current matrix. + * + * @param rightSide The matrix to subtract. + * @return A new ModMatrix instance with the result of the subtraction. + * @throws InvalidGeometryException If the matrices do not have the same dimensions. + */ public ModMatrix subtract(ModMatrix rightSide){ return new ModMatrix(super.subtract(rightSide), mod); } + /** + * Subtracts the scalar from every element in the matrix. + * + * @param scalar The scalar to subtract. + * @return A new ModMatrix instance with the result of the subtraction. + */ @Override public ModMatrix subtract(int scalar){ return new ModMatrix(super.subtract(scalar), mod); } + /** + * Multiplies the current matrix by the specified matrix. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to multiply by. + * @return A new ModMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ @Override - public ModMatrix multiply(IntegerMatrix matrix){ - return multiply(new ModMatrix(matrix, Integer.MAX_VALUE)); + public ModMatrix multiply(IntegerMatrix rightSide){ + return multiply(new ModMatrix(rightSide, mod)); } - public ModMatrix multiply(ModMatrix matrix){ - return new ModMatrix(super.multiply(matrix), mod); + /** + * Multiplies the current matrix by the specified matrix. + * + * @param rightSide The matrix to multiply by. + * @return A new ModMatrix instance with the result of the multiplication. + * @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix. + */ + public ModMatrix multiply(ModMatrix rightSide){ + return new ModMatrix(super.multiply(rightSide), mod); } + /** + * Multiplies every element in the matrix by the scalar. + * + * @param scalar the scalar to multiply + * @return A new ModMatrix instance with the result of the multiplication + */ @Override public ModMatrix multiply(int scalar){ return new ModMatrix(super.multiply(scalar), mod); } + /** + * Multiplies the current matrix by itself the given number of times. + * + * @param power The number of times to multiply the matrix by itself + * @return A new ModMatrix instance with the result of the multiplication + * @throws InvalidScalarException If the power is negative + * @throws InvalidGeometryException If the matrix is not square + */ @Override public ModMatrix pow(int power){ //Make sure the matrix is square so it can be multiplied @@ -230,30 +540,82 @@ public class ModMatrix extends IntegerMatrix{ //Return the new grid return newMatrix; } + /** + * Calculates the dot product of the two matrices. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ @Override public int dotProduct(IntegerMatrix rightSide){ - return dotProduct(new ModMatrix(rightSide, Integer.MAX_VALUE)); + return dotProduct(new ModMatrix(rightSide, mod)); } + /** + * Calculates the dot product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The dot product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public int dotProduct(ModMatrix rightSide){ - return super.dotProduct(rightSide); + return super.dotProduct(rightSide) % mod; } + /** + * Calculates the Hadamard product of the two matrices. + * Converts the IntegerMatrix to a ModMatrix before the operation. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ @Override public ModMatrix hadamardProduct(IntegerMatrix rightSide){ - return hadamardProduct(new ModMatrix(rightSide, Integer.MAX_VALUE)); + return hadamardProduct(new ModMatrix(rightSide, mod)); } + /** + * Calculates the Hadamard product of the two matrices. + * + * @param rightSide The matrix to use on the right side of the calculation + * @return The Hadamard product of the two matrices + * @throws NullMatrixException If the right matrix is null + * @throws InvalidGeometryException If the matrices do not have compatible dimensions + */ public ModMatrix hadamardProduct(ModMatrix rightSide){ return new ModMatrix(super.hadamardProduct(rightSide), mod); } - //Complex operations + //?Complex operations + /** + * Transposes the current matrix (i.e., swaps rows and columns). + * + * @return A new ModMatrix instance representing the transposed matrix. + */ @Override public ModMatrix transpose(){ return new ModMatrix(super.transpose(), mod); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new ModMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #cofactor() + */ @Override public ModMatrix cof(){ return cofactor(); } + /** + * Calculates the cofactor matrix of the current matrix. + * + * @return A new ModMatrix instance representing the cofactor matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ @Override public ModMatrix cofactor(){ //Make sure the matrix is square @@ -281,14 +643,33 @@ public class ModMatrix extends IntegerMatrix{ //Return the new matrix return new ModMatrix(newGrid, mod); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new ModMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + * @see #adjoint() + */ @Override public ModMatrix adj(){ return adjoint(); } + /** + * Calculates the adjoint matrix of the current matrix. + * + * @return A new ModMatrix instance representing the adjoint matrix. + * @throws InvalidGeometryException If the matrix is not square. + */ @Override public ModMatrix adjoint(){ return cofactor().transpose(); } + /** + * Calculates the inverse of the current matrix. + * + * @return A new ModMatrix instance representing the inverse matrix. + * @throws InvalidGeometryException If the matrix is not square or if the determinant is 0. + */ @Override public ModMatrix inverse(){ //Make sure the matrix is square @@ -317,7 +698,15 @@ public class ModMatrix extends IntegerMatrix{ return adjoint().multiply(determinantInverse); } - //Object functions + //?Object functions + /** + * Determines whether the given object is equal to the current matrix. + * Can determine equality using ModMatrix or int[][]. + * + * @param rightSide The object to compare to the current matrix. + * @return True if the objects are equal, false otherwise. + * @see #equals(ModMatrix) + */ @Override public boolean equals(Object rightSide){ if(rightSide == null){ @@ -335,7 +724,17 @@ public class ModMatrix extends IntegerMatrix{ return false; } } + /** + * Determines whether the given ModMatrix is equal to the current matrix. + * + * @param rightMatrix The ModMatrix to compare to the current matrix. + * @return True if the matrices are equal, false otherwise. + */ public boolean equals(ModMatrix rightMatrix){ + if(rightMatrix == null){ + return false; + } + //Make sure they have the same number of elements if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ return false; @@ -353,10 +752,20 @@ public class ModMatrix extends IntegerMatrix{ //If false hasn't been return yet then they are equal return true; } + /** + * Calculates a hash code for the current matrix. + * + * @return The hash code for the current matrix. + */ @Override public int hashCode(){ return Arrays.hashCode(grid); } + /** + * Returns a string representation of the matrix, with rows and columns formatted for readability. + * + * @return A string representation of the matrix. + */ @Override public String toString(){ return super.toString() + "\nmod(" + mod + ")"; diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java index 2f0b3f1..5dc2138 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java @@ -1,25 +1,71 @@ //Matrix/src/main/java/com/mattrixwv/exceptions/InvalidCoordinatesException.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-07-22 +//Modified: 08-08-24 package com.mattrixwv.matrix.exceptions; +/** + * Exception thrown to indicate that a set of coordinates is invalid for a given matrix. + * + * @author Mattrixwv + */ public class InvalidCoordinatesException extends RuntimeException{ + /** + * Serialization identifier for this class. + */ public static final long serialVersionUID = 1; + + /** + * Constructs a new {@code InvalidCoordinatesException} with {@code null} as its detail message. + * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. + */ public InvalidCoordinatesException(){ super(); } + + /** + * Constructs a new {@code InvalidCoordinatesException} with the specified detail message. + * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + */ public InvalidCoordinatesException(String message){ super(message); } + + /** + * Constructs a new {@code InvalidCoordinatesException} with the specified cause and a detail message of + * {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message + * of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions. + * + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidCoordinatesException(Throwable throwable){ super(throwable); } + + /** + * Constructs a new {@code InvalidCoordinatesException} with the specified detail message and cause. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidCoordinatesException(String message, Throwable throwable){ super(message, throwable); } + + /** + * Constructs a new {@code InvalidCoordinatesException} with a default message indicating an invalid number + * of elements provided. The message is constructed using the given number of elements and the maximum + * allowed number of elements. + * + * @param givenElements the number of elements that were provided and deemed invalid. + * @param neededElements the maximum number of elements that was expected or allowed. + */ public InvalidCoordinatesException(int givenElements, int neededElements){ super("Invalid number of elements " + givenElements + " must be at most " + neededElements); } diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java index 37f2f4f..a5daa76 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java @@ -1,25 +1,71 @@ //Matrix/src/main/java/com/mattrixwv/exceptions/InvalidGeometryException.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-07-22 +//Modified: 08-08-24 package com.mattrixwv.matrix.exceptions; +/** + * Exception thrown to indicate that a geometry-related error has occurred. + * + * @author Mattrixwv + */ public class InvalidGeometryException extends RuntimeException{ + /** + * Serialization identifier for this class. + */ public static final long serialVersionUID = 1; + + /** + * Constructs a new {@code InvalidGeometryException} with {@code null} as its detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + */ public InvalidGeometryException(){ super(); } + + /** + * Constructs a new {@code InvalidGeometryException} with the specified detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + */ public InvalidGeometryException(String message){ super(message); } + + /** + * Constructs a new {@code InvalidGeometryException} with the specified cause and a detail message of + * {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message + * of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions. + * + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidGeometryException(Throwable throwable){ super(throwable); } + + /** + * Constructs a new {@code InvalidGeometryException} with the specified detail message and cause. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidGeometryException(String message, Throwable throwable){ super(message, throwable); } + + /** + * Constructs a new {@code InvalidGeometryException} with a default message indicating an invalid number + * of elements provided. The message is constructed using the given number of elements and the expected + * number of elements. + * + * @param givenElements the number of elements that were provided and deemed invalid. + * @param neededElements the expected or required number of elements. + */ public InvalidGeometryException(int givenElements, int neededElements){ super("Invalid number of elements " + givenElements + " must be " + neededElements); } diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java index 9d302ab..e0edd9e 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidRowSizeException.java @@ -1,22 +1,57 @@ //Matrix/src/main/java/com/mattrixwv/exceptions/InvalidRowSizeException.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-07-22 +//Modified: 08-08-24 package com.mattrixwv.matrix.exceptions; +/** + * Exception thrown to indicate that the size of a row is invalid for a given matrix. + */ public class InvalidRowSizeException extends RuntimeException{ + /** + * Serialization identifier for this class. + */ public static final long serialVersionUID = 1; + + /** + * Constructs a new {@code InvalidRowSizeException} with {@code null} as its detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + */ public InvalidRowSizeException(){ super(); } + + /** + * Constructs a new {@code InvalidRowSizeException} with the specified detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + */ public InvalidRowSizeException(String message){ super(message); } + + /** + * Constructs a new {@code InvalidRowSizeException} with the specified cause and a detail message of + * {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message + * of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions. + * + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidRowSizeException(Throwable throwable){ super(throwable); } + + /** + * Constructs a new {@code InvalidRowSizeException} with the specified detail message and cause. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidRowSizeException(String message, Throwable throwable){ super(message, throwable); } diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java index 5dffd15..0e4fbef 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java @@ -1,22 +1,57 @@ //Matrix/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java //Mattrixwv // Created: 02-07-22 -//Modified: 02-07-22 +//Modified: 08-08-24 package com.mattrixwv.matrix.exceptions; +/** + * Exception thrown to indicate that a scalar value passed to a matrix is invalid + */ public class InvalidScalarException extends RuntimeException{ + /** + * Serialization identifier for this class. + */ public static final long serialVersionUID = 1; + + /** + * Constructs a new {@code InvalidScalarException} with {@code null} as its detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + */ public InvalidScalarException(){ super(); } + + /** + * Constructs a new {@code InvalidScalarException} with the specified detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + */ public InvalidScalarException(String message){ super(message); } + + /** + * Constructs a new {@code InvalidScalarException} with the specified cause and a detail message of + * {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message + * of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions. + * + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidScalarException(Throwable throwable){ super(throwable); } + + /** + * Constructs a new {@code InvalidScalarException} with the specified detail message and cause. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public InvalidScalarException(String message, Throwable throwable){ super(message, throwable); } diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java b/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java index b25c6b4..7830881 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java @@ -1,22 +1,57 @@ //Matrix/src/main/java/com/mattrixwv/exceptions/NullMatrixException.java //Mattrixwv // Created: 06-30-22 -//Modified: 06-30-22 +//Modified: 08-08-24 package com.mattrixwv.matrix.exceptions; +/** + * Exception thrown to indicate that a null matrix has been illegally passed to a method + */ public class NullMatrixException extends RuntimeException{ + /** + * Serialization identifier for this class. + */ public static final long serialVersionUID = 1; + + /** + * Constructs a new {@code NullMatrixException} with {@code null} as its detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + */ public NullMatrixException(){ super(); } + + /** + * Constructs a new {@code NullMatrixException} with the specified detail message. + * The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + */ public NullMatrixException(String message){ super(message); } + + /** + * Constructs a new {@code NullMatrixException} with the specified cause and a detail message of + * {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message + * of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions. + * + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public NullMatrixException(Throwable throwable){ super(throwable); } + + /** + * Constructs a new {@code NullMatrixException} with the specified detail message and cause. + * + * @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method. + * @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method). + * (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.) + */ public NullMatrixException(String message, Throwable throwable){ super(message, throwable); } diff --git a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java index 43c833a..59b447a 100644 --- a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java @@ -1,15 +1,11 @@ -//Matrix/src/test/java/com/mattrixwv/matrix/TestBigBigIntegerMatrix.java +//Matrix/src/test/java/com/mattrixwv/matrix/TestBigBigBigIntegerMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 06-30-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.math.BigInteger; import java.util.Arrays; @@ -24,1879 +20,2224 @@ import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestBigIntegerMatrix{ - //Grid 1x1 - private static final BigInteger[][] grid1 = { - {BigInteger.ONE} + private static final BigInteger[][] negativeGrid2 = new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, + {BigInteger.valueOf(-3), BigInteger.valueOf(-4)} }; - private static final BigInteger[][] transformGrid1_1 = { - {BigInteger.ONE} + private static final BigInteger[][] negativeGrid10 = new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-5), BigInteger.valueOf(-6), BigInteger.valueOf(-7), BigInteger.valueOf(-8), BigInteger.valueOf(-9), BigInteger.valueOf(-10)}, + {BigInteger.valueOf(-11), BigInteger.valueOf(-12), BigInteger.valueOf(-13), BigInteger.valueOf(-14), BigInteger.valueOf(-15), BigInteger.valueOf(-16), BigInteger.valueOf(-17), BigInteger.valueOf(-18), BigInteger.valueOf(-19), BigInteger.valueOf(-20)}, + {BigInteger.valueOf(-21), BigInteger.valueOf(-22), BigInteger.valueOf(-23), BigInteger.valueOf(-24), BigInteger.valueOf(-25), BigInteger.valueOf(-26), BigInteger.valueOf(-27), BigInteger.valueOf(-28), BigInteger.valueOf(-29), BigInteger.valueOf(-30)}, + {BigInteger.valueOf(-31), BigInteger.valueOf(-32), BigInteger.valueOf(-33), BigInteger.valueOf(-34), BigInteger.valueOf(-35), BigInteger.valueOf(-36), BigInteger.valueOf(-37), BigInteger.valueOf(-38), BigInteger.valueOf(-39), BigInteger.valueOf(-40)}, + {BigInteger.valueOf(-41), BigInteger.valueOf(-42), BigInteger.valueOf(-43), BigInteger.valueOf(-44), BigInteger.valueOf(-45), BigInteger.valueOf(-46), BigInteger.valueOf(-47), BigInteger.valueOf(-48), BigInteger.valueOf(-49), BigInteger.valueOf(-50)}, + {BigInteger.valueOf(-51), BigInteger.valueOf(-52), BigInteger.valueOf(-53), BigInteger.valueOf(-54), BigInteger.valueOf(-55), BigInteger.valueOf(-56), BigInteger.valueOf(-57), BigInteger.valueOf(-58), BigInteger.valueOf(-59), BigInteger.valueOf(-60)}, + {BigInteger.valueOf(-61), BigInteger.valueOf(-62), BigInteger.valueOf(-63), BigInteger.valueOf(-64), BigInteger.valueOf(-65), BigInteger.valueOf(-66), BigInteger.valueOf(-67), BigInteger.valueOf(-68), BigInteger.valueOf(-69), BigInteger.valueOf(-70)}, + {BigInteger.valueOf(-71), BigInteger.valueOf(-72), BigInteger.valueOf(-73), BigInteger.valueOf(-74), BigInteger.valueOf(-75), BigInteger.valueOf(-76), BigInteger.valueOf(-77), BigInteger.valueOf(-78), BigInteger.valueOf(-79), BigInteger.valueOf(-80)}, + {BigInteger.valueOf(-81), BigInteger.valueOf(-82), BigInteger.valueOf(-83), BigInteger.valueOf(-84), BigInteger.valueOf(-85), BigInteger.valueOf(-86), BigInteger.valueOf(-87), BigInteger.valueOf(-88), BigInteger.valueOf(-89), BigInteger.valueOf(-90)}, + {BigInteger.valueOf(-91), BigInteger.valueOf(-92),BigInteger.valueOf( -93), BigInteger.valueOf(-94), BigInteger.valueOf(-95), BigInteger.valueOf(-96), BigInteger.valueOf(-97), BigInteger.valueOf(-98), BigInteger.valueOf(-99), BigInteger.valueOf(-100)} }; - private static final BigInteger[][] transformGrid1_2 = { - {BigInteger.TWO} + private static final BigInteger[][] negativeGrid2x10 = new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-5), BigInteger.valueOf(-6), BigInteger.valueOf(-7), BigInteger.valueOf(-8), BigInteger.valueOf(-9), BigInteger.valueOf(-10)}, + {BigInteger.valueOf(-11), BigInteger.valueOf(-12), BigInteger.valueOf(-13), BigInteger.valueOf(-14), BigInteger.valueOf(-15), BigInteger.valueOf(-16), BigInteger.valueOf(-17), BigInteger.valueOf(-18), BigInteger.valueOf(-19), BigInteger.valueOf(-20)} + }; + private static final BigInteger[][] negativeGrid10x2 = new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, + {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}, + {BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, + {BigInteger.valueOf(-7), BigInteger.valueOf(-8)}, + {BigInteger.valueOf(-9), BigInteger.valueOf(-10)}, + {BigInteger.valueOf(-11), BigInteger.valueOf(-12)}, + {BigInteger.valueOf(-13), BigInteger.valueOf(-14)}, + {BigInteger.valueOf(-15), BigInteger.valueOf(-16)}, + {BigInteger.valueOf(-17), BigInteger.valueOf(-18)}, + {BigInteger.valueOf(-19), BigInteger.valueOf(-20)} }; - //Grid 2x2 - private static final BigInteger[][] grid2 = { - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO} - }; - private static final BigInteger[][] transformGrid2_1 = { - {BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.ONE, BigInteger.ZERO} - }; - private static final BigInteger[][] transformGrid2_2 = { - {BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.TWO, BigInteger.valueOf(3)} - }; - - //Grid 3x3 - private static final BigInteger[][] grid3 = { - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }; - private static final BigInteger[][] transformGrid3_1 = { - {BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO} - }; - private static final BigInteger[][] transformGrid3_2 = { - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }; - - //Grid 4x4 - private static final BigInteger[][] grid4 = { - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }; - private static final BigInteger[][] transformGrid4_1 = { - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO} - }; - private static final BigInteger[][] transformGrid4_2 = { - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)} - }; - - //Grid 10x10 - private static final BigInteger[][] grid10 = { - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} - }; - private static final BigInteger[][] transformGrid10_1 = { - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE, BigInteger.ZERO} - }; - private static final BigInteger[][] transformGrid10_2 = { - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)} - }; - - + //! Constructor @Test - public void testConstructor(){ - //Default constructor + public void testConstructor_default(){ BigIntegerMatrix matrix = new BigIntegerMatrix(); + assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); - - //Filler constructor - //0 rows - assertThrows(InvalidGeometryException.class, () -> { - new BigIntegerMatrix(0, 0, BigInteger.ZERO); - }); - //0 cols - assertThrows(InvalidGeometryException.class, () -> { - new BigIntegerMatrix(1, 0, BigInteger.ZERO); - }); - //Good values - matrix = new BigIntegerMatrix(2, 2, BigInteger.ZERO); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(BigInteger.ZERO, matrix.get(0, 0)); - assertEquals(BigInteger.ZERO, matrix.get(0, 1)); - assertEquals(BigInteger.ZERO, matrix.get(1, 0)); - assertEquals(BigInteger.ZERO, matrix.get(1, 1)); - - //Matrix constructor - matrix.set(0, 0, BigInteger.ONE); - matrix.set(0, 1, BigInteger.TWO); - matrix.set(1, 0, BigInteger.ONE); - matrix.set(1, 1, BigInteger.TWO); - BigIntegerMatrix matrix2 = new BigIntegerMatrix(matrix); - assertEquals(2, matrix2.getNumRows()); - assertEquals(2, matrix2.getNumCols()); - assertEquals(BigInteger.ONE, matrix2.get(0, 0)); - assertEquals(BigInteger.TWO, matrix2.get(0, 1)); - assertEquals(BigInteger.ONE, matrix2.get(1, 0)); - assertEquals(BigInteger.TWO, matrix2.get(1, 1)); - - //Array constructor - //0 length - BigInteger[][] grid = new BigInteger[0][0]; - matrix = new BigIntegerMatrix(grid); - assertEquals(0, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //0 cols - grid = new BigInteger[1][0]; - matrix = new BigIntegerMatrix(grid); - assertEquals(1, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //Uneven rows - assertThrows(InvalidRowSizeException.class, () -> { - BigInteger[][] grid1 = new BigInteger[2][]; - grid1[0] = new BigInteger[1]; - grid1[0][0] = BigInteger.ZERO; - grid1[1] = new BigInteger[2]; - grid1[1][0] = BigInteger.ONE; - grid1[1][1] = BigInteger.TWO; - new BigIntegerMatrix(grid1); - }); - - //2x2 - grid = grid2; - matrix = new BigIntegerMatrix(grid); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); - assertEquals(BigInteger.TWO, matrix.get(0, 1)); - assertEquals(BigInteger.ONE, matrix.get(1, 0)); - assertEquals(BigInteger.TWO, matrix.get(1, 1)); } @Test - public void testEquals(){ - //Invalid equals - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - assertNotEquals(matrix, null); - assertNotEquals(matrix, new int[0]); + public void testConstructor_fill0Rows(){ + assertThrows(InvalidGeometryException.class, () -> { + new BigIntegerMatrix(0, 1, BigInteger.ZERO); + }); + } - //1x1 - boolean gridEquals = matrix.equals(matrix); - assertTrue(gridEquals); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals1 = matrix.equals(grid1); - assertTrue(gridEquals1); + @Test + public void testConstructor_fill0Cols(){ + assertThrows(InvalidGeometryException.class, () -> { + new BigIntegerMatrix(1, 0, BigInteger.ZERO); + }); + } - //2x2 - matrix = new BigIntegerMatrix(grid2); - boolean gridEquals2 = matrix.equals(matrix); - assertTrue(gridEquals2); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals21 = matrix.equals(grid2); - assertTrue(gridEquals21); - //false - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals22 = matrix.equals(transformGrid2_1); - assertFalse(gridEquals22); - gridEquals2 = matrix.equals(new BigIntegerMatrix(grid3)); - assertFalse(gridEquals2); - gridEquals2 = matrix.equals(new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO} - })); - assertFalse(gridEquals2); + @Test + public void testConstructor_fillSize2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(2, 2, BigInteger.valueOf(-1)); - //3x3 - matrix = new BigIntegerMatrix(grid3); - boolean gridEquals3 = matrix.equals(matrix); - assertTrue(gridEquals3); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals31 = matrix.equals(grid3); - assertTrue(gridEquals31); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(BigInteger.valueOf(-1), matrix.get(0, 0)); + assertEquals(BigInteger.valueOf(-1), matrix.get(0, 1)); + assertEquals(BigInteger.valueOf(-1), matrix.get(1, 0)); + assertEquals(BigInteger.valueOf(-1), matrix.get(1, 1)); + } - //4x4 - matrix = new BigIntegerMatrix(grid4); - boolean gridEquals4 = matrix.equals(matrix); - assertTrue(gridEquals4); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals41 = matrix.equals(grid4); - assertTrue(gridEquals41); + @Test + public void testConstructor_fillSize10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(10, 10, BigInteger.valueOf(-1)); - //10x10 - matrix = new BigIntegerMatrix(grid10); - boolean gridEquals10 = matrix.equals(matrix); - assertTrue(gridEquals10); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals101 = matrix.equals(grid10); - assertTrue(gridEquals101); + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(BigInteger[] row : matrix.copyGrid()){ + for(BigInteger num : row){ + assertEquals(BigInteger.valueOf(-1), num); + } + } + } + + @Test + public void testConstructor_arraySize0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_arraySize2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arrayUnevenRows(){ + BigInteger[][] grid = new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3)}, + {BigInteger.valueOf(-4), BigInteger.valueOf(-5)}, + {BigInteger.valueOf(-6), BigInteger.valueOf(-7), BigInteger.valueOf(-8), BigInteger.valueOf(-9)}, + {BigInteger.valueOf(-10), BigInteger.valueOf(-11), BigInteger.valueOf(-12)} + }; + + assertThrows(InvalidRowSizeException.class, () -> { + new BigIntegerMatrix(grid); + }); + } + + @Test + public void testConstructor_matrixSize0(){ + BigIntegerMatrix originalMatrix = new BigIntegerMatrix(); + BigIntegerMatrix matrix = new BigIntegerMatrix(originalMatrix); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_matrixSize2(){ + BigIntegerMatrix originalMatrix = new BigIntegerMatrix(2, 2, BigInteger.valueOf(-1)); + BigIntegerMatrix matrix = new BigIntegerMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(BigInteger[] row : matrix.copyGrid()){ + for(BigInteger num : row){ + assertEquals(BigInteger.valueOf(-1), num); + } + } + } + + @Test + public void testConstructor_matrixSize10(){ + BigIntegerMatrix originalMatrix = new BigIntegerMatrix(10, 10, BigInteger.valueOf(-1)); + BigIntegerMatrix matrix = new BigIntegerMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(BigInteger[] row : matrix.copyGrid()){ + for(BigInteger num : row){ + assertEquals(BigInteger.valueOf(-1), num); + } + } + } + + @Test + public void testConstructor_matrixSize2x10(){ + BigIntegerMatrix originalMatrix = new BigIntegerMatrix(2, 10, BigInteger.valueOf(-1)); + BigIntegerMatrix matrix = new BigIntegerMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(BigInteger[] row : matrix.copyGrid()){ + for(BigInteger num : row){ + assertEquals(BigInteger.valueOf(-1), num); + } + } + } + + @Test + public void testConstructor_matrixSize10x2(){ + BigIntegerMatrix originalMatrix = new BigIntegerMatrix(10, 2, BigInteger.valueOf(-1)); + BigIntegerMatrix matrix = new BigIntegerMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(BigInteger[] row : matrix.copyGrid()){ + for(BigInteger num : row){ + assertEquals(BigInteger.valueOf(-1), num); + } + } + } + + //! setGrid() + @Test + public void testSetGrid_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(grid); + + assertEquals(2, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.setGrid(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + //! isSquare() + @Test + public void testIsSquare_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertFalse(matrix.isSquare()); + } + + //! laplaceExpansionHelper() + @Test + public void testLaplaceExpansionHelper_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + BigIntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-4)}}, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(-1, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(2, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, -1); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, 2); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc0x0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + BigIntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + BigInteger[][] expectedGrid = new BigInteger[][]{ + {BigInteger.valueOf(-12), BigInteger.valueOf(-13), BigInteger.valueOf(-14), BigInteger.valueOf(-15), BigInteger.valueOf(-16), BigInteger.valueOf(-17), BigInteger.valueOf(-18), BigInteger.valueOf(-19), BigInteger.valueOf(-20)}, + {BigInteger.valueOf(-22), BigInteger.valueOf(-23), BigInteger.valueOf(-24), BigInteger.valueOf(-25), BigInteger.valueOf(-26), BigInteger.valueOf(-27), BigInteger.valueOf(-28), BigInteger.valueOf(-29), BigInteger.valueOf(-30)}, + {BigInteger.valueOf(-32), BigInteger.valueOf(-33), BigInteger.valueOf(-34), BigInteger.valueOf(-35), BigInteger.valueOf(-36), BigInteger.valueOf(-37), BigInteger.valueOf(-38), BigInteger.valueOf(-39), BigInteger.valueOf(-40)}, + {BigInteger.valueOf(-42), BigInteger.valueOf(-43), BigInteger.valueOf(-44), BigInteger.valueOf(-45), BigInteger.valueOf(-46), BigInteger.valueOf(-47), BigInteger.valueOf(-48), BigInteger.valueOf(-49), BigInteger.valueOf(-50)}, + {BigInteger.valueOf(-52), BigInteger.valueOf(-53), BigInteger.valueOf(-54), BigInteger.valueOf(-55), BigInteger.valueOf(-56), BigInteger.valueOf(-57), BigInteger.valueOf(-58), BigInteger.valueOf(-59), BigInteger.valueOf(-60)}, + {BigInteger.valueOf(-62), BigInteger.valueOf(-63), BigInteger.valueOf(-64), BigInteger.valueOf(-65), BigInteger.valueOf(-66), BigInteger.valueOf(-67), BigInteger.valueOf(-68), BigInteger.valueOf(-69), BigInteger.valueOf(-70)}, + {BigInteger.valueOf(-72), BigInteger.valueOf(-73), BigInteger.valueOf(-74), BigInteger.valueOf(-75), BigInteger.valueOf(-76), BigInteger.valueOf(-77), BigInteger.valueOf(-78), BigInteger.valueOf(-79), BigInteger.valueOf(-80)}, + {BigInteger.valueOf(-82), BigInteger.valueOf(-83), BigInteger.valueOf(-84), BigInteger.valueOf(-85), BigInteger.valueOf(-86), BigInteger.valueOf(-87), BigInteger.valueOf(-88), BigInteger.valueOf(-89), BigInteger.valueOf(-90)}, + {BigInteger.valueOf(-92), BigInteger.valueOf(-93), BigInteger.valueOf(-94), BigInteger.valueOf(-95), BigInteger.valueOf(-96), BigInteger.valueOf(-97), BigInteger.valueOf(-98), BigInteger.valueOf(-99), BigInteger.valueOf(-100)} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc4x4(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + BigIntegerMatrix result = matrix.laplaceExpansionHelper(4, 4); + + BigInteger[][] expectedGrid = new BigInteger[][]{ + { BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-6), BigInteger.valueOf(-7), BigInteger.valueOf(-8), BigInteger.valueOf(-9), BigInteger.valueOf(-10)}, + {BigInteger.valueOf(-11), BigInteger.valueOf(-12), BigInteger.valueOf(-13), BigInteger.valueOf(-14), BigInteger.valueOf(-16), BigInteger.valueOf(-17), BigInteger.valueOf(-18), BigInteger.valueOf(-19), BigInteger.valueOf(-20)}, + {BigInteger.valueOf(-21), BigInteger.valueOf(-22), BigInteger.valueOf(-23), BigInteger.valueOf(-24), BigInteger.valueOf(-26), BigInteger.valueOf(-27), BigInteger.valueOf(-28), BigInteger.valueOf(-29), BigInteger.valueOf(-30)}, + {BigInteger.valueOf(-31), BigInteger.valueOf(-32), BigInteger.valueOf(-33), BigInteger.valueOf(-34), BigInteger.valueOf(-36), BigInteger.valueOf(-37), BigInteger.valueOf(-38), BigInteger.valueOf(-39), BigInteger.valueOf(-40)}, + {BigInteger.valueOf(-51), BigInteger.valueOf(-52), BigInteger.valueOf(-53), BigInteger.valueOf(-54), BigInteger.valueOf(-56), BigInteger.valueOf(-57), BigInteger.valueOf(-58), BigInteger.valueOf(-59), BigInteger.valueOf(-60)}, + {BigInteger.valueOf(-61), BigInteger.valueOf(-62), BigInteger.valueOf(-63), BigInteger.valueOf(-64), BigInteger.valueOf(-66), BigInteger.valueOf(-67), BigInteger.valueOf(-68), BigInteger.valueOf(-69), BigInteger.valueOf(-70)}, + {BigInteger.valueOf(-71), BigInteger.valueOf(-72), BigInteger.valueOf(-73), BigInteger.valueOf(-74), BigInteger.valueOf(-76), BigInteger.valueOf(-77), BigInteger.valueOf(-78), BigInteger.valueOf(-79), BigInteger.valueOf(-80)}, + {BigInteger.valueOf(-81), BigInteger.valueOf(-82), BigInteger.valueOf(-83), BigInteger.valueOf(-84), BigInteger.valueOf(-86), BigInteger.valueOf(-87), BigInteger.valueOf(-88), BigInteger.valueOf(-89), BigInteger.valueOf(-90)}, + {BigInteger.valueOf(-91), BigInteger.valueOf(-92), BigInteger.valueOf(-93), BigInteger.valueOf(-94), BigInteger.valueOf(-96), BigInteger.valueOf(-97), BigInteger.valueOf(-98), BigInteger.valueOf(-99), BigInteger.valueOf(-100)} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + //! get() + @Test + public void testGet_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(2, 0); + }); + } + + @Test + public void testGet_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(-1, 0); + }); + } + + @Test + public void testGet_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, 2); + }); + } + + @Test + public void testGet_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, -1); + }); } @Test public void testGet(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals(BigInteger.valueOf(-1), matrix.get(0, 0)); + assertEquals(BigInteger.valueOf(-2), matrix.get(0, 1)); + assertEquals(BigInteger.valueOf(-3), matrix.get(1, 0)); + assertEquals(BigInteger.valueOf(-4), matrix.get(1, 1)); + } + + //! getRow() + @Test + public void testGetRow_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid gets - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(3, 3); + matrix.getRow(2); }); + } + + @Test + public void testGetRow_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(-1, -1); + matrix.getRow(-1); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, -1); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - assertEquals(BigInteger.ONE, matrix.get(0, 0)); } @Test public void testGetRow(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, matrix.getRow(0)); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}}, matrix.getRow(0).copyGrid()); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-3), BigInteger.valueOf(-4)}}, matrix.getRow(1).copyGrid()); + } + + //! getColumn() + @Test + public void testGetCol_0Rows(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); - //Invalid gets - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(-1); + matrix.getCol(0); }); + } + + @Test + public void testGetCol_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(3); + matrix.getCol(2); }); + } - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO}}); - assertEquals(correctMatrix, matrix.getRow(0)); + @Test + public void testGetCol_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //getNumRows - assertEquals(10, matrix.getNumRows()); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(-1); + }); } @Test public void testGetCol(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, matrix.getCol(0)); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid gets - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(-1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(3); - }); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid1); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix2.getCol(3); - }); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1)}, {BigInteger.valueOf(-3)}}, matrix.getCol(0).copyGrid()); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-2)}, {BigInteger.valueOf(-4)}}, matrix.getCol(1).copyGrid()); + } - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + //! getNumRows() + @Test + public void testGetNumRows_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + assertEquals(0, matrix.getNumRows()); + } - //4x4 - matrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + @Test + public void testGetNumRows_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); - //10x10 - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE}, - {BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + } + + //! getNumCols() + @Test + public void testGetNumCols_size0(){ + BigInteger[][] grid = new BigInteger[0][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); - //getNumCols - assertEquals(0, testMatrix.getNumCols()); assertEquals(10, matrix.getNumCols()); } + @Test + public void testGetNumCols_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertEquals(2, matrix.getNumCols()); + } + + //! set() + @Test + public void testSet_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(-1, 0, BigInteger.ZERO); + }); + } + + @Test + public void testSet_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(2, 0, BigInteger.ZERO); + }); + } + + @Test + public void testSet_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, -1, BigInteger.ZERO); + }); + } + + @Test + public void testSet_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, 2, BigInteger.ZERO); + }); + } + @Test public void testSet(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - matrix.set(0, 0, BigInteger.TWO); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.TWO}}); - assertEquals(correctMatrix, matrix); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + matrix.set(0, 0, BigInteger.valueOf(-5)); + matrix.set(0, 1, BigInteger.valueOf(-6)); + matrix.set(1, 0, BigInteger.valueOf(-7)); + matrix.set(1, 1, BigInteger.valueOf(-8)); + assertEquals(BigInteger.valueOf(-5), matrix.get(0, 0)); + assertEquals(BigInteger.valueOf(-6), matrix.get(0, 1)); + assertEquals(BigInteger.valueOf(-7), matrix.get(1, 0)); + assertEquals(BigInteger.valueOf(-8), matrix.get(1, 1)); + } + + //! setRow() + @Test + public void testSetRow_array_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid sets - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(-1, -1, null); + matrix.setRow(-1, new BigInteger[]{BigInteger.ZERO}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(2, 2, null); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, -1, null); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, 2, null); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - matrix.set(0, 0, BigInteger.valueOf(3)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix); - - - //3x3 - matrix = new BigIntegerMatrix(grid3); - matrix.set(0, 0, BigInteger.valueOf(3)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - matrix.set(0, 0, BigInteger.valueOf(3)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - matrix.set(0, 0, BigInteger.valueOf(3)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetRow(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - matrix.setRow(0, new BigInteger[]{BigInteger.ZERO}); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid setRows - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigInteger[] testGrid = {BigInteger.ZERO, BigInteger.ZERO}; - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(-1, testGrid); + matrix.setRow(2, new BigInteger[]{BigInteger.ZERO}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, (BigInteger[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setRow(0, (BigIntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testMatrix2); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - matrix.setRow(1, new BigInteger[]{BigInteger.TWO, BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.TWO, BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix); - //Matrix - BigIntegerMatrix matrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - matrix.setRow(1, matrix2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO }, - {BigInteger.ZERO, BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - matrix.setRow(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(10), BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(10), BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetCol(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - matrix.setCol(0, new BigInteger[]{BigInteger.ONE}); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_nullArray(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid setCols - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(); - final BigInteger[] testGrid = {BigInteger.ZERO, BigInteger.ZERO}; - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(2, testGrid); - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, (BigInteger[])null); + matrix.setRow(0, (BigInteger[])null); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(0, testGrid); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setCol(0, (BigIntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, testMatrix2); - }); - - - //2x2 - matrix = new BigIntegerMatrix(grid2); - matrix.setCol(0, new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO}, - {BigInteger.valueOf(3), BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix); - //Matrix - BigIntegerMatrix vector = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); - matrix.setCol(1, vector); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.ZERO}, - {BigInteger.valueOf(3), BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddRow(){ - //0x0 - BigIntegerMatrix matrix = new BigIntegerMatrix(); - matrix.addRow(new BigInteger[]{BigInteger.ZERO}); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_arrayLength0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //1x1 - matrix = new BigIntegerMatrix(grid1); - matrix.addRow(new BigInteger[]{BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}, {BigInteger.ONE}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO}); + matrix.setRow(0, new BigInteger[0]); }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((BigInteger[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((BigIntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(testMatrix2); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new BigIntegerMatrix(grid2); - matrix.addRow(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ZERO, BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddCol(){ - //0x0 - BigIntegerMatrix matrix = new BigIntegerMatrix(); - matrix.addCol(new BigInteger[]{BigInteger.ZERO}); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_invalidArrayLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //1x1 - matrix = new BigIntegerMatrix(grid1); - matrix.addCol(new BigInteger[]{BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.ONE}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO}); + matrix.setRow(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); }); + } + + @Test + public void testSetRow_array(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}); + matrix.setRow(1, new BigInteger[]{BigInteger.valueOf(-7), BigInteger.valueOf(-8)}); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, {BigInteger.valueOf(-7), BigInteger.valueOf(-8)}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((BigInteger[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((BigIntegerMatrix)null); + matrix.setRow(0, (BigIntegerMatrix)null); }); + } + + @Test + public void testSetRow_matrix_multipleRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(testMatrix2); + matrix.setRow(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); }); + } - //2x2 - matrix = new BigIntegerMatrix(grid2); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new BigIntegerMatrix(grid2); - matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}})); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_negativeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //3x3 - matrix = new BigIntegerMatrix(grid3); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}})); }); - assertEquals(correctMatrix, matrix); + } - //4x4 - matrix = new BigIntegerMatrix(grid4); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_largeRow(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //10x10 - matrix = new BigIntegerMatrix(grid10); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}})); + }); + } + + @Test + public void testSetRow_matrix_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new BigIntegerMatrix(new BigInteger[][]{{}})); + }); + } + + @Test + public void testSetRow_matrix_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testSetRow_matrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + matrix.setRow(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}})); + matrix.setRow(1, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-7), BigInteger.valueOf(-8)}})); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, {BigInteger.valueOf(-7), BigInteger.valueOf(-8)}}, matrix.copyGrid()); + } + + //! setCol() + @Test + public void testSetCol_array_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new BigInteger[]{BigInteger.ZERO}); + }); + } + + @Test + public void testSetCol_array_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new BigInteger[]{BigInteger.ZERO}); + }); + } + + @Test + public void testSetCol_array_nullArray(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, (BigInteger[])null); + }); + } + + @Test + public void testSetCol_array_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new BigInteger[0]); + }); + } + + @Test + public void testSetCol_array_invalidArrayLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); + }); + } + + @Test + public void testSetCol_array(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + matrix.setCol(0, new BigInteger[]{BigInteger.valueOf(-5), BigInteger.valueOf(-7)}); + matrix.setCol(1, new BigInteger[]{BigInteger.valueOf(-6), BigInteger.valueOf(-8)}); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, {BigInteger.valueOf(-7), BigInteger.valueOf(-8)}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.setCol(0, (BigIntegerMatrix)null); + }); + } + + @Test + public void testSetCol_matrix_multipleCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testSetCol_matrix_negativeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}})); + }); + } + + @Test + public void testSetCol_matrix_largeCol(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}})); + }); + } + + @Test + public void testSetCol_matrix_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new BigIntegerMatrix(new BigInteger[][]{{}})); + }); + } + + @Test + public void testSetCol_matrix_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(0), BigInteger.valueOf(0), BigInteger.valueOf(0)}})); + }); + } + + @Test + public void testSetCol_matrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + matrix.setCol(0, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-5)}, {BigInteger.valueOf(-7)}})); + matrix.setCol(1, new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-6)}, {BigInteger.valueOf(-8)}})); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, {BigInteger.valueOf(-7), BigInteger.valueOf(-8)}}, matrix.copyGrid()); + } + + //! addRow() + @Test + public void testAddRow_array_nullArray(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((BigInteger[])null); + }); + } + + @Test + public void testAddRow_array_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new BigInteger[0]); + }); + } + + @Test + public void testAddRow_array_invalidArrayLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); + }); + } + + @Test + public void testAddRow_array(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix.addRow(new BigInteger[]{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-5), BigInteger.valueOf(-6)}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((BigIntegerMatrix)null); + }); + } + + @Test + public void testAddRow_matrix_multipleRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAddRow_matrix_noRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new BigIntegerMatrix(new BigInteger[0][0])); + }); + } + + @Test + public void testAddRow_matrix_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAddRow_matrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix.addRow(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}})); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-5), BigInteger.valueOf(-6)}}, matrix.copyGrid()); + } + + //! addCol() + @Test + public void testAddCol_array_nullArray(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((BigInteger[])null); + }); + } + + @Test + public void testAddCol_array_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new BigInteger[0]); + }); + } + + @Test + public void testAddCol_array_invalidArrayLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); + }); + } + + @Test + public void testAddCol_array(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix.addCol(new BigInteger[]{BigInteger.valueOf(-5), BigInteger.valueOf(-6)}); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-5)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-6)}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((BigIntegerMatrix)null); + }); + } + + @Test + public void testAddCol_matrix_multipleCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAddCol_matrix_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{}})); + }); + } + + @Test + public void testAddCol_matrix_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAddCol_matrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-5)}, {BigInteger.valueOf(-6)}})); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-5)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-6)}}, matrix.copyGrid()); + } + + //! appendRight() + @Test + public void testAppendRight_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendRight((BigIntegerMatrix)null); + }); + } + + @Test + public void testAppendRight_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new BigIntegerMatrix(new BigInteger[0][0])); + }); + } + + @Test + public void testAppendRight_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); }); - assertEquals(correctMatrix, matrix); } @Test public void testAppendRight(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix secondMatrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.ONE}}); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.appendRight(matrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4), BigInteger.valueOf(-3), BigInteger.valueOf(-4)}}, matrix.copyGrid()); + } + + //! appendBottom() + @Test + public void testAppendBottom_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid appends - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendRight(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendRight(null); + matrix.appendBottom((BigIntegerMatrix)null); }); + } - //2x2 - matrix = new BigIntegerMatrix(grid2); - secondMatrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_length0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //3x3 - matrix = new BigIntegerMatrix(grid3); - secondMatrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new BigIntegerMatrix(new BigInteger[0][0])); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + } - //4x4 - matrix = new BigIntegerMatrix(grid4); - secondMatrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_invalidLength(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //10x10 - matrix = new BigIntegerMatrix(grid10); - secondMatrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}, {BigInteger.ZERO}})); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); } @Test public void testAppendBottom(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix secondMatrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.appendBottom(matrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}}, matrix.copyGrid()); + } + + //! add() + @Test + public void testAdd_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid appends - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendBottom(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendBottom(null); + matrix.add((BigIntegerMatrix)null); }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - secondMatrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - secondMatrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - secondMatrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - secondMatrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); } @Test - public void testIsSquare(){ + public void testAdd_matrix_fewRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAdd_matrix_manyRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}})); + }); + } + + @Test + public void testAdd_matrix_fewCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}})); + }); + } + + @Test + public void testAdd_matrix_manyCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testAdd_matrix_size2(){ + BigIntegerMatrix addMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-4), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-2), BigInteger.valueOf(-1)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-5), BigInteger.valueOf(-5)}, {BigInteger.valueOf(-5), BigInteger.valueOf(-5)}}, matrix.copyGrid()); + } + + @Test + public void testAdd_matrix_size10(){ + BigIntegerMatrix addMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.valueOf(-100), BigInteger.valueOf(-99), BigInteger.valueOf(-98), BigInteger.valueOf(-97), BigInteger.valueOf(-96), BigInteger.valueOf(-95), BigInteger.valueOf(-94), BigInteger.valueOf(-93), BigInteger.valueOf(-92), BigInteger.valueOf(-91)}, + { BigInteger.valueOf(-90), BigInteger.valueOf(-89), BigInteger.valueOf(-88), BigInteger.valueOf(-87), BigInteger.valueOf(-86), BigInteger.valueOf(-85), BigInteger.valueOf(-84), BigInteger.valueOf(-83), BigInteger.valueOf(-82), BigInteger.valueOf(-81)}, + { BigInteger.valueOf(-80), BigInteger.valueOf(-79), BigInteger.valueOf(-78), BigInteger.valueOf(-77), BigInteger.valueOf(-76), BigInteger.valueOf(-75), BigInteger.valueOf(-74), BigInteger.valueOf(-73), BigInteger.valueOf(-72), BigInteger.valueOf(-71)}, + { BigInteger.valueOf(-70), BigInteger.valueOf(-69), BigInteger.valueOf(-68), BigInteger.valueOf(-67), BigInteger.valueOf(-66), BigInteger.valueOf(-65), BigInteger.valueOf(-64), BigInteger.valueOf(-63), BigInteger.valueOf(-62), BigInteger.valueOf(-61)}, + { BigInteger.valueOf(-60), BigInteger.valueOf(-59), BigInteger.valueOf(-58), BigInteger.valueOf(-57), BigInteger.valueOf(-56), BigInteger.valueOf(-55), BigInteger.valueOf(-54), BigInteger.valueOf(-53), BigInteger.valueOf(-52), BigInteger.valueOf(-51)}, + { BigInteger.valueOf(-50), BigInteger.valueOf(-49), BigInteger.valueOf(-48), BigInteger.valueOf(-47), BigInteger.valueOf(-46), BigInteger.valueOf(-45), BigInteger.valueOf(-44), BigInteger.valueOf(-43), BigInteger.valueOf(-42), BigInteger.valueOf(-41)}, + { BigInteger.valueOf(-40), BigInteger.valueOf(-39), BigInteger.valueOf(-38), BigInteger.valueOf(-37), BigInteger.valueOf(-36), BigInteger.valueOf(-35), BigInteger.valueOf(-34), BigInteger.valueOf(-33), BigInteger.valueOf(-32), BigInteger.valueOf(-31)}, + { BigInteger.valueOf(-30), BigInteger.valueOf(-29), BigInteger.valueOf(-28), BigInteger.valueOf(-27), BigInteger.valueOf(-26), BigInteger.valueOf(-25), BigInteger.valueOf(-24), BigInteger.valueOf(-23), BigInteger.valueOf(-22), BigInteger.valueOf(-21)}, + { BigInteger.valueOf(-20), BigInteger.valueOf(-19), BigInteger.valueOf(-18), BigInteger.valueOf(-17), BigInteger.valueOf(-16), BigInteger.valueOf(-15), BigInteger.valueOf(-14), BigInteger.valueOf(-13), BigInteger.valueOf(-12), BigInteger.valueOf(-11)}, + { BigInteger.valueOf(-10), BigInteger.valueOf(-9), BigInteger.valueOf(-8), BigInteger.valueOf(-7), BigInteger.valueOf(-6), BigInteger.valueOf(-5), BigInteger.valueOf(-4), BigInteger.valueOf(-3), BigInteger.valueOf(-2), BigInteger.valueOf(-1)} + }); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new BigInteger[][]{ + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)}, + {BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101), BigInteger.valueOf(-101)} + }, matrix.copyGrid()); + } + + @Test + public void testAdd_scalar(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.add(BigInteger.ONE); + + assertArrayEquals(new BigInteger[][]{{BigInteger.ZERO, BigInteger.valueOf(-1)}, {BigInteger.valueOf(-2), BigInteger.valueOf(-3)}}, matrix.copyGrid()); + } + + //! subtract() + @Test + public void testSubtract_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.subtract((BigIntegerMatrix)null); + }); + } + + @Test + public void testSubtract_matrix_fewRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testSubtract_matrix_manyRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testSubtract_matrix_fewCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}})); + }); + } + + @Test + public void testSubtract_matrix_manyCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testSubtract_matrix_size2(){ + BigIntegerMatrix subMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-4), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-2), BigInteger.valueOf(-1)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(3), BigInteger.valueOf(1)}, {BigInteger.valueOf(-1), BigInteger.valueOf(-3)}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_matrix_size10(){ + BigIntegerMatrix subMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.valueOf(-100), BigInteger.valueOf(-99), BigInteger.valueOf(-98), BigInteger.valueOf(-97), BigInteger.valueOf(-96), BigInteger.valueOf(-95), BigInteger.valueOf(-94), BigInteger.valueOf(-93), BigInteger.valueOf(-92), BigInteger.valueOf(-91)}, + { BigInteger.valueOf(-90), BigInteger.valueOf(-89), BigInteger.valueOf(-88), BigInteger.valueOf(-87), BigInteger.valueOf(-86), BigInteger.valueOf(-85), BigInteger.valueOf(-84), BigInteger.valueOf(-83), BigInteger.valueOf(-82), BigInteger.valueOf(-81)}, + { BigInteger.valueOf(-80), BigInteger.valueOf(-79), BigInteger.valueOf(-78), BigInteger.valueOf(-77), BigInteger.valueOf(-76), BigInteger.valueOf(-75), BigInteger.valueOf(-74), BigInteger.valueOf(-73), BigInteger.valueOf(-72), BigInteger.valueOf(-71)}, + { BigInteger.valueOf(-70), BigInteger.valueOf(-69), BigInteger.valueOf(-68), BigInteger.valueOf(-67), BigInteger.valueOf(-66), BigInteger.valueOf(-65), BigInteger.valueOf(-64), BigInteger.valueOf(-63), BigInteger.valueOf(-62), BigInteger.valueOf(-61)}, + { BigInteger.valueOf(-60), BigInteger.valueOf(-59), BigInteger.valueOf(-58), BigInteger.valueOf(-57), BigInteger.valueOf(-56), BigInteger.valueOf(-55), BigInteger.valueOf(-54), BigInteger.valueOf(-53), BigInteger.valueOf(-52), BigInteger.valueOf(-51)}, + { BigInteger.valueOf(-50), BigInteger.valueOf(-49), BigInteger.valueOf(-48), BigInteger.valueOf(-47), BigInteger.valueOf(-46), BigInteger.valueOf(-45), BigInteger.valueOf(-44), BigInteger.valueOf(-43), BigInteger.valueOf(-42), BigInteger.valueOf(-41)}, + { BigInteger.valueOf(-40), BigInteger.valueOf(-39), BigInteger.valueOf(-38), BigInteger.valueOf(-37), BigInteger.valueOf(-36), BigInteger.valueOf(-35), BigInteger.valueOf(-34), BigInteger.valueOf(-33), BigInteger.valueOf(-32), BigInteger.valueOf(-31)}, + { BigInteger.valueOf(-30), BigInteger.valueOf(-29), BigInteger.valueOf(-28), BigInteger.valueOf(-27), BigInteger.valueOf(-26), BigInteger.valueOf(-25), BigInteger.valueOf(-24), BigInteger.valueOf(-23), BigInteger.valueOf(-22), BigInteger.valueOf(-21)}, + { BigInteger.valueOf(-20), BigInteger.valueOf(-19), BigInteger.valueOf(-18), BigInteger.valueOf(-17), BigInteger.valueOf(-16), BigInteger.valueOf(-15), BigInteger.valueOf(-14), BigInteger.valueOf(-13), BigInteger.valueOf(-12), BigInteger.valueOf(-11)}, + { BigInteger.valueOf(-10), BigInteger.valueOf(-9), BigInteger.valueOf(-8), BigInteger.valueOf(-7), BigInteger.valueOf(-6), BigInteger.valueOf(-5), BigInteger.valueOf(-4), BigInteger.valueOf(-3), BigInteger.valueOf(-2), BigInteger.valueOf(-1)} + }); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new BigInteger[][]{ + { BigInteger.valueOf(99), BigInteger.valueOf(97), BigInteger.valueOf(95), BigInteger.valueOf(93), BigInteger.valueOf(91), BigInteger.valueOf(89), BigInteger.valueOf(87), BigInteger.valueOf(85), BigInteger.valueOf(83), BigInteger.valueOf(81)}, + { BigInteger.valueOf(79), BigInteger.valueOf(77), BigInteger.valueOf(75), BigInteger.valueOf(73), BigInteger.valueOf(71), BigInteger.valueOf(69), BigInteger.valueOf(67), BigInteger.valueOf(65), BigInteger.valueOf(63), BigInteger.valueOf(61)}, + { BigInteger.valueOf(59), BigInteger.valueOf(57), BigInteger.valueOf(55), BigInteger.valueOf(53), BigInteger.valueOf(51), BigInteger.valueOf(49), BigInteger.valueOf(47), BigInteger.valueOf(45), BigInteger.valueOf(43), BigInteger.valueOf(41)}, + { BigInteger.valueOf(39), BigInteger.valueOf(37), BigInteger.valueOf(35), BigInteger.valueOf(33), BigInteger.valueOf(31), BigInteger.valueOf(29), BigInteger.valueOf(27), BigInteger.valueOf(25), BigInteger.valueOf(23), BigInteger.valueOf(21)}, + { BigInteger.valueOf(19), BigInteger.valueOf(17), BigInteger.valueOf(15), BigInteger.valueOf(13), BigInteger.valueOf(11), BigInteger.valueOf(9), BigInteger.valueOf(7), BigInteger.valueOf(5), BigInteger.valueOf(3), BigInteger.valueOf(1)}, + { BigInteger.valueOf(-1), BigInteger.valueOf(-3), BigInteger.valueOf(-5), BigInteger.valueOf(-7), BigInteger.valueOf(-9), BigInteger.valueOf(-11), BigInteger.valueOf(-13), BigInteger.valueOf(-15), BigInteger.valueOf(-17), BigInteger.valueOf(-19)}, + {BigInteger.valueOf(-21), BigInteger.valueOf(-23), BigInteger.valueOf(-25), BigInteger.valueOf(-27), BigInteger.valueOf(-29), BigInteger.valueOf(-31), BigInteger.valueOf(-33), BigInteger.valueOf(-35), BigInteger.valueOf(-37), BigInteger.valueOf(-39)}, + {BigInteger.valueOf(-41), BigInteger.valueOf(-43), BigInteger.valueOf(-45), BigInteger.valueOf(-47), BigInteger.valueOf(-49), BigInteger.valueOf(-51), BigInteger.valueOf(-53), BigInteger.valueOf(-55), BigInteger.valueOf(-57), BigInteger.valueOf(-59)}, + {BigInteger.valueOf(-61), BigInteger.valueOf(-63), BigInteger.valueOf(-65), BigInteger.valueOf(-67), BigInteger.valueOf(-69), BigInteger.valueOf(-71), BigInteger.valueOf(-73), BigInteger.valueOf(-75), BigInteger.valueOf(-77), BigInteger.valueOf(-79)}, + {BigInteger.valueOf(-81), BigInteger.valueOf(-83), BigInteger.valueOf(-85), BigInteger.valueOf(-87), BigInteger.valueOf(-89), BigInteger.valueOf(-91), BigInteger.valueOf(-93), BigInteger.valueOf(-95), BigInteger.valueOf(-97), BigInteger.valueOf(-99)} + }, matrix.copyGrid()); + } + + @Test + public void testSubtract_scalar(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.subtract(BigInteger.ONE); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-2), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-4), BigInteger.valueOf(-5)}}, matrix.copyGrid()); + } + + //! multiply() + @Test + public void testMultiply_matrix_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.multiply((BigIntegerMatrix)null); + }); + } + + @Test + public void testMultiply_matrix_manyRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testMultiply_matrix_fewRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testMultiply_matrix_square(){ + BigIntegerMatrix mulMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(1), BigInteger.valueOf(2)}, {BigInteger.valueOf(3), BigInteger.valueOf(4)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-7), BigInteger.valueOf(-10)}, {BigInteger.valueOf(-15), BigInteger.valueOf(-22)}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_matrix_rectangle(){ + BigIntegerMatrix mulMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3)}, {BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-9), BigInteger.valueOf(-12), BigInteger.valueOf(-15)}, {BigInteger.valueOf(-19), BigInteger.valueOf(-26), BigInteger.valueOf(-33)}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_scalar(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + matrix = matrix.multiply(BigInteger.valueOf(2)); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-2), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-6), BigInteger.valueOf(-8)}}, matrix.copyGrid()); + } + + //! pow + @Test + public void testPow_rectangle(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3)}, {BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6)}}); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.pow(2); + }); + } + + @Test + public void testPow_negative(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidScalarException.class, () -> { + matrix.pow(-1); + }); + } + + @Test + public void testPow_0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + BigIntegerMatrix result = matrix.pow(0); + + assertEquals(new BigIntegerMatrix(2, 2, BigInteger.valueOf(1)), result); + } + + @Test + public void testPow_2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + BigIntegerMatrix result = matrix.pow(2); + + assertEquals(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(7), BigInteger.valueOf(10)}, {BigInteger.valueOf(15), BigInteger.valueOf(22)}}), result); + } + + @Test + public void testPow_3(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + BigIntegerMatrix result = matrix.pow(3); + + assertEquals(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-37), BigInteger.valueOf(-54)}, {BigInteger.valueOf(-81), BigInteger.valueOf(-118)}}), result); + } + + //! dotProduct() + @Test + public void testDotProduct_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.dotProduct((BigIntegerMatrix)null); + }); + } + + @Test + public void testDotProduct_fewRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testDotProduct_manyRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testDotProduct_size2(){ + BigIntegerMatrix dotMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(1), BigInteger.valueOf(2)}, {BigInteger.valueOf(3), BigInteger.valueOf(4)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + BigInteger result = matrix.dotProduct(dotMatrix); + + assertEquals(BigInteger.valueOf(-54), result); + } + + @Test + public void testDotProduct_size10(){ + BigIntegerMatrix dotMatrix = new BigIntegerMatrix(new BigInteger[][]{ + { BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, + {BigInteger.valueOf(11), BigInteger.valueOf(12), BigInteger.valueOf(13), BigInteger.valueOf(14), BigInteger.valueOf(15), BigInteger.valueOf(16), BigInteger.valueOf(17), BigInteger.valueOf(18), BigInteger.valueOf(19), BigInteger.valueOf(20)}, + {BigInteger.valueOf(21), BigInteger.valueOf(22), BigInteger.valueOf(23), BigInteger.valueOf(24), BigInteger.valueOf(25), BigInteger.valueOf(26), BigInteger.valueOf(27), BigInteger.valueOf(28), BigInteger.valueOf(29), BigInteger.valueOf(30)}, + {BigInteger.valueOf(31), BigInteger.valueOf(32), BigInteger.valueOf(33), BigInteger.valueOf(34), BigInteger.valueOf(35), BigInteger.valueOf(36), BigInteger.valueOf(37), BigInteger.valueOf(38), BigInteger.valueOf(39), BigInteger.valueOf(40)}, + {BigInteger.valueOf(41), BigInteger.valueOf(42), BigInteger.valueOf(43), BigInteger.valueOf(44), BigInteger.valueOf(45), BigInteger.valueOf(46), BigInteger.valueOf(47), BigInteger.valueOf(48), BigInteger.valueOf(49), BigInteger.valueOf(50)}, + {BigInteger.valueOf(51), BigInteger.valueOf(52), BigInteger.valueOf(53), BigInteger.valueOf(54), BigInteger.valueOf(55), BigInteger.valueOf(56), BigInteger.valueOf(57), BigInteger.valueOf(58), BigInteger.valueOf(59), BigInteger.valueOf(60)}, + {BigInteger.valueOf(61), BigInteger.valueOf(62), BigInteger.valueOf(63), BigInteger.valueOf(64), BigInteger.valueOf(65), BigInteger.valueOf(66), BigInteger.valueOf(67), BigInteger.valueOf(68), BigInteger.valueOf(69), BigInteger.valueOf(70)}, + {BigInteger.valueOf(71), BigInteger.valueOf(72), BigInteger.valueOf(73), BigInteger.valueOf(74), BigInteger.valueOf(75), BigInteger.valueOf(76), BigInteger.valueOf(77), BigInteger.valueOf(78), BigInteger.valueOf(79), BigInteger.valueOf(80)}, + {BigInteger.valueOf(81), BigInteger.valueOf(82), BigInteger.valueOf(83), BigInteger.valueOf(84), BigInteger.valueOf(85), BigInteger.valueOf(86), BigInteger.valueOf(87), BigInteger.valueOf(88), BigInteger.valueOf(89), BigInteger.valueOf(90)}, + {BigInteger.valueOf(91), BigInteger.valueOf(92), BigInteger.valueOf(93), BigInteger.valueOf(94), BigInteger.valueOf(95), BigInteger.valueOf(96), BigInteger.valueOf(97), BigInteger.valueOf(98), BigInteger.valueOf(99), BigInteger.valueOf(100)} + }); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + BigInteger result = matrix.dotProduct(dotMatrix); + + assertEquals(BigInteger.valueOf(-2632750), result); + } + + //! hadamardProduct() + @Test + public void testHadamardProduct_nullMatrix(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.hadamardProduct((BigIntegerMatrix)null); + }); + } + + @Test + public void testHadamardProduct_fewRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testHadamardProduct_manyRows(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testHadamardProduct_fewCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}})); + }); + } + + @Test + public void testHadamardProduct_manyCols(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}})); + }); + } + + @Test + public void testHadamardProduct_size2(){ + BigIntegerMatrix hadMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(1), BigInteger.valueOf(2)}, {BigInteger.valueOf(3), BigInteger.valueOf(4)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + BigIntegerMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-9), BigInteger.valueOf(-16)}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_size10(){ + BigIntegerMatrix hadMatrix = new BigIntegerMatrix(new BigInteger[][]{ + { BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, + {BigInteger.valueOf(11), BigInteger.valueOf(12), BigInteger.valueOf(13), BigInteger.valueOf(14), BigInteger.valueOf(15), BigInteger.valueOf(16), BigInteger.valueOf(17), BigInteger.valueOf(18), BigInteger.valueOf(19), BigInteger.valueOf(20)}, + {BigInteger.valueOf(21), BigInteger.valueOf(22), BigInteger.valueOf(23), BigInteger.valueOf(24), BigInteger.valueOf(25), BigInteger.valueOf(26), BigInteger.valueOf(27), BigInteger.valueOf(28), BigInteger.valueOf(29), BigInteger.valueOf(30)}, + {BigInteger.valueOf(31), BigInteger.valueOf(32), BigInteger.valueOf(33), BigInteger.valueOf(34), BigInteger.valueOf(35), BigInteger.valueOf(36), BigInteger.valueOf(37), BigInteger.valueOf(38), BigInteger.valueOf(39), BigInteger.valueOf(40)}, + {BigInteger.valueOf(41), BigInteger.valueOf(42), BigInteger.valueOf(43), BigInteger.valueOf(44), BigInteger.valueOf(45), BigInteger.valueOf(46), BigInteger.valueOf(47), BigInteger.valueOf(48), BigInteger.valueOf(49), BigInteger.valueOf(50)}, + {BigInteger.valueOf(51), BigInteger.valueOf(52), BigInteger.valueOf(53), BigInteger.valueOf(54), BigInteger.valueOf(55), BigInteger.valueOf(56), BigInteger.valueOf(57), BigInteger.valueOf(58), BigInteger.valueOf(59), BigInteger.valueOf(60)}, + {BigInteger.valueOf(61), BigInteger.valueOf(62), BigInteger.valueOf(63), BigInteger.valueOf(64), BigInteger.valueOf(65), BigInteger.valueOf(66), BigInteger.valueOf(67), BigInteger.valueOf(68), BigInteger.valueOf(69), BigInteger.valueOf(70)}, + {BigInteger.valueOf(71), BigInteger.valueOf(72), BigInteger.valueOf(73), BigInteger.valueOf(74), BigInteger.valueOf(75), BigInteger.valueOf(76), BigInteger.valueOf(77), BigInteger.valueOf(78), BigInteger.valueOf(79), BigInteger.valueOf(80)}, + {BigInteger.valueOf(81), BigInteger.valueOf(82), BigInteger.valueOf(83), BigInteger.valueOf(84), BigInteger.valueOf(85), BigInteger.valueOf(86), BigInteger.valueOf(87), BigInteger.valueOf(88), BigInteger.valueOf(89), BigInteger.valueOf(90)}, + {BigInteger.valueOf(91), BigInteger.valueOf(92), BigInteger.valueOf(93), BigInteger.valueOf(94), BigInteger.valueOf(95), BigInteger.valueOf(96), BigInteger.valueOf(97), BigInteger.valueOf(98), BigInteger.valueOf(99), BigInteger.valueOf(100)} + }); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + BigIntegerMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new BigInteger[][]{ + { BigInteger.valueOf(-1), BigInteger.valueOf(-4), BigInteger.valueOf(-9), BigInteger.valueOf(-16), BigInteger.valueOf(-25), BigInteger.valueOf(-36), BigInteger.valueOf(-49), BigInteger.valueOf(-64), BigInteger.valueOf(-81), BigInteger.valueOf(-100)}, + { BigInteger.valueOf(-121), BigInteger.valueOf(-144), BigInteger.valueOf(-169), BigInteger.valueOf(-196), BigInteger.valueOf(-225), BigInteger.valueOf(-256), BigInteger.valueOf(-289), BigInteger.valueOf(-324), BigInteger.valueOf(-361), BigInteger.valueOf(-400)}, + { BigInteger.valueOf(-441), BigInteger.valueOf(-484), BigInteger.valueOf(-529), BigInteger.valueOf(-576), BigInteger.valueOf(-625), BigInteger.valueOf(-676), BigInteger.valueOf(-729), BigInteger.valueOf(-784), BigInteger.valueOf(-841), BigInteger.valueOf(-900)}, + { BigInteger.valueOf(-961), BigInteger.valueOf(-1024), BigInteger.valueOf(-1089), BigInteger.valueOf(-1156), BigInteger.valueOf(-1225), BigInteger.valueOf(-1296), BigInteger.valueOf(-1369), BigInteger.valueOf(-1444), BigInteger.valueOf(-1521), BigInteger.valueOf(-1600)}, + {BigInteger.valueOf(-1681), BigInteger.valueOf(-1764), BigInteger.valueOf(-1849), BigInteger.valueOf(-1936), BigInteger.valueOf(-2025), BigInteger.valueOf(-2116), BigInteger.valueOf(-2209), BigInteger.valueOf(-2304), BigInteger.valueOf(-2401), BigInteger.valueOf(-2500)}, + {BigInteger.valueOf(-2601), BigInteger.valueOf(-2704), BigInteger.valueOf(-2809), BigInteger.valueOf(-2916), BigInteger.valueOf(-3025), BigInteger.valueOf(-3136), BigInteger.valueOf(-3249), BigInteger.valueOf(-3364), BigInteger.valueOf(-3481), BigInteger.valueOf(-3600)}, + {BigInteger.valueOf(-3721), BigInteger.valueOf(-3844), BigInteger.valueOf(-3969), BigInteger.valueOf(-4096), BigInteger.valueOf(-4225), BigInteger.valueOf(-4356), BigInteger.valueOf(-4489), BigInteger.valueOf(-4624), BigInteger.valueOf(-4761), BigInteger.valueOf(-4900)}, + {BigInteger.valueOf(-5041), BigInteger.valueOf(-5184), BigInteger.valueOf(-5329), BigInteger.valueOf(-5476), BigInteger.valueOf(-5625), BigInteger.valueOf(-5776), BigInteger.valueOf(-5929), BigInteger.valueOf(-6084), BigInteger.valueOf(-6241), BigInteger.valueOf(-6400)}, + {BigInteger.valueOf(-6561), BigInteger.valueOf(-6724), BigInteger.valueOf(-6889), BigInteger.valueOf(-7056), BigInteger.valueOf(-7225), BigInteger.valueOf(-7396), BigInteger.valueOf(-7569), BigInteger.valueOf(-7744), BigInteger.valueOf(-7921), BigInteger.valueOf(-8100)}, + {BigInteger.valueOf(-8281), BigInteger.valueOf(-8464), BigInteger.valueOf(-8649), BigInteger.valueOf(-8836), BigInteger.valueOf(-9025), BigInteger.valueOf(-9216), BigInteger.valueOf(-9409), BigInteger.valueOf(-9604), BigInteger.valueOf(-9801), BigInteger.valueOf(-10000)} + }, result.copyGrid()); + } + + //! transpose() + @Test + public void testTranspose_size0(){ BigIntegerMatrix matrix = new BigIntegerMatrix(); - assertFalse(matrix.isSquare()); - matrix = new BigIntegerMatrix(2, 2, BigInteger.ZERO); - assertTrue(matrix.isSquare()); + BigIntegerMatrix result = matrix.transpose(); - matrix = new BigIntegerMatrix(2, 3, BigInteger.ZERO); - assertFalse(matrix.isSquare()); + assertEquals(new BigIntegerMatrix(), result); } @Test - public void testAddition(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix transformMatrix = new BigIntegerMatrix(transformGrid1_1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.TWO}}); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - assertEquals(correctMatrix, matrix.add(BigInteger.ONE)); + public void testTranspose_size0x2(){ + BigInteger[][] grid = new BigInteger[0][2]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); - //Invalid adds - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); - final BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix3); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - transformMatrix = new BigIntegerMatrix(transformGrid2_1); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.TWO}, - {BigInteger.TWO, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix.add(BigInteger.ONE)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - transformMatrix = new BigIntegerMatrix(transformGrid3_1); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)}, - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)}, - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.add(BigInteger.ONE)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - transformMatrix = new BigIntegerMatrix(transformGrid4_1); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)} - }); - assertEquals(correctMatrix, matrix.add(BigInteger.ONE)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - transformMatrix = new BigIntegerMatrix(transformGrid10_1); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11)} - }); - assertEquals(correctMatrix, matrix.add(BigInteger.ONE)); + assertArrayEquals(new BigInteger[0][0], matrix.transpose().copyGrid()); } @Test - public void testSubtraction(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix transformMatrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - assertEquals(correctMatrix, matrix.subtract(BigInteger.ONE)); + public void testTranspose_size2x0(){ + BigInteger[][] grid = new BigInteger[2][0]; + BigIntegerMatrix matrix = new BigIntegerMatrix(grid); - //Invalid subtracts - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); - final BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix3); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - transformMatrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(2, 2, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE}, - {BigInteger.ZERO, BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.subtract(BigInteger.ONE)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - transformMatrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(3, 3, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix.subtract(BigInteger.ONE)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - transformMatrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(4, 4, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix.subtract(BigInteger.ONE)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - transformMatrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(10, 10, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9)} - }); - assertEquals(correctMatrix, matrix.subtract(BigInteger.ONE)); + assertArrayEquals(new BigInteger[0][0], matrix.transpose().copyGrid()); } @Test - public void testMultiplication(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix transformMatrix = new BigIntegerMatrix(transformGrid1_2); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(transformGrid1_2); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - assertEquals(correctMatrix, matrix.multiply(BigInteger.TWO)); + public void testTranspose_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid multiplication - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.multiply(testMatrix2); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - transformMatrix = new BigIntegerMatrix(transformGrid2_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(6), BigInteger.valueOf(9)}, - {BigInteger.valueOf(6), BigInteger.valueOf(9)} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - BigIntegerMatrix vector = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO}, - {BigInteger.valueOf(3)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(8)}, - {BigInteger.valueOf(8)} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.multiply(BigInteger.TWO)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - transformMatrix = new BigIntegerMatrix(transformGrid3_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(12), BigInteger.valueOf(18), BigInteger.valueOf(24)}, - {BigInteger.valueOf(12), BigInteger.valueOf(18), BigInteger.valueOf(24)}, - {BigInteger.valueOf(12), BigInteger.valueOf(18), BigInteger.valueOf(24)} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO}, - {BigInteger.valueOf(3)}, - {BigInteger.valueOf(4)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(20)}, - {BigInteger.valueOf(20)}, - {BigInteger.valueOf(20)} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6)} - }); - assertEquals(correctMatrix, matrix.multiply(BigInteger.TWO)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - transformMatrix = new BigIntegerMatrix(transformGrid4_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(40), BigInteger.valueOf(50)}, - {BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(40), BigInteger.valueOf(50)}, - {BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(40), BigInteger.valueOf(50)}, - {BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(40), BigInteger.valueOf(50)}, - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO}, - {BigInteger.valueOf(3)}, - {BigInteger.valueOf(4)}, - {BigInteger.valueOf(5)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(40)}, - {BigInteger.valueOf(40)}, - {BigInteger.valueOf(40)}, - {BigInteger.valueOf(40)} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8)} - }); - assertEquals(correctMatrix, matrix.multiply(BigInteger.TWO)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - transformMatrix = new BigIntegerMatrix(transformGrid10_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)}, - {BigInteger.valueOf(110), BigInteger.valueOf(165), BigInteger.valueOf(220), BigInteger.valueOf(275), BigInteger.valueOf(330), BigInteger.valueOf(385), BigInteger.valueOf(440), BigInteger.valueOf(495), BigInteger.valueOf(550), BigInteger.valueOf(605)} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO}, - {BigInteger.valueOf(3)}, - {BigInteger.valueOf(4)}, - {BigInteger.valueOf(5)}, - {BigInteger.valueOf(6)}, - {BigInteger.valueOf(7)}, - {BigInteger.valueOf(8)}, - {BigInteger.valueOf(9)}, - {BigInteger.valueOf(10)}, - {BigInteger.valueOf(11)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)}, - {BigInteger.valueOf(440)} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.valueOf(6), BigInteger.valueOf(8), BigInteger.valueOf(10), BigInteger.valueOf(12), BigInteger.valueOf(14), BigInteger.valueOf(16), BigInteger.valueOf(18), BigInteger.valueOf(20)} - }); - assertEquals(correctMatrix, matrix.multiply(BigInteger.TWO)); + assertArrayEquals(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-2), BigInteger.valueOf(-4)}}, matrix.transpose().copyGrid()); } @Test - public void testDotProduct(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix transformMatrix = new BigIntegerMatrix(transformGrid1_2); - assertEquals(BigInteger.TWO, matrix.dotProduct(transformMatrix)); + public void testTranspose_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); - //Invalid products - BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.dotProduct(testMatrix2); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - transformMatrix = new BigIntegerMatrix(transformGrid2_2); - assertEquals(BigInteger.valueOf(30), matrix.dotProduct(transformMatrix)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - transformMatrix = new BigIntegerMatrix(transformGrid3_2); - assertEquals(BigInteger.valueOf(162), matrix.dotProduct(transformMatrix)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - transformMatrix = new BigIntegerMatrix(transformGrid4_2); - assertEquals(BigInteger.valueOf(560), matrix.dotProduct(transformMatrix)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - transformMatrix = new BigIntegerMatrix(transformGrid10_2); - assertEquals(BigInteger.valueOf(35750), matrix.dotProduct(transformMatrix)); + assertArrayEquals(new BigInteger[][]{ + { BigInteger.valueOf(-1), BigInteger.valueOf(-11), BigInteger.valueOf(-21), BigInteger.valueOf(-31), BigInteger.valueOf(-41), BigInteger.valueOf(-51), BigInteger.valueOf(-61), BigInteger.valueOf(-71), BigInteger.valueOf(-81), BigInteger.valueOf(-91)}, + { BigInteger.valueOf(-2), BigInteger.valueOf(-12), BigInteger.valueOf(-22), BigInteger.valueOf(-32), BigInteger.valueOf(-42), BigInteger.valueOf(-52), BigInteger.valueOf(-62), BigInteger.valueOf(-72), BigInteger.valueOf(-82), BigInteger.valueOf(-92)}, + { BigInteger.valueOf(-3), BigInteger.valueOf(-13), BigInteger.valueOf(-23), BigInteger.valueOf(-33), BigInteger.valueOf(-43), BigInteger.valueOf(-53), BigInteger.valueOf(-63), BigInteger.valueOf(-73), BigInteger.valueOf(-83), BigInteger.valueOf(-93)}, + { BigInteger.valueOf(-4), BigInteger.valueOf(-14), BigInteger.valueOf(-24), BigInteger.valueOf(-34), BigInteger.valueOf(-44), BigInteger.valueOf(-54), BigInteger.valueOf(-64), BigInteger.valueOf(-74), BigInteger.valueOf(-84), BigInteger.valueOf(-94)}, + { BigInteger.valueOf(-5), BigInteger.valueOf(-15), BigInteger.valueOf(-25), BigInteger.valueOf(-35), BigInteger.valueOf(-45), BigInteger.valueOf(-55), BigInteger.valueOf(-65), BigInteger.valueOf(-75), BigInteger.valueOf(-85), BigInteger.valueOf(-95)}, + { BigInteger.valueOf(-6), BigInteger.valueOf(-16), BigInteger.valueOf(-26), BigInteger.valueOf(-36), BigInteger.valueOf(-46), BigInteger.valueOf(-56), BigInteger.valueOf(-66), BigInteger.valueOf(-76), BigInteger.valueOf(-86), BigInteger.valueOf(-96)}, + { BigInteger.valueOf(-7), BigInteger.valueOf(-17), BigInteger.valueOf(-27), BigInteger.valueOf(-37), BigInteger.valueOf(-47), BigInteger.valueOf(-57), BigInteger.valueOf(-67), BigInteger.valueOf(-77), BigInteger.valueOf(-87), BigInteger.valueOf(-97)}, + { BigInteger.valueOf(-8), BigInteger.valueOf(-18), BigInteger.valueOf(-28), BigInteger.valueOf(-38), BigInteger.valueOf(-48), BigInteger.valueOf(-58), BigInteger.valueOf(-68), BigInteger.valueOf(-78), BigInteger.valueOf(-88), BigInteger.valueOf(-98)}, + { BigInteger.valueOf(-9), BigInteger.valueOf(-19), BigInteger.valueOf(-29), BigInteger.valueOf(-39), BigInteger.valueOf(-49), BigInteger.valueOf(-59), BigInteger.valueOf(-69), BigInteger.valueOf(-79), BigInteger.valueOf(-89), BigInteger.valueOf(-99)}, + {BigInteger.valueOf(-10), BigInteger.valueOf(-20), BigInteger.valueOf(-30), BigInteger.valueOf(-40), BigInteger.valueOf(-50), BigInteger.valueOf(-60), BigInteger.valueOf(-70), BigInteger.valueOf(-80), BigInteger.valueOf(-90), BigInteger.valueOf(-100)} + }, matrix.transpose().copyGrid()); } @Test - public void testHadamardProduct(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix transformMatrix = new BigIntegerMatrix(transformGrid1_2); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.TWO}}); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); + public void testTranspose_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); - //Invalid hadamard products - BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); - BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix3); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - transformMatrix = new BigIntegerMatrix(transformGrid2_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(6)}, - {BigInteger.TWO, BigInteger.valueOf(6)} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - transformMatrix = new BigIntegerMatrix(transformGrid3_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12)} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - transformMatrix = new BigIntegerMatrix(transformGrid4_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20)} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - transformMatrix = new BigIntegerMatrix(transformGrid10_2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)}, - {BigInteger.TWO, BigInteger.valueOf(6), BigInteger.valueOf(12), BigInteger.valueOf(20), BigInteger.valueOf(30), BigInteger.valueOf(42), BigInteger.valueOf(56), BigInteger.valueOf(72), BigInteger.valueOf(90), BigInteger.valueOf(110)} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); + assertArrayEquals(new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-11)}, + {BigInteger.valueOf(-2), BigInteger.valueOf(-12)}, + {BigInteger.valueOf(-3), BigInteger.valueOf(-13)}, + {BigInteger.valueOf(-4), BigInteger.valueOf(-14)}, + {BigInteger.valueOf(-5), BigInteger.valueOf(-15)}, + {BigInteger.valueOf(-6), BigInteger.valueOf(-16)}, + {BigInteger.valueOf(-7), BigInteger.valueOf(-17)}, + {BigInteger.valueOf(-8), BigInteger.valueOf(-18)}, + {BigInteger.valueOf(-9), BigInteger.valueOf(-19)}, + {BigInteger.valueOf(-10), BigInteger.valueOf(-20)} + }, matrix.transpose().copyGrid()); } @Test - public void testTranspose(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, matrix.transpose()); + public void testTranspose_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ONE}, - {BigInteger.TWO, BigInteger.TWO} - }); - assertEquals(correctMatrix, matrix.transpose()); + assertArrayEquals(new BigInteger[][]{ + {BigInteger.valueOf(-1), BigInteger.valueOf(-3), BigInteger.valueOf(-5), BigInteger.valueOf(-7), BigInteger.valueOf(-9), BigInteger.valueOf(-11), BigInteger.valueOf(-13), BigInteger.valueOf(-15), BigInteger.valueOf(-17), BigInteger.valueOf(-19)}, + {BigInteger.valueOf(-2), BigInteger.valueOf(-4), BigInteger.valueOf(-6), BigInteger.valueOf(-8), BigInteger.valueOf(-10), BigInteger.valueOf(-12), BigInteger.valueOf(-14), BigInteger.valueOf(-16), BigInteger.valueOf(-18), BigInteger.valueOf(-20)} + }, matrix.transpose().copyGrid()); + } - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.TWO, BigInteger.TWO, BigInteger.TWO}, - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)} - }); - assertEquals(correctMatrix, matrix.transpose()); + //! determinant() / det() + @Test + public void testDeterminant_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); - //4x4 - matrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO}, - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)} + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); }); - assertEquals(correctMatrix, matrix.transpose()); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO, BigInteger.TWO}, - {BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(3)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}, - {BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5)}, - {BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6), BigInteger.valueOf(6)}, - {BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7), BigInteger.valueOf(7)}, - {BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8), BigInteger.valueOf(8)}, - {BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9), BigInteger.valueOf(9)}, - {BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10), BigInteger.valueOf(10)}, + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); }); - assertEquals(correctMatrix, matrix.transpose()); } @Test - public void testDeterminant(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + public void testDeterminant_size1(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + assertEquals(BigInteger.ONE, matrix.determinant()); - - //Invalid determinants - BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.determinant(); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - assertEquals(BigInteger.ZERO, matrix.determinant()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.valueOf(4)}, - {BigInteger.valueOf(4), BigInteger.ONE} - }); - assertEquals(BigInteger.valueOf(-15), matrix.determinant()); - //det - assertEquals(matrix.determinant(), matrix.det()); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - assertEquals(BigInteger.ZERO, matrix.determinant()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.valueOf(4), BigInteger.TWO}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO} - }); - assertEquals(BigInteger.valueOf(-21), matrix.determinant()); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - assertEquals( BigInteger.ZERO, matrix.determinant()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals(BigInteger.valueOf(160), matrix.determinant()); - //Column - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(BigInteger.ZERO, matrix.determinant()); - //Column2 - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)} - }); - assertEquals(BigInteger.ZERO, matrix.determinant()); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - assertEquals(BigInteger.ZERO, matrix.determinant()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE}, - {BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO}, - {BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6)}, - {BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7)}, - {BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8)}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE} - }); - assertEquals(BigInteger.valueOf(-10000000), matrix.determinant()); + assertEquals(BigInteger.ONE, matrix.det()); } @Test - public void testCofactor(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.cofactor()); + public void testDeterminant_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); - //Invalid cofactor - BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.cofactor(); - }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.ONE.negate()}, - {BigInteger.TWO.negate(), BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.cofactor()); - //cof - assertEquals(matrix.cofactor(), matrix.cof()); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(3, 3, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.valueOf(4), BigInteger.TWO}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(7), BigInteger.ZERO, BigInteger.valueOf(-14)}, - {BigInteger.valueOf(-6), BigInteger.valueOf(-6), BigInteger.valueOf(15)}, - {BigInteger.valueOf(-4), BigInteger.valueOf(3), BigInteger.valueOf(-4)} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //4x4 - matrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(4, 4, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(-36), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(44)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(44), BigInteger.valueOf(-36)}, - {BigInteger.valueOf(4), BigInteger.valueOf(44), BigInteger.valueOf(-36), BigInteger.valueOf(4)}, - {BigInteger.valueOf(44), BigInteger.valueOf(-36), BigInteger.valueOf(4), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(10, BigInteger.valueOf(10), 0); - assertEquals("BigIntegerMatrix 10x10 failed cofactor1.", correctMatrix, matrix.cofactor()); - */ + assertEquals(BigInteger.valueOf(-2), matrix.determinant()); + assertEquals(BigInteger.valueOf(-2), matrix.det()); } @Test - public void testPower(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, matrix.pow(3)); + public void testDeterminant_size3(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-4), BigInteger.valueOf(-5), BigInteger.valueOf(-6)}, {BigInteger.valueOf(-7), BigInteger.valueOf(-8), BigInteger.valueOf(-9)}}); + + assertEquals(BigInteger.ZERO, matrix.determinant()); + assertEquals(BigInteger.ZERO, matrix.det()); + } + + @Test + public void testDeterminant_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertEquals(BigInteger.ZERO, matrix.determinant()); + assertEquals(BigInteger.ZERO, matrix.det()); + } + + @Test + public void testDeterminant_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); - //Invalid powers - final BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); - final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid1); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.pow(1); + matrix.determinant(); }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + //! cofactor() / cof() + @Test + public void testCofactor_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size1(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.ONE}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1)}}); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.valueOf(-4), BigInteger.valueOf(3)}, {BigInteger.valueOf(2), BigInteger.valueOf(-1)}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size10(){ + BigInteger[][] expectedGrid = new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + }; + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + //! adjoint() / adj() + @Test + public void testAdjoint_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size1(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.ONE}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1)}}); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.valueOf(-4), BigInteger.valueOf(2)}, {BigInteger.valueOf(3), BigInteger.valueOf(-1)}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size10(){ + BigInteger[][] expectedGrid = new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO} + }; + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + //! inverse() + @Test + public void testInverse_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size1(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.valueOf(-1)}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1)}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size2(){ + BigInteger[][] expectedGrid = new BigInteger[][]{{BigInteger.valueOf(-2), BigInteger.valueOf(3)}, {BigInteger.valueOf(3), BigInteger.valueOf(-4)}}; + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(4), BigInteger.valueOf(3)}, {BigInteger.valueOf(3), BigInteger.valueOf(2)}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + assertThrows(InvalidScalarException.class, () -> { - testMatrix2.pow(-1); + matrix.inverse(); }); - - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(9), BigInteger.valueOf(18)}, - {BigInteger.valueOf(9), BigInteger.valueOf(18)} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(36), BigInteger.valueOf(72), BigInteger.valueOf(108)}, - {BigInteger.valueOf(36), BigInteger.valueOf(72), BigInteger.valueOf(108)}, - {BigInteger.valueOf(36), BigInteger.valueOf(72), BigInteger.valueOf(108)} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //4x4 - //0 - matrix = new BigIntegerMatrix(grid4); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.pow(0)); - //1 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.pow(1)); - //3 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(100), BigInteger.valueOf(200), BigInteger.valueOf(300), BigInteger.valueOf(400)}, - {BigInteger.valueOf(100), BigInteger.valueOf(200), BigInteger.valueOf(300), BigInteger.valueOf(400)}, - {BigInteger.valueOf(100), BigInteger.valueOf(200), BigInteger.valueOf(300), BigInteger.valueOf(400)}, - {BigInteger.valueOf(100), BigInteger.valueOf(200), BigInteger.valueOf(300), BigInteger.valueOf(400)} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //10x10 - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)}, - {BigInteger.valueOf(3025), BigInteger.valueOf(6050), BigInteger.valueOf(9075), BigInteger.valueOf(12100), BigInteger.valueOf(15125), BigInteger.valueOf(18150), BigInteger.valueOf(21175), BigInteger.valueOf(24200), BigInteger.valueOf(27225), BigInteger.valueOf(30250)} - }); - assertEquals(correctMatrix, matrix.pow(3)); } @Test - public void testAdjoint(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.adjoint()); + public void testInverse_size2x10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2x10); - //2x2 - matrix = new BigIntegerMatrix(grid2); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.TWO, BigInteger.TWO.negate()}, - {BigInteger.ONE.negate(), BigInteger.ONE} - }); - assertEquals(correctMatrix, matrix.adjoint()); - //adj - assertEquals(matrix.adjoint(), matrix.adj()); - - //3x3 - matrix = new BigIntegerMatrix(grid3); - correctMatrix = new BigIntegerMatrix(3, 3, BigInteger.ZERO); - assertEquals(correctMatrix, matrix.adjoint()); - - //4x4 - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(-36), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(44)}, - {BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(44), BigInteger.valueOf(-36)}, - {BigInteger.valueOf(4), BigInteger.valueOf(44), BigInteger.valueOf(-36), BigInteger.valueOf(4)}, - {BigInteger.valueOf(44), BigInteger.valueOf(-36), BigInteger.valueOf(4), BigInteger.valueOf(4)} - }); - assertEquals(correctMatrix, matrix.adjoint()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new BigIntegerMatrix(grid10); - correctMatrix = new BigIntegerMatrix(10, BigInteger.valueOf(10), 0); - assertEquals("BigIntegerMatrix 10x10 failed adjoint.", correctMatrix, matrix.adjoint()); - */ - } - - @Test - public void testInverse(){ - //1x1 - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.inverse()); - - //Invalid inverse - BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); - BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)} - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.inverse(); + matrix.inverse(); }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.inverse(); - }); - - //2x2 - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.valueOf(4)}, - {BigInteger.valueOf(4), BigInteger.ONE} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO.negate(), BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO.negate()} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //3x3 - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.valueOf(4), BigInteger.TWO}, - {BigInteger.TWO, BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO.negate(), BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO.negate()}, - {BigInteger.ZERO, BigInteger.ZERO.negate(), BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //4x4 - matrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE}, - {BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO}, - {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO.negate(), BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO.negate()}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO.negate(), BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO.negate(), BigInteger.ZERO, BigInteger.ZERO} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //10x10 - //?Skipped 10x10 because it would take a long time to compute } @Test - public void testGenerateIdentity(){ - //0x0 + public void testInverse_size10x2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + //! equals() + @Test + public void testEquals_null(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertFalse(matrix.equals(null)); + } + + @Test + public void testEquals_matrixObject(){ + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(negativeGrid2); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals((Object)equalsMatrix)); + } + + @Test + public void testEquals_array(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(negativeGrid2)); + } + + @Test + public void testEquals_invalidType(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(1)); + } + + @Test + public void testEquals_manyRows(){ + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}, {BigInteger.valueOf(-3), BigInteger.valueOf(-4)}, {BigInteger.valueOf(-5), BigInteger.valueOf(-6)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewRows(){ + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_manyCols(){ + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1), BigInteger.valueOf(-2), BigInteger.valueOf(-3)}, {BigInteger.valueOf(-4), BigInteger.valueOf(-5), BigInteger.valueOf(-6)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewCols(){ + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.valueOf(-1)}, {BigInteger.valueOf(-2)}}); + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_notEquals(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + BigIntegerMatrix equalsMatrix = matrix.transpose(); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_equals(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + BigIntegerMatrix equalsMatrix = new BigIntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_self(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(matrix)); + } + + //! hashCode() + @Test + public void testHashCode_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size1(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + //! toString() + @Test + public void testToString_size0(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + + assertEquals("[]", matrix.toString()); + } + + @Test + public void testToString_size1(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + + assertEquals("[1]", matrix.toString()); + } + + @Test + public void testToString_size2(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid2); + + assertEquals("[-1, -2]\n[-3, -4]", matrix.toString()); + } + + @Test + public void testToString_size10(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(negativeGrid10); + + assertEquals( + "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]\n" + + "[-11, -12, -13, -14, -15, -16, -17, -18, -19, -20]\n" + + "[-21, -22, -23, -24, -25, -26, -27, -28, -29, -30]\n" + + "[-31, -32, -33, -34, -35, -36, -37, -38, -39, -40]\n" + + "[-41, -42, -43, -44, -45, -46, -47, -48, -49, -50]\n" + + "[-51, -52, -53, -54, -55, -56, -57, -58, -59, -60]\n" + + "[-61, -62, -63, -64, -65, -66, -67, -68, -69, -70]\n" + + "[-71, -72, -73, -74, -75, -76, -77, -78, -79, -80]\n" + + "[-81, -82, -83, -84, -85, -86, -87, -88, -89, -90]\n" + + "[-91, -92, -93, -94, -95, -96, -97, -98, -99, -100]", + matrix.toString()); + } + + //! generateIdentity() + @Test + public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { BigIntegerMatrix.generateIdentity(0); }); - - //1x1 - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(1)); - - //2x2 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ONE } - }); - assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(2)); - - //3x3 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } - }); - assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(3)); - - //4x4 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } - }); - assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(4)); - - //10x10 - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, - {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } - }); - assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(10)); } @Test - public void testHashCode(){ - BigIntegerMatrix matrix = new BigIntegerMatrix(); - assertEquals(Arrays.hashCode(new BigInteger[0][0]), matrix.hashCode()); + public void testGenerateIdentity_negativeSize(){ + assertThrows(InvalidGeometryException.class, () -> { + BigIntegerMatrix.generateIdentity(-1); + }); } @Test - public void testToString(){ - BigIntegerMatrix matrix = new BigIntegerMatrix(grid3); - String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; - assertEquals(matrixString, matrix.toString()); + public void testGenerateIdentity_size2(){ + BigIntegerMatrix matrix = BigIntegerMatrix.generateIdentity(2); + + assertArrayEquals(new BigInteger[][]{{BigInteger.ONE, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ONE}}, matrix.copyGrid()); } @Test - public void testLaplaceExpansionHelper(){ - BigIntegerMatrix matrix = new BigIntegerMatrix(); - matrix.addRow(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix.laplaceExpansionHelper(0, 0); - }); + public void testGenerateIdentity_size3(){ + BigIntegerMatrix matrix = BigIntegerMatrix.generateIdentity(3); - BigIntegerMatrix matrix2 = new BigIntegerMatrix(); - matrix2.setGrid(grid1); - assertThrows(InvalidGeometryException.class, () -> { - matrix2.laplaceExpansionHelper(0, 0); - }); + assertArrayEquals(new BigInteger[][]{{BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE}}, matrix.copyGrid()); + } - BigIntegerMatrix matrix3 = new BigIntegerMatrix(); - matrix3.setGrid(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(-1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, 2); - }); + @Test + public void testGenerateIdentity_size10(){ + BigIntegerMatrix matrix = BigIntegerMatrix.generateIdentity(10); - BigIntegerMatrix matrix4 = new BigIntegerMatrix(); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - matrix4.addCol(grid2[1]); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); + assertArrayEquals(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE} + }, matrix.copyGrid()); } } diff --git a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java index 81f21f1..6c80d08 100644 --- a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java @@ -1,15 +1,11 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 06-30-23 +//Modified: 08-10-24 package com.mattrixwv.matrix; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; @@ -23,1872 +19,2225 @@ import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestDoubleMatrix{ - //Grid 1x1 - private static final double[][] grid1 = { - {0.5} + private static final double[][] negativeGrid2 = new double[][]{ + {-1, -2}, + {-3, -4} }; - private static final double[][] transformGrid1_1 = { - {0.5} + private static final double[][] negativeGrid10 = new double[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} }; - private static final double[][] transformGrid1_2 = { - {1.5} + private static final double[][] negativeGrid2x10 = new double[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; - - //Grid 2x2 - private static final double[][] grid2 = { - {0.5, 1.5}, - {0.5, 1.5} - }; - private static final double[][] transformGrid2_1 = { - {0.5, 0}, - {0.5, 0} - }; - private static final double[][] transformGrid2_2 = { - {1.5, 2.5}, - {1.5, 2.5} - }; - - //Grid 3x3 - private static final double[][] grid3 = { - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }; - private static final double[][] transformGrid3_1 = { - {1.5, 0.5, 0}, - {1.5, 0.5, 0}, - {1.5, 0.5, 0} - }; - private static final double[][] transformGrid3_2 = { - {1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5} - }; - - //Grid 4x4 - private static final double[][] grid4 = { - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }; - private static final double[][] transformGrid4_1 = { - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0} - }; - private static final double[][] transformGrid4_2 = { - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0}, - {2.5, 1.5, 0.5, 0} - }; - - //Grid 10x10 - private static final double[][] grid10 = { - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }; - private static final double[][] transformGrid10_1 = { - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0}, - {8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 0} - }; - private static final double[][] transformGrid10_2 = { - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} + private static final double[][] negativeGrid10x2 = new double[][]{ + { -1, -2}, + { -3, -4}, + { -5, -6}, + { -7, -8}, + { -9, -10}, + {-11, -12}, + {-13, -14}, + {-15, -16}, + {-17, -18}, + {-19, -20} }; + //! Constructor @Test - public void testConstructor(){ - //Default constructor + public void testConstructor_default(){ DoubleMatrix matrix = new DoubleMatrix(); + assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); - - //Filler constructor - //0 rows - assertThrows(InvalidGeometryException.class, () -> { - new DoubleMatrix(0, 0, 0.0); - }); - //0 cols - assertThrows(InvalidGeometryException.class, () -> { - new DoubleMatrix(1, 0, 0.0); - }); - //Good values - matrix = new DoubleMatrix(2, 2, 0.0); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(0.0, matrix.get(0, 0), 0.0001); - assertEquals(0.0, matrix.get(0, 1), 0.0001); - assertEquals(0.0, matrix.get(1, 0), 0.0001); - assertEquals(0.0, matrix.get(1, 1), 0.0001); - - //Matrix constructor - matrix.set(0, 0, 1.0); - matrix.set(0, 1, 2.0); - matrix.set(1, 0, 1.0); - matrix.set(1, 1, 2.0); - DoubleMatrix matrix2 = new DoubleMatrix(matrix); - assertEquals(2, matrix2.getNumRows()); - assertEquals(2, matrix2.getNumCols()); - assertEquals(1.0, matrix2.get(0, 0), 0.0001); - assertEquals(2.0, matrix2.get(0, 1), 0.0001); - assertEquals(1.0, matrix2.get(1, 0), 0.0001); - assertEquals(2.0, matrix2.get(1, 1), 0.0001); - - //Array constructor - //0 length - double[][] grid = new double[0][0]; - matrix = new DoubleMatrix(grid); - assertEquals(0, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //0 cols - grid = new double[1][0]; - matrix = new DoubleMatrix(grid); - assertEquals(1, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //Uneven rows - assertThrows(InvalidRowSizeException.class, () -> { - double[][] grid1 = new double[2][]; - grid1[0] = new double[1]; - grid1[0][0] = 0.0; - grid1[1] = new double[2]; - grid1[1][0] = 1.0; - grid1[1][1] = 2.0; - new DoubleMatrix(grid1); - }); - - //2x2 - grid = grid2; - matrix = new DoubleMatrix(grid); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(0.5, matrix.get(0, 0), 0.0001); - assertEquals(1.5, matrix.get(0, 1), 0.0001); - assertEquals(0.5, matrix.get(1, 0), 0.0001); - assertEquals(1.5, matrix.get(1, 1), 0.0001); } @Test - public void testEquals(){ - //Invalid equals - DoubleMatrix matrix = new DoubleMatrix(grid1); - assertNotEquals(matrix, null); - assertNotEquals(matrix, new int[0]); + public void testConstructor_fill0Rows(){ + assertThrows(InvalidGeometryException.class, () -> { + new DoubleMatrix(0, 1, 0); + }); + } - //1x1 - matrix = new DoubleMatrix(grid1); - boolean gridEquals = matrix.equals(matrix); - assertTrue(gridEquals); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals1 = matrix.equals(grid1); - assertTrue(gridEquals1); - //With delta - boolean gridEquals12 = matrix.equals(matrix, 0.0001); - assertTrue(gridEquals12); + @Test + public void testConstructor_fill0Cols(){ + assertThrows(InvalidGeometryException.class, () -> { + new DoubleMatrix(1, 0, 0); + }); + } - //2x2 - matrix = new DoubleMatrix(grid2); - boolean gridEquals2 = matrix.equals(matrix); - assertTrue(gridEquals2); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals21 = matrix.equals(grid2); - assertTrue(gridEquals21); - //false - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals22 = matrix.equals(transformGrid2_1); - assertFalse(gridEquals22); - gridEquals2 = matrix.equals(new DoubleMatrix(grid3)); - assertFalse(gridEquals2); - gridEquals2 = matrix.equals(new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - })); - assertFalse(gridEquals2); + @Test + public void testConstructor_fillSize2(){ + DoubleMatrix matrix = new DoubleMatrix(2, 2, -1); - //3x3 - matrix = new DoubleMatrix(grid3); - boolean gridEquals3 = matrix.equals(matrix); - assertTrue(gridEquals3); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals31 = matrix.equals(grid3); - assertTrue(gridEquals31); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-1, matrix.get(0, 1)); + assertEquals(-1, matrix.get(1, 0)); + assertEquals(-1, matrix.get(1, 1)); + } - //4x4 - matrix = new DoubleMatrix(grid4); - boolean gridEquals4 = matrix.equals(matrix); - assertTrue(gridEquals4); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals41 = matrix.equals(grid4); - assertTrue(gridEquals41); + @Test + public void testConstructor_fillSize10(){ + DoubleMatrix matrix = new DoubleMatrix(10, 10, -1); - //10x10 - matrix = new DoubleMatrix(grid10); - boolean gridEquals10 = matrix.equals(matrix); - assertTrue(gridEquals10); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals101 = matrix.equals(grid10); - assertTrue(gridEquals101); + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(double[] row : matrix.copyGrid()){ + for(double num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_arraySize0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_arraySize2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arrayUnevenRows(){ + double[][] grid = new double[][]{ + {-1, -2, -3}, + {-4, -5}, + {-6, -7, -8, -9}, + {-10, -11, -12} + }; + + assertThrows(InvalidRowSizeException.class, () -> { + new DoubleMatrix(grid); + }); + } + + @Test + public void testConstructor_matrixSize0(){ + DoubleMatrix originalMatrix = new DoubleMatrix(); + DoubleMatrix matrix = new DoubleMatrix(originalMatrix); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_matrixSize2(){ + DoubleMatrix originalMatrix = new DoubleMatrix(2, 2, -1); + DoubleMatrix matrix = new DoubleMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(double[] row : matrix.copyGrid()){ + for(double num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10(){ + DoubleMatrix originalMatrix = new DoubleMatrix(10, 10, -1); + DoubleMatrix matrix = new DoubleMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(double[] row : matrix.copyGrid()){ + for(double num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize2x10(){ + DoubleMatrix originalMatrix = new DoubleMatrix(2, 10, -1); + DoubleMatrix matrix = new DoubleMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(double[] row : matrix.copyGrid()){ + for(double num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10x2(){ + DoubleMatrix originalMatrix = new DoubleMatrix(10, 2, -1); + DoubleMatrix matrix = new DoubleMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(double[] row : matrix.copyGrid()){ + for(double num : row){ + assertEquals(-1, num); + } + } + } + + //! setGrid() + @Test + public void testSetGrid_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(grid); + + assertEquals(2, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2(){ + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10(){ + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(); + matrix.setGrid(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + //! isSquare() + @Test + public void testIsSquare_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertFalse(matrix.isSquare()); + } + + //! laplaceExpansionHelper() + @Test + public void testLaplaceExpansionHelper_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + DoubleMatrix result = matrix.laplaceExpansionHelper(0, 0); + + assertArrayEquals(new double[][]{{-4}}, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(-1, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(2, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, -1); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, 2); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc0x0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + DoubleMatrix result = matrix.laplaceExpansionHelper(0, 0); + + double[][] expectedGrid = new double[][]{ + {-12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-92, -93, -94, -95, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc4x4(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + DoubleMatrix result = matrix.laplaceExpansionHelper(4, 4); + + double[][] expectedGrid = new double[][]{ + { -1, -2, -3, -4, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -36, -37, -38, -39, -40}, + {-51, -52, -53, -54, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + //! get() + @Test + public void testGet_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(2, 0); + }); + } + + @Test + public void testGet_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(-1, 0); + }); + } + + @Test + public void testGet_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, 2); + }); + } + + @Test + public void testGet_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, -1); + }); } @Test public void testGet(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - assertEquals(0.5, matrix.get(0, 0), 0.0000001); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-2, matrix.get(0, 1)); + assertEquals(-3, matrix.get(1, 0)); + assertEquals(-4, matrix.get(1, 1)); + } + + //! getRow() + @Test + public void testGetRow_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid gets - final DoubleMatrix testMatrix = new DoubleMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(3, 3); + matrix.getRow(2); }); + } + + @Test + public void testGetRow_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(-1, -1); + matrix.getRow(-1); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, -1); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - assertEquals(0.5, matrix.get(0, 0), 0.0000001); - - //3x3 - matrix = new DoubleMatrix(grid3); - assertEquals(0.5, matrix.get(0, 0), 0.0000001); - - //4x4 - matrix = new DoubleMatrix(grid4); - assertEquals(0.5, matrix.get(0, 0), 0.0000001); - - //10x10 - matrix = new DoubleMatrix(grid10); - assertEquals(0.5, matrix.get(0, 0), 0.0000001); } @Test public void testGetRow(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals(correctMatrix, matrix.getRow(0)); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertArrayEquals(new double[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); + assertArrayEquals(new double[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); + } + + //! getColumn() + @Test + public void testGetCol_0Rows(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); - //Invalid gets - final DoubleMatrix testMatrix = new DoubleMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(-1); + matrix.getCol(0); }); + } + + @Test + public void testGetCol_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(3); + matrix.getCol(2); }); + } - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5}}); - assertEquals(correctMatrix, matrix.getRow(0)); + @Test + public void testGetCol_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //4x4 - matrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //10x10 - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}}); - assertEquals(correctMatrix, matrix.getRow(0)); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(-1); + }); } @Test public void testGetCol(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals(correctMatrix, matrix.getCol(0)); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertArrayEquals(new double[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); + assertArrayEquals(new double[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); + } + + //! getNumRows() + @Test + public void testGetNumRows_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + } + + //! getNumCols() + @Test + public void testGetNumCols_size0(){ + double[][] grid = new double[0][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertEquals(2, matrix.getNumCols()); + } + + //! set() + @Test + public void testSet_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid gets - final DoubleMatrix testMatrix = new DoubleMatrix(); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(-1); + matrix.set(-1, 0, 0); }); + } + + @Test + public void testSet_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(3); + matrix.set(2, 0, 0); }); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); + } + + @Test + public void testSet_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix2.getCol(3); + matrix.set(0, -1, 0); }); + } - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + @Test + public void testSet_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5}, - {0.5} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, 2, 0); }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //4x4 - matrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5}, - {0.5}, - {0.5} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //10x10 - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5}, - {0.5} - }); - assertEquals(correctMatrix, matrix.getCol(0)); } @Test public void testSet(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - matrix.set(0, 0, 1.5); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.5}}); - assertEquals(correctMatrix, matrix); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + matrix.set(0, 0, -5); + matrix.set(0, 1, -6); + matrix.set(1, 0, -7); + matrix.set(1, 1, -8); + assertEquals(-5, matrix.get(0, 0)); + assertEquals(-6, matrix.get(0, 1)); + assertEquals(-7, matrix.get(1, 0)); + assertEquals(-8, matrix.get(1, 1)); + } + + //! setRow() + @Test + public void testSetRow_array_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid sets - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(-1, -1, 0.0); + matrix.setRow(-1, new double[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(2, 2, 0.0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, -1, 0.0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, 2, 0.0); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - matrix.set(0, 0, 2.5); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5}, - {0.5, 1.5} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new DoubleMatrix(grid3); - matrix.set(0, 0, 2.5); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new DoubleMatrix(grid4); - matrix.set(0, 0, 2.5); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new DoubleMatrix(grid10); - matrix.set(0, 0, 2.5); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetRow(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - matrix.setRow(0, new double[]{0.0}); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid setRows - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final double[] testGrid = {0.0, 0.0}; - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(-1, testGrid); + matrix.setRow(2, new double[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, (double[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setRow(0, (DoubleMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testMatrix2); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - matrix.setRow(1, new double[]{1.5, 0.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {1.5, 0.5} - }); - assertEquals(correctMatrix, matrix); - DoubleMatrix matrix2 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - matrix.setRow(1, matrix2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {0.0, 0.0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new DoubleMatrix(grid3); - matrix.setRow(0, new double[]{0, 0.5, 1.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 0.5, 1.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new DoubleMatrix(grid4); - matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {3.5, 2.5, 1.5, 0.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new DoubleMatrix(grid10); - matrix.setRow(0, new double[]{9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetCol(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - matrix.setCol(0, new double[]{0.5}); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_nullArray(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid setCols - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(); - final double[] testGrid = {0.0, 0.0}; - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(2, testGrid); - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, (double[])null); + matrix.setRow(0, (double[])null); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(0, testGrid); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setCol(0, (DoubleMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, testMatrix2); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - matrix.setCol(0, new double[]{2.5, 2.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5}, - {2.5, 1.5} - }); - assertEquals(correctMatrix, matrix); - //Matrix - DoubleMatrix vector = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); - matrix.setCol(1, vector); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 0.0}, - {2.5, 0.0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new DoubleMatrix(grid3); - matrix.setCol(0, new double[]{0.0, 0.0, 0.0}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.5, 2.5}, - {0.0, 1.5, 2.5}, - {0.0, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new DoubleMatrix(grid4); - matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.5, 2.5, 3.5}, - {0.0, 1.5, 2.5, 3.5}, - {0.0, 1.5, 2.5, 3.5}, - {0.0, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new DoubleMatrix(grid10); - matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddRow(){ - //0x0 - DoubleMatrix matrix = new DoubleMatrix(); - matrix.addRow(new double[]{0.0}); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_arrayLength0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //1x1 - matrix = new DoubleMatrix(grid1); - matrix.addRow(new double[]{0.5}); - correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(new double[]{0.0, 0.0}); + matrix.setRow(0, new double[0]); }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((double[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((DoubleMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(testMatrix2); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - matrix.addRow(new double[]{0.5, 1.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {0.5, 1.5}, - {0.5, 1.5} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new DoubleMatrix(grid2); - matrix.addRow(new DoubleMatrix(new double[][]{{1.0, 2.0}})); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {0.5, 1.5}, - {1.0, 2.0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new DoubleMatrix(grid3); - matrix.addRow(new double[]{0.5, 1.5, 2.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new DoubleMatrix(grid4); - matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new DoubleMatrix(grid10); - matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddCol(){ - //0x0 - DoubleMatrix matrix = new DoubleMatrix(); - matrix.addCol(new double[]{0.0}); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_invalidArrayLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //1x1 - matrix = new DoubleMatrix(grid1); - matrix.addCol(new double[]{0.5}); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(new double[]{0.0, 0.}); + matrix.setRow(0, new double[]{0, 0, 0}); }); + } + + @Test + public void testSetRow_array(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + matrix.setRow(0, new double[]{-5, -6}); + matrix.setRow(1, new double[]{-7, -8}); + assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((double[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((DoubleMatrix)null); + matrix.setRow(0, (DoubleMatrix)null); }); + } + + @Test + public void testSetRow_matrix_multipleRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(testMatrix2); + matrix.setRow(0, new DoubleMatrix(new double[][]{{0, 0}, {0, 0}})); }); + } - //2x2 - matrix = new DoubleMatrix(grid2); - matrix.addCol(new double[]{2.5, 2.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new DoubleMatrix(grid2); - matrix.addCol(new DoubleMatrix(new double[][]{{0.0}, {0.0}})); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 0.0}, - {0.5, 1.5, 0.0} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_negativeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //3x3 - matrix = new DoubleMatrix(grid3); - matrix.addCol(new double[]{3.5, 3.5, 3.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new DoubleMatrix(new double[][]{{0}})); }); - assertEquals(correctMatrix, matrix); + } - //4x4 - matrix = new DoubleMatrix(grid4); - matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_largeRow(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //10x10 - matrix = new DoubleMatrix(grid10); - matrix.addCol(new double[]{10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new DoubleMatrix(new double[][]{{0}})); + }); + } + + @Test + public void testSetRow_matrix_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new DoubleMatrix(new double[][]{{}})); + }); + } + + @Test + public void testSetRow_matrix_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new DoubleMatrix(new double[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetRow_matrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + matrix.setRow(0, new DoubleMatrix(new double[][]{{-5, -6}})); + matrix.setRow(1, new DoubleMatrix(new double[][]{{-7, -8}})); + assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! setCol() + @Test + public void testSetCol_array_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new double[]{0}); + }); + } + + @Test + public void testSetCol_array_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new double[]{0}); + }); + } + + @Test + public void testSetCol_array_nullArray(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, (double[])null); + }); + } + + @Test + public void testSetCol_array_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new double[0]); + }); + } + + @Test + public void testSetCol_array_invalidArrayLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new double[]{0, 0, 0}); + }); + } + + @Test + public void testSetCol_array(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + matrix.setCol(0, new double[]{-5, -7}); + matrix.setCol(1, new double[]{-6, -8}); + assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.setCol(0, (DoubleMatrix)null); + }); + } + + @Test + public void testSetCol_matrix_multipleCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testSetCol_matrix_negativeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new DoubleMatrix(new double[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_largeCol(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new DoubleMatrix(new double[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new DoubleMatrix(new double[][]{{}})); + }); + } + + @Test + public void testSetCol_matrix_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new DoubleMatrix(new double[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetCol_matrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + matrix.setCol(0, new DoubleMatrix(new double[][]{{-5}, {-7}})); + matrix.setCol(1, new DoubleMatrix(new double[][]{{-6}, {-8}})); + assertArrayEquals(new double[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! addRow() + @Test + public void testAddRow_array_nullArray(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((double[])null); + }); + } + + @Test + public void testAddRow_array_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new double[0]); + }); + } + + @Test + public void testAddRow_array_invalidArrayLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new double[]{0, 0, 0}); + }); + } + + @Test + public void testAddRow_array(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix.addRow(new double[]{-5, -6}); + + assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((DoubleMatrix)null); + }); + } + + @Test + public void testAddRow_matrix_multipleRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}})); + }); + } + + @Test + public void testAddRow_matrix_noRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new DoubleMatrix(new double[0][0])); + }); + } + + @Test + public void testAddRow_matrix_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new DoubleMatrix(new double[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddRow_matrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix.addRow(new DoubleMatrix(new double[][]{{-5, -6}})); + + assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + //! addCol() + @Test + public void testAddCol_array_nullArray(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((double[])null); + }); + } + + @Test + public void testAddCol_array_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new double[0]); + }); + } + + @Test + public void testAddCol_array_invalidArrayLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new double[]{0, 0, 0}); + }); + } + + @Test + public void testAddCol_array(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix.addCol(new double[]{-5, -6}); + + assertArrayEquals(new double[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((DoubleMatrix)null); + }); + } + + @Test + public void testAddCol_matrix_multipleCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testAddCol_matrix_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new DoubleMatrix(new double[][]{{}})); + }); + } + + @Test + public void testAddCol_matrix_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new DoubleMatrix(new double[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddCol_matrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix.addCol(new DoubleMatrix(new double[][]{{-5}, {-6}})); + + assertArrayEquals(new double[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + //! appendRight() + @Test + public void testAppendRight_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendRight((DoubleMatrix)null); + }); + } + + @Test + public void testAppendRight_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new DoubleMatrix(new double[0][0])); + }); + } + + @Test + public void testAppendRight_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new DoubleMatrix(new double[][]{{0, 0, 0}})); }); - assertEquals(correctMatrix, matrix); } @Test public void testAppendRight(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix secondMatrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.appendRight(matrix); + + assertArrayEquals(new double[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); + } + + //! appendBottom() + @Test + public void testAppendBottom_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid appends - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendRight(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendRight(null); + matrix.appendBottom((DoubleMatrix)null); }); + } - //2x2 - matrix = new DoubleMatrix(grid2); - secondMatrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 0.5, 1.5}, - {0.5, 1.5, 0.5, 1.5} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_length0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //3x3 - matrix = new DoubleMatrix(grid3); - secondMatrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5, 0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5, 0.5, 1.5, 2.5} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new DoubleMatrix(new double[0][0])); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + } - //4x4 - matrix = new DoubleMatrix(grid4); - secondMatrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_invalidLength(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //10x10 - matrix = new DoubleMatrix(grid10); - secondMatrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new DoubleMatrix(new double[][]{{0}, {0}, {0}})); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); } @Test public void testAppendBottom(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix secondMatrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.appendBottom(matrix); + + assertArrayEquals(new double[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); + } + + //! add() + @Test + public void testAdd_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid appends - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendBottom(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendBottom(null); + matrix.add((DoubleMatrix)null); }); - - //2x2 - matrix = new DoubleMatrix(grid2); - secondMatrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {0.5, 1.5}, - {0.5, 1.5}, - {0.5, 1.5} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //3x3 - matrix = new DoubleMatrix(grid3); - secondMatrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //4x4 - matrix = new DoubleMatrix(grid4); - secondMatrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //10x10 - matrix = new DoubleMatrix(grid10); - secondMatrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); } @Test - public void testIsSquare(){ + public void testAdd_matrix_fewRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testAdd_matrix_manyRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new DoubleMatrix(new double[][]{{0}, {0}})); + }); + } + + @Test + public void testAdd_matrix_fewCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new DoubleMatrix(new double[][]{{0}})); + }); + } + + @Test + public void testAdd_matrix_manyCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testAdd_matrix_size2(){ + DoubleMatrix addMatrix = new DoubleMatrix(new double[][]{{-4, -3}, {-2, -1}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new double[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); + } + + @Test + public void testAdd_matrix_size10(){ + DoubleMatrix addMatrix = new DoubleMatrix(new double[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new double[][]{ + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101} + }, matrix.copyGrid()); + } + + @Test + public void testAdd_scalar(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.add(1); + + assertArrayEquals(new double[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); + } + + //! subtract() + @Test + public void testSubtract_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.subtract((DoubleMatrix)null); + }); + } + + @Test + public void testSubtract_matrix_fewRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_manyRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_fewCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new DoubleMatrix(new double[][]{{0}, {0}})); + }); + } + + @Test + public void testSubtract_matrix_manyCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_size2(){ + DoubleMatrix subMatrix = new DoubleMatrix(new double[][]{{-4, -3}, {-2, -1}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new double[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_matrix_size10(){ + DoubleMatrix subMatrix = new DoubleMatrix(new double[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new double[][]{ + { 99, 97, 95, 93, 91, 89, 87, 85, 83, 81}, + { 79, 77, 75, 73, 71, 69, 67, 65, 63, 61}, + { 59, 57, 55, 53, 51, 49, 47, 45, 43, 41}, + { 39, 37, 35, 33, 31, 29, 27, 25, 23, 21}, + { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, + { -1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-21, -23, -25, -27, -29, -31, -33, -35, -37, -39}, + {-41, -43, -45, -47, -49, -51, -53, -55, -57, -59}, + {-61, -63, -65, -67, -69, -71, -73, -75, -77, -79}, + {-81, -83, -85, -87, -89, -91, -93, -95, -97, -99} + }, matrix.copyGrid()); + } + + @Test + public void testSubtract_scalar(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.subtract(1); + + assertArrayEquals(new double[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); + } + + //! multiply() + @Test + public void testMultiply_matrix_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.multiply((DoubleMatrix)null); + }); + } + + @Test + public void testMultiply_matrix_manyRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_fewRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_square(){ + DoubleMatrix mulMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new double[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_matrix_rectangle(){ + DoubleMatrix mulMatrix = new DoubleMatrix(new double[][]{{1, 2, 3}, {4, 5, 6}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new double[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_scalar(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + matrix = matrix.multiply(2); + + assertArrayEquals(new double[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); + } + + //! pow + @Test + public void testPow_rectangle(){ + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1, 2, 3}, {4, 5, 6}}); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.pow(2); + }); + } + + @Test + public void testPow_negative(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidScalarException.class, () -> { + matrix.pow(-1); + }); + } + + @Test + public void testPow_0(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + DoubleMatrix result = matrix.pow(0); + + assertEquals(new DoubleMatrix(2, 2, 1), result); + } + + @Test + public void testPow_2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + DoubleMatrix result = matrix.pow(2); + + assertEquals(new DoubleMatrix(new double[][]{{7, 10}, {15, 22}}), result); + } + + @Test + public void testPow_3(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + DoubleMatrix result = matrix.pow(3); + + assertEquals(new DoubleMatrix(new double[][]{{-37, -54}, {-81, -118}}), result); + } + + //! dotProduct() + @Test + public void testDotProduct_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.dotProduct((DoubleMatrix)null); + }); + } + + @Test + public void testDotProduct_fewRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testDotProduct_manyRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testDotProduct_size2(){ + DoubleMatrix dotMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + double result = matrix.dotProduct(dotMatrix); + + assertEquals(-54, result); + } + + @Test + public void testDotProduct_size10(){ + DoubleMatrix dotMatrix = new DoubleMatrix(new double[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + double result = matrix.dotProduct(dotMatrix); + + assertEquals(-2632750, result); + } + + //! hadamardProduct() + @Test + public void testHadamardProduct_nullMatrix(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.hadamardProduct((DoubleMatrix)null); + }); + } + + @Test + public void testHadamardProduct_fewRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0}})); + }); + } + + @Test + public void testHadamardProduct_manyRows(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testHadamardProduct_fewCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0}, {0}})); + }); + } + + @Test + public void testHadamardProduct_manyCols(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new DoubleMatrix(new double[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testHadamardProduct_size2(){ + DoubleMatrix hadMatrix = new DoubleMatrix(new double[][]{{1, 2}, {3, 4}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + DoubleMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new double[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_size10(){ + DoubleMatrix hadMatrix = new DoubleMatrix(new double[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + DoubleMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new double[][]{ + { -1, -4, -9, -16, -25, -36, -49, -64, -81, -100}, + { -121, -144, -169, -196, -225, -256, -289, -324, -361, -400}, + { -441, -484, -529, -576, -625, -676, -729, -784, -841, -900}, + { -961, -1024, -1089, -1156, -1225, -1296, -1369, -1444, -1521, -1600}, + {-1681, -1764, -1849, -1936, -2025, -2116, -2209, -2304, -2401, -2500}, + {-2601, -2704, -2809, -2916, -3025, -3136, -3249, -3364, -3481, -3600}, + {-3721, -3844, -3969, -4096, -4225, -4356, -4489, -4624, -4761, -4900}, + {-5041, -5184, -5329, -5476, -5625, -5776, -5929, -6084, -6241, -6400}, + {-6561, -6724, -6889, -7056, -7225, -7396, -7569, -7744, -7921, -8100}, + {-8281, -8464, -8649, -8836, -9025, -9216, -9409, -9604, -9801, -10000} + }, result.copyGrid()); + } + + //! transpose() + @Test + public void testTranspose_size0(){ DoubleMatrix matrix = new DoubleMatrix(); - assertFalse(matrix.isSquare()); - matrix = new DoubleMatrix(2, 2, 0.0); - assertTrue(matrix.isSquare()); + DoubleMatrix result = matrix.transpose(); - matrix = new DoubleMatrix(2, 3, 0.0); - assertFalse(matrix.isSquare()); + assertEquals(new DoubleMatrix(), result); } @Test - public void testAddition(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - assertEquals(correctMatrix, matrix.add(0.5)); + public void testTranspose_size0x2(){ + double[][] grid = new double[0][2]; + DoubleMatrix matrix = new DoubleMatrix(grid); - //Invalid adds - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); - final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix3); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - transformMatrix = new DoubleMatrix(transformGrid2_1); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 1.5}, - {1.0, 1.5} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 2.0}, - {1.0, 2.0} - }); - assertEquals(correctMatrix, matrix.add(0.5)); - - //3x3 - matrix = new DoubleMatrix(grid3); - transformMatrix = new DoubleMatrix(transformGrid3_1); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.0, 2.0, 2.5}, - {2.0, 2.0, 2.5}, - {2.0, 2.0, 2.5} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 2.0, 3.0}, - {1.0, 2.0, 3.0}, - {1.0, 2.0, 3.0} - }); - assertEquals(correctMatrix, matrix.add(0.5)); - - //4x4 - matrix = new DoubleMatrix(grid4); - transformMatrix = new DoubleMatrix(transformGrid4_1); - correctMatrix = new DoubleMatrix(new double[][]{ - {3.0, 3.0, 3.0, 3.5}, - {3.0, 3.0, 3.0, 3.5}, - {3.0, 3.0, 3.0, 3.5}, - {3.0, 3.0, 3.0, 3.5} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 2.0, 3.0, 4.0}, - {1.0, 2.0, 3.0, 4.0}, - {1.0, 2.0, 3.0, 4.0}, - {1.0, 2.0, 3.0, 4.0} - }); - assertEquals(correctMatrix, matrix.add(0.5)); - - //10x10 - matrix = new DoubleMatrix(grid10); - transformMatrix = new DoubleMatrix(transformGrid10_1); - correctMatrix = new DoubleMatrix(new double[][]{ - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5}, - {9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.5} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, - {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} - }); - assertEquals(correctMatrix, matrix.add(0.5)); + assertArrayEquals(new double[0][0], matrix.transpose().copyGrid()); } @Test - public void testSubtraction(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix transformMatrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(1, 1, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - assertEquals(correctMatrix, matrix.subtract(0.5)); + public void testTranspose_size2x0(){ + double[][] grid = new double[2][0]; + DoubleMatrix matrix = new DoubleMatrix(grid); - //Invalid subtracts - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); - final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix3); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - transformMatrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(2, 2, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.0}, - {0.0, 1.0} - }); - assertEquals(correctMatrix, matrix.subtract(0.5)); - - //3x3 - matrix = new DoubleMatrix(grid3); - transformMatrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.0, 2.0}, - {0.0, 1.0, 2.0}, - {0.0, 1.0, 2.0} - }); - assertEquals(correctMatrix, matrix.subtract(0.5)); - - //4x4 - matrix = new DoubleMatrix(grid4); - transformMatrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(4, 4, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.0, 2.0, 3.0}, - {0.0, 1.0, 2.0, 3.0}, - {0.0, 1.0, 2.0, 3.0}, - {0.0, 1.0, 2.0, 3.0} - }); - assertEquals(correctMatrix, matrix.subtract(0.5)); - - //10x10 - matrix = new DoubleMatrix(grid10); - transformMatrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, - {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0} - }); - assertEquals(correctMatrix, matrix.subtract(0.5)); + assertArrayEquals(new double[0][0], matrix.transpose().copyGrid()); } @Test - public void testMultiplication(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); - assertEquals(correctMatrix, matrix.multiply(2.0)); + public void testTranspose_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid multiplication - final DoubleMatrix testMatrix = new DoubleMatrix(grid1); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.multiply(testMatrix2); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - transformMatrix = new DoubleMatrix(transformGrid2_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {3.0, 5.0}, - {3.0, 5.0} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - DoubleMatrix vector = new DoubleMatrix(new double[][]{ - {1.5}, - {2.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {4.5}, - {4.5} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 3.0}, - {1.0, 3.0} - }); - assertEquals(correctMatrix, matrix.multiply(2.0)); - - //3x3 - matrix = new DoubleMatrix(grid3); - transformMatrix = new DoubleMatrix(transformGrid3_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {6.75, 11.25, 15.75}, - {6.75, 11.25, 15.75}, - {6.75, 11.25, 15.75} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new DoubleMatrix(new double[][]{ - {1.5}, - {2.5}, - {3.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {13.25}, - {13.25}, - {13.25} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 3.0, 5.0}, - {1.0, 3.0, 5.0}, - {1.0, 3.0, 5.0} - }); - assertEquals(correctMatrix, matrix.multiply(2.0)); - - //4x4 - matrix = new DoubleMatrix(grid4); - transformMatrix = new DoubleMatrix(transformGrid4_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {20, 12, 4, 0}, - {20, 12, 4, 0}, - {20, 12, 4, 0}, - {20, 12, 4, 0} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new DoubleMatrix(new double[][]{ - {2.5}, - {1.5}, - {0.5}, - {0.0} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {4.75}, - {4.75}, - {4.75}, - {4.75} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 3.0, 5.0, 7.0}, - {1.0, 3.0, 5.0, 7.0}, - {1.0, 3.0, 5.0, 7.0}, - {1.0, 3.0, 5.0, 7.0} - }); - assertEquals(correctMatrix, matrix.multiply(2.0)); - - //10x10 - matrix = new DoubleMatrix(grid10); - transformMatrix = new DoubleMatrix(transformGrid10_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525}, - {75, 125, 175, 225, 275, 325, 375, 425, 475, 525} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new DoubleMatrix(new double[][]{ - {1.5}, - {2.5}, - {3.5}, - {4.5}, - {5.5}, - {6.5}, - {7.5}, - {8.5}, - {9.5}, - {10.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5}, - {382.5} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0}, - {1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0} - }); - assertEquals(correctMatrix, matrix.multiply(2.0)); + assertArrayEquals(new double[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); } @Test - public void testDotProduct(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); - assertEquals(0.75, matrix.dotProduct(transformMatrix), 0.0000001); + public void testTranspose_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); - //Invalid products - DoubleMatrix testMatrix = new DoubleMatrix(grid1); - DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.dotProduct(testMatrix2); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - transformMatrix = new DoubleMatrix(transformGrid2_2); - assertEquals(16, matrix.dotProduct(transformMatrix), 0.0000001); - - //3x3 - matrix = new DoubleMatrix(grid3); - transformMatrix = new DoubleMatrix(transformGrid3_2); - assertEquals(101.25, matrix.dotProduct(transformMatrix), 0.0000001); - - //4x4 - matrix = new DoubleMatrix(grid4); - transformMatrix = new DoubleMatrix(transformGrid4_2); - assertEquals(144, matrix.dotProduct(transformMatrix), 0.0000001); - - //10x10 - matrix = new DoubleMatrix(grid10); - transformMatrix = new DoubleMatrix(transformGrid10_2); - assertEquals(30000, matrix.dotProduct(transformMatrix), 0.0000001); + assertArrayEquals(new double[][]{ + {-1, -11, -21, -31, -41, -51, -61, -71, -81, -91}, + {-2, -12, -22, -32, -42, -52, -62, -72, -82, -92}, + {-3, -13, -23, -33, -43, -53, -63, -73, -83, -93}, + {-4, -14, -24, -34, -44, -54, -64, -74, -84, -94}, + {-5, -15, -25, -35, -45, -55, -65, -75, -85, -95}, + {-6, -16, -26, -36, -46, -56, -66, -76, -86, -96}, + {-7, -17, -27, -37, -47, -57, -67, -77, -87, -97}, + {-8, -18, -28, -38, -48, -58, -68, -78, -88, -98}, + {-9, -19, -29, -39, -49, -59, -69, -79, -89, -99}, + {-10, -20, -30, -40, -50, -60, -70, -80, -90, -100} + }, matrix.transpose().copyGrid()); } @Test - public void testHadamardProduct(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); + public void testTranspose_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); - //Invalid hadamard products - DoubleMatrix testMatrix = new DoubleMatrix(grid1); - DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); - DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix3); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - transformMatrix = new DoubleMatrix(transformGrid2_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.75, 3.75}, - {0.75, 3.75} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //3x3 - matrix = new DoubleMatrix(grid3); - transformMatrix = new DoubleMatrix(transformGrid3_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.75, 3.75, 8.75}, - {0.75, 3.75, 8.75}, - {0.75, 3.75, 8.75} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //4x4 - matrix = new DoubleMatrix(grid4); - transformMatrix = new DoubleMatrix(transformGrid4_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.25, 2.25, 1.25, 0}, - {1.25, 2.25, 1.25, 0}, - {1.25, 2.25, 1.25, 0}, - {1.25, 2.25, 1.25, 0} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //10x10 - matrix = new DoubleMatrix(grid10); - transformMatrix = new DoubleMatrix(transformGrid10_2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75}, - {0.75, 3.75, 8.75, 15.75, 24.75, 35.75, 48.75, 63.75, 80.75, 99.75} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); + assertArrayEquals(new double[][]{ + {-1, -11}, + {-2, -12}, + {-3, -13}, + {-4, -14}, + {-5, -15}, + {-6, -16}, + {-7, -17}, + {-8, -18}, + {-9, -19}, + {-10, -20} + }, matrix.transpose().copyGrid()); } @Test - public void testTranspose(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals(correctMatrix, matrix.transpose()); + public void testTranspose_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 0.5}, - {1.5, 1.5} - }); - assertEquals(correctMatrix, matrix.transpose()); + assertArrayEquals(new double[][]{ + {-1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20} + }, matrix.transpose().copyGrid()); + } - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 0.5, 0.5}, - {1.5, 1.5, 1.5}, - {2.5, 2.5, 2.5} - }); - assertEquals(correctMatrix, matrix.transpose()); + //! determinant() / det() + @Test + public void testDeterminant_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); - //4x4 - matrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 0.5, 0.5, 0.5}, - {1.5, 1.5, 1.5, 1.5}, - {2.5, 2.5, 2.5, 2.5}, - {3.5, 3.5, 3.5, 3.5} + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); }); - assertEquals(correctMatrix, matrix.transpose()); - - //10x10 - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}, - {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}, - {2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5}, - {3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5}, - {4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5}, - {5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5}, - {6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5}, - {7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5}, - {8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5}, - {9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5} + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); }); - assertEquals(correctMatrix, matrix.transpose()); } @Test - public void testDeterminant(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - assertEquals(0.5, matrix.determinant(), 0.0000001); + public void testDeterminant_size1(){ + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); - //Invalid determinants - DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.determinant(); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - assertEquals(0, matrix.determinant(), 0.0000001); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 3.5}, - {3.5, 0.5} - }); - assertEquals(-12.0, matrix.determinant(), 0.0000001); - //det - assertEquals(matrix.determinant(), matrix.det(), 0.0000001); - - //3x3 - matrix = new DoubleMatrix(grid3); - assertEquals(0, matrix.determinant(), 0.0000001); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {1.5, 2.5, 0.5}, - {2.5, 0.5, 1.5} - }); - assertEquals(-13.5, matrix.determinant(), 0.0000001); - - //4x4 - matrix = new DoubleMatrix(grid4); - assertEquals(0, matrix.determinant(), 0.0000001); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5, 0.5}, - {2.5, 3.5, 0.5, 1.5}, - {3.5, 0.5, 1.5, 2.5} - }); - assertEquals(128.0, matrix.determinant(), 0.0000001); - //Column - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 0.0, 2.5, 3.5}, - {0.5, 0.0, 2.5, 3.5}, - {0.0, 0.0, 2.5, 3.5} - }); - assertEquals(0.0, matrix.determinant(), 0.0000001); - //Column2 - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 0.0, 3.5}, - {0.5, 1.5, 0.0, 3.5}, - {0.5, 1.5, 0.0, 3.5}, - {0.0, 1.5, 0.0, 3.5} - }); - assertEquals(0.0, matrix.determinant(), 0.0000001); - - //10x10 - matrix = new DoubleMatrix(grid10); - assertEquals(0, matrix.determinant(), 0.0000001); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, - {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5}, - {2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5}, - {3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5}, - {4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5}, - {5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5}, - {6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5}, - {7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5}, - {8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5}, - {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0}, - }); - assertEquals(-10000000.0, matrix.determinant(), 0.0000001); + assertEquals(1, matrix.determinant()); + assertEquals(1, matrix.det()); } @Test - public void testCofactor(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1}}); - assertEquals(correctMatrix, matrix.cofactor()); + public void testDeterminant_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); - //Invalid cofactor - DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.cofactor(); - }); - - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.5, -0.5}, - {-1.5, 0.5} - }); - assertEquals(correctMatrix, matrix.cofactor()); - //cof - assertEquals(matrix.cofactor(), matrix.cof()); - - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {1.5, 2.5, 0.5}, - {2.5, 0.5, 1.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {3.5, -1, -5.5}, - {-1, -5.5, 3.5}, - {-5.5, 3.5, -1} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //4x4 - matrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(4, 4, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5, 0.5}, - {2.5, 3.5, 0.5, 1.5}, - {3.5, 0.5, 1.5, 2.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {-28, 4, 4, 36}, - {4, 4, 36, -28}, - {4, 36, -28, 4}, - {36, -28, 4, 4} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.cofactor()); - */ + assertEquals(-2, matrix.determinant()); + assertEquals(-2, matrix.det()); } @Test - public void testPower(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.125}}); - assertEquals(correctMatrix, matrix.pow(3)); + public void testDeterminant_size3(){ + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); - //Invalid powers - final DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); - final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.pow(1); + matrix.determinant(); }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + //! cofactor() / cof() + @Test + public void testCofactor_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size1(){ + double[][] expectedGrid = new double[][]{{1}}; + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2(){ + double[][] expectedGrid = new double[][]{{-4, 3}, {2, -1}}; + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size10(){ + double[][] expectedGrid = new double[][]{ + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D} + }; + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + //! adjoint() / adj() + @Test + public void testAdjoint_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size1(){ + double[][] expectedGrid = new double[][]{{1}}; + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2(){ + double[][] expectedGrid = new double[][]{{-4, 2}, {3, -1}}; + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size10(){ + double[][] expectedGrid = new double[][]{ + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D}, + { 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D}, + {-0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D, -0D, 0D} + }; + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + //! inverse() + @Test + public void testInverse_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size1(){ + double[][] expectedGrid = new double[][]{{-1}}; + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size2(){ + double[][] expectedGrid = new double[][]{{-2, 3}, {3, -4}}; + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{4, 3}, {3, 2}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + assertThrows(InvalidScalarException.class, () -> { - testMatrix2.pow(-1); + matrix.inverse(); }); - - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {2, 6}, - {2, 6} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(new double[][]{ - {10.125, 30.375, 50.625}, - {10.125, 30.375, 50.625}, - {10.125, 30.375, 50.625} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //4x4 - //0 - matrix = new DoubleMatrix(grid4); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 1.0, 1.0, 1.0}, - {1.0, 1.0, 1.0, 1.0}, - {1.0, 1.0, 1.0, 1.0}, - {1.0, 1.0, 1.0, 1.0} - }); - assertEquals(correctMatrix, matrix.pow(0)); - //1 - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals(correctMatrix, matrix.pow(1)); - //3 - correctMatrix = new DoubleMatrix(new double[][]{ - {32.0, 96.0, 160.0, 224.0}, - {32.0, 96.0, 160.0, 224.0}, - {32.0, 96.0, 160.0, 224.0}, - {32.0, 96.0, 160.0, 224.0} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //10x10 - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(new double[][]{ - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750}, - {1250, 3750, 6250, 8750, 11250, 13750, 16250, 18750, 21250, 23750} - }); - assertEquals(correctMatrix, matrix.pow(3)); } @Test - public void testAdjoint(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); - assertEquals(correctMatrix, matrix.adjoint()); + public void testInverse_size2x10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2x10); - //2x2 - matrix = new DoubleMatrix(grid2); - correctMatrix = new DoubleMatrix(new double[][]{ - {1.5, -1.5}, - {-0.5, 0.5} - }); - assertEquals(correctMatrix, matrix.adjoint()); - //adj - assertEquals(matrix.adjoint(), matrix.adj()); - - //3x3 - matrix = new DoubleMatrix(grid3); - correctMatrix = new DoubleMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.adjoint()); - - //4x4 - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5, 0.5}, - {2.5, 3.5, 0.5, 1.5}, - {3.5, 0.5, 1.5, 2.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {-28, 4, 4, 36}, - {4, 4, 36, -28}, - {4, 36, -28, 4}, - {36, -28, 4, 4} - }); - assertEquals(correctMatrix, matrix.adjoint()); - - //10x10 - //?Skippng 10x10 test because test took > 5s by itself - /* - matrix = new DoubleMatrix(grid10); - correctMatrix = new DoubleMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.adjoint()); - */ - } - - @Test - public void testInverse(){ - //1x1 - DoubleMatrix matrix = new DoubleMatrix(grid1); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{2.0}}); - assertEquals(correctMatrix, matrix.inverse()); - - //Invalid inverse - DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); - DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{ - {1.0, 2.0, 3.0}, - {1.0, 2.0, 3.0}, - {1.0, 2.0, 3.0} - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.inverse(); + matrix.inverse(); }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.inverse(); - }); - - //2x2 - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {1.5, 0.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {-0.25, 0.75}, - {0.75, -0.25} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //3x3 - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {1.5, 2.5, 0.5}, - {2.5, 0.5, 1.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {-7.0/27.0, 2.0/27.0, 11.0/27.0}, - {2.0/27.0, 11.0/27.0, -7.0/27.0}, - {11.0/27.0, -7.0/27.0, 2.0/27.0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //4x4 - matrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {1.5, 2.5, 3.5, 0.5}, - {2.5, 3.5, 0.5, 1.5}, - {3.5, 0.5, 1.5, 2.5} - }); - correctMatrix = new DoubleMatrix(new double[][]{ - {-0.21875, 0.03125, 0.03125, 0.28125}, - {0.03125, 0.03125, 0.28125, -0.21875}, - {0.03125, 0.28125, -0.21875, 0.03125}, - {0.28125, -0.21875, 0.03125, 0.03125} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //10x10 - //?Skipped 10x10 because it would take a long time to compute } @Test - public void testGenerateIdentity(){ - //0x0 + public void testInverse_size10x2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + //! equals() + @Test + public void testEquals_null(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertFalse(matrix.equals(null)); + } + + @Test + public void testEquals_matrixObject(){ + DoubleMatrix equalsMatrix = new DoubleMatrix(negativeGrid2); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertTrue(matrix.equals((Object)equalsMatrix)); + } + + @Test + public void testEquals_array(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertTrue(matrix.equals(negativeGrid2)); + } + + @Test + public void testEquals_invalidType(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertFalse(matrix.equals(1)); + } + + @Test + public void testEquals_manyRows(){ + DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2}, {-3, -4}, {-5, -6}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewRows(){ + DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_manyCols(){ + DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1, -2, -3}, {-4, -5, -6}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewCols(){ + DoubleMatrix equalsMatrix = new DoubleMatrix(new double[][]{{-1}, {-2}}); + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_notEquals(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + DoubleMatrix equalsMatrix = matrix.transpose(); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_equals(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + DoubleMatrix equalsMatrix = new DoubleMatrix(negativeGrid2); + + assertTrue(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_self(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertTrue(matrix.equals(matrix)); + } + + //! hashCode() + @Test + public void testHashCode_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size1(){ + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + //! toString() + @Test + public void testToString_size0(){ + DoubleMatrix matrix = new DoubleMatrix(); + + assertEquals("[]", matrix.toString()); + } + + @Test + public void testToString_size1(){ + DoubleMatrix matrix = new DoubleMatrix(new double[][]{{1}}); + + assertEquals("[1.0]", matrix.toString()); + } + + @Test + public void testToString_size2(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid2); + + assertEquals("[-1.0, -2.0]\n[-3.0, -4.0]", matrix.toString()); + } + + @Test + public void testToString_size10(){ + DoubleMatrix matrix = new DoubleMatrix(negativeGrid10); + + assertEquals( + "[-1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -10.0]\n" + + "[-11.0, -12.0, -13.0, -14.0, -15.0, -16.0, -17.0, -18.0, -19.0, -20.0]\n" + + "[-21.0, -22.0, -23.0, -24.0, -25.0, -26.0, -27.0, -28.0, -29.0, -30.0]\n" + + "[-31.0, -32.0, -33.0, -34.0, -35.0, -36.0, -37.0, -38.0, -39.0, -40.0]\n" + + "[-41.0, -42.0, -43.0, -44.0, -45.0, -46.0, -47.0, -48.0, -49.0, -50.0]\n" + + "[-51.0, -52.0, -53.0, -54.0, -55.0, -56.0, -57.0, -58.0, -59.0, -60.0]\n" + + "[-61.0, -62.0, -63.0, -64.0, -65.0, -66.0, -67.0, -68.0, -69.0, -70.0]\n" + + "[-71.0, -72.0, -73.0, -74.0, -75.0, -76.0, -77.0, -78.0, -79.0, -80.0]\n" + + "[-81.0, -82.0, -83.0, -84.0, -85.0, -86.0, -87.0, -88.0, -89.0, -90.0]\n" + + "[-91.0, -92.0, -93.0, -94.0, -95.0, -96.0, -97.0, -98.0, -99.0, -100.0]", + matrix.toString()); + } + + //! generateIdentity() + @Test + public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { DoubleMatrix.generateIdentity(0); }); - - //1x1 - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); - assertEquals(correctMatrix, DoubleMatrix.generateIdentity(1)); - - //2x2 - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 0.0}, - {0.0, 1.0} - }); - assertEquals(correctMatrix, DoubleMatrix.generateIdentity(2)); - - //3x3 - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 0.0, 0.0}, - {0.0, 1.0, 0.0}, - {0.0, 0.0, 1.0} - }); - assertEquals(correctMatrix, DoubleMatrix.generateIdentity(3)); - - //4x4 - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 0.0, 0.0, 0.0}, - {0.0, 1.0, 0.0, 0.0}, - {0.0, 0.0, 1.0, 0.0}, - {0.0, 0.0, 0.0, 1.0} - }); - assertEquals(correctMatrix, DoubleMatrix.generateIdentity(4)); - - //10x10 - correctMatrix = new DoubleMatrix(new double[][]{ - {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, - {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0} - }); - assertEquals(correctMatrix, DoubleMatrix.generateIdentity(10)); } @Test - public void testHashCode(){ - DoubleMatrix matrix = new DoubleMatrix(); - assertEquals(Arrays.hashCode(new double[0][0]), matrix.hashCode()); + public void testGenerateIdentity_negativeSize(){ + assertThrows(InvalidGeometryException.class, () -> { + DoubleMatrix.generateIdentity(-1); + }); } @Test - public void testToString(){ - DoubleMatrix matrix = new DoubleMatrix(grid3); - String matrixString = "[0.5,1.5,2.5]\n[0.5,1.5,2.5]\n[0.5,1.5,2.5]"; - assertEquals(matrixString, matrix.toString()); + public void testGenerateIdentity_size2(){ + DoubleMatrix matrix = DoubleMatrix.generateIdentity(2); + + assertArrayEquals(new double[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); } @Test - public void testLaplaceExpansionHelper(){ - DoubleMatrix matrix = new DoubleMatrix(); - matrix.addRow(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix.laplaceExpansionHelper(0, 0); - }); + public void testGenerateIdentity_size3(){ + DoubleMatrix matrix = DoubleMatrix.generateIdentity(3); - DoubleMatrix matrix2 = new DoubleMatrix(); - matrix2.setGrid(grid1); - assertThrows(InvalidGeometryException.class, () -> { - matrix2.laplaceExpansionHelper(0, 0); - }); + assertArrayEquals(new double[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); + } - DoubleMatrix matrix3 = new DoubleMatrix(); - matrix3.setGrid(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(-1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, 2); - }); + @Test + public void testGenerateIdentity_size10(){ + DoubleMatrix matrix = DoubleMatrix.generateIdentity(10); - DoubleMatrix matrix4 = new DoubleMatrix(); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - matrix4.addCol(grid2[1]); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); + assertArrayEquals(new double[][]{ + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + }, matrix.copyGrid()); } } diff --git a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java index b63ca79..a925adb 100644 --- a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java @@ -1,1893 +1,2244 @@ -//Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java -//Mattrixwv -// Created: 02-01-22 -//Modified: 06-30-23 -package com.mattrixwv.matrix; - - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Arrays; - -import org.junit.jupiter.api.Test; - -import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; -import com.mattrixwv.matrix.exceptions.InvalidGeometryException; -import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; -import com.mattrixwv.matrix.exceptions.InvalidScalarException; -import com.mattrixwv.matrix.exceptions.NullMatrixException; - - -public class 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 testConstructor(){ - //Default constructor - IntegerMatrix matrix = new IntegerMatrix(); - assertEquals(0, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - - //Filler constructor - //0 rows - assertThrows(InvalidGeometryException.class, () -> { - new IntegerMatrix(0, 0, 0); - }); - //0 cols - assertThrows(InvalidGeometryException.class, () -> { - new IntegerMatrix(1, 0, 0); - }); - //Good values - matrix = new IntegerMatrix(2, 2, 0); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(0, matrix.get(0, 0)); - assertEquals(0, matrix.get(0, 1)); - assertEquals(0, matrix.get(1, 0)); - assertEquals(0, matrix.get(1, 1)); - - //Matrix constructor - matrix.set(0, 0, 1); - matrix.set(0, 1, 2); - matrix.set(1, 0, 1); - matrix.set(1, 1, 2); - IntegerMatrix matrix2 = new IntegerMatrix(matrix); - assertEquals(2, matrix2.getNumRows()); - assertEquals(2, matrix2.getNumCols()); - assertEquals(1, matrix2.get(0, 0)); - assertEquals(2, matrix2.get(0, 1)); - assertEquals(1, matrix2.get(1, 0)); - assertEquals(2, matrix2.get(1, 1)); - - //Array constructor - //0 length - int[][] grid = new int[0][0]; - matrix = new IntegerMatrix(grid); - assertEquals(0, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //0 cols - grid = new int[1][0]; - matrix = new IntegerMatrix(grid); - assertEquals(1, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //Uneven rows - assertThrows(InvalidRowSizeException.class, () -> { - int[][] grid1 = new int[2][]; - grid1[0] = new int[1]; - grid1[0][0] = 0; - grid1[1] = new int[2]; - grid1[1][0] = 1; - grid1[1][1] = 2; - new IntegerMatrix(grid1); - }); - - //2x2 - grid = grid2; - matrix = new IntegerMatrix(grid); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(1, matrix.get(0, 0)); - assertEquals(2, matrix.get(0, 1)); - assertEquals(1, matrix.get(1, 0)); - assertEquals(2, matrix.get(1, 1)); - } - - @Test - public void testEquals(){ - //Invalid equals - IntegerMatrix matrix = new IntegerMatrix(grid1); - assertNotEquals(matrix, null); - assertNotEquals(matrix, new double[0]); - - //1x1 - matrix = new IntegerMatrix(grid1); - boolean gridEquals = matrix.equals(matrix); - assertTrue(gridEquals); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals1 = matrix.equals(grid1); - assertTrue(gridEquals1); - - //2x2 - matrix = new IntegerMatrix(grid2); - boolean gridEquals2 = matrix.equals(matrix); - assertTrue(gridEquals2); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals21 = matrix.equals(grid2); - assertTrue(gridEquals21); - //false - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals22 = matrix.equals(transformGrid2_1); - assertFalse(gridEquals22); - gridEquals2 = matrix.equals(new IntegerMatrix(grid3)); - assertFalse(gridEquals2); - gridEquals = matrix.equals(new IntegerMatrix(new int[][]{ - {0, 1, 2}, - {0, 1, 2} - })); - assertFalse(gridEquals2); - - //3x3 - matrix = new IntegerMatrix(grid3); - boolean gridEquals3 = matrix.equals(matrix); - assertTrue(gridEquals3); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals31 = matrix.equals(grid3); - assertTrue(gridEquals31); - - //4x4 - matrix = new IntegerMatrix(grid4); - boolean gridEquals4 = matrix.equals(matrix); - assertTrue(gridEquals4); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals41 = matrix.equals(grid4); - assertTrue(gridEquals41); - - //10x10 - matrix = new IntegerMatrix(grid10); - boolean gridEquals10 = matrix.equals(matrix); - assertTrue(gridEquals10); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals101 = matrix.equals(grid10); - assertTrue(gridEquals101); - } - - @Test - public void testGet(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - assertEquals(1, matrix.get(0, 0)); - - //Invalid gets - final IntegerMatrix testMatrix = new IntegerMatrix(matrix); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(3, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(-1, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, -1); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - assertEquals(1, matrix.get(0, 0)); - - //3x3 - matrix = new IntegerMatrix(grid3); - assertEquals(1, matrix.get(0, 0)); - - //4x4 - matrix = new IntegerMatrix(grid4); - assertEquals(1, matrix.get(0, 0)); - - //10x10 - matrix = new IntegerMatrix(grid10); - assertEquals(1, matrix.get(0, 0)); - } - - @Test - public void testGetRow(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //Invalid gets - final IntegerMatrix testMatrix = new IntegerMatrix(matrix); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(-1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(3); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{{1, 2}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //4x4 - matrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //10x10 - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); - assertEquals(correctMatrix, matrix.getRow(0)); - } - - @Test - public void testGetCol(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, matrix.getCol(0)); - - //Invalid gets - final IntegerMatrix testMatrix = new IntegerMatrix(); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(-1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(3); - }); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid1); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix2.getCol(3); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //4x4 - matrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1}, - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //10x10 - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - } - - @Test - public void testSet(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - matrix.set(0, 0, 2); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); - assertEquals(correctMatrix, matrix); - - //Invalid sets - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(-1, -1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(2, 2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, -1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, 2, 0); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - matrix.set(0, 0, 3); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new IntegerMatrix(grid3); - matrix.set(0, 0, 3); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new IntegerMatrix(grid4); - matrix.set(0, 0, 3); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new IntegerMatrix(grid10); - matrix.set(0, 0, 3); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix); - } - - @Test - public void testSetRow(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - matrix.setRow(0, new int[]{0}); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); - assertEquals(correctMatrix, matrix); - - //Invalid setRows - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final int[] testGrid = {0, 0}; - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, (int[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setRow(0, (IntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - matrix.setRow(1, new int[]{2, 1}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {2, 1} - }); - assertEquals(correctMatrix, matrix); - //Matrix - IntegerMatrix matrix2 = new IntegerMatrix(new int[][]{{0, 0}}); - matrix.setRow(1, matrix2); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {0, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new IntegerMatrix(grid3); - matrix.setRow(0, new int[]{0, 1, 2}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new IntegerMatrix(grid4); - matrix.setRow(0, new int[]{4, 3, 2, 1}); - correctMatrix = new IntegerMatrix(new int[][]{ - {4, 3, 2, 1}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new IntegerMatrix(grid10); - matrix.setRow(0, new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix); - } - - @Test - public void testSetCol(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - matrix.setCol(0, new int[]{1}); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, matrix); - - //Invalid setCols - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(); - final int[] testGrid = {0, 0}; - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, (int[])null); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(0, testGrid); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setCol(0, (IntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - matrix.setCol(0, new int[]{3, 3}); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2}, - {3, 2} - }); - assertEquals(correctMatrix, matrix); - //Matrix - IntegerMatrix vector = new IntegerMatrix(new int[][]{{0}, {0}}); - matrix.setCol(1, vector); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 0}, - {3, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new IntegerMatrix(grid3); - matrix.setCol(0, new int[]{0, 0, 0}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 2, 3}, - {0, 2, 3}, - {0, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new IntegerMatrix(grid4); - matrix.setCol(0, new int[]{0, 0, 0, 0}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new IntegerMatrix(grid10); - matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); - correctMatrix = new IntegerMatrix(new int[][]{ - {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}, - {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} - }); - assertEquals(correctMatrix, matrix); - } - - @Test - public void testAddRow(){ - //0x0 - IntegerMatrix matrix = new IntegerMatrix(); - matrix.addRow(new int[]{0}); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); - assertEquals(correctMatrix, matrix); - - //1x1 - matrix = new IntegerMatrix(grid1); - matrix.addRow(new int[]{1}); - correctMatrix = new IntegerMatrix(new int[][]{{1}, {1}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(new int[]{0, 0}); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((int[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((IntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - matrix.addRow(new int[]{1, 2}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new IntegerMatrix(grid2); - matrix.addRow(new IntegerMatrix(new int[][]{{0, 0}})); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {0, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new IntegerMatrix(grid3); - matrix.addRow(new int[]{1, 2, 3}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new IntegerMatrix(grid4); - matrix.addRow(new int[]{1, 2, 3, 4}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new IntegerMatrix(grid10); - matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - 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}, - {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} - }); - assertEquals(correctMatrix, matrix); - } - - @Test - public void testAddCol(){ - //0x0 - IntegerMatrix matrix = new IntegerMatrix(); - matrix.addCol(new int[]{0}); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); - assertEquals(correctMatrix, matrix); - - //1x1 - matrix = new IntegerMatrix(grid1); - matrix.addCol(new int[]{1}); - correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(new int[]{0, 0}); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((int[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((IntegerMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - matrix.addCol(new int[]{3, 3}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new IntegerMatrix(grid2); - matrix.addCol(new IntegerMatrix(new int[][]{{0}, {0}})); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 0}, - {1, 2, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new IntegerMatrix(grid3); - matrix.addCol(new int[]{4, 4, 4}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new IntegerMatrix(grid4); - matrix.addCol(new int[]{5, 5, 5, 5}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new IntegerMatrix(grid10); - matrix.addCol(new int[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix); - } - - @Test - public void testAppendRight(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix secondMatrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); - - //Invalid appends - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendRight(testMatrix2); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.appendRight(null); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - secondMatrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 1, 2}, - {1, 2, 1, 2} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); - - //3x3 - matrix = new IntegerMatrix(grid3); - secondMatrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 1, 2, 3}, - {1, 2, 3, 1, 2, 3}, - {1, 2, 3, 1, 2, 3} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); - - //4x4 - matrix = new IntegerMatrix(grid4); - secondMatrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); - - //10x10 - matrix = new IntegerMatrix(grid10); - secondMatrix = new IntegerMatrix(grid10); - 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}, - {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} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); - } - - @Test - public void testAppendBottom(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix secondMatrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //Invalid appends - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendBottom(testMatrix2); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.appendBottom(null); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - secondMatrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //3x3 - matrix = new IntegerMatrix(grid3); - secondMatrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //4x4 - matrix = new IntegerMatrix(grid4); - secondMatrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //10x10 - matrix = new IntegerMatrix(grid10); - secondMatrix = new IntegerMatrix(grid10); - 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}, - {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}, - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - } - - @Test - public void testIsSquare(){ - IntegerMatrix matrix = new IntegerMatrix(); - assertFalse(matrix.isSquare()); - - matrix = new IntegerMatrix(2, 2, 0); - assertTrue(matrix.isSquare()); - - matrix = new IntegerMatrix(2, 3, 0); - assertFalse(matrix.isSquare()); - } - - @Test - public void testAddition(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - assertEquals(correctMatrix, matrix.add(1)); - - //Invalid adds - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); - final IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix3); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - transformMatrix = new IntegerMatrix(transformGrid2_1); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 2}, - {2, 2} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 3}, - {2, 3} - }); - assertEquals(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(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 3, 4}, - {2, 3, 4}, - {2, 3, 4} - }); - assertEquals(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(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(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(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(correctMatrix, matrix.add(1)); - } - - @Test - public void testSubtraction(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix transformMatrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{ - {0} - }); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - assertEquals(correctMatrix, matrix.subtract(1)); - - //Invalid subtracts - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); - final IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix3); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - transformMatrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(2, 2, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 1}, - {0, 1} - }); - assertEquals(correctMatrix, matrix.subtract(1)); - - //3x3 - matrix = new IntegerMatrix(grid3); - transformMatrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 1, 2}, - {0, 1, 2}, - {0, 1, 2} - }); - assertEquals(correctMatrix, matrix.subtract(1)); - - //4x4 - matrix = new IntegerMatrix(grid4); - transformMatrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(4, 4, 0); - assertEquals(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(correctMatrix, matrix.subtract(1)); - - //10x10 - matrix = new IntegerMatrix(grid10); - transformMatrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(10, 10, 0); - assertEquals(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(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(correctMatrix, matrix.multiply(transformMatrix)); - assertEquals(correctMatrix, matrix.multiply(2)); - - //Invalid multiplication - final IntegerMatrix testMatrix = new IntegerMatrix(grid1); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.multiply(testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - transformMatrix = new IntegerMatrix(transformGrid2_2); - correctMatrix = new IntegerMatrix(new int[][]{ - {6, 9}, - {6, 9} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - IntegerMatrix vector = new IntegerMatrix(new int[][]{ - {2}, - {3} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {8}, - {8} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 4}, - {2, 4} - }); - assertEquals(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(correctMatrix, matrix.multiply(transformMatrix)); - vector = new IntegerMatrix(new int[][]{ - {2}, - {3}, - {4} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {20}, - {20}, - {20} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 4, 6}, - {2, 4, 6}, - {2, 4, 6} - }); - assertEquals(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(correctMatrix, matrix.multiply(transformMatrix)); - vector = new IntegerMatrix(new int[][]{ - {2}, - {3}, - {4}, - {5} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {40}, - {40}, - {40}, - {40} - }); - assertEquals(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(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(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(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(correctMatrix, matrix.multiply(2)); - } - - @Test - public void testDotProduct(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2); - assertEquals(2, matrix.dotProduct(transformMatrix)); - - //Invalid products - IntegerMatrix testMatrix = new IntegerMatrix(grid1); - IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.dotProduct(testMatrix2); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - transformMatrix = new IntegerMatrix(transformGrid2_2); - assertEquals(30, matrix.dotProduct(transformMatrix)); - - //3x3 - matrix = new IntegerMatrix(grid3); - transformMatrix = new IntegerMatrix(transformGrid3_2); - assertEquals(162, matrix.dotProduct(transformMatrix)); - - //4x4 - matrix = new IntegerMatrix(grid4); - transformMatrix = new IntegerMatrix(transformGrid4_2); - assertEquals(560, matrix.dotProduct(transformMatrix)); - - //10x10 - matrix = new IntegerMatrix(grid10); - transformMatrix = new IntegerMatrix(transformGrid10_2); - assertEquals(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(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //Invalid hadamard products - IntegerMatrix testMatrix = new IntegerMatrix(grid1); - IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); - IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix3); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - transformMatrix = new IntegerMatrix(transformGrid2_2); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, 6}, - {2, 6} - }); - assertEquals(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(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(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(correctMatrix, matrix.hadamardProduct(transformMatrix)); - } - - @Test - public void testTranspose(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, matrix.transpose()); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 1}, - {2, 2} - }); - assertEquals(correctMatrix, matrix.transpose()); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 1, 1}, - {2, 2, 2}, - {3, 3, 3} - }); - assertEquals(correctMatrix, matrix.transpose()); - - //4x4 - matrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 1, 1, 1}, - {2, 2, 2, 2}, - {3, 3, 3, 3}, - {4, 4, 4, 4} - }); - assertEquals(correctMatrix, matrix.transpose()); - - //10x10 - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(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}, - }); - assertEquals(correctMatrix, matrix.transpose()); - } - - @Test - public void testDeterminant(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - assertEquals(1, matrix.determinant()); - - //Invalid determinants - IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.determinant(); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - assertEquals(0, matrix.determinant()); - matrix = new IntegerMatrix(new int[][]{ - {1, 4}, - {4, 1} - }); - assertEquals(-15, matrix.determinant()); - //det - assertEquals(matrix.determinant(), matrix.det()); - - //3x3 - matrix = new IntegerMatrix(grid3); - assertEquals(0, matrix.determinant()); - matrix = new IntegerMatrix(new int[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - assertEquals(-21, matrix.determinant()); - - //4x4 - matrix = new IntegerMatrix(grid4); - assertEquals(0, matrix.determinant()); - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - assertEquals(160, matrix.determinant()); - //Column - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {1, 0, 3, 4}, - {1, 0, 3, 4}, - {0, 0, 3, 4} - }); - assertEquals(0, matrix.determinant()); - //Column2 - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 0, 4}, - {1, 2, 0, 4}, - {1, 2, 0, 4}, - {0, 2, 0, 4}, - }); - assertEquals(0, matrix.determinant()); - - //10x10 - matrix = new IntegerMatrix(grid10); - assertEquals(0, matrix.determinant()); - matrix = new IntegerMatrix(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} - }); - assertEquals(-10000000, matrix.determinant()); - } - - @Test - public void testCofactor(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.cofactor()); - - //Invalid cofactor - IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.cofactor(); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, -1}, - {-2, 1} - }); - assertEquals(correctMatrix, matrix.cofactor()); - //cof - assertEquals(matrix.cofactor(), matrix.cof()); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new IntegerMatrix(new int[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {7, 0, -14}, - {-6, -6, 15}, - {-4, 3, -4} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //4x4 - matrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(4, 4, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {-36, 4, 4, 44}, - {4, 4, 44, -36}, - {4, 44, -36, 4}, - {44, -36, 4, 4} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.cofactor()); - */ - } - - @Test - public void testPower(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, matrix.pow(3)); - - //Invalid powers - final IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0}, {0}}); - final IntegerMatrix testMatrix2 = new IntegerMatrix(grid1); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.pow(1); - }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.pow(-1); - }); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {9, 18}, - {9, 18} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(new int[][]{ - {36, 72, 108}, - {36, 72, 108}, - {36, 72, 108} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //4x4 - //0 - matrix = new IntegerMatrix(grid4); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 1, 1, 1}, - {1, 1, 1, 1}, - {1, 1, 1, 1}, - {1, 1, 1, 1} - }); - assertEquals(correctMatrix, matrix.pow(0)); - //1 - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix.pow(1)); - //3 - correctMatrix = new IntegerMatrix(new int[][]{ - {100, 200, 300, 400}, - {100, 200, 300, 400}, - {100, 200, 300, 400}, - {100, 200, 300, 400} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //10x10 - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(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} - }); - assertEquals(correctMatrix, matrix.pow(3)); - } - - @Test - public void testAdjoint(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.adjoint()); - - //2x2 - matrix = new IntegerMatrix(grid2); - correctMatrix = new IntegerMatrix(new int[][]{ - {2, -2}, - {-1, 1} - }); - assertEquals(correctMatrix, matrix.adjoint()); - //adj - assertEquals(matrix.adjoint(), matrix.adj()); - - //3x3 - matrix = new IntegerMatrix(grid3); - correctMatrix = new IntegerMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.adjoint()); - - //4x4 - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {-36, 4, 4, 44}, - {4, 4, 44, -36}, - {4, 44, -36, 4}, - {44, -36, 4, 4} - }); - assertEquals(correctMatrix, matrix.adjoint()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new IntegerMatrix(grid10); - correctMatrix = new IntegerMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.adjoint()); - */ - } - - @Test - public void testInverse(){ - //1x1 - IntegerMatrix matrix = new IntegerMatrix(grid1); - IntegerMatrix correctMatrix = new IntegerMatrix(grid1); - assertEquals(correctMatrix, matrix.inverse()); - - //Invalid inverse - IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); - IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.inverse(); - }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.inverse(); - }); - - //2x2 - matrix = new IntegerMatrix(new int[][]{ - {1, 4}, - {4, 1} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {-0, 0}, - {0, -0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //3x3 - matrix = new IntegerMatrix(new int[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {-0, 0, 0}, - {0, 0, -0}, - {0, -0, 0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //4x4 - matrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new IntegerMatrix(new int[][]{ - {-0, 0, 0, 0}, - {0, 0, 0, -0}, - {0, 0, -0, 0}, - {0, -0, 0, 0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //10x10 - //?Skipped 10x10 because it would take a long time to compute - } - - @Test - public void testGenerateIdentity(){ - //0x0 - assertThrows(InvalidGeometryException.class, () -> { - IntegerMatrix.generateIdentity(0); - }); - - //1x1 - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals(correctMatrix, IntegerMatrix.generateIdentity(1)); - - //2x2 - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 0}, - {0, 1} - }); - assertEquals(correctMatrix, IntegerMatrix.generateIdentity(2)); - - //3x3 - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1} - }); - assertEquals(correctMatrix, IntegerMatrix.generateIdentity(3)); - - //4x4 - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - }); - assertEquals(correctMatrix, IntegerMatrix.generateIdentity(4)); - - //10x10 - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - }); - assertEquals(correctMatrix, IntegerMatrix.generateIdentity(10)); - } - - @Test - public void testHashCode(){ - IntegerMatrix matrix = new IntegerMatrix(); - assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); - } - - @Test - public void testToString(){ - IntegerMatrix matrix = new IntegerMatrix(grid3); - String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; - assertEquals(matrixString, matrix.toString()); - } - - @Test - public void testLaplaceExpansionHelper(){ - IntegerMatrix matrix = new IntegerMatrix(); - matrix.addRow(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix.laplaceExpansionHelper(0, 0); - }); - - IntegerMatrix matrix2 = new IntegerMatrix(); - matrix2.setGrid(grid1); - assertThrows(InvalidGeometryException.class, () -> { - matrix2.laplaceExpansionHelper(0, 0); - }); - - IntegerMatrix matrix3 = new IntegerMatrix(); - matrix3.setGrid(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(-1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, 2); - }); - - IntegerMatrix matrix4 = new IntegerMatrix(); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - matrix4.addCol(grid2[1]); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - } -} +//Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +//Mattrixwv +// Created: 02-01-22 +//Modified: 08-10-24 +package com.mattrixwv.matrix; + + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; +import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; +import com.mattrixwv.matrix.exceptions.InvalidScalarException; +import com.mattrixwv.matrix.exceptions.NullMatrixException; + + +public class TestIntegerMatrix{ + private static final int[][] negativeGrid2 = new int[][]{ + {-1, -2}, + {-3, -4} + }; + private static final int[][] negativeGrid10 = new int[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} + }; + private static final int[][] negativeGrid2x10 = new int[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} + }; + private static final int[][] negativeGrid10x2 = new int[][]{ + { -1, -2}, + { -3, -4}, + { -5, -6}, + { -7, -8}, + { -9, -10}, + {-11, -12}, + {-13, -14}, + {-15, -16}, + {-17, -18}, + {-19, -20} + }; + + + //! Constructor + @Test + public void testConstructor_default(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_fill0Rows(){ + assertThrows(InvalidGeometryException.class, () -> { + new IntegerMatrix(0, 1, 0); + }); + } + + @Test + public void testConstructor_fill0Cols(){ + assertThrows(InvalidGeometryException.class, () -> { + new IntegerMatrix(1, 0, 0); + }); + } + + @Test + public void testConstructor_fillSize2(){ + IntegerMatrix matrix = new IntegerMatrix(2, 2, -1); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-1, matrix.get(0, 1)); + assertEquals(-1, matrix.get(1, 0)); + assertEquals(-1, matrix.get(1, 1)); + } + + @Test + public void testConstructor_fillSize10(){ + IntegerMatrix matrix = new IntegerMatrix(10, 10, -1); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_arraySize0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_arraySize2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arrayUnevenRows(){ + int[][] grid = new int[][]{ + {-1, -2, -3}, + {-4, -5}, + {-6, -7, -8, -9}, + {-10, -11, -12} + }; + + assertThrows(InvalidRowSizeException.class, () -> { + new IntegerMatrix(grid); + }); + } + + @Test + public void testConstructor_matrixSize0(){ + IntegerMatrix originalMatrix = new IntegerMatrix(); + IntegerMatrix matrix = new IntegerMatrix(originalMatrix); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_matrixSize2(){ + IntegerMatrix originalMatrix = new IntegerMatrix(2, 2, -1); + IntegerMatrix matrix = new IntegerMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10(){ + IntegerMatrix originalMatrix = new IntegerMatrix(10, 10, -1); + IntegerMatrix matrix = new IntegerMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize2x10(){ + IntegerMatrix originalMatrix = new IntegerMatrix(2, 10, -1); + IntegerMatrix matrix = new IntegerMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10x2(){ + IntegerMatrix originalMatrix = new IntegerMatrix(10, 2, -1); + IntegerMatrix matrix = new IntegerMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(-1, num); + } + } + } + + //! setGrid() + @Test + public void testSetGrid_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(grid); + + assertEquals(2, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2(){ + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10(){ + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(); + matrix.setGrid(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + //! isSquare() + @Test + public void testIsSquare_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertFalse(matrix.isSquare()); + } + + //! laplaceExpansionHelper() + @Test + public void testLaplaceExpansionHelper_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + assertArrayEquals(new int[][]{{-4}}, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(-1, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(2, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, -1); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, 2); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc0x0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + int[][] expectedGrid = new int[][]{ + {-12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-92, -93, -94, -95, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc4x4(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + IntegerMatrix result = matrix.laplaceExpansionHelper(4, 4); + + int[][] expectedGrid = new int[][]{ + { -1, -2, -3, -4, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -36, -37, -38, -39, -40}, + {-51, -52, -53, -54, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + //! get() + @Test + public void testGet_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(2, 0); + }); + } + + @Test + public void testGet_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(-1, 0); + }); + } + + @Test + public void testGet_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, 2); + }); + } + + @Test + public void testGet_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, -1); + }); + } + + @Test + public void testGet(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-2, matrix.get(0, 1)); + assertEquals(-3, matrix.get(1, 0)); + assertEquals(-4, matrix.get(1, 1)); + } + + //! getRow() + @Test + public void testGetRow_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getRow(2); + }); + } + + @Test + public void testGetRow_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getRow(-1); + }); + } + + @Test + public void testGetRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(new int[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); + assertArrayEquals(new int[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); + } + + //! getColumn() + @Test + public void testGetCol_0Rows(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(0); + }); + } + + @Test + public void testGetCol_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(2); + }); + } + + @Test + public void testGetCol_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(-1); + }); + } + + @Test + public void testGetCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(new int[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); + assertArrayEquals(new int[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); + } + + //! getNumRows() + @Test + public void testGetNumRows_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + } + + //! getNumCols() + @Test + public void testGetNumCols_size0(){ + int[][] grid = new int[0][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertEquals(2, matrix.getNumCols()); + } + + //! set() + @Test + public void testSet_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(-1, 0, 0); + }); + } + + @Test + public void testSet_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(2, 0, 0); + }); + } + + @Test + public void testSet_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, -1, 0); + }); + } + + @Test + public void testSet_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, 2, 0); + }); + } + + @Test + public void testSet(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + matrix.set(0, 0, -5); + matrix.set(0, 1, -6); + matrix.set(1, 0, -7); + matrix.set(1, 1, -8); + assertEquals(-5, matrix.get(0, 0)); + assertEquals(-6, matrix.get(0, 1)); + assertEquals(-7, matrix.get(1, 0)); + assertEquals(-8, matrix.get(1, 1)); + } + + //! setRow() + @Test + public void testSetRow_array_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new int[]{0}); + }); + } + + @Test + public void testSetRow_array_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new int[]{0}); + }); + } + + @Test + public void testSetRow_array_nullArray(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, (int[])null); + }); + } + + @Test + public void testSetRow_array_arrayLength0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new int[0]); + }); + } + + @Test + public void testSetRow_array_invalidArrayLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new int[]{0, 0, 0}); + }); + } + + @Test + public void testSetRow_array(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + matrix.setRow(0, new int[]{-5, -6}); + matrix.setRow(1, new int[]{-7, -8}); + assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.setRow(0, (IntegerMatrix)null); + }); + } + + @Test + public void testSetRow_matrix_multipleRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new IntegerMatrix(new int[][]{{0, 0}, {0, 0}})); + }); + } + + @Test + public void testSetRow_matrix_negativeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new IntegerMatrix(new int[][]{{0}})); + }); + } + + @Test + public void testSetRow_matrix_largeRow(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new IntegerMatrix(new int[][]{{0}})); + }); + } + + @Test + public void testSetRow_matrix_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new IntegerMatrix(new int[][]{{}})); + }); + } + + @Test + public void testSetRow_matrix_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new IntegerMatrix(new int[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetRow_matrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + matrix.setRow(0, new IntegerMatrix(new int[][]{{-5, -6}})); + matrix.setRow(1, new IntegerMatrix(new int[][]{{-7, -8}})); + assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! setCol() + @Test + public void testSetCol_array_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new int[]{0}); + }); + } + + @Test + public void testSetCol_array_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new int[]{0}); + }); + } + + @Test + public void testSetCol_array_nullArray(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, (int[])null); + }); + } + + @Test + public void testSetCol_array_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new int[0]); + }); + } + + @Test + public void testSetCol_array_invalidArrayLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new int[]{0, 0, 0}); + }); + } + + @Test + public void testSetCol_array(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + matrix.setCol(0, new int[]{-5, -7}); + matrix.setCol(1, new int[]{-6, -8}); + assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.setCol(0, (IntegerMatrix)null); + }); + } + + @Test + public void testSetCol_matrix_multipleCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testSetCol_matrix_negativeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new IntegerMatrix(new int[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_largeCol(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new IntegerMatrix(new int[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new IntegerMatrix(new int[][]{{}})); + }); + } + + @Test + public void testSetCol_matrix_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new IntegerMatrix(new int[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetCol_matrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + matrix.setCol(0, new IntegerMatrix(new int[][]{{-5}, {-7}})); + matrix.setCol(1, new IntegerMatrix(new int[][]{{-6}, {-8}})); + assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! addRow() + @Test + public void testAddRow_array_nullArray(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((int[])null); + }); + } + + @Test + public void testAddRow_array_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new int[0]); + }); + } + + @Test + public void testAddRow_array_invalidArrayLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new int[]{0, 0, 0}); + }); + } + + @Test + public void testAddRow_array(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix.addRow(new int[]{-5, -6}); + + assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((IntegerMatrix)null); + }); + } + + @Test + public void testAddRow_matrix_multipleRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}})); + }); + } + + @Test + public void testAddRow_matrix_noRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new IntegerMatrix(new int[0][0])); + }); + } + + @Test + public void testAddRow_matrix_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new IntegerMatrix(new int[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddRow_matrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix.addRow(new IntegerMatrix(new int[][]{{-5, -6}})); + + assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + //! addCol() + @Test + public void testAddCol_array_nullArray(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((int[])null); + }); + } + + @Test + public void testAddCol_array_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new int[0]); + }); + } + + @Test + public void testAddCol_array_invalidArrayLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new int[]{0, 0, 0}); + }); + } + + @Test + public void testAddCol_array(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix.addCol(new int[]{-5, -6}); + + assertArrayEquals(new int[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((IntegerMatrix)null); + }); + } + + @Test + public void testAddCol_matrix_multipleCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testAddCol_matrix_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new IntegerMatrix(new int[][]{{}})); + }); + } + + @Test + public void testAddCol_matrix_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new IntegerMatrix(new int[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddCol_matrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix.addCol(new IntegerMatrix(new int[][]{{-5}, {-6}})); + + assertArrayEquals(new int[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + //! appendRight() + @Test + public void testAppendRight_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendRight((IntegerMatrix)null); + }); + } + + @Test + public void testAppendRight_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new IntegerMatrix(new int[0][0])); + }); + } + + @Test + public void testAppendRight_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new IntegerMatrix(new int[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAppendRight(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.appendRight(matrix); + + assertArrayEquals(new int[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); + } + + //! appendBottom() + @Test + public void testAppendBottom_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendBottom((IntegerMatrix)null); + }); + } + + @Test + public void testAppendBottom_length0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new IntegerMatrix(new int[0][0])); + }); + } + + @Test + public void testAppendBottom_invalidLength(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new IntegerMatrix(new int[][]{{0}, {0}, {0}})); + }); + } + + @Test + public void testAppendBottom(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.appendBottom(matrix); + + assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); + } + + //! add() + @Test + public void testAdd_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.add((IntegerMatrix)null); + }); + } + + @Test + public void testAdd_matrix_fewRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testAdd_matrix_manyRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new IntegerMatrix(new int[][]{{0}, {0}})); + }); + } + + @Test + public void testAdd_matrix_fewCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new IntegerMatrix(new int[][]{{0}})); + }); + } + + @Test + public void testAdd_matrix_manyCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testAdd_matrix_size2(){ + IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{{-4, -3}, {-2, -1}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new int[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); + } + + @Test + public void testAdd_matrix_size10(){ + IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new int[][]{ + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101} + }, matrix.copyGrid()); + } + + @Test + public void testAdd_scalar(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.add(1); + + assertArrayEquals(new int[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); + } + + //! subtract() + @Test + public void testSubtract_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.subtract((IntegerMatrix)null); + }); + } + + @Test + public void testSubtract_matrix_fewRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_manyRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_fewCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new IntegerMatrix(new int[][]{{0}, {0}})); + }); + } + + @Test + public void testSubtract_matrix_manyCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_size2(){ + IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{{-4, -3}, {-2, -1}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_matrix_size10(){ + IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{ + { 99, 97, 95, 93, 91, 89, 87, 85, 83, 81}, + { 79, 77, 75, 73, 71, 69, 67, 65, 63, 61}, + { 59, 57, 55, 53, 51, 49, 47, 45, 43, 41}, + { 39, 37, 35, 33, 31, 29, 27, 25, 23, 21}, + { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, + { -1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-21, -23, -25, -27, -29, -31, -33, -35, -37, -39}, + {-41, -43, -45, -47, -49, -51, -53, -55, -57, -59}, + {-61, -63, -65, -67, -69, -71, -73, -75, -77, -79}, + {-81, -83, -85, -87, -89, -91, -93, -95, -97, -99} + }, matrix.copyGrid()); + } + + @Test + public void testSubtract_scalar(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.subtract(1); + + assertArrayEquals(new int[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); + } + + //! multiply() + @Test + public void testMultiply_matrix_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.multiply((IntegerMatrix)null); + }); + } + + @Test + public void testMultiply_matrix_manyRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_fewRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_square(){ + IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_matrix_rectangle(){ + IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_scalar(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + matrix = matrix.multiply(2); + + assertArrayEquals(new int[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); + } + + //! pow + @Test + public void testPow_rectangle(){ + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.pow(2); + }); + } + + @Test + public void testPow_negative(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidScalarException.class, () -> { + matrix.pow(-1); + }); + } + + @Test + public void testPow_0(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + IntegerMatrix result = matrix.pow(0); + + assertEquals(new IntegerMatrix(2, 2, 1), result); + } + + @Test + public void testPow_2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + IntegerMatrix result = matrix.pow(2); + + assertEquals(new IntegerMatrix(new int[][]{{7, 10}, {15, 22}}), result); + } + + @Test + public void testPow_3(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + IntegerMatrix result = matrix.pow(3); + + assertEquals(new IntegerMatrix(new int[][]{{-37, -54}, {-81, -118}}), result); + } + + //! dotProduct() + @Test + public void testDotProduct_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.dotProduct((IntegerMatrix)null); + }); + } + + @Test + public void testDotProduct_fewRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testDotProduct_manyRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testDotProduct_size2(){ + IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(-54, result); + } + + @Test + public void testDotProduct_size10(){ + IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(-2632750, result); + } + + //! hadamardProduct() + @Test + public void testHadamardProduct_nullMatrix(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.hadamardProduct((IntegerMatrix)null); + }); + } + + @Test + public void testHadamardProduct_fewRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0}})); + }); + } + + @Test + public void testHadamardProduct_manyRows(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testHadamardProduct_fewCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0}, {0}})); + }); + } + + @Test + public void testHadamardProduct_manyCols(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testHadamardProduct_size2(){ + IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + IntegerMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_size10(){ + IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + IntegerMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{ + { -1, -4, -9, -16, -25, -36, -49, -64, -81, -100}, + { -121, -144, -169, -196, -225, -256, -289, -324, -361, -400}, + { -441, -484, -529, -576, -625, -676, -729, -784, -841, -900}, + { -961, -1024, -1089, -1156, -1225, -1296, -1369, -1444, -1521, -1600}, + {-1681, -1764, -1849, -1936, -2025, -2116, -2209, -2304, -2401, -2500}, + {-2601, -2704, -2809, -2916, -3025, -3136, -3249, -3364, -3481, -3600}, + {-3721, -3844, -3969, -4096, -4225, -4356, -4489, -4624, -4761, -4900}, + {-5041, -5184, -5329, -5476, -5625, -5776, -5929, -6084, -6241, -6400}, + {-6561, -6724, -6889, -7056, -7225, -7396, -7569, -7744, -7921, -8100}, + {-8281, -8464, -8649, -8836, -9025, -9216, -9409, -9604, -9801, -10000} + }, result.copyGrid()); + } + + //! transpose() + @Test + public void testTranspose_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + IntegerMatrix result = matrix.transpose(); + + assertEquals(new IntegerMatrix(), result); + } + + @Test + public void testTranspose_size0x2(){ + int[][] grid = new int[0][2]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2x0(){ + int[][] grid = new int[2][0]; + IntegerMatrix matrix = new IntegerMatrix(grid); + + assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(new int[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertArrayEquals(new int[][]{ + {-1, -11, -21, -31, -41, -51, -61, -71, -81, -91}, + {-2, -12, -22, -32, -42, -52, -62, -72, -82, -92}, + {-3, -13, -23, -33, -43, -53, -63, -73, -83, -93}, + {-4, -14, -24, -34, -44, -54, -64, -74, -84, -94}, + {-5, -15, -25, -35, -45, -55, -65, -75, -85, -95}, + {-6, -16, -26, -36, -46, -56, -66, -76, -86, -96}, + {-7, -17, -27, -37, -47, -57, -67, -77, -87, -97}, + {-8, -18, -28, -38, -48, -58, -68, -78, -88, -98}, + {-9, -19, -29, -39, -49, -59, -69, -79, -89, -99}, + {-10, -20, -30, -40, -50, -60, -70, -80, -90, -100} + }, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertArrayEquals(new int[][]{ + {-1, -11}, + {-2, -12}, + {-3, -13}, + {-4, -14}, + {-5, -15}, + {-6, -16}, + {-7, -17}, + {-8, -18}, + {-9, -19}, + {-10, -20} + }, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertArrayEquals(new int[][]{ + {-1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20} + }, matrix.transpose().copyGrid()); + } + + //! determinant() / det() + @Test + public void testDeterminant_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size1(){ + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); + + assertEquals(1, matrix.determinant()); + assertEquals(1, matrix.det()); + } + + @Test + public void testDeterminant_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(-2, matrix.determinant()); + assertEquals(-2, matrix.det()); + } + + @Test + public void testDeterminant_size3(){ + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + //! cofactor() / cof() + @Test + public void testCofactor_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size1(){ + int[][] expectedGrid = new int[][]{{1}}; + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2(){ + int[][] expectedGrid = new int[][]{{-4, 3}, {2, -1}}; + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size10(){ + int[][] expectedGrid = 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}, + }; + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + //! adjoint() / adj() + @Test + public void testAdjoint_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size1(){ + int[][] expectedGrid = new int[][]{{1}}; + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2(){ + int[][] expectedGrid = new int[][]{{-4, 2}, {3, -1}}; + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size10(){ + int[][] expectedGrid = 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} + }; + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + //! inverse() + @Test + public void testInverse_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size1(){ + int[][] expectedGrid = new int[][]{{-1}}; + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size2(){ + int[][] expectedGrid = new int[][]{{-2, 3}, {3, -4}}; + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{4, 3}, {3, 2}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertThrows(InvalidScalarException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size2x10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size10x2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + //! equals() + @Test + public void testEquals_null(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertFalse(matrix.equals(null)); + assertFalse(matrix.equals((IntegerMatrix)null)); + } + + @Test + public void testEquals_matrixObject(){ + IntegerMatrix equalsMatrix = new IntegerMatrix(negativeGrid2); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals((Object)equalsMatrix)); + } + + @Test + public void testEquals_array(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(negativeGrid2)); + } + + @Test + public void testEquals_invalidType(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(1)); + } + + @Test + public void testEquals_manyRows(){ + IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewRows(){ + IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_manyCols(){ + IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewCols(){ + IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1}, {-2}}); + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_notEquals(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + IntegerMatrix equalsMatrix = matrix.transpose(); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_equals(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + IntegerMatrix equalsMatrix = new IntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_self(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertTrue(matrix.equals(matrix)); + } + + //! hashCode() + @Test + public void testHashCode_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size1(){ + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + //! toString() + @Test + public void testToString_size0(){ + IntegerMatrix matrix = new IntegerMatrix(); + + assertEquals("[]", matrix.toString()); + } + + @Test + public void testToString_size1(){ + IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); + + assertEquals("[1]", matrix.toString()); + } + + @Test + public void testToString_size2(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); + + assertEquals("[-1, -2]\n[-3, -4]", matrix.toString()); + } + + @Test + public void testToString_size10(){ + IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); + + assertEquals( + "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]\n" + + "[-11, -12, -13, -14, -15, -16, -17, -18, -19, -20]\n" + + "[-21, -22, -23, -24, -25, -26, -27, -28, -29, -30]\n" + + "[-31, -32, -33, -34, -35, -36, -37, -38, -39, -40]\n" + + "[-41, -42, -43, -44, -45, -46, -47, -48, -49, -50]\n" + + "[-51, -52, -53, -54, -55, -56, -57, -58, -59, -60]\n" + + "[-61, -62, -63, -64, -65, -66, -67, -68, -69, -70]\n" + + "[-71, -72, -73, -74, -75, -76, -77, -78, -79, -80]\n" + + "[-81, -82, -83, -84, -85, -86, -87, -88, -89, -90]\n" + + "[-91, -92, -93, -94, -95, -96, -97, -98, -99, -100]", + matrix.toString()); + } + + //! generateIdentity() + @Test + public void testGenerateIdentity_size0(){ + assertThrows(InvalidGeometryException.class, () -> { + IntegerMatrix.generateIdentity(0); + }); + } + + @Test + public void testGenerateIdentity_negativeSize(){ + assertThrows(InvalidGeometryException.class, () -> { + IntegerMatrix.generateIdentity(-1); + }); + } + + @Test + public void testGenerateIdentity_size2(){ + IntegerMatrix matrix = IntegerMatrix.generateIdentity(2); + + assertArrayEquals(new int[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); + } + + @Test + public void testGenerateIdentity_size3(){ + IntegerMatrix matrix = IntegerMatrix.generateIdentity(3); + + assertArrayEquals(new int[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); + } + + @Test + public void testGenerateIdentity_size10(){ + IntegerMatrix matrix = IntegerMatrix.generateIdentity(10); + + assertArrayEquals(new int[][]{ + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + }, matrix.copyGrid()); + } +} diff --git a/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java b/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java index e66f965..269b525 100644 --- a/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java @@ -1,15 +1,11 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 04-13-23 +//Modified: 08-10-24 package com.mattrixwv.matrix; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; @@ -23,1806 +19,2216 @@ import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestLongMatrix{ - //Grid 1x1 - private static final long[][] grid1 = { - {1} + private static final long[][] negativeGrid2 = new long[][]{ + {-1, -2}, + {-3, -4} }; - private static final long[][] transformGrid1_1 = { - {1} + private static final long[][] negativeGrid10 = new long[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} }; - private static final long[][] transformGrid1_2 = { - {2} + private static final long[][] negativeGrid2x10 = new long[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; - - //Grid 2x2 - private static final long[][] grid2 = { - {1, 2}, - {1, 2} - }; - private static final long[][] transformGrid2_1 = { - {1, 0}, - {1, 0} - }; - private static final long[][] transformGrid2_2 = { - {2, 3}, - {2, 3} - }; - - //Grid 3x3 - private static final long[][] grid3 = { - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }; - private static final long[][] transformGrid3_1 = { - {2, 1, 0}, - {2, 1, 0}, - {2, 1, 0} - }; - private static final long[][] transformGrid3_2 = { - {2, 3, 4}, - {2, 3, 4}, - {2, 3, 4} - }; - - //Grid 4x4 - private static final long[][] grid4 = { - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }; - private static final long[][] transformGrid4_1 = { - {3, 2, 1, 0}, - {3, 2, 1, 0}, - {3, 2, 1, 0}, - {3, 2, 1, 0} - }; - private static final long[][] transformGrid4_2 = { - {2, 3, 4, 5}, - {2, 3, 4, 5}, - {2, 3, 4, 5}, - {2, 3, 4, 5} - }; - - //Grid 10x10 - private static final long[][] 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 long[][] 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 long[][] 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} + private static final long[][] negativeGrid10x2 = new long[][]{ + { -1, -2}, + { -3, -4}, + { -5, -6}, + { -7, -8}, + { -9, -10}, + {-11, -12}, + {-13, -14}, + {-15, -16}, + {-17, -18}, + {-19, -20} }; + //! Constructor @Test - public void testConstructor(){ - //Default constructor + public void testConstructor_default(){ LongMatrix matrix = new LongMatrix(); + assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); - - //Filler constructor - //0 rows - assertThrows(InvalidGeometryException.class, () -> { - new LongMatrix(0, 0, 0); - }); - assertThrows(InvalidGeometryException.class, () -> { - new LongMatrix(1, 0, 0); - }); - //Good values - matrix = new LongMatrix(2, 2, 0); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(0, matrix.get(0, 0)); - assertEquals(0, matrix.get(0, 1)); - assertEquals(0, matrix.get(1, 0)); - assertEquals(0, matrix.get(1, 1)); - - //Matrix constructor - matrix.set(0, 0, 1); - matrix.set(0, 1, 2); - matrix.set(1, 0, 1); - matrix.set(1, 1, 2); - LongMatrix matrix2 = new LongMatrix(matrix); - assertEquals(2, matrix2.getNumRows()); - assertEquals(2, matrix2.getNumCols()); - assertEquals(1, matrix2.get(0, 0)); - assertEquals(2, matrix2.get(0, 1)); - assertEquals(1, matrix.get(1, 0)); - assertEquals(2, matrix.get(1, 1)); - - //Array constructor - //0 length - long[][] grid = new long[0][0]; - matrix = new LongMatrix(grid); - assertEquals(0, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //0 cols - grid = new long[1][0]; - matrix = new LongMatrix(grid); - assertEquals(1, matrix.getNumRows()); - assertEquals(0, matrix.getNumCols()); - //Uneven rows - assertThrows(InvalidRowSizeException.class, () -> { - long[][] grid1 = new long[2][]; - grid1[0] = new long[1]; - grid1[0][0] = 0; - grid1[1] = new long[2]; - grid1[1][0] = 1; - grid1[1][1] = 2; - new LongMatrix(grid1); - }); - - //2x2 - grid = grid2; - matrix = new LongMatrix(grid); - assertEquals(2, matrix.getNumRows()); - assertEquals(2, matrix.getNumCols()); - assertEquals(1, matrix.get(0, 0)); - assertEquals(2, matrix.get(0, 1)); - assertEquals(1, matrix.get(1, 0)); - assertEquals(2, matrix.get(1, 1)); } @Test - public void testEquals(){ - //Invalid equals - LongMatrix matrix = new LongMatrix(grid1); - assertNotEquals(matrix, null); - assertNotEquals(matrix, new int[0]); + public void testConstructor_fill0Rows(){ + assertThrows(InvalidGeometryException.class, () -> { + new LongMatrix(0, 1, 0); + }); + } - //1x1 - matrix = new LongMatrix(grid1); - boolean gridEquals = matrix.equals(matrix); - assertTrue(gridEquals); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals1 = matrix.equals(grid1); - assertTrue(gridEquals1); + @Test + public void testConstructor_fill0Cols(){ + assertThrows(InvalidGeometryException.class, () -> { + new LongMatrix(1, 0, 0); + }); + } - //2x2 - matrix = new LongMatrix(grid2); - boolean gridEquals2 = matrix.equals(matrix); - assertTrue(gridEquals2); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals21 = matrix.equals(grid2); - assertTrue(gridEquals21); - //false - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals22 = matrix.equals(transformGrid2_1); - assertFalse(gridEquals22); - gridEquals2 = matrix.equals(new LongMatrix(grid3)); - assertFalse(gridEquals2); - gridEquals2 = matrix.equals(new LongMatrix(new long[][]{ - {0, 1, 2}, - {0, 1, 2} - })); - assertFalse(gridEquals2); + @Test + public void testConstructor_fillSize2(){ + LongMatrix matrix = new LongMatrix(2, 2, -1); - //3x3 - matrix = new LongMatrix(grid3); - boolean gridEquals3 = matrix.equals(matrix); - assertTrue(gridEquals3); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals31 = matrix.equals(grid3); - assertTrue(gridEquals31); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-1, matrix.get(0, 1)); + assertEquals(-1, matrix.get(1, 0)); + assertEquals(-1, matrix.get(1, 1)); + } - //4x4 - matrix = new LongMatrix(grid4); - boolean gridEquals4 = matrix.equals(matrix); - assertTrue(gridEquals4); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals41 = matrix.equals(grid4); - assertTrue(gridEquals41); + @Test + public void testConstructor_fillSize10(){ + LongMatrix matrix = new LongMatrix(10, 10, -1); - //10x10 - matrix = new LongMatrix(grid10); - boolean gridEquals10 = matrix.equals(matrix); - assertTrue(gridEquals10); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals101 = matrix.equals(grid10); - assertTrue(gridEquals101); + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(long[] row : matrix.copyGrid()){ + for(long num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_arraySize0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_arraySize2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testConstructor_arrayUnevenRows(){ + long[][] grid = new long[][]{ + {-1, -2, -3}, + {-4, -5}, + {-6, -7, -8, -9}, + {-10, -11, -12} + }; + + assertThrows(InvalidRowSizeException.class, () -> { + new LongMatrix(grid); + }); + } + + @Test + public void testConstructor_matrixSize0(){ + LongMatrix originalMatrix = new LongMatrix(); + LongMatrix matrix = new LongMatrix(originalMatrix); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_matrixSize2(){ + LongMatrix originalMatrix = new LongMatrix(2, 2, -1); + LongMatrix matrix = new LongMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(long[] row : matrix.copyGrid()){ + for(long num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10(){ + LongMatrix originalMatrix = new LongMatrix(10, 10, -1); + LongMatrix matrix = new LongMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(long[] row : matrix.copyGrid()){ + for(long num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize2x10(){ + LongMatrix originalMatrix = new LongMatrix(2, 10, -1); + LongMatrix matrix = new LongMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(long[] row : matrix.copyGrid()){ + for(long num : row){ + assertEquals(-1, num); + } + } + } + + @Test + public void testConstructor_matrixSize10x2(){ + LongMatrix originalMatrix = new LongMatrix(10, 2, -1); + LongMatrix matrix = new LongMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(long[] row : matrix.copyGrid()){ + for(long num : row){ + assertEquals(-1, num); + } + } + } + + //! setGrid() + @Test + public void testSetGrid_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(grid); + + assertEquals(2, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2(){ + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10(){ + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size2x10(){ + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10x2(){ + LongMatrix matrix = new LongMatrix(); + matrix.setGrid(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertArrayEquals(negativeGrid2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertArrayEquals(negativeGrid10, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); + } + + //! isSquare() + @Test + public void testIsSquare_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertFalse(matrix.isSquare()); + } + + //! laplaceExpansionHelper() + @Test + public void testLaplaceExpansionHelper_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + LongMatrix result = matrix.laplaceExpansionHelper(0, 0); + + assertArrayEquals(new long[][]{{-4}}, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(-1, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(2, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, -1); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, 2); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc0x0(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + LongMatrix result = matrix.laplaceExpansionHelper(0, 0); + + long[][] expectedGrid = new long[][]{ + {-12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-92, -93, -94, -95, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc4x4(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + LongMatrix result = matrix.laplaceExpansionHelper(4, 4); + + long[][] expectedGrid = new long[][]{ + { -1, -2, -3, -4, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -36, -37, -38, -39, -40}, + {-51, -52, -53, -54, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -96, -97, -98, -99, -100} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + //! get() + @Test + public void testGet_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(2, 0); + }); + } + + @Test + public void testGet_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(-1, 0); + }); + } + + @Test + public void testGet_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, 2); + }); + } + + @Test + public void testGet_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, -1); + }); } @Test public void testGet(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - assertEquals(1, matrix.get(0, 0)); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals(-1, matrix.get(0, 0)); + assertEquals(-2, matrix.get(0, 1)); + assertEquals(-3, matrix.get(1, 0)); + assertEquals(-4, matrix.get(1, 1)); + } + + //! getRow() + @Test + public void testGetRow_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid gets - final LongMatrix testMatrix = new LongMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(3, 3); + matrix.getRow(2); }); + } + + @Test + public void testGetRow_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(-1, -1); + matrix.getRow(-1); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, -1); - }); - - //2x2 - matrix = new LongMatrix(grid2); - assertEquals(1, matrix.get(0, 0)); - - //3x3 - matrix = new LongMatrix(grid3); - assertEquals(1, matrix.get(0, 0)); - - //4x4 - matrix = new LongMatrix(grid4); - assertEquals(1, matrix.get(0, 0)); - - //10x10 - matrix = new LongMatrix(grid10); - assertEquals(1, matrix.get(0, 0)); } @Test public void testGetRow(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, matrix.getRow(0)); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertArrayEquals(new long[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); + assertArrayEquals(new long[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); + } + + + //! getColumn() + @Test + public void testGetCol_0Rows(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); - //Invalid gets - final LongMatrix testMatrix = new LongMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(-1); + matrix.getCol(0); }); + } + + @Test + public void testGetCol_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(3); + matrix.getCol(2); }); + } - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{{1, 2}}); - assertEquals(correctMatrix, matrix.getRow(0)); + @Test + public void testGetCol_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //4x4 - matrix = new LongMatrix(grid4); - matrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4}}); - assertEquals(correctMatrix, matrix.getRow(0)); - - //10x10 - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); - assertEquals(correctMatrix, matrix.getRow(0)); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(-1); + }); } @Test public void testGetCol(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, matrix.getCol(0)); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertArrayEquals(new long[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); + assertArrayEquals(new long[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); + } + + //! getNumRows() + @Test + public void testGetNumRows_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + } + + //! getNumCols() + @Test + public void testGetNumCols_size0(){ + long[][] grid = new long[0][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals(2, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertEquals(2, matrix.getNumCols()); + } + + //! set() + @Test + public void testSet_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid gets - final LongMatrix testMatrix = new LongMatrix(); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(-1); + matrix.set(-1, 0, 0); }); + } + + @Test + public void testSet_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(3); + matrix.set(2, 0, 0); }); - final LongMatrix testMatrix2 = new LongMatrix(grid1); + } + + @Test + public void testSet_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix2.getCol(3); + matrix.set(0, -1, 0); }); + } - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); + @Test + public void testSet_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1}, - {1} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, 2, 0); }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //4x4 - matrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1}, - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); - - //10x10 - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.getCol(0)); } @Test public void testSet(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - matrix.set(0, 0, 2); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}}); - assertEquals(correctMatrix, matrix); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + matrix.set(0, 0, -5); + matrix.set(0, 1, -6); + matrix.set(1, 0, -7); + matrix.set(1, 1, -8); + assertEquals(-5, matrix.get(0, 0)); + assertEquals(-6, matrix.get(0, 1)); + assertEquals(-7, matrix.get(1, 0)); + assertEquals(-8, matrix.get(1, 1)); + } + + //! setRow() + @Test + public void testSetRow_array_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid sets - final LongMatrix testMatrix = new LongMatrix(grid1); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(-1, -1, 0); + matrix.setRow(-1, new long[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(2, 2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, -1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, 2, 0); - }); - - //2x2 - matrix = new LongMatrix(grid2); - matrix.set(0, 0, 3); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new LongMatrix(grid3); - matrix.set(0, 0, 3); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new LongMatrix(grid4); - matrix.set(0, 0, 3); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new LongMatrix(grid10); - matrix.set(0, 0, 3); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetRow(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - matrix.setRow(0, new long[]{0}); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid setRows - final LongMatrix testMatrix = new LongMatrix(grid1); - final long[] testGrid = {0, 0}; - final LongMatrix testMatrix2 = new LongMatrix(grid2); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(-1, testGrid); + matrix.setRow(2, new long[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, (long[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setRow(0, (LongMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testMatrix2); - }); - - //2x2 - matrix = new LongMatrix(grid2); - matrix.setRow(1, new long[]{2, 1}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {2, 1} - }); - assertEquals(correctMatrix, matrix); - //Matrix - LongMatrix matrix2 = new LongMatrix(new long[][]{{0, 0}}); - matrix.setRow(1, matrix2); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {0, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new LongMatrix(grid3); - matrix.setRow(0, new long[]{0, 1, 2}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new LongMatrix(grid4); - matrix.setRow(0, new long[]{4, 3, 2, 1}); - correctMatrix = new LongMatrix(new long[][]{ - {4, 3, 2, 1}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new LongMatrix(grid10); - matrix.setRow(0, new long[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testSetCol(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - matrix.setCol(0, new long[]{1}); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_nullArray(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid setCols - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(); - final long[] testGrid = {0, 0}; - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(2, testGrid); - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, (long[])null); + matrix.setRow(0, (long[])null); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(0, testGrid); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setCol(0, (LongMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, testMatrix2); - }); - - //2x2 - matrix = new LongMatrix(grid2); - matrix.setCol(0, new long[]{3, 3}); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2}, - {3, 2} - }); - assertEquals(correctMatrix, matrix); - //Matrix - LongMatrix vector = new LongMatrix(new long[][]{{0}, {0}}); - matrix.setCol(1, vector); - correctMatrix = new LongMatrix(new long[][]{ - {3, 0}, - {3, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new LongMatrix(grid3); - matrix.setCol(0, new long[]{0, 0, 0}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 2, 3}, - {0, 2, 3}, - {0, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new LongMatrix(grid4); - matrix.setCol(0, new long[]{0, 0, 0, 0}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new LongMatrix(grid10); - matrix.setCol(0, new long[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); - correctMatrix = new LongMatrix(new long[][]{ - {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}, - {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddRow(){ - //0x0 - LongMatrix matrix = new LongMatrix(); - matrix.addRow(new long[]{0}); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_arrayLength0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //1x1 - matrix = new LongMatrix(grid1); - matrix.addRow(new long[]{1}); - correctMatrix = new LongMatrix(new long[][]{{1}, {1}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(new long[]{0, 0}); + matrix.setRow(0, new long[0]); }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((long[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((LongMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(testMatrix2); - }); - - //2x2 - matrix = new LongMatrix(grid2); - matrix.addRow(new long[]{1, 2}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new LongMatrix(grid2); - matrix.addRow(new LongMatrix(new long[][]{{0, 0}})); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {1, 2}, - {0, 0} - }); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new LongMatrix(grid3); - matrix.addRow(new long[]{1, 2, 3}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new LongMatrix(grid4); - matrix.addRow(new long[]{1, 2, 3, 4}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new LongMatrix(grid10); - matrix.addRow(new long[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix); } @Test - public void testAddCol(){ - //0x0 - LongMatrix matrix = new LongMatrix(); - matrix.addCol(new long[]{0}); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_invalidArrayLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //1x1 - matrix = new LongMatrix(grid1); - matrix.addCol(new long[]{1}); - correctMatrix = new LongMatrix(new long[][]{{1, 1}}); - assertEquals(correctMatrix, matrix); - - //Invalid adds - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(grid2); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(new long[]{0, 0}); + matrix.setRow(0, new long[]{0, 0, 0}); }); + } + + @Test + public void testSetRow_array(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + matrix.setRow(0, new long[]{-5, -6}); + matrix.setRow(1, new long[]{-7, -8}); + assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((long[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((LongMatrix)null); + matrix.setRow(0, (LongMatrix)null); }); + } + + @Test + public void testSetRow_matrix_multipleRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(testMatrix2); + matrix.setRow(0, new LongMatrix(new long[][]{{0, 0}, {0, 0}})); }); + } - //2x2 - matrix = new LongMatrix(grid2); - matrix.addCol(new long[]{3, 3}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new LongMatrix(grid2); - matrix.addCol(new LongMatrix(new long[][]{{0}, {0}})); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 0}, - {1, 2, 0} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_negativeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //3x3 - matrix = new LongMatrix(grid3); - matrix.addCol(new long[]{4, 4, 4}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new LongMatrix(new long[][]{{0}})); }); - assertEquals(correctMatrix, matrix); + } - //4x4 - matrix = new LongMatrix(grid4); - matrix.addCol(new long[]{5, 5, 5, 5}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5} - }); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_largeRow(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //10x10 - matrix = new LongMatrix(grid10); - matrix.addCol(new long[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); - correctMatrix = new LongMatrix(new long[][]{ - {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} + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new LongMatrix(new long[][]{{0}})); + }); + } + + @Test + public void testSetRow_matrix_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new LongMatrix(new long[][]{{}})); + }); + } + + @Test + public void testSetRow_matrix_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new LongMatrix(new long[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetRow_matrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + matrix.setRow(0, new LongMatrix(new long[][]{{-5, -6}})); + matrix.setRow(1, new LongMatrix(new long[][]{{-7, -8}})); + assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! setCol() + @Test + public void testSetCol_array_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new long[]{0}); + }); + } + + @Test + public void testSetCol_array_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new long[]{0}); + }); + } + + @Test + public void testSetCol_array_nullArray(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, (long[])null); + }); + } + + @Test + public void testSetCol_array_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new long[0]); + }); + } + + @Test + public void testSetCol_array_invalidArrayLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new long[]{0, 0, 0}); + }); + } + + @Test + public void testSetCol_array(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + matrix.setCol(0, new long[]{-5, -7}); + matrix.setCol(1, new long[]{-6, -8}); + assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.setCol(0, (LongMatrix)null); + }); + } + + @Test + public void testSetCol_matrix_multipleCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testSetCol_matrix_negativeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new LongMatrix(new long[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_largeCol(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new LongMatrix(new long[][]{{0}})); + }); + } + + @Test + public void testSetCol_matrix_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new LongMatrix(new long[][]{{}})); + }); + } + + @Test + public void testSetCol_matrix_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new LongMatrix(new long[][]{{0, 0, 0}})); + }); + } + + @Test + public void testSetCol_matrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + matrix.setCol(0, new LongMatrix(new long[][]{{-5}, {-7}})); + matrix.setCol(1, new LongMatrix(new long[][]{{-6}, {-8}})); + assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); + } + + //! addRow() + @Test + public void testAddRow_array_nullArray(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((long[])null); + }); + } + + @Test + public void testAddRow_array_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new long[0]); + }); + } + + @Test + public void testAddRow_array_invalidArrayLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new long[]{0, 0, 0}); + }); + } + + @Test + public void testAddRow_array(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix.addRow(new long[]{-5, -6}); + + assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((LongMatrix)null); + }); + } + + @Test + public void testAddRow_matrix_multipleRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new LongMatrix(new long[][]{{0, 0}, {0, 0}})); + }); + } + + @Test + public void testAddRow_matrix_noRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new LongMatrix(new long[0][0])); + }); + } + + @Test + public void testAddRow_matrix_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new LongMatrix(new long[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddRow_matrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix.addRow(new LongMatrix(new long[][]{{-5, -6}})); + + assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); + } + + //! addCol() + @Test + public void testAddCol_array_nullArray(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((long[])null); + }); + } + + @Test + public void testAddCol_array_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new long[0]); + }); + } + + @Test + public void testAddCol_array_invalidArrayLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new long[]{0, 0, 0}); + }); + } + + @Test + public void testAddCol_array(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix.addCol(new long[]{-5, -6}); + + assertArrayEquals(new long[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((LongMatrix)null); + }); + } + + @Test + public void testAddCol_matrix_multipleCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testAddCol_matrix_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new LongMatrix(new long[][]{{}})); + }); + } + + @Test + public void testAddCol_matrix_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new LongMatrix(new long[][]{{0, 0, 0}})); + }); + } + + @Test + public void testAddCol_matrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix.addCol(new LongMatrix(new long[][]{{-5}, {-6}})); + + assertArrayEquals(new long[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); + } + + //! appendRight() + @Test + public void testAppendRight_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendRight((LongMatrix)null); + }); + } + + @Test + public void testAppendRight_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new LongMatrix(new long[0][0])); + }); + } + + @Test + public void testAppendRight_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new LongMatrix(new long[][]{{0, 0, 0}})); }); - assertEquals(correctMatrix, matrix); } @Test public void testAppendRight(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix secondMatrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1, 1}}); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.appendRight(matrix); + + assertArrayEquals(new long[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); + } + + //! appendBottom() + @Test + public void testAppendBottom_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid appends - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendRight(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendRight(null); + matrix.appendBottom((LongMatrix)null); }); + } - //2x2 - matrix = new LongMatrix(grid2); - secondMatrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 1, 2}, - {1, 2, 1, 2} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_length0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //3x3 - matrix = new LongMatrix(grid3); - secondMatrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 1, 2, 3}, - {1, 2, 3, 1, 2, 3}, - {1, 2, 3, 1, 2, 3} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new LongMatrix(new long[0][0])); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + } - //4x4 - matrix = new LongMatrix(grid4); - secondMatrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_invalidLength(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //10x10 - matrix = new LongMatrix(grid10); - secondMatrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{ - {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} + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new LongMatrix(new long[][]{{0}, {0}, {0}})); }); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); } @Test public void testAppendBottom(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix secondMatrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.appendBottom(matrix); + + assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); + } + + //! add() + @Test + public void testAdd_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid appends - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendBottom(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendBottom(null); + matrix.add((LongMatrix)null); }); - - //2x2 - matrix = new LongMatrix(grid2); - secondMatrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //3x3 - matrix = new LongMatrix(grid3); - secondMatrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //4x4 - matrix = new LongMatrix(grid4); - secondMatrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); - - //10x10 - matrix = new LongMatrix(grid10); - secondMatrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{ - {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}, - }); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); } @Test - public void testIsSquare(){ + public void testAdd_matrix_fewRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testAdd_matrix_manyRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new LongMatrix(new long[][]{{0}, {0}})); + }); + } + + @Test + public void testAdd_matrix_fewCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new LongMatrix(new long[][]{{0}})); + }); + } + + @Test + public void testAdd_matrix_manyCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testAdd_matrix_size2(){ + LongMatrix addMatrix = new LongMatrix(new long[][]{{-4, -3}, {-2, -1}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new long[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); + } + + @Test + public void testAdd_matrix_size10(){ + LongMatrix addMatrix = new LongMatrix(new long[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + LongMatrix matrix = new LongMatrix(negativeGrid10); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new long[][]{ + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, + {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101} + }, matrix.copyGrid()); + } + + @Test + public void testAdd_scalar(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.add(1); + + assertArrayEquals(new long[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); + } + + //! subtract() + @Test + public void testSubtract_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.subtract((LongMatrix)null); + }); + } + + @Test + public void testSubtract_matrix_fewRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_manyRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_fewCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new LongMatrix(new long[][]{{0}, {0}})); + }); + } + + @Test + public void testSubtract_matrix_manyCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testSubtract_matrix_size2(){ + LongMatrix subMatrix = new LongMatrix(new long[][]{{-4, -3}, {-2, -1}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new long[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_matrix_size10(){ + LongMatrix subMatrix = new LongMatrix(new long[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + LongMatrix matrix = new LongMatrix(negativeGrid10); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new long[][]{ + { 99, 97, 95, 93, 91, 89, 87, 85, 83, 81}, + { 79, 77, 75, 73, 71, 69, 67, 65, 63, 61}, + { 59, 57, 55, 53, 51, 49, 47, 45, 43, 41}, + { 39, 37, 35, 33, 31, 29, 27, 25, 23, 21}, + { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, + { -1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-21, -23, -25, -27, -29, -31, -33, -35, -37, -39}, + {-41, -43, -45, -47, -49, -51, -53, -55, -57, -59}, + {-61, -63, -65, -67, -69, -71, -73, -75, -77, -79}, + {-81, -83, -85, -87, -89, -91, -93, -95, -97, -99} + }, matrix.copyGrid()); + } + + @Test + public void testSubtract_scalar(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.subtract(1); + + assertArrayEquals(new long[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); + } + + //! multiply() + @Test + public void testMultiply_matrix_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.multiply((LongMatrix)null); + }); + } + + @Test + public void testMultiply_matrix_manyRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_fewRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testMultiply_matrix_square(){ + LongMatrix mulMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new long[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_matrix_rectangle(){ + LongMatrix mulMatrix = new LongMatrix(new long[][]{{1, 2, 3}, {4, 5, 6}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new long[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_scalar(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + matrix = matrix.multiply(2); + + assertArrayEquals(new long[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); + } + + //! pow + @Test + public void testPow_rectangle(){ + LongMatrix matrix = new LongMatrix(new long[][]{{1, 2, 3}, {4, 5, 6}}); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.pow(2); + }); + } + + @Test + public void testPow_negative(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidScalarException.class, () -> { + matrix.pow(-1); + }); + } + + @Test + public void testPow_0(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + LongMatrix result = matrix.pow(0); + + assertEquals(new LongMatrix(2, 2, 1), result); + } + + @Test + public void testPow_2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + LongMatrix result = matrix.pow(2); + + assertEquals(new LongMatrix(new long[][]{{7, 10}, {15, 22}}), result); + } + + @Test + public void testPow_3(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + LongMatrix result = matrix.pow(3); + + assertEquals(new LongMatrix(new long[][]{{-37, -54}, {-81, -118}}), result); + } + + //! dotProduct() + @Test + public void testDotProduct_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.dotProduct((LongMatrix)null); + }); + } + + @Test + public void testDotProduct_fewRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testDotProduct_manyRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testDotProduct_size2(){ + LongMatrix dotMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + long result = matrix.dotProduct(dotMatrix); + + assertEquals(-54, result); + } + + @Test + public void testDotProduct_size10(){ + LongMatrix dotMatrix = new LongMatrix(new long[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + LongMatrix matrix = new LongMatrix(negativeGrid10); + long result = matrix.dotProduct(dotMatrix); + + assertEquals(-2632750, result); + } + + //! hadamardProduct() + @Test + public void testHadamardProduct_nullMatrix(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(NullMatrixException.class, () -> { + matrix.hadamardProduct((LongMatrix)null); + }); + } + + @Test + public void testHadamardProduct_fewRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0}})); + }); + } + + @Test + public void testHadamardProduct_manyRows(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); + }); + } + + @Test + public void testHadamardProduct_fewCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new LongMatrix(new long[][]{{0}, {0}})); + }); + } + + @Test + public void testHadamardProduct_manyCols(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); + }); + } + + @Test + public void testHadamardProduct_size2(){ + LongMatrix hadMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + LongMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new long[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_size10(){ + LongMatrix hadMatrix = new LongMatrix(new long[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + LongMatrix matrix = new LongMatrix(negativeGrid10); + LongMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new long[][]{ + { -1, -4, -9, -16, -25, -36, -49, -64, -81, -100}, + { -121, -144, -169, -196, -225, -256, -289, -324, -361, -400}, + { -441, -484, -529, -576, -625, -676, -729, -784, -841, -900}, + { -961, -1024, -1089, -1156, -1225, -1296, -1369, -1444, -1521, -1600}, + {-1681, -1764, -1849, -1936, -2025, -2116, -2209, -2304, -2401, -2500}, + {-2601, -2704, -2809, -2916, -3025, -3136, -3249, -3364, -3481, -3600}, + {-3721, -3844, -3969, -4096, -4225, -4356, -4489, -4624, -4761, -4900}, + {-5041, -5184, -5329, -5476, -5625, -5776, -5929, -6084, -6241, -6400}, + {-6561, -6724, -6889, -7056, -7225, -7396, -7569, -7744, -7921, -8100}, + {-8281, -8464, -8649, -8836, -9025, -9216, -9409, -9604, -9801, -10000} + }, result.copyGrid()); + } + + //! transpose() + @Test + public void testTranspose_size0(){ LongMatrix matrix = new LongMatrix(); - assertFalse(matrix.isSquare()); - matrix = new LongMatrix(2, 2, 0); - assertTrue(matrix.isSquare()); + LongMatrix result = matrix.transpose(); - matrix = new LongMatrix(2, 3, 0); - assertFalse(matrix.isSquare()); + assertEquals(new LongMatrix(), result); } @Test - public void testAddition(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix transformMatrix = new LongMatrix(transformGrid1_1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}}); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - assertEquals(correctMatrix, matrix.add(1)); + public void testTranspose_size0x2(){ + long[][] grid = new long[0][2]; + LongMatrix matrix = new LongMatrix(grid); - //Invalid adds - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); - final LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix3); - }); - - //2x2 - matrix = new LongMatrix(grid2); - transformMatrix = new LongMatrix(transformGrid2_1); - correctMatrix = new LongMatrix(new long[][]{ - {2, 2}, - {2, 2} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 3}, - {2, 3} - }); - assertEquals(correctMatrix, matrix.add(1)); - - //3x3 - matrix = new LongMatrix(grid3); - transformMatrix = new LongMatrix(transformGrid3_1); - correctMatrix = new LongMatrix(new long[][]{ - {3, 3, 3}, - {3, 3, 3}, - {3, 3, 3} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 3, 4}, - {2, 3, 4}, - {2, 3, 4} - }); - assertEquals(correctMatrix, matrix.add(1)); - - //4x4 - matrix = new LongMatrix(grid4); - transformMatrix = new LongMatrix(transformGrid4_1); - correctMatrix = new LongMatrix(new long[][]{ - {4, 4, 4, 4}, - {4, 4, 4, 4}, - {4, 4, 4, 4}, - {4, 4, 4, 4} - }); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 3, 4, 5}, - {2, 3, 4, 5}, - {2, 3, 4, 5}, - {2, 3, 4, 5} - }); - assertEquals(correctMatrix, matrix.add(1)); - - //10x10 - matrix = new LongMatrix(grid10); - transformMatrix = new LongMatrix(transformGrid10_1); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.add(1)); + assertArrayEquals(new long[0][0], matrix.transpose().copyGrid()); } @Test - public void testSubtraction(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix transformMatrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{ - {0} - }); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - assertEquals(correctMatrix, matrix.subtract(1)); + public void testTranspose_size2x0(){ + long[][] grid = new long[2][0]; + LongMatrix matrix = new LongMatrix(grid); - //Invalid subtracts - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); - final LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix3); - }); - - //2x2 - matrix = new LongMatrix(grid2); - transformMatrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(2, 2, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1}, - {0, 1} - }); - assertEquals(correctMatrix, matrix.subtract(1)); - - //3x3 - matrix = new LongMatrix(grid3); - transformMatrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1, 2}, - {0, 1, 2}, - {0, 1, 2} - }); - assertEquals(correctMatrix, matrix.subtract(1)); - - //4x4 - matrix = new LongMatrix(grid4); - transformMatrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(4, 4, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1, 2, 3}, - {0, 1, 2, 3}, - {0, 1, 2, 3}, - {0, 1, 2, 3} - }); - assertEquals(correctMatrix, matrix.subtract(1)); - - //10x10 - matrix = new LongMatrix(grid10); - transformMatrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.subtract(1)); + assertArrayEquals(new long[0][0], matrix.transpose().copyGrid()); } @Test - public void testMultiplication(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix transformMatrix = new LongMatrix(transformGrid1_2); - LongMatrix correctMatrix = new LongMatrix(transformGrid1_2); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - assertEquals(correctMatrix, matrix.multiply(2)); + public void testTranspose_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid multiplication - final LongMatrix testMatrix = new LongMatrix(grid1); - final LongMatrix testMatrix2 = new LongMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.multiply(testMatrix2); - }); - - //2x2 - matrix = new LongMatrix(grid2); - transformMatrix = new LongMatrix(transformGrid2_2); - correctMatrix = new LongMatrix(new long[][]{ - {6, 9}, - {6, 9} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - LongMatrix vector = new LongMatrix(new long[][]{ - {2}, - {3} - }); - correctMatrix = new LongMatrix(new long[][]{ - {8}, - {8} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 4}, - {2, 4} - }); - assertEquals(correctMatrix, matrix.multiply(2)); - - //3x3 - matrix = new LongMatrix(grid3); - transformMatrix = new LongMatrix(transformGrid3_2); - correctMatrix = new LongMatrix(new long[][]{ - {12, 18, 24}, - {12, 18, 24}, - {12, 18, 24} - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new LongMatrix(new long[][]{ - {2}, - {3}, - {4} - }); - correctMatrix = new LongMatrix(new long[][]{ - {20}, - {20}, - {20} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 4, 6}, - {2, 4, 6}, - {2, 4, 6} - }); - assertEquals(correctMatrix, matrix.multiply(2)); - - //4x4 - matrix = new LongMatrix(grid4); - transformMatrix = new LongMatrix(transformGrid4_2); - correctMatrix = new LongMatrix(new long[][]{ - {20, 30, 40, 50}, - {20, 30, 40, 50}, - {20, 30, 40, 50}, - {20, 30, 40, 50}, - }); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - vector = new LongMatrix(new long[][]{ - {2}, - {3}, - {4}, - {5} - }); - correctMatrix = new LongMatrix(new long[][]{ - {40}, - {40}, - {40}, - {40} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new LongMatrix(new long[][]{ - {2, 4, 6, 8}, - {2, 4, 6, 8}, - {2, 4, 6, 8}, - {2, 4, 6, 8} - }); - assertEquals(correctMatrix, matrix.multiply(2)); - - //10x10 - matrix = new LongMatrix(grid10); - transformMatrix = new LongMatrix(transformGrid10_2); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.multiply(transformMatrix)); - vector = new LongMatrix(new long[][]{ - {2}, - {3}, - {4}, - {5}, - {6}, - {7}, - {8}, - {9}, - {10}, - {11} - }); - correctMatrix = new LongMatrix(new long[][]{ - {440}, - {440}, - {440}, - {440}, - {440}, - {440}, - {440}, - {440}, - {440}, - {440} - }); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.multiply(2)); + assertArrayEquals(new long[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); } @Test - public void testDotProduct(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix transformMatrix = new LongMatrix(transformGrid1_2); - assertEquals(2, matrix.dotProduct(transformMatrix)); + public void testTranspose_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); - //Invalid products - LongMatrix testMatrix = new LongMatrix(grid1); - LongMatrix testMatrix2 = new LongMatrix(grid2); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.dotProduct(testMatrix2); - }); - - //2x2 - matrix = new LongMatrix(grid2); - transformMatrix = new LongMatrix(transformGrid2_2); - assertEquals(30, matrix.dotProduct(transformMatrix)); - - //3x3 - matrix = new LongMatrix(grid3); - transformMatrix = new LongMatrix(transformGrid3_2); - assertEquals(162, matrix.dotProduct(transformMatrix)); - - //4x4 - matrix = new LongMatrix(grid4); - transformMatrix = new LongMatrix(transformGrid4_2); - assertEquals(560, matrix.dotProduct(transformMatrix)); - - //10x10 - matrix = new LongMatrix(grid10); - transformMatrix = new LongMatrix(transformGrid10_2); - assertEquals(35750, matrix.dotProduct(transformMatrix)); + assertArrayEquals(new long[][]{ + {-1, -11, -21, -31, -41, -51, -61, -71, -81, -91}, + {-2, -12, -22, -32, -42, -52, -62, -72, -82, -92}, + {-3, -13, -23, -33, -43, -53, -63, -73, -83, -93}, + {-4, -14, -24, -34, -44, -54, -64, -74, -84, -94}, + {-5, -15, -25, -35, -45, -55, -65, -75, -85, -95}, + {-6, -16, -26, -36, -46, -56, -66, -76, -86, -96}, + {-7, -17, -27, -37, -47, -57, -67, -77, -87, -97}, + {-8, -18, -28, -38, -48, -58, -68, -78, -88, -98}, + {-9, -19, -29, -39, -49, -59, -69, -79, -89, -99}, + {-10, -20, -30, -40, -50, -60, -70, -80, -90, -100} + }, matrix.transpose().copyGrid()); } @Test - public void testHadamardProduct(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix transformMatrix = new LongMatrix(transformGrid1_2); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}}); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); + public void testTranspose_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); - //Invalid hadamard products - LongMatrix testMatrix = new LongMatrix(grid1); - LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); - LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix3); - }); - - //2x2 - matrix = new LongMatrix(grid2); - transformMatrix = new LongMatrix(transformGrid2_2); - correctMatrix = new LongMatrix(new long[][]{ - {2, 6}, - {2, 6} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //3x3 - matrix = new LongMatrix(grid3); - transformMatrix = new LongMatrix(transformGrid3_2); - correctMatrix = new LongMatrix(new long[][]{ - {2, 6, 12}, - {2, 6, 12}, - {2, 6, 12} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //4x4 - matrix = new LongMatrix(grid4); - transformMatrix = new LongMatrix(transformGrid4_2); - correctMatrix = new LongMatrix(new long[][]{ - {2, 6, 12, 20}, - {2, 6, 12, 20}, - {2, 6, 12, 20}, - {2, 6, 12, 20} - }); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - - //10x10 - matrix = new LongMatrix(grid10); - transformMatrix = new LongMatrix(transformGrid10_2); - correctMatrix = new LongMatrix(new long[][]{ - {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(correctMatrix, matrix.hadamardProduct(transformMatrix)); + assertArrayEquals(new long[][]{ + {-1, -11}, + {-2, -12}, + {-3, -13}, + {-4, -14}, + {-5, -15}, + {-6, -16}, + {-7, -17}, + {-8, -18}, + {-9, -19}, + {-10, -20} + }, matrix.transpose().copyGrid()); } @Test - public void testTranspose(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, matrix.transpose()); + public void testTranspose_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {1, 1}, - {2, 2} - }); - assertEquals(correctMatrix, matrix.transpose()); + assertArrayEquals(new long[][]{ + {-1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, + {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20} + }, matrix.transpose().copyGrid()); + } - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{ - {1, 1, 1}, - {2, 2, 2}, - {3, 3, 3} - }); - assertEquals(correctMatrix, matrix.transpose()); + //! determinant() / det() + @Test + public void testDeterminant_size0(){ + LongMatrix matrix = new LongMatrix(); - //4x4 - matrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{ - {1, 1, 1, 1}, - {2, 2, 2, 2}, - {3, 3, 3, 3}, - {4, 4, 4, 4} + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); }); - assertEquals(correctMatrix, matrix.transpose()); - - //10x10 - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{ - {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}, + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); }); - assertEquals(correctMatrix, matrix.transpose()); } @Test - public void testDeterminant(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); + public void testDeterminant_size1(){ + LongMatrix matrix = new LongMatrix(new long[][]{{1}}); + assertEquals(1, matrix.determinant()); - - //Invalid determinants - LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.determinant(); - }); - - //2x2 - matrix = new LongMatrix(grid2); - assertEquals(0, matrix.determinant()); - matrix = new LongMatrix(new long[][]{ - {1, 4}, - {4, 1} - }); - assertEquals(-15, matrix.determinant()); - //det - assertEquals(matrix.determinant(), matrix.det()); - - //3x3 - matrix = new LongMatrix(grid3); - assertEquals(0, matrix.determinant()); - matrix = new LongMatrix(new long[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - assertEquals(-21, matrix.determinant()); - - //4x4 - matrix = new LongMatrix(grid4); - assertEquals(0, matrix.determinant()); - matrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - assertEquals(160, matrix.determinant()); - //Column - matrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {1, 0, 3, 4}, - {1, 0, 3, 4}, - {0, 0, 3, 4} - }); - assertEquals(0, matrix.determinant()); - //Column2 - matrix = new LongMatrix(new long[][]{ - {1, 2, 0, 4}, - {1, 2, 0, 4}, - {1, 2, 0, 4}, - {0, 2, 0, 4}, - }); - assertEquals(0, matrix.determinant()); - - //10x10 - matrix = new LongMatrix(grid10); - assertEquals(0, matrix.determinant()); - matrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(-10000000, matrix.determinant()); + assertEquals(1, matrix.det()); } @Test - public void testCofactor(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(grid1); - assertEquals(correctMatrix, matrix.cofactor()); + public void testDeterminant_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); - //Invalid cofactor - LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.cofactor(); - }); - - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {2, -1}, - {-2, 1} - }); - assertEquals(correctMatrix, matrix.cofactor()); - //cof - assertEquals(matrix.cofactor(), matrix.cof()); - - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new LongMatrix(new long[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - correctMatrix = new LongMatrix(new long[][]{ - {7, 0, -14}, - {-6, -6, 15}, - {-4, 3, -4} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //4x4 - matrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(4, 4, 0); - assertEquals(correctMatrix, matrix.cofactor()); - matrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new LongMatrix(new long[][]{ - {-36, 4, 4, 44}, - {4, 4, 44, -36}, - {4, 44, -36, 4}, - {44, -36, 4, 4} - }); - assertEquals(correctMatrix, matrix.cofactor()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.cofactor()); - */ + assertEquals(-2, matrix.determinant()); + assertEquals(-2, matrix.det()); } @Test - public void testPower(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, matrix.pow(3)); + public void testDeterminant_size3(){ + LongMatrix matrix = new LongMatrix(new long[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); - //Invalid powers - final LongMatrix testMatrix = new LongMatrix(new long[][]{{0}, {0}}); - final LongMatrix testMatrix2 = new LongMatrix(grid1); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.pow(1); + matrix.determinant(); }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + //! cofactor() / cof() + @Test + public void testCofactor_size0(){ + LongMatrix matrix = new LongMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size1(){ + long[][] expectedGrid = new long[][]{{1}}; + LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2(){ + long[][] expectedGrid = new long[][]{{-4, 3}, {2, -1}}; + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size10(){ + long[][] expectedGrid = new long[][]{ + {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}, + }; + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + //! adjoint() / adj() + @Test + public void testAdjoint_size0(){ + LongMatrix matrix = new LongMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size1(){ + long[][] expectedGrid = new long[][]{{1}}; + LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2(){ + long[][] expectedGrid = new long[][]{{-4, 2}, {3, -1}}; + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size10(){ + long[][] expectedGrid = new long[][]{ + {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} + }; + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + //! inverse() + @Test + public void testInverse_size0(){ + LongMatrix matrix = new LongMatrix(); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size1(){ + long[][] expectedGrid = new long[][]{{-1}}; + LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size2(){ + long[][] expectedGrid = new long[][]{{-2, 3}, {3, -4}}; + LongMatrix matrix = new LongMatrix(new long[][]{{4, 3}, {3, 2}}); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + assertThrows(InvalidScalarException.class, () -> { - testMatrix2.pow(-1); + matrix.inverse(); }); - - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {9, 18}, - {9, 18} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(new long[][]{ - {36, 72, 108}, - {36, 72, 108}, - {36, 72, 108} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //4x4 - //0 - matrix = new LongMatrix(grid4); - correctMatrix = new LongMatrix(new long[][]{ - {1, 1, 1, 1}, - {1, 1, 1, 1}, - {1, 1, 1, 1}, - {1, 1, 1, 1} - }); - assertEquals(correctMatrix, matrix.pow(0)); - //1 - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals(correctMatrix, matrix.pow(1)); - //3 - correctMatrix = new LongMatrix(new long[][]{ - {100, 200, 300, 400}, - {100, 200, 300, 400}, - {100, 200, 300, 400}, - {100, 200, 300, 400} - }); - assertEquals(correctMatrix, matrix.pow(3)); - - //10x10 - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals(correctMatrix, matrix.pow(3)); } @Test - public void testAdjoint(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(grid1); - assertEquals(correctMatrix, matrix.adjoint()); + public void testInverse_size2x10(){ + LongMatrix matrix = new LongMatrix(negativeGrid2x10); - //2x2 - matrix = new LongMatrix(grid2); - correctMatrix = new LongMatrix(new long[][]{ - {2, -2}, - {-1, 1} - }); - assertEquals(correctMatrix, matrix.adjoint()); - //adj - assertEquals(matrix.adjoint(), matrix.adj()); - - //3x3 - matrix = new LongMatrix(grid3); - correctMatrix = new LongMatrix(3, 3, 0); - assertEquals(correctMatrix, matrix.adjoint()); - - //4x4 - matrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new LongMatrix(new long[][]{ - {-36, 4, 4, 44}, - {4, 4, 44, -36}, - {4, 44, -36, 4}, - {44, -36, 4, 4} - }); - assertEquals(correctMatrix, matrix.adjoint()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new LongMatrix(grid10); - correctMatrix = new LongMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.adjoint()); - */ - } - - @Test - public void testInverse(){ - //1x1 - LongMatrix matrix = new LongMatrix(grid1); - LongMatrix correctMatrix = new LongMatrix(grid1); - assertEquals(correctMatrix, matrix.inverse()); - - //Invalid inverse - LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); - LongMatrix testMatrix2 = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.inverse(); + matrix.inverse(); }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.inverse(); - }); - - //2x2 - matrix = new LongMatrix(new long[][]{ - {1, 4}, - {4, 1} - }); - correctMatrix = new LongMatrix(new long[][]{ - {-0, 0}, - {0, -0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //3x3 - matrix = new LongMatrix(new long[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }); - correctMatrix = new LongMatrix(new long[][]{ - {-0, 0, 0}, - {0, 0, -0}, - {0, -0, 0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //4x4 - matrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {2, 3, 4, 1}, - {3, 4, 1, 2}, - {4, 1, 2, 3} - }); - correctMatrix = new LongMatrix(new long[][]{ - {-0, 0, 0, 0}, - {0, 0, 0, -0}, - {0, 0, -0, 0}, - {0, -0, 0, 0} - }); - assertEquals(correctMatrix, matrix.inverse()); - - //10x10 - //?Skipped 10x10 because it would take a long time to compute } @Test - public void testGenerateIdentity(){ - //0x0 + public void testInverse_size10x2(){ + LongMatrix matrix = new LongMatrix(negativeGrid10x2); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + //! equals() + @Test + public void testEquals_null(){ + LongMatrix matrix = new LongMatrix(); + + assertFalse(matrix.equals(null)); + } + + @Test + public void testEquals_matrixObject(){ + LongMatrix equalsMatrix = new LongMatrix(negativeGrid2); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertTrue(matrix.equals((Object)equalsMatrix)); + } + + @Test + public void testEquals_array(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertTrue(matrix.equals(negativeGrid2)); + } + + @Test + public void testEquals_invalidType(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertFalse(matrix.equals(1)); + } + + @Test + public void testEquals_manyRows(){ + LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewRows(){ + LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_manyCols(){ + LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2, -3}, {-4, -5, -6}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewCols(){ + LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1}, {-2}}); + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_notEquals(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + LongMatrix equalsMatrix = matrix.transpose(); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_equals(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + LongMatrix equalsMatrix = new LongMatrix(negativeGrid2); + + assertTrue(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_self(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertTrue(matrix.equals(matrix)); + } + + //! hashCode() + @Test + public void testHashCode_size0(){ + LongMatrix matrix = new LongMatrix(); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size1(){ + LongMatrix matrix = new LongMatrix(new long[][]{{1}}); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + //! toString() + @Test + public void testToString_size0(){ + LongMatrix matrix = new LongMatrix(); + + assertEquals("[]", matrix.toString()); + } + + @Test + public void testToString_size1(){ + LongMatrix matrix = new LongMatrix(new long[][]{{1}}); + + assertEquals("[1]", matrix.toString()); + } + + @Test + public void testToString_size2(){ + LongMatrix matrix = new LongMatrix(negativeGrid2); + + assertEquals("[-1, -2]\n[-3, -4]", matrix.toString()); + } + + @Test + public void testToString_size10(){ + LongMatrix matrix = new LongMatrix(negativeGrid10); + + assertEquals( + "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]\n" + + "[-11, -12, -13, -14, -15, -16, -17, -18, -19, -20]\n" + + "[-21, -22, -23, -24, -25, -26, -27, -28, -29, -30]\n" + + "[-31, -32, -33, -34, -35, -36, -37, -38, -39, -40]\n" + + "[-41, -42, -43, -44, -45, -46, -47, -48, -49, -50]\n" + + "[-51, -52, -53, -54, -55, -56, -57, -58, -59, -60]\n" + + "[-61, -62, -63, -64, -65, -66, -67, -68, -69, -70]\n" + + "[-71, -72, -73, -74, -75, -76, -77, -78, -79, -80]\n" + + "[-81, -82, -83, -84, -85, -86, -87, -88, -89, -90]\n" + + "[-91, -92, -93, -94, -95, -96, -97, -98, -99, -100]", + matrix.toString()); + } + + //! generateIdentity() + @Test + public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { LongMatrix.generateIdentity(0); }); + } - //1x1 - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals(correctMatrix, LongMatrix.generateIdentity(1)); - - //2x2 - correctMatrix = new LongMatrix(new long[][]{ - {1, 0}, - {0, 1} + @Test + public void testGenerateIdentity_negativeSize(){ + assertThrows(InvalidGeometryException.class, () -> { + LongMatrix.generateIdentity(-1); }); - assertEquals(correctMatrix, LongMatrix.generateIdentity(2)); + } - //3x3 - correctMatrix = new LongMatrix(new long[][]{ - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1} - }); - assertEquals(correctMatrix, LongMatrix.generateIdentity(3)); + @Test + public void testGenerateIdentity_size2(){ + LongMatrix matrix = LongMatrix.generateIdentity(2); - //4x4 - correctMatrix = new LongMatrix(new long[][]{ - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - }); - assertEquals(correctMatrix, LongMatrix.generateIdentity(4)); + assertArrayEquals(new long[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); + } - //10x10 - correctMatrix = new LongMatrix(new long[][]{ + @Test + public void testGenerateIdentity_size3(){ + LongMatrix matrix = LongMatrix.generateIdentity(3); + + assertArrayEquals(new long[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); + } + + @Test + public void testGenerateIdentity_size10(){ + LongMatrix matrix = LongMatrix.generateIdentity(10); + + assertArrayEquals(new long[][]{ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, @@ -1832,62 +2238,7 @@ public class TestLongMatrix{ {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - }); - assertEquals(correctMatrix, LongMatrix.generateIdentity(10)); - } - - @Test - public void testHashCode(){ - LongMatrix matrix = new LongMatrix(); - assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); - } - - @Test - public void testToString(){ - LongMatrix matrix = new LongMatrix(grid3); - String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; - assertEquals(matrixString, matrix.toString()); - } - - @Test - public void testLaplaceExpansionHelper(){ - LongMatrix matrix = new LongMatrix(); - matrix.addRow(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix.laplaceExpansionHelper(0, 0); - }); - - LongMatrix matrix2 = new LongMatrix(); - matrix2.setGrid(grid1); - assertThrows(InvalidGeometryException.class, () -> { - matrix2.laplaceExpansionHelper(0, 0); - }); - - LongMatrix matrix3 = new LongMatrix(); - matrix3.setGrid(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(-1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, 2); - }); - - LongMatrix matrix4 = new LongMatrix(); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - matrix4.addCol(grid2[1]); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + }, matrix.copyGrid()); } } diff --git a/src/test/java/com/mattrixwv/matrix/TestModMatrix.java b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java index 3118116..bbe8d80 100644 --- a/src/test/java/com/mattrixwv/matrix/TestModMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java @@ -1,15 +1,11 @@ //Matrix/src/test/java/com/mattixwv/matrix/TestModMatrix.java //Mattrixwv // Created: 02-09-22 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; @@ -17,1800 +13,2638 @@ import org.junit.jupiter.api.Test; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestModMatrix{ - //Grid 1x1 - private static final int[][] grid1 = { - {1} + private static final int[][] negativeGrid2 = new int[][]{ + {-1, -2}, + {-3, -4} }; - private static final int[][] transformGrid1_1 = { - {1} + private static final int[][] negativeGrid2Mod26 = new int[][]{ + {25, 24}, + {23, 22} }; - private static final int[][] transformGrid1_2 = { - {2} + private static final int[][] negativeGrid10 = new int[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, + {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, + {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, + {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, + {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, + {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, + {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, + {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, + {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} }; - - //Grid 2x2 - private static final int[][] grid2 = { - {1, 2}, - {1, 2} + private static final int[][] negativeGrid10Mod26 = new int[][]{ + {25, 24, 23, 22, 21, 20, 19, 18, 17, 16}, + {15, 14, 13, 12, 11, 10, 9, 8, 7, 6}, + { 5, 4, 3, 2, 1, 0, 25, 24, 23, 22}, + {21, 20, 19, 18, 17, 16, 15, 14, 13, 12}, + {11, 10, 9, 8, 7, 6, 5, 4, 3, 2}, + { 1, 0, 25, 24, 23, 22, 21, 20, 19, 18}, + {17, 16, 15, 14, 13, 12, 11, 10, 9, 8}, + { 7, 6, 5, 4, 3, 2, 1, 0, 25, 24}, + {23, 22, 21, 20, 19, 18, 17, 16, 15, 14}, + {13, 12, 11, 10, 9, 8, 7, 6, 5, 4} }; - private static final int[][] transformGrid2_1 = { - {1, 0}, - {1, 0} + private static final int[][] negativeGrid2x10 = new int[][]{ + { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, + {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; - private static final int[][] transformGrid2_2 = { - {2, 3}, - {2, 3} + private static final int[][] negativeGrid2x10Mod26 = new int[][]{ + {25, 24, 23, 22, 21, 20, 19, 18, 17, 16}, + {15, 14, 13, 12, 11, 10, 9, 8, 7, 6} }; - - //Grid 3x3 - private static final int[][] grid3 = { - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} + private static final int[][] negativeGrid10x2 = new int[][]{ + { -1, -2}, + { -3, -4}, + { -5, -6}, + { -7, -8}, + { -9, -10}, + {-11, -12}, + {-13, -14}, + {-15, -16}, + {-17, -18}, + {-19, -20} }; - 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} + private static final int[][] negativeGrid10x2Mod26 = new int[][]{ + {25, 24}, + {23, 22}, + {21, 20}, + {19, 18}, + {17, 16}, + {15, 14}, + {13, 12}, + {11, 10}, + { 9, 8}, + { 7, 6} }; + private static final int mod = 26; + //! Constructor @Test - public void testConstructor(){ - assertThrows(InvalidScalarException.class, () -> { - new ModMatrix(0); - }); + public void testConstructor_mod(){ + ModMatrix matrix = new ModMatrix(mod); - ModMatrix matrix = new ModMatrix(1); - assertEquals(1, matrix.getMod()); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); } @Test - public void testEquals(){ - //Invalid equals - ModMatrix matrix = new ModMatrix(grid1, 26); - assertNotEquals(matrix, null); - assertNotEquals(matrix, new double[0]); + public void testConstructor_fill0Rows(){ + assertThrows(InvalidGeometryException.class, () -> { + new ModMatrix(0, 1, 0, mod); + }); + } - //1x1 - matrix = new ModMatrix(grid1, 26); - assertTrue(matrix.equals(matrix)); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals = matrix.equals(grid1); - assertTrue(gridEquals); + @Test + public void testConstructor_fill0Cols(){ + assertThrows(InvalidGeometryException.class, () -> { + new ModMatrix(1, 0, 0, mod); + }); + } - //2x2 - matrix = new ModMatrix(grid2, 26); - assertTrue(matrix.equals(matrix)); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals2 = matrix.equals(grid2); - assertTrue(gridEquals2); - //false - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals22 = matrix.equals(transformGrid2_1); - assertFalse(gridEquals22); - gridEquals2 = matrix.equals(new ModMatrix(grid3, 26)); - assertFalse(gridEquals2); - gridEquals = matrix.equals(new ModMatrix(new int[][]{ - {0, 1, 2}, - {0, 1, 2} - }, 26)); - assertFalse(gridEquals2); + @Test + public void testConstructor_fillSize2(){ + ModMatrix matrix = new ModMatrix(2, 2, -1, mod); - //3x3 - matrix = new ModMatrix(grid3, 26); - assertTrue(matrix.equals(matrix)); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals3 = matrix.equals(grid3); - assertTrue(gridEquals3); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(25, matrix.get(0, 0)); + assertEquals(25, matrix.get(0, 1)); + assertEquals(25, matrix.get(1, 0)); + assertEquals(25, matrix.get(1, 1)); + } - //4x4 - matrix = new ModMatrix(grid4, 26); - assertTrue(matrix.equals(matrix)); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals4 = matrix.equals(grid4); - assertTrue(gridEquals4); + @Test + public void testConstructor_fillSize10(){ + ModMatrix matrix = new ModMatrix(10, 10, -1, mod); - //10x10 - matrix = new ModMatrix(grid10, 26); - assertTrue(matrix.equals(matrix)); - @SuppressWarnings("unlikely-arg-type") - boolean gridEquals10 = matrix.equals(grid10); - assertTrue(gridEquals10); + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(25, num); + } + } + } + + @Test + public void testConstructor_arraySize0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_arraySize2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2Mod26, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10Mod26, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10Mod26, matrix.copyGrid()); + } + + @Test + public void testConstructor_arraySize10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2Mod26, matrix.copyGrid()); + } + + @Test + public void testConstructor_arrayUnevenRows(){ + int[][] grid = new int[][]{ + {-1, -2, -3}, + {-4, -5}, + {-6, -7, -8, -9}, + {-10, -11, -12} + }; + + assertThrows(InvalidRowSizeException.class, () -> { + new ModMatrix(grid, mod); + }); + } + + @Test + public void testConstructor_matrixSize0(){ + ModMatrix originalMatrix = new ModMatrix(mod); + ModMatrix matrix = new ModMatrix(originalMatrix); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testConstructor_matrixSize2(){ + ModMatrix originalMatrix = new ModMatrix(2, 2, -1, mod); + ModMatrix matrix = new ModMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(25, num); + } + } + } + + @Test + public void testConstructor_matrixSize10(){ + ModMatrix originalMatrix = new ModMatrix(10, 10, -1, mod); + ModMatrix matrix = new ModMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(25, num); + } + } + } + + @Test + public void testConstructor_matrixSize2x10(){ + ModMatrix originalMatrix = new ModMatrix(2, 10, -1, mod); + ModMatrix matrix = new ModMatrix(originalMatrix); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(25, num); + } + } + } + + @Test + public void testConstructor_matrixSize10x2(){ + ModMatrix originalMatrix = new ModMatrix(10, 2, -1, mod); + ModMatrix matrix = new ModMatrix(originalMatrix); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + for(int[] row : matrix.copyGrid()){ + for(int num : row){ + assertEquals(25, num); + } + } + } + + //! setGrid() + @Test + public void testSetGrid_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(grid); + + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(grid); + + assertEquals(2, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testSetGrid_size2(){ + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(negativeGrid2); + + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid2Mod26, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10(){ + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(negativeGrid10); + + assertEquals(10, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid10Mod26, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size2x10(){ + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(negativeGrid2x10); + + assertEquals(2, matrix.getNumRows()); + assertEquals(10, matrix.getNumCols()); + assertArrayEquals(negativeGrid2x10Mod26, matrix.copyGrid()); + } + + @Test + public void testSetGrid_size10x2(){ + ModMatrix matrix = new ModMatrix(mod); + matrix.setGrid(negativeGrid10x2); + + assertEquals(10, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertArrayEquals(negativeGrid10x2Mod26, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertArrayEquals(grid, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(negativeGrid2Mod26, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertArrayEquals(negativeGrid10Mod26, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertArrayEquals(negativeGrid10x2Mod26, matrix.copyGrid()); + } + + @Test + public void testCopyGrid_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertArrayEquals(negativeGrid2x10Mod26, matrix.copyGrid()); + } + + @Test + public void testSetGrid(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.setGrid(negativeGrid2); + + assertArrayEquals(negativeGrid2Mod26, matrix.grid); + } + + //! setMod() + @Test + public void testSetMod_negative(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidScalarException.class, () -> { + matrix.setMod(-1); + }); + } + + @Test + public void testSetMod_0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidScalarException.class, () -> { + matrix.setMod(0); + }); + } + + @Test + public void testSetMod(){ + ModMatrix matrix = new ModMatrix(mod); + + matrix.setMod(2); + + assertEquals(2, matrix.mod); + } + + //! modValue() + @Test + public void testModValue_negative(){ + ModMatrix matrix = new ModMatrix(mod); + + int value = matrix.modValue(-1); + + assertEquals(25, value); + } + + @Test + public void testModValue_negativeLarge(){ + ModMatrix matrix = new ModMatrix(mod); + + int value = matrix.modValue(-28); + + assertEquals(24, value); + } + + @Test + public void testModValue_small(){ + ModMatrix matrix = new ModMatrix(mod); + + int value = matrix.modValue(1); + + assertEquals(1, value); + } + + @Test + public void testModValue_large(){ + ModMatrix matrix = new ModMatrix(mod); + + int value = matrix.modValue(28); + + assertEquals(2, value); + } + + //! modValues() + @Test + public void testModValues_null(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.modValues(null); + }); + } + + @Test + public void testModValues(){ + int[] values = new int[]{1, 2, 27, 28, -1, -2}; + int[] expectedValues = new int[]{1, 2, 1, 2, 25, 24}; + ModMatrix matrix = new ModMatrix(mod); + + int[] returnedValues = matrix.modValues(values); + + assertArrayEquals(expectedValues, returnedValues); + } + + //! modGrid() + @Test + public void testModGrid_size0(){ + int[][] expectedGrid = new int[0][0]; + ModMatrix matrix = new ModMatrix(mod); + matrix.modGrid(); + + assertArrayEquals(expectedGrid, matrix.grid); + } + + @Test + public void testModGrid_size0x2(){ + int[][] expectedGrid = new int[0][2]; + ModMatrix matrix = new ModMatrix(new int[0][2], mod); + matrix.modGrid(); + + assertArrayEquals(expectedGrid, matrix.grid); + } + + @Test + public void testModGrid_size2x0(){ + int[][] expectedGrid = new int[2][0]; + ModMatrix matrix = new ModMatrix(new int[2][0], mod); + matrix.modGrid(); + + assertArrayEquals(expectedGrid, matrix.grid); + } + + @Test + public void testModGrid_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.grid = negativeGrid2; + matrix.modGrid(); + + assertArrayEquals(negativeGrid2Mod26, matrix.grid); + } + + @Test + public void testModGrid_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + matrix.grid = negativeGrid10; + matrix.modGrid(); + + assertArrayEquals(negativeGrid10Mod26, matrix.grid); + } + + @Test + public void testModGrid_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + matrix.grid = negativeGrid2x10; + matrix.modGrid(); + + assertArrayEquals(negativeGrid2x10Mod26, matrix.grid); + } + + @Test + public void testModGrid_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + matrix.grid = negativeGrid10x2; + matrix.modGrid(); + + assertArrayEquals(negativeGrid10x2Mod26, matrix.grid); + } + + //! getMod() + @Test + public void testGetMod(){ + ModMatrix matrix = new ModMatrix(mod); + + assertEquals(mod, matrix.getMod()); + } + + //! isSquare() + @Test + public void testIsSquare_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertTrue(matrix.isSquare()); + } + + @Test + public void testIsSquare_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertFalse(matrix.isSquare()); + } + + @Test + public void testIsSquare_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertFalse(matrix.isSquare()); + } + + //! laplaceExpansionHelper() + @Test + public void testLaplaceExpansionHelper_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + assertArrayEquals(new int[][]{{22}}, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(-1, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(2, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, -1); + }); + } + + @Test + public void testLaplaceExpansionHelper_size2_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.laplaceExpansionHelper(0, 2); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc0x0(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); + + int[][] expectedGrid = new int[][]{ + {14, 13, 12, 11, 10, 9, 8, 7, 6}, + { 4, 3, 2, 1, 0, 25, 24, 23, 22}, + {20, 19, 18, 17, 16, 15, 14, 13, 12}, + {10, 9, 8, 7, 6, 5, 4, 3, 2}, + { 0, 25, 24, 23, 22, 21, 20, 19, 18}, + {16, 15, 14, 13, 12, 11, 10, 9, 8}, + { 6, 5, 4, 3, 2, 1, 0, 25, 24}, + {22, 21, 20, 19, 18, 17, 16, 15, 14}, + {12, 11, 10, 9, 8, 7, 6, 5, 4} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size10_loc4x4(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + IntegerMatrix result = matrix.laplaceExpansionHelper(4, 4); + + int[][] expectedGrid = new int[][]{ + {25, 24, 23, 22, 20, 19, 18, 17, 16}, + {15, 14, 13, 12, 10, 9, 8, 7, 6}, + { 5, 4, 3, 2, 0, 25, 24, 23, 22}, + {21, 20, 19, 18, 16, 15, 14, 13, 12}, + { 1, 0, 25, 24, 22, 21, 20, 19, 18}, + {17, 16, 15, 14, 12, 11, 10, 9, 8}, + { 7, 6, 5, 4, 2, 1, 0, 25, 24}, + {23, 22, 21, 20, 18, 17, 16, 15, 14}, + {13, 12, 11, 10, 8, 7, 6, 5, 4} + }; + assertArrayEquals(expectedGrid, result.copyGrid()); + } + + @Test + public void testLaplaceExpansionHelper_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + @Test + public void testLaplaceExpansionHelper_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.laplaceExpansionHelper(0, 0); + }); + } + + //! get() + @Test + public void testGet_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(2, 0); + }); + } + + @Test + public void testGet_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(-1, 0); + }); + } + + @Test + public void testGet_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, 2); + }); + } + + @Test + public void testGet_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.get(0, -1); + }); } @Test public void testGet(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - assertEquals(1, matrix.get(0, 0)); - //mod - assertEquals(26, matrix.getMod()); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals(25, matrix.get(0, 0)); + assertEquals(24, matrix.get(0, 1)); + assertEquals(23, matrix.get(1, 0)); + assertEquals(22, matrix.get(1, 1)); + } + + //! getRow() + @Test + public void testGetRow_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid gets - final ModMatrix testMatrix = new ModMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(3, 3); + matrix.getRow(2); }); + } + + @Test + public void testGetRow_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(-1, -1); + matrix.getRow(-1); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, 3); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.get(0, -1); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - assertEquals(1, matrix.get(0, 0)); - - //3x3 - matrix = new ModMatrix(grid3, 26); - assertEquals(1, matrix.get(0, 0)); - - //4x4 - matrix = new ModMatrix(grid4, 26); - assertEquals(1, matrix.get(0, 0)); - - //10x10 - matrix = new ModMatrix(grid10, 26); - assertEquals(1, matrix.get(0, 0)); } @Test public void testGetRow(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, matrix.getRow(0)); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(new int[][]{{25, 24}}, matrix.getRow(0).copyGrid()); + assertArrayEquals(new int[][]{{23, 22}}, matrix.getRow(1).copyGrid()); + } + + //! getColumn() + @Test + public void testGetCol_0Rows(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); - //Invalid gets - final ModMatrix testMatrix = new ModMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(-1); + matrix.getCol(0); }); + } + + @Test + public void testGetCol_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getRow(3); + matrix.getCol(2); }); + } - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{{1, 2}}, 26); - assertEquals(correctMatrix, matrix.getRow(0)); + @Test + public void testGetCol_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //3x3 - matrix = new ModMatrix(grid3, 26); - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3}}, 26); - assertEquals(correctMatrix, matrix.getRow(0)); - - //4x4 - matrix = new ModMatrix(grid4, 26); - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4}}, 26); - assertEquals(correctMatrix, matrix.getRow(0)); - - //10x10 - matrix = new ModMatrix(grid10, 26); - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 26); - assertEquals(correctMatrix, matrix.getRow(0)); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.getCol(-1); + }); } @Test public void testGetCol(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, matrix.getCol(0)); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(new int[][]{{25}, {23}}, matrix.getCol(0).copyGrid()); + assertArrayEquals(new int[][]{{24}, {22}}, matrix.getCol(1).copyGrid()); + } + + //! getNumRows() + @Test + public void testGetNumRows_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertEquals(2, matrix.getNumRows()); + } + + @Test + public void testGetNumRows_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertEquals(10, matrix.getNumRows()); + } + + //! getNumCols() + @Test + public void testGetNumCols_size0(){ + int[][] grid = new int[0][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertEquals(0, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals(2, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertEquals(10, matrix.getNumCols()); + } + + @Test + public void testGetNumCols_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertEquals(2, matrix.getNumCols()); + } + + //! set() + @Test + public void testSet_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid gets - final ModMatrix testMatrix = new ModMatrix(26); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(-1); + matrix.set(-1, 0, 0); }); + } + + @Test + public void testSet_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.getCol(3); + matrix.set(2, 0, 0); }); - final ModMatrix testMatrix2 = new ModMatrix(grid1, 26); + } + + @Test + public void testSet_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix2.getCol(3); + matrix.set(0, -1, 0); }); + } - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1}, - }, 26); - assertEquals(correctMatrix, matrix.getCol(0)); + @Test + public void testSet_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //3x3 - matrix = new ModMatrix(grid3, 26); - correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1}, - {1} - }, 26); - assertEquals(correctMatrix, matrix.getCol(0)); - - //4x4 - matrix = new ModMatrix(grid4, 26); - correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1}, - {1}, - {1} - }, 26); - assertEquals(correctMatrix, matrix.getCol(0)); - - //10x10 - matrix = new ModMatrix(grid10, 26); - correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1}, - {1} - }, 26); - assertEquals(correctMatrix, matrix.getCol(0)); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.set(0, 2, 0); + }); } @Test public void testSet(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - matrix.set(0, 0, 2); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); - assertEquals(correctMatrix, matrix); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.set(0, 0, -5); + matrix.set(0, 1, -6); + matrix.set(1, 0, -7); + matrix.set(1, 1, -8); + assertEquals(21, matrix.get(0, 0)); + assertEquals(20, matrix.get(0, 1)); + assertEquals(19, matrix.get(1, 0)); + assertEquals(18, matrix.get(1, 1)); + } + + //! setRow() + @Test + public void testSetRow_array_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid sets - final ModMatrix testMatrix = new ModMatrix(grid1, 26); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(-1, -1, 0); + matrix.setRow(-1, new int[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(2, 2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, -1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.set(0, 2, 0); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - matrix.set(0, 0, 3); - correctMatrix = new ModMatrix(new int[][]{ - {3, 2}, - {1, 2} - }, 26); - assertEquals(correctMatrix, matrix); - - //3x3 - 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(correctMatrix, matrix); - - //4x4 - 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(correctMatrix, matrix); - - //10x10 - 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(correctMatrix, matrix); } @Test - public void testSetRow(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - matrix.setRow(0, new int[]{0}); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid setRows - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final int[] testGrid = {0, 0}; - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(-1, testGrid); + matrix.setRow(2, new int[]{0}); }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setRow(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, (int[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.setRow(0, (ModMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setRow(0, testMatrix2); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - matrix.setRow(1, new int[]{2, 1}); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {2, 1} - }, 26); - assertEquals(correctMatrix, matrix); - //Matrix - ModMatrix matrix2 = new ModMatrix(new int[][]{{0, 0}}, 26); - matrix.setRow(1, matrix2); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {0, 0} - }, 26); - assertEquals(correctMatrix, matrix); - //Integer matrix - IntegerMatrix matrix3 = new IntegerMatrix(new int[][]{{1, 1}}); - matrix.setRow(1, matrix3); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {1, 1} - }, 26); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new ModMatrix(grid3, 26); - matrix.setRow(0, new int[]{0, 1, 2}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }, 26); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new ModMatrix(grid4, 26); - 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(correctMatrix, matrix); - - //10x10 - matrix = new ModMatrix(grid10, 26); - 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(correctMatrix, matrix); } @Test - public void testSetCol(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - matrix.setCol(0, new int[]{1}); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_nullArray(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid setCols - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(26); - final int[] testGrid = {0, 0}; - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(-1, testGrid); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(2, testGrid); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, (int[])null); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - testMatrix.setCol(0, testGrid); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.setCol(0, (ModMatrix)null); + matrix.setRow(0, (int[])null); }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.setCol(0, testMatrix2); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - matrix.setCol(0, new int[]{3, 3}); - correctMatrix = new ModMatrix(new int[][]{ - {3, 2}, - {3, 2} - }, 26); - assertEquals(correctMatrix, matrix); - //Matrix - ModMatrix vector = new ModMatrix(new int[][]{{0}, {0}}, 26); - matrix.setCol(1, vector); - correctMatrix = new ModMatrix(new int[][]{ - {3, 0}, - {3, 0} - }, 26); - assertEquals(correctMatrix, matrix); - //Integer matrix - IntegerMatrix vector2 = new IntegerMatrix(new int[][]{{1}, {1}}); - matrix.setCol(1, vector2); - correctMatrix = new ModMatrix(new int[][]{ - {3, 1}, - {3, 1} - }, 26); - assertEquals(correctMatrix, matrix); - - //3x3 - matrix = new ModMatrix(grid3, 26); - matrix.setCol(0, new int[]{0, 0, 0}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 2, 3}, - {0, 2, 3}, - {0, 2, 3} - }, 26); - assertEquals(correctMatrix, matrix); - - //4x4 - matrix = new ModMatrix(grid4, 26); - matrix.setCol(0, new int[]{0, 0, 0, 0}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }, 26); - assertEquals(correctMatrix, matrix); - - //10x10 - matrix = new ModMatrix(grid10, 26); - matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); - correctMatrix = new ModMatrix(new int[][]{ - {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}, - {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} - }, 26); - assertEquals(correctMatrix, matrix); } @Test - public void testAddRow(){ - //0x0 - ModMatrix matrix = new ModMatrix(26); - matrix.addRow(new int[]{0}); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_arrayLength0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //1x1 - matrix = new ModMatrix(grid1, 26); - matrix.addRow(new int[]{1}); - correctMatrix = new ModMatrix(new int[][]{{1}, {1}}, 26); - assertEquals(correctMatrix, matrix); - - //Invalid addsd - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(new int[]{0, 0}); + matrix.setRow(0, new int[0]); }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow((int[])null); - }); - assertThrows(NullMatrixException.class, () -> { - testMatrix.addRow((ModMatrix)null); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addRow(testMatrix2); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - matrix.addRow(new int[]{1, 2}); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 2} - }, 26); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new ModMatrix(grid2, 26); - matrix.addRow(new ModMatrix(new int[][]{{0, 0}}, 26)); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {0, 0} - }, 26); - assertEquals(correctMatrix, matrix); - //Integer matrix - matrix = new ModMatrix(grid2, 26); - matrix.addRow(new IntegerMatrix(new int[][]{{1, 1}})); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 1} - }, 26); - - //3x3 - 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(correctMatrix, matrix); - - //4x4 - 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(correctMatrix, matrix); - - //10x10 - 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(correctMatrix, matrix); } @Test - public void testAddCol(){ - //0x0 - ModMatrix matrix = new ModMatrix(26); - matrix.addCol(new int[]{0}); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); - assertEquals(correctMatrix, matrix); + public void testSetRow_array_invalidArrayLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //1x1 - matrix = new ModMatrix(grid1, 26); - matrix.addCol(new int[]{1}); - correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); - assertEquals(correctMatrix, matrix); + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new int[]{0, 0, 0}); + }); + } + + @Test + public void testSetRow_array(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setRow(0, new int[]{-5, -6}); + matrix.setRow(1, new int[]{-7, -8}); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid adds - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(new int[]{0, 0}); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol((int[])null); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.addCol((ModMatrix)null); + matrix.setRow(0, (ModMatrix)null); }); + } + + @Test + public void testSetRow_matrix_multipleRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + assertThrows(InvalidGeometryException.class, () -> { - testMatrix.addCol(testMatrix2); + matrix.setRow(0, new ModMatrix(new int[][]{{0, 0}, {0, 0}}, mod)); }); + } - //2x2 - matrix = new ModMatrix(grid2, 26); - matrix.addCol(new int[]{3, 3}); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3} - }, 26); - assertEquals(correctMatrix, matrix); - //Matrix - matrix = new ModMatrix(grid2, 26); - matrix.addCol(new ModMatrix(new int[][]{{0}, {0}}, 26)); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2, 0}, - {1, 2, 0} - }, 26); - assertEquals(correctMatrix, matrix); - //Integer matrix - matrix = new ModMatrix(grid2, 26); - matrix.addCol(new IntegerMatrix(new int[][]{{1}, {1}})); - correctMatrix = new ModMatrix(new int[][]{ - {1, 2, 1}, - {1, 2, 1} - }, 26); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_negativeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //3x3 - 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(correctMatrix, matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(-1, new ModMatrix(new int[][]{{0}}, mod)); + }); + } - //4x4 - matrix = new ModMatrix(grid4, 26); - matrix.addCol(new int[]{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} - }, 26); - assertEquals(correctMatrix, matrix); + @Test + public void testSetRow_matrix_largeRow(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //10x10 - 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(correctMatrix, matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setRow(2, new ModMatrix(new int[][]{{0}}, mod)); + }); + } + + @Test + public void testSetRow_matrix_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new ModMatrix(new int[][]{{}}, mod)); + }); + } + + @Test + public void testSetRow_matrix_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setRow(0, new ModMatrix(new int[][]{{0, 0, 0}}, mod)); + }); + } + + @Test + public void testSetRow_matrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setRow(0, new ModMatrix(new int[][]{{-5, -6}}, mod)); + matrix.setRow(1, new ModMatrix(new int[][]{{-7, -8}}, mod)); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + @Test + public void testSetRow_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setRow(0, new IntegerMatrix(new int[][]{{-5, -6}})); + matrix.setRow(1, new IntegerMatrix(new int[][]{{-7, -8}})); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + //! setCol() + @Test + public void testSetCol_array_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(25, new int[]{0}); + }); + } + + @Test + public void testSetCol_array_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new int[]{0}); + }); + } + + @Test + public void testSetCol_array_nullArray(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, (int[])null); + }); + } + + @Test + public void testSetCol_array_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new int[0]); + }); + } + + @Test + public void testSetCol_array_invalidArrayLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new int[]{0, 0, 0}); + }); + } + + @Test + public void testSetCol_array(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setCol(0, new int[]{-5, -7}); + matrix.setCol(1, new int[]{-6, -8}); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.setCol(0, (ModMatrix)null); + }); + } + + @Test + public void testSetCol_matrix_multipleCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testSetCol_matrix_negativeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(-1, new ModMatrix(new int[][]{{0}}, mod)); + }); + } + + @Test + public void testSetCol_matrix_largeCol(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidCoordinatesException.class, () -> { + matrix.setCol(2, new ModMatrix(new int[][]{{0}}, mod)); + }); + } + + @Test + public void testSetCol_matrix_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new ModMatrix(new int[][]{{}}, mod)); + }); + } + + @Test + public void testSetCol_matrix_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.setCol(0, new ModMatrix(new int[][]{{0, 0, 0}}, mod)); + }); + } + + @Test + public void testSetCol_matrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setCol(0, new ModMatrix(new int[][]{{-5}, {-7}}, mod)); + matrix.setCol(1, new ModMatrix(new int[][]{{-6}, {-8}}, mod)); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + @Test + public void testSetCol_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + matrix.setCol(0, new IntegerMatrix(new int[][]{{-5}, {-7}})); + matrix.setCol(1, new IntegerMatrix(new int[][]{{-6}, {-8}})); + assertArrayEquals(new int[][]{{21, 20}, {19, 18}}, matrix.copyGrid()); + } + + //! addRow() + @Test + public void testAddRow_array_nullArray(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((int[])null); + }); + } + + @Test + public void testAddRow_array_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new int[0]); + }); + } + + @Test + public void testAddRow_array_invalidArrayLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new int[]{0, 0, 0}); + }); + } + + @Test + public void testAddRow_array(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addRow(new int[]{-5, -6}); + + assertArrayEquals(new int[][]{{25, 24}, {23, 22}, {21, 20}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.addRow((ModMatrix)null); + }); + } + + @Test + public void testAddRow_matrix_multipleRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new ModMatrix(new int[][]{{0, 0}, {0, 0}}, mod)); + }); + } + + @Test + public void testAddRow_matrix_noRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new ModMatrix(new int[0][0], mod)); + }); + } + + @Test + public void testAddRow_matrix_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addRow(new ModMatrix(new int[][]{{0, 0, 0}}, mod)); + }); + } + + @Test + public void testAddRow_matrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addRow(new ModMatrix(new int[][]{{-5, -6}}, mod)); + + assertArrayEquals(new int[][]{{25, 24}, {23, 22}, {21, 20}}, matrix.copyGrid()); + } + + @Test + public void testAddRow_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addRow(new IntegerMatrix(new int[][]{{-5, -6}})); + + assertArrayEquals(new int[][]{{25, 24}, {23, 22}, {21, 20}}, matrix.copyGrid()); + } + + //! addCol() + @Test + public void testAddCol_array_nullArray(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((int[])null); + }); + } + + @Test + public void testAddCol_array_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new int[0]); + }); + } + + @Test + public void testAddCol_array_invalidArrayLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new int[]{0, 0, 0}); + }); + } + + @Test + public void testAddCol_array(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addCol(new int[]{-5, -6}); + + assertArrayEquals(new int[][]{{25, 24, 21}, {23, 22, 20}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.addCol((ModMatrix)null); + }); + } + + @Test + public void testAddCol_matrix_multipleCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testAddCol_matrix_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new ModMatrix(new int[][]{{}}, mod)); + }); + } + + @Test + public void testAddCol_matrix_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.addCol(new ModMatrix(new int[][]{{0, 0, 0}}, mod)); + }); + } + + @Test + public void testAddCol_matrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addCol(new ModMatrix(new int[][]{{-5}, {-6}}, mod)); + + assertArrayEquals(new int[][]{{25, 24, 21}, {23, 22, 20}}, matrix.copyGrid()); + } + + @Test + public void testAddCol_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix.addCol(new IntegerMatrix(new int[][]{{-5}, {-6}})); + + assertArrayEquals(new int[][]{{25, 24, 21}, {23, 22, 20}}, matrix.copyGrid()); + } + + //! appendRight() + @Test + public void testAppendRight_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.appendRight((ModMatrix)null); + }); + } + + @Test + public void testAppendRight_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new ModMatrix(new int[0][0], mod)); + }); + } + + @Test + public void testAppendRight_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendRight(new ModMatrix(new int[][]{{0, 0, 0}}, mod)); + }); } @Test public void testAppendRight(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix secondMatrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); - assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.appendRight(matrix); + + assertArrayEquals(new int[][]{{25, 24, 25, 24}, {23, 22, 23, 22}}, matrix.copyGrid()); + } + + @Test + public void testAppendRight_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.appendRight(new IntegerMatrix(negativeGrid2)); + + assertArrayEquals(new int[][]{{25, 24, 25, 24}, {23, 22, 23, 22}}, matrix.copyGrid()); + } + + //! appendBottom() + @Test + public void testAppendBottom_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid appends - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendRight(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendRight(null); + matrix.appendBottom((ModMatrix)null); }); + } - //2x2 - 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(correctMatrix, matrix.appendRight(secondMatrix)); - //Integer matrix - IntegerMatrix thirdMatrix = new IntegerMatrix(grid2); - assertEquals(correctMatrix, matrix.appendRight(thirdMatrix)); + @Test + public void testAppendBottom_length0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //3x3 - 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(correctMatrix, matrix.appendRight(secondMatrix)); + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new ModMatrix(new int[0][0], mod)); + }); + } - //4x4 - 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(correctMatrix, matrix.appendRight(secondMatrix)); + @Test + public void testAppendBottom_invalidLength(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //10x10 - 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(correctMatrix, matrix.appendRight(secondMatrix)); + assertThrows(InvalidGeometryException.class, () -> { + matrix.appendBottom(new ModMatrix(new int[][]{{0}, {0}, {0}}, mod)); + }); } @Test public void testAppendBottom(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix secondMatrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1} - }, 26); - assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.appendBottom(matrix); + + assertArrayEquals(new int[][]{{25, 24}, {23, 22}, {25, 24}, {23, 22}}, matrix.copyGrid()); + } + + @Test + public void testAppendBottom_integerMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.appendBottom(new IntegerMatrix(negativeGrid2)); + + assertArrayEquals(new int[][]{{25, 24}, {23, 22}, {25, 24}, {23, 22}}, matrix.copyGrid()); + } + + //! add() + @Test + public void testAdd_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid appends - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.appendBottom(testMatrix2); - }); assertThrows(NullMatrixException.class, () -> { - testMatrix.appendBottom(null); + matrix.add((ModMatrix)null); }); - - //2x2 - 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(correctMatrix, matrix.appendBottom(secondMatrix)); - //Integer matrix - IntegerMatrix thirdMatrix = new IntegerMatrix(grid2); - assertEquals(correctMatrix, matrix.appendBottom(thirdMatrix)); - - //3x3 - 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(correctMatrix, matrix.appendBottom(secondMatrix)); - - //4x4 - 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(correctMatrix, matrix.appendBottom(secondMatrix)); - - //10x10 - 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(correctMatrix, matrix.appendBottom(secondMatrix)); } @Test - public void testIsSquare(){ - ModMatrix matrix = new ModMatrix(26); - assertFalse(matrix.isSquare()); + public void testAdd_matrix_fewRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - matrix = new ModMatrix(2, 2, 0, 26); - assertTrue(matrix.isSquare()); - - matrix = new ModMatrix(2, 3, 0, 26); - assertFalse(matrix.isSquare()); + assertThrows(InvalidGeometryException.class, () -> { + matrix.add(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); } @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(correctMatrix, matrix.add(transformMatrix)); - assertEquals(correctMatrix, matrix.add(1)); + public void testAdd_matrix_manyRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid adds - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); - final ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix2); + matrix.add(new ModMatrix(new int[][]{{0}, {0}}, mod)); }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.add(testMatrix3); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - transformMatrix = new ModMatrix(transformGrid2_1, 26); - correctMatrix = new ModMatrix(new int[][]{ - {2, 2}, - {2, 2} - }, 26); - assertEquals(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new ModMatrix(new int[][]{ - {2, 3}, - {2, 3} - }, 26); - assertEquals(correctMatrix, matrix.add(1)); - //Integer matrix - IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_1); - correctMatrix = new ModMatrix(new int[][]{ - {2, 2}, - {2, 2} - }, 26); - assertEquals(correctMatrix, matrix.add(transformMatrix2)); - - //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(correctMatrix, matrix.add(transformMatrix)); - correctMatrix = new ModMatrix(new int[][]{ - {2, 3, 4}, - {2, 3, 4}, - {2, 3, 4} - }, 26); - assertEquals(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(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(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(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(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(correctMatrix, matrix.subtract(transformMatrix)); - assertEquals(correctMatrix, matrix.subtract(1)); + public void testAdd_matrix_fewCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid subtracts - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); - final ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix2); + matrix.add(new ModMatrix(new int[][]{{0}}, mod)); }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.subtract(testMatrix3); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - transformMatrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(2, 2, 0, 26); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new ModMatrix(new int[][]{ - {0, 1}, - {0, 1} - }, 26); - assertEquals(correctMatrix, matrix.subtract(1)); - //Integer matrix - IntegerMatrix transformMatrix2 = new IntegerMatrix(grid2); - correctMatrix = new ModMatrix(2, 2, 0, 26); - assertEquals(correctMatrix, matrix.subtract(transformMatrix2)); - - //3x3 - matrix = new ModMatrix(grid3, 26); - transformMatrix = new ModMatrix(grid3, 26); - correctMatrix = new ModMatrix(3, 3, 0, 26); - assertEquals(correctMatrix, matrix.subtract(transformMatrix)); - correctMatrix = new ModMatrix(new int[][]{ - {0, 1, 2}, - {0, 1, 2}, - {0, 1, 2} - }, 26); - assertEquals(correctMatrix, matrix.subtract(1)); - - //4x4 - matrix = new ModMatrix(grid4, 26); - transformMatrix = new ModMatrix(grid4, 26); - correctMatrix = new ModMatrix(4, 4, 0, 26); - assertEquals(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(correctMatrix, matrix.subtract(1)); - - //10x10 - matrix = new ModMatrix(grid10, 26); - transformMatrix = new ModMatrix(grid10, 26); - correctMatrix = new ModMatrix(10, 10, 0, 26); - assertEquals(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(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(correctMatrix, matrix.multiply(transformMatrix)); - assertEquals(correctMatrix, matrix.multiply(2)); + public void testAdd_matrix_manyCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid multiplication - final ModMatrix testMatrix = new ModMatrix(grid1, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.multiply(testMatrix2); + matrix.add(new ModMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}}, mod)); }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - transformMatrix = new ModMatrix(transformGrid2_2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {6, 9}, - {6, 9} - }, 26); - assertEquals(correctMatrix, matrix.multiply(transformMatrix)); - ModMatrix vector = new ModMatrix(new int[][]{ - {2}, - {3} - }, 26); - correctMatrix = new ModMatrix(new int[][]{ - {8}, - {8} - }, 26); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new ModMatrix(new int[][]{ - {2, 4}, - {2, 4} - }, 26); - assertEquals(correctMatrix, matrix.multiply(2)); - //Integer matrix - IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); - correctMatrix = new ModMatrix(new int[][]{ - {6, 9}, - {6, 9} - }, 26); - assertEquals(correctMatrix, matrix.multiply(transformMatrix2)); - - //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(correctMatrix, matrix.multiply(transformMatrix)); - vector = new ModMatrix(new int[][]{ - {2}, - {3}, - {4} - }, 26); - correctMatrix = new ModMatrix(new int[][]{ - {20}, - {20}, - {20} - }, 26); - assertEquals(correctMatrix, matrix.multiply(vector)); - correctMatrix = new ModMatrix(new int[][]{ - {2, 4, 6}, - {2, 4, 6}, - {2, 4, 6} - }, 26); - assertEquals(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(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(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(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(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(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(correctMatrix, matrix.multiply(2)); } @Test - public void testDotProduct(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); - assertEquals(2, matrix.dotProduct(transformMatrix)); + public void testAdd_matrix_size2(){ + ModMatrix addMatrix = new ModMatrix(new int[][]{{22, 23}, {24, 25}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.add(addMatrix); - //Invalid products - ModMatrix testMatrix = new ModMatrix(grid1, 26); - ModMatrix testMatrix2 = new ModMatrix(grid2, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.dotProduct(testMatrix2); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - transformMatrix = new ModMatrix(transformGrid2_2, 26); - assertEquals(30, matrix.dotProduct(transformMatrix)); - //Integer matrix - IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); - assertEquals(30, matrix.dotProduct(transformMatrix2)); - - //3x3 - matrix = new ModMatrix(grid3, 26); - transformMatrix = new ModMatrix(transformGrid3_2, 26); - assertEquals(162, matrix.dotProduct(transformMatrix)); - - //4x4 - matrix = new ModMatrix(grid4, 26); - transformMatrix = new ModMatrix(transformGrid4_2, 26); - assertEquals(560, matrix.dotProduct(transformMatrix)); - - //10x10 - matrix = new ModMatrix(grid10, 26); - transformMatrix = new ModMatrix(transformGrid10_2, 26); - assertEquals(35750, matrix.dotProduct(transformMatrix)); + assertArrayEquals(new int[][]{{21, 21}, {21, 21}}, matrix.copyGrid()); } @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(correctMatrix, matrix.hadamardProduct(transformMatrix)); + public void testAdd_matrix_size10(){ + ModMatrix addMatrix = new ModMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }, mod); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + matrix = matrix.add(addMatrix); - //Invalid hadamard products - ModMatrix testMatrix = new ModMatrix(grid1, 26); - ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); - ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix2); - }); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.hadamardProduct(testMatrix3); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - transformMatrix = new ModMatrix(transformGrid2_2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {2, 6}, - {2, 6} - }, 26); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); - //Integer matrix - IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); - assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix2)); - - //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(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(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(correctMatrix, matrix.hadamardProduct(transformMatrix)); - } - - @Test - public void testTranspose(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, matrix.transpose()); - - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {1, 1}, - {2, 2} - }, 26); - assertEquals(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(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(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}, + assertArrayEquals(new int[][]{ {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(correctMatrix, matrix.transpose()); + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3} + }, matrix.copyGrid()); } @Test - public void testDeterminant(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); + public void testAdd_scalar(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.add(1); + + assertArrayEquals(new int[][]{{0, 25}, {24, 23}}, matrix.copyGrid()); + } + + @Test + public void testAdd_integerMatrix_size2(){ + IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{{22, 23}, {24, 25}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new int[][]{{21, 21}, {21, 21}}, matrix.copyGrid()); + } + + @Test + public void testAdd_integerMatrix_size10(){ + IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + matrix = matrix.add(addMatrix); + + assertArrayEquals(new int[][]{ + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + {3, 3, 3, 3, 3, 3, 3, 3, 3, 3} + }, matrix.copyGrid()); + } + + //! subtract() + @Test + public void testSubtract_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.subtract((ModMatrix)null); + }); + } + + @Test + public void testSubtract_matrix_fewRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testSubtract_matrix_manyRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new ModMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}}, mod)); + }); + } + + @Test + public void testSubtract_matrix_fewCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new ModMatrix(new int[][]{{0}, {0}}, mod)); + }); + } + + @Test + public void testSubtract_matrix_manyCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.subtract(new ModMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}}, mod)); + }); + } + + @Test + public void testSubtract_matrix_size2(){ + ModMatrix subMatrix = new ModMatrix(new int[][]{{-4, -3}, {-2, -1}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{{3, 1}, {25, 23}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_matrix_size10(){ + ModMatrix subMatrix = new ModMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }, mod); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{ + { 21, 19, 17, 15, 13, 11, 9, 7, 5, 3}, + { 1, 25, 23, 21, 19, 17, 15, 13, 11, 9}, + { 7, 5, 3, 1, 25, 23, 21, 19, 17, 15}, + { 13, 11, 9, 7, 5, 3, 1, 25, 23, 21}, + { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, + { 25, 23, 21, 19, 17, 15, 13, 11, 9, 7}, + { 5, 3, 1, 25, 23, 21, 19, 17, 15, 13}, + { 11, 9, 7, 5, 3, 1, 25, 23, 21, 19}, + { 17, 15, 13, 11, 9, 7, 5, 3, 1, 25}, + { 23, 21, 19, 17, 15, 13, 11, 9, 7, 5} + }, matrix.copyGrid()); + } + + @Test + public void testSubtract_scalar(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.subtract(1); + + assertArrayEquals(new int[][]{{24, 23}, {22, 21}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_integerMatrix_size2(){ + IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{{-4, -3}, {-2, -1}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{{3, 1}, {25, 23}}, matrix.copyGrid()); + } + + @Test + public void testSubtract_integerMatrix_size10(){ + IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{ + {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, + { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, + { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, + { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, + { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, + { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, + { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, + { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, + { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, + { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} + }); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + matrix = matrix.subtract(subMatrix); + + assertArrayEquals(new int[][]{ + { 21, 19, 17, 15, 13, 11, 9, 7, 5, 3}, + { 1, 25, 23, 21, 19, 17, 15, 13, 11, 9}, + { 7, 5, 3, 1, 25, 23, 21, 19, 17, 15}, + { 13, 11, 9, 7, 5, 3, 1, 25, 23, 21}, + { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, + { 25, 23, 21, 19, 17, 15, 13, 11, 9, 7}, + { 5, 3, 1, 25, 23, 21, 19, 17, 15, 13}, + { 11, 9, 7, 5, 3, 1, 25, 23, 21, 19}, + { 17, 15, 13, 11, 9, 7, 5, 3, 1, 25}, + { 23, 21, 19, 17, 15, 13, 11, 9, 7, 5} + }, matrix.copyGrid()); + } + + //! multiply() + @Test + public void testMultiply_matrix_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.multiply((ModMatrix)null); + }); + } + + @Test + public void testMultiply_matrix_manyRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new ModMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}}, mod)); + }); + } + + @Test + public void testMultiply_matrix_fewRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.multiply(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testMultiply_matrix_square(){ + ModMatrix mulMatrix = new ModMatrix(new int[][]{{1, 2}, {3, 4}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{19, 16}, {11, 4}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_matrix_rectangle(){ + ModMatrix mulMatrix = new ModMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{17, 14, 11}, {7, 0, 19}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_scalar(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.multiply(2); + + assertArrayEquals(new int[][]{{24, 22}, {20, 18}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_integerMatrix_square(){ + IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{19, 16}, {11, 4}}, matrix.copyGrid()); + } + + @Test + public void testMultiply_integerMatrix_rectangle(){ + IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + matrix = matrix.multiply(mulMatrix); + + assertArrayEquals(new int[][]{{17, 14, 11}, {7, 0, 19}}, matrix.copyGrid()); + } + + //! pow + @Test + public void testPow_rectangle(){ + ModMatrix matrix = new ModMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.pow(2); + }); + } + + @Test + public void testPow_negative(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidScalarException.class, () -> { + matrix.pow(-1); + }); + } + + @Test + public void testPow_0(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + ModMatrix result = matrix.pow(0); + + assertEquals(new ModMatrix(2, 2, 1, mod), result); + } + + @Test + public void testPow_2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + ModMatrix result = matrix.pow(2); + + assertEquals(new ModMatrix(new int[][]{{7, 10}, {15, 22}}, mod), result); + } + + @Test + public void testPow_3(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + ModMatrix result = matrix.pow(3); + + assertEquals(new ModMatrix(new int[][]{{15, 24}, {23, 12}}, mod), result); + } + + //! dotProduct() + @Test + public void testDotProduct_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.dotProduct((ModMatrix)null); + }); + } + + @Test + public void testDotProduct_fewRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testDotProduct_manyRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.dotProduct(new ModMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}}, mod)); + }); + } + + @Test + public void testDotProduct_size2(){ + ModMatrix dotMatrix = new ModMatrix(new int[][]{{1, 2}, {3, 4}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(24, result); + } + + @Test + public void testDotProduct_size10(){ + ModMatrix dotMatrix = new ModMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }, mod); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(10, result); + } + + @Test + public void testDotProduct_integerMatrix_size2(){ + IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(24, result); + } + + @Test + public void testDotProduct_integerMatrix_size10(){ + IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + int result = matrix.dotProduct(dotMatrix); + + assertEquals(10, result); + } + + //! hadamardProduct() + @Test + public void testHadamardProduct_nullMatrix(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(NullMatrixException.class, () -> { + matrix.hadamardProduct((ModMatrix)null); + }); + } + + @Test + public void testHadamardProduct_fewRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new ModMatrix(new int[][]{{0, 0}}, mod)); + }); + } + + @Test + public void testHadamardProduct_manyRows(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new ModMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}}, mod)); + }); + } + + @Test + public void testHadamardProduct_fewCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new ModMatrix(new int[][]{{0}, {0}}, mod)); + }); + } + + @Test + public void testHadamardProduct_manyCols(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.hadamardProduct(new ModMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}}, mod)); + }); + } + + @Test + public void testHadamardProduct_size2(){ + ModMatrix hadMatrix = new ModMatrix(new int[][]{{1, 2}, {3, 4}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + ModMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{{25, 22}, {17, 10}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_size10(){ + ModMatrix hadMatrix = new ModMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }, mod); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + ModMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{ + {25, 22, 17, 10, 1, 16, 3, 14, 23, 4}, + { 9, 12, 13, 12, 9, 4, 23, 14, 3, 16}, + { 1, 10, 17, 22, 25, 0, 25, 22, 17, 10}, + { 1, 16, 3, 14, 23, 4, 9, 12, 13, 12}, + { 9, 4, 23, 14, 3, 16, 1, 10, 17, 22}, + {25, 0, 25, 22, 17, 10, 1, 16, 3, 14}, + {23, 4, 9, 12, 13, 12, 9, 4, 23, 14}, + { 3, 16, 1, 10, 17, 22, 25, 0, 25, 22}, + {17, 10, 1, 16, 3, 14, 23, 4, 9, 12}, + {13, 12, 9, 4, 23, 14, 3, 16, 1, 10} + }, result.copyGrid()); + } + + @Test + public void testHadamardProduct_integerMatrix_size2(){ + IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + ModMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{{25, 22}, {17, 10}}, result.copyGrid()); + } + + @Test + public void testHadamardProduct_integerMatrix_size10(){ + IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{ + { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, + {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, + {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, + {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, + {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, + {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, + {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, + {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} + }); + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + ModMatrix result = matrix.hadamardProduct(hadMatrix); + + assertArrayEquals(new int[][]{ + {25, 22, 17, 10, 1, 16, 3, 14, 23, 4}, + { 9, 12, 13, 12, 9, 4, 23, 14, 3, 16}, + { 1, 10, 17, 22, 25, 0, 25, 22, 17, 10}, + { 1, 16, 3, 14, 23, 4, 9, 12, 13, 12}, + { 9, 4, 23, 14, 3, 16, 1, 10, 17, 22}, + {25, 0, 25, 22, 17, 10, 1, 16, 3, 14}, + {23, 4, 9, 12, 13, 12, 9, 4, 23, 14}, + { 3, 16, 1, 10, 17, 22, 25, 0, 25, 22}, + {17, 10, 1, 16, 3, 14, 23, 4, 9, 12}, + {13, 12, 9, 4, 23, 14, 3, 16, 1, 10} + }, result.copyGrid()); + } + + //! transpose() + @Test + public void testTranspose_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + ModMatrix result = matrix.transpose(); + + assertEquals(new ModMatrix(mod), result); + } + + @Test + public void testTranspose_size0x2(){ + int[][] grid = new int[0][2]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2x0(){ + int[][] grid = new int[2][0]; + ModMatrix matrix = new ModMatrix(grid, mod); + + assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(new int[][]{{25, 23}, {24, 22}}, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertArrayEquals(new int[][]{ + {25, 15, 5, 21, 11, 1, 17, 7, 23, 13}, + {24, 14, 4, 20, 10, 0, 16, 6, 22, 12}, + {23, 13, 3, 19, 9, 25, 15, 5, 21, 11}, + {22, 12, 2, 18, 8, 24, 14, 4, 20, 10}, + {21, 11, 1, 17, 7, 23, 13, 3, 19, 9}, + {20, 10, 0, 16, 6, 22, 12, 2, 18, 8}, + {19, 9, 25, 15, 5, 21, 11, 1, 17, 7}, + {18, 8, 24, 14, 4, 20, 10, 0, 16, 6}, + {17, 7, 23, 13, 3, 19, 9, 25, 15, 5}, + {16, 6, 22, 12, 2, 18, 8, 24, 14, 4} + }, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertArrayEquals(new int[][]{ + {25, 15}, + {24, 14}, + {23, 13}, + {22, 12}, + {21, 11}, + {20, 10}, + {19, 9}, + {18, 8}, + {17, 7}, + {16, 6} + }, matrix.transpose().copyGrid()); + } + + @Test + public void testTranspose_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertArrayEquals(new int[][]{ + {25, 23, 21, 19, 17, 15, 13, 11, 9, 7}, + {24, 22, 20, 18, 16, 14, 12, 10, 8, 6} + }, matrix.transpose().copyGrid()); + } + + //! determinant() / det() + @Test + public void testDeterminant_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size1(){ + ModMatrix matrix = new ModMatrix(new int[][]{{1}}, mod); + assertEquals(1, matrix.determinant()); - - //Invalid determinants - ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.determinant(); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - assertEquals(0, matrix.determinant()); - matrix = new ModMatrix(new int[][]{ - {1, 4}, - {4, 1} - }, 26); - assertEquals(-15, matrix.determinant()); - //det - assertEquals(matrix.determinant(), matrix.det()); - - //3x3 - matrix = new ModMatrix(grid3, 26); - assertEquals(0, matrix.determinant()); - matrix = new ModMatrix(new int[][]{ - {1, 4, 2}, - {2, 4, 1}, - {4, 1, 2} - }, 26); - assertEquals(-21, matrix.determinant()); - - //4x4 - matrix = new ModMatrix(grid4, 26); - assertEquals(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(160, matrix.determinant()); - - //10x10 - matrix = new ModMatrix(grid10, 26); - assertEquals(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(-10000000, matrix.determinant()); + assertEquals(1, matrix.det()); } @Test - public void testCofactor(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(grid1, 26); - assertEquals(correctMatrix, matrix.cofactor()); + public void testDeterminant_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); - //Invalid cofactor - ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); - assertThrows(InvalidGeometryException.class, () -> { - testMatrix.cofactor(); - }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {2, -1}, - {-2, 1} - }, 26); - assertEquals(correctMatrix, matrix.cofactor()); - //cof - assertEquals(matrix.cofactor(), matrix.cof()); - - //3x3 - matrix = new ModMatrix(grid3, 26); - correctMatrix = new ModMatrix(3, 3, 0, 26); - assertEquals(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(correctMatrix, matrix.cofactor()); - - //4x4 - matrix = new ModMatrix(grid4, 26); - correctMatrix = new ModMatrix(4, 4, 0, 26); - assertEquals(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(correctMatrix, matrix.cofactor()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new ModMatrix(grid10); - correctMatrix = new ModMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.cofactor()); - */ + assertEquals(-2, matrix.determinant()); + assertEquals(-2, matrix.det()); } @Test - public void testPower(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, matrix.pow(3)); + public void testDeterminant_size3(){ + ModMatrix matrix = new ModMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}, mod); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals(0, matrix.determinant()); + assertEquals(0, matrix.det()); + } + + @Test + public void testDeterminant_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); - //Invalid powers - final ModMatrix testMatrix = new ModMatrix(new int[][]{{0}, {0}}, 26); - final ModMatrix testMatrix2 = new ModMatrix(grid1, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.pow(1); + matrix.determinant(); }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + @Test + public void testDeterminant_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.determinant(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.det(); + }); + } + + //! cofactor() / cof() + @Test + public void testCofactor_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size1(){ + int[][] expectedGrid = new int[][]{{1}}; + ModMatrix matrix = new ModMatrix(new int[][]{{-1}}, mod); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2(){ + int[][] expectedGrid = new int[][]{{22, 3}, {2, 25}}; + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size10(){ + int[][] expectedGrid = 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}, + }; + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); + assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); + } + + @Test + public void testCofactor_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + @Test + public void testCofactor_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.cofactor(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.cof(); + }); + } + + //! adjoint() / adj() + @Test + public void testAdjoint_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size1(){ + int[][] expectedGrid = new int[][]{{1}}; + ModMatrix matrix = new ModMatrix(new int[][]{{-1}}, mod); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2(){ + int[][] expectedGrid = new int[][]{{22, 2}, {3, 25}}; + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size10(){ + int[][] expectedGrid = 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} + }; + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); + assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); + } + + @Test + public void testAdjoint_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + @Test + public void testAdjoint_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.adjoint(); + }); + assertThrows(InvalidGeometryException.class, () -> { + matrix.adj(); + }); + } + + //! inverse() + @Test + public void testInverse_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + @Test + public void testInverse_size1(){ + int[][] expectedGrid = new int[][]{{25}}; + ModMatrix matrix = new ModMatrix(new int[][]{{-1}}, mod); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size2(){ + int[][] expectedGrid = new int[][]{{24, 3}, {3, 22}}; + ModMatrix matrix = new ModMatrix(new int[][]{{4, 3}, {3, 2}}, mod); + + assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); + } + + @Test + public void testInverse_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + assertThrows(InvalidScalarException.class, () -> { - testMatrix2.pow(-1); + matrix.inverse(); }); - - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {9, 18}, - {9, 18} - }, 26); - assertEquals(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(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(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(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(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(correctMatrix, matrix.pow(3)); } @Test - public void testAdjoint(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(grid1, 26); - assertEquals(correctMatrix, matrix.adjoint()); + public void testInverse_size2x10(){ + ModMatrix matrix = new ModMatrix(negativeGrid2x10, mod); - //2x2 - matrix = new ModMatrix(grid2, 26); - correctMatrix = new ModMatrix(new int[][]{ - {2, -2}, - {-1, 1} - }, 26); - assertEquals(correctMatrix, matrix.adjoint()); - //adj - assertEquals(matrix.adjoint(), matrix.adj()); - - //3x3 - matrix = new ModMatrix(grid3, 26); - correctMatrix = new ModMatrix(3, 3, 0, 26); - assertEquals(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(correctMatrix, matrix.adjoint()); - - //10x10 - //?Skipping 10x10 test because test took > 5s by itself - /* - matrix = new ModMatrix(grid10); - correctMatrix = new ModMatrix(10, 10, 0); - assertEquals(correctMatrix, matrix.adjoint()); - */ - } - - @Test - public void testInverse(){ - //1x1 - ModMatrix matrix = new ModMatrix(grid1, 26); - ModMatrix correctMatrix = new ModMatrix(grid1, 26); - assertEquals(correctMatrix, matrix.inverse()); - - //Invalid inverse - ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); - ModMatrix testMatrix2 = new ModMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }, 26); - ModMatrix testMatrix3 = new ModMatrix(new int[][]{ - {1, 1}, - {0, 2} - }, 26); assertThrows(InvalidGeometryException.class, () -> { - testMatrix.inverse(); + matrix.inverse(); }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix2.inverse(); - }); - assertThrows(InvalidScalarException.class, () -> { - testMatrix3.inverse(); - }); - - //2x2 - matrix = new ModMatrix(new int[][]{ - {1, 4}, - {4, 1} - }, 26); - correctMatrix = new ModMatrix(new int[][]{ - {19, 2}, - {2, 19} - }, 26); - assertEquals(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(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(correctMatrix, matrix.inverse()); - - //10x10 - //?Skipped 10x10 because it would take a long time to compute } @Test - public void testGenerateIdentity(){ - //0x0 + public void testInverse_size10x2(){ + ModMatrix matrix = new ModMatrix(negativeGrid10x2, mod); + + assertThrows(InvalidGeometryException.class, () -> { + matrix.inverse(); + }); + } + + //! equals() + @Test + public void testEquals_null(){ + ModMatrix matrix = new ModMatrix(mod); + + assertFalse(matrix.equals(null)); + } + + @Test + public void testEquals_matrixObject(){ + ModMatrix equalsMatrix = new ModMatrix(negativeGrid2, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertTrue(matrix.equals((Object)equalsMatrix)); + } + + @Test + public void testEquals_array(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertTrue(matrix.equals(negativeGrid2)); + } + + @Test + public void testEquals_invalidType(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertFalse(matrix.equals(1)); + } + + @Test + public void testEquals_manyRows(){ + ModMatrix equalsMatrix = new ModMatrix(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewRows(){ + ModMatrix equalsMatrix = new ModMatrix(new int[][]{{-1, -2}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_manyCols(){ + ModMatrix equalsMatrix = new ModMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_fewCols(){ + ModMatrix equalsMatrix = new ModMatrix(new int[][]{{-1}, {-2}}, mod); + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_notEquals(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + ModMatrix equalsMatrix = matrix.transpose(); + + assertFalse(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_equals(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + ModMatrix equalsMatrix = new ModMatrix(negativeGrid2, mod); + + assertTrue(matrix.equals(equalsMatrix)); + } + + @Test + public void testEquals_self(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertTrue(matrix.equals(matrix)); + } + + //! hashCode() + @Test + public void testHashCode_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size1(){ + ModMatrix matrix = new ModMatrix(new int[][]{{1}}, mod); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + @Test + public void testHashCode_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); + } + + //! toString() + @Test + public void testToString_size0(){ + ModMatrix matrix = new ModMatrix(mod); + + assertEquals("[]\nmod(26)", matrix.toString()); + } + + @Test + public void testToString_size1(){ + ModMatrix matrix = new ModMatrix(new int[][]{{1}}, mod); + + assertEquals("[1]\nmod(26)", matrix.toString()); + } + + @Test + public void testToString_size2(){ + ModMatrix matrix = new ModMatrix(negativeGrid2, mod); + + assertEquals("[25, 24]\n[23, 22]\nmod(26)", matrix.toString()); + } + + @Test + public void testToString_size10(){ + ModMatrix matrix = new ModMatrix(negativeGrid10, mod); + + assertEquals( + "[25, 24, 23, 22, 21, 20, 19, 18, 17, 16]\n" + + "[15, 14, 13, 12, 11, 10, 9, 8, 7, 6]\n" + + "[5, 4, 3, 2, 1, 0, 25, 24, 23, 22]\n" + + "[21, 20, 19, 18, 17, 16, 15, 14, 13, 12]\n" + + "[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]\n" + + "[1, 0, 25, 24, 23, 22, 21, 20, 19, 18]\n" + + "[17, 16, 15, 14, 13, 12, 11, 10, 9, 8]\n" + + "[7, 6, 5, 4, 3, 2, 1, 0, 25, 24]\n" + + "[23, 22, 21, 20, 19, 18, 17, 16, 15, 14]\n" + + "[13, 12, 11, 10, 9, 8, 7, 6, 5, 4]\n" + + "mod(26)", + matrix.toString()); + } + + //! generateIdentity() + @Test + public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { ModMatrix.generateIdentity(0); }); + } - //1x1 - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals(correctMatrix, ModMatrix.generateIdentity(1)); + @Test + public void testGenerateIdentity_negativeSize(){ + assertThrows(InvalidGeometryException.class, () -> { + ModMatrix.generateIdentity(-1); + }); + } - //2x2 - correctMatrix = new ModMatrix(new int[][]{ - {1, 0}, - {0, 1} - }, 26); - assertEquals(correctMatrix, ModMatrix.generateIdentity(2)); + @Test + public void testGenerateIdentity_size2(){ + ModMatrix matrix = ModMatrix.generateIdentity(2); - //3x3 - correctMatrix = new ModMatrix(new int[][]{ - {1, 0, 0}, - {0, 1, 0}, - {0, 0, 1} - }, 26); - assertEquals(correctMatrix, ModMatrix.generateIdentity(3)); + assertArrayEquals(new int[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); + } - //4x4 - correctMatrix = new ModMatrix(new int[][]{ - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - }, 26); - assertEquals(correctMatrix, ModMatrix.generateIdentity(4)); + @Test + public void testGenerateIdentity_size3(){ + ModMatrix matrix = ModMatrix.generateIdentity(3); - //10x10 - correctMatrix = new ModMatrix(new int[][]{ + assertArrayEquals(new int[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); + } + + @Test + public void testGenerateIdentity_size10(){ + ModMatrix matrix = ModMatrix.generateIdentity(10); + + assertArrayEquals(new int[][]{ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, @@ -1820,62 +2654,8 @@ public class TestModMatrix{ {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - }, 26); - assertEquals(correctMatrix, ModMatrix.generateIdentity(10)); + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + }, matrix.copyGrid()); } - @Test - public void testHashCode(){ - ModMatrix matrix = new ModMatrix(26); - assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); - } - - @Test - public void testToString(){ - ModMatrix matrix = new ModMatrix(grid3, 26); - String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]\nmod(26)"; - assertEquals(matrixString, matrix.toString()); - } - - @Test - public void testLaplaceExpansionHelper(){ - ModMatrix matrix = new ModMatrix(Integer.MAX_VALUE); - matrix.addRow(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix.laplaceExpansionHelper(0, 0); - }); - - ModMatrix matrix2 = new ModMatrix(Integer.MAX_VALUE); - matrix2.setGrid(grid1); - assertThrows(InvalidGeometryException.class, () -> { - matrix2.laplaceExpansionHelper(0, 0); - }); - - ModMatrix matrix3 = new ModMatrix(Integer.MAX_VALUE); - matrix3.setGrid(grid2); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(-1, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(2, 0); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, -1); - }); - assertThrows(InvalidCoordinatesException.class, () -> { - matrix3.laplaceExpansionHelper(0, 2); - }); - - ModMatrix matrix4 = new ModMatrix(Integer.MAX_VALUE); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - matrix4.addCol(grid2[1]); - matrix4.addCol(grid2[0]); - assertThrows(InvalidGeometryException.class, () -> { - matrix4.laplaceExpansionHelper(0, 0); - }); - } } diff --git a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java index 81f042d..bdbd137 100644 --- a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java +++ b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java @@ -1,37 +1,55 @@ //Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java //Mattrixwv // Created: 04-13-23 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class TestInvalidCoordinatesException{ - private String message = "message"; - private Throwable cause = new Exception(); + private static final String message = "message"; + private static final Throwable cause = new Exception(); @Test - public void testConstructor(){ + public void testConstructor_default(){ InvalidCoordinatesException exception = new InvalidCoordinatesException(); assertNull(exception.getMessage()); assertNull(exception.getCause()); - - exception = new InvalidCoordinatesException(message); + } + @Test + public void testConstructor_message(){ + InvalidCoordinatesException exception = new InvalidCoordinatesException(message); assertEquals(message, exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidCoordinatesException(cause); + @Test + public void testConstructor_cause(){ + InvalidCoordinatesException exception = new InvalidCoordinatesException(cause); assertEquals(cause.toString(), exception.getMessage()); assertEquals(cause, exception.getCause()); - - exception = new InvalidCoordinatesException(message, cause); + } + @Test + public void testConstructor_messageCause(){ + InvalidCoordinatesException exception = new InvalidCoordinatesException(message, cause); assertEquals(message, exception.getMessage()); assertEquals(cause, exception.getCause()); } + + @Test + public void testConstructor_elements(){ + int givenElements = 3; + int neededElements = 2; + String elementsMessage = "Invalid number of elements " + givenElements + " must be at most " + neededElements; + + InvalidCoordinatesException exception = new InvalidCoordinatesException(givenElements, neededElements); + + assertEquals(elementsMessage, exception.getMessage()); + assertNull(exception.getCause()); + } } diff --git a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java index 7662194..0003a54 100644 --- a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java +++ b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java @@ -1,36 +1,44 @@ //Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java //Mattrixwv // Created: 04-13-23 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class TestInvalidGeometryException{ - private String message = "message"; - private Throwable cause = new Exception(); + private static final String message = "message"; + private static final Throwable cause = new Exception(); @Test - public void testConstructor(){ + public void testConstructor_default(){ InvalidGeometryException exception = new InvalidGeometryException(); assertNull(exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidGeometryException(message); + @Test + public void testConstructor_message(){ + InvalidGeometryException exception = new InvalidGeometryException(message); assertEquals(message, exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidGeometryException(cause); + @Test + public void testConstructor_cause(){ + InvalidGeometryException exception = new InvalidGeometryException(cause); assertEquals(cause.toString(), exception.getMessage()); assertEquals(cause, exception.getCause()); + } - exception = new InvalidGeometryException(message, cause); + @Test + public void testConstructor_messageCause(){ + InvalidGeometryException exception = new InvalidGeometryException(message, cause); assertEquals(message, exception.getMessage()); assertEquals(cause, exception.getCause()); } diff --git a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java index fcf5349..eba4635 100644 --- a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java +++ b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java @@ -1,36 +1,44 @@ //Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java //Mattrixwv // Created: 04-13-23 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class TestInvalidRowSizeException{ - private String message = "message"; - private Throwable cause = new Exception(); + private static final String message = "message"; + private static final Throwable cause = new Exception(); @Test - public void testConstructor(){ + public void testConstructor_default(){ InvalidRowSizeException exception = new InvalidRowSizeException(); assertNull(exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidRowSizeException(message); + @Test + public void testConstructor_message(){ + InvalidRowSizeException exception = new InvalidRowSizeException(message); assertEquals(message, exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidRowSizeException(cause); + @Test + public void testConstructor_cause(){ + InvalidRowSizeException exception = new InvalidRowSizeException(cause); assertEquals(cause.toString(), exception.getMessage()); assertEquals(cause, exception.getCause()); + } - exception = new InvalidRowSizeException(message, cause); + @Test + public void testConstructor_messageCause(){ + InvalidRowSizeException exception = new InvalidRowSizeException(message, cause); assertEquals(message, exception.getMessage()); assertEquals(cause, exception.getCause()); } diff --git a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java index ccb44d7..c014706 100644 --- a/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java +++ b/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java @@ -1,36 +1,44 @@ //Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java //Mattrixwv // Created: 04-13-23 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class TestInvalidScalarException{ - private String message = "message"; - private Throwable cause = new Exception(); + private static final String message = "message"; + private static final Throwable cause = new Exception(); @Test - public void testConstructor(){ + public void testConstructor_default(){ InvalidScalarException exception = new InvalidScalarException(); assertNull(exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidScalarException(message); + @Test + public void testConstructor_message(){ + InvalidScalarException exception = new InvalidScalarException(message); assertEquals(message, exception.getMessage()); assertNull(exception.getCause()); + } - exception = new InvalidScalarException(cause); + @Test + public void testConstructor_cause(){ + InvalidScalarException exception = new InvalidScalarException(cause); assertEquals(cause.toString(), exception.getMessage()); assertEquals(cause, exception.getCause()); + } - exception = new InvalidScalarException(message, cause); + @Test + public void testConstructor_messageCause(){ + InvalidScalarException exception = new InvalidScalarException(message, cause); assertEquals(message, exception.getMessage()); assertEquals(cause, exception.getCause()); } diff --git a/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java b/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java index ff5a682..81c4ed8 100644 --- a/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java +++ b/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java @@ -1,36 +1,43 @@ //Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java //Mattrixwv // Created: 04-13-23 -//Modified: 04-13-23 +//Modified: 08-11-24 package com.mattrixwv.matrix.exceptions; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class TestNullMatrixException{ - private String message = "message"; - private Throwable cause = new Exception(); + private static final String message = "message"; + private static final Throwable cause = new Exception(); @Test - public void testConstructor(){ + public void testConstructor_default(){ NullMatrixException exception = new NullMatrixException(); assertNull(exception.getMessage()); assertNull(exception.getCause()); + } - exception = new NullMatrixException(message); + @Test + public void testConstructor_message(){ + NullMatrixException exception = new NullMatrixException(message); assertEquals(message, exception.getMessage()); assertNull(exception.getCause()); + } - exception = new NullMatrixException(cause); + @Test + public void testConstructor_cause(){ + NullMatrixException exception = new NullMatrixException(cause); assertEquals(cause.toString(), exception.getMessage()); assertEquals(cause, exception.getCause()); - - exception = new NullMatrixException(message, cause); + } + @Test + public void testConstructor_messageCause(){ + NullMatrixException exception = new NullMatrixException(message, cause); assertEquals(message, exception.getMessage()); assertEquals(cause, exception.getCause()); } diff --git a/src/test/resources/junit-platform.properties b/src/test/resources/junit-platform.properties new file mode 100644 index 0000000..08b9f59 --- /dev/null +++ b/src/test/resources/junit-platform.properties @@ -0,0 +1,4 @@ +#junit.jupiter.execution.parallel.enabled=true +#junit.jupiter.execution.parallel.config.strategy=dynamic +#junit.jupiter.execution.parallel.mode.default=concurrent +#junit.jupiter.execution.parallel.mode.classes.default=concurrent