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
//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);
}
}