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/BigIntegerMatrix.java
|
||||
//Mattrixwv
|
||||
// Created: 02-10-22
|
||||
//Modified: 06-29-22
|
||||
//Modified: 06-30-22
|
||||
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.InvalidRowSizeException;
|
||||
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
import com.mattrixwv.matrix.exceptions.NullMatrixException;
|
||||
|
||||
|
||||
public class BigIntegerMatrix{
|
||||
@@ -142,9 +143,15 @@ public class BigIntegerMatrix{
|
||||
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];
|
||||
@@ -163,12 +170,7 @@ public class BigIntegerMatrix{
|
||||
return new BigIntegerMatrix(newRow);
|
||||
}
|
||||
public int getNumRows(){
|
||||
if(grid == null){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return grid.length;
|
||||
}
|
||||
return grid.length;
|
||||
}
|
||||
public BigIntegerMatrix getCol(int col){
|
||||
//Make sure the column number is valid
|
||||
@@ -193,6 +195,7 @@ public class BigIntegerMatrix{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Sets
|
||||
public void set(int row, int col, BigInteger value){
|
||||
//Make sure the row number is valid
|
||||
@@ -213,7 +216,10 @@ public class BigIntegerMatrix{
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -221,6 +227,10 @@ public class BigIntegerMatrix{
|
||||
grid[row] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
if(matrix.getNumRows() != 1){
|
||||
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);
|
||||
}
|
||||
//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);
|
||||
}
|
||||
|
||||
@@ -245,18 +258,32 @@ public class BigIntegerMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
throw new InvalidGeometryException("Setting 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.grid[cnt][0];
|
||||
}
|
||||
|
||||
//Set the column
|
||||
setCol(col, matrix.getCol(0));
|
||||
setCol(col, vector);
|
||||
}
|
||||
|
||||
//Adds
|
||||
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
|
||||
if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
else if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
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){
|
||||
@@ -272,8 +299,12 @@ public class BigIntegerMatrix{
|
||||
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
if(matrix.getNumRows() != 1){
|
||||
else if(matrix.getNumRows() != 1){
|
||||
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]);
|
||||
}
|
||||
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
|
||||
if(grid.length == 0){
|
||||
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){
|
||||
//Copy all existing data into the new grid
|
||||
@@ -297,17 +335,30 @@ public class BigIntegerMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
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
|
||||
addCol(matrix.getCol(0));
|
||||
addCol(vector);
|
||||
}
|
||||
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
|
||||
if(getNumRows() != rightSide.getNumRows()){
|
||||
else if(getNumRows() != rightSide.getNumRows()){
|
||||
throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
|
||||
}
|
||||
|
||||
@@ -327,8 +378,12 @@ public class BigIntegerMatrix{
|
||||
return new BigIntegerMatrix(newGrid);
|
||||
}
|
||||
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
|
||||
if(getNumCols() != rightSide.getNumCols()){
|
||||
else if(getNumCols() != rightSide.getNumCols()){
|
||||
throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java
|
||||
//Mattrixwv
|
||||
// Created: 02-07-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 DoubleMatrix{
|
||||
@@ -146,9 +147,15 @@ public class DoubleMatrix{
|
||||
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];
|
||||
@@ -167,12 +174,7 @@ public class DoubleMatrix{
|
||||
return new DoubleMatrix(newRow);
|
||||
}
|
||||
public int getNumRows(){
|
||||
if(grid == null){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return grid.length;
|
||||
}
|
||||
return grid.length;
|
||||
}
|
||||
public DoubleMatrix getCol(int col){
|
||||
//Make sure the column number is valid
|
||||
@@ -197,6 +199,7 @@ public class DoubleMatrix{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Sets
|
||||
public void set(int row, int col, double value){
|
||||
//Make sure the row number is valid
|
||||
@@ -217,7 +220,10 @@ public class DoubleMatrix{
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -225,6 +231,10 @@ public class DoubleMatrix{
|
||||
grid[row] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
if(matrix.getNumRows() != 1){
|
||||
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);
|
||||
}
|
||||
//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);
|
||||
}
|
||||
|
||||
@@ -249,18 +262,32 @@ public class DoubleMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
throw new InvalidGeometryException("Setting 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.grid[cnt][0];
|
||||
}
|
||||
|
||||
//Set the column
|
||||
setCol(col, matrix.getCol(0));
|
||||
setCol(col, vector);
|
||||
}
|
||||
|
||||
//Adds
|
||||
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
|
||||
if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
else if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
double[][] newGrid = new double[grid.length + 1][elements.length];
|
||||
//Copy all existing data into the new grid
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
@@ -276,8 +303,12 @@ public class DoubleMatrix{
|
||||
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
if(matrix.getNumRows() != 1){
|
||||
else if(matrix.getNumRows() != 1){
|
||||
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]);
|
||||
}
|
||||
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
|
||||
if(grid.length == 0){
|
||||
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){
|
||||
//Copy all existing data into the new grid
|
||||
@@ -301,17 +339,30 @@ public class DoubleMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
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
|
||||
addCol(matrix.getCol(0));
|
||||
addCol(vector);
|
||||
}
|
||||
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
|
||||
if(getNumRows() != rightSide.getNumRows()){
|
||||
else if(getNumRows() != rightSide.getNumRows()){
|
||||
throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
|
||||
}
|
||||
|
||||
@@ -331,8 +382,12 @@ public class DoubleMatrix{
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
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
|
||||
if(getNumCols() != rightSide.getNumCols()){
|
||||
else if(getNumCols() != rightSide.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");
|
||||
}
|
||||
}
|
||||
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){
|
||||
//Make sure the matrices have compatable geometry
|
||||
if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java
|
||||
//Mattrixwv
|
||||
// Created: 02-10-22
|
||||
//Modified: 06-29-22
|
||||
//Modified: 07-01-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 LongMatrix{
|
||||
@@ -141,9 +142,15 @@ public class LongMatrix{
|
||||
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 LongMatrix{
|
||||
return new LongMatrix(newRow);
|
||||
}
|
||||
public int getNumRows(){
|
||||
if(grid == null){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return grid.length;
|
||||
}
|
||||
return grid.length;
|
||||
}
|
||||
public LongMatrix getCol(int col){
|
||||
//Make sure the column number is valid
|
||||
@@ -192,6 +194,7 @@ public class LongMatrix{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Sets
|
||||
public void set(int row, int col, long value){
|
||||
//Make sure the row number is valid
|
||||
@@ -212,7 +215,10 @@ public class LongMatrix{
|
||||
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 LongMatrix{
|
||||
grid[row] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
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 LongMatrix{
|
||||
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 LongMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
throw new InvalidGeometryException("Setting 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.grid[cnt][0];
|
||||
}
|
||||
|
||||
//Set the column
|
||||
setCol(col, matrix.getCol(0));
|
||||
setCol(col, vector);
|
||||
}
|
||||
|
||||
//Adds
|
||||
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
|
||||
if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
else if((grid.length == 0) || (getNumCols() == elements.length)){
|
||||
long[][] newGrid = new long[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 LongMatrix{
|
||||
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
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
|
||||
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 LongMatrix{
|
||||
addRow(matrix.grid[0]);
|
||||
}
|
||||
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
|
||||
if(grid.length == 0){
|
||||
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){
|
||||
//Copy all existing data into the new grid
|
||||
@@ -296,17 +334,30 @@ public class LongMatrix{
|
||||
}
|
||||
}
|
||||
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
|
||||
if(matrix.getNumCols() != 1){
|
||||
else if(matrix.getNumCols() != 1){
|
||||
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
|
||||
addCol(matrix.getCol(0));
|
||||
addCol(vector);
|
||||
}
|
||||
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
|
||||
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 LongMatrix{
|
||||
return new LongMatrix(newGrid);
|
||||
}
|
||||
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
|
||||
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 LongMatrix{
|
||||
}
|
||||
public long det4(){
|
||||
long det = 0;
|
||||
|
||||
//Find the row/column with the largest number of 0's
|
||||
int zerosLocation = 0;
|
||||
int maxNumZeros = 0;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java
|
||||
//Mattrixwv
|
||||
// Created: 02-09-22
|
||||
//Modified: 06-29-22
|
||||
//Modified: 07-01-22
|
||||
package com.mattrixwv.matrix;
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ public class ModMatrix extends IntegerMatrix{
|
||||
return newValue;
|
||||
}
|
||||
protected int[] modValues(int[] values){
|
||||
if(values == null){
|
||||
throw new InvalidGeometryException("Array cannot be null");
|
||||
}
|
||||
|
||||
int[] newValues = new int[values.length];
|
||||
|
||||
for(int cnt = 0;cnt < values.length;++cnt){
|
||||
@@ -319,7 +323,7 @@ public class ModMatrix extends IntegerMatrix{
|
||||
if(rightSide == null){
|
||||
return false;
|
||||
}
|
||||
if(rightSide.getClass().equals(this.getClass())){
|
||||
else if(rightSide.getClass().equals(this.getClass())){
|
||||
return equals((ModMatrix)rightSide);
|
||||
}
|
||||
else if(rightSide.getClass().equals(int[][].class)){
|
||||
|
||||
@@ -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
Reference in New Issue
Block a user