降阶法计算行列式方法有个地方有Bug(原文也已更正,此为更正后部分)

来源:互联网 发布:友价源码怎么样 编辑:程序博客网 时间:2024/04/28 05:03
今天用此函数做方程求解时发现有误,特此更正:
/// <summary>        /// 降阶法计算行列式        /// </summary>        /// <param name="Determinants">N阶行列式</param>        /// <param name="ZeroOptimization">是否0优化</param>        /// <returns>计算结果</returns>        public static decimal CalcDeterminantAij(decimal[,] Determinants, bool ZeroOptimization = false)        {            var theN = Determinants.GetLength(0);            //如果为2阶,直接计算            if (theN == 2)            {                return Determinants[0, 0] * Determinants[1, 1] - Determinants[0, 1] * Determinants[1, 0];            }            if (theN == 1)            {                return Determinants[0, 0];            }            if (theN == 0)            {                throw new Exception("参数错误!");            }            if (ZeroOptimization)            {                //找0最多的行                int theRowIndex = 0;                int theMaxZeroCountR = -1;                for (int i = 0; i < theN; i++)                {                    int theZeroNum = 0;                    for (int j = 0; j < theN; j++)                    {                        if (Determinants[i, j] == 0)                        {                            theZeroNum++;                        }                    }                    if (theZeroNum > theMaxZeroCountR)                    {                        theRowIndex = i;                        theMaxZeroCountR = theZeroNum;                    }                }                //找0最多的列                int theColIndex = 0;                int theMaxZeroCountC = -1;                for (int i = 0; i < theN; i++)                {                    int theZeroNum = 0;                    for (int j = 0; j < theN; j++)                    {                        if (Determinants[j, i] == 0)                        {                            theZeroNum++;                        }                    }                    if (theZeroNum > theMaxZeroCountC)                    {                        theColIndex = i;                        theMaxZeroCountC = theZeroNum;                    }                }                if (theMaxZeroCountR >= theMaxZeroCountC)                {                    decimal theRetDec = 0;                    //第i=theRowIndex+1行展开                    int i = theRowIndex + 1;                    for (int j = 1; j <= theN; j++)                    {                        var theSign = CalcDeterMijSign(i, j);                        var theNewMij = GetDeterminantMij(Determinants, i, j);                        theRetDec += theSign * Determinants[i - 1, j - 1] * CalcDeterminantAij(theNewMij, ZeroOptimization);                    }                    return theRetDec;                }                else                {                    decimal theRetDec = 0;                    //第j=theColIndex+1列展开                    int j = theColIndex + 1;                    for (int i = 1; i <= theN; i++)                    {                        var theSign = CalcDeterMijSign(i, j);                        var theNewMij = GetDeterminantMij(Determinants, i, j);                        theRetDec += theSign * Determinants[i, j] * CalcDeterminantAij(theNewMij, ZeroOptimization);                    }                    return theRetDec;                }            }            else            {                //采用随机法展开一行                var i = new Random().Next(1, theN);                decimal theRetDec = 0;                for (int j = 1; j <= theN; j++)                {                    var theSign = CalcDeterMijSign(i, j);                    var theNewMij = GetDeterminantMij(Determinants, i, j);                    //此处修改theRetDec += theSign * Determinants[i, j] * CalcDeterminantAij(theNewMij, ZeroOptimization);                    theRetDec += theSign * Determinants[i-1, j-1] * CalcDeterminantAij(theNewMij, ZeroOptimization);                }                return theRetDec;            }        }

0 0