From a643248aa5e1ad322776f413b0874fb9b1fe9b14 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 7 Feb 2022 19:27:53 +0000 Subject: [PATCH] Added adjoint and inverse functions --- .../com/mattrixwv/matrix/IntegerMatrix.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 205b695..a77eb76 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -680,6 +680,9 @@ public class IntegerMatrix{ //Return the determinant return det; } + public IntegerMatrix cof(){ + return cofactor(); + } public IntegerMatrix cofactor(){ //Make sure the matrix is square if(!isSquare()){ @@ -706,6 +709,27 @@ public class IntegerMatrix{ //Return the new matrix return new IntegerMatrix(newGrid); } + public IntegerMatrix adj(){ + return adjoint(); + } + public IntegerMatrix adjoint(){ + return cofactor().transpose(); + } + public IntegerMatrix inverse(){ + //Make sure the matrix is square + if(!isSquare()){ + throw new InvalidGeometryException("A matrix must be square for it to have an inverse"); + } + + //Make sure the determinant is not 0 + int determinant = determinant(); + if(determinant == 0){ + throw new InvalidScalarException("The determinant cannot be 0"); + } + + //Return the new matrix + return adjoint().multiply(1 / determinant); + } //Object funtions @Override