2

我试图计算一个 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);
4

2 回答 2

4

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

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;
        }
    }
};
于 2018-07-31T00:20:23.880 回答
0

你有一种糟糕的更新方式mn. 您应该在外循环中递增m,并在外循环内i初始化n并在内循环内递增。我认为您的代码会在您编写时工作,但我认为您的条件应该是i < n-1而不是i < n-2. 但是,与其更改最少的字符数以使代码正常工作,我建议重组增量以便不会出现问题。

于 2011-10-26T03:14:51.390 回答