zoukankan      html  css  js  c++  java
  • 降阶法计算行列式方法有个地方有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;
                }
            }

  • 相关阅读:
    Python字典处理技巧
    javascript常用对象
    8. 异步操作
    九度OnlineJudge之1022:游船出租
    直方图(下)
    MySQL中关于日期、时间的数据类型和函数
    libvirt(virsh命令介绍)
    11g的alert日志路径
    使用GridView来获取xml文件数据
    MediaPlayer视频播放器
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5382548.html
Copyright © 2011-2022 走看看