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

news/2024/9/23 18:25:42/

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
{/// <summary>/// 矩阵类/// 作者:周长发/// 改进:深度混淆/// https://blog.csdn.net/beijinghorn/// </summary>public partial class Matrix{/// <summary>/// 求行列式值的全选主元高斯消去法/// </summary>/// <param name="src">源矩阵</param>/// <returns>行列式的值</returns>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

 


http://www.ppmy.cn/news/42980.html

相关文章

基础设施建设发展的几个阶段

1、完全托管 IDC 模式 与电信运营商或者第三方 ISP 合作&#xff0c;租赁其 IDC 机房中的机柜。而其他主机硬件和网络设备都是企业自行采购&#xff0c;然后放入机房中进行托管。 2、资源短期租赁模式 峰值流量的激增&#xff0c;导致短时资源需求量庞大。如果再靠一次性采购…

【MyBatis Plus】001 -- MyBatis-Plus快速入门(介绍、QuickStart)

目录 1、了解MyBatis-Plus 1.1 MyBatis-Plus介绍 1.2 代码及文档 1.3 特性 1.4 架构 1.5 作者 2、快速开始 2.1 创建数据库以及表 2.2 创建工程 2.3 MyBatis MP 2.3.1 创建子module 2.3.2 MyBatis实现查询User&#xff08;无Service方法&#xff0c;直接通过Mapper实现查询&am…

FE_CSS CSS 的三大特性

1 层叠性 相同选择器给设置相同的样式&#xff0c;此时一个样式就会覆盖&#xff08;层叠&#xff09;另一个冲突的样式。层叠性主要解决样式冲突的问题 层叠性原则&#xff1a; 样式冲突&#xff0c;遵循的原则是就近原则&#xff0c;哪个样式离结构近&#xff0c;就执行哪个…

reviewSpringBoot

1.springboot简介说明 Springboot简化新Spring应用的初始搭建以及开发过程 SpringBoot是基于Spring的框架&#xff0c;该框架使用了特定的方式来进行配置&#xff0c;从而使开发人员不再需要定义样板化的配置。 SpringBoot集成了绝大部分目前流行的开发框架&#xff0c;就像…

LeetCode算法小抄--O(1)时间下删除-查找数组中任意元素

LeetCode算法小抄O(1)时间下删除-查找数组中任意元素[380. O(1) 时间插入、删除和获取随机元素](https://leetcode.cn/problems/insert-delete-getrandom-o1/)[710. 黑名单中的随机数](https://leetcode.cn/problems/random-pick-with-blacklist/)[hard]⚠申明&#xff1a; 未经…

「VS」Visual Studio 常用小技巧

目录指定代码不编译设置选中项目为启动项代码区显示行号新建垂直文档组生成后将dll复制到指定目录指定代码不编译 说明&#xff1a;在项目开发时&#xff0c;有时候已经将代码加入到项目中&#xff0c;但有不想要编译时可以一下操作。 文件处右键→属性→常规→从生成中排除→选…

【项目分析】基于工艺融合的数控编程方法的设计与实现

系列综述&#xff1a; &#x1f49e;目的&#xff1a;本系列是个人整理为了秋招项目的&#xff0c;按照面试常问及项目核心点整理 &#x1f970;来源&#xff1a;该项目源于数控系统迭代的实验项目 &#x1f92d;结语&#xff1a;如果有帮到你的地方&#xff0c;就点个赞和关注…

2023年14界蓝桥杯省赛题解

2023年14界蓝桥杯省赛题解 蒟蒻笔者大二&#xff0c;第一次省赛。总结一下&#xff1a;“300块没了&#xff0c;退钱&#xff01;” A、日期统计 问题描述 小蓝现在有一个长度为 100 的数组&#xff0c;数组中的每个元素的值都在 0 到 9 的范围之内。数组中的元素从左至右如…