Updated Sonarqube findings

This commit is contained in:
2022-06-29 10:41:37 -04:00
parent f73c7830c5
commit ee300f3da3
8 changed files with 530 additions and 598 deletions

View File

@@ -1,11 +1,12 @@
//Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java //Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java
//Mattrixwv //Mattrixwv
// Created: 02-10-22 // Created: 02-10-22
//Modified: 02-17-22 //Modified: 06-29-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringJoiner; import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
@@ -37,9 +38,7 @@ public class BigIntegerMatrix{
//Copy every element over to a new grid //Copy every element over to a new grid
BigInteger[][] newGrid = new BigInteger[grid.length][grid[0].length]; BigInteger[][] newGrid = new BigInteger[grid.length][grid[0].length];
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length);
newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt];
}
} }
//Save the new grid //Save the new grid
@@ -52,9 +51,7 @@ public class BigIntegerMatrix{
//Copy every element from the current grid to the new one //Copy every element from the current grid to the new one
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){ newGrid[row] = Arrays.copyOf(grid[row], grid.length);
newGrid[row][col] = grid[row][col];
}
} }
//Return the new grid //Return the new grid
@@ -160,9 +157,7 @@ public class BigIntegerMatrix{
//Generate a copy of the row //Generate a copy of the row
BigInteger[][] newRow = new BigInteger[1][grid[row].length]; BigInteger[][] newRow = new BigInteger[1][grid[row].length];
for(int col = 0;col < grid[row].length;++col){ newRow[0] = Arrays.copyOf(grid[row], grid[row].length);
newRow[0][col] = grid[row][col];
}
//Return the new matrix //Return the new matrix
return new BigIntegerMatrix(newRow); return new BigIntegerMatrix(newRow);
@@ -219,13 +214,11 @@ public class BigIntegerMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != getNumCols()){ 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 //Save the elements
for(int col = 0;col < elements.length;++col){ grid[row] = Arrays.copyOf(elements, elements.length);
grid[row][col] = elements[col];
}
} }
public void setRow(int row, BigIntegerMatrix matrix){ public void setRow(int row, BigIntegerMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -243,7 +236,7 @@ public class BigIntegerMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != grid.length){ 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 //Save the elements
@@ -267,18 +260,16 @@ public class BigIntegerMatrix{
BigInteger[][] newGrid = new BigInteger[grid.length + 1][elements.length]; BigInteger[][] newGrid = new BigInteger[grid.length + 1][elements.length];
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
newGrid[row] = grid[row]; newGrid[row] = Arrays.copyOf(grid[row], grid[row].length);
} }
grid = newGrid; grid = newGrid;
} }
else{ 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 //Add all elements to the grid
for(int col = 0;col < elements.length;++col){ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
grid[grid.length - 1][col] = elements[col];
}
} }
public void addRow(BigIntegerMatrix matrix){ public void addRow(BigIntegerMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -297,20 +288,12 @@ public class BigIntegerMatrix{
else if(grid.length == elements.length){ else if(grid.length == elements.length){
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
BigInteger[] workingRow = new BigInteger[grid[row].length + 1]; grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1);
for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ grid[row][grid[row].length - 1] = elements[row];
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
} }
} }
else{ 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 row = 0;row < elements.length;++row){
grid[row][grid[row].length - 1] = elements[row];
} }
} }
public void addCol(BigIntegerMatrix matrix){ public void addCol(BigIntegerMatrix matrix){
@@ -332,9 +315,8 @@ public class BigIntegerMatrix{
BigInteger[][] newGrid = new BigInteger[getNumRows()][getNumCols() + rightSide.getNumCols()]; BigInteger[][] newGrid = new BigInteger[getNumRows()][getNumCols() + rightSide.getNumCols()];
for(int row = 0;row < getNumRows();++row){ for(int row = 0;row < getNumRows();++row){
//Set all elements from the current grid's row //Set all elements from the current grid's row
for(int col = 0;col < getNumCols();++col){ newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length);
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's row //Set all elements from the right side grid's row
for(int col = 0;col < rightSide.getNumCols();++col){ for(int col = 0;col < rightSide.getNumCols();++col){
newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; newGrid[row][getNumCols() + col] = rightSide.grid[row][col];
@@ -511,7 +493,7 @@ public class BigIntegerMatrix{
} }
//Create a new matrix for the product //Create a new matrix for the product
BigIntegerMatrix newMatrix = clone(); BigIntegerMatrix newMatrix = new BigIntegerMatrix(this);
//Multiply the current grid power times //Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){ for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this); newMatrix = newMatrix.multiply(this);
@@ -578,31 +560,16 @@ public class BigIntegerMatrix{
public BigInteger det(){ public BigInteger det(){
return determinant(); return determinant();
} }
public BigInteger determinant(){ private BigInteger det2(){
//Make sure the matrix is square return (grid[0][0].multiply(grid[1][1])).subtract(grid[0][1].multiply(grid[1][0]));
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
} }
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix 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
//Determine the formula do use for the determinant
BigInteger det = BigInteger.ZERO;
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;
//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])); (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; private BigInteger det4(){
//If the matrix is larger break it down and try again BigInteger det = BigInteger.ZERO;
default : {
//Find the row/column with the largest number of 0's //Find the row/column with the largest number of 0's
int zerosLocation = 0; int zerosLocation = 0;
int maxNumZeros = 0; int maxNumZeros = 0;
@@ -666,7 +633,27 @@ public class BigIntegerMatrix{
multiplier = multiplier.negate(); multiplier = multiplier.negate();
} }
} }
return det;
} }
public BigInteger determinant(){
//Make sure the matrix is square
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
}
//?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;
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 = det2(); break;
//If the matrix is 3x3 use the formula
case 3 : det = det3(); break;
//If the matrix is larger break it down and try again
default : det = det4();
} }
//Return the determinant //Return the determinant
@@ -726,14 +713,23 @@ public class BigIntegerMatrix{
//Object funtions //Object funtions
@Override @Override
public boolean equals(Object rightSide){ public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){ if(rightSide == null){
BigIntegerMatrix rightMatrix = (BigIntegerMatrix)rightSide;
//Make sure they have the same number of elements
if(getNumRows() != rightMatrix.getNumRows()){
return false; return false;
} }
else if(getNumCols() != rightMatrix.getNumCols()){ 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; return false;
} }
@@ -749,18 +745,9 @@ public class BigIntegerMatrix{
//If false hasn't been returned yet then they are equal //If false hasn't been returned yet then they are equal
return true; return true;
} }
else if(rightSide.getClass().equals(BigInteger[][].class)){
BigInteger[][] rightMatrix = (BigInteger[][])rightSide;
return equals(new BigIntegerMatrix(rightMatrix));
}
else{
return false;
}
}
@Override @Override
public int hashCode(){ public int hashCode(){
return grid.hashCode(); return Arrays.hashCode(grid);
} }
@Override @Override
public String toString(){ public String toString(){
@@ -776,8 +763,4 @@ public class BigIntegerMatrix{
} }
return matrix.toString(); return matrix.toString();
} }
@Override
public BigIntegerMatrix clone(){
return new BigIntegerMatrix(grid);
}
} }

