简体   繁体   English

在 C 中获取 N x N 矩阵行列式

[英]Getting N x N matrix determinant in C

I've been trying to compute an N x N matrix determinant, so that this is what I've got so far.我一直在尝试计算一个 N x N 矩阵行列式,所以这就是我到目前为止所得到的。 The result of if it's -378029970 .如果是-378029970的结果。 I don't know what is going on because if I change the N value for the matrix size it works fine and prints the right result which is -20 .我不知道发生了什么,因为如果我更改矩阵大小的 N 值,它可以正常工作并打印正确的结果-20 I debugged the whole script and found out the problem is when I change the N value.我调试了整个脚本,发现问题出在我更改 N 值时。 I'm kind new in C language so I appreciate if you could give me a hand with it.我是 C 语言的新手,所以如果你能帮我一下,我很感激。 Thanks!谢谢!

void getCofactor(int q, int n, int matrix[][n], int temp[][n]) {
    int i = 0;
    int j = 0;
    int p = 0;

    for (int row = 0; row < n; row++) {
        for (int col = 0; col < n; col++) {
            if (row != p && col != q) {
                temp[i][j++] = matrix[row][col];
                if (j == n - 1) {
                    j = 0;
                    i++;
                }
            }
        }
    }
}

int determinante(int n, int matrix[][n]) {
    int D = 0;

    if (n == 1) {
        return matrix[0][0];
    }

    int temp[n][n];
    int sign = 1;

    for (int f = 0; f < n; f++) {
        getCofactor(f, n, matrix, temp);

        D += sign * matrix[0][f] * determinante(n - 1, temp);

        sign = -sign;
    }
    return D;
}

int main() {

    int matrix[3][3] = { { 3, -2, 5}, { -2, 8, 10}, { 3, -2, 4 }};
    int LINHA = 3;

    printf("Determinante: %d", determinante(LINHA, matrix));

    return 0;
}

The problem is temp is defined as int temp[n][n] but this matrix' dimensions are incompatible when passed to determinante(n - 1, temp) .问题是temp被定义为int temp[n][n]但是这个矩阵的维度在传递给determinante(n - 1, temp)时是不兼容的。

You should modify the definition of temp to reduce the size and modify the prototype of getCofactor to reflect the actual dimensions of matrix and temp .您应该修改temp的定义以减小大小并修改getCofactor的原型以反映matrixtemp的实际维度。

Here is a modified version:这是一个修改后的版本:

#include <stdio.h>
#include <stdlib.h>

void getCofactor(int q, int n, int matrix[][n], int temp[][n - 1]) {
    int i = 0;
    int j = 0;
    int p = 0;

    for (int row = 0; row < n; row++) {
        for (int col = 0; col < n; col++) {
            if (row != p && col != q) {
                temp[i][j++] = matrix[row][col];
                if (j == n - 1) {
                    j = 0;
                    i++;
                }
            }
        }
    }
}

int determinante(int n, int matrix[][n]) {
    if (n == 1) {
        return matrix[0][0];
    }

    int temp[n - 1][n - 1];
    int sign = 1;
    int D = 0;

    for (int f = 0; f < n; f++) {
        getCofactor(f, n, matrix, temp);
        D += sign * matrix[0][f] * determinante(n - 1, temp);
        sign = -sign;
    }
    return D;
}

int main() {
    int matrix[3][3] = { { 3, -2, 5}, { -2, 8, 10}, { 3, -2, 4 } };
    int LINHA = sizeof(matrix) / sizeof(matrix[0]);

    printf("Determinante: %d\n", determinante(LINHA, matrix));
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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