import java.io.*;

class SCO10F
{
  public static void pivot(double a[][],int o[],double s[],int k)
  {// GaussLU için pivot metodu
    int n=a.length;
    int dummy1=0;
    double buyuk;
    double dummy=0;
    //pivotlama
	int p=k;
	buyuk=Math.abs(a[o[k]][k]);
	for(int ii=k+1;ii<n;ii++)
	{  dummy=Math.abs(a[o[ii]][k]/s[o[ii]]);
	   if(dummy > buyuk) {buyuk=dummy;p=ii;}
	}
	dummy1=o[p];
	o[p]=o[k];
	o[k]=dummy1;	   
  }
  
  public static int[] sirali_indeks(int n)
  {   // sıralı rakamları taşıyan bir vektör oluşturur 
	  int o[]=new int[n];
	  for(int i=0;i<n;i++) {o[i]=i;}
	  return o;
  }	  
    
  public static double[] yerine_koyma(double ai[][],double bi[])
  {   int n=ai.length;
	  return yerine_koyma(ai,bi,sirali_indeks(n));
  }
  
  public static double[][] yerine_koyma(double ai[][],double bi[][])
  {   int n=ai.length;
	  return yerine_koyma(ai,bi,sirali_indeks(n));
  }  
  
  public static double[] yerine_koyma(double ai[][],double bi[],int o[])
  { //gauss LU çözümüyle LU indirgenmesi yapılmış Matris kullanarak lineer 
    //denklem sistemi kökleri
    int n=ai.length;
    double toplam;
    double x[]=new double[n];
     double a[][]=new double[n][n];
    double b[]=new double[n];
    int i,j;
  for(i=0;i<n;i++)
      {for(j=0;j<n;j++){a[i][j]=ai[i][j];};b[i]=bi[i];}   
  //System.out.println("a=\n "+Matrix.toString(a));
  //System.out.println("b=\n "+Matrix.toStringT(b));             
  for(i=1;i<n;i++)
      {toplam=b[o[i]];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]];};b[o[i]]=toplam;}
   //System.out.println("d=\n "+Matrix.toStringT(b));      
  x[n-1]=b[o[n-1]]/a[o[n-1]][n-1];
  for(i=(n-2);i>=0;i--)
      {toplam=0;      
       for(j=i+1;j<n;j++)      
        {toplam+=a[o[i]][j]*x[j];};
        x[i]=(b[o[i]]-toplam)/a[o[i]][i];
      }
  //System.out.println("d=\n "+Matrix.toStringT(x));        
  return x;              
  }  
  
  public static double[][] yerine_koyma(double ai[][],double bi[][],int o[])
  {
    int n=ai.length;
    int m=bi[0].length;
    double toplam;
    double x[][]=new double[n][m];
    double a[][]=new double[n][n];
    double b[][]=new double[n][m];
    int i,j,k;
  for(i=0;i<n;i++)
      {for(j=0;j<n;j++){a[i][j]=ai[i][j];};for(k=0;k<m;k++) {b[i][k]=bi[i][k];}}   
  //System.out.println("a=\n "+Matrix.toString(a));
  //System.out.println("b=\n "+Matrix.toStringT(b));             
  for(k=0;k<m;k++)
  {
  for(i=1;i<n;i++)
      {toplam=b[o[i]][k];for(j=0;j<i;j++){toplam-=a[o[i]][j]*b[o[j]][k];};b[o[i]][k]=toplam;}
   //System.out.println("d=\n "+Matrix.toStringT(b));      
  x[n-1][k]=b[o[n-1]][k]/a[o[n-1]][n-1];
  for(i=(n-2);i>=0;i--)
      {toplam=0;      
       for(j=i+1;j<n;j++)      
        {toplam+=a[o[i]][j]*x[j][k];};
        x[i][k]=(b[o[i]][k]-toplam)/a[o[i]][i];
      }
  }//kin sonu   
  //System.out.println("d=\n "+Matrix.toStringT(x));        
  return x;          
  }
        
public static double[][] crautLU(double c[][])
{
//craut LU ayrıştırma, pivotsuz

int n=c.length;
double sum;
int i,j,k; 
double d[][]=new double[n][n];
for(i=0;i<n;i++)
{for(j=0;j<n;j++) d[i][j]=c[i][j];}

for(j=1;j<=n;j++)
{ 
  for(i=1;i<j;i++)
    {
    sum=d[i-1][j-1];
    for(k=1;k<i;k++)
	{ sum-=d[i-1][k-1]*d[k-1][j-1];}
    d[i-1][j-1]=sum;
    }
  for(i=j;i<=n;i++)
    {
    sum=d[i-1][j-1];
    for(k=1;k<j;k++)
      {sum-=d[i-1][k-1]*d[k-1][j-1];}
    d[i-1][j-1]=sum;
    }
  if(j != n)
  { for(i=j+1;i<=n;i++)
       d[i-1][j-1]/=d[j-1][j-1];
  }    
}    
System.out.println(Matrix.toString(d));
return d;
}


    
  public static void main(String args[]) throws IOException
  {
  double a[][]={{2,-6,1},{-1,7,-1},{1,-3,2}};
  double b[]={12,-8,16};
  double c[][]= crautLU(a);
  double x[]=yerine_koyma(c,b);
  Text.print(x,"craut LU ayrıştırma ve yerine koyma metodu çözümü");
  }
}