View File

@@ -1,10 +1,11 @@
//Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java //Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java
//Mattrixwv //Mattrixwv
// Created: 02-07-22 // Created: 02-07-22
//Modified: 02-17-22 //Modified: 06-29-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
import java.util.Arrays;
import java.util.StringJoiner; import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
@@ -37,9 +38,7 @@ public class DoubleMatrix{
//Copy every element over to a new grid //Copy every element over to a new grid
double[][] newGrid = new double[grid.length][grid[0].length]; double[][] newGrid = new double[grid.length][grid[0].length];
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length);
newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt];
}
} }
//Save the new grid //Save the new grid
@@ -52,9 +51,7 @@ public class DoubleMatrix{
//Copy every element from the current grid to the new one //Copy every element from the current grid to the new one
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){ newGrid[row] = Arrays.copyOf(grid[row], grid.length);
newGrid[row][col] = grid[row][col];
}
} }
//Return the new grid //Return the new grid
@@ -164,9 +161,7 @@ public class DoubleMatrix{
//Generate a copy of the row //Generate a copy of the row
double[][] newRow = new double[1][grid[row].length]; double[][] newRow = new double[1][grid[row].length];
for(int col = 0;col < grid[row].length;++col){ newRow[0] = Arrays.copyOf(grid[row], grid[row].length);
newRow[0][col] = grid[row][col];
}
//Return the new matrix //Return the new matrix
return new DoubleMatrix(newRow); return new DoubleMatrix(newRow);
@@ -223,13 +218,11 @@ public class DoubleMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != getNumCols()){ 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 //Save the elements
for(int col = 0;col < elements.length;++col){ grid[row] = Arrays.copyOf(elements, elements.length);
grid[row][col] = elements[col];
}
} }
public void setRow(int row, DoubleMatrix matrix){ public void setRow(int row, DoubleMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -247,7 +240,7 @@ public class DoubleMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != grid.length){ 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 //Save the elements
@@ -271,18 +264,16 @@ public class DoubleMatrix{
double[][] newGrid = new double[grid.length + 1][elements.length]; double[][] newGrid = new double[grid.length + 1][elements.length];
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
newGrid[row] = grid[row]; newGrid[row] = Arrays.copyOf(grid[row], grid[row].length);
} }
grid = newGrid; grid = newGrid;
} }
else{ 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 //Add all elements to the grid
for(int col = 0;col < elements.length;++col){ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
grid[grid.length - 1][col] = elements[col];
}
} }
public void addRow(DoubleMatrix matrix){ public void addRow(DoubleMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -301,20 +292,12 @@ public class DoubleMatrix{
else if(grid.length == elements.length){ else if(grid.length == elements.length){
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
double[] workingRow = new double[grid[row].length + 1]; grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1);
for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ grid[row][grid[row].length - 1] = elements[row];
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
} }
} }
else{ 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 row = 0;row < elements.length;++row){
grid[row][grid[row].length - 1] = elements[row];
} }
} }
public void addCol(DoubleMatrix matrix){ public void addCol(DoubleMatrix matrix){
@@ -336,9 +319,8 @@ public class DoubleMatrix{
double[][] newGrid = new double[getNumRows()][getNumCols() + rightSide.getNumCols()]; double[][] newGrid = new double[getNumRows()][getNumCols() + rightSide.getNumCols()];
for(int row = 0;row < getNumRows();++row){ for(int row = 0;row < getNumRows();++row){
//Set all elements from the current grid's row //Set all elements from the current grid's row
for(int col = 0;col < getNumCols();++col){ newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length);
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's row //Set all elements from the right side grid's row
for(int col = 0;col < rightSide.getNumCols();++col){ for(int col = 0;col < rightSide.getNumCols();++col){
newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; newGrid[row][getNumCols() + col] = rightSide.grid[row][col];
@@ -529,7 +511,7 @@ public class DoubleMatrix{
} }
//Create a new matrix for the product //Create a new matrix for the product
DoubleMatrix newMatrix = clone(); DoubleMatrix newMatrix = new DoubleMatrix(this);
//Multiply the current grid power times //Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){ for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this); newMatrix = newMatrix.multiply(this);
@@ -596,31 +578,16 @@ public class DoubleMatrix{
public double det(){ public double det(){
return determinant(); return determinant();
} }
public double determinant(){ private double det2(){
//Make sure the matrix is square return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]);
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
} }
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix 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]) -
//Determine the formula do use for the determinant
double det = 0;
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] * grid[1][1]) - (grid[0][1] * grid[1][0]);
}
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]); (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; private double det4(){
//If the matrix is larger break it down and try again double det = 0;
default : {
//Find the row/column with the largest number of 0's //Find the row/column with the largest number of 0's
int zerosLocation = 0; int zerosLocation = 0;
int maxNumZeros = 0; int maxNumZeros = 0;
@@ -684,7 +651,27 @@ public class DoubleMatrix{
multiplier = -multiplier; multiplier = -multiplier;
} }
} }
return det;
} }
public double determinant(){
//Make sure the matrix is square
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
}
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix
//Determine the formula do use for the determinant
double det = 0;
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 = det2(); break;
//If the matrix is 3x3 use the formula
case 3 : det = det3(); break;
//If the matrix is larger break it down and try again
default : det = det4();
} }
//Return the determinant //Return the determinant
@@ -744,14 +731,23 @@ public class DoubleMatrix{
//Object funtions //Object funtions
@Override @Override
public boolean equals(Object rightSide){ public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){ if(rightSide == null){
DoubleMatrix rightMatrix = (DoubleMatrix)rightSide;
//Make sure they have the same number of elements
if(getNumRows() != rightMatrix.getNumRows()){
return false; return false;
} }
else if(getNumCols() != rightMatrix.getNumCols()){ 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; return false;
} }
@@ -767,15 +763,6 @@ public class DoubleMatrix{
//If false hasn't been returned yet then they are equal //If false hasn't been returned yet then they are equal
return true; return true;
} }
else if(rightSide.getClass().equals(double[][].class)){
double[][] rightMatrix = (double[][])rightSide;
return equals(new DoubleMatrix(rightMatrix));
}
else{
return false;
}
}
public boolean equals(Object rightSide, double delta){ public boolean equals(Object rightSide, double delta){
setEqualsDelta(delta); setEqualsDelta(delta);
return equals(rightSide); return equals(rightSide);
@@ -785,7 +772,7 @@ public class DoubleMatrix{
} }
@Override @Override
public int hashCode(){ public int hashCode(){
return grid.hashCode(); return Arrays.hashCode(grid);
} }
@Override @Override
public String toString(){ public String toString(){
@@ -801,8 +788,4 @@ public class DoubleMatrix{
} }
return matrix.toString(); return matrix.toString();
} }
@Override
public DoubleMatrix clone(){
return new DoubleMatrix(grid);
}
} }

