Skip to content

Instantly share code, notes, and snippets.

@thraizz
Last active January 8, 2018 16:04
Show Gist options
  • Save thraizz/c33b338b88269e38a9cba471c1c87e7c to your computer and use it in GitHub Desktop.
Save thraizz/c33b338b88269e38a9cba471c1c87e7c to your computer and use it in GitHub Desktop.
DeterminatenProgramm
public class determinantenProgramm{
public int[][] matrix;
static String toString(int [][] matrix) {
String result = "";
for(int i = 0 ; i < (matrix.length*matrix.length) ; i++){
int j = i % matrix.length;
int k = i / matrix.length;
if (j == 0) {
result += "( " + matrix[j][k];
}
else {
result += " , " + matrix[j][k];
}
if (j == matrix.length-1) {
result += " ) \n";
}
}
return result;
}
public static int det(int[][] matrix) {
int determinante = 0;
int vorzeichen;
// Falls Matrix eindimensional
if(matrix.length == 1) {
return matrix[0][0];
}
//Falls mehrdimensional
for(int i = 0; i < matrix[0].length; i++) {
int temp[][] = new int[matrix.length - 1][matrix[0].length -1];
for(int j = 1; j < matrix.length; j++) {
for(int k = 0; k < matrix.length; k++) {
if(k < i) {
temp[j - 1][k] = matrix[j][k];
}
else if(k > i) {
temp[j - 1][k - 1] = matrix[j][k];
}
}
}
if(i%2==0){ //Falls durch zwei geteilt rest null ergibt ist das Feld gerade
vorzeichen=1;
}
else{ //Sonst ungerade
vorzeichen=-1;
}
//HIER MUSS DETERMINANTE AKTUALISIERT WERDEN
determinante+=vorzeichen*matrix[0][i]*(det(temp));
}
return determinante;
}
public static void main(String[] args) {
int[][] matrix = {{4,6,-2,-5,3},{1,2,3,1,1},{0,1,2,5,4},{2,5,-4,0,1},{3,2,2,1,0}};
System.out.println(toString(matrix));
System.out.println(matrix.length);
System.out.println(det(matrix));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment