import java.io.*;
import javax.swing.*;
class NA18
{  
  public static double[][] gaussLU(double ai[][])
  {
  int n=ai.length;	  
  double x[]=new double[n];
  double a[][]=new double[n][n];
  double carpan=0;
  double toplam=0;
  //gauss eleme
  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<(n-1);k++)
  {
    for(i=k+1;i<n;i++)
    {  carpan=a[i][k]/a[k][k];
       a[i][k]=carpan;///L elemanı
       for(j=k+1;j<n;j++)
       {   a[i][j]-=carpan*a[k][j]; }
	}
  }
  return a;
  }

  
  public static double[] yerine_koyma(double ai[][],double bi[])
  { //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[i];for(j=0;j<i;j++){toplam-=a[i][j]*b[j];};b[i]=toplam;}
  System.out.println("d=\n "+Matrix.toStringT(b));      
  x[n-1]=b[n-1]/a[n-1][n-1];
  for(i=(n-2);i>=0;i--)
      {toplam=0;      
       for(j=i+1;j<n;j++)      
        {toplam+=a[i][j]*x[j];};
        x[i]=(b[i]-toplam)/a[i][i];
      }
  System.out.println("d=\n "+Matrix.toStringT(x));        
  return x;              
  }  
  
  public static double[][] yerine_koyma(double ai[][],double bi[][])
  {
    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[i][k];for(j=0;j<i;j++){toplam-=a[i][j]*b[j][k];};b[i][k]=toplam;}
  System.out.println("d=\n "+Matrix.toStringT(b));      
  x[n-1][k]=b[n-1][k]/a[n-1][n-1];
  for(i=(n-2);i>=0;i--)
      {toplam=0;      
       for(j=i+1;j<n;j++)      
        {toplam+=a[i][j]*x[j][k];};
        x[i][k]=(b[i][k]-toplam)/a[i][i];
      }
  }//kin sonu   
  System.out.println("d=\n "+Matrix.toStringT(x));        
  return x;          
  }
  
  public static double [] AXB(double a[][],double b[])
  {
  //denklem sistemi çözümü b vektörü için	  
  int n=a.length;	  
  double c[][]=gaussLU(a);
  Matrix.toString(c);
  double s[]=yerine_koyma(c,b);
  return s;
  }
  
  public static double [][] AXB(double a[][],double b[][])
  {//denklem sistemi çözümü b matrisi için	  
  int n=a.length;	  
  double c[][]=gaussLU(a);
  Matrix.toString(c);
  double s[][]=yerine_koyma(c,b);
  return s;
  }
  
  public static void main(String args[]) throws IOException
  {
  //double a[][]={{1,2,5},{2,1,2},{3,1,1}};
  //double b[]={1,1,2};
  double a[][]={{6,15,55},{15,55,225},{55,225,979}};
  double b[]={1,1,1};
  JPanel y=new JPanel();
  Text.add(y,a,"Matrix A");
  Text.addT(y,b,"Matrix b");
  double x[]=AXB(a,b);
  Text.addT(y,x,"Solution Matrix");
  Text.print(y,"Solution of Gauss(Doolittle) decomposition and elimination");
  }
}
