求解矩阵特征值-determinant

int cal_N_det(int a[MAX][MAX], int N){

    int det=0;

    int temp[MAX][MAX];

    int index, row, column, i, j;

    if(N==1){  // only one element

        return a[0][0];

        

    } else if(N==2){  // 2*2 matrix

        det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]);

        return det;

        

    } else {

        for(index=0; index<N; index++){

            row = 0;

            column = 0;

            for(i=1; i<N; i++){    // abstract the target matrix while avoiding the column which index on.

                for(j=0; j<N; j++){

                    if(j==index) {

                        continue;    // back to 'for'

                    }

                    temp[row][column] = a[i][j];

                    column++;

                    if(column==N-1){

                      row++;

                      column = 0;

                    }

                }

            }

            det += a[0][index]*pow(-1,index)*cal_N_det(temp,N-1);   // core

      }

      return det;

    }

}

posted on 2020-03-25 13:49  BruceSue  阅读(357)  评论(0编辑  收藏  举报

导航