Updated for SonarQube findings
This commit is contained in:
@@ -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())){
|
||||
|
||||
Reference in New Issue
Block a user