import java.io.*;

class SCO10I
{

public static double[][] choleskyLU(double c[][])
{
//cholesky LU 
//sadece simetrik matrisler içindir	
double factor=0;
double buyuk;
int i,j,k,p,ii,jj;
int n=c.length;
double d[][]=new double[n][n];
double dummy;
double toplam;
for(i=0;i<n;i++)
{for(j=0;j<n;j++) d[i][j]=c[i][j];}
for(k=1;k<=n;k++)
{
 for(i=1;i<=(k-1);i++)
    {toplam=0;;
     for(j=1;j<=(i-1);j++)
     { toplam+=d[i-1][j-1]*d[k-1][j-1]; }
    d[k-1][i-1]=(d[k-1][i-1]-toplam)/d[i-1][i-1];
    }
 toplam=0.0;
 for(j=1;j<=(k-1);j++)
 { toplam+=d[k-1][j-1]*d[k-1][j-1]; }
 d[k-1][k-1]=Math.sqrt((d[k-1][k-1]-toplam));
 for(j=k+1;j<=n;j++) d[k-1][j-1]=0.0;      
}
return d;
}

  public static double[] substitutecholesky(double c[][],double b[])
  {
  //ileriye doğru yerine koyma cholesky simetrik LU metoduyla 
  //indirgenmiş olmalıdır
  int i,j;
  double U[][]=T(c);
  double L[][]=c;
  System.out.println("L*L(T) = \n"+Matrix.toString(Matrix.multiply(L,U)));
  int n=b.length;
  double toplam=0;
  double x[]=new double[n];
  for(i=1;i<n;i++)
    {toplam=b[i];
     for(j=0;j<i;j++) 
       {toplam-=U[i][j]*b[j];}
    b[i]=toplam/L[i][i];;
    }
  //geriye yerine koyma  
  x[n-1]=b[n-1]/L[n-1][n-1];
  for(i=n-2;i>=0;i--)
  {
    toplam=0;
    for(j=(i+1);j<n;j++)
    {  toplam+=U[i][j]*x[j];}
    x[i]=(b[i]-toplam)/U[i][i];
  }  
  return x;
  }
  
  public static double[][] T(double [][] left)
  {
  //transpose matrix (if A=a(i,j) T(A)=a(j,i)
  int i,j;
  int n=left.length;
  int m=left[0].length;
  double b[][];
  b=new double[m][n];
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
    {
    b[j][i]=left[i][j];
    }
  }
  return b;
  }
  
  public static void main(String args[]) throws IOException
  {
  double a[][]={{6,15,55},{15,55,225},{55,225,979}};
  double b[]={1,1,1};
  double L[][]=choleskyLU(a);
  System.out.println("Matris L : \n"+Matrix.toString(L));
  double x[]=substitutecholesky(L,b); 
  Text.print(x,"Cholesky LU metodu çözümü");
  }
}