简体   繁体   中英

How to calculate the determinant of a matrix NxN ? [Recursively]

I've been searching in all over the internet for an algorithm to calculate the determinant of NxN martix recursively . (I do not have any idea about the dimension, so N can be every integer that is less than 256)

complex<double> Matrix::matrixDeterminant(complex<double> **matrix, int n) {

  complex<double> det(0,0);

  complex<double> **submatrix;
  submatrix[i] =  new complex<double>[n]


  for(int i = 0; i< n; i++) {
      submatrix[i] = new complex<double>[n];
    }


   if (n == 2)
      return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
   else {
      for (int x = 0; x < n; x++) {
            int subi = 0;
            for (int i = 1; i < n; i++) {
               int subj = 0;
               for (int j = 0; j < n; j++) {
                  if (j == x)
                  continue;
                  submatrix[subi][subj] = matrix[i][j];
                  subj++;
               }
               subi++;
            }
            det = det + (pow(-1, x) * matrix[0][x] * matrixDeterminant(submatrix, n - 1 ));
      }

   }
     return det;
}


as you can see, the matrix is a Complex matrix which is all the numbers inside are Complex numbers, and also returns complex number.

This method doesn't work. any ideas what to change to make it works?

Use Sarrus' Rule (non recursive method) example on below link is in Javascript, but easily can be written in C https://github.com/apanasara/Faster_nxn_Determinant

nxn Determinant computation thru only two loops

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM