Bugfixes
Update tests Add javadoc comments
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-30-23
|
||||
//Modified: 08-11-24
|
||||
package com.mattrixwv.matrix;
|
||||
|
||||
|
||||
@@ -15,11 +15,27 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
import com.mattrixwv.matrix.exceptions.NullMatrixException;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a matrix of floating point values and provides various matrix operations.
|
||||
*/
|
||||
public class DoubleMatrix{
|
||||
/**
|
||||
* The grid that represents the matrix
|
||||
*/
|
||||
protected double[][] grid;
|
||||
/**
|
||||
* The difference an element can be and still be considered equals
|
||||
*/
|
||||
protected double delta;
|
||||
|
||||
//Helper functions
|
||||
//?Helper functions
|
||||
/**
|
||||
* Sets the matrix grid to the specified 2D array. Validates the input to ensure
|
||||
* all rows are of equal length.
|
||||
*
|
||||
* @param grid The 2D array to set as the matrix grid.
|
||||
* @throws InvalidRowSizeException If the rows of the matrix are not all the same length.
|
||||
*/
|
||||
protected void setGrid(double[][] grid){
|
||||
if(grid.length == 0){
|
||||
this.grid = new double[0][0];
|
||||
@@ -46,18 +62,32 @@ public class DoubleMatrix{
|
||||
this.grid = newGrid;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates a deep copy of the matrix grid.
|
||||
*
|
||||
* @return A new DoubleMatrix instance containing the copied grid.
|
||||
*/
|
||||
protected double[][] copyGrid(){
|
||||
if(getNumCols() == 0){
|
||||
return new double[grid.length][0];
|
||||
}
|
||||
|
||||
//Allocate memory for the new grid
|
||||
double[][] newGrid = new double[grid.length][grid[0].length];
|
||||
|
||||
//Copy every element from the current grid to the new one
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
newGrid[row] = Arrays.copyOf(grid[row], grid.length);
|
||||
newGrid[row] = Arrays.copyOf(grid[row], grid[row].length);
|
||||
}
|
||||
|
||||
//Return the new grid
|
||||
return newGrid;
|
||||
}
|
||||
/**
|
||||
* Checks if the matrix is a square matrix (i.e., the number of rows equals the number of columns).
|
||||
*
|
||||
* @return {@code true} if the matrix is square, {@code false} otherwise.
|
||||
*/
|
||||
public boolean isSquare(){
|
||||
if(getNumRows() == 0){
|
||||
return false;
|
||||
@@ -66,7 +96,16 @@ public class DoubleMatrix{
|
||||
return getNumRows() == getNumCols();
|
||||
}
|
||||
}
|
||||
//Returns a matrix with the supplied row and column removed
|
||||
/**
|
||||
* Creates a new matrix with the specified row and column removed.
|
||||
* This matrix is smaller by one row and one column than the current matrix.
|
||||
*
|
||||
* @param row The index of the row to remove.
|
||||
* @param col The index of the column to remove.
|
||||
* @return A new DoubleMatrix instance with the specified row and column removed.
|
||||
* @throws InvalidGeometryException If the matrix is not square or too small for Laplace expansion.
|
||||
* @throws InvalidCoordinatesException If the row or column index is out of bounds.
|
||||
*/
|
||||
protected DoubleMatrix laplaceExpansionHelper(int row, int col){
|
||||
//Make sure the matrix is large enough to have this operation performed
|
||||
if((getNumRows() <= 1) || (getNumCols() <= 1)){
|
||||
@@ -110,19 +149,40 @@ public class DoubleMatrix{
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs an empty matrix (0x0).
|
||||
*/
|
||||
public DoubleMatrix(){
|
||||
grid = new double[0][0];
|
||||
delta = 0.0;
|
||||
}
|
||||
/**
|
||||
* Constructs a matrix with the specified grid.
|
||||
*
|
||||
* @param grid The 2D array to initialize the matrix with.
|
||||
*/
|
||||
public DoubleMatrix(double[][] grid){
|
||||
setGrid(grid);
|
||||
delta = 0.0;
|
||||
}
|
||||
/**
|
||||
* Constructs a copy of the specified matrix.
|
||||
*
|
||||
* @param matrix The matrix to copy.
|
||||
*/
|
||||
public DoubleMatrix(DoubleMatrix matrix){
|
||||
setGrid(matrix.grid);
|
||||
delta = 0.0;
|
||||
}
|
||||
/**
|
||||
* Constructs a matrix with the specified number of rows and columns, filled with the specified value.
|
||||
*
|
||||
* @param rows The number of rows.
|
||||
* @param cols The number of columns.
|
||||
* @param fill The value to fill the matrix with.
|
||||
* @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero.
|
||||
*/
|
||||
public DoubleMatrix(int rows, int cols, double fill){
|
||||
if(rows <= 0){
|
||||
throw new InvalidGeometryException("A filled matrix must have at least 1 row");
|
||||
@@ -141,7 +201,15 @@ public class DoubleMatrix{
|
||||
}
|
||||
}
|
||||
|
||||
//Gets
|
||||
//?Gets
|
||||
/**
|
||||
* Gets the value at the specified row and column.
|
||||
*
|
||||
* @param row The row index.
|
||||
* @param col The column index.
|
||||
* @return The value at the specified row and column.
|
||||
* @throws InvalidCoordinatesException If the row or column index is out of bounds.
|
||||
*/
|
||||
public double get(int row, int col){
|
||||
//Make sure the row and column are valid
|
||||
if(row >= grid.length){
|
||||
@@ -150,7 +218,7 @@ public class DoubleMatrix{
|
||||
else if(row < 0){
|
||||
throw new InvalidCoordinatesException("Row cannot be less than 0");
|
||||
}
|
||||
else if(col >= grid[0].length){
|
||||
else if(col >= grid[row].length){
|
||||
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns");
|
||||
}
|
||||
else if(col < 0){
|
||||
@@ -160,6 +228,13 @@ public class DoubleMatrix{
|
||||
//Return the location in the grid
|
||||
return grid[row][col];
|
||||
}
|
||||
/**
|
||||
* Returns a new matrix that is a copy of the specified row.
|
||||
*
|
||||
* @param row The index of the row to retrieve.
|
||||
* @return A new DoubleMatrix instance containing the specified row.
|
||||
* @throws InvalidCoordinatesException If the row index is out of bounds.
|
||||
*/
|
||||
public DoubleMatrix getRow(int row){
|
||||
//Make sure the row number is valid
|
||||
if((row < 0) || (row >= grid.length)){
|
||||
@@ -173,12 +248,24 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newRow);
|
||||
}
|
||||
/**
|
||||
* Returns the number of rows in the matrix.
|
||||
*
|
||||
* @return The number of rows.
|
||||
*/
|
||||
public int getNumRows(){
|
||||
return grid.length;
|
||||
}
|
||||
/**
|
||||
* Returns a new matrix that is a copy of the specified column.
|
||||
*
|
||||
* @param col The index of the column to retrieve.
|
||||
* @return A new DoubleMatrix instance containing the specified column.
|
||||
* @throws InvalidCoordinatesException If the column index is out of bounds.
|
||||
*/
|
||||
public DoubleMatrix getCol(int col){
|
||||
//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)){
|
||||
throw new InvalidCoordinatesException("The column number " + col + " is not valid");
|
||||
}
|
||||
|
||||
@@ -191,6 +278,11 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newColumn);
|
||||
}
|
||||
/**
|
||||
* Returns the number of columns in the matrix.
|
||||
*
|
||||
* @return The number of columns.
|
||||
*/
|
||||
public int getNumCols(){
|
||||
if(grid.length > 0){
|
||||
return grid[0].length;
|
||||
@@ -200,7 +292,15 @@ public class DoubleMatrix{
|
||||
}
|
||||
}
|
||||
|
||||
//Sets
|
||||
//?Sets
|
||||
/**
|
||||
* Sets the value at the specified row and column.
|
||||
*
|
||||
* @param row The row index.
|
||||
* @param col The column index.
|
||||
* @param value The value to set.
|
||||
* @throws InvalidCoordinatesException If the row or column index is out of bounds.
|
||||
*/
|
||||
public void set(int row, int col, double value){
|
||||
//Make sure the row number is valid
|
||||
if((row < 0) || (row >= grid.length)){
|
||||
@@ -214,6 +314,14 @@ public class DoubleMatrix{
|
||||
//Save the element
|
||||
grid[row][col] = value;
|
||||
}
|
||||
/**
|
||||
* Sets the specified row with the given array of elements.
|
||||
*
|
||||
* @param row The row index.
|
||||
* @param elements The array of elements to set.
|
||||
* @throws InvalidCoordinatesException If the row index is out of bounds.
|
||||
* @throws InvalidGeometryException If the length of the elements array does not match the number of columns.
|
||||
*/
|
||||
public void setRow(int row, double[] elements){
|
||||
//Make sure the row number is valid
|
||||
if((row < 0) || (row >= grid.length)){
|
||||
@@ -230,6 +338,14 @@ public class DoubleMatrix{
|
||||
//Save the elements
|
||||
grid[row] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
/**
|
||||
* Sets the specified row with the given matrix containing a single row.
|
||||
*
|
||||
* @param row The row index.
|
||||
* @param matrix The matrix containing a single row to set.
|
||||
* @throws NullMatrixException If the matrix is null.
|
||||
* @throws InvalidGeometryException If the matrix does not contain a single row.
|
||||
*/
|
||||
public void setRow(int row, DoubleMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
@@ -243,6 +359,14 @@ public class DoubleMatrix{
|
||||
//Set the row
|
||||
setRow(row, matrix.grid[0]);
|
||||
}
|
||||
/**
|
||||
* Sets the specified column with the given array of elements.
|
||||
*
|
||||
* @param col The column index.
|
||||
* @param elements The array of elements to set.
|
||||
* @throws InvalidCoordinatesException If the column index is out of bounds.
|
||||
* @throws InvalidGeometryException If the length of the elements array does not match the number of rows.
|
||||
*/
|
||||
public void setCol(int col, double[] elements){
|
||||
//Make sure the column number is valid
|
||||
if((col < 0) || (col >= getNumCols())){
|
||||
@@ -253,7 +377,7 @@ public class DoubleMatrix{
|
||||
throw new InvalidGeometryException("Column cannot be null");
|
||||
}
|
||||
else if(elements.length != grid.length){
|
||||
throw new InvalidCoordinatesException(elements.length, grid.length);
|
||||
throw new InvalidGeometryException(elements.length, grid.length);
|
||||
}
|
||||
|
||||
//Save the elements
|
||||
@@ -261,6 +385,14 @@ public class DoubleMatrix{
|
||||
grid[row][col] = elements[row];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the specified column with the given matrix containing a single column.
|
||||
*
|
||||
* @param col The column index.
|
||||
* @param matrix The matrix containing a single column to set.
|
||||
* @throws NullMatrixException If the matrix is null.
|
||||
* @throws InvalidGeometryException If the matrix does not contain a single column.
|
||||
*/
|
||||
public void setCol(int col, DoubleMatrix matrix){
|
||||
//Make sure teh matrix isn't null
|
||||
if(matrix == null){
|
||||
@@ -280,7 +412,14 @@ public class DoubleMatrix{
|
||||
setCol(col, vector);
|
||||
}
|
||||
|
||||
//Adds
|
||||
//?Adds
|
||||
/**
|
||||
* Adds a new row with the specified array of elements to the matrix.
|
||||
*
|
||||
* @param elements The array of elements to add as a new row.
|
||||
* @throws NullMatrixException If the elements array is null.
|
||||
* @throws InvalidGeometryException If the length of the elements array does not match the number of columns.
|
||||
*/
|
||||
public void addRow(double[] elements){
|
||||
//Make sure the matrix isn't null
|
||||
if(elements == null){
|
||||
@@ -302,6 +441,13 @@ public class DoubleMatrix{
|
||||
//Add all elements to the grid
|
||||
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
|
||||
}
|
||||
/**
|
||||
* Adds a new row with the given matrix containing a single row to the matrix.
|
||||
*
|
||||
* @param matrix The matrix containing a single row to add.
|
||||
* @throws NullMatrixException If the matrix is null.
|
||||
* @throws InvalidGeometryException If the matrix does not contain a single row.
|
||||
*/
|
||||
public void addRow(DoubleMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
@@ -315,6 +461,13 @@ public class DoubleMatrix{
|
||||
//Add the row
|
||||
addRow(matrix.grid[0]);
|
||||
}
|
||||
/**
|
||||
* Adds a new column with the specified array of elements to the matrix.
|
||||
*
|
||||
* @param elements The array of elements to add as a new column.
|
||||
* @throws NullMatrixException If the elements array is null.
|
||||
* @throws InvalidGeometryException If the length of the elements array does not match the number of rows.
|
||||
*/
|
||||
public void addCol(double[] elements){
|
||||
//Make sure the matrix isn't null
|
||||
if(elements == null){
|
||||
@@ -338,6 +491,13 @@ public class DoubleMatrix{
|
||||
throw new InvalidGeometryException(elements.length, getNumCols());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds a new column with the given matrix containing a single column to the matrix.
|
||||
*
|
||||
* @param matrix The matrix containing a single column to add.
|
||||
* @throws NullMatrixException If the matrix is null.
|
||||
* @throws InvalidGeometryException If the matrix does not contain a single column.
|
||||
*/
|
||||
public void addCol(DoubleMatrix matrix){
|
||||
//Make sure the matrix isn't null
|
||||
if(matrix == null){
|
||||
@@ -356,6 +516,14 @@ public class DoubleMatrix{
|
||||
//Add the column
|
||||
addCol(vector);
|
||||
}
|
||||
/**
|
||||
* Appends the specified matrix to the right side of the current matrix.
|
||||
*
|
||||
* @param rightSide The matrix to append to the right side.
|
||||
* @return A new DoubleMatrix instance with the right-side matrix appended.
|
||||
* @throws NullMatrixException If the right-side matrix is null.
|
||||
* @throws InvalidGeometryException If the number of rows does not match.
|
||||
*/
|
||||
public DoubleMatrix appendRight(DoubleMatrix rightSide){
|
||||
//Make sure teh matrix isn't null
|
||||
if(rightSide == null){
|
||||
@@ -381,26 +549,34 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
public DoubleMatrix appendBottom(DoubleMatrix rightSide){
|
||||
/**
|
||||
* Appends the specified matrix to the bottom of the current matrix.
|
||||
*
|
||||
* @param bottomSide The matrix to append to the bottom.
|
||||
* @return A new DoubleMatrix instance with the bottom matrix appended.
|
||||
* @throws NullMatrixException If the bottom matrix is null.
|
||||
* @throws InvalidGeometryException If the number of columns does not match.
|
||||
*/
|
||||
public DoubleMatrix appendBottom(DoubleMatrix bottomSide){
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
if(bottomSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
//Make sure the matrices have the same number of columns
|
||||
else if(getNumCols() != rightSide.getNumCols()){
|
||||
throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
|
||||
else if(getNumCols() != bottomSide.getNumCols()){
|
||||
throw new InvalidGeometryException("Invalid number of columns. " + bottomSide.getNumCols() + " must be " + getNumCols());
|
||||
}
|
||||
|
||||
//Traverse both matrices and set their values in the new matrix
|
||||
double[][] newGrid = new double[getNumRows() + rightSide.getNumRows()][getNumCols()];
|
||||
double[][] newGrid = new double[getNumRows() + bottomSide.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];
|
||||
for(int row = 0;row < bottomSide.getNumRows();++row){
|
||||
newGrid[getNumRows() + row][col] = bottomSide.grid[row][col];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,7 +584,14 @@ public class DoubleMatrix{
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
|
||||
//Simple operations
|
||||
//?Simple operations
|
||||
/**
|
||||
* Generates an identity matrix of the given size.
|
||||
*
|
||||
* @param size The size of the identity matrix.
|
||||
* @return A new DoubleMatrix instance representing the identity matrix.
|
||||
* @throws InvalidGeometryException If the size is less than or equal to zero.
|
||||
*/
|
||||
public static DoubleMatrix generateIdentity(int size){
|
||||
//Make sure the size is valid
|
||||
if(size > 0){
|
||||
@@ -434,8 +617,20 @@ public class DoubleMatrix{
|
||||
throw new InvalidGeometryException("An identity matrix must have a size > 0");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds the specified matrix to the current matrix.
|
||||
*
|
||||
* @param rightSide The matrix to add.
|
||||
* @return A new DoubleMatrix instance with the result of the addition.
|
||||
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
|
||||
*/
|
||||
public DoubleMatrix add(DoubleMatrix rightSide){
|
||||
//Make sure the matrices have compatable geometry
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
|
||||
//Make sure the matrices have compatible 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());
|
||||
}
|
||||
@@ -443,7 +638,7 @@ public class DoubleMatrix{
|
||||
//Create a new grid with the same elements as the current grid
|
||||
double[][] newGrid = copyGrid();
|
||||
|
||||
//Add each element in the righ tmatrix to the corresponding element in the left matrix
|
||||
//Add 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];
|
||||
@@ -453,6 +648,12 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Adds the scalar to every element in the matrix.
|
||||
*
|
||||
* @param scalar The scalar to add.
|
||||
* @return A new DoubleMatrix instance with the result of the addition.
|
||||
*/
|
||||
public DoubleMatrix add(double scalar){
|
||||
//Create a new grid with the same elements as the current grid
|
||||
double[][] newGrid = copyGrid();
|
||||
@@ -467,8 +668,20 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Subtracts the specified matrix from the current matrix.
|
||||
*
|
||||
* @param rightSide The matrix to subtract.
|
||||
* @return A new DoubleMatrix instance with the result of the subtraction.
|
||||
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
|
||||
*/
|
||||
public DoubleMatrix subtract(DoubleMatrix rightSide){
|
||||
//Make sure the matrices have compatable geometry
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
|
||||
//Make sure the matrices have compatible 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());
|
||||
}
|
||||
@@ -476,16 +689,22 @@ public class DoubleMatrix{
|
||||
//Create a new grid with the same elements as the current gird
|
||||
double[][] newGrid = copyGrid();
|
||||
|
||||
//Subtract each element in the righ tmatrix from the corresponding element in the left matrix
|
||||
//Subtract each element in the right matrix 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];
|
||||
newGrid[row][col] -= rightSide.grid[row][col];
|
||||
}
|
||||
}
|
||||
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Subtracts the scalar from every element in the matrix.
|
||||
*
|
||||
* @param scalar The scalar to subtract.
|
||||
* @return A new DoubleMatrix instance with the result of the subtraction.
|
||||
*/
|
||||
public DoubleMatrix subtract(double scalar){
|
||||
//Create a new grid with the same elements as the current grid
|
||||
double[][] newGrid = copyGrid();
|
||||
@@ -500,8 +719,20 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Multiplies the current matrix by the specified matrix.
|
||||
*
|
||||
* @param rightSide The matrix to multiply by.
|
||||
* @return A new DoubleMatrix instance with the result of the multiplication.
|
||||
* @throws InvalidGeometryException If the number of columns in the current matrix does not match the number of rows in the right-side matrix.
|
||||
*/
|
||||
public DoubleMatrix multiply(DoubleMatrix rightSide){
|
||||
//Make sure the matrices have compatable geometry
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
|
||||
//Make sure the matrices have compatible 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());
|
||||
}
|
||||
@@ -524,6 +755,12 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Multiplies every element in the matrix by the scalar.
|
||||
*
|
||||
* @param scalar the scalar to multiply
|
||||
* @return A new DoubleMatrix instance with the result of the multiplication
|
||||
*/
|
||||
public DoubleMatrix multiply(double scalar){
|
||||
//Create a new grid with the same elements as the current grid
|
||||
double[][] newGrid = copyGrid();
|
||||
@@ -538,6 +775,14 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Multiplies the current matrix by itself the given number of times.
|
||||
*
|
||||
* @param power The number of times to multiply the matrix by itself
|
||||
* @return A new DoubleMatrix instance with the result of the multiplication
|
||||
* @throws InvalidScalarException If the power is negative
|
||||
* @throws InvalidGeometryException If the matrix is not square
|
||||
*/
|
||||
public DoubleMatrix pow(int power){
|
||||
//Make sure the matrix is square so it can be multiplied
|
||||
if(!isSquare()){
|
||||
@@ -561,8 +806,21 @@ public class DoubleMatrix{
|
||||
//Return the new grid
|
||||
return newMatrix;
|
||||
}
|
||||
/**
|
||||
* Calculates the dot product of the two matrices.
|
||||
*
|
||||
* @param rightSide The matrix to use on the right side of the calculation
|
||||
* @return The dot product of the two matrices
|
||||
* @throws NullMatrixException If the right matrix is null
|
||||
* @throws InvalidGeometryException If the matrices do not have compatible dimensions
|
||||
*/
|
||||
public double dotProduct(DoubleMatrix rightSide){
|
||||
//Make sure the matrices have compatable geometry
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
|
||||
//Make sure the matrices have compatible 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());
|
||||
}
|
||||
@@ -581,8 +839,21 @@ public class DoubleMatrix{
|
||||
//Return the sum
|
||||
return sum;
|
||||
}
|
||||
/**
|
||||
* Calculates the Hadamard product of the two matrices.
|
||||
*
|
||||
* @param rightSide The matrix to use on the right side of the calculation
|
||||
* @return The Hadamard product of the two matrices
|
||||
* @throws NullMatrixException If the right matrix is null
|
||||
* @throws InvalidGeometryException If the matrices do not have compatible dimensions
|
||||
*/
|
||||
public DoubleMatrix hadamardProduct(DoubleMatrix rightSide){
|
||||
//Make sure the matrices have compatable geometry
|
||||
//Make sure the matrix isn't null
|
||||
if(rightSide == null){
|
||||
throw new NullMatrixException();
|
||||
}
|
||||
|
||||
//Make sure the matrices have compatible 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());
|
||||
}
|
||||
@@ -601,7 +872,12 @@ public class DoubleMatrix{
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
|
||||
//Complex operations
|
||||
//?Complex operations
|
||||
/**
|
||||
* Transposes the current matrix (i.e., swaps rows and columns).
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the transposed matrix.
|
||||
*/
|
||||
public DoubleMatrix transpose(){
|
||||
//Create a new grid
|
||||
double[][] newGrid = new double[getNumCols()][getNumRows()];
|
||||
@@ -616,16 +892,41 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used.
|
||||
*
|
||||
* @return The determinant of the matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
* @see #determinant()
|
||||
*/
|
||||
public double det(){
|
||||
return determinant();
|
||||
}
|
||||
/**
|
||||
* Calculates the determinant of a 2x2 matrix.
|
||||
*
|
||||
* @return The determinant of the matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not 2x2.
|
||||
*/
|
||||
private double det2(){
|
||||
return (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]);
|
||||
}
|
||||
/**
|
||||
* Calculates the determinant of a 3x3 matrix.
|
||||
*
|
||||
* @return The determinant of the matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not 3x3.
|
||||
*/
|
||||
private double det3(){
|
||||
return (grid[0][0] * grid[1][1] * grid[2][2]) + (grid[0][1] * grid[1][2] * grid[2][0]) + (grid[0][2] * grid[1][0] * grid[2][1]) -
|
||||
(grid[2][0] * grid[1][1] * grid[0][2]) - (grid[2][1] * grid[1][2] * grid[0][0]) - (grid[2][2] * grid[1][0] * grid[0][1]);
|
||||
}
|
||||
/**
|
||||
* Calculates the determinant of a 4x4 or larger matrix.
|
||||
*
|
||||
* @return The determinant of the matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not 4x4.
|
||||
*/
|
||||
private double det4(){
|
||||
double det = 0;
|
||||
|
||||
@@ -695,6 +996,12 @@ public class DoubleMatrix{
|
||||
|
||||
return det;
|
||||
}
|
||||
/**
|
||||
* Calculates the determinant of the matrix. For matrices 4x4 or larger, a recursive approach is used.
|
||||
*
|
||||
* @return The determinant of the matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
*/
|
||||
public double determinant(){
|
||||
//Make sure the matrix is square
|
||||
if(!isSquare()){
|
||||
@@ -718,9 +1025,22 @@ public class DoubleMatrix{
|
||||
//Return the determinant
|
||||
return det;
|
||||
}
|
||||
/**
|
||||
* Calculates the cofactor matrix of the current matrix.
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the cofactor matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
* @see #cofactor()
|
||||
*/
|
||||
public DoubleMatrix cof(){
|
||||
return cofactor();
|
||||
}
|
||||
/**
|
||||
* Calculates the cofactor matrix of the current matrix.
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the cofactor matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
*/
|
||||
public DoubleMatrix cofactor(){
|
||||
//Make sure the matrix is square
|
||||
if(!isSquare()){
|
||||
@@ -747,12 +1067,31 @@ public class DoubleMatrix{
|
||||
//Return the new matrix
|
||||
return new DoubleMatrix(newGrid);
|
||||
}
|
||||
/**
|
||||
* Calculates the adjoint matrix of the current matrix.
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the adjoint matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
* @see #adjoint()
|
||||
*/
|
||||
public DoubleMatrix adj(){
|
||||
return adjoint();
|
||||
}
|
||||
/**
|
||||
* Calculates the adjoint matrix of the current matrix.
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the adjoint matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square.
|
||||
*/
|
||||
public DoubleMatrix adjoint(){
|
||||
return cofactor().transpose();
|
||||
}
|
||||
/**
|
||||
* Calculates the inverse of the current matrix.
|
||||
*
|
||||
* @return A new DoubleMatrix instance representing the inverse matrix.
|
||||
* @throws InvalidGeometryException If the matrix is not square or if the determinant is 0.
|
||||
*/
|
||||
public DoubleMatrix inverse(){
|
||||
//Make sure the matrix is square
|
||||
if(!isSquare()){
|
||||
@@ -769,7 +1108,15 @@ public class DoubleMatrix{
|
||||
return adjoint().multiply(1 / determinant);
|
||||
}
|
||||
|
||||
//Object funtions
|
||||
//?Object functions
|
||||
/**
|
||||
* Determines whether the given object is equal to the current matrix.
|
||||
* Can determine equality using DoubleMatrix or double[][].
|
||||
*
|
||||
* @param rightSide The object to compare to the current matrix.
|
||||
* @return True if the objects are equal, false otherwise.
|
||||
* @see #equals(DoubleMatrix)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object rightSide){
|
||||
if(rightSide == null){
|
||||
@@ -786,7 +1133,17 @@ public class DoubleMatrix{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Determines whether the given DoubleMatrix is equal to the current matrix.
|
||||
*
|
||||
* @param rightMatrix The DoubleMatrix to compare to the current matrix.
|
||||
* @return True if the matrices are equal, false otherwise.
|
||||
*/
|
||||
private boolean equals(DoubleMatrix rightMatrix){
|
||||
if(rightMatrix == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
//Make sure they have the same number of elements
|
||||
if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){
|
||||
return false;
|
||||
@@ -804,22 +1161,46 @@ public class DoubleMatrix{
|
||||
//If false hasn't been returned yet then they are equal
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Determines whether the given object is approximately equal to the current matrix, with delta as the allowed difference to be "equal".
|
||||
*
|
||||
* @param rightSide The object to compare to the current matrix.
|
||||
* @param delta The allowed difference for equality.
|
||||
* @return True if the objects are approximately equal, false otherwise.
|
||||
* @see #setEqualsDelta(double)
|
||||
* @see #equals(Object)
|
||||
*/
|
||||
public boolean equals(Object rightSide, double delta){
|
||||
setEqualsDelta(delta);
|
||||
return equals(rightSide);
|
||||
}
|
||||
/**
|
||||
* Set the difference that is allowed for equality.
|
||||
*
|
||||
* @param delta The allowed difference for equality.
|
||||
*/
|
||||
public void setEqualsDelta(double delta){
|
||||
this.delta = Math.abs(delta);
|
||||
}
|
||||
/**
|
||||
* Calculates a hash code for the current matrix.
|
||||
*
|
||||
* @return The hash code for the current matrix.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return Arrays.hashCode(grid);
|
||||
}
|
||||
/**
|
||||
* Returns a string representation of the matrix, with rows and columns formatted for readability.
|
||||
*
|
||||
* @return A string representation of the matrix.
|
||||
*/
|
||||
@Override
|
||||
public String toString(){
|
||||
StringJoiner matrix = new StringJoiner("\n");
|
||||
StringJoiner matrix = new StringJoiner("\n").setEmptyValue("[]");
|
||||
for(int rowCnt = 0;rowCnt < getNumRows();++rowCnt){
|
||||
StringJoiner row = new StringJoiner(",", "[", "]");
|
||||
StringJoiner row = new StringJoiner(", ", "[", "]");
|
||||
|
||||
for(int colCnt = 0;colCnt < getNumCols();++colCnt){
|
||||
row.add(Double.toString(grid[rowCnt][colCnt]));
|
||||
|
||||
Reference in New Issue
Block a user