0
double Determinant(double *X, int N){

    /*Solution*/

} 

int main(void)
{

  double X[] = {3, 1, 2, 
                7, 9, 2, 
                4, 6, 9};

  if (Determinant(X,3) == 164) 
   {
    printf("✓");   
   } 

 }

how to find one dimensional array NxN determinant matrix? Can someone help me? thanks in advance.

Sofune
  • 3
  • 2
  • Since a matrix is a 2D array you mast have a convention to represent it with 1D one. What is it? Does the first three elements are a row? Or a column? – Roberto Caboni Apr 25 '20 at 14:07
  • Please clarify your problem. It is almost impossible to understand. – RobertS supports Monica Cellio Apr 25 '20 at 14:12
  • In the example I gave a one-dimensional, 3 by 3 matrix. {3, 1, 2, 7, 9, 2, 4, 6, 9}; if the result of the determinant is correct, print ✓ – Sofune Apr 25 '20 at 14:15
  • 2
    @RobertoCaboni: when computing a determinant, it does not matter whether the 1D representation is by rows (C order) or columns (Fortran order) because the determinant of the transposate is equal to the determinant of the original matrix. – Serge Ballesta Apr 25 '20 at 15:08

1 Answers1

2

A determinant is commonly computed in a recursive form for N > 2 as SUM(ai0 * det(Xi * (-1)i) where Xi is the sub-matrix obtained by removing the first column and the i-th line. In C language, it can be written as:

double Determinant(double *X, int N) {
    if (N == 2) {                         // trivial for a 2-2 matrix
        return X[0] * X[3] - X[1] * X[2];
    }
    // allocate a sequential array for the sub-matrix
    double *Y = malloc((N - 1) * (N - 1) * sizeof(double));
    // recursively compute the determinant
    double det = 0.;
    for (int k = 0, s = 1; k < N; k++) {
        // build the submatrix
        for (int i = 0, l=0; i < N; i++) {
            if (i == k) continue;
            for (int j = 1; j < N; j++) {
                Y[l++] = X[j + i * N];
            }
        }
        det += X[k * N] * Determinant(Y, N - 1) * s;
        s = -s;
    }
    free(Y);                        // do not forget to de-alloc...
    return det;
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Just extending a bit. This works well for small N, but can perform poorly for a large matrix. In the following question (https://stackoverflow.com/questions/1886280/how-to-find-determinant-of-large-matrix) you can have a good answer regarding this. – luizinho Apr 25 '20 at 15:41