用C ++计算行列式

时间:2011-10-26 03:04:24

标签: c matrix determinants

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

2 个答案:

答案 0 :(得分:1)

这可能会有所帮助-请查看代码中的注释以获取说明:

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;
        }
    }
};

答案 1 :(得分:0)

您有一种更新mn的简洁方法。您应该在m的外部循环中增加i,并在外部循环中初始化n并在内部循环中递增它。我认为您的代码在编写时会起作用,但我认为您的条件应该是i < n-1而不是i < n-2。但是,我建议不要改变最少数量的字符以使代码工作,而是重建增量,以免出现问题。