当前位置:网站首页>Numerical calculation method chapter5 Direct method for solving linear equations
Numerical calculation method chapter5 Direct method for solving linear equations
2022-06-12 07:38:00 【Espresso Macchiato】
- Numerical calculation method Chapter5. A direct method for solving linear equations
0. Problem description
This chapter examines how to solve linear equations :
{ a 11 x 1 + a 12 x 2 + . . . + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + . . . + a 2 n x n = b 2 . . . a n 1 x 1 + a n 2 x 2 + . . . + a n n x n = b n \left\{ \begin{aligned} a_{11}x_1 + a_{12}x_2 + ... + a_{1n}x_n &= b_1 \\ a_{21}x_1 + a_{22}x_2 + ... + a_{2n}x_n &= b_2 \\ ... \\ a_{n1}x_1 + a_{n2}x_2 + ... + a_{nn}x_n &= b_n \end{aligned} \right. ⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧a11x1+a12x2+...+a1nxna21x1+a22x2+...+a2nxn...an1x1+an2x2+...+annxn=b1=b2=bn
Or it can be expressed as a matrix :
( a 11 a 12 . . . a 1 n a 21 a 22 . . . a 2 n . . . a n 1 a n 2 . . . a n n ) ( x 1 x 2 . . . x n ) = ( b 1 b 2 . . . b n ) \begin{pmatrix} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ ... \\ a_{n1} & a_{n2} & ... & a_{nn} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} =\begin{pmatrix} b_1 \\ b_2 \\ ... \\ b_n \end{pmatrix} ⎝⎜⎜⎛a11a21...an1a12a22an2.........a1na2nann⎠⎟⎟⎞⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞=⎝⎜⎜⎛b1b2...bn⎠⎟⎟⎞
1. Elimination method
1. Trigonometric equations
First , Let's look at some special forms of equations :
1. Diagonal equations
The functional form of diagonal equations is as follows :
( a 11 a 22 . . . a n n ) ( x 1 x 2 . . . x n ) = ( b 1 b 2 . . . b n ) \begin{pmatrix} a_{11} & & & \\ & a_{22} & & \\ & & ... & \\ & & & a_{nn} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} =\begin{pmatrix} b_1 \\ b_2 \\ ... \\ b_n \end{pmatrix} ⎝⎜⎜⎛a11a22...ann⎠⎟⎟⎞⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞=⎝⎜⎜⎛b1b2...bn⎠⎟⎟⎞
obviously , You can write directly x i = b i / a i i x_i = b_i / a_{ii} xi=bi/aii, among , i ∈ [ 1 , n ] i \in [1, n] i∈[1,n].
2. Lower trigonometric equations
below , Let's look at a slightly more complicated situation , That is, the case of lower triangular matrix :
( a 11 a 21 a 22 . . . a n 1 a n 2 . . . a n n ) ( x 1 x 2 . . . x n ) = ( b 1 b 2 . . . b n ) \begin{pmatrix} a_{11} & & & \\ a_{21} & a_{22} & & \\ & & ... & \\ a_{n1} & a_{n2} & ... & a_{nn} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} = \begin{pmatrix} b_1 \\ b_2 \\ ... \\ b_n \end{pmatrix} ⎝⎜⎜⎛a11a21an1a22an2......ann⎠⎟⎟⎞⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞=⎝⎜⎜⎛b1b2...bn⎠⎟⎟⎞
here , The difficulty of the problem will be a little more complicated , But it is also possible to give a direct answer as follows :
x i = b i − ∑ j < i a i j x j a i i x_i = \frac{b_i - \sum_{j < i}a_{ij}x_j}{a_ii} xi=aiibi−∑j<iaijxj
We can give python The pseudocode is as follows :
def down_triangle(A, b):
n = len(A)
x = [0 for _ in range(n)]
for i in range(n):
y = b[i]
for j in range(i):
y -= A[i][j] * x[j]
x[i] = y / A[i][i]
return x
3. Upper triangular equations
alike , For the case of upper trigonometric function , We also have :
( a 11 a 12 . . . a 1 n a 22 . . . a 2 n . . . a n n ) ( x 1 x 2 . . . x n ) = ( b 1 b 2 . . . b n ) \begin{pmatrix} a_{11} & a_{12} & ... & a_{1n} \\ & a_{22} & ... & a_{2n} \\ & & ... & \\ & & & a_{nn} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} =\begin{pmatrix} b_1 \\ b_2 \\ ... \\ b_n \end{pmatrix} ⎝⎜⎜⎛a11a12a22.........a1na2nann⎠⎟⎟⎞⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞=⎝⎜⎜⎛b1b2...bn⎠⎟⎟⎞
It can be solved that :
x i = b i − ∑ j > i a i j x j a i i x_i = \frac{b_i - \sum_{j > i}a_{ij}x_j}{a_ii} xi=aiibi−∑j>iaijxj
alike , We can give python The pseudocode is as follows :
def up_triangle(A, b):
n = len(A)
x = [0 for _ in range(n)]
for i in range(n-1, -1, -1):
y = b[i]
for j in range(i+1, n):
y -= A[i][j] * x[j]
x[i] = y / A[i][i]
return x
2. Gauss Elimination method
Now? , Let's look at the solution of a general form of multivariate linear equation .
The core idea is to convert it into a triangular matrix and then solve it .
We also give python The pseudocode is as follows :
def gauss_elimination(A, b):
n = len(A)
for i in range(n-1):
for k in range(i+1, n):
c = A[k][i] / A[i][i]
for j in range(i, n):
A[k][j] -= c * A[i][j]
b[k] -= c * b[i]
return up_triangle(A, b)
3. Gauss-Jordan Elimination method
Gauss-Jordan Elimination method and the above Gauss The elimination method is essentially the same , however Gauss The elimination method is to convert a general matrix into a triangular matrix , and Gauss-Jordan The elimination method is to convert a general matrix into a diagonal matrix .
alike , We give python The pseudocode is as follows :
def gauss_jordan_elimination(A, b):
n = len(A)
for i in range(n-1):
for k in range(i+1, n):
c = A[k][i] / A[i][i]
for j in range(i, n):
A[k][j] -= c * A[i][j]
b[k] -= c * b[i]
for j in range(n-1, 0, -1):
for i in range(j):
c = A[i][j] / A[j][j]
A[i][j] = 0
b[i] -= c * b[j]
res = [b[i] / A[i][i] for i in range(n)]
return res
2. Direct decomposition method
There is no essential difference between the direct decomposition method and the above elimination method , But the difference is , The core idea of the direct decomposition method is to constantly try to convert the general matrix into the form of triangular matrix and then solve it based on the specificity of triangular matrix .
1. Dolittle decompose
Dolittle The idea of decomposition is to say that the general n n n Order matrix A A A convert to A = L U A = LU A=LU, among , L L L Is a diagonal element for 1 The lower triangular matrix of , and U U U It's an upper triangular matrix .
namely :
( a 11 a 12 . . . a 1 n a 21 a 22 . . . a 2 n . . . a n 1 a n 2 . . . a n n ) = ( 1 l 21 1 . . . l n 1 l n 2 . . . 1 ) ( u 11 u 12 . . . u 1 n u 22 . . . u 2 n . . . u n n ) \begin{pmatrix} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ & & ... & \\ a_{n1} & a_{n2} & ... & a_{nn} \end{pmatrix} = \begin{pmatrix} 1 & & & \\ l_{21} & 1 & & \\ & ... & & \\ l_{n1} & l_{n2} & ... & 1 \end{pmatrix} \begin{pmatrix} u_{11} & u_{12} & ... & u_{1n} \\ & u_{22} & ... & u_{2n} \\ & & ... & \\ & & & u_{nn} \end{pmatrix} ⎝⎜⎜⎛a11a21an1a12a22an2............a1na2nann⎠⎟⎟⎞=⎝⎜⎜⎛1l21ln11...ln2...1⎠⎟⎟⎞⎝⎜⎜⎛u11u12u22.........u1nu2nunn⎠⎟⎟⎞
And two trigonometric matrices L L L and U U U Are relatively easy to solve .
We are easy to have :
{ u i j = a i j − ∑ k = 1 i − 1 l i k u k j l i j = ( a i j − ∑ k = 1 j − 1 l i k u k j ) / u j j \left\{ \begin{aligned} u_{ij} &= a_{ij} - \sum_{k=1}^{i-1}l_{ik}u_{kj} \\ l_{ij} &= (a_{ij} - \sum_{k=1}^{j-1}l_{ik}u_{kj}) / u_{jj} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧uijlij=aij−k=1∑i−1likukj=(aij−k=1∑j−1likukj)/ujj
here , For the equation to be solved A x = b Ax = b Ax=b Can become :
L U x = b LUx = b LUx=b
further , We can split it into two equations :
{ L y = b U x = y \left\{ \begin{aligned} Ly &= b \\ Ux &= y \end{aligned} \right. { LyUx=b=y
Solve the above two equations respectively , The final solution can be obtained x x x:
{ y i = b i − ∑ j = 1 i − 1 l i j y j x i = ( y i − ∑ j = i + 1 n u i j x j ) / u i i \left\{ \begin{aligned} y_i &= b_i - \sum_{j=1}^{i-1} l_{ij}y_{j} \\ x_i &= (y_{i} - \sum_{j=i+1}^{n} u_{ij}x_j) / u_{ii} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧yixi=bi−j=1∑i−1lijyj=(yi−j=i+1∑nuijxj)/uii
alike , We can give python The pseudocode is as follows :
def dolittle_solve(A, b):
n = len(A)
L = [[0 for _ in range(n)] for _ in range(n)]
U = [[0 for _ in range(n)] for _ in range(n)]
for k in range(n):
L[k][k] = 1
for j in range(k, n):
U[k][j] = A[k][j]
for r in range(k):
U[k][j] -= L[k][r] * U[r][j]
for i in range(k+1, n):
L[i][k] = A[i][k]
for r in range(k):
L[i][k] -= L[i][r] * U[r][k]
L[i][k] /= U[k][k]
y = [0 for _ in range(n)]
for i in range(n):
y[i] = b[i]
for j in range(i):
y[i] -= L[i][j] * y[j]
x = [0 for _ in range(n)]
for i in range(n-1, -1, -1):
x[i] = y[i]
for j in range(i+1, n):
x[i] -= U[i][j] * x[j]
x[i] /= U[i][i]
return x
2. Courant decompose
Courant Break down and Dolittle Decomposition is essentially indistinguishable , It is still the general parameter matrix A A A Split into two three matrices , But for the Courant In terms of decomposition , L L L Is a general lower triangular matrix , and U U U Is that the diagonal elements are all 1 The upper triangular matrix of .
namely :
( a 11 a 12 . . . a 1 n a 21 a 22 . . . a 2 n . . . a n 1 a n 2 . . . a n n ) = ( l 11 l 21 l 22 . . . l n 1 l n 2 . . . l n n ) ( 1 u 12 . . . u 1 n 1 . . . u 2 n . . . 1 ) \begin{pmatrix} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ & & ... & \\ a_{n1} & a_{n2} & ... & a_{nn} \end{pmatrix} = \begin{pmatrix} l_{11} & & & \\ l_{21} & l_{22} & & \\ & ... & & \\ l_{n1} & l_{n2} & ... & l_{nn} \end{pmatrix} \begin{pmatrix} 1 & u_{12} & ... & u_{1n} \\ & 1 & ... & u_{2n} \\ & & ... & \\ & & & 1 \end{pmatrix} ⎝⎜⎜⎛a11a21an1a12a22an2............a1na2nann⎠⎟⎟⎞=⎝⎜⎜⎛l11l21ln1l22...ln2...lnn⎠⎟⎟⎞⎝⎜⎜⎛1u121.........u1nu2n1⎠⎟⎟⎞
here , In imitation, we will soon get L L L and U U U The expression of is as follows :
{ l i j = a i k − ∑ r = 1 k − 1 l i r u r k u i j = ( a k j − ∑ r = 1 k − 1 l k r u r j ) / l k k \left\{ \begin{aligned} l_{ij} &= a_{ik} - \sum_{r=1}^{k-1} l_{ir}u_{rk} \\ u_{ij} &= (a_{kj} - \sum_{r=1}^{k-1}l_{kr}u_{rj}) / l_{kk} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧lijuij=aik−r=1∑k−1lirurk=(akj−r=1∑k−1lkrurj)/lkk
alike , We can determine the final solution by the following function x x x To solve the :
{ L y = b U x = y \left\{ \begin{aligned} Ly &= b \\ Ux &= y \end{aligned} \right. { LyUx=b=y
The final result expression is as follows :
{ y i = ( b i − ∑ j = 1 i − 1 l i j y j ) / l i i x i = y i − ∑ j = i + 1 n u i j x j \left\{ \begin{aligned} y_i &= (b_i - \sum_{j=1}^{i-1} l_{ij}y_{j}) / l_{ii} \\ x_i &= y_{i} - \sum_{j=i+1}^{n} u_{ij}x_j \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧yixi=(bi−j=1∑i−1lijyj)/lii=yi−j=i+1∑nuijxj
Also given python The pseudo code is implemented as follows :
def courant_solve(A, b):
n = len(A)
L = [[0 for _ in range(n)] for _ in range(n)]
U = [[0 for _ in range(n)] for _ in range(n)]
for k in range(n):
for i in range(k, n):
L[i][k] = A[i][k]
for r in range(k):
L[i][k] -= L[i][r] * U[r][k]
U[k][k] = 1
for j in range(k+1, n):
U[k][j] = A[k][j]
for r in range(k):
U[k][j] -= L[k][r] * U[r][j]
U[k][j] /= L[k][k]
y = [0 for _ in range(n)]
for i in range(n):
y[i] = b[i]
for j in range(i):
y[i] -= L[i][j] * y[j]
y[i] /= L[i][i]
x = [0 for _ in range(n)]
for i in range(n-1, -1, -1):
x[i] = y[i]
for j in range(i+1, n):
x[i] -= U[i][j] * x[j]
return x
3. Catch up method
The catch-up method is essentially just Courant A special case of decomposition .
Its solution and Courant Decomposition is completely consistent , The only difference is , there A A A Matrix is a special ternary matrix , namely :
A = ( a 1 b 1 c 2 a 2 b 2 . . . c n − 1 a n − 1 b n − 1 c n a n ) A = \begin{pmatrix} a_1 & b_1 \\ c_2 & a_2 & b_2 \\ & & ... & \\ & & c_{n-1} & a_{n-1} & b_{n-1} \\ & & & c_{n} & a_{n} \end{pmatrix} A=⎝⎜⎜⎜⎜⎛a1c2b1a2b2...cn−1an−1cnbn−1an⎠⎟⎟⎟⎟⎞
thus , Expand the matrix L L L and U U U It can also be synchronously modified to :
L = ( u 1 w 2 u 2 . . . . . . w n u n ) , U = ( 1 v 1 1 v 2 . . . 1 ) L = \begin{pmatrix} u_1 \\ w_2 & u_2 \\ & ... & ... \\ & & w_n & u_n \end{pmatrix} , U = \begin{pmatrix} 1 & v_1\\ & 1 & v_2\\ & & ... \\ & & & 1 \end{pmatrix} L=⎝⎜⎜⎛u1w2u2......wnun⎠⎟⎟⎞,U=⎝⎜⎜⎛1v11v2...1⎠⎟⎟⎞
here , Because of the matrix L L L and U U U The particularity of , We can directly write the final solution as follows :
{ u i = a i − c i v i − 1 v i = b i / u i y i = ( f i − c i y i − 1 ) / u i x i = ( y i − v i x i + 1 ) \left\{ \begin{aligned} u_i &= a_i - c_iv_{i-1} \\ v_i &= b_i / u_i \\ y_i &= (f_i - c_i y_{i-1}) / u_i \\ x_i &= (y_i - v_i x_{i+1}) \end{aligned} \right. ⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧uiviyixi=ai−civi−1=bi/ui=(fi−ciyi−1)/ui=(yi−vixi+1)
4. Symmetric positive definite matrix L D L T LDL^T LDLT decompose
alike , L D L T LDL^T LDLT Decomposition and the above Dolittle Break down and Courant Decomposition is essentially the same , But here's the difference , The problem here is Symmetric positive definite matrices .
therefore , For symmetric positive definite matrices , We can always have :
A = L D L T = ( 1 l 21 1 . . . l n 1 l n 2 . . . 1 ) ( d 1 d 2 . . . d n ) ( 1 l 12 . . . l 1 n 1 . . . l 2 n . . . 1 ) \begin{aligned} A &= LDL^T \\ &= \begin{pmatrix} 1 \\ l_{21} & 1 \\ & ... \\ l_{n1} & l_{n2} & ... & 1 \end{pmatrix} \begin{pmatrix} d_1 \\ & d_2 \\ & & ... \\ & & & d_n \end{pmatrix} \begin{pmatrix} 1 & l_{12} & ... & l_{1n}\\ & 1 & ... & l_{2n}\\ & & ... \\ & & & 1 \end{pmatrix} \end{aligned} A=LDLT=⎝⎜⎜⎛1l21ln11...ln2...1⎠⎟⎟⎞⎝⎜⎜⎛d1d2...dn⎠⎟⎟⎞⎝⎜⎜⎛1l121.........l1nl2n1⎠⎟⎟⎞
here , We can solve :
{ d k = a k k − ∑ r = 1 k − 1 d r l k r 2 l i k = ( a i k − ∑ r = 1 k − 1 d r l i r l k r ) / d k \left\{ \begin{aligned} d_k &= a_{kk} - \sum_{r=1}^{k-1}d_rl_{kr}^2\\ l_{ik} &= (a_{ik} - \sum_{r=1}^{k-1}d_rl_{ir}l_{kr}) / d_k \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧dklik=akk−r=1∑k−1drlkr2=(aik−r=1∑k−1drlirlkr)/dk
, in turn, , We can put the equation A x = L D L T x = b Ax = LDL^Tx = b Ax=LDLTx=b Disassemble into three steps to complete :
{ L z = b D y = z L T x = y \left\{ \begin{aligned} Lz &= b \\ Dy &= z \\ L^T x &= y \end{aligned} \right. ⎩⎪⎨⎪⎧LzDyLTx=b=z=y
Solution :
{ z i = b i − ∑ j = 1 i − 1 l i j z j y i = z i / d i x i = y i − ∑ j = i + 1 n l j i x j \left\{ \begin{aligned} z_i &= b_i - \sum_{j=1}^{i-1}l_{ij}z_{j} \\ y_i &= z_i / d_i \\ x_i &= y_i - \sum_{j=i+1}^{n}l_{ji}x_j \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧ziyixi=bi−j=1∑i−1lijzj=zi/di=yi−j=i+1∑nljixj
Again , We give python The pseudocode is as follows :
def ldl_solve(A, b):
n = len(A)
l = [[0 for _ in range(n)] for _ in range(n)]
d = [0 for _ in range(n)]
for k in range(n):
d[k] = A[k][k]
for r in range(k):
d[k] -= d[r] * l[k][r] * l[k][r]
l[k][k] = 1
for i in range(k+1, n):
l[i][k] = A[i][k]
for r in range(k):
l[i][k] -= d[r] * l[i][r] * l[k][r]
l[i][k] /= d[k]
z = [0 for _ in range(n)]
for i in range(n):
z[i] = b[i]
for j in range(i):
z[i] -= l[i][j] * z[j]
y = [z[i]/d[i] for i in range(n)]
x = [0 for _ in range(n)]
for i in range(n-1, -1, -1):
x[i] = y[i]
for j in range(i+1, n):
x[i] -= l[j][i] * x[j]
return x
边栏推荐
- Personalized federated learning with Moreau envelopes
- ECMAScript6面试题
- Voice assistant - those classification models used in the assistant
- Use case design of software testing interview questions
- 谋新局、促发展,桂林绿色数字经济的头雁效应
- Voice assistant - potential skills and uncalled call technique mining
- 2022年G3锅炉水处理复训题库及答案
- Chapter V - message authentication and digital signature
- VS2019 MFC IP Address Control 控件继承CIPAddressCtrl类重绘
- 20220524 深度学习技术点
猜你喜欢

Non IID data and continuous learning processes in federated learning: a long road ahead

Logistic regression

Design an open source continuous deployment pipeline based on requirements

Personalized federated learning using hypernetworks paper reading notes + code interpretation

鸿蒙os-第一次培训

Complete set of typescript Basics

Personalized federated learning with exact stochastic gradient descent

Chapter 2 - cyber threats and attacks

Learning to continuously learn paper notes + code interpretation

Modelants II
随机推荐
最新hbuilderX编辑uni-app项目运行于夜神模拟器
[yolo-v5 learning notes]
Voice assistant - potential skills and uncalled call technique mining
Bi skills - beginning of the month
Modelants II
解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method
Right click the general solution of file rotation jam, refresh, white screen, flash back and desktop crash
Summary of machine learning + pattern recognition learning (IV) -- decision tree
Exploring shared representations for personalized federated learning paper notes + code interpretation
20220525 RCNN--->Faster RCNN
Utilize user behavior data
Learning to continuously learn paper notes + code interpretation
2022年G3锅炉水处理复训题库及答案
C language sizeof strlen
The function of C language string Terminator
LeetCode笔记:Weekly Contest 295
AI狂想|来这场大会,一起盘盘 AI 的新工具!
初步认知Next.js中ISR/RSC/Edge Runtime/Streaming等新概念
2022r2 mobile pressure vessel filling test question simulation test platform operation