Advertisement
Guest User

Untitled

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