-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 30334ea
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.util.Scanner; | ||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
float[][] matrix = new float[3][3]; | ||
|
||
System.out.println("Enter the elements of the 3x3 matrix row by row (3 numbers per line, separated by spaces):"); | ||
for (int i = 0; i < 3; i++) { | ||
System.out.print("Enter row " + (i + 1) + ": "); | ||
String input = scanner.nextLine(); | ||
String[] elements = input.split("\\s+"); | ||
|
||
if (elements.length != 3) { | ||
System.out.println("Enter 3 numbers."); | ||
i--; | ||
continue; | ||
} | ||
|
||
try { | ||
for (int j = 0; j < 3; j++) { | ||
matrix[i][j] = Float.parseFloat(elements[j]); | ||
} | ||
} catch (NumberFormatException e) { | ||
System.out.println("Enter floats."); | ||
i--; // This will allow the user to re-enter the row | ||
} | ||
} | ||
scanner.close(); | ||
Matrix myMatrixObject = new Matrix(matrix); | ||
myMatrixObject.inverseMatrix(); | ||
myMatrixObject.prettyPrintMatrix(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import java.util.Arrays; | ||
/* | ||
How to calculate the inverse of a 3x3 matrix | ||
D - dirty | ||
M - miners | ||
C - cough | ||
T - (on public) transport | ||
D - (and) Die | ||
Morbid but works ^ | ||
1. Calculate the determinate (D) | ||
2. Calculate the matrix of Minors (M) | ||
3. Apply the cofactor matrix: | ||
|+ - +| | ||
|- + -| | ||
|+ - +| | ||
4. Transpose the matrix (along the leading diagonal) | ||
5. Multiply every item in the matrix by 1/determinate | ||
*/ | ||
|
||
public class Matrix { | ||
private float[][] matrix; | ||
public Matrix(float[][] matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
public float determinant2x2(float a, float b, float c, float d) { | ||
// Calculate the determinate of a 2x2 matrix using the formula a*d - b*c | ||
return a*d - b*c; | ||
} | ||
|
||
public float[][] getRawMatrix() { | ||
return this.matrix; | ||
} | ||
|
||
public float determinant3x3() { | ||
// Get items in each position (as shown below) | ||
float a = matrix[0][0], b = matrix[0][1], c = matrix[0][2]; | ||
float d = matrix[1][0], e = matrix[1][1], f = matrix[1][2]; | ||
float g = matrix[2][0], h = matrix[2][1], i = matrix[2][2]; | ||
// use the standard determinate 3x3 formula to calculate the determinate, using the 2x2 function | ||
return a * determinant2x2(e,f,h,i) | ||
- b * determinant2x2(d, f, g, i) | ||
+ c * determinant2x2(d, e, g, h); | ||
} | ||
|
||
public void matrixMinors() { | ||
float[][] minors = new float[3][3]; | ||
// Calculate minors for each element | ||
minors[0][0] = determinant2x2(matrix[1][1], matrix[1][2], matrix[2][1], matrix[2][2]); | ||
minors[0][1] = determinant2x2(matrix[1][0], matrix[1][2], matrix[2][0], matrix[2][2]); | ||
minors[0][2] = determinant2x2(matrix[1][0], matrix[1][1], matrix[2][0], matrix[2][1]); | ||
|
||
minors[1][0] = determinant2x2(matrix[0][1], matrix[0][2], matrix[2][1], matrix[2][2]); | ||
minors[1][1] = determinant2x2(matrix[0][0], matrix[0][2], matrix[2][0], matrix[2][2]); | ||
minors[1][2] = determinant2x2(matrix[0][0], matrix[0][1], matrix[2][0], matrix[2][1]); | ||
|
||
minors[2][0] = determinant2x2(matrix[0][1], matrix[0][2], matrix[1][1], matrix[1][2]); | ||
minors[2][1] = determinant2x2(matrix[0][0], matrix[0][2], matrix[1][0], matrix[1][2]); | ||
minors[2][2] = determinant2x2(matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1]); | ||
matrix = minors; | ||
} | ||
|
||
public void applyCofactorMatrix() { | ||
// Apply the cofactor matrix to our matrix | ||
int[][] cofactor = {{1, -1, 1}, | ||
{-1, 1, -1}, | ||
{1, -1, 1}}; | ||
for(int i=0; i<matrix.length; i++) { | ||
for(int j=0; j<matrix[0].length; j++) { | ||
matrix[i][j] = cofactor[i][j]*matrix[i][j]; | ||
} | ||
} | ||
} | ||
|
||
public void transpose() { | ||
// transpose our matrix | ||
float[][] transposed = new float[3][3]; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
transposed[i][j] = matrix[j][i]; | ||
} | ||
} | ||
// Store it in our matrix variable | ||
matrix = transposed; | ||
} | ||
|
||
public void multiplyEveryItemByFloat(float a) { | ||
// for every item in the matrix, multiply it by a scalar (a) | ||
for(int i=0; i<matrix.length; i++) { | ||
for(int j=0; j<matrix[0].length; j++) { | ||
matrix[i][j] = a*matrix[i][j]; | ||
} | ||
} | ||
} | ||
|
||
public void inverseMatrix() { | ||
// Finally, inverse the matrix | ||
float determinate = determinant3x3(); | ||
if((int) determinate == 0) { | ||
throw new IllegalArgumentException("The determinate of the matrix must not be 0"); | ||
} | ||
matrixMinors(); | ||
applyCofactorMatrix(); | ||
transpose(); | ||
multiplyEveryItemByFloat(1/determinate); | ||
} | ||
|
||
public void prettyPrintMatrix() { | ||
// This is not needed to inverse the matrix, it just makes it look nice in the console | ||
for (float[] row : matrix) { | ||
System.out.printf("| "); | ||
for (int i = 0; i < row.length; i++) { | ||
System.out.printf("%.6f", row[i]); | ||
if (i < row.length - 1) { | ||
System.out.printf(" "); | ||
} | ||
} | ||
System.out.println(" |"); | ||
} | ||
} | ||
} |