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

今天用此函数做方程求解时发现有误,特此更正:
/// <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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值