import java.io.*;

class NA14
{
  public static double[] gauss(double a[][],double b[])
  {
  int n=b.length;	  
  double x[]=new double[n];
  double carpan=0;
  double toplam=0;
  //gauss eleme
  int i,j,k;
  System.out.println("Original matrix :\n"+Matrix.toString(a));
  for(k=0;k<(n-1);k++)
  {
    for(i=k+1;i<n;i++)
    {  carpan=a[i][k]/a[k][k];
       a[i][k]=0;
       for(j=k+1;j<n;j++)
       {   a[i][j]-=carpan*a[k][j]; }
           b[i]   =b[i]   -carpan*b[k];
           
	}
	System.out.println("A :\n"+Matrix.toString(a));
	System.out.println("b:\n"+Matrix.toString(b));
  }
  
  //back substituting
  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=toplam+a[i][j]*x[j];}
    x[i]=(b[i]-toplam)/a[i][i];
  }
  return x;
  }
    
    
  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 x[]=gauss(a,b);
  Text.print(x,"Gauss elimination solution matrix");
  }
}