<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gauss-Jordan &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/gauss-jordan/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 17:24:53 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]选主元的高斯-约当（Gauss-Jordan）消元法解线性方程组/求逆矩阵</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%85%a8%e9%80%89%e4%b8%bb%e5%85%83%e7%9a%84%e9%ab%98%e6%96%af-%e7%ba%a6%e5%bd%93%ef%bc%88gauss-jordan%ef%bc%89%e6%b6%88%e5%85%83%e6%b3%95%e8%a7%a3%e7%ba%bf%e6%80%a7%e6%96%b9/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%85%a8%e9%80%89%e4%b8%bb%e5%85%83%e7%9a%84%e9%ab%98%e6%96%af-%e7%ba%a6%e5%bd%93%ef%bc%88gauss-jordan%ef%bc%89%e6%b6%88%e5%85%83%e6%b3%95%e8%a7%a3%e7%ba%bf%e6%80%a7%e6%96%b9/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 13 Mar 2011 14:39:15 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Gauss-Jordan]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[最优化]]></category>
		<category><![CDATA[消元]]></category>
		<category><![CDATA[线性方程组]]></category>
		<category><![CDATA[选主元]]></category>
		<category><![CDATA[高斯-约当]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=1288</guid>

					<description><![CDATA[<p>
	&#160;</p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">选主元的高斯-约当（Gauss-Jordan）消元法在很多地方都会用到，例如求一个矩阵的逆矩阵、解线性方程组（插一句：<a href="http://www.codelast.com/?p=29" target="_blank" rel="noopener noreferrer">LM算法</a>求解的一个步骤），等等。它的速度不是最快的，但是它非常<span style="color:#f00;">稳定</span>（<span style="color:#008000;">来自网上的定义：一个计算方法，如果在使用此方法的计算过程中，舍入误差得到控制，对计算结果影响较小，称此方法为数值稳定的</span></span>）<span style="font-family:arial,helvetica,sans-serif;">，同时它的求解过程也比较清晰明了，因而人们使用较多。下面我就用一个例子来告诉你Gauss-Jordan法的求解过程吧。顺便再提及一些注意事项以及扩展话题。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">对本文中所提到的&#8220;主元&#8221;等概念的解释，可以参考<a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">此链接</a>。</span></span></p>
<p>
<span id="more-1288"></span>	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设有如下的方程组：</span></span></p>
<p style="text-align: center; ">
	<span style="font-family:arial,helvetica,sans-serif;"><img decoding="async" alt="equation set" height="141" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_1.png" style="cursor: default; " width="262" /> </span></p>
<p>
	写成矩阵形式就是：AX=B，其中：</p>
<p style="text-align: center; ">
	<img fetchpriority="high" decoding="async" alt="Matrix A &#38; B" height="164" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_2.png" style="cursor: default; " width="339" /></p>
<p>
	<font class="Apple-style-span" face="arial, helvetica, sans-serif">且X=(X<sub>1</sub>, X<sub>2</sub>, X<sub>3</sub>)<sup>T</sup>。</font></p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p>
	<font class="Apple-style-span" face="arial, helvetica, sans-serif">现对矩阵A作</font><a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">初等变换</a><font class="Apple-style-span" face="arial, helvetica, sans-serif">，同时矩阵B也作同样的</font>初等变换，则当A化为单位矩阵的时候，有：</p>
<p style="text-align: center; ">
	<img decoding="async" alt="AX=B" height="124" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_12.png" style="cursor: default; " width="224" /></p>
<p>
	显而易见，我们得到了方程组的解X=(1, 2, 4)<sup>T</sup>。</p>
<p>
	所以，我们要以一定的策略，对A和B施以一系列的<a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">初等变换</a>，当A化为单位矩阵的时候，B就为方程组的解。</p>
<p>
	选主元的G-J消元法通过这样的方法来进行初等变换：在每一个循环过程中，先寻找到主元，并将主元通过行变换（无需列变换）移动到矩阵的主对角线上，然后将主元所在的行内的所有元素除以主元，使得主元化为1；然后观察主元所在的列上的其他元素，将它们所在的行减去主元所在的行乘以一定的倍数，使得主元所在的列内、除主元外的其他元素化为0，这样就使得主元所在的列化为了单位矩阵的形式。这就是一个循环内做的工作。然后，在第二轮循环的过程中，不考虑上一轮计算过程中主元所在的行和列内的元素，在剩下的矩阵范围内寻找主元，然后（如果其不在主对角线上的话）将其移动到主对角线上，并再次进行列的处理，将列化为单位矩阵的形式。余下的步骤依此类推。具体的计算过程的一个例子，请看下面我举的求逆矩阵的过程。</p>
<p>
	<span style="font-family: arial, helvetica, sans-serif; ">如果要解系数矩阵相同、右端向量不同的N个方程组，在设计</span>程序<span style="font-family: arial, helvetica, sans-serif; ">的时候，没有必要&#8221;解N次方程组&#8220;，我们完全可以在程序中，将所有的右端向量以矩阵的数据结构（类似于二维数组）来表示，在系数矩阵作行变换的时候，矩阵里的每一个</span>右端向量也做同样的变换，这样，我们在一次求解运算的过程中，实际上就是同时在解N个方程组了，这是要注意的地方。</p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%85%a8%e9%80%89%e4%b8%bb%e5%85%83%e7%9a%84%e9%ab%98%e6%96%af-%e7%ba%a6%e5%bd%93%ef%bc%88gauss-jordan%ef%bc%89%e6%b6%88%e5%85%83%e6%b3%95%e8%a7%a3%e7%ba%bf%e6%80%a7%e6%96%b9/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	&nbsp;</p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">选主元的高斯-约当（Gauss-Jordan）消元法在很多地方都会用到，例如求一个矩阵的逆矩阵、解线性方程组（插一句：<a href="http://www.codelast.com/?p=29" target="_blank" rel="noopener noreferrer">LM算法</a>求解的一个步骤），等等。它的速度不是最快的，但是它非常<span style="color:#f00;">稳定</span>（<span style="color:#008000;">来自网上的定义：一个计算方法，如果在使用此方法的计算过程中，舍入误差得到控制，对计算结果影响较小，称此方法为数值稳定的</span></span>）<span style="font-family:arial,helvetica,sans-serif;">，同时它的求解过程也比较清晰明了，因而人们使用较多。下面我就用一个例子来告诉你Gauss-Jordan法的求解过程吧。顺便再提及一些注意事项以及扩展话题。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">对本文中所提到的&ldquo;主元&rdquo;等概念的解释，可以参考<a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">此链接</a>。</span></span></p>
<p>
<span id="more-1288"></span>	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设有如下的方程组：</span></span></p>
<p style="text-align: center; ">
	<span style="font-family:arial,helvetica,sans-serif;"><img loading="lazy" decoding="async" alt="equation set" height="141" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_1.png" style="cursor: default; " width="262" /> </span></p>
<p>
	写成矩阵形式就是：AX=B，其中：</p>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Matrix A &amp; B" height="164" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_2.png" style="cursor: default; " width="339" /></p>
<p>
	<font class="Apple-style-span" face="arial, helvetica, sans-serif">且X=(X<sub>1</sub>, X<sub>2</sub>, X<sub>3</sub>)<sup>T</sup>。</font></p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p>
	<font class="Apple-style-span" face="arial, helvetica, sans-serif">现对矩阵A作</font><a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">初等变换</a><font class="Apple-style-span" face="arial, helvetica, sans-serif">，同时矩阵B也作同样的</font>初等变换，则当A化为单位矩阵的时候，有：</p>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="AX=B" height="124" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_12.png" style="cursor: default; " width="224" /></p>
<p>
	显而易见，我们得到了方程组的解X=(1, 2, 4)<sup>T</sup>。</p>
<p>
	所以，我们要以一定的策略，对A和B施以一系列的<a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">初等变换</a>，当A化为单位矩阵的时候，B就为方程组的解。</p>
<p>
	选主元的G-J消元法通过这样的方法来进行初等变换：在每一个循环过程中，先寻找到主元，并将主元通过行变换（无需列变换）移动到矩阵的主对角线上，然后将主元所在的行内的所有元素除以主元，使得主元化为1；然后观察主元所在的列上的其他元素，将它们所在的行减去主元所在的行乘以一定的倍数，使得主元所在的列内、除主元外的其他元素化为0，这样就使得主元所在的列化为了单位矩阵的形式。这就是一个循环内做的工作。然后，在第二轮循环的过程中，不考虑上一轮计算过程中主元所在的行和列内的元素，在剩下的矩阵范围内寻找主元，然后（如果其不在主对角线上的话）将其移动到主对角线上，并再次进行列的处理，将列化为单位矩阵的形式。余下的步骤依此类推。具体的计算过程的一个例子，请看下面我举的求逆矩阵的过程。</p>
<p>
	<span style="font-family: arial, helvetica, sans-serif; ">如果要解系数矩阵相同、右端向量不同的N个方程组，在设计</span>程序<span style="font-family: arial, helvetica, sans-serif; ">的时候，没有必要&rdquo;解N次方程组&ldquo;，我们完全可以在程序中，将所有的右端向量以矩阵的数据结构（类似于二维数组）来表示，在系数矩阵作行变换的时候，矩阵里的每一个</span>右端向量也做同样的变换，这样，我们在一次求解运算的过程中，实际上就是同时在解N个方程组了，这是要注意的地方。</p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">那么，G-J法为什么可以用来求逆矩阵？</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设<span style="color:#00f;">AX=E</span>，其中，A为n阶系数矩阵</span>（与上面的解线性方程组对照）<span style="font-size:14px;">；E为单位矩阵，即E=(</span>e<sub>1</sub>,e<sub>2</sub>,&hellip;,e<sub>n</sub>)，其中e<sub>i&nbsp;</sub>(i=1,2,&hellip;,n) 为单位列向量；X为n个列向量构成的矩阵，即X=(x<sub>1</sub>,x<sub>2</sub>,&hellip;,x<sub>n</sub>)，其中x<sub>i&nbsp;</sub>(i=1,2,&hellip;,n) 为列向量。于是，可以把等式AX=E看成是求解n个线性方程组Ax<sub>i</sub>=e<sub>i</sub>&nbsp;(i=1,2,&hellip;,n)，求出了所有的x<sub>i</sub>之后，也即得到了矩阵X。而由AX=E可知，矩阵X是A的逆矩阵，即X=A<sup>-1</sup>。这样，就求出了A的逆矩阵了。于是，求逆矩阵的过程被化成了解线性方程组的过程，因此我们可以用Gauss-Jordan消元法来求逆矩阵。</span></p>
<p>
	求逆矩阵时，系数矩阵A和单位矩阵E可以共用一块存储区，在每一次约化过程中，系数矩阵逐渐被其逆矩阵替代。</p>
<p>
	&nbsp;</p>
<p>
	在这里，我用一个实际的例子来说明G-J法求逆矩阵的过程：</p>
<p>
	有如下的方程组：</p>
<p style="text-align: center; ">
	<font class="Apple-style-span" face="arial, helvetica, sans-serif"><img loading="lazy" decoding="async" alt="equation set" height="141" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_1.png" width="262" /><br />
	</font></p>
<p>
	显而易见，该方程组对应的系数矩阵A和右端向量矩阵B（此处只有一个右端向量）分别为：</p>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Matrix A &amp; B" height="164" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_2.png" width="339" /></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">其实在求逆矩阵的过程中，矩阵B无关紧要，可以忽略，不过此处还是把它写出来了。下面，把单位矩阵E附在A的右边，构成另一个矩阵（A|E）：</span></span></p>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="(A|E)" height="137" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_3.png" width="339" /></p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">下面，我们就通过矩阵的<a href="http://www.codelast.com/?page_id=963" target="_blank" rel="noopener noreferrer">初等变换</a>，将A化为单位矩阵E，而E则化为了A的逆矩阵。以下是转化步骤：</span></span></p>
<ul>
<li>
		<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">【Step 01】主元选为3，所以将Row1（第一行）与Row2（第二行）交换：</span></span></li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Setp 01" height="154" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_5.png" width="297" /></p>
<ul>
<li>
		【Step 02】主元所在行的所有元素除以主元：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 02" height="170" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_6.png" width="201" /></p>
<ul>
<li>
		【Step 03】Row1 - Row2，Row3 - 2 &times; Row2：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 03" height="189" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_7.png" width="239" /></p>
<p>
	现在，原来的矩阵A有一列被化为了单位阵的形式。</p>
<ul>
<li>
		【Step 04】重新选主元，这一次主元选为5/3，于是Row1 &divide; 5/3（主元所在行的所有元素除以主元）：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 04" height="188" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_8.png" width="245" /></p>
<ul>
<li>
		【Step 05】Row2 - (1/3) &times; Row1，Row3 - (4/3) &times; Row1：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 05" height="188" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_9.png" width="270" /></p>
<p>
	现在，原来的矩阵A又有一列被化为了单位阵的形式。</p>
<ul>
<li>
		【Step 06】重新选主元，这一次主元选为-1/5，于是Row3 &divide; (-1/5)（主元所在行的所有元素除以主元）：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 06" height="188" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_10.png" width="263" /></p>
<ul>
<li>
		【Step 07】Row1 - (2/5) &times; Row3，Row2 - (1/5) &times; Row3：</li>
</ul>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Step 07" height="123" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_11.png" width="272" /></p>
<p>
	现在，原来的矩阵A的所有列都被化为了单位阵的形式。</p>
<p>
	<font class="Apple-style-span" face="arial, helvetica, sans-serif">可见，以上过程非常适合于计算机编程求解。</font></p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p>
	至此，我们完成了从A到E的转换，这个过程中使用了选主元的方法，但没有使用列交换。于是，原来的单位矩阵E就变成了A<sup>-1</sup>，即：</p>
<p style="text-align: center; ">
	<img loading="lazy" decoding="async" alt="Inverse Matrix of A" height="155" src="http://www.codelast.com/wp-content/uploads/2011/03/G-J_Inverse_Matrix_4.png" width="306" /></p>
<p>
	有人说，在进行转化的过程中，如果某一步发现选中的主元为0，怎么办？当然，这种情况就进行不下去了（矩阵是奇异的）。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%85%a8%e9%80%89%e4%b8%bb%e5%85%83%e7%9a%84%e9%ab%98%e6%96%af-%e7%ba%a6%e5%bd%93%ef%bc%88gauss-jordan%ef%bc%89%e6%b6%88%e5%85%83%e6%b3%95%e8%a7%a3%e7%ba%bf%e6%80%a7%e6%96%b9/feed/</wfw:commentRss>
			<slash:comments>15</slash:comments>
		
		
			</item>
	</channel>
</rss>