View File

@@ -1,10 +1,11 @@
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java //Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
//Mattrixwv //Mattrixwv
// Created: 02-01-22 // Created: 02-01-22
//Modified: 02-17-22 //Modified: 06-29-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
import java.util.Arrays;
import java.util.StringJoiner; import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
@@ -36,9 +37,7 @@ public class IntegerMatrix{
//Copy every element over to a new grid //Copy every element over to a new grid
int[][] newGrid = new int[grid.length][grid[0].length]; int[][] newGrid = new int[grid.length][grid[0].length];
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length);
newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt];
}
} }
//Save the new grid //Save the new grid
@@ -51,9 +50,7 @@ public class IntegerMatrix{
//Copy every element from the current grid to the new one //Copy every element from the current grid to the new one
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){ newGrid[row] = Arrays.copyOf(grid[row], grid.length);
newGrid[row][col] = grid[row][col];
}
} }
//Return the new grid //Return the new grid
@@ -159,9 +156,7 @@ public class IntegerMatrix{
//Generate a copy of the row //Generate a copy of the row
int[][] newRow = new int[1][grid[row].length]; int[][] newRow = new int[1][grid[row].length];
for(int col = 0;col < grid[row].length;++col){ newRow[0] = Arrays.copyOf(grid[row], grid[row].length);
newRow[0][col] = grid[row][col];
}
//Return the new matrix //Return the new matrix
return new IntegerMatrix(newRow); return new IntegerMatrix(newRow);
@@ -218,13 +213,11 @@ public class IntegerMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != getNumCols()){ 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 //Save the elements
for(int col = 0;col < elements.length;++col){ grid[row] = Arrays.copyOf(elements, elements.length);
grid[row][col] = elements[col];
}
} }
public void setRow(int row, IntegerMatrix matrix){ public void setRow(int row, IntegerMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -242,7 +235,7 @@ public class IntegerMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != grid.length){ 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 //Save the elements
@@ -266,18 +259,16 @@ public class IntegerMatrix{
int[][] newGrid = new int[grid.length + 1][elements.length]; int[][] newGrid = new int[grid.length + 1][elements.length];
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
newGrid[row] = grid[row]; newGrid[row] = Arrays.copyOf(grid[row], grid[row].length);
} }
grid = newGrid; grid = newGrid;
} }
else{ 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 //Add all elements to the grid
for(int col = 0;col < elements.length;++col){ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
grid[grid.length - 1][col] = elements[col];
}
} }
public void addRow(IntegerMatrix matrix){ public void addRow(IntegerMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -296,20 +287,12 @@ public class IntegerMatrix{
else if(grid.length == elements.length){ else if(grid.length == elements.length){
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
int[] workingRow = new int[grid[row].length + 1]; grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1);
for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ grid[row][grid[row].length - 1] = elements[row];
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
} }
} }
else{ 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 row = 0;row < elements.length;++row){
grid[row][grid[row].length - 1] = elements[row];
} }
} }
public void addCol(IntegerMatrix matrix){ public void addCol(IntegerMatrix matrix){
@@ -331,9 +314,8 @@ public class IntegerMatrix{
int[][] newGrid = new int[getNumRows()][getNumCols() + rightSide.getNumCols()]; int[][] newGrid = new int[getNumRows()][getNumCols() + rightSide.getNumCols()];
for(int row = 0;row < getNumRows();++row){ for(int row = 0;row < getNumRows();++row){
//Set all elements from the current grid's row //Set all elements from the current grid's row
for(int col = 0;col < getNumCols();++col){ newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length);
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's row //Set all elements from the right side grid's row
for(int col = 0;col < rightSide.getNumCols();++col){ for(int col = 0;col < rightSide.getNumCols();++col){
newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; newGrid[row][getNumCols() + col] = rightSide.grid[row][col];
@@ -510,7 +492,7 @@ public class IntegerMatrix{
} }
//Create a new matrix for the product //Create a new matrix for the product
IntegerMatrix newMatrix = clone(); IntegerMatrix newMatrix = new IntegerMatrix(this);
//Multiply the current grid power times //Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){ for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this); newMatrix = newMatrix.multiply(this);
@@ -577,31 +559,15 @@ public class IntegerMatrix{
public int det(){ public int det(){
return determinant(); return determinant();
} }
public int determinant(){ private int det2(){
//Make sure the matrix is square return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]);
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
} }
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix 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]) -
//Determine the formula do use for the determinant
int det = 0;
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] * grid[1][1]) - (grid[0][1] * grid[1][0]);
}
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]); (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; private int det4(){
//If the matrix is larger break it down and try again int det = 0;
default : {
//Find the row/column with the largest number of 0's //Find the row/column with the largest number of 0's
int zerosLocation = 0; int zerosLocation = 0;
int maxNumZeros = 0; int maxNumZeros = 0;
@@ -665,7 +631,27 @@ public class IntegerMatrix{
multiplier = -multiplier; multiplier = -multiplier;
} }
} }
return det;
} }
public int determinant(){
//Make sure the matrix is square
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
}
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix
//Determine the formula do use for the determinant
int det = 0;
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 = det2(); break;
//If the matrix is 3x3 use the formula
case 3 : det = det3(); break;
//If the matrix is larger break it down and try again
default : det = det4();
} }
//Return the determinant //Return the determinant
@@ -725,14 +711,23 @@ public class IntegerMatrix{
//Object funtions //Object funtions
@Override @Override
public boolean equals(Object rightSide){ public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){ if(rightSide == null){
IntegerMatrix rightMatrix = (IntegerMatrix)rightSide;
//Make sure they have the same number of elements
if(getNumRows() != rightMatrix.getNumRows()){
return false; return false;
} }
else if(getNumCols() != rightMatrix.getNumCols()){ if(rightSide.getClass().equals(this.getClass())){
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; return false;
} }
@@ -748,18 +743,9 @@ public class IntegerMatrix{
//If false hasn't been returned yet then they are equal //If false hasn't been returned yet then they are equal
return true; return true;
} }
else if(rightSide.getClass().equals(int[][].class)){
int[][] rightMatrix = (int[][])rightSide;
return equals(new IntegerMatrix(rightMatrix));
}
else{
return false;
}
}
@Override @Override
public int hashCode(){ public int hashCode(){
return grid.hashCode(); return Arrays.hashCode(grid);
} }
@Override @Override
public String toString(){ public String toString(){
@@ -775,8 +761,4 @@ public class IntegerMatrix{
} }
return matrix.toString(); return matrix.toString();
} }
@Override
public IntegerMatrix clone(){
return new IntegerMatrix(grid);
}
} }

