mirror of
https://bitbucket.org/Mattrixwv/matrix.git
synced 2025-12-06 23:13:57 -05:00
Updated for SonarQube findings
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
|
||||
//Mattrixwv
|
||||
// Created: 02-01-22
|
||||
//Modified: 06-29-22
|
||||
//Modified: 06-30-22
|
||||
package com.mattrixwv.matrix;
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
import com.mattrixwv.matrix.exceptions.NullMatrixException;
|
||||
|
||||
|
||||
public class IntegerMatrix{
|
||||
@@ -141,9 +142,15 @@ public class IntegerMatrix{
|
||||
if(row >= grid.length){
|
||||
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows");
|
||||
}
|
||||
else if(row < 0){
|
||||
throw new InvalidCoordinatesException("Row cannot be less than 0");
|
||||
}
|
||||
else if(col >= grid[0].length){
|
||||
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns");
|
||||
}
|
||||
else if(col < 0){
|
||||
throw new InvalidCoordinatesException("Column cannot be less than 0");
|
||||
}
|
||||
|
||||
//Return the location in the grid
|
||||
return grid[row][col];
|
||||
@@ -162,12 +169,7 @@ public class IntegerMatrix{
|
||||
return new IntegerMatrix(newRow);
|
||||
}
|
||||
public int getNumRows(){
|
||||
if(grid == null){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return grid.length;
|
||||
}
|
||||
return grid.length;
|
||||
}
|
||||
public IntegerMatrix getCol(int col){
|
||||
//Make sure the column number is valid
|
||||
@@ -192,6 +194,7 @@ public class IntegerMatrix{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Sets
|
||||
public void set(int row, int col, int value){
|
||||
//Make sure the row number is valid
|
||||
@@ -212,7 +215,10 @@ public class IntegerMatrix{
|
||||
throw new InvalidCoordinatesException("Invalid row number " + row);
|
||||
}
|
||||
//Make sure the number of elements is valid
|
||||
if(elements.length != getNumCols()){
|
||||
if(elements == null){
|
||||
throw new InvalidGeometryException("Row cannot be null");
|
||||
}
|
||||
else if(elements.length != getNumCols()){
|
||||
throw new InvalidGeometryException(elements.length, getNumCols());
|
||||
}
|
||||
|
||||
@@ -220,6 +226,10 @@ public class IntegerMatrix{
|
||||
grid[row] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
public void setRow(int row, IntegerMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrix has a single row
|
||||
if(matrix.getNumRows() != 1){
|
||||
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row");
|
||||
@@ -234,7 +244,10 @@ public class IntegerMatrix{
|
||||
throw new InvalidCoordinatesException("Invalid column number " + col);
|
||||
}
|
||||
//Make sure the number of elements is valid
|
||||
if(elements.length != grid.length){
|
||||
if(elements == null){
|
||||
throw new InvalidGeometryException("Column cannot be null");
|
||||
}
|
||||
else if(elements.length != grid.length){
|
||||
throw new InvalidCoordinatesException(elements.length, grid.length);
|
||||
}
|
||||
|
||||
@@ -244,18 +257,32 @@ public class IntegerMatrix{
|
||||
}
|
||||
}
|
||||
public void setCol(int col, IntegerMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrix has a single column
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
|
||||
}
|
||||
|
||||
int[] vector = new int[matrix.getNumRows()];
|
||||
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
|
||||
vector[cnt] = matrix.grid[cnt][0];
|
||||
}
|
||||
|
||||
//Set the column
|
||||
setCol(col, matrix.getCol(0));
|
||||
setCol(col, vector);
|
||||
}
|
||||
|
||||
//Adds
|
||||
public void addRow(int[] elements){
|
||||
//Make sure the matrix isn't null
|
||||
if(elements == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the number of columns is valid
|
||||
if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
else if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
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){
|
||||
@@ -271,8 +298,12 @@ public class IntegerMatrix{
|
||||
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
public void addRow(IntegerMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrix has a single row
|
||||
if(matrix.getNumRows() != 1){
|
||||
else if(matrix.getNumRows() != 1){
|
||||
throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
|
||||
}
|
||||
|
||||
@@ -280,9 +311,16 @@ public class IntegerMatrix{
|
||||
addRow(matrix.grid[0]);
|
||||
}
|
||||
public void addCol(int[] elements){
|
||||
//Make sure the matrix isn't null
|
||||
if(elements == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the number of rows is valid
|
||||
if(grid.length == 0){
|
||||
grid = new int[elements.length][1];
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
grid[row][0] = elements[row];
|
||||
}
|
||||
}
|
||||
else if(grid.length == elements.length){
|
||||
//Copy all existing data into the new grid
|
||||
@@ -296,17 +334,30 @@ public class IntegerMatrix{
|
||||
}
|
||||
}
|
||||
public void addCol(IntegerMatrix matrix){
|
||||
//Make sure teh matrix isn't null
|
||||
if(matrix == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrix has a single column
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
|
||||
}
|
||||
|
||||
int[] vector = new int[matrix.getNumRows()];
|
||||
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
|
||||
vector[cnt] = matrix.get(cnt, 0);
|
||||
}
|
||||
|
||||
//Add the column
|
||||
addCol(matrix.getCol(0));
|
||||
addCol(vector);
|
||||
}
|
||||
public IntegerMatrix appendRight(IntegerMatrix rightSide){
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrices have the same number of rows
|
||||
if(getNumRows() != rightSide.getNumRows()){
|
||||
else if(getNumRows() != rightSide.getNumRows()){
|
||||
throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
|
||||
}
|
||||
|
||||
@@ -326,8 +377,12 @@ public class IntegerMatrix{
|
||||
return new IntegerMatrix(newGrid);
|
||||
}
|
||||
public IntegerMatrix appendBottom(IntegerMatrix rightSide){
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrices have the same number of columns
|
||||
if(getNumCols() != rightSide.getNumCols()){
|
||||
else if(getNumCols() != rightSide.getNumCols()){
|
||||
throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
|
||||
}
|
||||
|
||||
@@ -568,6 +623,7 @@ public class IntegerMatrix{
|
||||
}
|
||||
private int det4(){
|
||||
int det = 0;
|
||||
|
||||
//Find the row/column with the largest number of 0's
|
||||
int zerosLocation = 0;
|
||||
int maxNumZeros = 0;
|
||||
|
||||
Reference in New Issue
Block a user