Skip to content

Commit

Permalink
feat: Add support for determinant calculation (#131)
Browse files Browse the repository at this point in the history
This pull request includes several feature updates and documentation improvements to enhance the functionality and usability of the JMatrix library, especially focusing on adding support for determinant calculation.

Signed-off-by: Ryuu Mitsuki <dhefam31@gmail.com>
  • Loading branch information
mitsuki31 committed Sep 13, 2024
2 parents 654cb32 + bee7566 commit f80d147
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 3 deletions.
114 changes: 113 additions & 1 deletion src/main/java/com/mitsuki/jmatrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.mitsuki.jmatrix.core.MatrixChecker;
import com.mitsuki.jmatrix.core.MatrixUtils;
import com.mitsuki.jmatrix.core.MatrixDeterminant;
import com.mitsuki.jmatrix.exception.IllegalMatrixSizeException;
import com.mitsuki.jmatrix.exception.InvalidIndexException;
import com.mitsuki.jmatrix.exception.JMatrixBaseException;
Expand Down Expand Up @@ -117,7 +118,7 @@
*
* @author <a href="https://github.com/mitsuki31" target="_blank">
* Ryuu Mitsuki</a>
* @version 3.3, 12 September 2024
* @version 3.4, 13 September 2024
* @since 0.1.0
* @license <a href="https://www.apache.org/licenses/LICENSE-2.0" target="_blank">
* Apache License 2.0</a>
Expand Down Expand Up @@ -201,6 +202,7 @@ public class Matrix implements MatrixUtils {
:: MATRIX CONSTRUCTORS
::
=========================================*/
// region Constructors


/**
Expand Down Expand Up @@ -411,6 +413,7 @@ public Matrix(double[ ][ ] arr) {
:: MATRIX SPECIAL CONSTRUCTORS
::
=========================================*/
// region Special Constructors

/*--------------------------
:: Create
Expand Down Expand Up @@ -1740,6 +1743,7 @@ private static double multCell(double[ ][ ] a, double[ ][ ] b, int row, int col)
:: MATRIX OPERATIONS
::
=========================================*/
// region Matrix Operations


/*--------------------------
Expand Down Expand Up @@ -3372,11 +3376,117 @@ public static Matrix minorMatrix(Matrix m, int row, int col) {
}


/*--------------------------
:: Determinant Matrix
--------------------------*/

/**
* Calculates the determinant of the matrix.
*
* <p>This method calls the static method {@link Matrix#determinant(Matrix)} to calculate
* the determinant of this matrix.
*
* <p>It will automatically chooses the best algorithm for calculating the determinant
* based on the size of the matrix. If the matrix is smaller than {@code 4x4}, this method
* uses the cofactor expansion algorithm. Otherwise, it uses Gaussian elimination.
*
* @return The determinant of the matrix.
*
* @throws NullMatrixException
* If the matrix is {@code null}.
* @throws IllegalMatrixSizeException
* If the matrix is not a square matrix.
*
* @implNote
* This method is just a wrapper for {@link Matrix#determinant(Matrix)}. It is more convenient
* to call this method directly on the matrix instance instead of calling the static method.
*
* @since 1.5.0
* @see #determinant(Matrix)
* @see MatrixDeterminant#determinant_CofactorExpansion(Matrix)
* @see MatrixDeterminant#determinant_GaussElimination(Matrix)
*/
public double determinant() {
return Matrix.determinant(this);
}

/**
* Calculates the determinant of the given matrix.
*
* <p>This method automatically chooses the best algorithm for calculating the determinant
* based on the size of the matrix. If the matrix is smaller than {@code 4x4}, this method
* uses the cofactor expansion algorithm. Otherwise, it uses Gaussian elimination.
*
* @param m The matrix to calculate the determinant of.
* @return The determinant of the matrix.
*
* @throws NullMatrixException
* If the matrix is {@code null} or contains {@code null} references.
* @throws IllegalMatrixSizeException
* If the matrix is not a square matrix.
*
* @since 1.5.0
* @see #determinant(double[][])
* @see MatrixDeterminant#determinant_CofactorExpansion(Matrix)
* @see MatrixDeterminant#determinant_GaussElimination(Matrix)
*/
public static double determinant(Matrix m) {
if (MatrixUtils.isNullEntries(m)) {
raise(new NullMatrixException(
"Matrix is null. Please ensure the matrix are initialized")
);
}

if (m.getNumRows() <= 4) { // Not larger than 4x4
// Cofactor expansion is faster for smaller matrices
return MatrixDeterminant.determinant_CofactorExpansion(m);
} else {
return MatrixDeterminant.determinant_GaussElimination(m);
}
}

/**
* Calculates the determinant of the given matrix.
*
* <p>This method automatically chooses the best algorithm for calculating the determinant
* based on the size of the matrix. If the matrix is smaller than {@code 4x4}, this method
* uses the cofactor expansion algorithm. Otherwise, it uses Gaussian elimination.
*
* @param a The 2D array representation of the matrix to calculate the determinant of.
* @return The determinant of the matrix.
*
* @throws NullMatrixException
* If the matrix is {@code null} or contains {@code null} references.
* @throws IllegalMatrixSizeException
* If the matrix is not a square matrix.
*
* @since 1.5.0
* @see #determinant(Matrix)
* @see MatrixDeterminant#determinant_CofactorExpansion(double[][])
* @see MatrixDeterminant#determinant_GaussElimination(double[][])
*/
public static double determinant(double[][] a) {
if (a == null || a.length == 0) {
raise(new NullMatrixException(
"Matrix is null. Please ensure the matrix are initialized")
);
}

if (a.length <= 4) { // Not larger than 4x4
// Cofactor expansion is faster for smaller matrices
return MatrixDeterminant.determinant_CofactorExpansion(a);
} else {
return MatrixDeterminant.determinant_GaussElimination(a);
}
}


/*=========================================
::
:: MATRIX TYPE CHECKERS
::
=========================================*/
// region Type Checkers


/*---------------------------
Expand Down Expand Up @@ -4278,6 +4388,7 @@ public static boolean isIdentity(double[][] arr) {
:: ADDITIONAL / UTILITIES METHODS
::
=========================================*/
// region Utilities Methods


/*---------------------------
Expand Down Expand Up @@ -5225,6 +5336,7 @@ final public static void display(double[ ][ ] arr, int index) {
:: OVERRIDEN METHODS
::
=========================================*/
// region Overriden Methods


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
* to be created to use its methods and provides static methods for checking matrix properties
* such as nullity, squareness, diagonalness, and identity.
*
* @since 1.5.0
* @since 1.5.0
* @version 1.0, 12 September 2024
* @author <a href="https://github.com/mitsuki31" target="_blank">
* Ryuu Mitsuki</a>
* @license <a href="https://www.apache.org/licenses/LICENSE-2.0" target="_blank">
* Apache License 2.0</a>
*/
public class DoubleArray2DChecker {
/** Hide the constructor because it is a static class */
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/mitsuki/jmatrix/core/MatrixChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
* the properties of matrices. It extends the {@link DoubleArray2DChecker} class and provides
* static methods for checking matrix properties such as nullity, squareness, diagonalness, and identity.
*
* @since 1.5.0
* @since 1.5.0
* @version 1.0, 12 September 2024
* @author <a href="https://github.com/mitsuki31" target="_blank">
* Ryuu Mitsuki</a>
* @license <a href="https://www.apache.org/licenses/LICENSE-2.0" target="_blank">
* Apache License 2.0</a>
*/
public class MatrixChecker extends DoubleArray2DChecker {
/** Hide the constructor because it is a static class */
Expand Down
Loading

0 comments on commit f80d147

Please sign in to comment.