/*
 * Created on 08.04.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author GerberDom
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */


/**
 * @author Dominik Wenger
 *
 * Die LU-Zerlegung nach der Variante von M.Vogel.
 * Die L und die U Matrizen werden in einem Feld berechnet.
 * DEs bestet
 */

import javax.swing.*;

public class LU_Zerlegung_Vogel {

	public static void main(String[] args) {
		
		/**
		 * Einlesen der Anzahl Nachkommastellen
		 */
		String genauigkeit = JOptionPane.showInputDialog("Anzahl Nachkommastellen eingeben");
		
		int digits = Integer.parseInt(genauigkeit);
		
		/**
		 * Einlesen der Matrix-Grösse
		 */
		String size = JOptionPane.showInputDialog("Matrix-Grösse eingeben");
		
		int n = Integer.parseInt(size);
		
		double [][] A = new double[n][n];
		
		for(int i=0;i<A.length;i++){
			for(int j=0;j<A[i].length;j++){
				String fieldValue = JOptionPane.showInputDialog("Wert für Matrix A ["+(i+1)+","+(j+1)+"] eingeben");
				double value = Double.parseDouble(fieldValue);
				A[i][j] = value;
			}
		}
		
		/**
		 * Ausgabe der Eingabe-Matrix auf die Konsole:
		 */
		System.out.println("");
		System.out.println(n+"x"+n+" Matrix");
		System.out.println("");
		
		for(int i=0;i<A.length;i++){
			for(int j=0;j<A[i].length;j++){
				InOut.print(A[i][j],5,digits);
				System.out.print(" ");
			}
			System.out.println("");
		}
		
		/**
		 * Berechnung der Lösungs-Matrix
		 */
		for(int j=2;j<=n;j++){
			A[j-1][0] = A[j-1][0]/A[0][0];
		}
		
		for(int i=2;i<=n-1;i++){
			for(int j=i;j<=n;j++)
				A[i-1][j-1] = A[i-1][j-1] - sum1(A,i,j);
			for(int j=i+1;j<=n;j++){
				A[j-1][i-1] = 1.0/A[i-1][i-1] * (A[j-1][i-1]- sum2(A,i,j));
			}
		}
		
		A[n-1][n-1] = A[n-1][n-1] - sum(A,n);
		
		/**
		 * Ausgabe der Lösungs-Matrix auf die Konsole:
		 */
		System.out.println("");
		System.out.println("Lösungs-Matrix");
		System.out.println("");
		
		for(int i=0;i<A.length;i++){
			for(int j=0;j<A[i].length;j++){
				InOut.print(A[i][j],5,digits);
				System.out.print(" ");
			}
			System.out.println("");
		}
		System.exit(0);
	}
	
	/**
	 * Method sum.
	 * @param A
	 * @param n
	 * @return double
	 */
	private static double sum(double[][] A, int n) {
		double sum = 0;		
		for(int k=1;k<=n-1;k++){
			sum += A[n-1][k-1]*A[k-1][n-1];			
		}
		return sum;
	}

	/**
	 * Method sum.
	 * @param A
	 * @param i
	 * @param j
	 * @return double
	 */
	private static double sum1(double[][] A, int i, int j) {
		double sum = 0;	
		for(int k=1;k<=i-1;k++){
			sum += A[i-1][k-1]*A[k-1][j-1];			
		}
		return sum;
	}
	
	/**
	 * Method sum.
	 * @param A
	 * @param i
	 * @param j
	 * @return double
	 */
	private static double sum2(double[][] A, int i, int j) {
		double sum = 0;
		for(int k=1;k<=i-1;k++){
			sum += A[j-1][k-1]*A[k-1][i-1];			
		}
		return sum;
	}
}
