首页 > 解决方案 > 查找大于 3X3 C# 的矩阵的行列式

问题描述

在我的 Matrix 课程中,我能够为最大 3X3 的矩阵编写自己的代码

class Matrix
{
    public float Determinant()
    {
        if (!isSquare())
            return 0;
        else if (_rows == 1)
            return this[0, 0];
        else if (_rows == 2)
        {
                       /* |a  b|
                          |c  d|*/
            float a = this[0, 0];
            float b = this[0, 1];
            float c = this[1, 0];
            float d = this[1, 1];

            return (a * d) - (b * c);
        }
        else
        {
            float sum = 0;
            int i = 0;
            for (int j = 0; j < _cols; j++)
            {
                //finding cofactor
                float a = (float)Math.Pow(-1, i + j);
                float b = (j  % 2 == 0) ? - a * this[i,j] : a * this[i,j];
                Matrix m = subMatrix(i, j);

                //getting determinant by recursion
                float d = m.Determinant();
                sum += b * d;
            }
            return sum;
        }
    }
}

此代码对大于 3X3 的矩阵停止工作。我读过其他人发布的一些类似的帖子,但这些并没有真正帮助我。我不需要勺子喂食的代码,只需要一些解释或者描述我需要做什么的文章。

标签: c#matrixdeterminants

解决方案


推荐阅读