Professor Java
Your Ad Here

Matrix Determinant Calculator






The Matrix Determinant Calculator calculates determinants of all size matrixes: 1x1, 2x2, 3x3, 4x4, 5x5, 6x6, 7x7, 8x8, 9x9, 10x10... to any size you want. Sample input is already placed in the text box. There can be decimal point values, and negatives. Values are seperated by spaces, and each line is a line of the matrix. Do not press space after the last value of each line. Erase input and try your own matrix! Note: a matrix has the same amount of values in each row and column (a square). Inputting a non-square will not give you a valid answer.


Matrix Determinant

public static int determinant(int[][] matrix){ //method sig. takes a matrix (two dimensional array), returns determinant.
    int sum=0; 
    int s;
    if(matrix.length==1){  //bottom case of recursion. size 1 matrix determinant is itself.
      return(matrix[0][0]);
    }
    for(int i=0;i<matrix.length;i++){ //finds determinant using row-by-row expansion
      int[][]smaller= new int[matrix.length-1][matrix.length-1]; //creates smaller matrix- values not in same row, column
      for(int a=1;a<matrix.length;a++){
        for(int b=0;b<matrix.length;b++){
          if(b<i){
            smaller[a-1][b]=matrix[a][b];
          }
          else if(b>i){
            smaller[a-1][b-1]=matrix[a][b];
          }
        }
      }
      if(i%2==0){ //sign changes based on i
        s=1;
      }
      else{
        s=-1;
      }
      sum+=s*matrix[0][i]*(determinant(smaller)); recursive step: determinant of larger determined by smaller.
    }
    return(sum); //returns determinant value. once stack is finished, returns final determinant.
  }

Definitely among my favorite algorithms. Learned "Row by row expansion" in my math class (see wiki page for details) and decided to implement a recursive solution for a determinant. Pretty self-explanatory if you know the method, It basically finds the determinant of a 4x4 matrix by summing and multiplying the determinants of 4 3x3 matrixes, and so on. Using the data structure stack in java, which allows returns to help find the larger determinant until it is finished, returning the final answer. Note that the code becomes progressively slower as the size of the matrix increases. Not quite exponentially slower, the speed of the algorithm is more of O(n!). For a size 7 matrix, you need 7! recursions, and for a size 10 matrix, you need 10! recursions. The javascript matrix determinant calculator above is very useful. It also takes decimal values, and is more quick because once the recursion goes down to 3x3, it is returned using a formula instead of repeating recursion, making it much faster for very large matrixes. It finds the determinant for any size matrix.

website-hit-counters.com
Provided by website-hit-counters.com site.
Your Ad Here