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

class NA131A
{ static double y[];
  //===========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;
   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);
     System.out.println(Matrix.toString(y));
   return 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[][]={{2,-3,6},{0,3,-4},{0,2,-3}};
  String s="Orijinal Matrix : \n";
  s+=Matrix.toString(a);
  s+="Dominant eigenvalue = "+eigen_power(a)+"\n";
  s+="Dominant eigenvector = "+Matrix.toString(y);
  JOptionPane.showMessageDialog(null,s,
     "Power method to calculate dominant eigenvalue : ",JOptionPane.PLAIN_MESSAGE);
  }
}
