From ee300f3da3a4195024914ec7f7134f07f347d193 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 29 Jun 2022 10:41:37 -0400 Subject: [PATCH] Updated Sonarqube findings --- .../mattrixwv/matrix/BigIntegerMatrix.java | 259 ++++++++---------- .../com/mattrixwv/matrix/DoubleMatrix.java | 257 ++++++++--------- .../com/mattrixwv/matrix/IntegerMatrix.java | 254 ++++++++--------- .../java/com/mattrixwv/matrix/LongMatrix.java | 254 ++++++++--------- .../java/com/mattrixwv/matrix/ModMatrix.java | 63 ++--- .../InvalidCoordinatesException.java | 3 + .../exceptions/InvalidGeometryException.java | 3 + .../matrix/TestBigIntegerMatrix.java | 35 ++- 8 files changed, 530 insertions(+), 598 deletions(-) diff --git a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java index 48e9d8c..70eff5d 100644 --- a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java @@ -1,11 +1,12 @@ //Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 02-17-22 +//Modified: 06-29-22 package com.mattrixwv.matrix; import java.math.BigInteger; +import java.util.Arrays; import java.util.StringJoiner; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; @@ -37,9 +38,7 @@ public class BigIntegerMatrix{ //Copy every element over to a new grid BigInteger[][] newGrid = new BigInteger[grid.length][grid[0].length]; for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ - for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ - newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt]; - } + newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length); } //Save the new grid @@ -52,9 +51,7 @@ public class BigIntegerMatrix{ //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - for(int col = 0;col < grid[0].length;++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid.length); } //Return the new grid @@ -160,9 +157,7 @@ public class BigIntegerMatrix{ //Generate a copy of the row BigInteger[][] newRow = new BigInteger[1][grid[row].length]; - for(int col = 0;col < grid[row].length;++col){ - newRow[0][col] = grid[row][col]; - } + newRow[0] = Arrays.copyOf(grid[row], grid[row].length); //Return the new matrix return new BigIntegerMatrix(newRow); @@ -219,13 +214,11 @@ public class BigIntegerMatrix{ } //Make sure the number of elements is valid if(elements.length != getNumCols()){ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Save the elements - for(int col = 0;col < elements.length;++col){ - grid[row][col] = elements[col]; - } + grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, BigIntegerMatrix matrix){ //Make sure the matrix has a single row @@ -243,7 +236,7 @@ public class BigIntegerMatrix{ } //Make sure the number of elements is valid if(elements.length != grid.length){ - throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length); + throw new InvalidCoordinatesException(elements.length, grid.length); } //Save the elements @@ -267,18 +260,16 @@ public class BigIntegerMatrix{ BigInteger[][] newGrid = new BigInteger[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - newGrid[row] = grid[row]; + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } grid = newGrid; } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Add all elements to the grid - for(int col = 0;col < elements.length;++col){ - grid[grid.length - 1][col] = elements[col]; - } + grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(BigIntegerMatrix matrix){ //Make sure the matrix has a single row @@ -297,20 +288,12 @@ public class BigIntegerMatrix{ else if(grid.length == elements.length){ //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - BigInteger[] workingRow = new BigInteger[grid[row].length + 1]; - for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ - workingRow[workingCol] = grid[row][workingCol]; - } - grid[row] = workingRow; + grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1); + grid[row][grid[row].length - 1] = elements[row]; } } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); - } - - //Add all elements to the grid - for(int row = 0;row < elements.length;++row){ - grid[row][grid[row].length - 1] = elements[row]; + throw new InvalidGeometryException(elements.length, getNumCols()); } } public void addCol(BigIntegerMatrix matrix){ @@ -332,9 +315,8 @@ public class BigIntegerMatrix{ BigInteger[][] newGrid = new BigInteger[getNumRows()][getNumCols() + rightSide.getNumCols()]; for(int row = 0;row < getNumRows();++row){ //Set all elements from the current grid's row - for(int col = 0;col < getNumCols();++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length); + //Set all elements from the right side grid's row for(int col = 0;col < rightSide.getNumCols();++col){ newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; @@ -511,7 +493,7 @@ public class BigIntegerMatrix{ } //Create a new matrix for the product - BigIntegerMatrix newMatrix = clone(); + BigIntegerMatrix newMatrix = new BigIntegerMatrix(this); //Multiply the current grid power times for(int currentPower = 1;currentPower < power;++currentPower){ newMatrix = newMatrix.multiply(this); @@ -578,6 +560,82 @@ public class BigIntegerMatrix{ public BigInteger det(){ return determinant(); } + private BigInteger det2(){ + return (grid[0][0].multiply(grid[1][1])).subtract(grid[0][1].multiply(grid[1][0])); + } + 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])); + } + private BigInteger det4(){ + BigInteger det = BigInteger.ZERO; + + //Find the row/column with the largest number of 0's + int zerosLocation = 0; + int maxNumZeros = 0; + boolean zerosRow = true; + //Check the rows + for(int row = 0;row < getNumRows();++row){ + int numZeros = 0; + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col].equals(BigInteger.ZERO)){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = row; + zerosRow = true; + } + } + //Check the columns + for(int col = 0;col < getNumCols();++col){ + int numZeros = 0; + for(int row = 0;row < getNumRows();++row){ + if(grid[row][col].equals(BigInteger.ZERO)){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = col; + zerosRow = false; + } + } + + //If the largest number of zeros were found in a row + if(zerosRow){ + //Set a variable to make sure the appropriate + or - is applied to the scalar + BigInteger multiplier = BigInteger.ONE; + if((zerosLocation % 2) == 1){ + multiplier = BigInteger.ONE.negate(); + } + //Go through every column in the optimal row using the formula + for(int col = 0;col < getNumCols();++col){ + if(!grid[zerosLocation][col].equals(BigInteger.ZERO)){ + det = det.add(multiplier.multiply(grid[zerosLocation][col]).multiply(laplaceExpansionHelper(zerosLocation, col).determinant())); + } + multiplier = multiplier.negate(); + } + } + //If the largest number of zeros were found in a column + else{ + //Set a variable to make sure the appropriate + or - is applied to the scalar + BigInteger multiplier = BigInteger.ONE; + if((zerosLocation % 2) == 1){ + multiplier = BigInteger.ONE.negate(); + } + //Go through every row in the coptimal column using the formula + for(int row = 0;row < getNumRows();++row){ + if(!grid[row][zerosLocation].equals(BigInteger.ZERO)){ + det = det.add(multiplier.multiply(grid[row][zerosLocation]).multiply(laplaceExpansionHelper(row, zerosLocation).determinant())); + } + multiplier = multiplier.negate(); + } + } + + return det; + } public BigInteger determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -586,87 +644,16 @@ public class BigIntegerMatrix{ //?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix //Determine the formula do use for the determinant - BigInteger det = BigInteger.ZERO; + BigInteger det; switch(getNumRows()){ //If the matrix is 1x1 return the number case 1 : det = grid[0][0]; break; //If the matrix is 2x2 use the formula - case 2 : { - det = (grid[0][0].multiply(grid[1][1])).subtract(grid[0][1].multiply(grid[1][0])); - } - break; + case 2 : det = det2(); break; //If the matrix is 3x3 use the formula - case 3 : { - det = 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])); - } - break; + case 3 : det = det3(); break; //If the matrix is larger break it down and try again - default : { - //Find the row/column with the largest number of 0's - int zerosLocation = 0; - int maxNumZeros = 0; - boolean zerosRow = true; - //Check the rows - for(int row = 0;row < getNumRows();++row){ - int numZeros = 0; - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col].equals(BigInteger.ZERO)){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = row; - zerosRow = true; - } - } - //Check the columns - for(int col = 0;col < getNumCols();++col){ - int numZeros = 0; - for(int row = 0;row < getNumRows();++row){ - if(grid[row][col].equals(BigInteger.ZERO)){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = col; - zerosRow = false; - } - } - - //If the largest number of zeros were found in a row - if(zerosRow){ - //Set a variable to make sure the appropriate + or - is applied to the scalar - BigInteger multiplier = BigInteger.ONE; - if((zerosLocation % 2) == 1){ - multiplier = BigInteger.ONE.negate(); - } - //Go through every column in the optimal row using the formula - for(int col = 0;col < getNumCols();++col){ - if(!grid[zerosLocation][col].equals(BigInteger.ZERO)){ - det = det.add(multiplier.multiply(grid[zerosLocation][col]).multiply(laplaceExpansionHelper(zerosLocation, col).determinant())); - } - multiplier = multiplier.negate(); - } - } - //If the largest number of zeros were found in a column - else{ - //Set a variable to make sure the appropriate + or - is applied to the scalar - BigInteger multiplier = BigInteger.ONE; - if((zerosLocation % 2) == 1){ - multiplier = BigInteger.ONE.negate(); - } - //Go through every row in the coptimal column using the formula - for(int row = 0;row < getNumRows();++row){ - if(!grid[row][zerosLocation].equals(BigInteger.ZERO)){ - det = det.add(multiplier.multiply(grid[row][zerosLocation]).multiply(laplaceExpansionHelper(row, zerosLocation).determinant())); - } - multiplier = multiplier.negate(); - } - } - } + default : det = det4(); } //Return the determinant @@ -726,41 +713,41 @@ public class BigIntegerMatrix{ //Object funtions @Override public boolean equals(Object rightSide){ - if(rightSide.getClass().equals(this.getClass())){ - BigIntegerMatrix rightMatrix = (BigIntegerMatrix)rightSide; - - //Make sure they have the same number of elements - if(getNumRows() != rightMatrix.getNumRows()){ - return false; - } - else if(getNumCols() != rightMatrix.getNumCols()){ - return false; - } - - //Check every element - for(int row = 0;row < getNumRows();++row){ - for(int col = 0;col < getNumCols();++col){ - if(!grid[row][col].equals(rightMatrix.grid[row][col])){ - return false; - } - } - } - - //If false hasn't been returned yet then they are equal - return true; + if(rightSide == null){ + return false; + } + else if(rightSide.getClass().equals(this.getClass())){ + return equals((BigIntegerMatrix) rightSide); } else if(rightSide.getClass().equals(BigInteger[][].class)){ BigInteger[][] rightMatrix = (BigInteger[][])rightSide; - return equals(new BigIntegerMatrix(rightMatrix)); } else{ return false; } } + private boolean equals(BigIntegerMatrix rightMatrix){ + //Make sure they have the same number of elements + if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(!grid[row][col].equals(rightMatrix.grid[row][col])){ + return false; + } + } + } + + //If false hasn't been returned yet then they are equal + return true; + } @Override public int hashCode(){ - return grid.hashCode(); + return Arrays.hashCode(grid); } @Override public String toString(){ @@ -776,8 +763,4 @@ public class BigIntegerMatrix{ } return matrix.toString(); } - @Override - public BigIntegerMatrix clone(){ - return new BigIntegerMatrix(grid); - } } diff --git a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java index 8e4c9b0..60dd376 100644 --- a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java @@ -1,10 +1,11 @@ //Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 02-17-22 +//Modified: 06-29-22 package com.mattrixwv.matrix; +import java.util.Arrays; import java.util.StringJoiner; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; @@ -37,9 +38,7 @@ public class DoubleMatrix{ //Copy every element over to a new grid double[][] newGrid = new double[grid.length][grid[0].length]; for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ - for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ - newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt]; - } + newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length); } //Save the new grid @@ -52,9 +51,7 @@ public class DoubleMatrix{ //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - for(int col = 0;col < grid[0].length;++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid.length); } //Return the new grid @@ -164,9 +161,7 @@ public class DoubleMatrix{ //Generate a copy of the row double[][] newRow = new double[1][grid[row].length]; - for(int col = 0;col < grid[row].length;++col){ - newRow[0][col] = grid[row][col]; - } + newRow[0] = Arrays.copyOf(grid[row], grid[row].length); //Return the new matrix return new DoubleMatrix(newRow); @@ -223,13 +218,11 @@ public class DoubleMatrix{ } //Make sure the number of elements is valid if(elements.length != getNumCols()){ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Save the elements - for(int col = 0;col < elements.length;++col){ - grid[row][col] = elements[col]; - } + grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, DoubleMatrix matrix){ //Make sure the matrix has a single row @@ -247,7 +240,7 @@ public class DoubleMatrix{ } //Make sure the number of elements is valid if(elements.length != grid.length){ - throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length); + throw new InvalidCoordinatesException(elements.length, grid.length); } //Save the elements @@ -271,18 +264,16 @@ public class DoubleMatrix{ double[][] newGrid = new double[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - newGrid[row] = grid[row]; + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } grid = newGrid; } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Add all elements to the grid - for(int col = 0;col < elements.length;++col){ - grid[grid.length - 1][col] = elements[col]; - } + grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(DoubleMatrix matrix){ //Make sure the matrix has a single row @@ -301,20 +292,12 @@ public class DoubleMatrix{ else if(grid.length == elements.length){ //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - double[] workingRow = new double[grid[row].length + 1]; - for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ - workingRow[workingCol] = grid[row][workingCol]; - } - grid[row] = workingRow; + grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1); + grid[row][grid[row].length - 1] = elements[row]; } } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); - } - - //Add all elements to the grid - for(int row = 0;row < elements.length;++row){ - grid[row][grid[row].length - 1] = elements[row]; + throw new InvalidGeometryException(elements.length, getNumCols()); } } public void addCol(DoubleMatrix matrix){ @@ -336,9 +319,8 @@ public class DoubleMatrix{ double[][] newGrid = new double[getNumRows()][getNumCols() + rightSide.getNumCols()]; for(int row = 0;row < getNumRows();++row){ //Set all elements from the current grid's row - for(int col = 0;col < getNumCols();++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length); + //Set all elements from the right side grid's row for(int col = 0;col < rightSide.getNumCols();++col){ newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; @@ -529,7 +511,7 @@ public class DoubleMatrix{ } //Create a new matrix for the product - DoubleMatrix newMatrix = clone(); + DoubleMatrix newMatrix = new DoubleMatrix(this); //Multiply the current grid power times for(int currentPower = 1;currentPower < power;++currentPower){ newMatrix = newMatrix.multiply(this); @@ -596,6 +578,82 @@ public class DoubleMatrix{ public double det(){ return determinant(); } + private double det2(){ + return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); + } + 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]); + } + private double det4(){ + double det = 0; + + //Find the row/column with the largest number of 0's + int zerosLocation = 0; + int maxNumZeros = 0; + boolean zerosRow = true; + //Check the rows + for(int row = 0;row < getNumRows();++row){ + int numZeros = 0; + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = row; + zerosRow = true; + } + } + //Check the columns + for(int col = 0;col < getNumCols();++col){ + int numZeros = 0; + for(int row = 0;row < getNumRows();++row){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = col; + zerosRow = false; + } + } + + //If the largest number of zeros were found in a row + if(zerosRow){ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every column in the optimal row using the formula + for(int col = 0;col < getNumCols();++col){ + if(grid[zerosLocation][col] != 0){ + det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); + } + multiplier = -multiplier; + } + } + //If the largest number of zeros were found in a column + else{ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every row in the coptimal column using the formula + for(int row = 0;row < getNumRows();++row){ + if(grid[row][zerosLocation] != 0){ + det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); + } + multiplier = -multiplier; + } + } + + return det; + } public double determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -609,82 +667,11 @@ public class DoubleMatrix{ //If the matrix is 1x1 return the number case 1 : det = grid[0][0]; break; //If the matrix is 2x2 use the formula - case 2 : { - det = (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); - } - break; + case 2 : det = det2(); break; //If the matrix is 3x3 use the formula - case 3 : { - det = (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]); - } - break; + case 3 : det = det3(); break; //If the matrix is larger break it down and try again - default : { - //Find the row/column with the largest number of 0's - int zerosLocation = 0; - int maxNumZeros = 0; - boolean zerosRow = true; - //Check the rows - for(int row = 0;row < getNumRows();++row){ - int numZeros = 0; - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = row; - zerosRow = true; - } - } - //Check the columns - for(int col = 0;col < getNumCols();++col){ - int numZeros = 0; - for(int row = 0;row < getNumRows();++row){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = col; - zerosRow = false; - } - } - - //If the largest number of zeros were found in a row - if(zerosRow){ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every column in the optimal row using the formula - for(int col = 0;col < getNumCols();++col){ - if(grid[zerosLocation][col] != 0){ - det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); - } - multiplier = -multiplier; - } - } - //If the largest number of zeros were found in a column - else{ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every row in the coptimal column using the formula - for(int row = 0;row < getNumRows();++row){ - if(grid[row][zerosLocation] != 0){ - det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); - } - multiplier = -multiplier; - } - } - } + default : det = det4(); } //Return the determinant @@ -744,38 +731,38 @@ public class DoubleMatrix{ //Object funtions @Override public boolean equals(Object rightSide){ - if(rightSide.getClass().equals(this.getClass())){ - DoubleMatrix rightMatrix = (DoubleMatrix)rightSide; - - //Make sure they have the same number of elements - if(getNumRows() != rightMatrix.getNumRows()){ - return false; - } - else if(getNumCols() != rightMatrix.getNumCols()){ - return false; - } - - //Check every element - for(int row = 0;row < getNumRows();++row){ - for(int col = 0;col < getNumCols();++col){ - if(Math.abs(grid[row][col] - rightMatrix.grid[row][col]) > delta){ - return false; - } - } - } - - //If false hasn't been returned yet then they are equal - return true; + if(rightSide == null){ + return false; + } + else if(rightSide.getClass().equals(this.getClass())){ + return equals((DoubleMatrix) rightSide); } else if(rightSide.getClass().equals(double[][].class)){ double[][] rightMatrix = (double[][])rightSide; - return equals(new DoubleMatrix(rightMatrix)); } else{ return false; } } + private boolean equals(DoubleMatrix rightMatrix){ + //Make sure they have the same number of elements + if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(Math.abs(grid[row][col] - rightMatrix.grid[row][col]) > delta){ + return false; + } + } + } + + //If false hasn't been returned yet then they are equal + return true; + } public boolean equals(Object rightSide, double delta){ setEqualsDelta(delta); return equals(rightSide); @@ -785,7 +772,7 @@ public class DoubleMatrix{ } @Override public int hashCode(){ - return grid.hashCode(); + return Arrays.hashCode(grid); } @Override public String toString(){ @@ -801,8 +788,4 @@ public class DoubleMatrix{ } return matrix.toString(); } - @Override - public DoubleMatrix clone(){ - return new DoubleMatrix(grid); - } } diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 8b80213..2d16c04 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -1,10 +1,11 @@ //Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-17-22 +//Modified: 06-29-22 package com.mattrixwv.matrix; +import java.util.Arrays; import java.util.StringJoiner; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; @@ -36,9 +37,7 @@ public class IntegerMatrix{ //Copy every element over to a new grid int[][] newGrid = new int[grid.length][grid[0].length]; for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ - for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ - newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt]; - } + newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length); } //Save the new grid @@ -51,9 +50,7 @@ public class IntegerMatrix{ //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - for(int col = 0;col < grid[0].length;++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid.length); } //Return the new grid @@ -159,9 +156,7 @@ public class IntegerMatrix{ //Generate a copy of the row int[][] newRow = new int[1][grid[row].length]; - for(int col = 0;col < grid[row].length;++col){ - newRow[0][col] = grid[row][col]; - } + newRow[0] = Arrays.copyOf(grid[row], grid[row].length); //Return the new matrix return new IntegerMatrix(newRow); @@ -218,13 +213,11 @@ public class IntegerMatrix{ } //Make sure the number of elements is valid if(elements.length != getNumCols()){ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Save the elements - for(int col = 0;col < elements.length;++col){ - grid[row][col] = elements[col]; - } + grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, IntegerMatrix matrix){ //Make sure the matrix has a single row @@ -242,7 +235,7 @@ public class IntegerMatrix{ } //Make sure the number of elements is valid if(elements.length != grid.length){ - throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length); + throw new InvalidCoordinatesException(elements.length, grid.length); } //Save the elements @@ -266,18 +259,16 @@ public class IntegerMatrix{ int[][] newGrid = new int[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - newGrid[row] = grid[row]; + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } grid = newGrid; } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Add all elements to the grid - for(int col = 0;col < elements.length;++col){ - grid[grid.length - 1][col] = elements[col]; - } + grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(IntegerMatrix matrix){ //Make sure the matrix has a single row @@ -296,20 +287,12 @@ public class IntegerMatrix{ else if(grid.length == elements.length){ //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - int[] workingRow = new int[grid[row].length + 1]; - for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ - workingRow[workingCol] = grid[row][workingCol]; - } - grid[row] = workingRow; + grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1); + grid[row][grid[row].length - 1] = elements[row]; } } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); - } - - //Add all elements to the grid - for(int row = 0;row < elements.length;++row){ - grid[row][grid[row].length - 1] = elements[row]; + throw new InvalidGeometryException(elements.length, getNumCols()); } } public void addCol(IntegerMatrix matrix){ @@ -331,9 +314,8 @@ public class IntegerMatrix{ int[][] newGrid = new int[getNumRows()][getNumCols() + rightSide.getNumCols()]; for(int row = 0;row < getNumRows();++row){ //Set all elements from the current grid's row - for(int col = 0;col < getNumCols();++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length); + //Set all elements from the right side grid's row for(int col = 0;col < rightSide.getNumCols();++col){ newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; @@ -510,7 +492,7 @@ public class IntegerMatrix{ } //Create a new matrix for the product - IntegerMatrix newMatrix = clone(); + IntegerMatrix newMatrix = new IntegerMatrix(this); //Multiply the current grid power times for(int currentPower = 1;currentPower < power;++currentPower){ newMatrix = newMatrix.multiply(this); @@ -577,6 +559,81 @@ public class IntegerMatrix{ public int det(){ return determinant(); } + private int det2(){ + return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); + } + 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]); + } + private int det4(){ + int det = 0; + //Find the row/column with the largest number of 0's + int zerosLocation = 0; + int maxNumZeros = 0; + boolean zerosRow = true; + //Check the rows + for(int row = 0;row < getNumRows();++row){ + int numZeros = 0; + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = row; + zerosRow = true; + } + } + //Check the columns + for(int col = 0;col < getNumCols();++col){ + int numZeros = 0; + for(int row = 0;row < getNumRows();++row){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = col; + zerosRow = false; + } + } + + //If the largest number of zeros were found in a row + if(zerosRow){ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every column in the optimal row using the formula + for(int col = 0;col < getNumCols();++col){ + if(grid[zerosLocation][col] != 0){ + det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); + } + multiplier = -multiplier; + } + } + //If the largest number of zeros were found in a column + else{ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every row in the coptimal column using the formula + for(int row = 0;row < getNumRows();++row){ + if(grid[row][zerosLocation] != 0){ + det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); + } + multiplier = -multiplier; + } + } + + return det; + } public int determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -590,82 +647,11 @@ public class IntegerMatrix{ //If the matrix is 1x1 return the number case 1 : det = grid[0][0]; break; //If the matrix is 2x2 use the formula - case 2 : { - det = (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); - } - break; + case 2 : det = det2(); break; //If the matrix is 3x3 use the formula - case 3 : { - det = (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]); - } - break; + case 3 : det = det3(); break; //If the matrix is larger break it down and try again - default : { - //Find the row/column with the largest number of 0's - int zerosLocation = 0; - int maxNumZeros = 0; - boolean zerosRow = true; - //Check the rows - for(int row = 0;row < getNumRows();++row){ - int numZeros = 0; - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = row; - zerosRow = true; - } - } - //Check the columns - for(int col = 0;col < getNumCols();++col){ - int numZeros = 0; - for(int row = 0;row < getNumRows();++row){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = col; - zerosRow = false; - } - } - - //If the largest number of zeros were found in a row - if(zerosRow){ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every column in the optimal row using the formula - for(int col = 0;col < getNumCols();++col){ - if(grid[zerosLocation][col] != 0){ - det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); - } - multiplier = -multiplier; - } - } - //If the largest number of zeros were found in a column - else{ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every row in the coptimal column using the formula - for(int row = 0;row < getNumRows();++row){ - if(grid[row][zerosLocation] != 0){ - det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); - } - multiplier = -multiplier; - } - } - } + default : det = det4(); } //Return the determinant @@ -725,41 +711,41 @@ public class IntegerMatrix{ //Object funtions @Override public boolean equals(Object rightSide){ + if(rightSide == null){ + return false; + } if(rightSide.getClass().equals(this.getClass())){ - IntegerMatrix rightMatrix = (IntegerMatrix)rightSide; - - //Make sure they have the same number of elements - if(getNumRows() != rightMatrix.getNumRows()){ - return false; - } - else if(getNumCols() != rightMatrix.getNumCols()){ - return false; - } - - //Check every element - for(int row = 0;row < getNumRows();++row){ - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] != rightMatrix.grid[row][col]){ - return false; - } - } - } - - //If false hasn't been returned yet then they are equal - return true; + return equals((IntegerMatrix)rightSide); } else if(rightSide.getClass().equals(int[][].class)){ int[][] rightMatrix = (int[][])rightSide; - return equals(new IntegerMatrix(rightMatrix)); } else{ return false; } } + private boolean equals(IntegerMatrix rightMatrix){ + //Make sure they have the same number of elements + if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] != rightMatrix.grid[row][col]){ + return false; + } + } + } + + //If false hasn't been returned yet then they are equal + return true; + } @Override public int hashCode(){ - return grid.hashCode(); + return Arrays.hashCode(grid); } @Override public String toString(){ @@ -775,8 +761,4 @@ public class IntegerMatrix{ } return matrix.toString(); } - @Override - public IntegerMatrix clone(){ - return new IntegerMatrix(grid); - } } diff --git a/src/main/java/com/mattrixwv/matrix/LongMatrix.java b/src/main/java/com/mattrixwv/matrix/LongMatrix.java index 0905636..7fc8439 100644 --- a/src/main/java/com/mattrixwv/matrix/LongMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/LongMatrix.java @@ -1,10 +1,11 @@ //Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 02-17-22 +//Modified: 06-29-22 package com.mattrixwv.matrix; +import java.util.Arrays; import java.util.StringJoiner; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; @@ -36,9 +37,7 @@ public class LongMatrix{ //Copy every element over to a new grid long[][] newGrid = new long[grid.length][grid[0].length]; for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ - for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ - newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt]; - } + newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length); } //Save the new grid @@ -51,9 +50,7 @@ public class LongMatrix{ //Copy every element from the current grid to the new one for(int row = 0;row < grid.length;++row){ - for(int col = 0;col < grid[0].length;++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid.length); } //Return the new grid @@ -159,9 +156,7 @@ public class LongMatrix{ //Generate a copy of the row long[][] newRow = new long[1][grid[row].length]; - for(int col = 0;col < grid[row].length;++col){ - newRow[0][col] = grid[row][col]; - } + newRow[0] = Arrays.copyOf(grid[row], grid[row].length); //Return the new matrix return new LongMatrix(newRow); @@ -218,13 +213,11 @@ public class LongMatrix{ } //Make sure the number of elements is valid if(elements.length != getNumCols()){ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Save the elements - for(int col = 0;col < elements.length;++col){ - grid[row][col] = elements[col]; - } + grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, LongMatrix matrix){ //Make sure the matrix has a single row @@ -242,7 +235,7 @@ public class LongMatrix{ } //Make sure the number of elements is valid if(elements.length != grid.length){ - throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length); + throw new InvalidCoordinatesException(elements.length, grid.length); } //Save the elements @@ -266,18 +259,16 @@ public class LongMatrix{ long[][] newGrid = new long[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - newGrid[row] = grid[row]; + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length); } grid = newGrid; } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); + throw new InvalidGeometryException(elements.length, getNumCols()); } //Add all elements to the grid - for(int col = 0;col < elements.length;++col){ - grid[grid.length - 1][col] = elements[col]; - } + grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(LongMatrix matrix){ //Make sure the matrix has a single row @@ -296,20 +287,12 @@ public class LongMatrix{ else if(grid.length == elements.length){ //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ - long[] workingRow = new long[grid[row].length + 1]; - for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ - workingRow[workingCol] = grid[row][workingCol]; - } - grid[row] = workingRow; + grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1); + grid[row][grid[row].length - 1] = elements[row]; } } else{ - throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols()); - } - - //Add all elements to the grid - for(int row = 0;row < elements.length;++row){ - grid[row][grid[row].length - 1] = elements[row]; + throw new InvalidGeometryException(elements.length, getNumCols()); } } public void addCol(LongMatrix matrix){ @@ -331,9 +314,8 @@ public class LongMatrix{ long[][] newGrid = new long[getNumRows()][getNumCols() + rightSide.getNumCols()]; for(int row = 0;row < getNumRows();++row){ //Set all elements from the current grid's row - for(int col = 0;col < getNumCols();++col){ - newGrid[row][col] = grid[row][col]; - } + newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length); + //Set all elements from the right side grid's row for(int col = 0;col < rightSide.getNumCols();++col){ newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; @@ -510,7 +492,7 @@ public class LongMatrix{ } //Create a new matrix for the product - LongMatrix newMatrix = clone(); + LongMatrix newMatrix = new LongMatrix(this); //Multiply the current grid power times for(int currentPower = 1;currentPower < power;++currentPower){ newMatrix = newMatrix.multiply(this); @@ -577,6 +559,81 @@ public class LongMatrix{ public long det(){ return determinant(); } + public long det2(){ + return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); + } + 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]); + } + public long det4(){ + long det = 0; + //Find the row/column with the largest number of 0's + int zerosLocation = 0; + int maxNumZeros = 0; + boolean zerosRow = true; + //Check the rows + for(int row = 0;row < getNumRows();++row){ + int numZeros = 0; + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = row; + zerosRow = true; + } + } + //Check the columns + for(int col = 0;col < getNumCols();++col){ + int numZeros = 0; + for(int row = 0;row < getNumRows();++row){ + if(grid[row][col] == 0){ + ++numZeros; + } + } + if(numZeros > maxNumZeros){ + maxNumZeros = numZeros; + zerosLocation = col; + zerosRow = false; + } + } + + //If the largest number of zeros were found in a row + if(zerosRow){ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every column in the optimal row using the formula + for(int col = 0;col < getNumCols();++col){ + if(grid[zerosLocation][col] != 0){ + det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); + } + multiplier = -multiplier; + } + } + //If the largest number of zeros were found in a column + else{ + //Set a variable to make sure the appropriate + or - is applied to the scalar + int multiplier = 1; + if((zerosLocation % 2) == 1){ + multiplier = -1; + } + //Go through every row in the coptimal column using the formula + for(int row = 0;row < getNumRows();++row){ + if(grid[row][zerosLocation] != 0){ + det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); + } + multiplier = -multiplier; + } + } + + return det; + } public long determinant(){ //Make sure the matrix is square if(!isSquare()){ @@ -590,82 +647,11 @@ public class LongMatrix{ //If the matrix is 1x1 return the number case 1 : det = grid[0][0]; break; //If the matrix is 2x2 use the formula - case 2 : { - det = (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]); - } - break; + case 2 : det = det2(); break; //If the matrix is 3x3 use the formula - case 3 : { - det = (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]); - } - break; + case 3 : det = det3(); break; //If the matrix is larger break it down and try again - default : { - //Find the row/column with the largest number of 0's - int zerosLocation = 0; - int maxNumZeros = 0; - boolean zerosRow = true; - //Check the rows - for(int row = 0;row < getNumRows();++row){ - int numZeros = 0; - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = row; - zerosRow = true; - } - } - //Check the columns - for(int col = 0;col < getNumCols();++col){ - int numZeros = 0; - for(int row = 0;row < getNumRows();++row){ - if(grid[row][col] == 0){ - ++numZeros; - } - } - if(numZeros > maxNumZeros){ - maxNumZeros = numZeros; - zerosLocation = col; - zerosRow = false; - } - } - - //If the largest number of zeros were found in a row - if(zerosRow){ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every column in the optimal row using the formula - for(int col = 0;col < getNumCols();++col){ - if(grid[zerosLocation][col] != 0){ - det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant()); - } - multiplier = -multiplier; - } - } - //If the largest number of zeros were found in a column - else{ - //Set a variable to make sure the appropriate + or - is applied to the scalar - int multiplier = 1; - if((zerosLocation % 2) == 1){ - multiplier = -1; - } - //Go through every row in the coptimal column using the formula - for(int row = 0;row < getNumRows();++row){ - if(grid[row][zerosLocation] != 0){ - det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant()); - } - multiplier = -multiplier; - } - } - } + default : det = det4(); } //Return the determinant @@ -725,41 +711,41 @@ public class LongMatrix{ //Object funtions @Override public boolean equals(Object rightSide){ + if(rightSide == null){ + return false; + } if(rightSide.getClass().equals(this.getClass())){ - LongMatrix rightMatrix = (LongMatrix)rightSide; - - //Make sure they have the same number of elements - if(getNumRows() != rightMatrix.getNumRows()){ - return false; - } - else if(getNumCols() != rightMatrix.getNumCols()){ - return false; - } - - //Check every element - for(int row = 0;row < getNumRows();++row){ - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] != rightMatrix.grid[row][col]){ - return false; - } - } - } - - //If false hasn't been returned yet then they are equal - return true; + return equals((LongMatrix)rightSide); } else if(rightSide.getClass().equals(long[][].class)){ long[][] rightMatrix = (long[][])rightSide; - return equals(new LongMatrix(rightMatrix)); } else{ return false; } } + private boolean equals(LongMatrix rightMatrix){ + //Make sure they have the same number of elements + if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] != rightMatrix.grid[row][col]){ + return false; + } + } + } + + //If false hasn't been returned yet then they are equal + return true; + } @Override public int hashCode(){ - return grid.hashCode(); + return Arrays.hashCode(grid); } @Override public String toString(){ @@ -775,8 +761,4 @@ public class LongMatrix{ } return matrix.toString(); } - @Override - public LongMatrix clone(){ - return new LongMatrix(grid); - } } diff --git a/src/main/java/com/mattrixwv/matrix/ModMatrix.java b/src/main/java/com/mattrixwv/matrix/ModMatrix.java index 5036a3e..381fe90 100644 --- a/src/main/java/com/mattrixwv/matrix/ModMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/ModMatrix.java @@ -1,10 +1,12 @@ //Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java //Mattrixwv // Created: 02-09-22 -//Modified: 02-09-22 +//Modified: 06-29-22 package com.mattrixwv.matrix; +import java.util.Arrays; + import com.mattrixwv.matrix.exceptions.InvalidGeometryException; import com.mattrixwv.matrix.exceptions.InvalidScalarException; @@ -215,7 +217,7 @@ public class ModMatrix extends IntegerMatrix{ } //Create a new matrix for the product - ModMatrix newMatrix = clone(); + ModMatrix newMatrix = new ModMatrix(this); //Multiply the current grid power times for(int currentPower = 1;currentPower < power;++currentPower){ newMatrix = newMatrix.multiply(this); @@ -245,14 +247,6 @@ public class ModMatrix extends IntegerMatrix{ return new ModMatrix(super.transpose(), mod); } @Override - public int det(){ - return determinant(); - } - @Override - public int determinant(){ - return super.determinant(); - } - @Override public ModMatrix cof(){ return cofactor(); } @@ -322,28 +316,11 @@ public class ModMatrix extends IntegerMatrix{ //Object functions @Override public boolean equals(Object rightSide){ + if(rightSide == null){ + return false; + } if(rightSide.getClass().equals(this.getClass())){ - ModMatrix rightMatrix = (ModMatrix)rightSide; - - //Make sure they have the same number of elements - if(getNumRows() != rightMatrix.getNumRows()){ - return false; - } - else if(getNumCols() != rightMatrix.getNumCols()){ - return false; - } - - //Check every element - for(int row = 0;row < getNumRows();++row){ - for(int col = 0;col < getNumCols();++col){ - if(grid[row][col] != rightMatrix.grid[row][col]){ - return false; - } - } - } - - //If false hasn't been return yet then they are equal - return true; + return equals((ModMatrix)rightSide); } else if(rightSide.getClass().equals(int[][].class)){ int[][] rightMatrix = (int[][])rightSide; @@ -354,16 +331,30 @@ public class ModMatrix extends IntegerMatrix{ return false; } } + public boolean equals(ModMatrix rightMatrix){ + //Make sure they have the same number of elements + if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){ + return false; + } + + //Check every element + for(int row = 0;row < getNumRows();++row){ + for(int col = 0;col < getNumCols();++col){ + if(grid[row][col] != rightMatrix.grid[row][col]){ + return false; + } + } + } + + //If false hasn't been return yet then they are equal + return true; + } @Override public int hashCode(){ - return grid.hashCode(); + return Arrays.hashCode(grid); } @Override public String toString(){ return super.toString() + "\nmod(" + mod + ")"; } - @Override - public ModMatrix clone(){ - return new ModMatrix(grid, mod); - } } diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java index 57c2daf..2f0b3f1 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidCoordinatesException.java @@ -20,4 +20,7 @@ public class InvalidCoordinatesException extends RuntimeException{ public InvalidCoordinatesException(String message, Throwable throwable){ super(message, throwable); } + 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 ccfccbc..37f2f4f 100644 --- a/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java +++ b/src/main/java/com/mattrixwv/matrix/exceptions/InvalidGeometryException.java @@ -20,4 +20,7 @@ public class InvalidGeometryException extends RuntimeException{ public InvalidGeometryException(String message, Throwable throwable){ super(message, throwable); } + public InvalidGeometryException(int givenElements, int neededElements){ + super("Invalid number of elements " + givenElements + " must be " + neededElements); + } } diff --git a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java index 82189b8..2cbac62 100644 --- a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java @@ -119,38 +119,43 @@ public class TestBigIntegerMatrix{ public void testEquals(){ //1x1 BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - assertTrue("BigIntegerMatrix 1x1 failed equals BigIntegerMatrix.", matrix.equals(matrix)); + boolean gridEquals = matrix.equals(matrix); + assertTrue("BigIntegerMatrix 1x1 failed equals BigIntegerMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") - boolean gridEquals = matrix.equals(grid1); - assertTrue("BigIntegerMatrix 1x1 failed equals BigInteger[][].", gridEquals); + boolean gridEquals1 = matrix.equals(grid1); + assertTrue("BigIntegerMatrix 1x1 failed equals BigInteger[][].", gridEquals1); //2x2 matrix = new BigIntegerMatrix(grid2); - assertTrue("BigIntegerMatrix 2x2 failed equals BigIntegerMatrix.", matrix.equals(matrix)); + boolean gridEquals2 = matrix.equals(matrix); + assertTrue("BigIntegerMatrix 2x2 failed equals BigIntegerMatrix.", gridEquals2); @SuppressWarnings("unlikely-arg-type") - boolean gridEquals2 = matrix.equals(grid2); - assertTrue("BigIntegerMatrix 2x2 failed equals BigInteger[][].", gridEquals2); + boolean gridEquals21 = matrix.equals(grid2); + assertTrue("BigIntegerMatrix 2x2 failed equals BigInteger[][].", gridEquals21); //3x3 matrix = new BigIntegerMatrix(grid3); - assertTrue("BigIntegerMatrix 3x3 failed equals BigIntegerMatrix.", matrix.equals(matrix)); + boolean gridEquals3 = matrix.equals(matrix); + assertTrue("BigIntegerMatrix 3x3 failed equals BigIntegerMatrix.", gridEquals3); @SuppressWarnings("unlikely-arg-type") - boolean gridEquals3 = matrix.equals(grid3); - assertTrue("BigIntegerMatrix 3x3 failed equals BigInteger[][].", gridEquals3); + boolean gridEquals31 = matrix.equals(grid3); + assertTrue("BigIntegerMatrix 3x3 failed equals BigInteger[][].", gridEquals31); //4x4 matrix = new BigIntegerMatrix(grid4); - assertTrue("BigIntegerMatrix 4x4 failed equals BigIntegerMatrix.", matrix.equals(matrix)); + boolean gridEquals4 = matrix.equals(matrix); + assertTrue("BigIntegerMatrix 4x4 failed equals BigIntegerMatrix.", gridEquals4); @SuppressWarnings("unlikely-arg-type") - boolean gridEquals4 = matrix.equals(grid4); - assertTrue("BigIntegerMatrix 4x4 failed equals BigInteger[][].", gridEquals4); + boolean gridEquals41 = matrix.equals(grid4); + assertTrue("BigIntegerMatrix 4x4 failed equals BigInteger[][].", gridEquals41); //10x10 matrix = new BigIntegerMatrix(grid10); - assertTrue("BigIntegerMatrix = 10x10 failed equals BigIntegerMatrix.", matrix.equals(matrix)); + boolean gridEquals10 = matrix.equals(matrix); + assertTrue("BigIntegerMatrix = 10x10 failed equals BigIntegerMatrix.", gridEquals10); @SuppressWarnings("unlikely-arg-type") - boolean gridEquals10 = matrix.equals(grid10); - assertTrue("BigIntegerMatrix 10x10 failed equals BigInteger[][].", gridEquals10); + boolean gridEquals101 = matrix.equals(grid10); + assertTrue("BigIntegerMatrix 10x10 failed equals BigInteger[][].", gridEquals101); } @Test