View File

@@ -1,10 +1,11 @@
//Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java //Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java
//Mattrixwv //Mattrixwv
// Created: 02-10-22 // Created: 02-10-22
//Modified: 02-17-22 //Modified: 06-29-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
import java.util.Arrays;
import java.util.StringJoiner; import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
@@ -36,9 +37,7 @@ public class LongMatrix{
//Copy every element over to a new grid //Copy every element over to a new grid
long[][] newGrid = new long[grid.length][grid[0].length]; long[][] newGrid = new long[grid.length][grid[0].length];
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){ for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){ newGrid[rowCnt] = Arrays.copyOf(grid[rowCnt], grid[rowCnt].length);
newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt];
}
} }
//Save the new grid //Save the new grid
@@ -51,9 +50,7 @@ public class LongMatrix{
//Copy every element from the current grid to the new one //Copy every element from the current grid to the new one
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){ newGrid[row] = Arrays.copyOf(grid[row], grid.length);
newGrid[row][col] = grid[row][col];
}
} }
//Return the new grid //Return the new grid
@@ -159,9 +156,7 @@ public class LongMatrix{
//Generate a copy of the row //Generate a copy of the row
long[][] newRow = new long[1][grid[row].length]; long[][] newRow = new long[1][grid[row].length];
for(int col = 0;col < grid[row].length;++col){ newRow[0] = Arrays.copyOf(grid[row], grid[row].length);
newRow[0][col] = grid[row][col];
}
//Return the new matrix //Return the new matrix
return new LongMatrix(newRow); return new LongMatrix(newRow);
@@ -218,13 +213,11 @@ public class LongMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != getNumCols()){ 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 //Save the elements
for(int col = 0;col < elements.length;++col){ grid[row] = Arrays.copyOf(elements, elements.length);
grid[row][col] = elements[col];
}
} }
public void setRow(int row, LongMatrix matrix){ public void setRow(int row, LongMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -242,7 +235,7 @@ public class LongMatrix{
} }
//Make sure the number of elements is valid //Make sure the number of elements is valid
if(elements.length != grid.length){ 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 //Save the elements
@@ -266,18 +259,16 @@ public class LongMatrix{
long[][] newGrid = new long[grid.length + 1][elements.length]; long[][] newGrid = new long[grid.length + 1][elements.length];
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
newGrid[row] = grid[row]; newGrid[row] = Arrays.copyOf(grid[row], grid[row].length);
} }
grid = newGrid; grid = newGrid;
} }
else{ 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 //Add all elements to the grid
for(int col = 0;col < elements.length;++col){ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
grid[grid.length - 1][col] = elements[col];
}
} }
public void addRow(LongMatrix matrix){ public void addRow(LongMatrix matrix){
//Make sure the matrix has a single row //Make sure the matrix has a single row
@@ -296,20 +287,12 @@ public class LongMatrix{
else if(grid.length == elements.length){ else if(grid.length == elements.length){
//Copy all existing data into the new grid //Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){ for(int row = 0;row < grid.length;++row){
long[] workingRow = new long[grid[row].length + 1]; grid[row] = Arrays.copyOf(grid[row], grid[row].length + 1);
for(int workingCol = 0;workingCol < grid[row].length;++workingCol){ grid[row][grid[row].length - 1] = elements[row];
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
} }
} }
else{ 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 row = 0;row < elements.length;++row){
grid[row][grid[row].length - 1] = elements[row];
} }
} }
public void addCol(LongMatrix matrix){ public void addCol(LongMatrix matrix){
@@ -331,9 +314,8 @@ public class LongMatrix{
long[][] newGrid = new long[getNumRows()][getNumCols() + rightSide.getNumCols()]; long[][] newGrid = new long[getNumRows()][getNumCols() + rightSide.getNumCols()];
for(int row = 0;row < getNumRows();++row){ for(int row = 0;row < getNumRows();++row){
//Set all elements from the current grid's row //Set all elements from the current grid's row
for(int col = 0;col < getNumCols();++col){ newGrid[row] = Arrays.copyOf(grid[row], grid[row].length + rightSide.grid[row].length);
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's row //Set all elements from the right side grid's row
for(int col = 0;col < rightSide.getNumCols();++col){ for(int col = 0;col < rightSide.getNumCols();++col){
newGrid[row][getNumCols() + col] = rightSide.grid[row][col]; newGrid[row][getNumCols() + col] = rightSide.grid[row][col];
@@ -510,7 +492,7 @@ public class LongMatrix{
} }
//Create a new matrix for the product //Create a new matrix for the product
LongMatrix newMatrix = clone(); LongMatrix newMatrix = new LongMatrix(this);
//Multiply the current grid power times //Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){ for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this); newMatrix = newMatrix.multiply(this);
@@ -577,31 +559,15 @@ public class LongMatrix{
public long det(){ public long det(){
return determinant(); return determinant();
} }
public long determinant(){ public long det2(){
//Make sure the matrix is square return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]);
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
} }
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix 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]) -
//Determine the formula do use for the determinant
long det = 0;
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] * grid[1][1]) - (grid[0][1] * grid[1][0]);
}
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]); (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; public long det4(){
//If the matrix is larger break it down and try again long det = 0;
default : {
//Find the row/column with the largest number of 0's //Find the row/column with the largest number of 0's
int zerosLocation = 0; int zerosLocation = 0;
int maxNumZeros = 0; int maxNumZeros = 0;
@@ -665,7 +631,27 @@ public class LongMatrix{
multiplier = -multiplier; multiplier = -multiplier;
} }
} }
return det;
} }
public long determinant(){
//Make sure the matrix is square
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
}
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix
//Determine the formula do use for the determinant
long det = 0;
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 = det2(); break;
//If the matrix is 3x3 use the formula
case 3 : det = det3(); break;
//If the matrix is larger break it down and try again
default : det = det4();
} }
//Return the determinant //Return the determinant
@@ -725,14 +711,23 @@ public class LongMatrix{
//Object funtions //Object funtions
@Override @Override
public boolean equals(Object rightSide){ public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){ if(rightSide == null){
LongMatrix rightMatrix = (LongMatrix)rightSide;
//Make sure they have the same number of elements
if(getNumRows() != rightMatrix.getNumRows()){
return false; return false;
} }
else if(getNumCols() != rightMatrix.getNumCols()){ if(rightSide.getClass().equals(this.getClass())){
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; return false;
} }
@@ -748,18 +743,9 @@ public class LongMatrix{
//If false hasn't been returned yet then they are equal //If false hasn't been returned yet then they are equal
return true; return true;
} }
else if(rightSide.getClass().equals(long[][].class)){
long[][] rightMatrix = (long[][])rightSide;
return equals(new LongMatrix(rightMatrix));
}
else{
return false;
}
}
@Override @Override
public int hashCode(){ public int hashCode(){
return grid.hashCode(); return Arrays.hashCode(grid);
} }
@Override @Override
public String toString(){ public String toString(){
@@ -775,8 +761,4 @@ public class LongMatrix{
} }
return matrix.toString(); return matrix.toString();
} }
@Override
public LongMatrix clone(){
return new LongMatrix(grid);
}
} }

