Update tests
Add javadoc comments
This commit is contained in:
2024-08-11 18:46:38 -04:00
parent 10ee93ae49
commit 2162130cd3
22 changed files with 13147 additions and 8848 deletions

View File

@@ -27,7 +27,7 @@
<groupId>com.mattrixwv</groupId>
<artifactId>matrix</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>1.2.0</version>
<name>Matrix</name>
<description>A library for performing Matrix operations</description>
@@ -252,7 +252,6 @@
</goals>
<configuration>
<keyname>${gpg.keyname}</keyname>
<passphraseServerId>${gpg.keyname}</passphraseServerId>
</configuration>
</execution>
</executions>

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java
//Mattrixwv
// Created: 02-10-22
//Modified: 06-30-23
//Modified: 08-11-24
package com.mattrixwv.matrix;
@@ -16,10 +16,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
/**
* Represents a matrix of big integers and provides various matrix operations.
*/
public class BigIntegerMatrix{
/**
* The grid that represents the matrix
*/
protected BigInteger[][] grid;
//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(BigInteger[][] grid){
if(grid.length == 0){
this.grid = new BigInteger[0][0];
@@ -46,18 +59,32 @@ public class BigIntegerMatrix{
this.grid = newGrid;
}
}
/**
* Creates a deep copy of the matrix grid.
*
* @return A new BigIntegerMatrix instance containing the copied grid.
*/
protected BigInteger[][] copyGrid(){
if(getNumCols() == 0){
return new BigInteger[grid.length][0];
}
//Allocate memory for the new grid
BigInteger[][] newGrid = new BigInteger[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 +93,16 @@ public class BigIntegerMatrix{
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 BigIntegerMatrix 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 BigIntegerMatrix laplaceExpansionHelper(int row, int col){
//Make sure the matrix is large enough to have this operation performed
if((getNumRows() <= 1) || (getNumCols() <= 1)){
@@ -110,16 +146,37 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newGrid);
}
//Constructors
//?Constructors
/**
* Constructs an empty matrix (0x0).
*/
public BigIntegerMatrix(){
grid = new BigInteger[0][0];
}
/**
* Constructs a matrix with the specified grid.
*
* @param grid The 2D array to initialize the matrix with.
*/
public BigIntegerMatrix(BigInteger[][] grid){
setGrid(grid);
}
/**
* Constructs a copy of the specified matrix.
*
* @param matrix The matrix to copy.
*/
public BigIntegerMatrix(BigIntegerMatrix matrix){
setGrid(matrix.grid);
}
/**
* 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 BigIntegerMatrix(int rows, int cols, BigInteger fill){
if(rows <= 0){
throw new InvalidGeometryException("A filled matrix must have at least 1 row");
@@ -137,7 +194,15 @@ public class BigIntegerMatrix{
}
}
//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 BigInteger get(int row, int col){
//Make sure the row and column are valid
if(row >= grid.length){
@@ -146,7 +211,7 @@ public class BigIntegerMatrix{
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){
@@ -156,6 +221,13 @@ public class BigIntegerMatrix{
//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 BigIntegerMatrix instance containing the specified row.
* @throws InvalidCoordinatesException If the row index is out of bounds.
*/
public BigIntegerMatrix getRow(int row){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -169,12 +241,24 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(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 BigIntegerMatrix instance containing the specified column.
* @throws InvalidCoordinatesException If the column index is out of bounds.
*/
public BigIntegerMatrix 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");
}
@@ -187,6 +271,11 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(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;
@@ -196,7 +285,15 @@ public class BigIntegerMatrix{
}
}
//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, BigInteger value){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -210,6 +307,14 @@ public class BigIntegerMatrix{
//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, BigInteger[] elements){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -226,6 +331,14 @@ public class BigIntegerMatrix{
//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, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -239,6 +352,14 @@ public class BigIntegerMatrix{
//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, BigInteger[] elements){
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
@@ -249,7 +370,7 @@ public class BigIntegerMatrix{
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
@@ -257,6 +378,14 @@ public class BigIntegerMatrix{
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, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -276,7 +405,14 @@ public class BigIntegerMatrix{
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(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -298,6 +434,13 @@ public class BigIntegerMatrix{
//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(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -311,6 +454,13 @@ public class BigIntegerMatrix{
//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(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -334,6 +484,13 @@ public class BigIntegerMatrix{
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(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -352,6 +509,14 @@ public class BigIntegerMatrix{
//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 BigIntegerMatrix 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 BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
@@ -377,26 +542,34 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){
/**
* Appends the specified matrix to the bottom of the current matrix.
*
* @param bottomSide The matrix to append to the bottom.
* @return A new BigIntegerMatrix 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 BigIntegerMatrix appendBottom(BigIntegerMatrix 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
BigInteger[][] newGrid = new BigInteger[getNumRows() + rightSide.getNumRows()][getNumCols()];
BigInteger[][] newGrid = new BigInteger[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];
}
}
@@ -404,7 +577,14 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newGrid);
}
//Simple operations
//?Simple operations
/**
* Generates an identity matrix of the given size.
*
* @param size The size of the identity matrix.
* @return A new BigIntegerMatrix instance representing the identity matrix.
* @throws InvalidGeometryException If the size is less than or equal to zero.
*/
public static BigIntegerMatrix generateIdentity(int size){
//Make sure the size is valid
if(size > 0){
@@ -430,8 +610,20 @@ public class BigIntegerMatrix{
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 BigIntegerMatrix instance with the result of the addition.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public BigIntegerMatrix add(BigIntegerMatrix 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());
}
@@ -439,7 +631,7 @@ public class BigIntegerMatrix{
//Create a new grid with the same elements as the current grid
BigInteger[][] 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] = newGrid[row][col].add(rightSide.grid[row][col]);
@@ -449,6 +641,12 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Adds the scalar to every element in the matrix.
*
* @param scalar The scalar to add.
* @return A new BigIntegerMatrix instance with the result of the addition.
*/
public BigIntegerMatrix add(BigInteger scalar){
//Create a new grid with the same elements as the current grid
BigInteger[][] newGrid = copyGrid();
@@ -463,8 +661,20 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Subtracts the specified matrix from the current matrix.
*
* @param rightSide The matrix to subtract.
* @return A new BigIntegerMatrix instance with the result of the subtraction.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public BigIntegerMatrix subtract(BigIntegerMatrix 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());
}
@@ -472,16 +682,22 @@ public class BigIntegerMatrix{
//Create a new grid with the same elements as the current gird
BigInteger[][] 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] = newGrid[row][col].subtract(grid[row][col]);
newGrid[row][col] = newGrid[row][col].subtract(rightSide.grid[row][col]);
}
}
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Subtracts the scalar from every element in the matrix.
*
* @param scalar The scalar to subtract.
* @return A new BigIntegerMatrix instance with the result of the subtraction.
*/
public BigIntegerMatrix subtract(BigInteger scalar){
//Create a new grid with the same elements as the current grid
BigInteger[][] newGrid = copyGrid();
@@ -496,8 +712,20 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Multiplies the current matrix by the specified matrix.
*
* @param rightSide The matrix to multiply by.
* @return A new BigIntegerMatrix 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 BigIntegerMatrix multiply(BigIntegerMatrix 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());
}
@@ -520,6 +748,12 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Multiplies every element in the matrix by the scalar.
*
* @param scalar the scalar to multiply
* @return A new BigIntegerMatrix instance with the result of the multiplication
*/
public BigIntegerMatrix multiply(BigInteger scalar){
//Create a new grid with the same elements as the current grid
BigInteger[][] newGrid = copyGrid();
@@ -534,6 +768,14 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(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 BigIntegerMatrix instance with the result of the multiplication
* @throws InvalidScalarException If the power is negative
* @throws InvalidGeometryException If the matrix is not square
*/
public BigIntegerMatrix pow(int power){
//Make sure the matrix is square so it can be multiplied
if(!isSquare()){
@@ -557,8 +799,21 @@ public class BigIntegerMatrix{
//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 BigInteger dotProduct(BigIntegerMatrix 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());
}
@@ -577,8 +832,21 @@ public class BigIntegerMatrix{
//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 BigIntegerMatrix hadamardProduct(BigIntegerMatrix 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());
}
@@ -597,7 +865,12 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newGrid);
}
//Complex operations
//?Complex operations
/**
* Transposes the current matrix (i.e., swaps rows and columns).
*
* @return A new BigIntegerMatrix instance representing the transposed matrix.
*/
public BigIntegerMatrix transpose(){
//Create a new grid
BigInteger[][] newGrid = new BigInteger[getNumCols()][getNumRows()];
@@ -612,16 +885,41 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(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 BigInteger 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 BigInteger det2(){
return (grid[0][0].multiply(grid[1][1])).subtract(grid[0][1].multiply(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 BigInteger det3(){
return grid[0][0].multiply(grid[1][1]).multiply(grid[2][2]).add(grid[0][1].multiply(grid[1][2]).multiply(grid[2][0])).add(grid[0][2].multiply(grid[1][0]).multiply(grid[2][1])).subtract
(grid[2][0].multiply(grid[1][1]).multiply(grid[0][2])).subtract(grid[2][1].multiply(grid[1][2]).multiply(grid[0][0])).subtract(grid[2][2].multiply(grid[1][0]).multiply(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 BigInteger det4(){
BigInteger det = BigInteger.ZERO;
@@ -691,6 +989,12 @@ public class BigIntegerMatrix{
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 BigInteger determinant(){
//Make sure the matrix is square
if(!isSquare()){
@@ -714,9 +1018,22 @@ public class BigIntegerMatrix{
//Return the determinant
return det;
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new BigIntegerMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #cofactor()
*/
public BigIntegerMatrix cof(){
return cofactor();
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new BigIntegerMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public BigIntegerMatrix cofactor(){
//Make sure the matrix is square
if(!isSquare()){
@@ -743,12 +1060,31 @@ public class BigIntegerMatrix{
//Return the new matrix
return new BigIntegerMatrix(newGrid);
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new BigIntegerMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #adjoint()
*/
public BigIntegerMatrix adj(){
return adjoint();
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new BigIntegerMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public BigIntegerMatrix adjoint(){
return cofactor().transpose();
}
/**
* Calculates the inverse of the current matrix.
*
* @return A new BigIntegerMatrix instance representing the inverse matrix.
* @throws InvalidGeometryException If the matrix is not square or if the determinant is 0.
*/
public BigIntegerMatrix inverse(){
//Make sure the matrix is square
if(!isSquare()){
@@ -765,7 +1101,15 @@ public class BigIntegerMatrix{
return adjoint().multiply(BigInteger.ONE.divide(determinant));
}
//Object funtions
//?Object functions
/**
* Determines whether the given object is equal to the current matrix.
* Can determine equality using BigIntegerMatrix or BigInteger[][].
*
* @param rightSide The object to compare to the current matrix.
* @return True if the objects are equal, false otherwise.
* @see #equals(BigIntegerMatrix)
*/
@Override
public boolean equals(Object rightSide){
if(rightSide == null){
@@ -782,7 +1126,17 @@ public class BigIntegerMatrix{
return false;
}
}
/**
* Determines whether the given BigIntegerMatrix is equal to the current matrix.
*
* @param rightMatrix The BigIntegerMatrix to compare to the current matrix.
* @return True if the matrices are equal, false otherwise.
*/
private boolean equals(BigIntegerMatrix rightMatrix){
if(rightMatrix == null){
return false;
}
//Make sure they have the same number of elements
if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){
return false;
@@ -800,15 +1154,25 @@ public class BigIntegerMatrix{
//If false hasn't been returned yet then they are equal
return true;
}
/**
* 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(grid[rowCnt][colCnt].toString());

View File

@@ -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]));

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
//Mattrixwv
// Created: 02-01-22
//Modified: 06-30-23
//Modified: 08-11-24
package com.mattrixwv.matrix;
@@ -15,10 +15,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
/**
* Represents a matrix of integers and provides various matrix operations.
*/
public class IntegerMatrix{
/**
* The grid that represents the matrix
*/
protected int[][] grid;
//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(int[][] grid){
if(grid.length == 0){
this.grid = new int[0][0];
@@ -45,18 +58,32 @@ public class IntegerMatrix{
this.grid = newGrid;
}
}
/**
* Creates a deep copy of the matrix grid.
*
* @return A new IntegerMatrix instance containing the copied grid.
*/
protected int[][] copyGrid(){
if(getNumCols() == 0){
return new int[grid.length][0];
}
//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){
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;
@@ -65,7 +92,16 @@ public class IntegerMatrix{
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 IntegerMatrix 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 IntegerMatrix laplaceExpansionHelper(int row, int col){
//Make sure the matrix is large enough to have this operation performed
if((getNumRows() <= 1) || (getNumCols() <= 1)){
@@ -109,16 +145,37 @@ public class IntegerMatrix{
return new IntegerMatrix(newGrid);
}
//Constructors
//?Constructors
/**
* Constructs an empty matrix (0x0).
*/
public IntegerMatrix(){
grid = new int[0][0];
}
/**
* Constructs a matrix with the specified grid.
*
* @param grid The 2D array to initialize the matrix with.
*/
public IntegerMatrix(int[][] grid){
setGrid(grid);
}
/**
* Constructs a copy of the specified matrix.
*
* @param matrix The matrix to copy.
*/
public IntegerMatrix(IntegerMatrix matrix){
setGrid(matrix.grid);
}
/**
* 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 IntegerMatrix(int rows, int cols, int fill){
if(rows <= 0){
throw new InvalidGeometryException("A filled matrix must have at least 1 row");
@@ -136,7 +193,15 @@ public class IntegerMatrix{
}
}
//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 int get(int row, int col){
//Make sure the row and column are valid
if(row >= grid.length){
@@ -145,7 +210,7 @@ public class IntegerMatrix{
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){
@@ -155,6 +220,13 @@ public class IntegerMatrix{
//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 IntegerMatrix instance containing the specified row.
* @throws InvalidCoordinatesException If the row index is out of bounds.
*/
public IntegerMatrix getRow(int row){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -168,12 +240,24 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(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 IntegerMatrix instance containing the specified column.
* @throws InvalidCoordinatesException If the column index is out of bounds.
*/
public IntegerMatrix 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");
}
@@ -186,6 +270,11 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(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;
@@ -195,7 +284,15 @@ public class IntegerMatrix{
}
}
//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, int value){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -209,6 +306,14 @@ public class IntegerMatrix{
//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, int[] elements){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -225,6 +330,14 @@ public class IntegerMatrix{
//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, IntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -238,6 +351,14 @@ public class IntegerMatrix{
//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, int[] elements){
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
@@ -248,7 +369,7 @@ public class IntegerMatrix{
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
@@ -256,6 +377,14 @@ public class IntegerMatrix{
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, IntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -275,7 +404,14 @@ public class IntegerMatrix{
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(int[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -297,6 +433,13 @@ public class IntegerMatrix{
//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(IntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -310,6 +453,13 @@ public class IntegerMatrix{
//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(int[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -333,8 +483,15 @@ public class IntegerMatrix{
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(IntegerMatrix matrix){
//Make sure teh matrix isn't null
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
@@ -351,6 +508,14 @@ public class IntegerMatrix{
//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 IntegerMatrix 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 IntegerMatrix appendRight(IntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
@@ -376,26 +541,34 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix appendBottom(IntegerMatrix rightSide){
/**
* Appends the specified matrix to the bottom of the current matrix.
*
* @param bottomSide The matrix to append to the bottom.
* @return A new IntegerMatrix 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 IntegerMatrix appendBottom(IntegerMatrix 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
int[][] newGrid = new int[getNumRows() + rightSide.getNumRows()][getNumCols()];
int[][] newGrid = new int[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];
}
}
@@ -403,7 +576,14 @@ public class IntegerMatrix{
return new IntegerMatrix(newGrid);
}
//Simple operations
//?Simple operations
/**
* Generates an identity matrix of the given size.
*
* @param size The size of the identity matrix.
* @return A new IntegerMatrix instance representing the identity matrix.
* @throws InvalidGeometryException If the size is less than or equal to zero.
*/
public static IntegerMatrix generateIdentity(int size){
//Make sure the size is valid
if(size > 0){
@@ -429,8 +609,20 @@ public class IntegerMatrix{
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 IntegerMatrix instance with the result of the addition.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public IntegerMatrix add(IntegerMatrix 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());
}
@@ -438,7 +630,7 @@ public class IntegerMatrix{
//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
//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];
@@ -448,6 +640,12 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
/**
* Adds the scalar to every element in the matrix.
*
* @param scalar The scalar to add.
* @return A new IntegerMatrix instance with the result of the addition.
*/
public IntegerMatrix add(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
@@ -462,25 +660,43 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
/**
* Subtracts the specified matrix from the current matrix.
*
* @param rightSide The matrix to subtract.
* @return A new IntegerMatrix instance with the result of the subtraction.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public IntegerMatrix subtract(IntegerMatrix 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());
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 gird
int[][] 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 IntegerMatrix(newGrid);
}
/**
* Subtracts the scalar from every element in the matrix.
*
* @param scalar The scalar to subtract.
* @return A new IntegerMatrix instance with the result of the subtraction.
*/
public IntegerMatrix subtract(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
@@ -495,8 +711,20 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
/**
* Multiplies the current matrix by the specified matrix.
*
* @param rightSide The matrix to multiply by.
* @return A new IntegerMatrix 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 IntegerMatrix multiply(IntegerMatrix 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());
}
@@ -519,6 +747,12 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
/**
* Multiplies every element in the matrix by the scalar.
*
* @param scalar the scalar to multiply
* @return A new IntegerMatrix instance with the result of the multiplication
*/
public IntegerMatrix multiply(int scalar){
//Create a new grid with the same elements as the current grid
int[][] newGrid = copyGrid();
@@ -533,6 +767,14 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(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 IntegerMatrix instance with the result of the multiplication
* @throws InvalidScalarException If the power is negative
* @throws InvalidGeometryException If the matrix is not square
*/
public IntegerMatrix pow(int power){
//Make sure the matrix is square so it can be multiplied
if(!isSquare()){
@@ -556,8 +798,21 @@ public class IntegerMatrix{
//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 int dotProduct(IntegerMatrix 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());
}
@@ -576,8 +831,21 @@ public class IntegerMatrix{
//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 IntegerMatrix hadamardProduct(IntegerMatrix 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());
}
@@ -596,7 +864,12 @@ public class IntegerMatrix{
return new IntegerMatrix(newGrid);
}
//Complex operations
//?Complex operations
/**
* Transposes the current matrix (i.e., swaps rows and columns).
*
* @return A new IntegerMatrix instance representing the transposed matrix.
*/
public IntegerMatrix transpose(){
//Create a new grid
int[][] newGrid = new int[getNumCols()][getNumRows()];
@@ -611,16 +884,41 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(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 int 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 int 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 int 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 int det4(){
int det = 0;
@@ -690,6 +988,12 @@ public class IntegerMatrix{
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 int determinant(){
//Make sure the matrix is square
if(!isSquare()){
@@ -713,9 +1017,22 @@ public class IntegerMatrix{
//Return the determinant
return det;
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new IntegerMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #cofactor()
*/
public IntegerMatrix cof(){
return cofactor();
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new IntegerMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public IntegerMatrix cofactor(){
//Make sure the matrix is square
if(!isSquare()){
@@ -742,12 +1059,31 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new IntegerMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #adjoint()
*/
public IntegerMatrix adj(){
return adjoint();
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new IntegerMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public IntegerMatrix adjoint(){
return cofactor().transpose();
}
/**
* Calculates the inverse of the current matrix.
*
* @return A new IntegerMatrix instance representing the inverse matrix.
* @throws InvalidGeometryException If the matrix is not square or if the determinant is 0.
*/
public IntegerMatrix inverse(){
//Make sure the matrix is square
if(!isSquare()){
@@ -764,7 +1100,15 @@ public class IntegerMatrix{
return adjoint().multiply(1 / determinant);
}
//Object funtions
//?Object functions
/**
* Determines whether the given object is equal to the current matrix.
* Can determine equality using IntegerMatrix or int[][].
*
* @param rightSide The object to compare to the current matrix.
* @return True if the objects are equal, false otherwise.
* @see #equals(IntegerMatrix)
*/
@Override
public boolean equals(Object rightSide){
if(rightSide == null){
@@ -781,6 +1125,12 @@ public class IntegerMatrix{
return false;
}
}
/**
* Determines whether the given IntegerMatrix is equal to the current matrix.
*
* @param rightMatrix The IntegerMatrix to compare to the current matrix.
* @return True if the matrices are equal, false otherwise.
*/
private boolean equals(IntegerMatrix rightMatrix){
//Make sure they have the same number of elements
if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){
@@ -799,15 +1149,25 @@ public class IntegerMatrix{
//If false hasn't been returned yet then they are equal
return true;
}
/**
* 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(Integer.toString(grid[rowCnt][colCnt]));

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java
//Mattrixwv
// Created: 02-10-22
//Modified: 06-30-23
//Modified: 08-11-24
package com.mattrixwv.matrix;
@@ -15,10 +15,23 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
/**
* Represents a matrix of long integers and provides various matrix operations.
*/
public class LongMatrix{
/**
* The grid that represents the matrix
*/
protected long[][] grid;
//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(long[][] grid){
if(grid.length == 0){
this.grid = new long[0][0];
@@ -45,18 +58,32 @@ public class LongMatrix{
this.grid = newGrid;
}
}
/**
* Creates a deep copy of the matrix grid.
*
* @return A new LongMatrix instance containing the copied grid.
*/
protected long[][] copyGrid(){
if(getNumCols() == 0){
return new long[grid.length][0];
}
//Allocate memory for the new grid
long[][] newGrid = new long[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;
@@ -65,7 +92,16 @@ public class LongMatrix{
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 LongMatrix 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 LongMatrix laplaceExpansionHelper(int row, int col){
//Make sure the matrix is large enough to have this operation performed
if((getNumRows() <= 1) || (getNumCols() <= 1)){
@@ -109,16 +145,37 @@ public class LongMatrix{
return new LongMatrix(newGrid);
}
//Constructors
//?Constructors
/**
* Constructs an empty matrix (0x0).
*/
public LongMatrix(){
grid = new long[0][0];
}
/**
* Constructs a matrix with the specified grid.
*
* @param grid The 2D array to initialize the matrix with.
*/
public LongMatrix(long[][] grid){
setGrid(grid);
}
/**
* Constructs a copy of the specified matrix.
*
* @param matrix The matrix to copy.
*/
public LongMatrix(LongMatrix matrix){
setGrid(matrix.grid);
}
/**
* 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 LongMatrix(int rows, int cols, long fill){
if(rows <= 0){
throw new InvalidGeometryException("A filled matrix must have at least 1 row");
@@ -136,7 +193,15 @@ public class LongMatrix{
}
}
//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 long get(int row, int col){
//Make sure the row and column are valid
if(row >= grid.length){
@@ -145,7 +210,7 @@ public class LongMatrix{
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){
@@ -155,6 +220,13 @@ public class LongMatrix{
//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 LongMatrix instance containing the specified row.
* @throws InvalidCoordinatesException If the row index is out of bounds.
*/
public LongMatrix getRow(int row){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -168,12 +240,24 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(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 LongMatrix instance containing the specified column.
* @throws InvalidCoordinatesException If the column index is out of bounds.
*/
public LongMatrix 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");
}
@@ -186,6 +270,11 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(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;
@@ -195,7 +284,15 @@ public class LongMatrix{
}
}
//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, long value){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -209,6 +306,14 @@ public class LongMatrix{
//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, long[] elements){
//Make sure the row number is valid
if((row < 0) || (row >= grid.length)){
@@ -225,6 +330,14 @@ public class LongMatrix{
//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, LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -238,6 +351,14 @@ public class LongMatrix{
//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, long[] elements){
//Make sure the column number is valid
if((col < 0) || (col >= getNumCols())){
@@ -248,7 +369,7 @@ public class LongMatrix{
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
@@ -256,6 +377,14 @@ public class LongMatrix{
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, LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -275,7 +404,14 @@ public class LongMatrix{
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(long[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -297,6 +433,13 @@ public class LongMatrix{
//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(LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -310,6 +453,13 @@ public class LongMatrix{
//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(long[] elements){
//Make sure the matrix isn't null
if(elements == null){
@@ -333,6 +483,13 @@ public class LongMatrix{
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(LongMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
@@ -351,6 +508,14 @@ public class LongMatrix{
//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 LongMatrix 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 LongMatrix appendRight(LongMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
@@ -376,26 +541,34 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
public LongMatrix appendBottom(LongMatrix rightSide){
/**
* Appends the specified matrix to the bottom of the current matrix.
*
* @param bottomSide The matrix to append to the bottom.
* @return A new LongMatrix 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 LongMatrix appendBottom(LongMatrix 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
long[][] newGrid = new long[getNumRows() + rightSide.getNumRows()][getNumCols()];
long[][] newGrid = new long[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];
}
}
@@ -403,7 +576,14 @@ public class LongMatrix{
return new LongMatrix(newGrid);
}
//Simple operations
//?Simple operations
/**
* Generates an identity matrix of the given size.
*
* @param size The size of the identity matrix.
* @return A new LongMatrix instance representing the identity matrix.
* @throws InvalidGeometryException If the size is less than or equal to zero.
*/
public static LongMatrix generateIdentity(int size){
//Make sure the size is valid
if(size > 0){
@@ -429,8 +609,20 @@ public class LongMatrix{
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 LongMatrix instance with the result of the addition.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public LongMatrix add(LongMatrix 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());
}
@@ -438,7 +630,7 @@ public class LongMatrix{
//Create a new grid with the same elements as the current grid
long[][] 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];
@@ -448,6 +640,12 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
/**
* Adds the scalar to every element in the matrix.
*
* @param scalar The scalar to add.
* @return A new LongMatrix instance with the result of the addition.
*/
public LongMatrix add(long scalar){
//Create a new grid with the same elements as the current grid
long[][] newGrid = copyGrid();
@@ -462,8 +660,20 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
/**
* Subtracts the specified matrix from the current matrix.
*
* @param rightSide The matrix to subtract.
* @return A new LongMatrix instance with the result of the subtraction.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public LongMatrix subtract(LongMatrix 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());
}
@@ -471,16 +681,22 @@ public class LongMatrix{
//Create a new grid with the same elements as the current gird
long[][] 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 LongMatrix(newGrid);
}
/**
* Subtracts the scalar from every element in the matrix.
*
* @param scalar The scalar to subtract.
* @return A new LongMatrix instance with the result of the subtraction.
*/
public LongMatrix subtract(long scalar){
//Create a new grid with the same elements as the current grid
long[][] newGrid = copyGrid();
@@ -495,8 +711,20 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
/**
* Multiplies the current matrix by the specified matrix.
*
* @param rightSide The matrix to multiply by.
* @return A new LongMatrix 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 LongMatrix multiply(LongMatrix 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());
}
@@ -519,6 +747,12 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
/**
* Multiplies every element in the matrix by the scalar.
*
* @param scalar the scalar to multiply
* @return A new LongMatrix instance with the result of the multiplication
*/
public LongMatrix multiply(long scalar){
//Create a new grid with the same elements as the current grid
long[][] newGrid = copyGrid();
@@ -533,6 +767,14 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(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 LongMatrix instance with the result of the multiplication
* @throws InvalidScalarException If the power is negative
* @throws InvalidGeometryException If the matrix is not square
*/
public LongMatrix pow(long power){
//Make sure the matrix is square so it can be multiplied
if(!isSquare()){
@@ -556,8 +798,21 @@ public class LongMatrix{
//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 long dotProduct(LongMatrix 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());
}
@@ -576,8 +831,21 @@ public class LongMatrix{
//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 LongMatrix hadamardProduct(LongMatrix 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());
}
@@ -596,7 +864,12 @@ public class LongMatrix{
return new LongMatrix(newGrid);
}
//Complex operations
//?Complex operations
/**
* Transposes the current matrix (i.e., swaps rows and columns).
*
* @return A new LongMatrix instance representing the transposed matrix.
*/
public LongMatrix transpose(){
//Create a new grid
long[][] newGrid = new long[getNumCols()][getNumRows()];
@@ -611,16 +884,41 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(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 long det(){
return determinant();
}
/**
* Calculates the determinant of a 2x2 matrix.
*
* @return The determinant of the matrix.
* @throws InvalidGeometryException If the matrix is not 2x2.
*/
public long 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.
*/
public long 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.
*/
public long det4(){
long det = 0;
@@ -690,6 +988,12 @@ public class LongMatrix{
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 long determinant(){
//Make sure the matrix is square
if(!isSquare()){
@@ -713,9 +1017,22 @@ public class LongMatrix{
//Return the determinant
return det;
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new LongMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #cofactor()
*/
public LongMatrix cof(){
return cofactor();
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new LongMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public LongMatrix cofactor(){
//Make sure the matrix is square
if(!isSquare()){
@@ -742,12 +1059,31 @@ public class LongMatrix{
//Return the new matrix
return new LongMatrix(newGrid);
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new LongMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #adjoint()
*/
public LongMatrix adj(){
return adjoint();
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new LongMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
public LongMatrix adjoint(){
return cofactor().transpose();
}
/**
* Calculates the inverse of the current matrix.
*
* @return A new LongMatrix instance representing the inverse matrix.
* @throws InvalidGeometryException If the matrix is not square or if the determinant is 0.
*/
public LongMatrix inverse(){
//Make sure the matrix is square
if(!isSquare()){
@@ -764,7 +1100,15 @@ public class LongMatrix{
return adjoint().multiply(1 / determinant);
}
//Object funtions
//?Object functions
/**
* Determines whether the given object is equal to the current matrix.
* Can determine equality using LongMatrix or long[][].
*
* @param rightSide The object to compare to the current matrix.
* @return True if the objects are equal, false otherwise.
* @see #equals(LongMatrix)
*/
@Override
public boolean equals(Object rightSide){
if(rightSide == null){
@@ -781,7 +1125,17 @@ public class LongMatrix{
return false;
}
}
/**
* Determines whether the given LongMatrix is equal to the current matrix.
*
* @param rightMatrix The LongMatrix to compare to the current matrix.
* @return True if the matrices are equal, false otherwise.
*/
private boolean equals(LongMatrix rightMatrix){
if(rightMatrix == null){
return false;
}
//Make sure they have the same number of elements
if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){
return false;
@@ -799,15 +1153,25 @@ public class LongMatrix{
//If false hasn't been returned yet then they are equal
return true;
}
/**
* 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(Long.toString(grid[rowCnt][colCnt]));

View File

@@ -1,20 +1,35 @@
//Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java
//Mattrixwv
// Created: 02-09-22
//Modified: 07-01-22
//Modified: 08-11-24
package com.mattrixwv.matrix;
import java.util.Arrays;
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;
/**
* Represents a matrix of integers that have been run through a modulus function, and provides various matrix operations.
*/
public class ModMatrix extends IntegerMatrix{
/**
* The mod used on each element of the matrix
*/
protected int mod;
//Helper functions
//?Helper functions
/**
* Set the mod values
*
* @param mod The new mod value
* @throws InvalidScalarException If the mod value is less than or equal to 0
*/
protected void setMod(int mod){
if(mod <= 0){
throw new InvalidScalarException("The mod must be > 0");
@@ -22,6 +37,12 @@ public class ModMatrix extends IntegerMatrix{
this.mod = mod;
}
/**
* Get the mod value of a number
*
* @param value The value to mod
* @return The modded value
*/
protected int modValue(int value){
int newValue = value % mod;
@@ -31,9 +52,16 @@ public class ModMatrix extends IntegerMatrix{
return newValue;
}
/**
* Get the mod value of each element in an array
*
* @param values The array of values
* @return The array of modded values
* @throws NullMatrixException If the array is null
*/
protected int[] modValues(int[] values){
if(values == null){
throw new InvalidGeometryException("Array cannot be null");
throw new NullMatrixException("Array cannot be null");
}
int[] newValues = new int[values.length];
@@ -44,6 +72,9 @@ public class ModMatrix extends IntegerMatrix{
return newValues;
}
/**
* Get the mod value of each element in the matrix
*/
protected void modGrid(){
for(int row = 0;row < getNumRows();++row){
for(int col = 0;col < getNumCols();++col){
@@ -51,161 +82,440 @@ public class ModMatrix extends IntegerMatrix{
}
}
}
/**
* 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.
*/
@Override
protected void setGrid(int[][] grid){
super.setGrid(grid);
modGrid();
}
//Constructors
//?Constructors
/**
* Constructs an empty matrix (0x0).
*
* @param mod The mod value to use.
*/
public ModMatrix(int mod){
super();
setMod(mod);
modGrid();
}
/**
* Constructs a matrix with the specified grid.
*
* @param grid The 2D array to initialize the matrix with.
* @param mod The mod value to use.
*/
public ModMatrix(int[][] grid, int mod){
super();
setMod(mod);
setGrid(grid);
}
/**
* Constructs a copy of the specified matrix.
*
* @param matrix The matrix to copy.
*/
public ModMatrix(ModMatrix matrix){
super();
setMod(matrix.mod);
setGrid(matrix.grid);
}
/**
* Constructs a ModMatrix based on the given IntegerMatrix
*
* @param matrix The matrix to copy.
* @param mod The mod value to use.
*/
public ModMatrix(IntegerMatrix matrix, int mod){
super();
setMod(mod);
setGrid(matrix.grid);
}
/**
* 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.
* @param mod The mod value to use.
* @throws InvalidGeometryException If the number of rows or columns is less than or equal to zero.
*/
public ModMatrix(int rows, int cols, int fill, int mod){
super(rows, cols, fill);
setMod(mod);
modGrid();
}
//Gets
//?Gets
/**
* Get the mod value of the matrix
*
* @return The mod value
*/
public int getMod(){
return mod;
}
/**
* Returns a new matrix that is a copy of the specified row.
*
* @param row The index of the row to retrieve.
* @return A new ModMatrix instance containing the specified row.
* @throws InvalidCoordinatesException If the row index is out of bounds.
*/
@Override
public ModMatrix getRow(int row){
return new ModMatrix(super.getRow(row), mod);
}
/**
* Returns the number of rows in the matrix.
*
* @return The number of rows.
*/
@Override
public ModMatrix getCol(int col){
return new ModMatrix(super.getCol(col), mod);
}
//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.
*/
@Override
public void set(int row, int col, int value){
super.set(row, col, modValue(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.
*/
@Override
public void setRow(int row, int[] elements){
super.setRow(row, modValues(elements));
}
/**
* Sets the specified row with the given matrix containing a single row.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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.
*/
@Override
public void setRow(int row, IntegerMatrix matrix){
setRow(row, new ModMatrix(matrix, Integer.MAX_VALUE));
setRow(row, new ModMatrix(matrix, mod));
}
/**
* 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, ModMatrix matrix){
super.setRow(row, matrix);
modGrid();
}
/**
* 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.
*/
@Override
public void setCol(int col, int[] elements){
super.setCol(col, elements);
modGrid();
}
/**
* Sets the specified column with the given matrix containing a single column.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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.
*/
@Override
public void setCol(int col, IntegerMatrix matrix){
setCol(col, new ModMatrix(matrix, Integer.MAX_VALUE));
setCol(col, new ModMatrix(matrix, mod));
}
/**
* 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, ModMatrix matrix){
super.setCol(col, matrix);
modGrid();
}
//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.
*/
@Override
public void addRow(int[] elements){
super.addRow(modValues(elements));
}
/**
* Adds a new row with the given matrix containing a single row to the matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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.
*/
@Override
public void addRow(IntegerMatrix matrix){
addRow(new ModMatrix(matrix, Integer.MAX_VALUE));
addRow(new ModMatrix(matrix, mod));
}
/**
* 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(ModMatrix matrix){
super.addRow(matrix);
modGrid();
}
/**
* 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.
*/
@Override
public void addCol(int[] elements){
super.addCol(modValues(elements));
}
/**
* Adds a new column with the given matrix containing a single column to the matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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.
*/
@Override
public void addCol(IntegerMatrix matrix){
addCol(new ModMatrix(matrix, Integer.MAX_VALUE));
addCol(new ModMatrix(matrix, mod));
}
/**
* 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(ModMatrix matrix){
super.addCol(matrix);
modGrid();
}
/**
* Appends the specified matrix to the right side of the current matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @param rightSide The matrix to append to the right side.
* @return A new ModMatrix 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.
*/
@Override
public ModMatrix appendRight(IntegerMatrix rightSide){
return appendRight(new ModMatrix(rightSide, Integer.MAX_VALUE));
return appendRight(new ModMatrix(rightSide, mod));
}
/**
* 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 ModMatrix 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 ModMatrix appendRight(ModMatrix rightSide){
return new ModMatrix(super.appendRight(rightSide), mod);
}
/**
* Appends the specified matrix to the bottom of the current matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @param bottomSide The matrix to append to the bottom.
* @return A new ModMatrix instance with the bottom matrix appended.
* @throws NullMatrixException If the bottom matrix is null.
* @throws InvalidGeometryException If the number of columns does not match.
*/
@Override
public ModMatrix appendBottom(IntegerMatrix rightSide){
return appendBottom(new ModMatrix(rightSide, Integer.MAX_VALUE));
public ModMatrix appendBottom(IntegerMatrix bottomSide){
return appendBottom(new ModMatrix(bottomSide, mod));
}
public ModMatrix appendBottom(ModMatrix rightSide){
return new ModMatrix(super.appendBottom(rightSide), mod);
/**
* Appends the specified matrix to the bottom of the current matrix.
*
* @param bottomSide The matrix to append to the bottom.
* @return A new ModMatrix 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 ModMatrix appendBottom(ModMatrix bottomSide){
return new ModMatrix(super.appendBottom(bottomSide), mod);
}
//Simple operations
//?Simple operations
/**
* Generates an identity matrix of the given size.
*
* @param size The size of the identity matrix.
* @return A new ModMatrix instance representing the identity matrix.
* @throws InvalidGeometryException If the size is less than or equal to zero.
*/
public static ModMatrix generateIdentity(int size){
return generateIdentity(size, Integer.MAX_VALUE);
}
/**
* Generates an identity matrix of the given size.
*
* @param size The size of the identity matrix.
* @param mod The mod value to use.
* @return A new ModMatrix instance representing the identity matrix.
* @throws InvalidGeometryException If the size is less than or equal to zero.
*/
public static ModMatrix generateIdentity(int size, int mod){
return new ModMatrix(IntegerMatrix.generateIdentity(size), mod);
}
/**
* Adds the specified matrix to the current matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @param rightSide The matrix to add.
* @return A new ModMatrix instance with the result of the addition.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
@Override
public ModMatrix add(IntegerMatrix rightSide){
return add(new ModMatrix(rightSide, Integer.MAX_VALUE));
return add(new ModMatrix(rightSide, mod));
}
/**
* Adds the specified matrix to the current matrix.
*
* @param rightSide The matrix to add.
* @return A new ModMatrix instance with the result of the addition.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public ModMatrix add(ModMatrix rightSide){
return new ModMatrix(super.add(rightSide), mod);
}
/**
* Adds the scalar to every element in the matrix.
*
* @param scalar The scalar to add.
* @return A new ModMatrix instance with the result of the addition.
*/
@Override
public ModMatrix add(int scalar){
return new ModMatrix(super.add(scalar), mod);
}
/**
* Subtracts the specified matrix from the current matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @param rightSide The matrix to subtract.
* @return A new ModMatrix instance with the result of the subtraction.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
@Override
public ModMatrix subtract(IntegerMatrix rightSide){
return subtract(new ModMatrix(rightSide, Integer.MAX_VALUE));
return subtract(new ModMatrix(rightSide, mod));
}
/**
* Subtracts the specified matrix from the current matrix.
*
* @param rightSide The matrix to subtract.
* @return A new ModMatrix instance with the result of the subtraction.
* @throws InvalidGeometryException If the matrices do not have the same dimensions.
*/
public ModMatrix subtract(ModMatrix rightSide){
return new ModMatrix(super.subtract(rightSide), mod);
}
/**
* Subtracts the scalar from every element in the matrix.
*
* @param scalar The scalar to subtract.
* @return A new ModMatrix instance with the result of the subtraction.
*/
@Override
public ModMatrix subtract(int scalar){
return new ModMatrix(super.subtract(scalar), mod);
}
/**
* Multiplies the current matrix by the specified matrix.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @param rightSide The matrix to multiply by.
* @return A new ModMatrix 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.
*/
@Override
public ModMatrix multiply(IntegerMatrix matrix){
return multiply(new ModMatrix(matrix, Integer.MAX_VALUE));
public ModMatrix multiply(IntegerMatrix rightSide){
return multiply(new ModMatrix(rightSide, mod));
}
public ModMatrix multiply(ModMatrix matrix){
return new ModMatrix(super.multiply(matrix), mod);
/**
* Multiplies the current matrix by the specified matrix.
*
* @param rightSide The matrix to multiply by.
* @return A new ModMatrix 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 ModMatrix multiply(ModMatrix rightSide){
return new ModMatrix(super.multiply(rightSide), mod);
}
/**
* Multiplies every element in the matrix by the scalar.
*
* @param scalar the scalar to multiply
* @return A new ModMatrix instance with the result of the multiplication
*/
@Override
public ModMatrix multiply(int scalar){
return new ModMatrix(super.multiply(scalar), mod);
}
/**
* 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 ModMatrix instance with the result of the multiplication
* @throws InvalidScalarException If the power is negative
* @throws InvalidGeometryException If the matrix is not square
*/
@Override
public ModMatrix pow(int power){
//Make sure the matrix is square so it can be multiplied
@@ -230,30 +540,82 @@ public class ModMatrix extends IntegerMatrix{
//Return the new grid
return newMatrix;
}
/**
* Calculates the dot product of the two matrices.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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
*/
@Override
public int dotProduct(IntegerMatrix rightSide){
return dotProduct(new ModMatrix(rightSide, Integer.MAX_VALUE));
return dotProduct(new ModMatrix(rightSide, mod));
}
/**
* 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 int dotProduct(ModMatrix rightSide){
return super.dotProduct(rightSide);
return super.dotProduct(rightSide) % mod;
}
/**
* Calculates the Hadamard product of the two matrices.
* Converts the IntegerMatrix to a ModMatrix before the operation.
*
* @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
*/
@Override
public ModMatrix hadamardProduct(IntegerMatrix rightSide){
return hadamardProduct(new ModMatrix(rightSide, Integer.MAX_VALUE));
return hadamardProduct(new ModMatrix(rightSide, mod));
}
/**
* 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 ModMatrix hadamardProduct(ModMatrix rightSide){
return new ModMatrix(super.hadamardProduct(rightSide), mod);
}
//Complex operations
//?Complex operations
/**
* Transposes the current matrix (i.e., swaps rows and columns).
*
* @return A new ModMatrix instance representing the transposed matrix.
*/
@Override
public ModMatrix transpose(){
return new ModMatrix(super.transpose(), mod);
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new ModMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #cofactor()
*/
@Override
public ModMatrix cof(){
return cofactor();
}
/**
* Calculates the cofactor matrix of the current matrix.
*
* @return A new ModMatrix instance representing the cofactor matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
@Override
public ModMatrix cofactor(){
//Make sure the matrix is square
@@ -281,14 +643,33 @@ public class ModMatrix extends IntegerMatrix{
//Return the new matrix
return new ModMatrix(newGrid, mod);
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new ModMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
* @see #adjoint()
*/
@Override
public ModMatrix adj(){
return adjoint();
}
/**
* Calculates the adjoint matrix of the current matrix.
*
* @return A new ModMatrix instance representing the adjoint matrix.
* @throws InvalidGeometryException If the matrix is not square.
*/
@Override
public ModMatrix adjoint(){
return cofactor().transpose();
}
/**
* Calculates the inverse of the current matrix.
*
* @return A new ModMatrix instance representing the inverse matrix.
* @throws InvalidGeometryException If the matrix is not square or if the determinant is 0.
*/
@Override
public ModMatrix inverse(){
//Make sure the matrix is square
@@ -317,7 +698,15 @@ public class ModMatrix extends IntegerMatrix{
return adjoint().multiply(determinantInverse);
}
//Object functions
//?Object functions
/**
* Determines whether the given object is equal to the current matrix.
* Can determine equality using ModMatrix or int[][].
*
* @param rightSide The object to compare to the current matrix.
* @return True if the objects are equal, false otherwise.
* @see #equals(ModMatrix)
*/
@Override
public boolean equals(Object rightSide){
if(rightSide == null){
@@ -335,7 +724,17 @@ public class ModMatrix extends IntegerMatrix{
return false;
}
}
/**
* Determines whether the given ModMatrix is equal to the current matrix.
*
* @param rightMatrix The ModMatrix to compare to the current matrix.
* @return True if the matrices are equal, false otherwise.
*/
public boolean equals(ModMatrix rightMatrix){
if(rightMatrix == null){
return false;
}
//Make sure they have the same number of elements
if((getNumRows() != rightMatrix.getNumRows()) || (getNumCols() != rightMatrix.getNumCols())){
return false;
@@ -353,10 +752,20 @@ public class ModMatrix extends IntegerMatrix{
//If false hasn't been return yet then they are equal
return true;
}
/**
* 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(){
return super.toString() + "\nmod(" + mod + ")";

View File

@@ -1,25 +1,71 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidCoordinatesException.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-07-22
//Modified: 08-08-24
package com.mattrixwv.matrix.exceptions;
/**
* Exception thrown to indicate that a set of coordinates is invalid for a given matrix.
*
* @author Mattrixwv
*/
public class InvalidCoordinatesException extends RuntimeException{
/**
* Serialization identifier for this class.
*/
public static final long serialVersionUID = 1;
/**
* Constructs a new {@code InvalidCoordinatesException} with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
*/
public InvalidCoordinatesException(){
super();
}
/**
* Constructs a new {@code InvalidCoordinatesException} with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
*/
public InvalidCoordinatesException(String message){
super(message);
}
/**
* Constructs a new {@code InvalidCoordinatesException} with the specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message
* of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions.
*
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidCoordinatesException(Throwable throwable){
super(throwable);
}
/**
* Constructs a new {@code InvalidCoordinatesException} with the specified detail message and cause.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidCoordinatesException(String message, Throwable throwable){
super(message, throwable);
}
/**
* Constructs a new {@code InvalidCoordinatesException} with a default message indicating an invalid number
* of elements provided. The message is constructed using the given number of elements and the maximum
* allowed number of elements.
*
* @param givenElements the number of elements that were provided and deemed invalid.
* @param neededElements the maximum number of elements that was expected or allowed.
*/
public InvalidCoordinatesException(int givenElements, int neededElements){
super("Invalid number of elements " + givenElements + " must be at most " + neededElements);
}

View File

@@ -1,25 +1,71 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidGeometryException.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-07-22
//Modified: 08-08-24
package com.mattrixwv.matrix.exceptions;
/**
* Exception thrown to indicate that a geometry-related error has occurred.
*
* @author Mattrixwv
*/
public class InvalidGeometryException extends RuntimeException{
/**
* Serialization identifier for this class.
*/
public static final long serialVersionUID = 1;
/**
* Constructs a new {@code InvalidGeometryException} with {@code null} as its detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*/
public InvalidGeometryException(){
super();
}
/**
* Constructs a new {@code InvalidGeometryException} with the specified detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
*/
public InvalidGeometryException(String message){
super(message);
}
/**
* Constructs a new {@code InvalidGeometryException} with the specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message
* of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions.
*
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidGeometryException(Throwable throwable){
super(throwable);
}
/**
* Constructs a new {@code InvalidGeometryException} with the specified detail message and cause.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidGeometryException(String message, Throwable throwable){
super(message, throwable);
}
/**
* Constructs a new {@code InvalidGeometryException} with a default message indicating an invalid number
* of elements provided. The message is constructed using the given number of elements and the expected
* number of elements.
*
* @param givenElements the number of elements that were provided and deemed invalid.
* @param neededElements the expected or required number of elements.
*/
public InvalidGeometryException(int givenElements, int neededElements){
super("Invalid number of elements " + givenElements + " must be " + neededElements);
}

View File

@@ -1,22 +1,57 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/InvalidRowSizeException.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-07-22
//Modified: 08-08-24
package com.mattrixwv.matrix.exceptions;
/**
* Exception thrown to indicate that the size of a row is invalid for a given matrix.
*/
public class InvalidRowSizeException extends RuntimeException{
/**
* Serialization identifier for this class.
*/
public static final long serialVersionUID = 1;
/**
* Constructs a new {@code InvalidRowSizeException} with {@code null} as its detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*/
public InvalidRowSizeException(){
super();
}
/**
* Constructs a new {@code InvalidRowSizeException} with the specified detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
*/
public InvalidRowSizeException(String message){
super(message);
}
/**
* Constructs a new {@code InvalidRowSizeException} with the specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message
* of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions.
*
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidRowSizeException(Throwable throwable){
super(throwable);
}
/**
* Constructs a new {@code InvalidRowSizeException} with the specified detail message and cause.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidRowSizeException(String message, Throwable throwable){
super(message, throwable);
}

View File

@@ -1,22 +1,57 @@
//Matrix/src/main/java/com/mattrixwv/matrix/exceptions/InvalidScalarException.java
//Mattrixwv
// Created: 02-07-22
//Modified: 02-07-22
//Modified: 08-08-24
package com.mattrixwv.matrix.exceptions;
/**
* Exception thrown to indicate that a scalar value passed to a matrix is invalid
*/
public class InvalidScalarException extends RuntimeException{
/**
* Serialization identifier for this class.
*/
public static final long serialVersionUID = 1;
/**
* Constructs a new {@code InvalidScalarException} with {@code null} as its detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*/
public InvalidScalarException(){
super();
}
/**
* Constructs a new {@code InvalidScalarException} with the specified detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
*/
public InvalidScalarException(String message){
super(message);
}
/**
* Constructs a new {@code InvalidScalarException} with the specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message
* of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions.
*
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidScalarException(Throwable throwable){
super(throwable);
}
/**
* Constructs a new {@code InvalidScalarException} with the specified detail message and cause.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public InvalidScalarException(String message, Throwable throwable){
super(message, throwable);
}

View File

@@ -1,22 +1,57 @@
//Matrix/src/main/java/com/mattrixwv/exceptions/NullMatrixException.java
//Mattrixwv
// Created: 06-30-22
//Modified: 06-30-22
//Modified: 08-08-24
package com.mattrixwv.matrix.exceptions;
/**
* Exception thrown to indicate that a null matrix has been illegally passed to a method
*/
public class NullMatrixException extends RuntimeException{
/**
* Serialization identifier for this class.
*/
public static final long serialVersionUID = 1;
/**
* Constructs a new {@code NullMatrixException} with {@code null} as its detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*/
public NullMatrixException(){
super();
}
/**
* Constructs a new {@code NullMatrixException} with the specified detail message.
* The cause is not initialized and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
*/
public NullMatrixException(String message){
super(message);
}
/**
* Constructs a new {@code NullMatrixException} with the specified cause and a detail message of
* {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message
* of {@code cause}). This constructor is useful for exceptions that are wrappers for other exceptions.
*
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
public NullMatrixException(Throwable throwable){
super(throwable);
}
/**
* Constructs a new {@code NullMatrixException} with the specified detail message and cause.
*
* @param message the detail message, which is saved for later retrieval by the {@link #getMessage()} method.
* @param throwable the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
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

View File

@@ -1,37 +1,55 @@
//Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidCoordinatesException.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
//Modified: 08-11-24
package com.mattrixwv.matrix.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestInvalidCoordinatesException{
private String message = "message";
private Throwable cause = new Exception();
private static final String message = "message";
private static final Throwable cause = new Exception();
@Test
public void testConstructor(){
public void testConstructor_default(){
InvalidCoordinatesException exception = new InvalidCoordinatesException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
exception = new InvalidCoordinatesException(message);
}
@Test
public void testConstructor_message(){
InvalidCoordinatesException exception = new InvalidCoordinatesException(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidCoordinatesException(cause);
@Test
public void testConstructor_cause(){
InvalidCoordinatesException exception = new InvalidCoordinatesException(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
exception = new InvalidCoordinatesException(message, cause);
}
@Test
public void testConstructor_messageCause(){
InvalidCoordinatesException exception = new InvalidCoordinatesException(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}
@Test
public void testConstructor_elements(){
int givenElements = 3;
int neededElements = 2;
String elementsMessage = "Invalid number of elements " + givenElements + " must be at most " + neededElements;
InvalidCoordinatesException exception = new InvalidCoordinatesException(givenElements, neededElements);
assertEquals(elementsMessage, exception.getMessage());
assertNull(exception.getCause());
}
}

View File

@@ -1,36 +1,44 @@
//Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidGeometryException.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
//Modified: 08-11-24
package com.mattrixwv.matrix.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestInvalidGeometryException{
private String message = "message";
private Throwable cause = new Exception();
private static final String message = "message";
private static final Throwable cause = new Exception();
@Test
public void testConstructor(){
public void testConstructor_default(){
InvalidGeometryException exception = new InvalidGeometryException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidGeometryException(message);
@Test
public void testConstructor_message(){
InvalidGeometryException exception = new InvalidGeometryException(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidGeometryException(cause);
@Test
public void testConstructor_cause(){
InvalidGeometryException exception = new InvalidGeometryException(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
}
exception = new InvalidGeometryException(message, cause);
@Test
public void testConstructor_messageCause(){
InvalidGeometryException exception = new InvalidGeometryException(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}

View File

@@ -1,36 +1,44 @@
//Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidRowSizeException.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
//Modified: 08-11-24
package com.mattrixwv.matrix.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestInvalidRowSizeException{
private String message = "message";
private Throwable cause = new Exception();
private static final String message = "message";
private static final Throwable cause = new Exception();
@Test
public void testConstructor(){
public void testConstructor_default(){
InvalidRowSizeException exception = new InvalidRowSizeException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidRowSizeException(message);
@Test
public void testConstructor_message(){
InvalidRowSizeException exception = new InvalidRowSizeException(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidRowSizeException(cause);
@Test
public void testConstructor_cause(){
InvalidRowSizeException exception = new InvalidRowSizeException(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
}
exception = new InvalidRowSizeException(message, cause);
@Test
public void testConstructor_messageCause(){
InvalidRowSizeException exception = new InvalidRowSizeException(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}

View File

@@ -1,36 +1,44 @@
//Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestInvalidScalarException.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
//Modified: 08-11-24
package com.mattrixwv.matrix.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestInvalidScalarException{
private String message = "message";
private Throwable cause = new Exception();
private static final String message = "message";
private static final Throwable cause = new Exception();
@Test
public void testConstructor(){
public void testConstructor_default(){
InvalidScalarException exception = new InvalidScalarException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidScalarException(message);
@Test
public void testConstructor_message(){
InvalidScalarException exception = new InvalidScalarException(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
}
exception = new InvalidScalarException(cause);
@Test
public void testConstructor_cause(){
InvalidScalarException exception = new InvalidScalarException(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
}
exception = new InvalidScalarException(message, cause);
@Test
public void testConstructor_messageCause(){
InvalidScalarException exception = new InvalidScalarException(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}

View File

@@ -1,36 +1,43 @@
//Matrix/src/test/java/com/mattrixwv/matrix/exceptions/TestNullMatrixException.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
//Modified: 08-11-24
package com.mattrixwv.matrix.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class TestNullMatrixException{
private String message = "message";
private Throwable cause = new Exception();
private static final String message = "message";
private static final Throwable cause = new Exception();
@Test
public void testConstructor(){
public void testConstructor_default(){
NullMatrixException exception = new NullMatrixException();
assertNull(exception.getMessage());
assertNull(exception.getCause());
}
exception = new NullMatrixException(message);
@Test
public void testConstructor_message(){
NullMatrixException exception = new NullMatrixException(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
}
exception = new NullMatrixException(cause);
@Test
public void testConstructor_cause(){
NullMatrixException exception = new NullMatrixException(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
exception = new NullMatrixException(message, cause);
}
@Test
public void testConstructor_messageCause(){
NullMatrixException exception = new NullMatrixException(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}

View File

@@ -0,0 +1,4 @@
#junit.jupiter.execution.parallel.enabled=true
#junit.jupiter.execution.parallel.config.strategy=dynamic
#junit.jupiter.execution.parallel.mode.default=concurrent
#junit.jupiter.execution.parallel.mode.classes.default=concurrent