Added start of integer matrix

This commit is contained in:
2022-02-02 22:10:46 -05:00
parent e5efca7056
commit b5bfbf71f4
6 changed files with 1246 additions and 0 deletions

View File

@@ -0,0 +1,455 @@
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-01-22
package com.mattrixwv.matrix;
import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
public class IntegerMatrix{
private int[][] grid;
//Helper functions
private void setGrid(int[][] grid){
if(grid.length == 0){
grid = new int[0][0];
}
else if(grid[0].length == 0){
grid = new int[grid.length][0];
}
else{
//Make sure all rows are the same length
int length = grid[0].length;
for(int cnt = 1;cnt < grid.length;++cnt){
if(grid[cnt].length != length){
throw new InvalidRowSizeException("All rows in a matrix must be the same size");
}
}
//Copy every element over to a new grid
int[][] newGrid = new int[grid.length][grid[0].length];
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){
newGrid[rowCnt][colCnt] = grid[rowCnt][colCnt];
}
}
//Save the new grid
this.grid = newGrid;
}
}
private int[][] copyGrid(){
//Allocate memory for the new grid
int[][] newGrid = new int[grid.length][grid[0].length];
//Copy every element from the current grid to the new one
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){
newGrid[row][col] = grid[row][col];
}
}
//Return the new grid
return newGrid;
}
//Constructors
public IntegerMatrix(){
grid = new int[0][0];
}
public IntegerMatrix(int[][]grid){
setGrid(grid);
}
public IntegerMatrix(IntegerMatrix matrix){
setGrid(matrix.grid);
}
//Gets
public int get(int row, int col){
//Make sure the row and column are valid
if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows");
}
else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns");
}
//Return the location in the grid
return grid[row][col];
}
public IntegerMatrix getRow(int row){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
throw new InvalidCoordinatesException("The row number " + row + " is not valid");
}
//Generate a copy of the row
int[][] newRow = new int[1][grid[row].length];
for(int col = 0;col < grid[row].length;++col){
newRow[0][col] = grid[row][col];
}
//Return the new matrix
return new IntegerMatrix(newRow);
}
public int getNumRows(){
return grid.length;
}
public IntegerMatrix getCol(int col){
//Make sure the column number is valid
if((col < 0) || (grid.length == 0) || (col > grid[0].length)){
throw new InvalidCoordinatesException("The column number " + col + " is not valid");
}
//Generate a copy of the col
int[][] newColumn = new int[grid.length][1];
for(int row = 0;row < grid.length;++row){
newColumn[row][0] = grid[row][col];
}
//Return the new matrix
return new IntegerMatrix(newColumn);
}
public int getNumCols(){
if(grid.length > 0){
return grid[0].length;
}
else{
return 0;
}
}
//Sets
public void set(int row, int col, int value){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
throw new InvalidCoordinatesException("Invalid row number " + row);
}
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
throw new InvalidCoordinatesException("Invalid column number " + col);
}
//Save the element
grid[row][col] = value;
}
public void setRow(int row, int[] elements){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
throw new InvalidCoordinatesException("Invalid row number " + row);
}
//Make sure the number of elements is valid
if(elements.length != getNumCols()){
throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols());
}
//Save the elements
for(int col = 0;col < elements.length;++col){
grid[row][col] = elements[col];
}
}
public void setCol(int col, int[] elements){
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
throw new InvalidCoordinatesException("Invalid column number " + col);
}
//Make sure the number of elements is valid
if(elements.length != grid.length){
throw new InvalidCoordinatesException("Invalid number of elements " + elements.length + " must be " + grid.length);
}
//Save the elements
for(int row = 0;row < elements.length;++row){
grid[row][col] = elements[row];
}
}
//Adds
public void addRow(int[] elements){
//Make sure the number of columns is valid
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){
newGrid[row] = grid[row];
}
grid = newGrid;
}
else{
throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols());
}
//Add all elements to the grid
for(int col = 0;col < elements.length;++col){
grid[grid.length - 1][col] = elements[col];
}
}
public void addCol(int[] elements){
//Make sure the number of rows is valid
if(grid.length == 0){
grid = new int[1][elements.length];
}
else if(grid.length == elements.length){
//Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){
int[] workingRow = new int[grid[0].length + 1];
for(int workingCol = 0;workingCol < grid[0].length;++workingCol){
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
}
}
else{
throw new InvalidGeometryException("Invalid number of elements " + elements.length + " must be " + getNumCols());
}
//Add all elements to the grid
for(int row = 0;row < elements.length;++row){
grid[row][grid[row].length - 1] = elements[row];
}
}
//Simple operations
public static IntegerMatrix generateIdentity(int size){
//Make sure the size is valid
if(size > 0){
//Create a grid with the correct size
int[][] newGrid = new int[size][size];
//Go through every element and make sure it is set correctly
for(int row = 0;row < size;++row){
for(int col = 0;col < size;++col){
if(col == row){
newGrid[row][col] = 1;
}
else{
newGrid[row][col] = 0;
}
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
else{
throw new InvalidGeometryException("An identity matrix must have a size > 0");
}
}
public static IntegerMatrix generateFilled(int size, int fill){
//Create a grid with the correct size
int[][] newGrid = new int[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 IntegerMatrix(newGrid);
}
public IntegerMatrix add(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){
throw new InvalidGeometryException("Both matrices must have the same number of rows and columns: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols());
}
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
//Add each element in the righ tmatrix to the corresponding element in the left matrix
for(int row = 0;row < newGrid.length;++row){
for(int col = 0;col < newGrid[0].length;++col){
newGrid[row][col] += rightSide.grid[row][col];
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix add(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
//Add the scalar to each element in the grid
for(int row = 0;row < newGrid.length;++row){
for(int col = 0;col < newGrid[0].length;++col){
newGrid[row][col] += scalar;
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix subtract(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){
throw new InvalidGeometryException("Both matrices must have the same number of rows and columsn: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols());
}
//Create a new grid with the same elements as the current gird
int[][] newGrid = copyGrid();
//Subtract each element in the righ tmatrix from the corresponding element in the left matrix
for(int row = 0;row < newGrid.length;++row){
for(int col = 0;col < newGrid[0].length;++col){
newGrid[row][col] -= grid[row][col];
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix subtract(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
//Subtract the scalar from each element in the grid
for(int row = 0;row < newGrid.length;++row){
for(int col = 0;col < newGrid[0].length;++col){
newGrid[row][col] -= scalar;
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix multiply(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if(getNumCols() != rightSide.getNumRows()){
throw new InvalidGeometryException("The left matrix must have the same number of columns as the right matrix has rows: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols());
}
//Create a new grid with the same elements as the current grid
int[][] newGrid = new int[getNumRows()][rightSide.getNumCols()];
//Multiply each row in the left matrix with each column int he right matrix to come up with the current element
for(int leftRow = 0;leftRow < grid.length;++leftRow){
for(int rightCol = 0;rightCol < rightSide.getNumCols();++rightCol){
//Get a sum of the product of each column in the current row (left) and each row in the current column (right)
int elementProductSum = 0;
for(int incrementCounter = 0;incrementCounter < grid.length;++incrementCounter){
elementProductSum += grid[leftRow][incrementCounter] * rightSide.grid[incrementCounter][rightCol];
}
newGrid[leftRow][rightCol] = elementProductSum;
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix multiply(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
//Multiply every element in the grid by the scalar
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){
newGrid[row][col] *= scalar;
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public int dotProduct(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if(getNumCols() != rightSide.getNumRows()){
throw new InvalidGeometryException("The left matrix must have the same number of columns as the right matrix has rows: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols());
}
//Multiply each row in the left matrix with each column in the right matrix to come up with the current element
//?Would be simpler, but slower, to get the product matrix to come up with the current element
int sum = 0;
for(int leftRow = 0;leftRow < grid.length;++leftRow){
for(int rightCol = 0;rightCol < rightSide.getNumCols();++rightCol){
for(int incrementCounter = 0;incrementCounter < grid.length;++incrementCounter){
sum += grid[leftRow][incrementCounter] * rightSide.grid[incrementCounter][rightCol];
}
}
}
//Return the sum
return sum;
}
public IntegerMatrix hadamardProduct(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){
throw new InvalidGeometryException("Both matrices must have the same number of rows and columns: " + getNumRows() + "x" + getNumCols() + " + " + rightSide.getNumRows() + "x" + rightSide.getNumCols());
}
//Create a new grid with the same element as the current grid
int[][] newGrid = copyGrid();
//Multiply each element in the right matrix to the corresponding element in the left matrix
for(int row = 0;row < newGrid.length;++row){
for(int col = 0;col < newGrid[0].length;++col){
newGrid[row][col] *= rightSide.grid[row][col];
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
//Complex operations
//Object funtions
@Override
public boolean equals(Object rightSide){
if(rightSide.getClass().equals(this.getClass())){
IntegerMatrix rightMatrix = (IntegerMatrix)rightSide;
//Make sure they have the same number of elements
if(grid.length != rightMatrix.grid.length){
return false;
}
else if(getNumCols() != rightMatrix.getNumCols()){
return false;
}
//Check every element
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[0].length;++col){
if(grid[row][col] != rightMatrix.grid[row][col]){
return false;
}
}
}
//If false hasn't been returned yet then they are equal
return true;
}
else if(rightSide.getClass().equals(int[][].class)){
int[][] rightMatrix = (int[][])rightSide;
return equals(new IntegerMatrix(rightMatrix));
}
else{
return false;
}
}
@Override
public String toString(){
StringJoiner matrix = new StringJoiner("\n");
for(int rowCnt = 0;rowCnt < grid.length;++rowCnt){
StringJoiner row = new StringJoiner(",", "[", "]");
for(int colCnt = 0;colCnt < grid[0].length;++colCnt){
row.add(Integer.toString(grid[rowCnt][colCnt]));
}
matrix.add(row.toString());
}
return matrix.toString();
}
@Override
public IntegerMatrix clone(){
return new IntegerMatrix(grid);
}
}

View File

@@ -0,0 +1,21 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidCoordinates.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-01-22
package com.mattrixwv.matrix.exceptions;
public class InvalidCoordinatesException extends RuntimeException{
public InvalidCoordinatesException(){
super();
}
public InvalidCoordinatesException(String message){
super(message);
}
public InvalidCoordinatesException(Throwable throwable){
super(throwable);
}
public InvalidCoordinatesException(String message, Throwable throwable){
super(message, throwable);
}
}

View File

@@ -0,0 +1,21 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidGeometryException.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-02-22
package com.mattrixwv.matrix.exceptions;
public class InvalidGeometryException extends RuntimeException{
public InvalidGeometryException(){
super();
}
public InvalidGeometryException(String message){
super(message);
}
public InvalidGeometryException(Throwable throwable){
super(throwable);
}
public InvalidGeometryException(String message, Throwable throwable){
super(message, throwable);
}
}

View File

@@ -0,0 +1,21 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidRowSizeException.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-01-22
package com.mattrixwv.matrix.exceptions;
public class InvalidRowSizeException extends RuntimeException{
public InvalidRowSizeException(){
super();
}
public InvalidRowSizeException(String message){
super(message);
}
public InvalidRowSizeException(Throwable throwable){
super(throwable);
}
public InvalidRowSizeException(String message, Throwable throwable){
super(message, throwable);
}
}