View File

@@ -1,10 +1,12 @@
//Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java //Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java
//Mattrixwv //Mattrixwv
// Created: 02-09-22 // Created: 02-09-22
//Modified: 02-09-22 //Modified: 06-29-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
import java.util.Arrays;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.InvalidScalarException;
@@ -215,7 +217,7 @@ public class ModMatrix extends IntegerMatrix{
} }
//Create a new matrix for the product //Create a new matrix for the product
ModMatrix newMatrix = clone(); ModMatrix newMatrix = new ModMatrix(this);
//Multiply the current grid power times //Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){ for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this); newMatrix = newMatrix.multiply(this);
@@ -245,14 +247,6 @@ public class ModMatrix extends IntegerMatrix{
return new ModMatrix(super.transpose(), mod); return new ModMatrix(super.transpose(), mod);
} }
@Override @Override
public int det(){
return determinant();
}
@Override
public int determinant(){
return super.determinant();
}
@Override
public ModMatrix cof(){ public ModMatrix cof(){
return cofactor(); return cofactor();
} }
@@ -322,14 +316,24 @@ public class ModMatrix extends IntegerMatrix{
//Object functions //Object functions
@Override @Override
public boolean equals(Object rightSide){ public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){ if(rightSide == null){
ModMatrix rightMatrix = (ModMatrix)rightSide;
//Make sure they have the same number of elements
if(getNumRows() != rightMatrix.getNumRows()){
return false; return false;
} }
else if(getNumCols() != rightMatrix.getNumCols()){ if(rightSide.getClass().equals(this.getClass())){
return equals((ModMatrix)rightSide);
}
else if(rightSide.getClass().equals(int[][].class)){
int[][] rightMatrix = (int[][])rightSide;
return equals(new ModMatrix(rightMatrix, mod));
}
else{
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; return false;
} }
@@ -345,25 +349,12 @@ public class ModMatrix extends IntegerMatrix{
//If false hasn't been return yet then they are equal //If false hasn't been return yet then they are equal
return true; return true;
} }
else if(rightSide.getClass().equals(int[][].class)){
int[][] rightMatrix = (int[][])rightSide;
return equals(new ModMatrix(rightMatrix, mod));
}
else{
return false;
}
}
@Override @Override
public int hashCode(){ public int hashCode(){
return grid.hashCode(); return Arrays.hashCode(grid);
} }
@Override @Override
public String toString(){ public String toString(){
return super.toString() + "\nmod(" + mod + ")"; return super.toString() + "\nmod(" + mod + ")";
} }
@Override
public ModMatrix clone(){
return new ModMatrix(grid, mod);
}
} }

