Advertisement
Guest User

Untitled

a guest
May 30th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package project;
  2.  
  3. import java.util.Vector;
  4.  
  5. public class Matrix {
  6.     // Det=a11*(-1)^(1+1)*DetA11+a12*(-1)^(1+2)*DetA12 ...
  7.     // +a1n*(-1)^(1+n)*DetA1n
  8.     // valueInParent is a1n*(-1)^(1+n)
  9.     public double valueInParent = 1;
  10.     // how many threads we can have
  11.     public static int threadMax = 1;
  12.     // all the elements in the matrix.
  13.     // it's supposed to be a 2 dimensional array, but this works the same
  14.     public double[] elements;
  15.     // reference to the parent of this submatrix.
  16.     // only the first matrix doesn't have a parent
  17.     public Matrix parent;
  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.     }
  34.  
  35.     // return a[x,y]
  36.     public double Get(int x, int y) {
  37.         return elements[x + y * size];
  38.     }
  39.  
  40.     // reference to all the threads.
  41.     // each of them needs to know if the others are finished
  42.     public static Vector<ThreadedMatrix> threadedMatrices = new Vector<ThreadedMatrix>();
  43.  
  44.     // function that calculates the determinant of this matrix
  45.     // if the size of this matrix is 3 or bigger, it will return 0 and will push
  46.     // the submatrices into the thread
  47.     public double Determinant(boolean quiet) {
  48.         //remove this matrix from the thread que
  49.         thread.Pop(this);
  50.         if (size == 1) {// shouldn't really happen unless someone specifically
  51.                         // enters a 1 sized matrix
  52.             return elements[0];
  53.         } else if (size == 2) {
  54.             //calculate result if this is the lowest level submatrix
  55.             return elements[0] * elements[3] - elements[1] * elements[2];
  56.         } else {
  57.             //create submatrices for every element in the first row
  58.             for (int i = 0; i < size; i++) {
  59.                 Matrix sub = new Matrix(size - 1);
  60.                 //for a11 the submatrix valueInParent will be (-1)^(i+2)*1*a11=a11
  61.                 sub.valueInParent = Math.pow(-1, i + 2) * Get(i, 0) * valueInParent;
  62.                 sub.parent = this;
  63.                 //set the elements of the submatrix
  64.                 for (int x = 0; x < size; x++) {
  65.                     if (x != i) {
  66.                         for (int y = 1; y < size; y++) {
  67.                             sub.SetElement(Get(x, y), (x > i ? x - 1 : x), y - 1);
  68.                         }
  69.                     }
  70.                 }
  71.                 //see which thread has the least amount of processes to calculate
  72.                 //push this submatrix into it's que
  73.                 ThreadedMatrix smallest = thread;
  74.                 int min = 100;
  75.                 for (ThreadedMatrix _t : threadedMatrices) {
  76.                     if (_t.que.size() < min) {
  77.                         smallest = _t;
  78.                         min = _t.que.size();
  79.                     }
  80.                 }
  81.                 smallest.Push(sub);
  82.             }
  83.             //return 0, the submatrices will be calculated separately
  84.             return 0;
  85.         }
  86.  
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement