Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hausaufgabe5.la;
- import java.io.IOException;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public final class Matrix {
- private double[][] m;
- private int zeilenZahl;
- private int spaltenZahl;
- private double determinante;
- public Matrix(int zeilenZahl, int spaltenZahl){
- m = new double[zeilenZahl][spaltenZahl];
- }
- public Matrix(Scanner scan) throws IOException {
- String inputLine;
- try {
- inputLine = scan.nextLine();
- zeilenZahl = Integer.parseInt(inputLine);
- inputLine = scan.nextLine();
- spaltenZahl = Integer.parseInt(inputLine);
- m = new double[zeilenZahl][spaltenZahl];
- for (int i = 0; i < zeilenZahl; i++) {
- inputLine = scan.nextLine();
- String[] lineTokens = inputLine.split("\\s+");
- for (int j = 0; j < spaltenZahl; j++){
- m[i][j] = Double.parseDouble(lineTokens[j]);
- }
- }
- } catch (NumberFormatException e) {
- throw new IOException("Ungueltiger Dateiinhalt");
- } catch (NoSuchElementException e) {
- throw new IOException("Ungueltiger Dateiinhalt");
- }
- }
- public double determinante() throws IllegalStateException{
- zeilenZahl = getZeilenZahl();
- spaltenZahl = getSpaltenZahl();
- if(zeilenZahl == 1 && spaltenZahl == 1){
- determinante = m[0][0];
- return determinante;
- } else {
- int pot = -1;
- for(int i = 0; i < zeilenZahl; i++){
- pot *= -1;
- determinante += coMatrix(i).determinante()*m[i][0]*pot;
- }
- return determinante;
- }
- }
- private Matrix coMatrix(int x) throws IllegalArgumentException {
- zeilenZahl = getZeilenZahl();
- spaltenZahl = getSpaltenZahl();
- Matrix coMatrix = new Matrix(zeilenZahl-1, spaltenZahl-1);
- int f = -1;
- for(int g = 0; g < zeilenZahl; g++){
- if(g==x){
- continue;
- }
- f++;
- for(int h = 0; h < spaltenZahl-1; h++){
- coMatrix.m[f][h] = m[g][h+1];
- }
- }
- return coMatrix;
- }
- public int getZeilenZahl() {
- int zeilenZahl=m.length;
- return zeilenZahl;
- }
- public int getSpaltenZahl() {
- int spaltenZahl=m[0].length;
- return spaltenZahl;
- }
- }
Add Comment
Please, Sign In to add comment