Updated for SonarQube findings

This commit is contained in:
2022-07-03 17:45:08 -04:00
parent a7333c5f3e
commit 4b738d2817
11 changed files with 3896 additions and 962 deletions

View File

@@ -1,7 +1,7 @@
//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: 06-29-22 //Modified: 06-30-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
@@ -13,6 +13,7 @@ import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class BigIntegerMatrix{ public class BigIntegerMatrix{
@@ -142,9 +143,15 @@ public class BigIntegerMatrix{
if(row >= grid.length){ if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); 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){ else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); 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 the location in the grid
return grid[row][col]; return grid[row][col];
@@ -163,13 +170,8 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newRow); return new BigIntegerMatrix(newRow);
} }
public int getNumRows(){ public int getNumRows(){
if(grid == null){
return 0;
}
else{
return grid.length; return grid.length;
} }
}
public BigIntegerMatrix getCol(int col){ public BigIntegerMatrix getCol(int col){
//Make sure the column number is valid //Make sure the column number is valid
if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ if((col < 0) || (grid.length == 0) || (col > grid[0].length)){
@@ -193,6 +195,7 @@ public class BigIntegerMatrix{
return 0; return 0;
} }
} }
//Sets //Sets
public void set(int row, int col, BigInteger value){ public void set(int row, int col, BigInteger value){
//Make sure the row number is valid //Make sure the row number is valid
@@ -213,7 +216,10 @@ public class BigIntegerMatrix{
throw new InvalidCoordinatesException("Invalid row number " + row); throw new InvalidCoordinatesException("Invalid row number " + row);
} }
//Make sure the number of elements is valid //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()); throw new InvalidGeometryException(elements.length, getNumCols());
} }
@@ -221,6 +227,10 @@ public class BigIntegerMatrix{
grid[row] = Arrays.copyOf(elements, elements.length); grid[row] = Arrays.copyOf(elements, elements.length);
} }
public void setRow(int row, BigIntegerMatrix matrix){ public void setRow(int row, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //Make sure the matrix has a single row
if(matrix.getNumRows() != 1){ if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row");
@@ -235,7 +245,10 @@ public class BigIntegerMatrix{
throw new InvalidCoordinatesException("Invalid column number " + col); throw new InvalidCoordinatesException("Invalid column number " + col);
} }
//Make sure the number of elements is valid //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); throw new InvalidCoordinatesException(elements.length, grid.length);
} }
@@ -245,18 +258,32 @@ public class BigIntegerMatrix{
} }
} }
public void setCol(int col, BigIntegerMatrix matrix){ public void setCol(int col, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
} }
//Set the column BigInteger[] vector = new BigInteger[matrix.getNumRows()];
setCol(col, matrix.getCol(0)); for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.grid[cnt][0];
} }
//Set the column
setCol(col, vector);
}
//Adds //Adds
public void addRow(BigInteger[] elements){ public void addRow(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of columns is valid //Make sure the number of columns is valid
if((grid.length == 0) || (getNumCols() == elements.length)){ else if((grid.length == 0) || (getNumCols() == elements.length)){
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){
@@ -272,8 +299,12 @@ public class BigIntegerMatrix{
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
} }
public void addRow(BigIntegerMatrix matrix){ public void addRow(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //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"); throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
} }
@@ -281,9 +312,16 @@ public class BigIntegerMatrix{
addRow(matrix.grid[0]); addRow(matrix.grid[0]);
} }
public void addCol(BigInteger[] elements){ public void addCol(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of rows is valid //Make sure the number of rows is valid
if(grid.length == 0){ if(grid.length == 0){
grid = new BigInteger[elements.length][1]; grid = new BigInteger[elements.length][1];
for(int row = 0;row < grid.length;++row){
grid[row][0] = elements[row];
}
} }
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
@@ -297,17 +335,30 @@ public class BigIntegerMatrix{
} }
} }
public void addCol(BigIntegerMatrix matrix){ public void addCol(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
} }
BigInteger[] vector = new BigInteger[matrix.getNumRows()];
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.get(cnt, 0);
}
//Add the column //Add the column
addCol(matrix.getCol(0)); addCol(vector);
} }
public BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){ public BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of rows //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()); throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
} }
@@ -327,8 +378,12 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newGrid); return new BigIntegerMatrix(newGrid);
} }
public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){ public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of columns //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()); throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
} }

View File

@@ -1,7 +1,7 @@
//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: 06-29-22 //Modified: 06-30-22
package com.mattrixwv.matrix; 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.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class DoubleMatrix{ public class DoubleMatrix{
@@ -146,9 +147,15 @@ public class DoubleMatrix{
if(row >= grid.length){ if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); 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){ else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); 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 the location in the grid
return grid[row][col]; return grid[row][col];
@@ -167,13 +174,8 @@ public class DoubleMatrix{
return new DoubleMatrix(newRow); return new DoubleMatrix(newRow);
} }
public int getNumRows(){ public int getNumRows(){
if(grid == null){
return 0;
}
else{
return grid.length; return grid.length;
} }
}
public DoubleMatrix getCol(int col){ public DoubleMatrix getCol(int col){
//Make sure the column number is valid //Make sure the column number is valid
if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ if((col < 0) || (grid.length == 0) || (col > grid[0].length)){
@@ -197,6 +199,7 @@ public class DoubleMatrix{
return 0; return 0;
} }
} }
//Sets //Sets
public void set(int row, int col, double value){ public void set(int row, int col, double value){
//Make sure the row number is valid //Make sure the row number is valid
@@ -217,7 +220,10 @@ public class DoubleMatrix{
throw new InvalidCoordinatesException("Invalid row number " + row); throw new InvalidCoordinatesException("Invalid row number " + row);
} }
//Make sure the number of elements is valid //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()); throw new InvalidGeometryException(elements.length, getNumCols());
} }
@@ -225,6 +231,10 @@ public class DoubleMatrix{
grid[row] = Arrays.copyOf(elements, elements.length); grid[row] = Arrays.copyOf(elements, elements.length);
} }
public void setRow(int row, DoubleMatrix matrix){ public void setRow(int row, DoubleMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //Make sure the matrix has a single row
if(matrix.getNumRows() != 1){ if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row");
@@ -239,7 +249,10 @@ public class DoubleMatrix{
throw new InvalidCoordinatesException("Invalid column number " + col); throw new InvalidCoordinatesException("Invalid column number " + col);
} }
//Make sure the number of elements is valid //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); throw new InvalidCoordinatesException(elements.length, grid.length);
} }
@@ -249,18 +262,32 @@ public class DoubleMatrix{
} }
} }
public void setCol(int col, DoubleMatrix matrix){ public void setCol(int col, DoubleMatrix matrix){
//Make sure teh matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
} }
//Set the column double[] vector = new double[matrix.getNumRows()];
setCol(col, matrix.getCol(0)); for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.grid[cnt][0];
} }
//Set the column
setCol(col, vector);
}
//Adds //Adds
public void addRow(double[] elements){ public void addRow(double[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of columns is valid //Make sure the number of columns is valid
if((grid.length == 0) || (getNumCols() == elements.length)){ else if((grid.length == 0) || (getNumCols() == elements.length)){
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){
@@ -276,8 +303,12 @@ public class DoubleMatrix{
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
} }
public void addRow(DoubleMatrix matrix){ public void addRow(DoubleMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //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"); throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
} }
@@ -285,9 +316,16 @@ public class DoubleMatrix{
addRow(matrix.grid[0]); addRow(matrix.grid[0]);
} }
public void addCol(double[] elements){ public void addCol(double[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of rows is valid //Make sure the number of rows is valid
if(grid.length == 0){ if(grid.length == 0){
grid = new double[elements.length][1]; grid = new double[elements.length][1];
for(int row = 0;row < grid.length;++row){
grid[row][0] = elements[row];
}
} }
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
@@ -301,17 +339,30 @@ public class DoubleMatrix{
} }
} }
public void addCol(DoubleMatrix matrix){ public void addCol(DoubleMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
} }
double[] vector = new double[matrix.getNumRows()];
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.get(cnt, 0);
}
//Add the column //Add the column
addCol(matrix.getCol(0)); addCol(vector);
} }
public DoubleMatrix appendRight(DoubleMatrix rightSide){ public DoubleMatrix appendRight(DoubleMatrix rightSide){
//Make sure teh matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of rows //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()); throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
} }
@@ -331,8 +382,12 @@ public class DoubleMatrix{
return new DoubleMatrix(newGrid); return new DoubleMatrix(newGrid);
} }
public DoubleMatrix appendBottom(DoubleMatrix rightSide){ public DoubleMatrix appendBottom(DoubleMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of columns //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()); throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
} }
@@ -379,20 +434,6 @@ public class DoubleMatrix{
throw new InvalidGeometryException("An identity matrix must have a size > 0"); throw new InvalidGeometryException("An identity matrix must have a size > 0");
} }
} }
public static DoubleMatrix generateFilled(int size, double fill){
//Create a grid with the correct size
double[][] newGrid = new double[size][size];
//Set each element in the grid
for(int row = 0;row < size;++row){
for(int col = 0;col < size;++col){
newGrid[row][col] = fill;
}
}
//Return the new matrix
return new DoubleMatrix(newGrid);
}
public DoubleMatrix add(DoubleMatrix rightSide){ public DoubleMatrix add(DoubleMatrix rightSide){
//Make sure the matrices have compatable geometry //Make sure the matrices have compatable geometry
if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){ if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){

View File

@@ -1,7 +1,7 @@
//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: 06-29-22 //Modified: 06-30-22
package com.mattrixwv.matrix; 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.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class IntegerMatrix{ public class IntegerMatrix{
@@ -141,9 +142,15 @@ public class IntegerMatrix{
if(row >= grid.length){ if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); 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){ else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); 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 the location in the grid
return grid[row][col]; return grid[row][col];
@@ -162,13 +169,8 @@ public class IntegerMatrix{
return new IntegerMatrix(newRow); return new IntegerMatrix(newRow);
} }
public int getNumRows(){ public int getNumRows(){
if(grid == null){
return 0;
}
else{
return grid.length; return grid.length;
} }
}
public IntegerMatrix getCol(int col){ public IntegerMatrix getCol(int col){
//Make sure the column number is valid //Make sure the column number is valid
if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ if((col < 0) || (grid.length == 0) || (col > grid[0].length)){
@@ -192,6 +194,7 @@ public class IntegerMatrix{
return 0; return 0;
} }
} }
//Sets //Sets
public void set(int row, int col, int value){ public void set(int row, int col, int value){
//Make sure the row number is valid //Make sure the row number is valid
@@ -212,7 +215,10 @@ public class IntegerMatrix{
throw new InvalidCoordinatesException("Invalid row number " + row); throw new InvalidCoordinatesException("Invalid row number " + row);
} }
//Make sure the number of elements is valid //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()); throw new InvalidGeometryException(elements.length, getNumCols());
} }
@@ -220,6 +226,10 @@ public class IntegerMatrix{
grid[row] = Arrays.copyOf(elements, elements.length); grid[row] = Arrays.copyOf(elements, elements.length);
} }
public void setRow(int row, IntegerMatrix matrix){ 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 //Make sure the matrix has a single row
if(matrix.getNumRows() != 1){ if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); 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); throw new InvalidCoordinatesException("Invalid column number " + col);
} }
//Make sure the number of elements is valid //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); throw new InvalidCoordinatesException(elements.length, grid.length);
} }
@@ -244,18 +257,32 @@ public class IntegerMatrix{
} }
} }
public void setCol(int col, IntegerMatrix matrix){ 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 //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"); throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
} }
//Set the column int[] vector = new int[matrix.getNumRows()];
setCol(col, matrix.getCol(0)); for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.grid[cnt][0];
} }
//Set the column
setCol(col, vector);
}
//Adds //Adds
public void addRow(int[] elements){ 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 //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]; 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){
@@ -271,8 +298,12 @@ public class IntegerMatrix{
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
} }
public void addRow(IntegerMatrix matrix){ 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 //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"); 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]); addRow(matrix.grid[0]);
} }
public void addCol(int[] elements){ 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 //Make sure the number of rows is valid
if(grid.length == 0){ if(grid.length == 0){
grid = new int[elements.length][1]; 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){ else if(grid.length == elements.length){
//Copy all existing data into the new grid //Copy all existing data into the new grid
@@ -296,17 +334,30 @@ public class IntegerMatrix{
} }
} }
public void addCol(IntegerMatrix matrix){ 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 //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"); 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 //Add the column
addCol(matrix.getCol(0)); addCol(vector);
} }
public IntegerMatrix appendRight(IntegerMatrix rightSide){ 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 //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()); throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
} }
@@ -326,8 +377,12 @@ public class IntegerMatrix{
return new IntegerMatrix(newGrid); return new IntegerMatrix(newGrid);
} }
public IntegerMatrix appendBottom(IntegerMatrix rightSide){ 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 //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()); throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
} }
@@ -568,6 +623,7 @@ public class IntegerMatrix{
} }
private int det4(){ private int det4(){
int det = 0; int det = 0;
//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;

View File

@@ -1,7 +1,7 @@
//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: 06-29-22 //Modified: 07-01-22
package com.mattrixwv.matrix; 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.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class LongMatrix{ public class LongMatrix{
@@ -141,9 +142,15 @@ public class LongMatrix{
if(row >= grid.length){ if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); 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){ else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); 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 the location in the grid
return grid[row][col]; return grid[row][col];
@@ -162,13 +169,8 @@ public class LongMatrix{
return new LongMatrix(newRow); return new LongMatrix(newRow);
} }
public int getNumRows(){ public int getNumRows(){
if(grid == null){
return 0;
}
else{
return grid.length; return grid.length;
} }
}
public LongMatrix getCol(int col){ public LongMatrix getCol(int col){
//Make sure the column number is valid //Make sure the column number is valid
if((col < 0) || (grid.length == 0) || (col > grid[0].length)){ if((col < 0) || (grid.length == 0) || (col > grid[0].length)){
@@ -192,6 +194,7 @@ public class LongMatrix{
return 0; return 0;
} }
} }
//Sets //Sets
public void set(int row, int col, long value){ public void set(int row, int col, long value){
//Make sure the row number is valid //Make sure the row number is valid
@@ -212,7 +215,10 @@ public class LongMatrix{
throw new InvalidCoordinatesException("Invalid row number " + row); throw new InvalidCoordinatesException("Invalid row number " + row);
} }
//Make sure the number of elements is valid //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()); throw new InvalidGeometryException(elements.length, getNumCols());
} }
@@ -220,6 +226,10 @@ public class LongMatrix{
grid[row] = Arrays.copyOf(elements, elements.length); grid[row] = Arrays.copyOf(elements, elements.length);
} }
public void setRow(int row, LongMatrix matrix){ public void setRow(int row, LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //Make sure the matrix has a single row
if(matrix.getNumRows() != 1){ if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row");
@@ -234,7 +244,10 @@ public class LongMatrix{
throw new InvalidCoordinatesException("Invalid column number " + col); throw new InvalidCoordinatesException("Invalid column number " + col);
} }
//Make sure the number of elements is valid //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); throw new InvalidCoordinatesException(elements.length, grid.length);
} }
@@ -244,18 +257,32 @@ public class LongMatrix{
} }
} }
public void setCol(int col, LongMatrix matrix){ public void setCol(int col, LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
} }
//Set the column long[] vector = new long[matrix.getNumRows()];
setCol(col, matrix.getCol(0)); for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.grid[cnt][0];
} }
//Set the column
setCol(col, vector);
}
//Adds //Adds
public void addRow(long[] elements){ public void addRow(long[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of columns is valid //Make sure the number of columns is valid
if((grid.length == 0) || (getNumCols() == elements.length)){ else if((grid.length == 0) || (getNumCols() == elements.length)){
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){
@@ -271,8 +298,12 @@ public class LongMatrix{
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
} }
public void addRow(LongMatrix matrix){ public void addRow(LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row //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"); throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
} }
@@ -280,9 +311,16 @@ public class LongMatrix{
addRow(matrix.grid[0]); addRow(matrix.grid[0]);
} }
public void addCol(long[] elements){ public void addCol(long[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of rows is valid //Make sure the number of rows is valid
if(grid.length == 0){ if(grid.length == 0){
grid = new long[elements.length][1]; grid = new long[elements.length][1];
for(int row = 0;row < grid.length;++row){
grid[row][0] = elements[row];
}
} }
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
@@ -296,17 +334,30 @@ public class LongMatrix{
} }
} }
public void addCol(LongMatrix matrix){ public void addCol(LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column //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"); throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
} }
long[] vector = new long[matrix.getNumRows()];
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.get(cnt, 0);
}
//Add the column //Add the column
addCol(matrix.getCol(0)); addCol(vector);
} }
public LongMatrix appendRight(LongMatrix rightSide){ public LongMatrix appendRight(LongMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of rows //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()); throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
} }
@@ -326,8 +377,12 @@ public class LongMatrix{
return new LongMatrix(newGrid); return new LongMatrix(newGrid);
} }
public LongMatrix appendBottom(LongMatrix rightSide){ public LongMatrix appendBottom(LongMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of columns //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()); throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
} }
@@ -568,6 +623,7 @@ public class LongMatrix{
} }
public long det4(){ public long det4(){
long det = 0; long det = 0;
//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;

View File

@@ -1,7 +1,7 @@
//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: 06-29-22 //Modified: 07-01-22
package com.mattrixwv.matrix; package com.mattrixwv.matrix;
@@ -32,6 +32,10 @@ public class ModMatrix extends IntegerMatrix{
return newValue; return newValue;
} }
protected int[] modValues(int[] values){ protected int[] modValues(int[] values){
if(values == null){
throw new InvalidGeometryException("Array cannot be null");
}
int[] newValues = new int[values.length]; int[] newValues = new int[values.length];
for(int cnt = 0;cnt < values.length;++cnt){ for(int cnt = 0;cnt < values.length;++cnt){
@@ -319,7 +323,7 @@ public class ModMatrix extends IntegerMatrix{
if(rightSide == null){ if(rightSide == null){
return false; return false;
} }
if(rightSide.getClass().equals(this.getClass())){ else if(rightSide.getClass().equals(this.getClass())){
return equals((ModMatrix)rightSide); return equals((ModMatrix)rightSide);
} }
else if(rightSide.getClass().equals(int[][].class)){ else if(rightSide.getClass().equals(int[][].class)){

View File

@@ -0,0 +1,23 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/NullMatrixException.java
//Mattrixwv
// Created: 06-30-22
//Modified: 06-30-22
package com.mattrixwv.matrix.exceptions;
public class NullMatrixException extends RuntimeException{
public static final long serialVersionUID = 1;
public NullMatrixException(){
super();
}
public NullMatrixException(String message){
super(message);
}
public NullMatrixException(Throwable throwable){
super(throwable);
}
public NullMatrixException(String message, Throwable throwable){
super(message, throwable);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff