import java.io.*;

class NA23A
{    
  public static double [] thomas(double a[][],double r[])
  {
  //
  int n=a.length;
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double x[]=new double[n];
  for(int i=0;i<n;i++)     {f[i]=a[i][i];}
  for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];}
  for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];}
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
      System.out.println("k="+k+" e["+k+"]="+e[k]+"f["+k+"]="+f[k]);
     }
     //forward substitution
     System.out.println("Forward substitution");
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     System.out.println("k="+k+" r["+k+"]="+r[k]);
     }
     //backward substitution
  System.out.println("Backward substitution");
  x[n-1]=r[n-1]/f[n-1]; 
  System.out.println("k="+(n-1)+" x["+(n-1)+"]="+x[n-1]);  
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];
     System.out.println("k="+k+" x["+k+"]="+x[k]);
     }
  return x;      
  }
  
  public static double [] thomas(double f[],double e[],double g[],double r[])
  {
  int n=f.length;
  double x[]=new double[n];
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
      
  public static void main(String args[]) throws IOException
  {
  double a[][]={{0.8,-0.4,0.0},{-0.4,0.8,-0.4},{0,-0.4,0.8}};
  double b[]={41,25,105};
  double x[]=thomas(a,b);
  Text.print(x,"thomas algorithm for 3 band matrix");
  System.out.println("thomas : \n"+Matrix.toStringT(x));
  
  }
}