class simplex2 
{ 
      public static double[] simplex(double[][] a)
      {
      int n=a.length; //satır
      int m=a[0].length; //sütun
      System.out.println("m="+m+"n="+n);
      int m1=m-1;//x,y lerin sayısı
      int n1=n-1;
      int m2=m1+n1;
      int m3=m2+1;
      int keyrow=0;
      int keycolumn=0;
      double keynumber=0.0;
      double ratio=1.0;
      double b[][]=new double[n][m3];
      double d[]=new double[m];
      String s[]=new String[m];
      for(int i=0;i<n;i++)
      { b[i][m2]=a[i][m1]; }
      for(int i=0;i<n-1;i++)
        {
	    for(int j=0;j<m1;j++) {b[i][j]=a[i][j];}              
        for(int j=m1;j<m2;j++) {if(i==(j-m1)) b[i+1][j]=1.0;System.out.println("i="+i+"j="+j+"bij="+b[i+1][j]);}  
        }      
      double max;
      double min;
      System.out.println(Matrix.toString(b));
      for(int k=0;k<n1;k++)
      {
	  max=-9.999e30;
      min=9.999e30;    
      for(int j=0;j<m1;j++)
      {if( (b[0][j]>0) && Math.abs(b[0][j])>max) {max=Math.abs(b[0][j]);keycolumn=j;}} 
      for(int i=1;i<n;i++)
      { ratio=b[i][m2]/b[i][keycolumn];
        if(ratio<min)  {min=ratio;keyrow=i;}
      }
      keynumber=b[keyrow][keycolumn]; 
      double c[][]=new double[n][m3];
      for(int i=0;i<n;i++)
      { for(int j=0;j<m3;j++) {c[i][j]=b[i][j];}} 
      for(int i=0;i<n;i++)
      { if(i!=keyrow) {for(int j=0;j<m3;j++) {b[i][j]=c[i][j]-c[keyrow][j]*c[i][keycolumn]/keynumber;}} 
        else {for(int j=0;j<m3;j++) {b[i][j]=c[i][j]/keynumber;}} 
      }
      System.out.println("keynumber = "+keynumber);
      System.out.println("keyrow = "+keyrow+" keycolumn = "+keycolumn);	  
      System.out.println(Matrix.toString(b));
      }

      d[m1]=b[0][m2];
      s[m1]="y=f(x)";
      for(int j=0;j<m1;j++) 
          { s[j]="x["+j+"]";}
      for(int i=1;i<n;i++)
      {
          for(int j=0;j<m1;j++) 
          {    
	              if((b[i][j]==1.0)&& (b[0][j]==0.0)) 
                  { d[j]=b[i][m2];}              	              
          }
	      
      }
      Text.print(d,s,"Simplex algorithm");
      return d;
      }
	  public static void main(String arg[])
	  { 
	  //double a[][]=Text.readDouble_TextArea();
	  double a[][]={{-60,-50,0},{2,4,80},{3,2,60}};
	  double d[]=simplex(a);
	  System.out.println(Matrix.toString(d));
	  }
 }
