Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created May 12, 2010 09:20
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Cellane/398372 to your computer and use it in GitHub Desktop.
Save Cellane/398372 to your computer and use it in GitHub Desktop.
/**
* Created by IntelliJ IDEA.
* User: Milan Vít (Cellane)
* Date: May 12, 2010
* Time: 2:16:44 PM
*/
import java.text.MessageFormat;
public class MatrixDeterminant {
/**
* Main method that initializes the matrix and calls method to calculate
* its determinant
*
* @param args command line arguments; unused
*/
public static void main (String[] args) {
double x[][] = {
{14, 5, 2, 10,},
{12, -9, 0, 7,},
{1, 2, 0, -4,},
{-7, 29, 2, -16},
};
double determinant;
determinant = MatrixOperations.matrixDeterminant (x);
System.out.println (MessageFormat.format ("Determinant: {0}", Double.toString (determinant)));
}
}
/**
* Created by IntelliJ IDEA.
* User: Milan Vít (Cellane)
* Date: May 13, 2010
* Time: 1:10:02 PM
*/
public class MatrixInversion {
/**
* Main method that initializes the matrix and calls method to calculate
* inverted matrix of given matrix
*
* @param args command line arguments; unused
*/
public static void main (String[] args) {
double matrix[][] = {
{14, 5, 2, 10,},
{12, -9, 0, 7,},
{1, 2, 0, -4,},
{-7, 29, 2, -16},
};
double invertedMatrix[][];
invertedMatrix = MatrixOperations.invertMatrix (matrix);
MatrixOperations.printMatrix (invertedMatrix, 4);
}
}
/**
* Created by IntelliJ IDEA.
* User: Milan Vít (Cellane)
* Date: May 12, 2010
* Time: 10:32:52 AM
*/
public class MatrixMultiplication {
/**
* Main method that initialises matrices we'll be counting with, calls
* method that does the multiplication and prints results
*
* @param args command line arguments; unused
*/
public static void main (String args[]) {
int x[][] = {
{1, 2,},
{4, 5,},
{7, 8,},
};
int y[][] = {
{9, 8, 7,},
{6, 5, 4,},
};
int z[][];
z = MatrixOperations.multiplyMatrices (x, y);
MatrixOperations.printMatrix (z, 3);
}
}
/**
* Created by IntelliJ IDEA.
* User: Milan Vít (Cellane)
* Date: May 12, 2010
* Time: 11:17:33 AM
*/
import java.text.MessageFormat;
public class MatrixOperations {
/**
* Method that multiplies two matrices and returns the result
*
* @param x first matrix
* @param y second matrix
*
* @return result after multiplication
*/
public static int[][] multiplyMatrices (int[][] x, int[][] y) {
int[][] result;
int xColumns, xRows, yColumns, yRows;
xRows = x.length;
xColumns = x[0].length;
yRows = y.length;
yColumns = y[0].length;
result = new int[xRows][yColumns];
if (xColumns != yRows) {
throw new IllegalArgumentException (
MessageFormat.format ("Matrices don't match: {0} != {1}.", xColumns, yRows));
}
for (int i = 0; i < xRows; i++) {
for (int j = 0; j < yColumns; j++) {
for (int k = 0; k < xColumns; k++) {
result[i][j] += (x[i][k] * y[k][j]);
}
}
}
return (result);
}
/**
* Method that calculates determinant of given matrix
*
* @param matrix matrix of which we need to know determinant
*
* @return determinant of given matrix
*/
public static double matrixDeterminant (double[][] matrix) {
double temporary[][];
double result = 0;
if (matrix.length == 1) {
result = matrix[0][0];
return (result);
}
if (matrix.length == 2) {
result = ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
return (result);
}
for (int i = 0; i < matrix[0].length; i++) {
temporary = new double[matrix.length - 1][matrix[0].length - 1];
for (int j = 1; j < matrix.length; j++) {
for (int k = 0; k < matrix[0].length; k++) {
if (k < i) {
temporary[j - 1][k] = matrix[j][k];
} else if (k > i) {
temporary[j - 1][k - 1] = matrix[j][k];
}
}
}
result += matrix[0][i] * Math.pow (-1, (double) i) * matrixDeterminant (temporary);
}
return (result);
}
public static double[][] invertMatrix (double[][] matrix) {
double[][] auxiliaryMatrix, invertedMatrix;
int[] index;
auxiliaryMatrix = new double[matrix.length][matrix.length];
invertedMatrix = new double[matrix.length][matrix.length];
index = new int[matrix.length];
for (int i = 0; i < matrix.length; ++i) {
auxiliaryMatrix[i][i] = 1;
}
transformToUpperTriangle (matrix, index);
for (int i = 0; i < (matrix.length - 1); ++i) {
for (int j = (i + 1); j < matrix.length; ++j) {
for (int k = 0; k < matrix.length; ++k) {
auxiliaryMatrix[index[j]][k] -= matrix[index[j]][i] * auxiliaryMatrix[index[i]][k];
}
}
}
for (int i = 0; i < matrix.length; ++i) {
invertedMatrix[matrix.length - 1][i] = (auxiliaryMatrix[index[matrix.length - 1]][i] /
matrix[index[matrix.length - 1]][matrix.length - 1]);
for (int j = (matrix.length - 2); j >= 0; --j) {
invertedMatrix[j][i] = auxiliaryMatrix[index[j]][i];
for (int k = (j + 1); k < matrix.length; ++k) {
invertedMatrix[j][i] -= (matrix[index[j]][k] * invertedMatrix[k][i]);
}
invertedMatrix[j][i] /= matrix[index[j]][j];
}
}
return (invertedMatrix);
}
public static void transformToUpperTriangle (double[][] matrix, int[] index) {
double[] c;
double c0, c1, pi0, pi1, pj;
int itmp, k;
c = new double[matrix.length];
for (int i = 0; i < matrix.length; ++i) {
index[i] = i;
}
for (int i = 0; i < matrix.length; ++i) {
c1 = 0;
for (int j = 0; j < matrix.length; ++j) {
c0 = Math.abs (matrix[i][j]);
if (c0 > c1) {
c1 = c0;
}
}
c[i] = c1;
}
k = 0;
for (int j = 0; j < (matrix.length - 1); ++j) {
pi1 = 0;
for (int i = j; i < matrix.length; ++i) {
pi0 = Math.abs (matrix[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1) {
pi1 = pi0;
k = i;
}
}
itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i = (j + 1); i < matrix.length; ++i) {
pj = matrix[index[i]][j] / matrix[index[j]][j];
matrix[index[i]][j] = pj;
for (int l = (j + 1); l < matrix.length; ++l) {
matrix[index[i]][l] -= pj * matrix[index[j]][l];
}
}
}
}
/**
* Method that prints matrix
*
* @param matrix matrix to print
* @param id what does the matrix contain?
*/
public static void printMatrix (int[][] matrix, int id) {
double doubleMatrix[][] = new double[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
doubleMatrix[i][j] = (double) matrix[i][j];
}
}
printMatrix (doubleMatrix, id);
}
/**
* Method that prints matrix
*
* @param matrix matrix to print
* @param id what does the matrix contain?
*/
public static void printMatrix (double[][] matrix, int id) {
int cols, rows;
rows = matrix.length;
cols = matrix[0].length;
switch (id) {
case 1:
System.out.print (MessageFormat.format ("First matrix[{0}][{1}]:", rows, cols));
break;
case 2:
System.out.print (MessageFormat.format ("Second matrix[{0}][{1}]:", rows, cols));
break;
case 3:
System.out.print (MessageFormat.format ("Result[{0}][{1}]:", rows, cols));
break;
case 4:
System.out.print (MessageFormat.format ("Inverted matrix[{0}][{1}]:", rows, cols));
break;
default:
System.out.print (MessageFormat.format ("Matrix[{0}][{1}]:", rows, cols));
break;
}
System.out.println ();
for (int i = 0; i < matrix.length; i++) {
System.out.print ("[");
for (int j = 0; j < matrix[i].length; j++) {
System.out.print (matrix[i][j]);
if ((j + 1) != matrix[i].length) {
System.out.print (", ");
}
}
if ((i + 1) != matrix.length) {
System.out.println ("]");
} else {
System.out.println ("].");
}
}
System.out.println ();
}
}
@hreinn91
Copy link

I ran the determinant code for a 1000*1000 matrix and it took for ever. Is there something I am missing?

@i-make-robots
Copy link

May I use MatrixOperations in an open source project?

@Cellane
Copy link
Author

Cellane commented Jun 24, 2019

@hreinn91 Very very late reply, but while I don’t remember the code at all (I mean, it’s 9 years old now 😅), I would guess you’re not missing at all – it’s just that the code is not very efficient.

@i-make-robots Go for it! And please feel free to re-format the code; apparently, I had horrible coding standards 9 years ago 🤣

@hreinn91
Copy link

Ok! 👍

@exelstar12
Copy link

Thanks mate, i love your function to calculate matrix determinant. Very very helpful :)

@aspory
Copy link

aspory commented Nov 8, 2019

Thank you for the determinate code. Even though it takes 3 minutes to compute, it's better than anything I've made with recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment