在C ++中计算行列式 [英] Calculating the Determinant in C++

查看:183
本文介绍了在C ++中计算行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算3 * 3矩阵(或更多)的行列式,矩阵值的范围为(-1,至1).但是,计算行列式时得到的结果为0.

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

推荐答案

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

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

这篇关于在C ++中计算行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