矩阵 LUP 分解 解线性方程组 求行列式值 矩阵求逆 算法说解 |
您所在的位置:网站首页 › 矩阵求逆解线性方程组 › 矩阵 LUP 分解 解线性方程组 求行列式值 矩阵求逆 算法说解 |
算法:矩阵 LUP 分解 本文着笔于矩阵 LUP 分解算法,以及利用矩阵的 LUP 分解来解线性方程组、求矩阵对应行列式的值、求逆矩阵。 对于矩阵的定义代码如下: struct Matrix { double dat[MAX_N][MAX_N],det,LUdat[MAX_N][MAX_N],rev[MAX_N][MAX_N]; int dgr,pi[MAX_N]; bool LUP_Decomposition(); VectorColumn LUP_Solve(VectorColumn); void GetDeterminant(); void GetReverse(); }; 矩阵的 LUP 分解矩阵 A 的 LUP 分解即为找到下三角矩阵 L、上三角矩阵 U、行置换矩阵 P 使得 PA = LU ,实现方法为高斯消元。 E = I = ( 1 0 ⋯ 0 0 1 ⋯ 0 ⋮ ⋮ ⋱ ⋮ 0 0 ⋯ 1 ) , e i j = { 1 i = j 0 i ≠ j \begin{matrix} E \end{matrix} = \begin{matrix} I \end{matrix} = \begin{pmatrix} 1 ; 0 ; \cdots ; 0 \\ 0 ; 1 ; \cdots ; 0 \\ \vdots ; \vdots ; \ddots ; \vdots \\ 0 ; 0 ; \cdots ; 1 \end{pmatrix}, e_{ij} = \begin{cases} 1 ; i = j \\0 ; i \neq j \end{cases} E=I=⎝⎜⎜⎜⎛10⋮001⋮0⋯⋯⋱⋯00⋮1⎠⎟⎟⎟⎞,eij={10i=ji̸=j 初始矩阵 P = E。对于矩阵 A,我们可以将它分解成这样两个矩阵的乘积(这里直接借用了 Introduction to Algorithms 里的描述) A = ( a 11 ω T ν A ′ ) = ( 1 0 ν / a 11 I n − 1 ) ( a 11 ω T 0 A ′ − ν ω T / a 11 ) \begin{matrix} A \end{matrix} = \begin{pmatrix} a_{11} ; \omega^T \\ \nu ; A' \end{pmatrix} = \begin{pmatrix} 1 ; 0 \\ \nu / a_{11} ; I_{n-1} \end{pmatrix} \begin{pmatrix} a_{11} ; \omega^T \\ 0 ; A' - \nu \omega^T / a_{11} \end{pmatrix} A=(a11νωTA′)=(1ν/a110In−1)(a110ωTA′−νωT/a11) 这样,将这个规模为 n 的问题分解为一个规模为 n - 1 的子问题,迭代之即可求得矩阵 PA 的 LU 分解。最后得到的矩阵 U 相当于高斯消元后的结果,因为求矩阵 U 的实质是用当前规模矩阵的第一行的相应倍数减其它行,将其它行首列元素消为 0。 为了避免除数为零或过小引起精度问题,在迭代到矩阵第 i 行时,我们可以找到 j 使得 ∣ A j , i ∣ \vert A_{j, i} \vert ∣Aj,i∣ 尽可能大,其中 i ≤ j ≤ n i \leq j \leq n i≤j≤n。当 i 不等于 j 时,交换矩阵 PA 的第 i 行与第 j 行,相当于矩阵 A 不变,矩阵 P 的第 i 行与第 j 行交换。 对于矩阵的 LUP 分解代码如下: #define ABS(x) \ ({ \ typeof(x) __tmp_x=(x); \ __tmp_x pi[i]=i; for(int j=1;j det=-det; EXCHANGE(pi[i],pi[p]); } for(int j=1;j double dat[MAX_N]; int raw; }; VectorColumn Matrix::LUP_Solve(VectorColumn b) { VectorColumn x; x.raw=dgr; for(int i=1;i for(int j=dgr;j>i;j--) x.dat[i]-=LUdat[i][j]*x.dat[j]; x.dat[i]/=LUdat[i][i]; } return x; }时间复杂度 O ( n 2 ) O(n^2) O(n2)。(若算上矩阵 LUP 分解,总时间复杂度 O ( n 3 ) O(n^3) O(n3)) 矩阵的行列式值求解由行列式性质可知 ∣ P ∣ ∣ A ∣ = ∣ L ∣ ∣ U ∣ \begin{vmatrix} P \end{vmatrix} \begin{vmatrix} A \end{vmatrix} = \begin{vmatrix} L \end{vmatrix} \begin{vmatrix} U \end{vmatrix} ∣∣P∣∣∣∣A∣∣=∣∣L∣∣∣∣U∣∣ 其中,L 为对角线元素均为 1 的下三角矩阵,故其行列式值为 1;U 为上三角矩阵,其行列式值为对角线元素之积;P 初始行列式值为 1,每对 P 进行一次行交换,相当于行列式值取其相反数。 在具体实现过程中,行列式值的初值为 1 或 -1 在 LUP 分解过程中决定,此部分代码见上文。 对于矩阵的行列式值求解代码如下: void Matrix::GetDeterminant() { for(int i=1;i curcol.dat[i-1]=0,curcol.dat[i]=1; revcol=LUP_Solve(curcol); for(int j=1;j |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |