/*
 * 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
 */
/* LUZerlegung_Inplace_Variante02.java           by Christof Senn, March 2003 */
/* Die L und die U Matrizen werden im ursprünglichen Feld berechnet.          */
/* LU-Matrix:  ( u_ii=1 fuer i={1,2,3, ..,n} )                                */


public class LU_Zerlegung_InplaceVariante02 {


	/* Matrixgroesse: */
	private static int n = 3;
	private static final int vorKomma = 3;
	private static final int nachKomma= 5;

	public static void main(String[] args){	
		double A[][] = 
		{	{2.1756,  4.0231,   -2.1732,  5.1967},
	 		{-4.0231, 6.0,      0.0,      1.1973},
	 		{-1.0,    -5.2107,  1.1111,   0.0},
			{6.0235,  7.0,      0.0,      -4.1561}};
	
	//	/*
		double Kontrolle[][] = new double[n][n];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
	   			Kontrolle[i][j] = 0.0;
			}
		}
	//	*/
		
		/* Ausgabe der Eingabe-Matrix auf die Konsole: */
		InOut.print("\n"+n+" x "+n+" Matrix: A\n\n");	
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				InOut.print(A[i][j], vorKomma, nachKomma);
				InOut.print("\t");
			}
			InOut.print("\n");
		}
		InOut.print("\n\n");
		
		/* Berechnung der Lösungs-Matrix */
		for(int j = 1; j < n; j++){
			A[0][j] = A[0][j] / A[0][0];
		}
		/*
		for(int i = 1;i < n-1; i++){
			for(int j = i;j < n; j++){
				A[i][j] = A[i][j] - sum(A,i, i, j);
			}
			for(int j = i+1; j < n; j++){ 
				A[j][i] = 1.0 / A[i][i] * (A[j][i] - sum(A,i,j,i));
			}
		}
		*/
		for(int i = 1;i < n-1; i++){
			for(int j = i;j < n; j++){
				A[j][i] = A[j][i] - sum(A,i, j, i);
			}
			for(int j = i+1; j < n; j++){ 
				A[i][j] = 1.0 / A[i][i] * (A[i][j] - sum(A,i,i,j));
			}
		}
		A[n-1][n-1] = A[n-1][n-1] - sum(A,n-1,n-1,n-1);
		
		/* Kontrolle: */
	//	/*
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				for(int k = 0; k <= i && k <= j; k++){
					if(k == j){
						Kontrolle[i][j] += A[i][k];
					}
					else{
						Kontrolle[i][j] += A[i][k] * A[k][j];
					}
				}
			}
		}
	//	*/
		
		/* Ausgabe der Lösungs-Matrix auf die Konsole: */
	//	/*
		InOut.print("\nKontroll-Matrix: A = LU\n\n");
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				InOut.print(Kontrolle[i][j], vorKomma, nachKomma);
				InOut.print("\t");
			}
			InOut.print("\n");
		}
		InOut.print("\n\n");
	//	*/
		
		InOut.print("\nLU-Matrix  ( u_ii=1 fuer i={1,2,3, ..,n} )\n\n");	
		for(int i = 0; i < n; i++){
			for(int j = 0;j < n; j++){
				InOut.print(A[i][j], vorKomma, nachKomma);
				InOut.print("\t");
			}
			InOut.print("\n");
		}
		InOut.print("\n\n");
	}
	
	private static double sum(double A[][], int bis, int i, int j) {
		double sum = 0;	
		for(int k = 0; k < bis; k++){
			sum += A[i][k] * A[k][j];			
			}
	return sum;
	}
}