1
class matrix
{
public:
    int nrow;
    int ncol;
    double **m;

    matrix(int r, int c)
    {
        nrow = r; ncol = c;

        m = (double**)malloc(nrow*sizeof(double*));
        for(int i=0; i<nrow; i++)
            m[i] = (double*)malloc(ncol*sizeof(double));

        for(int i=0; i<nrow; i++)
            for(int j=0; j<ncol; j++)
                m[i][j] = 0;
    }

    ~matrix()
    {
        for(int i=0; i<nrow; i++)
        {
            printf("destructor loop: %d\n", i); fflush(stdout);
            free(m[i]);
        }
        free(m);
    }

    matrix(const matrix& that)
    {
        this->nrow = that.nrow;
        this->ncol = that.ncol;

        this->m = (double**)malloc(nrow*sizeof(double*));   // dynamic mem allocation
        for(int i=0; i<this->nrow; i++)
            m[i] = (double*)malloc(ncol*sizeof(double));

        for(int i=0; i<that.nrow; i++)
            for(int j=0; j<that.ncol; j++)
                this->m[i][j] = that.m[i][j]; 
    }

    void operator=(const matrix &that)
    {
        this->nrow = that.nrow;
        this->ncol = that.ncol;

        for(int i=0; i<nrow; i++)  // clear current m
            free(m[i]);
        free(m);

        this->m = (double**)malloc(nrow*sizeof(double*));   // dynamic mem allocation
        for(int i=0; i<this->nrow; i++)
            m[i] = (double*)malloc(ncol*sizeof(double));

        for(int i=0; i<that.nrow; i++)
            for(int j=0; j<that.ncol; j++)
                this->m[i][j] = that.m[i][j]; 
    }

    void show()
    {
        printf("  Marix:\n");
        for(int i=0; i<nrow; i++)
        {
            printf("  ");
            for(int j=0; j<ncol; j++)
            {
                if(m[i][j] >= 0) printf(" ");
                printf("%.6lf ", m[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }

    matrix cofactor(int r, int c) // returns cofactor of a[r][c]
    {
        printf("cofactor: size:(%d, %d) element:(%d, %d)\n", nrow, ncol, r, c); fflush(stdout);

        matrix ans(nrow-1, ncol-1);

        int x = 0, y = 0;
        for(int i=0; i<nrow; i++)
        {
            if(i == r) continue;
            for(int j=0; j<ncol; j++)
            {
                if(j == c) continue;
                ans.m[x][y] = m[i][j];
                y++;
            }
            x++;
        }
        return ans;
    }

    double det()
    {
        if(nrow != ncol)
        {
            printf("non-square matrix: (%d, %d)\n\n", nrow, ncol);
            exit(1);
        }

        if(nrow == 1) return m[0][0]; 
        if(nrow == 2) 
        {
            double ans = m[0][0]*m[1][1] - m[1][0]*m[0][1];
            return ans;
        }

        double ans = 0;
        int sign = 1;
        for(int i=0; i<nrow; i++)
        {
            printf("det[size:%d, %d] loop row: %d\n", nrow, ncol, i); fflush(stdout);
            matrix a(2, 2); 
            a = cofactor(i, 0);
            ans += (sign*a.det());
            sign = sign*(-1);
        }
        return ans;
    }
};

int main()
{
    matrix c(3, 3);
    c.m[0][0] = 2; c.m[0][1] = -3; c.m[0][2] =  1;
    c.m[1][0] = 2; c.m[1][1] =  0; c.m[1][2] = -3;
    c.m[2][0] = 1; c.m[2][1] =  4; c.m[2][2] =  5;

    c.show();
    printf("determinant: %lf\n", c.det());

    return 0;
}

为偶数 3x3 矩阵调用 det 函数时会发生分段错误。我没有使用复杂调试器的经验,但使用 printf 语句看起来运行时错误发生在析构函数中的循环迭代期间。我也尝试过使用向量<向量<双>>而不是双**。我认为在这种情况下它不需要析构函数。然后它给了我一个“双重免费错误”。我了解它何时发生,但在我的代码中找不到错误。我也尝试过计算内联辅因子而不是调用函数,但它没有帮助。

4

2 回答 2

2
    int x = 0, y = 0;
    for(int i=0; i<nrow; i++)
    {
        if(i == r) continue;
        for(int j=0; j<ncol; j++)
        {
            if(j == c) continue;
            ans.m[x][y] = m[i][j];
            y++;
        }
        x++;
    }

递增的逻辑在y这里被打破。做一个简单的心理实验。如果原始矩阵是 3x3,并且它们在此处被简化为2x2矩阵,您的ans, 那么您希望在ans此处初始化每个值,并且

            ans.m[x][y] = m[i][j];
            y++;

将被执行四次。由于y最初设置为 0并且永远不会重置为 0,因此此分配中 used 的值y将在 0 到 3 之间,因此,在某些时候,这将尝试为 分配一些东西ans.m[x][3],当然,这不存在,随之而来的是山岭。

似乎这里明显的意图是重置

  y=0;

作为外for循环中的第一项业务,而不是一开始就执行一次。

于 2020-08-08T02:22:12.217 回答
1

问题出在辅因子函数中。您已int x = 0, y = 0在循环之外声明。在 x (辅因子矩阵的行,ans)的情况下,它很好。但是我们希望每次从新行开始时 y 都为 0,即 y 应该与 j 同步,跳过 when j==c

发生的事情是你跳过第 0 行,因为i==r,然后在下一行 ( i=1),你迭代列,跳过 col 0,因为j==c,然后遍历 col 1,col 2,所以执行的 代码是 ans.m[0][0] = m[1][1],,,,,然后我们就完成了列。y++ans.m[0][1] = m.[1][2]y++

我们增加 x(x=1现在),我们转到下一行,第 2 行(i=2),但是y=2,而不是重置。所以下一行执行这段代码:( ans.m[1][2] = m[2][1]跳过 col 0),它超出了 的范围ans,因为ansncols-1 = 2列。

ans当在辅因子函数的末尾调用析构函数时,这会导致错误。我修复了辅因子代码,这在为 3*3 矩阵调用 det 时不会导致错误。

matrix cofactor(int r, int c) // returns cofactor of a[r][c]
{
    printf("cofactor: size:(%d, %d) element:(%d, %d)\n", nrow, ncol, r, c); fflush(stdout);

    matrix ans(nrow-1, ncol-1);

    int x = 0;
    for(int i=0; i<nrow; i++)
    {
        if(i == r) continue;
        int y = 0;
        for(int j=0; j<ncol; j++)
        {
            if(j == c) continue;
            ans.m[x][y] = m[i][j];
            y++;
        }
        x++;
    }
    return ans;
}

要调试,我会推荐 gdb。您可以使用 onlinegdb.com 将您的代码粘贴到那里,然后单击调试。然后通过 break <line_number> 设置断点,然后运行,n 代表下一条指令,s 代表进入函数。您可以搜索以了解更多信息。

于 2020-08-08T02:20:21.263 回答