import java.io.*;
import javax.swing.*;


class SCO13D
{
  //===========Eigen value calculations ==============

//*************norm methods definition****************
public static double norm(double v[])
{
// vector norm
double total=0;;
for(int i=0;i<v.length;i++)
  {total+=v[i]*v[i];}
return Math.sqrt(total);
}

  public static double norm(double v[],int p)
  {
   //p vector norm
  if(p==0) p=1;
  double total=0;
  double x,y;
  for(int i=0;i<v.length;i++)
    {
    x=Math.abs(v[i]);
    total+=Math.pow(x,p);
    }
  return Math.pow(total,(1.0/p));
  }
  public static double norminf(double v[])
    {
     //infinite vector norm
    double x;
    double max=0;
    for(int i=0;i<v.length;i++)
      {
      x=Math.abs(v[i]);
      if(x>max) max=x;
      }
    return max;
    }
   public static double norminf(double a[][])
        {
          //infinite matrix norm
          double x;
          double max = 0;
          double total;
        int i,j;

        int n=a.length;
        for(i=0;i<n;i++)
          {
          total=0;
          for(j=0;j<n;j++)
          {total+=Math.abs(a[i][j]);}
          x=total;
          if(x>max) max=x;
          }
        return max;
        }
   public static double norm(double a[][])
       {
       //matrix norm
                double x;
                double max = 0;
                double total;
                int i,j;
                int n=a.length;
                for(j=0;j<n;i++)
                  {
                  total=0;
                  for(i=0;i<n;j++)
                  {total+=Math.abs(a[i][j]);}
                  x=total;
                  if(x>max) max=x;
                  }
                return max;
      }
   public static double normE(double a[][])
             {
             //Euclidian matrix norm
             double x;
             double total;
             int i,j;
             total=0;
             int n=a.length;
             for(j=0;j<n;i++)
             {for(i=0;i<n;j++) {total+=a[i][j]*a[i][j];}}
             return Math.sqrt(total);
            }
  public static double[] multiply(double[][] left,double[] right)
  {
  //multiplication of one matrix with one vector
  int ii,jj,i,j,k;
  int m1=left[0].length;
  int n1=left.length;
  int m2=right.length;
  double[] b;
  b=new double[m2];
  if(n1 != m2) {
  System.out.println("inner matrix dimensions must agree");
  for(ii=0;ii<n1;ii++)
   {b[ii]=0;}
   return b;   }
 for(i=0;i<m1;i++)
   { b[i]=0;
     for(k=0;k<n1;k++)
	 b[i]+=left[i][k]*right[k];}
 return b;
 //end of multiply of a matrix and a vector
 }

  public static double  YT_X(double [] left,double [] right)
  {
  //multiplys a vector transpose with a vector	
  int n=left.length;
  double tot=0;
  for(int i=0;i<n;i++)
  {
  tot+=left[i]*right[i];
  }
  return tot;  
  }
          
  public static double eigen_power(double A[][])
  {
   double eps=1e-15;
   double err=1;
   int m=100;
   int n=A.length;
   double y[]=new double[n];
   double r[]=new double[n];
   double xnorm;
   double mu=1.0;
   for(int i=0;i<n;i++) y[i]=1.0;
   int k=0;
   double mueski;
   double x[]=new double[n];
   do{  
	    mueski=mu;
	    x=multiply(A,y);
	    xnorm=norminf(x);
	    for(int i=0;i<n;i++) {y[i]=x[i]/xnorm;}
	    mu=YT_X(y,x)/YT_X(y,y);
	    for(int i=0;i<n;i++) {r[i]=mu*y[i]-x[i];}
	    k++;
	    err=Math.abs((mu-mueski)/mu);
     }while(err>eps);
   return mu;	  
  }
    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[][] gaussLU(double ai[][],int o[])
  { //kısmi pivotlu gauss eleme yöntemi
  int n=ai.length;	    
  double a[][]=new double[n][n];
  double s[]=new double[n];
  double carpan=0;
  double toplam=0;
  double absaij;

  //gauss eleme
  int i,j,k,p,ii,jj;
  //System.out.println("Orijinal Matris :\n"+Matrix.toString(ai));
  for(i=0;i<n;i++)
  {for(j=0;j<n;j++){a[i][j]=ai[i][j];}}
  for(i=0;i<n;i++)
  { o[i]=i;s[i]=Math.abs(a[i][0]);
    for(j=1;j<n;j++)
      { absaij=Math.abs(a[i][j]);
        if(absaij>s[i]) {s[i]=absaij;}
      }
  }
  for(k=0;k<(n-1);k++)
  { pivot(a,o,s,k);
	//pivot referansını değiştir		
	//System.out.println("pivotlu Matris : k="+k+"\n"+Matrix.toString(a));
	//gauss elemeyi çözme  
    for(i=k+1;i<n;i++)
    {  carpan=a[o[i]][k]/a[o[k]][k];
       a[o[i]][k]=carpan;
       for(j=k+1;j<n;j++)
       {   a[o[i]][j]-=carpan*a[o[k]][j]; }
	}
  }
  return a;
  }
  
  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 [] AXB(double a[][],double b[])
  {
  //denklem sistemi çözümü b vektörü için	  
  int n=a.length;	  
  int q[]=new int[n];
  double c[][]=gaussLU(a,q);
  double s[]=yerine_koyma(c,b,q);
  return s;
  }
  
  public static double [][] AXB(double a[][],double b[][])
  {//denklem sistemi çözümü b matrisi için	  
  int n=a.length;	  
  int q[]=new int[n];
  double c[][]=gaussLU(a,q);
  double s[][]=yerine_koyma(c,b,q);
  return s;
  }
  
  public static double eigen_inversepower(double A[][])
  {
   double eps=1e-15;
   double err=1;
   int m=100;
   int n=A.length;
   double y[]=new double[n];
   double r[]=new double[n];
   int o[]=new int[n];
   double B[][]=new double[n][n];
   double xnorm;
   double mu=1.0;
   for(int i=0;i<n;i++) y[i]=1.0;
   int k=0;
   double mueski;
   double x[]=new double[n];
   do{  
	    mueski=mu;
	    B=gaussLU(A,o);
	    x=yerine_koyma(B,y,o);
	    xnorm=norminf(x);
	    for(int i=0;i<n;i++) {y[i]=x[i]/xnorm;}
	    x=yerine_koyma(B,y,o);
	    mu=YT_X(y,x)/YT_X(y,y);
	    for(int i=0;i<n;i++) {r[i]=mu*y[i]-x[i];}
	    k++;
	    err=Math.abs((mu-mueski)/mu);
     }while(err>eps);
   return 1.0/mu;	  
  }
  
  
  public static void main(String args[]) throws IOException
  {
  //Eigen Value calculations using  method
  //double a[][]={{3.556,-1.778,0},{-1.778,3.556,-1.778},{0,-1.778,3.556}};
  //double a[][]={{-3.9,-0.6,-3.9,0.4},{1,0,0,0},{0,1,0,0},{0,0,1,0}};
  //double a[][]={{-1,2},{2,2}};
  double a[][]={{1,-2,3},{-2,4,1},{3,1,2}};
  String s="Orijinal Matris : \n";
  s+=Matrix.toString(a);
  s+="en küçük sınır değer = "+eigen_inversepower(a);
  JOptionPane.showMessageDialog(null,s,
     "En küçük sınır değer metodu : ",JOptionPane.PLAIN_MESSAGE);
  }
}
