mirror of
https://bitbucket.org/Mattrixwv/matrix.git
synced 2025-12-06 23:13:57 -05:00
Updated Sonarqube findings
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user