View File

@@ -20,4 +20,7 @@ public class InvalidCoordinatesException extends RuntimeException{
public InvalidCoordinatesException(String message, Throwable throwable){ public InvalidCoordinatesException(String message, Throwable throwable){
super(message, throwable); super(message, throwable);
} }
public InvalidCoordinatesException(int givenElements, int neededElements){
super("Invalid number of elements " + givenElements + " must be at most " + neededElements);
}
} }

View File

@@ -20,4 +20,7 @@ public class InvalidGeometryException extends RuntimeException{
public InvalidGeometryException(String message, Throwable throwable){ public InvalidGeometryException(String message, Throwable throwable){
super(message, throwable); super(message, throwable);
} }
public InvalidGeometryException(int givenElements, int neededElements){
super("Invalid number of elements " + givenElements + " must be " + neededElements);
}
} }

View File

@@ -119,38 +119,43 @@ public class TestBigIntegerMatrix{
public void testEquals(){ public void testEquals(){
//1x1 //1x1
BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); 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") @SuppressWarnings("unlikely-arg-type")
boolean gridEquals = matrix.equals(grid1); boolean gridEquals1 = matrix.equals(grid1);
assertTrue("BigIntegerMatrix 1x1 failed equals BigInteger[][].", gridEquals); assertTrue("BigIntegerMatrix 1x1 failed equals BigInteger[][].", gridEquals1);
//2x2 //2x2
matrix = new BigIntegerMatrix(grid2); 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") @SuppressWarnings("unlikely-arg-type")
boolean gridEquals2 = matrix.equals(grid2); boolean gridEquals21 = matrix.equals(grid2);
assertTrue("BigIntegerMatrix 2x2 failed equals BigInteger[][].", gridEquals2); assertTrue("BigIntegerMatrix 2x2 failed equals BigInteger[][].", gridEquals21);
//3x3 //3x3
matrix = new BigIntegerMatrix(grid3); 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") @SuppressWarnings("unlikely-arg-type")
boolean gridEquals3 = matrix.equals(grid3); boolean gridEquals31 = matrix.equals(grid3);
assertTrue("BigIntegerMatrix 3x3 failed equals BigInteger[][].", gridEquals3); assertTrue("BigIntegerMatrix 3x3 failed equals BigInteger[][].", gridEquals31);
//4x4 //4x4
matrix = new BigIntegerMatrix(grid4); 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") @SuppressWarnings("unlikely-arg-type")
boolean gridEquals4 = matrix.equals(grid4); boolean gridEquals41 = matrix.equals(grid4);
assertTrue("BigIntegerMatrix 4x4 failed equals BigInteger[][].", gridEquals4); assertTrue("BigIntegerMatrix 4x4 failed equals BigInteger[][].", gridEquals41);
//10x10 //10x10
matrix = new BigIntegerMatrix(grid10); 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") @SuppressWarnings("unlikely-arg-type")
boolean gridEquals10 = matrix.equals(grid10); boolean gridEquals101 = matrix.equals(grid10);
assertTrue("BigIntegerMatrix 10x10 failed equals BigInteger[][].", gridEquals10); assertTrue("BigIntegerMatrix 10x10 failed equals BigInteger[][].", gridEquals101);
} }
@Test @Test