IntegerMatrix.java edited online with Bitbucket

This commit is contained in:
2022-02-03 19:41:18 +00:00
parent 742e6d1256
commit d26214f562

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-01-22
//Modified: 02-03-22
package com.mattrixwv.matrix;
@@ -153,6 +153,15 @@ public class IntegerMatrix{
grid[row][col] = elements[col];
}
}
public void setRow(int row, IntegerMatrix matrix){
//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");
}
//Set the row
setRow(row, matrix.grid[0]);
}
public void setCol(int col, int[] elements){
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
@@ -168,6 +177,15 @@ public class IntegerMatrix{
grid[row][col] = elements[row];
}
}
public void setCol(int col, IntegerMatrix matrix){
//Make sure the matrix has a single column
if(matrix.getNumCols() != 1){
throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
}
//Set the column
setCol(col, matrix.getCol(0));
}
//Adds
public void addRow(int[] elements){
//Make sure the number of columns is valid
@@ -188,6 +206,15 @@ public class IntegerMatrix{
grid[grid.length - 1][col] = elements[col];
}
}
public void addRow(IntegerMatrix matrix){
//Make sure the matrix has a single row
if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
}
//Add the row
addRow(matrix.grid[0]);
}
public void addCol(int[] elements){
//Make sure the number of rows is valid
if(grid.length == 0){
@@ -196,8 +223,8 @@ public class IntegerMatrix{
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){
int[] workingRow = new int[grid[row].length + 1];
for(int workingCol = 0;workingCol < grid[row].length;++workingCol){
workingRow[workingCol] = grid[row][workingCol];
}
grid[row] = workingRow;
@@ -212,6 +239,59 @@ public class IntegerMatrix{
grid[row][grid[row].length - 1] = elements[row];
}
}
public void addCol(IntegerMatrix matrix){
//Make sure the matrix has a single column
if(matrix.getNumCols() != 1){
throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
}
//Add the column
addCol(matrix.getCol(0));
}
public IntegerMatrix appendRight(IntegerMatrix rightSide){
//Make sure the matrices have the same number of rows
if(getNumRows() != rightSide.getNumRows()){
throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
}
//Traverse both matrices and set their values in the new matrix
int[][] newGrid = new int[getNumRows()][getNumCols() + rightSide.getNumCols()];
for(int row = 0;row < getNumRows();++row){
//Set all elements from the current grid's row
for(int col = 0;col < getNumCols();++col){
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's row
for(int col = 0;col < rightSide.getNumCols();++col){
newGrid[row][getNumCols() + col] = rightSide.grid[row][col];
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix appendBottom(IntegerMatrix rightSide){
//Make sure the matrices have the same number of columns
if(getNumCols() != rightSide.getNumCols()){
throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
}
//Traverse both matrices and set their values in the new matrix
int[][] newGrid = new int[getNumRows() + rightSide.getNumRows()][getNumCols()];
for(int col = 0;col < getNumCols();++col){
//Set all elements from the current grid's column
for(int row = 0;row < getNumRows();++row){
newGrid[row][col] = grid[row][col];
}
//Set all elements from the right side grid's column
for(int row = 0;row < rightSide.getNumRows();++row){
newGrid[getNumRows() + row][col] = rightSide.grid[row][col];
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
//Simple operations
public static IntegerMatrix generateIdentity(int size){