C#,码海拾贝(16)——求行列式值的全选主元高斯消去法,《C#数值计算算法编程》源代码升级改进版

C#,码海拾贝(16)——求行列式值的全选主元高斯消去法,《C#数值计算算法编程》源代码升级改进版_第1张图片

1 高斯消去法

数学上,高斯消元法(或译:高斯消去法),是线性代数规划中的一个算法,可用来为线性方程组求解。但其算法十分复杂,不常用于加减消元法,求出矩阵的秩,以及求出可逆方阵的逆矩阵。不过,如果有过百万条等式时,这个算法会十分省时。一些极大的方程组通常会用迭代法以及花式消元来解决。当用于一个矩阵时,高斯消元法会产生出一个“行梯阵式”。高斯消元法可以用在电脑中来解决数千条等式及未知数。亦有一些方法特地用来解决一些有特别排列的系数的方程组。
消元法是将方程组中的一方程的未知数用含有另一未知数的代数式表示,并将其代入到另一方程中,这就消去了一未知数,得到一解;或将方程组中的一方程倍乘某个常数加到另外一方程中去,也可达到消去一未知数的目的。消元法主要用于二元一次方程组的求解。
核心
1)两方程互换,解不变;
2)一方程乘以非零数k,解不变;
3)一方程乘以数k加上另一方程,解不变。

2 列主元消去法

列主元素消去法是为控制舍入误差而提出来的一种算法,列主元素消去法计算基本上能控制舍入误差的影响,其基本思想是:在进行第 k(k=1,2,...,n-1)步消元时,从第k列的 akk及其以下的各元素中选取绝对值最大的元素,然后通过行变换将它交换到主元素akk的位置上,再进行消元。

高斯消去法从第k步到第k+1步的消元过程,必须满足条件。而这个元素即被称为第k步的主元(素)。显然,高斯消去法是按方程排列的自然顺序产生主元的,这样,一旦出现计算就归于失败,
而且即使,但若其绝对值很小,也将会因用它作除数,引起其他元素的数量级及舍人误差急剧增大,导致最终计算结果不可靠。为了避免在高斯消去法应用中可能出现的这类问题,就发展形成了列主元、全主元等多种消去法。这些方法的基本点在于对高斯消去法的过程作某些技术性修改,全面或局部地选取绝对值最大的元素为主元素,从而构成了相应的主元(素)消去法。列主元(素)消去法以处理简单、相对计算量小的特点,在各类主元消去法中得到最为广泛的应用。

列主元消去法的基本思想是:在进行第 步消元时,从第k列的 及其以下的各元素中选取绝对值最大的元素,然后通过行变换将它交换到主元素 的位置上,再进行消元。

The column main element elimination method is an algorithm proposed to control the round-off error. The column main element elimination method can basically control the influence of round-off error. Its basic idea is: when performing the elimination in step k (k=1,2,..., n-1), select the element with the largest absolute value from the akk and the following elements in column k, and then exchange it to the position of the main element akk through line transformation, and then perform the elimination.

The elimination process of the Gaussian elimination method from step k to step k+1 must meet the conditions. And this element is called the principal element (prime) of step k. Obviously, the Gaussian elimination method generates principal components in the natural order of the equation arrangement, so that once calculations occur, they will fail,

Moreover, even if its absolute value is small, using it as a divisor will cause a sharp increase in the order of magnitude and rounding errors of other elements, resulting in unreliable final calculation results. In order to avoid such problems that may arise in the application of Gaussian elimination methods, various elimination methods such as column principal element and full principal element have been developed. The basic point of these methods is to make certain technical modifications to the process of Gaussian elimination, comprehensively or locally selecting the element with the highest absolute value as the main element, thus forming the corresponding principal component (prime) elimination method. The column principal component (prime) elimination method is widely used among various principal component elimination methods due to its simple processing and relatively low computational complexity.

The basic idea of the column principal element elimination method is to select the element with the highest absolute value from the elements in the k-th column and below, and then exchange it to the position of the main element through row transformation before performing the elimination.

3 求行列式值的全选主元高斯消去法C#源程序

using System;

namespace Zhou.CSharp.Algorithm
{
    /// 
    /// 矩阵类
    /// 作者:周长发
    /// 改进:深度混淆
    /// https://blog.csdn.net/beijinghorn
    /// 
    public partial class Matrix
    {
        /// 
        /// 求行列式值的全选主元高斯消去法
        /// 
        /// 源矩阵
        /// 行列式的值
        public static double ComputeDetGauss(Matrix src)
        {
            int i, j, k, nis = 0, js = 0, z, u, v;
            double f, det, q, d;

            // 初值
            f = 1.0;
            det = 1.0;

            // 消元
            for (k = 0; k <= src.Columns - 2; k++)
            {
                q = 0.0;
                for (i = k; i <= src.Columns - 1; i++)
                {
                    for (j = k; j <= src.Columns - 1; j++)
                    {
                        z = i * src.Columns + j;
                        d = Math.Abs(src[z]);
                        if (d > q)
                        {
                            q = d;
                            nis = i;
                            js = j;
                        }
                    }
                }

                if (Math.Abs(q) < float.Epsilon)
                {
                    det = 0.0;
                    return (det);
                }

                if (nis != k)
                {
                    f = -f;
                    for (j = k; j <= src.Columns - 1; j++)
                    {
                        u = k * src.Columns + j;
                        v = nis * src.Columns + j;
                        d = src[u];
                        src[u] = src[v];
                        src[v] = d;
                    }
                }

                if (js != k)
                {
                    f = -f;
                    for (i = k; i <= src.Columns - 1; i++)
                    {
                        u = i * src.Columns + js;
                        v = i * src.Columns + k;
                        d = src[u];
                        src[u] = src[v];
                        src[v] = d;
                    }
                }

                z = k * src.Columns + k;
                det = det * src[z];
                for (i = k + 1; i <= src.Columns - 1; i++)
                {
                    d = src[i * src.Columns + k] / src[z];
                    for (j = k + 1; j <= src.Columns - 1; j++)
                    {
                        u = i * src.Columns + j;
                        src[u] = src[u] - d * src[k * src.Columns + j];
                    }
                }
            }

            // 求值
            det = f * det * src[src.Columns * src.Columns - 1];

            return (det);
        }
    }
}

POWER BY 315SOFT.COM & TRUFFER.CN

C#,码海拾贝(16)——求行列式值的全选主元高斯消去法,《C#数值计算算法编程》源代码升级改进版_第2张图片

 

你可能感兴趣的:(C#数值计算,Numerical,Recipes,C#入门教程,Beginner‘s,Recipes,c#,算法,开发语言)