mramine364

mat_help

Apr 30th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4.  
  5.  
  6. public class mat_help {
  7.     public final double[][] M;
  8.  
  9.    
  10.     public double[] characteristicPolynomialCoefficients(){
  11.         int n =  M[0].length;
  12.         double[] b = new double[n+1];
  13.         double[] T = new double[n];
  14.         mat_help m = mat_help.I(n);
  15.         for(int i=0;i<n;i++){
  16.             m = m.mul(this);
  17.             T[i] = m.trace();
  18.         }
  19.         b[0] = (n%2==0)? 1 : -1;
  20.         for(int i=1;i<n+1;i++){
  21.             double s = 0;
  22.             for(int j=i-1;j>=0;j--){
  23.                 s += b[j]*T[i-j-1];                
  24.             }
  25.             b[i] = -s/i;
  26.         }
  27.         return b;
  28.     }
  29.  
  30.     public static mat_help I(int n){
  31.         mat_help m = new mat_help(n,n);
  32.         for(int i=0;i<n;i++){
  33.             m.M[i][i] = 1;
  34.         }
  35.         return m;
  36.     }
  37.  
  38.     public mat_help mul(mat_help b){
  39.         mat_help m = new mat_help(M.length, M[0].length);
  40.         for(int i=0;i< M.length;i++){
  41.             for(int j=0;j< M[0].length;j++){
  42.                 double rij = 0;
  43.                 for(int ki=0;ki< M[0].length;ki++){
  44.                     rij += M[i][ki]*b.M[ki][j];
  45.                 }
  46.                 m.M[i][j] = rij;
  47.             }
  48.         }
  49.         return m;
  50.     }
  51.  
  52.     public double trace(){
  53.         double tr = 0;
  54.         for(int i=0;i<M.length;i++){            
  55.             tr += M[i][i];
  56.         }
  57.         return tr;
  58.     }
  59.  
  60.     public mat_help(int n, int m){
  61.         M = new double[n][m];
  62.     }
  63.  
  64.     public mat_help(double[][] t){
  65.         M = new double[t.length][t[0].length];
  66.         for(int i=0;i<t.length;i++){
  67.             for(int j=0;j<t[i].length;j++){
  68.                 M[i][j]= t[i][j];
  69.             }
  70.         }
  71.     }
  72.    
  73.     public static boolean existence(double t[], double a, double b){
  74.         return calc(t, a)*calc(t, b)<=0;
  75.     }
  76.    
  77.     public double[] eigenValues(){
  78.         double[] pc = characteristicPolynomialCoefficients();
  79.         return solve(pc);
  80.     }
  81.  
  82.     public static double[] solve(double[] t){
  83.         //System.out.println("t: "+Arrays.toString(t));
  84.  
  85.         if( t.length==2 )
  86.             return new double[]{-t[1]/t[0]};
  87.         double[] s = solve(prime(t));
  88.         //System.out.println("s: "+Arrays.toString(s));
  89.         int i;
  90.         boolean increasing;
  91.         ArrayList<Double> al = new ArrayList<>();
  92.         if( existence(t, -1e20, s[0]) ){
  93.             increasing = isIncreasing(t, s[0]-1, s[0]);
  94.             al.add(dichotomieSolve(t, -1e20, s[0], increasing));
  95.         }
  96.         for(i=0;i<s.length-1;i++){
  97.             if( existence(t, s[i], s[i+1]) ){
  98.                 increasing = isIncreasing(t, s[i], s[i+1]);
  99.                 al.add(dichotomieSolve(t, s[i], s[i+1], increasing));
  100.             }
  101.         }
  102.         if( existence(t, s[s.length-1], 1e20) ){
  103.             increasing = isIncreasing(t, s[s.length-1], s[s.length-1]+1);
  104.             al.add(dichotomieSolve(t, s[s.length-1], 1e20, increasing));
  105.         }
  106.         double[] r = new double[al.size()]; i=0;
  107.         //System.out.println("al: "+al);
  108.         for(Double d: al){
  109.             r[i] = d; i++;
  110.         }
  111.         return r;
  112.     }
  113.  
  114.     public static double[] prime(double[] t){
  115.         double[] tp = new double[t.length-1];
  116.         for(int i=0;i<t.length-1;i++){
  117.             tp[i] = t[i]*(t.length-1-i);
  118.         }
  119.         return tp;
  120.     }
  121.  
  122.     public static boolean isIncreasing(double t[], double a, double b){
  123.         return calc(t, a)<calc(t, b);
  124.     }
  125.    
  126.     public static double dichotomieSolve(double t[], double a, double b, boolean increasing){
  127.         double n = calc(t, (a+b)/2);
  128.         if( Math.abs(n)<1e-7 )
  129.             return (a+b)/2;
  130.         if( increasing ){
  131.             if( n>0 )
  132.                 return dichotomieSolve(t, a, (a+b)/2, increasing);
  133.             else
  134.                 return dichotomieSolve(t, (a+b)/2, b, increasing);
  135.         }else{
  136.             if( n>0 )
  137.                 return dichotomieSolve(t, (a+b)/2, b, increasing);
  138.             else
  139.                 return dichotomieSolve(t, a, (a+b)/2, increasing);
  140.         }        
  141.     }  
  142.  
  143.     public static double calc(double t[], double X){
  144.         double res = 0;
  145.         for(int k=0;k<t.length;k++){
  146.             if( t[k]!=0 ){
  147.                 res += t[k]*Math.pow(X, t.length-1-k);
  148.             }
  149.         }
  150.         return res;
  151.     }
  152.  
  153.  
  154.     public static void main(String[] args){
  155.         mat_help A = new mat_help(new double[][]{
  156.             {1, 0, -1},
  157.             {0, 1, -1},
  158.             {-1, -1, 2}
  159.         });
  160.         System.out.println(Arrays.toString(A.eigenValues()));
  161.     }
  162.    
  163.    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment