Guest User

Untitled

a guest
Jun 3rd, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package project;
  2.  
  3. import java.util.Random;
  4. import java.util.Vector;
  5.  
  6. public class Matrix {
  7.     // leftover matrices that haven't been processed yet
  8.     public static Vector<Matrix> matricesToBeProcessed = new Vector<Matrix>();
  9.     // Det=a11*(-1)^(1+1)*DetA11+a12*(-1)^(1+2)*DetA12 ...
  10.     // +a1n*(-1)^(1+n)*DetA1n
  11.     // valueInParent is a1n*(-1)^(1+n)
  12.     public double valueInParent = 1;
  13.     // how many threads we can have
  14.     public static int threadMax = 1;
  15.     // all the elements in the matrix.
  16.     // it's supposed to be a 2 dimensional array, but this works the same
  17.     public double[] elements;
  18.     // how many rows/columns this matrix has
  19.     int size;
  20.     // reference to the thread that's calculating this matrix.
  21.     // necessary in order to clear the que of that thread
  22.     public ThreadedMatrix thread;
  23.  
  24.     // a[x,y]=val
  25.     public void SetElement(double val, int x, int y) {
  26.         elements[size * y + x] = val;
  27.     }
  28.  
  29.     // constructor
  30.     public Matrix(int _size) {
  31.         size = _size;
  32.         elements = new double[size * size];
  33.         matricesToBeProcessed.add(this);
  34.     }
  35.  
  36.     // return a[x,y]
  37.     public double Get(int x, int y) {
  38.         return elements[x + y * size];
  39.     }
  40.  
  41.     // reference to all the threads.
  42.     // each of them needs to know if the others are finished
  43.     public static Vector<ThreadedMatrix> threadedMatrices = new Vector<ThreadedMatrix>();
  44.  
  45.     // how many processes are left
  46.     public static int numberOfProcesses;
  47.  
  48.  
  49.     //generate submatrices
  50.     //if the size of this one is 2, add a result to the array of results instead
  51.     public void run() {
  52.         // remove this matrix from the thread que
  53.         thread.Pop(this);
  54.         if (size == 2) {
  55.             // no longer needs to be processed
  56.             matricesToBeProcessed.remove(this);
  57.             // calculate result if this is the lowest level submatrix
  58.             ThreadedMatrix.results[numberOfProcesses - 1] = (elements[0] * elements[3] - elements[1] * elements[2])
  59.                     * valueInParent;
  60.             // decrement remaining processes
  61.             Matrix.numberOfProcesses--;
  62.         } else {
  63.             //see which thread has the smallest amount of matrices
  64.             ThreadedMatrix smallest=thread;
  65.             for(ThreadedMatrix thread: threadedMatrices)
  66.             {
  67.                 if(thread.que.size()<smallest.que.size())
  68.                 {
  69.                     smallest=thread;
  70.                     }
  71.             }
  72.             // create submatrices for every element in the first row
  73.             for (int i = 0; i < size; i++) {
  74.                 Matrix sub = new Matrix(size - 1);
  75.                 // for a11 the submatrix valueInParent will be
  76.                 // (-1)^(i+2)*1*a11=a11
  77.                 sub.valueInParent = Math.pow(-1, i + 2) * Get(i, 0) * valueInParent;
  78.                 // set the elements of the submatrix
  79.                 for (int x = 0; x < size; x++) {
  80.                     if (x != i) {
  81.                         for (int y = 1; y < size; y++) {
  82.                             sub.SetElement(Get(x, y), (x > i ? x - 1 : x), y - 1);
  83.                         }
  84.                     }
  85.                 }
  86.                 // push this submatrix into it's que
  87.                 smallest.Push(sub);
  88.  
  89.             }
  90.             matricesToBeProcessed.remove(this);
  91.         }
  92.  
  93.     }
  94. }
Add Comment
Please, Sign In to add comment