计算 C++ 中的行列式

发布于 2024-12-12 05:27:27 字数 1080 浏览 0 评论 0原文

我试图计算 3 * 3 矩阵(或更多)的行列式,矩阵值范围从 (-1, 到 1)。然而,当我计算行列式时,我得到的结果是0。

[...]

srand(time(NULL));
    //Random generation of values between -1 and 1
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            temp = (rand() % (500)) + 0;
            temp = temp/250;
            array[i][j] = (temp - 1);
        }

[...]

double array2[10][10];
double detrm = 0;
int s = 1;
int i, j, m, n, c;

for (c = 0; c < x; c++)
{
    m = 0;
    n = 0;
    for (i = 0; i < x; i++)
    {
        for (j = 0; j < x; j++)
        {
            array2[i][j] = 0;
            if (i != 0 && j != c)
            {
                array2[m][n] = a[i][j];
                if ( n < (x - 2))
                {
                    n++;
                }
                else
                {
                    n = 0;
                    m++;
                }
            }
        }
    }
    detrm = detrm + (s*a[0][c]*determinant(array2, (x - 1)));
    s = -1*s;
}
return(detrm);

I was trying to calculate the determinant of a 3 * 3 matrix (or more) with the matrix values ranging from (-1, to 1). However, I get a result of 0 when I calculate the determinant.

[...]

srand(time(NULL));
    //Random generation of values between -1 and 1
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            temp = (rand() % (500)) + 0;
            temp = temp/250;
            array[i][j] = (temp - 1);
        }

[...]

double array2[10][10];
double detrm = 0;
int s = 1;
int i, j, m, n, c;

for (c = 0; c < x; c++)
{
    m = 0;
    n = 0;
    for (i = 0; i < x; i++)
    {
        for (j = 0; j < x; j++)
        {
            array2[i][j] = 0;
            if (i != 0 && j != c)
            {
                array2[m][n] = a[i][j];
                if ( n < (x - 2))
                {
                    n++;
                }
                else
                {
                    n = 0;
                    m++;
                }
            }
        }
    }
    detrm = detrm + (s*a[0][c]*determinant(array2, (x - 1)));
    s = -1*s;
}
return(detrm);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-12-19 05:27:27

这可能会有所帮助 - 请参阅代码中的注释以获取解释:

static int CalcDeterminant(vector<vector<int>> Matrix)
   {
        //this function is written in c++ to calculate the determinant of matrix
        // it's a recursive function that can handle matrix of any dimension
        int det = 0; // the determinant value will be stored here
        if (Matrix.size() == 1)
        {
            return Matrix[0][0]; // no calculation needed
        }
        else if (Matrix.size() == 2)
        {
            //in this case we calculate the determinant of a 2-dimensional matrix in a 
            //default procedure
            det = (Matrix[0][0] * Matrix[1][1] - Matrix[0][1] * Matrix[1][0]);
            return det;
        }
        else
        {
            //in this case we calculate the determinant of a squared matrix that have 
            // for example 3x3 order greater than 2
            for (int p = 0; p < Matrix[0].size(); p++)
            {
                //this loop iterate on each elements of the first row in the matrix.
                //at each element we cancel the row and column it exist in
                //and form a matrix from the rest of the elements in the matrix
                vector<vector<int>> TempMatrix; // to hold the shaped matrix;
                for (int i = 1; i < Matrix.size(); i++)
                {
                    // iteration will start from row one cancelling the first row values
                    vector<int> TempRow;
                    for (int j = 0; j < Matrix[i].size(); j++)
                    {
                        // iteration will pass all cells of the i row excluding the j 
                        //value that match p column
                        if (j != p)
                        {
                           TempRow.push_back(Matrix[i][j]);//add current cell to TempRow 
                        }
                    }
                    if (TempRow.size() > 0)
                        TempMatrix.push_back(TempRow);
                    //after adding each row of the new matrix to the vector tempx
                    //we add it to the vector temp which is the vector where the new 
                    //matrix will be formed
                }
                det = det + Matrix[0][p] * pow(-1, p) * CalcDeterminant(TempMatrix);
                //then we calculate the value of determinant by using a recursive way
                //where we re-call the function by passing to it the new formed matrix
                //we keep doing this until we get our determinant
            }
            return det;
        }
    }
};

This may Help - see comments within the code for an explanation:

static int CalcDeterminant(vector<vector<int>> Matrix)
   {
        //this function is written in c++ to calculate the determinant of matrix
        // it's a recursive function that can handle matrix of any dimension
        int det = 0; // the determinant value will be stored here
        if (Matrix.size() == 1)
        {
            return Matrix[0][0]; // no calculation needed
        }
        else if (Matrix.size() == 2)
        {
            //in this case we calculate the determinant of a 2-dimensional matrix in a 
            //default procedure
            det = (Matrix[0][0] * Matrix[1][1] - Matrix[0][1] * Matrix[1][0]);
            return det;
        }
        else
        {
            //in this case we calculate the determinant of a squared matrix that have 
            // for example 3x3 order greater than 2
            for (int p = 0; p < Matrix[0].size(); p++)
            {
                //this loop iterate on each elements of the first row in the matrix.
                //at each element we cancel the row and column it exist in
                //and form a matrix from the rest of the elements in the matrix
                vector<vector<int>> TempMatrix; // to hold the shaped matrix;
                for (int i = 1; i < Matrix.size(); i++)
                {
                    // iteration will start from row one cancelling the first row values
                    vector<int> TempRow;
                    for (int j = 0; j < Matrix[i].size(); j++)
                    {
                        // iteration will pass all cells of the i row excluding the j 
                        //value that match p column
                        if (j != p)
                        {
                           TempRow.push_back(Matrix[i][j]);//add current cell to TempRow 
                        }
                    }
                    if (TempRow.size() > 0)
                        TempMatrix.push_back(TempRow);
                    //after adding each row of the new matrix to the vector tempx
                    //we add it to the vector temp which is the vector where the new 
                    //matrix will be formed
                }
                det = det + Matrix[0][p] * pow(-1, p) * CalcDeterminant(TempMatrix);
                //then we calculate the value of determinant by using a recursive way
                //where we re-call the function by passing to it the new formed matrix
                //we keep doing this until we get our determinant
            }
            return det;
        }
    }
};
憧憬巴黎街头的黎明 2024-12-19 05:27:27

您有一种更新 mn 的粗暴方法。您应该在外循环中对 i 递增 m,并在外循环中初始化 n 并在内循环中递增它。我认为你的代码会像你编写的那样工作,但我认为你的条件应该是 i i i i i i i i i i i n-1 而不是 i n-2。但我建议不要更改最少数量的字符来使代码正常工作,而是重新调整增量,以免出现问题。

You have a crufty way of updating m and n. You should increment m in the outer loop over i, and initialize n within the outer loop and increment it within the inner loop. I think that your code would work as you wrote it, but I think that your conditional should have been i < n-1 instead of i < n-2. But instead of changing the fewest number of characters to get the code to work, I recommend restructuring the increments so that the issue doesn't arise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文